MOAB: Mesh Oriented datABase  (version 5.4.1)
MBiMesh.hpp
Go to the documentation of this file.
00001 #ifndef MBIMESH_HPP
00002 #define MBIMESH_HPP
00003 
00004 #include "moab/Core.hpp"
00005 #include <vector>
00006 #include <algorithm>
00007 #include <cstring>
00008 
00009 using namespace moab;
00010 
00011 /* map from MOAB's ErrorCode to tstt's */
00012 extern "C" const iBase_ErrorType iBase_ERROR_MAP[MB_FAILURE + 1];
00013 
00014 class MBiMesh
00015 {
00016   private:
00017     bool haveDeletedEntities;
00018     bool iCreatedInterface;
00019     std::vector< Tag > setHandleTags, entHandleTags;
00020 
00021   public:
00022     MBiMesh( moab::Interface* mbImpl = NULL );
00023 
00024     virtual ~MBiMesh();
00025     bool have_deleted_ents( bool reset )
00026     {
00027         bool result = haveDeletedEntities;
00028         if( reset ) haveDeletedEntities = false;
00029         return result;
00030     }
00031 
00032     virtual ErrorCode delete_mesh();
00033     virtual ErrorCode delete_entities( const EntityHandle*, const int );
00034     virtual ErrorCode delete_entities( const Range& );
00035     iBase_AdjacencyCost AdjTable[16];
00036     moab::Interface* mbImpl;
00037     int lastErrorType;
00038     char lastErrorDescription[120];
00039 
00040     inline void note_set_handle_tag( Tag );
00041     inline void note_ent_handle_tag( Tag );
00042     inline void note_tag_destroyed( Tag );
00043     inline bool is_set_handle_tag( Tag ) const;
00044     inline bool is_ent_handle_tag( Tag ) const;
00045 
00046     inline int set_last_error( int, const char* );
00047     inline int set_last_error( ErrorCode, const char* );
00048 };
00049 
00050 static inline MBiMesh* mbimeshi_instance( iMesh_Instance instance )
00051 {
00052     return reinterpret_cast< MBiMesh* >( instance );
00053 }
00054 #define MBIMESHI mbimeshi_instance( instance )
00055 #define MOABI    MBIMESHI->mbImpl
00056 
00057 inline MBiMesh::MBiMesh( Interface* impl )
00058     : haveDeletedEntities( false ), iCreatedInterface( false ), mbImpl( impl ), lastErrorType( iBase_SUCCESS )
00059 {
00060     lastErrorDescription[0] = '\0';
00061 
00062     iBase_AdjacencyCost tmp_table[] = { iBase_ALL_ORDER_1,     iBase_SOME_ORDER_1,    iBase_SOME_ORDER_1,
00063                                         iBase_ALL_ORDER_1,     iBase_ALL_ORDER_1,     iBase_UNAVAILABLE,
00064                                         iBase_SOME_ORDER_LOGN, iBase_SOME_ORDER_LOGN, iBase_ALL_ORDER_1,
00065                                         iBase_SOME_ORDER_LOGN, iBase_UNAVAILABLE,     iBase_SOME_ORDER_LOGN,
00066                                         iBase_ALL_ORDER_1,     iBase_SOME_ORDER_LOGN, iBase_SOME_ORDER_LOGN,
00067                                         iBase_ALL_ORDER_1 };
00068     memcpy( AdjTable, tmp_table, 16 * sizeof( iBase_AdjacencyCost ) );
00069 
00070     if( !mbImpl )
00071     {
00072         mbImpl            = new Core();
00073         iCreatedInterface = true;
00074     }
00075 }
00076 
00077 inline MBiMesh::~MBiMesh()
00078 {
00079     if( iCreatedInterface ) delete mbImpl;
00080 }
00081 
00082 inline ErrorCode MBiMesh::delete_mesh()
00083 {
00084     haveDeletedEntities = true;
00085     return mbImpl->delete_mesh();
00086 }
00087 
00088 inline ErrorCode MBiMesh::delete_entities( const EntityHandle* a, const int n )
00089 {
00090     if( n > 0 ) haveDeletedEntities = true;
00091     return mbImpl->delete_entities( a, n );
00092 }
00093 
00094 inline ErrorCode MBiMesh::delete_entities( const Range& r )
00095 {
00096     if( !r.empty() ) haveDeletedEntities = true;
00097     return mbImpl->delete_entities( r );
00098 }
00099 
00100 void MBiMesh::note_set_handle_tag( Tag t )
00101 {
00102     std::vector< Tag >::iterator i;
00103     i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
00104     if( i != entHandleTags.end() && *i == t ) entHandleTags.erase( i );
00105     i = std::lower_bound( setHandleTags.begin(), setHandleTags.end(), t );
00106     if( i == setHandleTags.end() || *i != t ) setHandleTags.insert( i, t );
00107 }
00108 
00109 void MBiMesh::note_ent_handle_tag( Tag t )
00110 {
00111     std::vector< Tag >::iterator i;
00112     i = std::lower_bound( setHandleTags.begin(), setHandleTags.end(), t );
00113     if( i != setHandleTags.end() && *i == t ) setHandleTags.erase( i );
00114     i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
00115     if( i == entHandleTags.end() || *i != t ) entHandleTags.insert( i, t );
00116 }
00117 
00118 void MBiMesh::note_tag_destroyed( Tag t )
00119 {
00120     std::vector< Tag >::iterator i;
00121     i = std::lower_bound( setHandleTags.begin(), setHandleTags.end(), t );
00122     if( i != setHandleTags.end() && *i == t ) setHandleTags.erase( i );
00123     i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
00124     if( i != entHandleTags.end() && *i == t ) entHandleTags.erase( i );
00125 }
00126 
00127 bool MBiMesh::is_set_handle_tag( Tag t ) const
00128 {
00129     return std::binary_search( setHandleTags.begin(), setHandleTags.end(), t );
00130 }
00131 
00132 bool MBiMesh::is_ent_handle_tag( Tag t ) const
00133 {
00134     return std::binary_search( entHandleTags.begin(), entHandleTags.end(), t );
00135 }
00136 
00137 int MBiMesh::set_last_error( int code, const char* msg )
00138 {
00139     std::strncpy( lastErrorDescription, msg, sizeof( lastErrorDescription ) );
00140     lastErrorDescription[sizeof( lastErrorDescription ) - 1] = '\0';
00141     return ( lastErrorType = static_cast< iBase_ErrorType >( code ) );
00142 }
00143 
00144 int MBiMesh::set_last_error( ErrorCode code, const char* msg )
00145 {
00146     std::string message( msg );
00147     message += "  (MOAB Error Code: ";
00148     message += mbImpl->get_error_string( code );
00149     message += ")";
00150     return set_last_error( iBase_ERROR_MAP[code], message.c_str() );
00151 }
00152 
00153 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines