MOAB: Mesh Oriented datABase  (version 5.5.0)
read_cgm_connectivity_test.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "moab/Interface.hpp"
3 #ifndef IS_BUILDING_MB
4 #define IS_BUILDING_MB
5 #endif
6 #include "TestUtil.hpp"
7 #include "Internals.hpp"
8 #include "moab/Core.hpp"
9 #include "MBTagConventions.hpp"
10 #include "InitCGMA.hpp"
11 #include "GeometryQueryTool.hpp"
12 #include "moab/MeshTopoUtil.hpp"
13 using namespace moab;
14 
15 #define CHKERR( A ) \
16  do \
17  { \
18  if( MB_SUCCESS != ( A ) ) \
19  { \
20  std::cerr << "Failure (error code " << ( A ) << ") at " __FILE__ ":" << __LINE__ << std::endl; \
21  return A; \
22  } \
23  } while( false )
24 
25 #ifdef HAVE_OCC_STEP
26 std::string input_cube = TestDir + "unittest/io/cube.stp";
27 #else
28 std::string input_cube = TestDir + "unittest/io/cube.sat";
29 #endif
30 
31 // Function used to load the test file
32 void read_file( Interface* moab, const char* input_file );
33 
34 // List of tests in this file
40 
41 // Other functions
42 void match_tri_edges_w_curve( const Range& tri_edges, const Range& curves );
43 
44 int main( int /* argc */, char** /* argv */ )
45 {
46  int result = 0;
47 
51  result += RUN_TEST( cube_tri_vertex_test );
52 
53  return result;
54 }
55 
56 void read_file( Interface* moab, const char* input_file )
57 {
58  InitCGMA::initialize_cgma();
59  GeometryQueryTool::instance()->delete_geometry();
60 
61  ErrorCode rval = moab->load_file( input_file );CHECK_ERR( rval );
62 }
63 
64 // Checks the adjacency of each vertex entity in a simple cube file load
65 // to make sure it isn't adjacent to too many or too few triangles.
67 {
68 
69  ErrorCode rval;
70  // Open the test file
71  Core moab;
72  Interface* mb = &moab;
73  read_file( mb, input_cube.c_str() );
74 
75  // Get all vertex handles from the mesh
76  Range verts;
77  rval = mb->get_entities_by_type( 0, MBVERTEX, verts );CHECK_ERR( rval );
78 
79  // Check that each vertex connects to at least 4 and no more than 6 triangles
80  for( Range::const_iterator i = verts.begin(); i != verts.end(); ++i )
81  {
82  std::vector< EntityHandle > adj_tris;
83  rval = mb->get_adjacencies( &( *i ), 1, 2, false, adj_tris );CHECK_ERR( rval );
84 
85  int adj_size = adj_tris.size();
86  CHECK( adj_size >= 4 && adj_size <= 6 );
87  }
88 }
89 
90 // Check that each triangle in the mesh is adjacent to
91 // exactly three other triangles
93 {
94  ErrorCode rval;
95  // Open the test file
96  Core moab;
97  Interface* mb = &moab;
98  read_file( mb, input_cube.c_str() );
99 
100  // Get triangles from the mesh
101  Range tris;
102  rval = mb->get_entities_by_type( 0, MBTRI, tris );CHECK_ERR( rval );
103 
104  int expected_num_of_adj_tris = 3;
105 
106  for( Range::const_iterator i = tris.begin() + 1; i != tris.end(); ++i )
107  {
108  Range adj_tris;
109  moab::MeshTopoUtil mu( mb );
110  // Use Triangle edges to get all adjacent triangles
111  rval = mu.get_bridge_adjacencies( *i, 1, 2, adj_tris );CHECK_ERR( rval );
112  CHECK_EQUAL( expected_num_of_adj_tris, (int)adj_tris.size() );
113 
114  // Check that the entities we found from bridge_adjacencies
115  // are triangles
116  Range adj_tri_test = adj_tris.subset_by_type( MBTRI );
117  CHECK_EQUAL( (int)adj_tris.size(), (int)adj_tri_test.size() );
118  }
119 }
120 
121 // Takes triangle edges and makes sure they match the EntityHandles of
122 // curves in the case of a cube mesh
124 {
125 
126  ErrorCode rval;
127  // Open the test file
128  Core moab;
129  Interface* mb = &moab;
130  read_file( mb, input_cube.c_str() );
131 
132  // Get curves from the mesh
133  Range curves;
134  rval = mb->get_entities_by_type( 0, MBEDGE, curves );CHECK_ERR( rval );
135  curves.print();
136 
137  // Get triangles from the mesh
138  Range tris;
139  rval = mb->get_entities_by_type( 0, MBTRI, tris );CHECK_ERR( rval );
140 
141  for( Range::const_iterator i = tris.begin(); i != tris.end(); ++i )
142  {
143  // Get the any curve edges that are a part of the triangle
144  Range tri_edges;
145  rval = mb->get_adjacencies( &( *i ), 1, 1, false, tri_edges );CHECK_ERR( rval );
146  // Check that we've retrieved two edges from get_adjacencies
147  // For a this file (cube), each triangle should have two curve
148  // edges
149  int num_of_tri_edges = tri_edges.size();
150  CHECK_EQUAL( 2, num_of_tri_edges );
151  match_tri_edges_w_curve( tri_edges, curves );CHECK_ERR( rval );
152  }
153 }
154 
155 void match_tri_edges_w_curve( const Range& tri_edges, const Range& curves )
156 {
157  int match_counter = 0;
158  int num_of_tri_edges = tri_edges.size();
159  CHECK( num_of_tri_edges );
160  for( Range::const_iterator i = tri_edges.begin(); i != tri_edges.end(); ++i )
161  {
162  for( Range::const_iterator j = curves.begin(); j != curves.end(); ++j )
163  {
164  // If the edge handle matches a curve handle, increment the number
165  // matches
166  if( *i == *j ) match_counter++;
167  }
168  }
169  // Make sure that each edge returned from triangle edges
170  // has been matched to a curve
171  CHECK_EQUAL( num_of_tri_edges, match_counter );
172 }
173 
174 // Ensures that each triangle edge is adjacent to no more than
175 // two triangles.
177 {
178  ErrorCode rval;
179  // Open the test file
180  Core moab;
181  Interface* mb = &moab;
182  read_file( mb, input_cube.c_str() );
183 
184  // Get the curves
185  Range curves;
186  rval = mb->get_entities_by_type( 0, MBEDGE, curves );CHECK_ERR( rval );
187 
188  for( Range::const_iterator i = curves.begin(); i != curves.end(); ++i )
189  {
190  // Get triangle adjacent to each edge
191  Range adj_tris;
192  rval = mb->get_adjacencies( &( *i ), 1, 2, false, adj_tris );CHECK_ERR( rval );
193 
194  int num_adj_tris = adj_tris.size();
195  // Ensure that no edge is adjacent to more than two triangles
196  CHECK( num_adj_tris <= 2 );
197  }
198 }
199 
200 // Checks, for each triangle, that none of the verices are the same
202 {
203  ErrorCode rval;
204  // Open the test file
205  Core moab;
206  Interface* mb = &moab;
207  read_file( mb, input_cube.c_str() );
208 
209  // Get all triangles
210  Range tris;
211  rval = mb->get_entities_by_type( 0, MBTRI, tris );CHECK_ERR( rval );
212 
213  for( Range::const_iterator i = tris.begin(); i != tris.end(); ++i )
214  {
215  // Get all triangle vertices
216  Range verts;
217  rval = mb->get_connectivity( &( *i ), 1, verts );CHECK_ERR( rval );
218  // Make sure that each vertex making up
219  // the triangle is different
220  int number_of_verts = verts.size();
221  CHECK( 3 == number_of_verts );
222  CHECK( verts[0] != verts[1] );
223  CHECK( verts[1] != verts[2] );
224  CHECK( verts[2] != verts[0] );
225  }
226 }