MOAB: Mesh Oriented datABase  (version 5.4.1)
read_cgm_connectivity_test.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include "moab/Interface.hpp"
00003 #ifndef IS_BUILDING_MB
00004 #define IS_BUILDING_MB
00005 #endif
00006 #include "TestUtil.hpp"
00007 #include "Internals.hpp"
00008 #include "moab/Core.hpp"
00009 #include "MBTagConventions.hpp"
00010 #include "InitCGMA.hpp"
00011 #include "GeometryQueryTool.hpp"
00012 #include "moab/MeshTopoUtil.hpp"
00013 using namespace moab;
00014 
00015 #define CHKERR( A )                                                                                        \
00016     do                                                                                                     \
00017     {                                                                                                      \
00018         if( MB_SUCCESS != ( A ) )                                                                          \
00019         {                                                                                                  \
00020             std::cerr << "Failure (error code " << ( A ) << ") at " __FILE__ ":" << __LINE__ << std::endl; \
00021             return A;                                                                                      \
00022         }                                                                                                  \
00023     } while( false )
00024 
00025 #ifdef HAVE_OCC_STEP
00026 std::string input_cube = TestDir + "unittest/io/cube.stp";
00027 #else
00028 std::string input_cube = TestDir + "unittest/io/cube.sat";
00029 #endif
00030 
00031 // Function used to load the test file
00032 void read_file( Interface* moab, const char* input_file );
00033 
00034 // List of tests in this file
00035 void cube_verts_connectivity_test();
00036 void cube_tris_connectivity_test();
00037 void cube_tri_curve_coincidence_test();
00038 void cube_edge_adjacencies_test();
00039 void cube_tri_vertex_test();
00040 
00041 // Other functions
00042 void match_tri_edges_w_curve( const Range& tri_edges, const Range& curves );
00043 
00044 int main( int /* argc */, char** /* argv */ )
00045 {
00046     int result = 0;
00047 
00048     result += RUN_TEST( cube_verts_connectivity_test );
00049     result += RUN_TEST( cube_tris_connectivity_test );
00050     result += RUN_TEST( cube_tri_curve_coincidence_test );
00051     result += RUN_TEST( cube_tri_vertex_test );
00052 
00053     return result;
00054 }
00055 
00056 void read_file( Interface* moab, const char* input_file )
00057 {
00058     InitCGMA::initialize_cgma();
00059     GeometryQueryTool::instance()->delete_geometry();
00060 
00061     ErrorCode rval = moab->load_file( input_file );CHECK_ERR( rval );
00062 }
00063 
00064 // Checks the adjacency of each vertex entity in a simple cube file load
00065 // to make sure it isn't adjacent to too many or too few triangles.
00066 void cube_verts_connectivity_test()
00067 {
00068 
00069     ErrorCode rval;
00070     // Open the test file
00071     Core moab;
00072     Interface* mb = &moab;
00073     read_file( mb, input_cube.c_str() );
00074 
00075     // Get all vertex handles from the mesh
00076     Range verts;
00077     rval = mb->get_entities_by_type( 0, MBVERTEX, verts );CHECK_ERR( rval );
00078 
00079     // Check that each vertex connects to at least 4 and no more than 6 triangles
00080     for( Range::const_iterator i = verts.begin(); i != verts.end(); ++i )
00081     {
00082         std::vector< EntityHandle > adj_tris;
00083         rval = mb->get_adjacencies( &( *i ), 1, 2, false, adj_tris );CHECK_ERR( rval );
00084 
00085         int adj_size = adj_tris.size();
00086         CHECK( adj_size >= 4 && adj_size <= 6 );
00087     }
00088 }
00089 
00090 // Check that each triangle in the mesh is adjacent to
00091 // exactly three other triangles
00092 void cube_tris_connectivity_test()
00093 {
00094     ErrorCode rval;
00095     // Open the test file
00096     Core moab;
00097     Interface* mb = &moab;
00098     read_file( mb, input_cube.c_str() );
00099 
00100     // Get triangles from the mesh
00101     Range tris;
00102     rval = mb->get_entities_by_type( 0, MBTRI, tris );CHECK_ERR( rval );
00103 
00104     int expected_num_of_adj_tris = 3;
00105 
00106     for( Range::const_iterator i = tris.begin() + 1; i != tris.end(); ++i )
00107     {
00108         Range adj_tris;
00109         moab::MeshTopoUtil mu( mb );
00110         // Use Triangle edges to get all adjacent triangles
00111         rval = mu.get_bridge_adjacencies( *i, 1, 2, adj_tris );CHECK_ERR( rval );
00112         CHECK_EQUAL( expected_num_of_adj_tris, (int)adj_tris.size() );
00113 
00114         // Check that the entities we found from bridge_adjacencies
00115         // are triangles
00116         Range adj_tri_test = adj_tris.subset_by_type( MBTRI );
00117         CHECK_EQUAL( (int)adj_tris.size(), (int)adj_tri_test.size() );
00118     }
00119 }
00120 
00121 // Takes triangle edges and makes sure they match the EntityHandles of
00122 // curves in the case of a cube mesh
00123 void cube_tri_curve_coincidence_test()
00124 {
00125 
00126     ErrorCode rval;
00127     // Open the test file
00128     Core moab;
00129     Interface* mb = &moab;
00130     read_file( mb, input_cube.c_str() );
00131 
00132     // Get curves from the mesh
00133     Range curves;
00134     rval = mb->get_entities_by_type( 0, MBEDGE, curves );CHECK_ERR( rval );
00135     curves.print();
00136 
00137     // Get triangles from the mesh
00138     Range tris;
00139     rval = mb->get_entities_by_type( 0, MBTRI, tris );CHECK_ERR( rval );
00140 
00141     for( Range::const_iterator i = tris.begin(); i != tris.end(); ++i )
00142     {
00143         // Get the any curve edges that are a part of the triangle
00144         Range tri_edges;
00145         rval = mb->get_adjacencies( &( *i ), 1, 1, false, tri_edges );CHECK_ERR( rval );
00146         // Check that we've retrieved two edges from get_adjacencies
00147         // For a this file (cube), each triangle should have two curve
00148         // edges
00149         int num_of_tri_edges = tri_edges.size();
00150         CHECK_EQUAL( 2, num_of_tri_edges );
00151         match_tri_edges_w_curve( tri_edges, curves );CHECK_ERR( rval );
00152     }
00153 }
00154 
00155 void match_tri_edges_w_curve( const Range& tri_edges, const Range& curves )
00156 {
00157     int match_counter    = 0;
00158     int num_of_tri_edges = tri_edges.size();
00159     CHECK( num_of_tri_edges );
00160     for( Range::const_iterator i = tri_edges.begin(); i != tri_edges.end(); ++i )
00161     {
00162         for( Range::const_iterator j = curves.begin(); j != curves.end(); ++j )
00163         {
00164             // If the edge handle matches a curve handle, increment the number
00165             // matches
00166             if( *i == *j ) match_counter++;
00167         }
00168     }
00169     // Make sure that each edge returned from triangle edges
00170     // has been matched to a curve
00171     CHECK_EQUAL( num_of_tri_edges, match_counter );
00172 }
00173 
00174 // Ensures that each triangle edge is adjacent to no more than
00175 // two triangles.
00176 void cube_edge_adjacencies_test()
00177 {
00178     ErrorCode rval;
00179     // Open the test file
00180     Core moab;
00181     Interface* mb = &moab;
00182     read_file( mb, input_cube.c_str() );
00183 
00184     // Get the curves
00185     Range curves;
00186     rval = mb->get_entities_by_type( 0, MBEDGE, curves );CHECK_ERR( rval );
00187 
00188     for( Range::const_iterator i = curves.begin(); i != curves.end(); ++i )
00189     {
00190         // Get triangle adjacent to each edge
00191         Range adj_tris;
00192         rval = mb->get_adjacencies( &( *i ), 1, 2, false, adj_tris );CHECK_ERR( rval );
00193 
00194         int num_adj_tris = adj_tris.size();
00195         // Ensure that no edge is adjacent to more than two triangles
00196         CHECK( num_adj_tris <= 2 );
00197     }
00198 }
00199 
00200 // Checks, for each triangle, that none of the verices are the same
00201 void cube_tri_vertex_test()
00202 {
00203     ErrorCode rval;
00204     // Open the test file
00205     Core moab;
00206     Interface* mb = &moab;
00207     read_file( mb, input_cube.c_str() );
00208 
00209     // Get all triangles
00210     Range tris;
00211     rval = mb->get_entities_by_type( 0, MBTRI, tris );CHECK_ERR( rval );
00212 
00213     for( Range::const_iterator i = tris.begin(); i != tris.end(); ++i )
00214     {
00215         // Get all triangle vertices
00216         Range verts;
00217         rval = mb->get_connectivity( &( *i ), 1, verts );CHECK_ERR( rval );
00218         // Make sure that each vertex making up
00219         // the triangle is different
00220         int number_of_verts = verts.size();
00221         CHECK( 3 == number_of_verts );
00222         CHECK( verts[0] != verts[1] );
00223         CHECK( verts[1] != verts[2] );
00224         CHECK( verts[2] != verts[0] );
00225     }
00226 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines