MOAB: Mesh Oriented datABase  (version 5.5.0)
gqt_simple_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 
10 #include "moab/GeomTopoTool.hpp"
11 #include "moab/GeomQueryTool.hpp"
12 
13 using namespace moab;
14 
18 
19 #define CHKERR( A ) \
20  do \
21  { \
22  if( MB_SUCCESS != ( A ) ) \
23  { \
24  std::cerr << "Failure (error code " << ( A ) << ") at " __FILE__ ":" << __LINE__ << std::endl; \
25  return A; \
26  } \
27  } while( false )
28 
29 const std::string input_file = TestDir + "unittest/test_geom.h5m";
30 
32 {
33  MBI = new moab::Core();
34  ErrorCode rval = MBI->load_file( input_file.c_str() ); // open the
35  CHECK_ERR( rval );
36  GTT = new GeomTopoTool( MBI, true );
37  GQT = new GeomQueryTool( GTT );
38 }
39 
41 {
42  /* 1 - Test with external moab, load file in GTT/GQT*/
43  // make new moab core
44  ErrorCode rval;
45 
46  // load a file
47  MBI = new moab::Core();
48  rval = MBI->load_file( input_file.c_str() ); // open the
49  CHECK_ERR( rval );
50  GTT = new GeomTopoTool( MBI );
51  GQT = new GeomQueryTool( GTT );
52  rval = GQT->initialize();CHECK_ERR( rval );
53 }
54 
56 {
57  /* 2 - Test with external moab, load file in MOAB*/
58  // build implicit complement and obb trees
59  ErrorCode rval;
60 
61  // load file
62  MBI = new moab::Core();
63  rval = MBI->load_file( input_file.c_str() ); // open the file
64  CHECK_ERR( rval );
65  GTT = new GeomTopoTool( MBI );
66  GQT = new GeomQueryTool( GTT );
67  // create obb trees
68  rval = GQT->gttool()->setup_implicit_complement();CHECK_ERR( rval );
69  rval = GQT->gttool()->construct_obb_trees();CHECK_ERR( rval );
70 }
71 
73 {
74  /* 3 - Test with internal moab, load file in MOAB*/
75  // find geomsets, build implicit complement, and construct obb trees
76  ErrorCode rval;
77 
78  MBI = new moab::Core();
79  rval = MBI->load_file( input_file.c_str() ); // open the
80  CHECK_ERR( rval );
81  GTT = new GeomTopoTool( MBI, true, 0 );
82  GQT = new GeomQueryTool( GTT );
83  rval = GQT->gttool()->setup_implicit_complement();CHECK_ERR( rval );
84  rval = GQT->gttool()->construct_obb_trees();CHECK_ERR( rval );
85 }
86 
88 {
89  // make new dagmc
90  std::cout << "test_obb_retreval" << std::endl;
91 
92  ErrorCode rval;
93  // load a file
94  MBI = new moab::Core();
95  rval = MBI->load_file( input_file.c_str() ); // open the
96  CHECK_ERR( rval );
97  GTT = new GeomTopoTool( MBI, true, 0 );
98  GQT = new GeomQueryTool( GTT );
99  GQT->initialize();CHECK_ERR( rval );
100 
101  // write to file
102  rval = MBI->write_file( "fcad" );CHECK_ERR( rval );
103 
104  // cleanup instances
105  delete GQT;
106  delete GTT;
107  delete MBI;
108 
109  // re-load written file
110  MBI = new moab::Core();
111  rval = MBI->load_file( "fcad" ); // open the
112  CHECK_ERR( rval );
113  GTT = new GeomTopoTool( MBI, true, 0 );
114  GQT = new GeomQueryTool( GTT );
115  rval = GQT->initialize();CHECK_ERR( rval );
116 
117  // remove file
118  remove( "fcad" );
119 }
120 
122 {
124 }
125 
127 {
128  ErrorCode rval = GQT->gttool()->construct_obb_trees();CHECK_ERR( rval );
129 }
130 
132 {
133  int expect_num_vols = 2;
134  int num_vols = GQT->gttool()->num_ents_of_dim( 3 );
135  CHECK_EQUAL( expect_num_vols, num_vols );
136 }
137 
139 {
140  int result = 0;
141  int expect_result = 1;
142  int vol_idx = 1;
143  double xyz[3] = { 0.0, 0.0, 0.0 };
144  EntityHandle vol_h;
145 
146  // load file and create instances
147  MBI = new moab::Core();
148  ErrorCode rval = MBI->load_file( input_file.c_str() ); // open the
149  CHECK_ERR( rval );
150  GTT = new GeomTopoTool( MBI, true, 0 );
151  GQT = new GeomQueryTool( GTT );
152  // initialize for geom queries
153  GQT->initialize();
154 
155  // get volume
156  vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
157  // test point in volume
158  GQT->point_in_volume( vol_h, xyz, result );CHECK_ERR( rval );
159  CHECK_EQUAL( expect_result, result );
160 }
161 
163 {
164  // make new dagmc
165  std::cout << "test_obb_retreval and ray_fire" << std::endl;
166 
167  ErrorCode rval;
168 
169  // load file and initialize
170  MBI = new moab::Core();
171  rval = MBI->load_file( input_file.c_str() ); // open the
172  CHECK_ERR( rval );
173  GTT = new GeomTopoTool( MBI, true, 0 );
174  GQT = new GeomQueryTool( GTT );
175  GQT->initialize();CHECK_ERR( rval );
176 
177  // write to file
178  rval = MBI->write_file( "fcad" );CHECK_ERR( rval );
179 
180  // delete instances
181  delete GTT;
182  delete GQT;
183  delete MBI;
184 
185  // load the file that was written and initialize
186  MBI = new moab::Core();
187  rval = MBI->load_file( "fcad" ); // open the
188  CHECK_ERR( rval );
189  GTT = new GeomTopoTool( MBI, true, 0 );
190  GQT = new GeomQueryTool( GTT );
191  rval = GQT->initialize();CHECK_ERR( rval );
192 
193  // remove generated test file
194  remove( "fcad" );
195 
196  // now perform full ray fire
197  double eps = 1.e-6;
198  int vol_idx = 1;
199  // note model is cube of side 10, centred at 0,0,0, so ray fire along
200  // any unit direction should be exactly 5.0
201  double xyz[3] = { 0.0, 0.0, 0.0 };
202  double dir[3] = { 0.0, 0.0, 1.0 };
203  EntityHandle next_surf;
204  double next_surf_dist;
205  double expect_next_surf_dist = 5.0;
206  EntityHandle vol_h;
207 
208  // get volume handle to fire on
209  vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
210  // test ray fire
211  rval = GQT->ray_fire( vol_h, xyz, dir, next_surf, next_surf_dist );CHECK_ERR( rval );
212  CHECK_REAL_EQUAL( expect_next_surf_dist, next_surf_dist, eps );
213 }
214 
216 {
217  const double eps = 1e-6; // epsilon for test, faceting tol?
218 
219  int vol_idx = 1;
220  // note model is cube of side 10, centred at 0,0,0, so ray fire along
221  // any unit direction should be exactly 5.0
222  double xyz[3] = { 0.0, 0.0, 0.0 };
223  double dir[3] = { 0.0, 0.0, 1.0 };
224  EntityHandle next_surf;
225  double next_surf_dist;
226  double expect_next_surf_dist = 5.0;
227  EntityHandle vol_h;
228  ErrorCode rval;
229 
230  MBI = new moab::Core();
231  rval = MBI->load_file( input_file.c_str() ); // open the
232  CHECK_ERR( rval );
233  GTT = new GeomTopoTool( MBI, true, 0 );
234  GQT = new GeomQueryTool( GTT );
235  GQT->initialize();CHECK_ERR( rval );
236 
237  vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
238  rval = GQT->ray_fire( vol_h, xyz, dir, next_surf, next_surf_dist );CHECK_ERR( rval );
239  CHECK_REAL_EQUAL( expect_next_surf_dist, next_surf_dist, eps );
240 }
241 
243 {
244  const double eps = 1e-6; // epsilon for test, faceting tol?
245 
246  int vol_idx = 1;
247  // note model is cube of side 10, centred at 0,0,0, so ray fire along
248  // any unit direction should be exactly 5.0
249  double xyz[3] = { -6.0, 0.0, 0.0 };
250  double distance; // distance from point to nearest surface
251  double expect_distance = 1.0;
252  EntityHandle vol_h;
253  ErrorCode rval;
254 
255  // get the volume handle
256  vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
257 
258  // test closest to location
259  rval = GQT->closest_to_location( vol_h, xyz, distance );CHECK_ERR( rval );
260 
261  // distance should be 1.0 cm
262  CHECK_REAL_EQUAL( expect_distance, distance, eps );
263 }
264 
266 {
267  int vol_idx = 1;
268  EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
269  int surf_idx = 1;
270  EntityHandle surf_h = GQT->gttool()->entity_by_id( 2, surf_idx );
271 
272  double xyz[3] = { 0.0, 0.0, 5.0 };
273  double dir[3] = { 0.0, 0.0, 1.0 };
274  int result;
275  int expect_result = 0;
276 
277  // test vol boundary
278  ErrorCode rval = GQT->test_volume_boundary( vol_h, surf_h, xyz, dir, result );CHECK_ERR( rval );
279  // check ray leaving volume
280  CHECK_EQUAL( expect_result, result );
281 }
282 
284 {
285  int vol_id = 1;
286  double origin[3] = { 0.0, 0.0, 0.0 };
287  EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
288  int result;
289  ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
290  CHECK_EQUAL( rval, moab::MB_SUCCESS );
291  CHECK_EQUAL( result, 1 ); // inside
292 }
293 
295 {
296  int vol_id = 1;
297  double origin[3] = { 5.0, 0.0, 0.0 };
298  EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
299  int result;
300  ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
301  CHECK_EQUAL( rval, moab::MB_SUCCESS );
302  CHECK_EQUAL( result, 1 ); // inside
303 }
304 
306 {
307  int vol_id = 1;
308  double origin[3] = { 5.1, 0.0, 0.0 };
309  EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
310  int result;
311  ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
312  CHECK_EQUAL( rval, moab::MB_SUCCESS );
313  CHECK_EQUAL( result, 0 ); // outside
314 }
315 
317 {
318  int vol_id = 1;
319  double origin[3] = { 5.1, 5.1, 5.1 };
320  EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
321  int result;
322  ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
323  CHECK_EQUAL( rval, moab::MB_SUCCESS );
324  CHECK_EQUAL( result, 0 ); // outside
325 }
326 
328 {
329  int vol_id = 1;
330  double origin[3] = { 5.0 - 1.e-308, 5.0 - 1.e-308, 5.0 - 1.e-308 };
331  EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
332  int result;
333  ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
334  CHECK_EQUAL( rval, moab::MB_SUCCESS );
335  CHECK_EQUAL( result, 1 ); // outside
336 }
337 
338 void cleanup()
339 {
340  // cleanup instances
341  delete GQT;
342  delete GTT;
343  delete MBI;
344 }
345 
346 int main( int /* argc */, char** /* argv */ )
347 {
348  int result = 0;
349 
350  result += RUN_TEST( gqt_load_file ); // test ray fire
351  result += RUN_TEST( gqt_build_obb ); // build the obb
352  result += RUN_TEST( gqt_create_impl_compl ); // build the obb
353  result += RUN_TEST( gqt_num_vols ); // make sure the num of vols correct
354  cleanup();
355 
356  result += RUN_TEST( gqt_load_file_dagmc_build_obb ); //
357  cleanup();
358 
360  cleanup();
361 
363  cleanup();
364 
365  result += RUN_TEST( gqt_test_obb_retreval ); // check that we are retreving loaded obbs
366  cleanup();
367 
368  result += RUN_TEST( gqt_test_obb_retreval_rayfire ); // check that we can ray fire on loaded obbs
369  cleanup();
370 
371  result += RUN_TEST( gqt_point_in ); // check entity by point
372  cleanup();
373 
374  result += RUN_TEST( gqt_rayfire ); // ensure ray fire distance is correct
375  result += RUN_TEST( gqt_closest_to ); // check the distance to surface nearest point
376  result += RUN_TEST( gqt_test_boundary ); // check particle entering leaving
377  cleanup();
378 
379  return result;
380 }