MeshKit
1.0
|
00001 #include "meshkit/JaalMoabConverter.hpp" 00002 00003 iBase_EntityHandle 00004 JaalMoabConverter :: new_MOAB_Handle(iMesh_Instance imesh, Jaal::Vertex *vertex) 00005 { 00006 int err; 00007 iBase_EntityHandle newHandle; 00008 00009 std::map<PNode, iBase_EntityHandle> ::iterator it; 00010 it = moabnode.find(vertex); 00011 00012 if( it != moabnode.end() ) { 00013 newHandle = it->second; 00014 iMesh_deleteEnt(imesh, newHandle, &err); 00015 assert( !err ); 00016 } 00017 00018 Point3D p = vertex->getXYZCoords(); 00019 iMesh_createVtx(imesh, p[0], p[1], p[2], &newHandle, &err); 00020 assert( !err ); 00021 00022 moabnode[vertex] = newHandle; 00023 jaalnode[newHandle] = vertex; 00024 return newHandle; 00025 } 00026 00027 00029 00030 iBase_EntityHandle 00031 JaalMoabConverter ::new_MOAB_Handle(iMesh_Instance imesh, Face *face) 00032 { 00033 int status, err; 00034 00035 iBase_EntityHandle newHandle; 00036 00037 std::map<PFace, iBase_EntityHandle> ::iterator fit; 00038 fit = moabface.find(face); 00039 00040 if( fit != moabface.end() ) { 00041 newHandle = fit->second; 00042 iMesh_deleteEnt(imesh, newHandle, &err); 00043 assert(!err); 00044 } 00045 00046 vector<iBase_EntityHandle> connect; 00047 00048 int nnodes = face->getSize(0); 00049 connect.resize(nnodes); 00050 00051 std::map<PNode, iBase_EntityHandle> ::iterator nit; 00052 for (int j = 0; j < nnodes; j++) { 00053 Vertex *v = face->getNodeAt(j); 00054 nit = moabnode.find( v ); 00055 assert( nit != moabnode.end() ); 00056 connect[j] = nit->second; 00057 } 00058 00059 switch (nnodes) { 00060 case 3: 00061 iMesh_createEnt(imesh, iMesh_TRIANGLE, &connect[0], nnodes, &newHandle, 00062 &status, &err); 00063 assert(!err); 00064 break; 00065 case 4: 00066 iMesh_createEnt(imesh, iMesh_QUADRILATERAL, &connect[0], nnodes, 00067 &newHandle, &status, &err); 00068 assert(!err); 00069 break; 00070 default: 00071 iMesh_createEnt(imesh, iMesh_POLYGON, &connect[0], nnodes, &newHandle, 00072 &status, &err); 00073 assert(!err); 00074 break; 00075 } 00076 00077 moabface[face] = newHandle; 00078 jaalface[newHandle] = face; 00079 00080 return newHandle; 00081 } 00082 00084 00085 int 00086 JaalMoabConverter::toMOAB(Jaal::Mesh *jmesh, iMesh_Instance &imesh, iBase_EntitySetHandle entitySet) 00087 { 00088 int err; 00089 00090 assert( jmesh->isPruned() ); 00091 00092 if (imesh == 0) { 00093 int optlen = 0; 00094 char *options = NULL; 00095 iMesh_newMesh(options, &imesh, &err, optlen); 00096 assert(!err); 00097 } 00098 00099 iBase_EntityHandle newHandle; 00100 std::map<PNode, iBase_EntityHandle> ::const_iterator niter; 00101 00102 size_t numnodes = jmesh->getSize(0); 00103 00104 size_t ncount0 = 0, ncount1 = 0; 00105 for (size_t i = 0; i < numnodes; i++) { 00106 Vertex *v = jmesh->getNodeAt(i); 00107 niter = moabnode.find(v); 00108 if( niter == moabnode.end() ) { 00109 ncount0++; 00110 newHandle = new_MOAB_Handle(imesh, v); 00111 if (entitySet) { 00112 iMesh_addEntToSet(imesh, newHandle, entitySet, &err); 00113 assert( !err ); 00114 } 00115 } else { 00116 ncount1++; 00117 newHandle = niter->second; 00118 Point3D p3d = v->getXYZCoords(); 00119 iMesh_setVtxCoord(imesh, newHandle, p3d[0], p3d[1], p3d[2], &err); 00120 assert( !err ); 00121 } 00122 } 00123 00124 size_t numfaces = jmesh->getSize(2); 00125 for (size_t i = 0; i < numfaces; i++) { 00126 Face *f = jmesh->getFaceAt(i); 00127 newHandle = new_MOAB_Handle(imesh, f); 00128 00129 if (entitySet) { 00130 iMesh_addEntToSet(imesh, newHandle, entitySet, &err); 00131 assert( !err ); 00132 } 00133 } 00134 return 0; 00135 } 00136 00138 00139 Jaal::Mesh* 00140 JaalMoabConverter ::fromMOAB(iMesh_Instance imesh, Jaal::Mesh *m, iBase_EntitySetHandle entitySet) 00141 { 00142 jmesh = m; 00143 if( jmesh == NULL ) jmesh = new Jaal::Mesh; 00144 00145 int err; 00146 00147 if (entitySet == 0) 00148 iMesh_getRootSet(imesh, &entitySet, &err); 00149 00150 SimpleArray<iBase_EntityHandle> tfaceHandles; 00151 iMesh_getEntities(imesh, entitySet, iBase_FACE, iMesh_TRIANGLE, ARRAY_INOUT(tfaceHandles), &err); 00152 00153 size_t numFaces= tfaceHandles.size(); 00154 00155 SimpleArray<iBase_EntityHandle> facenodes; 00156 00157 jaalnode.clear(); 00158 for( int i = 0; i < (int) numFaces; i++) { 00159 iMesh_getEntAdj(imesh, tfaceHandles[i], iBase_VERTEX, ARRAY_INOUT(facenodes), &err); 00160 for (int j = 0; j < 3; j++) 00161 jaalnode[ facenodes[j] ] = NULL; 00162 } 00163 00164 size_t numNodes = jaalnode.size(); 00165 00166 jmesh->reserve(numNodes, 0); 00167 00168 Point3D p3d; 00169 double x, y, z; 00170 00171 std::map<iBase_EntityHandle, PNode> ::const_iterator niter; 00172 Vertex *jnode; 00173 00174 size_t id = 0; 00175 for( niter = jaalnode.begin(); niter != jaalnode.end(); ++niter) { 00176 iBase_EntityHandle nhandle = niter->first; 00177 iMesh_getVtxCoord(imesh, nhandle, &x, &y, &z, &err); 00178 p3d[0] = x; 00179 p3d[1] = y; 00180 p3d[2] = z; 00181 jnode = Jaal::Vertex::newObject(); 00182 jnode->setXYZCoords(p3d); 00183 jnode->setID(id++); 00184 jmesh->addNode(jnode); 00185 moabnode[jnode] = nhandle; 00186 jaalnode[ nhandle ] = jnode; 00187 } 00188 00189 jmesh->reserve(numFaces, 2); 00190 00191 NodeSequence connect(3); 00192 00193 for (size_t i = 0; i < numFaces; i++) { 00194 SimpleArray<iBase_EntityHandle> facenodes; 00195 iMesh_getEntAdj(imesh, tfaceHandles[i], iBase_VERTEX, ARRAY_INOUT(facenodes), &err); 00196 for (int j = 0; j < 3; j++) 00197 connect[j] = jaalnode[facenodes[j]]; 00198 Face *face = Face::newObject();; 00199 face->setNodes(connect); 00200 jmesh->addFace(face); 00201 jaalface[tfaceHandles[i] ] = face; 00202 moabface[face] = tfaceHandles[i]; 00203 } 00204 00205 return jmesh; 00206 }