MOAB: Mesh Oriented datABase  (version 5.4.1)
gqt_simple_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 
00010 #include "moab/GeomTopoTool.hpp"
00011 #include "moab/GeomQueryTool.hpp"
00012 
00013 using namespace moab;
00014 
00015 GeomTopoTool* GTT;
00016 GeomQueryTool* GQT;
00017 Interface* MBI;
00018 
00019 #define CHKERR( A )                                                                                        \
00020     do                                                                                                     \
00021     {                                                                                                      \
00022         if( MB_SUCCESS != ( A ) )                                                                          \
00023         {                                                                                                  \
00024             std::cerr << "Failure (error code " << ( A ) << ") at " __FILE__ ":" << __LINE__ << std::endl; \
00025             return A;                                                                                      \
00026         }                                                                                                  \
00027     } while( false )
00028 
00029 const std::string input_file = TestDir + "unittest/test_geom.h5m";
00030 
00031 void gqt_load_file()
00032 {
00033     MBI            = new moab::Core();
00034     ErrorCode rval = MBI->load_file( input_file.c_str() );  // open the
00035     CHECK_ERR( rval );
00036     GTT = new GeomTopoTool( MBI, true );
00037     GQT = new GeomQueryTool( GTT );
00038 }
00039 
00040 void gqt_load_file_dagmc_build_obb()
00041 {
00042     /* 1 - Test with external moab, load file in GTT/GQT*/
00043     // make new moab core
00044     ErrorCode rval;
00045 
00046     // load a file
00047     MBI  = new moab::Core();
00048     rval = MBI->load_file( input_file.c_str() );  // open the
00049     CHECK_ERR( rval );
00050     GTT  = new GeomTopoTool( MBI );
00051     GQT  = new GeomQueryTool( GTT );
00052     rval = GQT->initialize();CHECK_ERR( rval );
00053 }
00054 
00055 void gqt_load_file_dagmc_via_moab_build_obb()
00056 {
00057     /* 2 - Test with external moab, load file in MOAB*/
00058     // build implicit complement and obb trees
00059     ErrorCode rval;
00060 
00061     // load file
00062     MBI  = new moab::Core();
00063     rval = MBI->load_file( input_file.c_str() );  // open the file
00064     CHECK_ERR( rval );
00065     GTT = new GeomTopoTool( MBI );
00066     GQT = new GeomQueryTool( GTT );
00067     // create obb trees
00068     rval = GQT->gttool()->setup_implicit_complement();CHECK_ERR( rval );
00069     rval = GQT->gttool()->construct_obb_trees();CHECK_ERR( rval );
00070 }
00071 
00072 void gqt_load_file_dagmc_internal_build_obb()
00073 {
00074     /* 3 - Test with internal moab, load file in MOAB*/
00075     // find geomsets, build implicit complement, and construct obb trees
00076     ErrorCode rval;
00077 
00078     MBI  = new moab::Core();
00079     rval = MBI->load_file( input_file.c_str() );  // open the
00080     CHECK_ERR( rval );
00081     GTT  = new GeomTopoTool( MBI, true, 0 );
00082     GQT  = new GeomQueryTool( GTT );
00083     rval = GQT->gttool()->setup_implicit_complement();CHECK_ERR( rval );
00084     rval = GQT->gttool()->construct_obb_trees();CHECK_ERR( rval );
00085 }
00086 
00087 void gqt_test_obb_retreval()
00088 {
00089     // make new dagmc
00090     std::cout << "test_obb_retreval" << std::endl;
00091 
00092     ErrorCode rval;
00093     // load a file
00094     MBI  = new moab::Core();
00095     rval = MBI->load_file( input_file.c_str() );  // open the
00096     CHECK_ERR( rval );
00097     GTT = new GeomTopoTool( MBI, true, 0 );
00098     GQT = new GeomQueryTool( GTT );
00099     GQT->initialize();CHECK_ERR( rval );
00100 
00101     // write to file
00102     rval = MBI->write_file( "fcad" );CHECK_ERR( rval );
00103 
00104     // cleanup instances
00105     delete GQT;
00106     delete GTT;
00107     delete MBI;
00108 
00109     // re-load written file
00110     MBI  = new moab::Core();
00111     rval = MBI->load_file( "fcad" );  // open the
00112     CHECK_ERR( rval );
00113     GTT  = new GeomTopoTool( MBI, true, 0 );
00114     GQT  = new GeomQueryTool( GTT );
00115     rval = GQT->initialize();CHECK_ERR( rval );
00116 
00117     // remove file
00118     remove( "fcad" );
00119 }
00120 
00121 void gqt_create_impl_compl()
00122 {
00123     ErrorCode rval = GQT->gttool()->setup_implicit_complement();CHECK_ERR( rval );
00124 }
00125 
00126 void gqt_build_obb()
00127 {
00128     ErrorCode rval = GQT->gttool()->construct_obb_trees();CHECK_ERR( rval );
00129 }
00130 
00131 void gqt_num_vols()
00132 {
00133     int expect_num_vols = 2;
00134     int num_vols        = GQT->gttool()->num_ents_of_dim( 3 );
00135     CHECK_EQUAL( expect_num_vols, num_vols );
00136 }
00137 
00138 void gqt_point_in()
00139 {
00140     int result        = 0;
00141     int expect_result = 1;
00142     int vol_idx       = 1;
00143     double xyz[3]     = { 0.0, 0.0, 0.0 };
00144     EntityHandle vol_h;
00145 
00146     // load file and create instances
00147     MBI            = new moab::Core();
00148     ErrorCode rval = MBI->load_file( input_file.c_str() );  // open the
00149     CHECK_ERR( rval );
00150     GTT = new GeomTopoTool( MBI, true, 0 );
00151     GQT = new GeomQueryTool( GTT );
00152     // initialize for geom queries
00153     GQT->initialize();
00154 
00155     // get volume
00156     vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
00157     // test point in volume
00158     GQT->point_in_volume( vol_h, xyz, result );CHECK_ERR( rval );
00159     CHECK_EQUAL( expect_result, result );
00160 }
00161 
00162 void gqt_test_obb_retreval_rayfire()
00163 {
00164     // make new dagmc
00165     std::cout << "test_obb_retreval and ray_fire" << std::endl;
00166 
00167     ErrorCode rval;
00168 
00169     // load file and initialize
00170     MBI  = new moab::Core();
00171     rval = MBI->load_file( input_file.c_str() );  // open the
00172     CHECK_ERR( rval );
00173     GTT = new GeomTopoTool( MBI, true, 0 );
00174     GQT = new GeomQueryTool( GTT );
00175     GQT->initialize();CHECK_ERR( rval );
00176 
00177     // write to file
00178     rval = MBI->write_file( "fcad" );CHECK_ERR( rval );
00179 
00180     // delete instances
00181     delete GTT;
00182     delete GQT;
00183     delete MBI;
00184 
00185     // load the file that was written and initialize
00186     MBI  = new moab::Core();
00187     rval = MBI->load_file( "fcad" );  // open the
00188     CHECK_ERR( rval );
00189     GTT  = new GeomTopoTool( MBI, true, 0 );
00190     GQT  = new GeomQueryTool( GTT );
00191     rval = GQT->initialize();CHECK_ERR( rval );
00192 
00193     // remove generated test file
00194     remove( "fcad" );
00195 
00196     // now perform full ray fire
00197     double eps  = 1.e-6;
00198     int vol_idx = 1;
00199     // note model is cube of side 10, centred at 0,0,0, so ray fire along
00200     // any unit direction should be exactly 5.0
00201     double xyz[3] = { 0.0, 0.0, 0.0 };
00202     double dir[3] = { 0.0, 0.0, 1.0 };
00203     EntityHandle next_surf;
00204     double next_surf_dist;
00205     double expect_next_surf_dist = 5.0;
00206     EntityHandle vol_h;
00207 
00208     // get volume handle to fire on
00209     vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
00210     // test ray fire
00211     rval = GQT->ray_fire( vol_h, xyz, dir, next_surf, next_surf_dist );CHECK_ERR( rval );
00212     CHECK_REAL_EQUAL( expect_next_surf_dist, next_surf_dist, eps );
00213 }
00214 
00215 void gqt_rayfire()
00216 {
00217     const double eps = 1e-6;  // epsilon for test, faceting tol?
00218 
00219     int vol_idx = 1;
00220     // note model is cube of side 10, centred at 0,0,0, so ray fire along
00221     // any unit direction should be exactly 5.0
00222     double xyz[3] = { 0.0, 0.0, 0.0 };
00223     double dir[3] = { 0.0, 0.0, 1.0 };
00224     EntityHandle next_surf;
00225     double next_surf_dist;
00226     double expect_next_surf_dist = 5.0;
00227     EntityHandle vol_h;
00228     ErrorCode rval;
00229 
00230     MBI  = new moab::Core();
00231     rval = MBI->load_file( input_file.c_str() );  // open the
00232     CHECK_ERR( rval );
00233     GTT = new GeomTopoTool( MBI, true, 0 );
00234     GQT = new GeomQueryTool( GTT );
00235     GQT->initialize();CHECK_ERR( rval );
00236 
00237     vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
00238     rval  = GQT->ray_fire( vol_h, xyz, dir, next_surf, next_surf_dist );CHECK_ERR( rval );
00239     CHECK_REAL_EQUAL( expect_next_surf_dist, next_surf_dist, eps );
00240 }
00241 
00242 void gqt_closest_to()
00243 {
00244     const double eps = 1e-6;  // epsilon for test, faceting tol?
00245 
00246     int vol_idx = 1;
00247     // note model is cube of side 10, centred at 0,0,0, so ray fire along
00248     // any unit direction should be exactly 5.0
00249     double xyz[3] = { -6.0, 0.0, 0.0 };
00250     double distance;  // distance from point to nearest surface
00251     double expect_distance = 1.0;
00252     EntityHandle vol_h;
00253     ErrorCode rval;
00254 
00255     // get the volume handle
00256     vol_h = GQT->gttool()->entity_by_id( 3, vol_idx );
00257 
00258     // test closest to location
00259     rval = GQT->closest_to_location( vol_h, xyz, distance );CHECK_ERR( rval );
00260 
00261     // distance should be 1.0 cm
00262     CHECK_REAL_EQUAL( expect_distance, distance, eps );
00263 }
00264 
00265 void gqt_test_boundary()
00266 {
00267     int vol_idx         = 1;
00268     EntityHandle vol_h  = GQT->gttool()->entity_by_id( 3, vol_idx );
00269     int surf_idx        = 1;
00270     EntityHandle surf_h = GQT->gttool()->entity_by_id( 2, surf_idx );
00271 
00272     double xyz[3] = { 0.0, 0.0, 5.0 };
00273     double dir[3] = { 0.0, 0.0, 1.0 };
00274     int result;
00275     int expect_result = 0;
00276 
00277     // test vol boundary
00278     ErrorCode rval = GQT->test_volume_boundary( vol_h, surf_h, xyz, dir, result );CHECK_ERR( rval );
00279     // check ray leaving volume
00280     CHECK_EQUAL( expect_result, result );
00281 }
00282 
00283 void gqt_point_in_box_1()
00284 {
00285     int vol_id         = 1;
00286     double origin[3]   = { 0.0, 0.0, 0.0 };
00287     EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
00288     int result;
00289     ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
00290     CHECK_EQUAL( rval, moab::MB_SUCCESS );
00291     CHECK_EQUAL( result, 1 );  // inside
00292 }
00293 
00294 void gqt_point_in_box_2()
00295 {
00296     int vol_id         = 1;
00297     double origin[3]   = { 5.0, 0.0, 0.0 };
00298     EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
00299     int result;
00300     ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
00301     CHECK_EQUAL( rval, moab::MB_SUCCESS );
00302     CHECK_EQUAL( result, 1 );  // inside
00303 }
00304 
00305 void gqt_point_in_box_3()
00306 {
00307     int vol_id         = 1;
00308     double origin[3]   = { 5.1, 0.0, 0.0 };
00309     EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
00310     int result;
00311     ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
00312     CHECK_EQUAL( rval, moab::MB_SUCCESS );
00313     CHECK_EQUAL( result, 0 );  // outside
00314 }
00315 
00316 void gqt_point_in_box_4()
00317 {
00318     int vol_id         = 1;
00319     double origin[3]   = { 5.1, 5.1, 5.1 };
00320     EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
00321     int result;
00322     ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
00323     CHECK_EQUAL( rval, moab::MB_SUCCESS );
00324     CHECK_EQUAL( result, 0 );  // outside
00325 }
00326 
00327 void gqt_point_in_box_5()
00328 {
00329     int vol_id         = 1;
00330     double origin[3]   = { 5.0 - 1.e-308, 5.0 - 1.e-308, 5.0 - 1.e-308 };
00331     EntityHandle vol_h = GQT->gttool()->entity_by_id( 3, vol_id );
00332     int result;
00333     ErrorCode rval = GQT->point_in_box( vol_h, origin, result );
00334     CHECK_EQUAL( rval, moab::MB_SUCCESS );
00335     CHECK_EQUAL( result, 1 );  // outside
00336 }
00337 
00338 void cleanup()
00339 {
00340     // cleanup instances
00341     delete GQT;
00342     delete GTT;
00343     delete MBI;
00344 }
00345 
00346 int main( int /* argc */, char** /* argv */ )
00347 {
00348     int result = 0;
00349 
00350     result += RUN_TEST( gqt_load_file );          // test ray fire
00351     result += RUN_TEST( gqt_build_obb );          // build the obb
00352     result += RUN_TEST( gqt_create_impl_compl );  // build the obb
00353     result += RUN_TEST( gqt_num_vols );           // make sure the num of vols correct
00354     cleanup();
00355 
00356     result += RUN_TEST( gqt_load_file_dagmc_build_obb );  //
00357     cleanup();
00358 
00359     result += RUN_TEST( gqt_load_file_dagmc_via_moab_build_obb );  //
00360     cleanup();
00361 
00362     result += RUN_TEST( gqt_load_file_dagmc_internal_build_obb );  //
00363     cleanup();
00364 
00365     result += RUN_TEST( gqt_test_obb_retreval );  // check that we are retreving loaded obbs
00366     cleanup();
00367 
00368     result += RUN_TEST( gqt_test_obb_retreval_rayfire );  // check that we can ray fire on loaded obbs
00369     cleanup();
00370 
00371     result += RUN_TEST( gqt_point_in );  // check entity by point
00372     cleanup();
00373 
00374     result += RUN_TEST( gqt_rayfire );        // ensure ray fire distance is correct
00375     result += RUN_TEST( gqt_closest_to );     // check the distance to surface nearest point
00376     result += RUN_TEST( gqt_test_boundary );  // check particle entering leaving
00377     cleanup();
00378 
00379     return result;
00380 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines