MeshKit  1.0
test_facetmeshreader.cpp
Go to the documentation of this file.
00001 
00007 #include "meshkit/MKCore.hpp"
00008 #include "meshkit/SurfaceFacetMeshReader.hpp"
00009 #include "meshkit/CurveFacetMeshReader.hpp"
00010 #include "meshkit/ModelEnt.hpp"
00011 
00012 
00013 using namespace MeshKit;
00014 
00015 #include "TestUtil.hpp"
00016 
00017 MKCore *mk = NULL;
00018 
00019 #ifdef HAVE_ACIS
00020 std::string extension = ".sat";
00021 #elif HAVE_OCC
00022 std::string extension = ".stp";
00023 #endif
00024 
00025 // Basic Tests
00026 void read_cube_tris_test();
00027 void read_cube_curves_test();
00028 void read_cube_surfs_test();
00029 void read_cube_vols_test();
00030 void read_cube_vertex_pos_test();
00031 
00032 //Connectivity Tests
00033 void cube_verts_connectivity_test();
00034 void cube_tris_connectivity_test();
00035 void cube_tri_curve_coincidence_test();
00036 void cube_edge_adjacencies_test(); 
00037 void cube_tri_verts_test(); 
00038 
00039 //Other functions
00040 int count_topo_for_dim( int dim, iMesh::EntityTopology topo);
00041 void match_tri_edges_w_curves( std::vector<iMesh::EntityHandle> edges, MEntVector curves);
00042 
00043 int main(int argc, char **argv) 
00044 {
00045   
00046   // start up MK and load the geometry
00047   mk = new MKCore();
00048 
00049   std::string filename = "cube" ;
00050 
00051   filename = TestDir + "/" + filename + extension;
00052 
00053   mk->load_geometry(&filename[0]);
00054 
00055   MEntVector surfs;
00056   mk->get_entities_by_dimension(2,surfs);
00057   SurfaceFacetMeshReader *sfmr;
00058 
00059   sfmr = (SurfaceFacetMeshReader*) mk->construct_meshop("SurfaceFacetMeshReader", surfs);
00060 
00061   double facet_tol = 1e-04, geom_resabs = 1e-06;
00062   sfmr->set_mesh_params(facet_tol, geom_resabs);
00063 
00064   mk->setup();
00065   mk->execute();
00066 
00067 
00068   //RUN TESTS
00069   int num_fail = 0;
00070   num_fail += RUN_TEST(read_cube_tris_test);
00071   num_fail += RUN_TEST(read_cube_curves_test);
00072   num_fail += RUN_TEST(read_cube_surfs_test);
00073   num_fail += RUN_TEST(read_cube_vols_test);
00074   num_fail += RUN_TEST(read_cube_vertex_pos_test);
00075   num_fail += RUN_TEST(cube_verts_connectivity_test);
00076   num_fail += RUN_TEST(cube_tris_connectivity_test);
00077   num_fail += RUN_TEST(cube_edge_adjacencies_test); 
00078   num_fail += RUN_TEST(cube_tri_verts_test); 
00079 
00080   return num_fail;
00081 }
00082 
00083 
00084 //Tests
00085 // NOTE: all tests should be performed using the iMesh interface as that is where our faceted
00086 //       information lives. It can, however, be compared to the iGeom information if desired.
00087 
00088 
00089 void read_cube_tris_test()
00090 {
00091   
00092   // get the number of tris in the surface model ents
00093   int num_of_tris = count_topo_for_dim( 2, iMesh_TRIANGLE);
00094 
00095   //For a cube, there should be exactly 2 triangles per face, totaling 12 for the cube.
00096   CHECK_EQUAL(12, num_of_tris);
00097 
00098 }
00099 
00100 void read_cube_curves_test()
00101 {
00102   
00103   MEntVector ents;
00104   
00105   // there should be 12 curve (dim==1) ModelEnts for a cube
00106   mk->get_entities_by_dimension(1,ents);
00107 
00108   int num_of_curves = ents.size();
00109 
00110   CHECK_EQUAL(12, num_of_curves);
00111 }
00112 
00113 void read_cube_surfs_test()
00114 {
00115   
00116   MEntVector ents;
00117   
00118   // there should be 6 surf (dim==1) ModelEnts for a cube
00119   mk->get_entities_by_dimension(2,ents);
00120 
00121   int num_of_surfs = ents.size();
00122 
00123   CHECK_EQUAL(6, num_of_surfs);
00124 }
00125 
00126 
00127 void read_cube_vols_test()
00128 {
00129   
00130   MEntVector ents;
00131   
00132   // there should be 1 vol (dim==1) ModelEnts for a cube
00133   mk->get_entities_by_dimension(3,ents);
00134 
00135   int num_of_surfs = ents.size();
00136 
00137   CHECK_EQUAL(1, num_of_surfs);
00138 }
00139 
00140 void read_cube_vertex_pos_test()
00141 {
00142 
00143   MEntVector ents;
00144 
00145   mk->get_entities_by_dimension(0, ents);
00146 
00147   int num_of_verts = ents.size();
00148 
00149   // should be 8 vertex model ents for the cube
00150   CHECK_EQUAL(8, num_of_verts);
00151 
00152   // get the vertex coordinates
00153   double x[8];
00154   double y[8];
00155   double z[8];
00156 
00157   for(unsigned int i = 0; i < ents.size(); i++)
00158     {
00159       //get the iMesh EntityHandle for the vert
00160       iMesh::EntitySetHandle sh = IBSH(ents[i]->mesh_handle());
00161 
00162       std::vector<iMesh::EntityHandle> vert;
00163       mk->imesh_instance()->getEntities(sh, iBase_ALL_TYPES, iMesh_ALL_TOPOLOGIES, vert);
00164 
00165       int vert_size = vert.size();
00166       // there should only be one entity in this set. If not, there is a problem.
00167       assert(1 == vert_size);
00168   
00169       //now get the vertex coords      
00170       mk->imesh_instance()->getVtxCoord(vert[0], x[i], y[i], z[i]);
00171     }
00172 
00173   //Check against known locations of the vertices
00174 
00175   std::vector<double> x_ref;
00176   std::vector<double> y_ref;
00177   std::vector<double> z_ref;
00178 
00179   // Vertex 1
00180   x_ref.push_back( 5 );
00181   y_ref.push_back( -5 );
00182   z_ref.push_back( 5 );
00183 
00184   // Vertex 2
00185   x_ref.push_back( 5 );
00186   y_ref.push_back( 5 );
00187   z_ref.push_back( 5 );
00188 
00189   // Vertex 3
00190   x_ref.push_back( -5 );
00191   y_ref.push_back( 5 );
00192   z_ref.push_back( 5 );
00193 
00194   // Vertex 4
00195   x_ref.push_back( -5 );
00196   y_ref.push_back( -5 );
00197   z_ref.push_back( 5 );
00198 
00199   // Vertex 5
00200   x_ref.push_back( 5 );
00201   y_ref.push_back( 5 );
00202   z_ref.push_back( -5 );
00203 
00204   // Vertex 6
00205   x_ref.push_back( 5 );
00206   y_ref.push_back( -5 );
00207   z_ref.push_back( -5 );
00208 
00209   // Vertex 7
00210   x_ref.push_back( -5 );
00211   y_ref.push_back( -5 );
00212   z_ref.push_back( -5 );
00213  
00214   // Vertex 8
00215   x_ref.push_back( -5 );
00216   y_ref.push_back( 5 );
00217   z_ref.push_back( -5 );
00218 
00219   
00220   for(unsigned int i=0; i<ents.size(); i++)
00221     {
00222       for(unsigned int j=0; j<x_ref.size(); j++)
00223         {
00224           if( x[i]==x_ref[j] && y[i]==y_ref[j] && z[i]==z_ref[j] )
00225             {
00226               x_ref.erase( x_ref.begin()+j );
00227               y_ref.erase( y_ref.begin()+j );
00228               z_ref.erase( z_ref.begin()+j );
00229             }
00230         }
00231     }
00232   
00233   //After looping through each vertex loaded from the mesh
00234   //there should be no entities left in the reference vector
00235   int leftovers = x_ref.size();
00236   CHECK_EQUAL( 0, leftovers );
00237 
00238 }
00239 
00240 void cube_verts_connectivity_test()
00241 {
00242 
00243   MEntVector verts;
00244   mk->get_entities_by_dimension(0, verts);
00245 
00246   MEntVector::iterator i;
00247   for( i = verts.begin(); i != verts.end(); i++)
00248     {
00249       std::vector<iMesh::EntityHandle> adj_tris;
00250       std::vector<int> dum;
00251       iMesh::EntitySetHandle sh = IBSH((*i)->mesh_handle());
00252       mk->imesh_instance()->getAdjEntities(sh, iBase_ALL_TYPES, iMesh_POINT, iBase_FACE, adj_tris, dum);
00253 
00254       int num_adj_tris = adj_tris.size();
00255       CHECK( num_adj_tris >=4 && num_adj_tris <=6);
00256 
00257     }
00258 
00259 }
00260 
00261 
00262 void cube_tris_connectivity_test()
00263 {
00264   
00265   //get all triangles from the model ents
00266   MEntVector surfs; 
00267   mk->get_entities_by_dimension(2, surfs);
00268   
00269   int expected_num_of_adj_tris = 3;
00270   
00271   MEntVector::iterator i; 
00272   
00273   for( i = surfs.begin(); i != surfs.end(); i++) 
00274     {
00275       //get the triangles for each surface
00276       std::vector<int> dum;
00277       iMesh::EntitySetHandle sh = IBSH((*i)->mesh_handle()); 
00278       std::vector<iMesh::EntityHandle> surf_tris;
00279       mk->imesh_instance()->getEntities( sh, iBase_FACE, iMesh_TRIANGLE, surf_tris); 
00280       
00281       //each triangle should be adjacent to exactly 3 other triangles 
00282       for( std::vector<iMesh::EntityHandle>::iterator j = surf_tris.begin();
00283            j != surf_tris.end(); j++)
00284         {
00285           std::vector<iMesh::EntityHandle> adjacent_tris; 
00286           mk->imesh_instance()->getEnt2ndAdj( *j, iBase_EDGE, iBase_FACE, adjacent_tris); 
00287           
00288 
00289           for(unsigned int k = 0; k < adjacent_tris.size(); k++) 
00290             {
00291 
00292               iMesh::EntityTopology topo; 
00293               mk->imesh_instance()->getEntTopo(adjacent_tris[k], topo); 
00294               std::cout << topo <<std::endl; 
00295 
00296             }
00297 
00298           CHECK(expected_num_of_adj_tris == int(adjacent_tris.size())); 
00299         }
00300     }
00301 
00302 }
00303 
00304 void cube_tri_curve_coincidence_test()
00305 {
00306   //get all curves from the mesh 
00307   MEntVector curves; 
00308   mk->get_entities_by_dimension(1, curves); 
00309   
00310   //get all triangles from the mesh
00311   std::vector<iMesh::EntityHandle> tris; 
00312   mk->imesh_instance()->getEntities(0, iBase_FACE, iMesh_TRIANGLE, tris); 
00313 
00314   std::vector<iMesh::EntityHandle>::iterator i; 
00315   for( i = tris.begin(); i != tris.end(); i++)
00316     {
00317 
00318       //get the edges for this triangle 
00319       std::vector<iMesh::EntityHandle> tri_edges;
00320       mk->imesh_instance()->getEntAdj( *i, iBase_EDGE, tri_edges); 
00321       //make sure we've retrieved two edges for this triangle 
00322       CHECK( 2 == int(tri_edges.size()) ); 
00323       match_tri_edges_w_curves( tri_edges, curves ); 
00324       
00325     }
00326   
00327 
00328 
00329 }
00330 
00331 void match_tri_edges_w_curves( std::vector<iMesh::EntityHandle> edges, MEntVector curves)
00332 {
00333 
00334   int match_counter = 0; 
00335   int num_of_tri_edges = edges.size(); 
00336   CHECK(num_of_tri_edges);
00337 
00338   for(std::vector<iMesh::EntityHandle>::iterator i = edges.begin(); 
00339       i != edges.end(); i++)
00340     {
00341       for(MEntVector::iterator j = curves.begin(); j != curves.end(); 
00342           j++)
00343         {
00344           //get the curve edges (there should be only one per curve for a cube)
00345           iMesh::EntitySetHandle sh = IBSH((*j)->mesh_handle());
00346           std::vector<iMesh::EntityHandle> cedges;
00347           mk->imesh_instance()->getEntities( sh, iBase_EDGE, iMesh_LINE_SEGMENT, cedges); 
00348 
00349           CHECK( 1 == int(cedges.size()) );
00350 
00351           iMesh::EntityHandle curve_edge_handle = cedges[0];
00352           iMesh::EntityHandle tri_edge_handle = *i;
00353           if( tri_edge_handle == curve_edge_handle ) match_counter++;
00354         }
00355 
00356       
00357     }
00358 
00359   //make sure we found a match for each triangle edge to a curve edge
00360   CHECK( num_of_tri_edges == match_counter ); 
00361 }
00362 
00363 void cube_edge_adjacencies_test()
00364 {
00365 
00366   //get all the curves of the cube
00367   MEntVector curves;
00368   mk->get_entities_by_dimension( 1, curves); 
00369   
00370   for(MEntVector::iterator i = curves.begin(); i != curves.end(); i++)
00371     {
00372 
00373       //get the iMesh set handle for the curve
00374       iMesh::EntitySetHandle sh = IBSH( (*i)->mesh_handle() ); 
00375 
00376       //get the curve edges
00377       std::vector<iMesh::EntityHandle> curve_edges; 
00378       mk->imesh_instance()->getEntities( sh, iBase_EDGE, iMesh_LINE_SEGMENT, curve_edges); 
00379       
00380       //for a cube there should only be one edge per curve
00381       CHECK( 1 == int(curve_edges.size()) );
00382 
00383       //check that each edge is adjacent to no more than 2 triangles 
00384       for(unsigned int i = 0; i < curve_edges.size(); i++) 
00385         {
00386           
00387           //get the adjacent triangles to the curve edge 
00388           std::vector<iMesh::EntityHandle> adj_tris; 
00389           mk->imesh_instance()->getEntAdj( curve_edges[i], iBase_FACE, adj_tris ); 
00390           
00391           //check that the entities returned are triangles 
00392           for( unsigned int j = 0; j < adj_tris.size(); j++)
00393             {
00394               iMesh::EntityTopology topo; 
00395               mk->imesh_instance()->getEntTopo( adj_tris[j], topo); 
00396               CHECK( iMesh_TRIANGLE == topo ); 
00397             }
00398 
00399           //check that there are no more than two triangles adjacent to this edge
00400           CHECK( 2 <= int(adj_tris.size()) ); 
00401 
00402         } //end curve edges loop
00403     } // end curves loop
00404 
00405 }
00406 
00407 void cube_tri_verts_test()
00408 {
00409 
00410   //get all of the triangles in the instance 
00411   std::vector<iMesh::EntityHandle> tris; 
00412   mk->imesh_instance()->getEntities( 0, iBase_FACE, iMesh_TRIANGLE, tris); 
00413 
00414   std::vector<iMesh::EntityHandle>::iterator i; 
00415   for( i = tris.begin(); i != tris.end(); i++)
00416     { 
00417       //get the triangle vertices
00418       std::vector<iMesh::EntityHandle> tri_verts;
00419       mk->imesh_instance()->getEntAdj( *i, iBase_VERTEX, tri_verts); 
00420 
00421       //check that there are three of them 
00422       CHECK( 3 == int(tri_verts.size()) ); 
00423       //check that none of the vertices are the same
00424       CHECK( tri_verts[0] != tri_verts[1] ); 
00425       CHECK( tri_verts[1] != tri_verts[2] ); 
00426       CHECK( tri_verts[2] != tri_verts[0] ); 
00427     }
00428 
00429 }
00430 
00431 
00432 int count_topo_for_dim( int dim, iMesh::EntityTopology topo)
00433 {
00434 
00435   MEntVector ents;
00436   mk->get_entities_by_dimension(dim, ents);
00437 
00438   int counter = 0;
00439 
00440   for(unsigned int i = 0; i < ents.size(); i++)
00441     {
00442       int temp = 0;
00443       iMesh::EntitySetHandle set_handle = IBSH(ents[i]->mesh_handle());
00444       mk->imesh_instance()->getNumOfTopo(set_handle, topo, temp);
00445 
00446       counter += temp;
00447       temp = 0;
00448     }
00449 
00450   return counter;
00451 }
00452 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines