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