moab
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 public:
00021   MBiMesh(moab::Interface *mbImpl = NULL);
00022 
00023   virtual ~MBiMesh();
00024   bool have_deleted_ents( bool reset ) {
00025     bool result = haveDeletedEntities;
00026     if (reset)
00027       haveDeletedEntities = false;
00028     return result;
00029   }
00030 
00031   virtual ErrorCode delete_mesh();
00032   virtual ErrorCode delete_entities( const EntityHandle*, const int );
00033   virtual ErrorCode delete_entities( const Range& );
00034   iBase_AdjacencyCost AdjTable[16];
00035   moab::Interface *mbImpl;
00036   int lastErrorType;
00037   char lastErrorDescription[120];
00038   
00039   inline void note_set_handle_tag( Tag );
00040   inline void note_ent_handle_tag( Tag );
00041   inline void note_tag_destroyed( Tag );
00042   inline bool is_set_handle_tag( Tag ) const;
00043   inline bool is_ent_handle_tag( Tag ) const;
00044 
00045   inline int set_last_error( int, const char* );
00046   inline int set_last_error( ErrorCode, const char* );
00047 };
00048 
00049 static inline MBiMesh *mbimeshi_instance(iMesh_Instance instance) {return reinterpret_cast<MBiMesh*>(instance);}
00050 #define MBIMESHI mbimeshi_instance(instance)
00051 #define MOABI MBIMESHI->mbImpl
00052 
00053 inline MBiMesh::MBiMesh(Interface *impl)
00054   : haveDeletedEntities(false), iCreatedInterface(false), mbImpl(impl),
00055     lastErrorType(iBase_SUCCESS)
00056 {
00057   lastErrorDescription[0] = '\0';
00058 
00059   iBase_AdjacencyCost tmp_table[] = {
00060       iBase_ALL_ORDER_1, iBase_SOME_ORDER_1,    iBase_SOME_ORDER_1,    iBase_ALL_ORDER_1,
00061       iBase_ALL_ORDER_1, iBase_UNAVAILABLE,     iBase_SOME_ORDER_LOGN, iBase_SOME_ORDER_LOGN,
00062       iBase_ALL_ORDER_1, iBase_SOME_ORDER_LOGN, iBase_UNAVAILABLE,     iBase_SOME_ORDER_LOGN,
00063       iBase_ALL_ORDER_1, iBase_SOME_ORDER_LOGN, iBase_SOME_ORDER_LOGN, iBase_ALL_ORDER_1
00064   };
00065   memcpy(AdjTable, tmp_table, 16*sizeof(iBase_AdjacencyCost));
00066 
00067   if (!mbImpl) {
00068     mbImpl = new Core();
00069     iCreatedInterface = true;
00070   }
00071 }
00072 
00073 inline MBiMesh::~MBiMesh() 
00074 {
00075   if (iCreatedInterface) delete mbImpl;
00076 }
00077 
00078 inline ErrorCode MBiMesh::delete_mesh() {
00079   haveDeletedEntities = true;
00080   return mbImpl->delete_mesh();
00081 }
00082 
00083 inline ErrorCode MBiMesh::delete_entities( const EntityHandle* a, const int n )
00084 {
00085   if (n > 0)
00086     haveDeletedEntities = true;
00087   return mbImpl->delete_entities( a, n );
00088 }
00089 
00090 inline ErrorCode MBiMesh::delete_entities( const Range& r )
00091 {
00092   if (!r.empty())
00093     haveDeletedEntities = true;
00094   return mbImpl->delete_entities( r );
00095 }
00096 
00097 
00098 void MBiMesh::note_set_handle_tag( Tag t )
00099 {
00100   std::vector<Tag>::iterator i;
00101   i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
00102   if (i != entHandleTags.end() && *i == t)
00103     entHandleTags.erase(i);
00104   i = std::lower_bound( setHandleTags.begin(), setHandleTags.end(), t );
00105   if (i == setHandleTags.end() || *i != t)
00106     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)
00114     setHandleTags.erase(i);
00115   i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
00116   if (i == entHandleTags.end() || *i != t)
00117     entHandleTags.insert( i, t );
00118 }
00119 
00120 void MBiMesh::note_tag_destroyed( Tag t )
00121 {
00122   std::vector<Tag>::iterator i;
00123   i = std::lower_bound( setHandleTags.begin(), setHandleTags.end(), t );
00124   if (i != setHandleTags.end() && *i == t)
00125     setHandleTags.erase(i);
00126   i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
00127   if (i != entHandleTags.end() && *i == t)
00128     entHandleTags.erase(i);
00129 }
00130 
00131 bool MBiMesh::is_set_handle_tag( Tag t ) const
00132 {
00133   return std::binary_search( setHandleTags.begin(), setHandleTags.end(), t );
00134 }
00135 
00136 bool MBiMesh::is_ent_handle_tag( Tag t ) const
00137 {
00138   return std::binary_search( entHandleTags.begin(), entHandleTags.end(), t );
00139 }
00140 
00141 int MBiMesh::set_last_error( int code, const char* msg )
00142 {
00143   std::strncpy( lastErrorDescription, msg, sizeof(lastErrorDescription) );
00144   lastErrorDescription[sizeof(lastErrorDescription)-1] = '\0';
00145   return (lastErrorType = static_cast<iBase_ErrorType>(code));
00146 }
00147 
00148 int MBiMesh::set_last_error( ErrorCode code, const char* msg )
00149 {
00150   std::string message(msg);
00151   message += "  (MOAB Error Code: ";
00152   message += mbImpl->get_error_string(code);
00153   message += ")";
00154   return set_last_error( iBase_ERROR_MAP[code], message.c_str() );
00155 }
00156 
00157 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines