MOAB: Mesh Oriented datABase  (version 5.5.0)
read_cgm_basic_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 
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 const std::string input_cube = TestDir + "unittest/io/cube.stp";
27 #else
28 const 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
37 void read_cube_tris_test();
39 void read_cube_vols_test();
41 // void delete_mesh_test();
42 
43 int main( int /* argc */, char** /* argv */ )
44 {
45  int result = 0;
46 
47  result += RUN_TEST( read_cube_verts_test );
48  result += RUN_TEST( read_cube_curves_test );
49  result += RUN_TEST( read_cube_tris_test );
50  result += RUN_TEST( read_cube_surfs_test );
51  result += RUN_TEST( read_cube_vols_test );
53 
54  return result;
55 }
56 
57 void read_file( Interface* moab, const char* input_file )
58 {
59  InitCGMA::initialize_cgma();
60  GeometryQueryTool::instance()->delete_geometry();
61 
62  ErrorCode rval = moab->load_file( input_file );CHECK_ERR( rval );
63 }
64 
65 // Gets the vertex entities from a simple cube file load and checks that the
66 // correct number of them exist.
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  int number_of_vertices;
76  rval = mb->get_number_entities_by_type( 0, MBVERTEX, number_of_vertices );CHECK_ERR( rval );
77  // For a cube there should be exactly 8 vertices
78  CHECK_EQUAL( 8, number_of_vertices );
79 }
80 
81 // Gets the triangle entities from a simple cube file load and checks that the
82 // correct number of them exist.
84 {
85  ErrorCode rval;
86  // Open the test file
87  Core moab;
88  Interface* mb = &moab;
89  read_file( mb, input_cube.c_str() );
90 
91  int number_of_tris;
92 
93  rval = mb->get_number_entities_by_type( 0, MBTRI, number_of_tris );
94  std::cout << "Number of Triangles = " << number_of_tris << std::endl;CHECK_ERR( rval );
95  // For a cube, there should be exactly 2 triangles per face
96  CHECK_EQUAL( 12, number_of_tris );
97 }
98 
99 // Gets the curve entities from a simple cube file load and checks that the
100 // correct number of them exist.
102 {
103  ErrorCode rval;
104  // Open the test file
105  Core moab;
106  Interface* mb = &moab;
107  read_file( mb, input_cube.c_str() );
108  // Get the geometry tag handle from the mesh
109  Tag geom_tag;
112  // Get the curves from the mesh
113  int dim = 1;
114  void* val[] = { &dim };
115  int number_of_curves;
116  rval = mb->get_number_entities_by_type_and_tag( 0, MBENTITYSET, &geom_tag, val, 1, number_of_curves );CHECK_ERR( rval );
117  // For a cube, there should be exactly 12 curves loaded from the file
118  CHECK_EQUAL( 12, number_of_curves );
119 }
120 
121 // Gets the surface entities from a simple cube file load and checks that the
122 // correct number of them exist.
124 {
125  ErrorCode rval;
126  // Open the test file
127  Core moab;
128  Interface* mb = &moab;
129  read_file( mb, input_cube.c_str() );
130 
131  // Get geometry tag for pulling curve data from the mesh
132  Tag geom_tag;
135 
136  // Get the number of surface from the mesh geometry data
137  int dim = 2;
138  void* val[] = { &dim };
139  int number_of_surfs;
140  rval = mb->get_number_entities_by_type_and_tag( 0, MBENTITYSET, &geom_tag, val, 1, number_of_surfs );CHECK_ERR( rval );
141  // For a cube, there should be exactly 6 surfaces
142  CHECK_EQUAL( 6, number_of_surfs );
143 }
144 
146 {
147  ErrorCode rval;
148  // Open the test file
149  Core moab;
150  Interface* mb = &moab;
151  read_file( mb, input_cube.c_str() );
152 
153  // Get geometry tag for pulling curve data from the mesh
154  Tag geom_tag;
157 
158  // Get the number of volumes from the mesh geometry data
159  int dim = 3;
160  void* val[] = { &dim };
161  int number_of_vols;
162  rval = mb->get_number_entities_by_type_and_tag( 0, MBENTITYSET, &geom_tag, val, 1, number_of_vols );CHECK_ERR( rval );
163  CHECK_EQUAL( 1, number_of_vols );
164 }
165 
166 // Gets the vertex eitities from a simple cube file load and checks that
167 // they are in the correct locations.
169 {
170 
171  ErrorCode rval;
172  // Open the test file
173  Core moab;
174  Interface* mb = &moab;
175  read_file( mb, input_cube.c_str() );
176 
177  // Retrieve all vertex handles from the mesh
178  Range verts;
179  rval = mb->get_entities_by_type( 0, MBVERTEX, verts );CHECK_ERR( rval );
180 
181  int number_of_verts = verts.size();
182  CHECK_EQUAL( 8, number_of_verts );
183  // Get the vertex coordinates
184  double x[8];
185  double y[8];
186  double z[8];
187  rval = mb->get_coords( verts, &x[0], &y[0], &z[0] );CHECK_ERR( rval );
188 
189  // Check against known locations of the vertices
190 
191  std::vector< double > x_ref;
192  std::vector< double > y_ref;
193  std::vector< double > z_ref;
194 
195  // Vertex 1
196  x_ref.push_back( 5 );
197  y_ref.push_back( -5 );
198  z_ref.push_back( 5 );
199 
200  // Vertex 2
201  x_ref.push_back( 5 );
202  y_ref.push_back( 5 );
203  z_ref.push_back( 5 );
204 
205  // Vertex 3
206  x_ref.push_back( -5 );
207  y_ref.push_back( 5 );
208  z_ref.push_back( 5 );
209 
210  // Vertex 4
211  x_ref.push_back( -5 );
212  y_ref.push_back( -5 );
213  z_ref.push_back( 5 );
214 
215  // Vertex 5
216  x_ref.push_back( 5 );
217  y_ref.push_back( 5 );
218  z_ref.push_back( -5 );
219 
220  // Vertex 6
221  x_ref.push_back( 5 );
222  y_ref.push_back( -5 );
223  z_ref.push_back( -5 );
224 
225  // Vertex 7
226  x_ref.push_back( -5 );
227  y_ref.push_back( -5 );
228  z_ref.push_back( -5 );
229 
230  // Vertex 8
231  x_ref.push_back( -5 );
232  y_ref.push_back( 5 );
233  z_ref.push_back( -5 );
234 
235  std::cout << verts.size() << std::endl;
236  std::cout << x_ref.size() << std::endl;
237 
238  for( unsigned int i = 0; i < verts.size(); i++ )
239  {
240  for( unsigned int j = 0; j < x_ref.size(); j++ )
241  {
242  if( x[i] == x_ref[j] && y[i] == y_ref[j] && z[i] == z_ref[j] )
243  {
244  x_ref.erase( x_ref.begin() + j );
245  y_ref.erase( y_ref.begin() + j );
246  z_ref.erase( z_ref.begin() + j );
247  }
248  }
249  }
250 
251  // After looping through each vertex loaded from the mesh
252  // there should be no entities left in the reference vector
253  int leftovers = x_ref.size();
254  CHECK_EQUAL( 0, leftovers );
255 }
256 
257 // Superfluous test for ReadCGM, but perhaps better
258 // than the test in place for moab::delete_geometry()
259 
260 /*
261 void delete_mesh_test()
262 {
263  Core moab;
264  Interface* mb = &moab;
265  read_file( mb, input_cube );
266 
267  ErrorCode rval;
268 
269  //Get geometry tag for pulling curve data from the mesh
270  Tag geom_tag;
271  rval = mb->tag_get_handle( GEOM_DIMENSION_TAG_NAME, 1, MB_TYPE_INTEGER,
272  geom_tag, moab::MB_TAG_DENSE|moab::MB_TAG_CREAT );CHECK_ERR(rval);
273 
274  Range geom_sets[4];
275 
276  for(unsigned dim=0; dim<4; dim++)
277  {
278  void *val[] = {&dim};
279  rval = mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &geom_tag,
280  val, 1, geom_sets[dim] );CHECK_ERR(rval);
281 
282  if( geom_sets[dim].size() == 0 ) std::cout << "Warning: No geom sets to begin with" <<
283 std::endl;
284 
285  }
286 
287  mb->delete_mesh();
288 
289  Range geom_sets_after[4];
290  for(unsigned dim=0; dim<4; dim++)
291  {
292  void *val_after[] = {&dim};
293  rval = mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &geom_tag,
294  val_after, 1, geom_sets_after[dim] );CHECK_ERR(rval);
295 
296  if( 0 != geom_sets_after[dim].size() ) rval = MB_FAILURE;
297 
298  CHECK_ERR(rval);
299  }
300 
301 }
302 
303 */