Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
PointInElementSearch.cpp
Go to the documentation of this file.
00001 /** \brief This test shows how to perform local point-in-element searches with MOAB's new tree
00002  * searching functionality.
00003  *
00004  * MOAB's SpatialLocator functionality performs point-in-element searches over a local or parallel
00005  * mesh. SpatialLocator is flexible as to what kind of tree is used and what kind of element basis
00006  * functions are used to localize elements and interpolate local fields.
00007  */
00008 
00009 #include <iostream>
00010 #include <cstdlib>
00011 #include <cstdio>
00012 
00013 #include "moab/Core.hpp"
00014 #include "moab/Interface.hpp"
00015 #include "moab/Range.hpp"
00016 #include "moab/AdaptiveKDTree.hpp"
00017 #include "moab/ElemEvaluator.hpp"
00018 #include "moab/CN.hpp"
00019 #include "moab/SpatialLocator.hpp"
00020 
00021 using namespace moab;
00022 using namespace std;
00023 
00024 #ifdef MOAB_HAVE_HDF5
00025 string test_file_name = string( MESH_DIR ) + string( "/64bricks_512hex_256part.h5m" );
00026 #else
00027 string test_file_name = string( MESH_DIR ) + string( "/mbtest1.vtk" );
00028 #endif
00029 int main( int argc, char** argv )
00030 {
00031     int num_queries = 1000000;
00032 
00033     if( argc > 3 )
00034     {
00035         cout << "Usage: " << argv[0] << " <filename> [num_queries]" << endl;
00036         return 0;
00037     }
00038     else if( argc == 3 )
00039     {
00040         test_file_name = argv[1];
00041         num_queries    = atoi( argv[2] );
00042     }
00043     else
00044     {
00045         num_queries = 100;
00046     }
00047 
00048     // Instantiate
00049     Core mb;
00050 
00051     // Load the file
00052     ErrorCode rval = mb.load_file( test_file_name.c_str() );MB_CHK_SET_ERR( rval, "Error loading file" );
00053 
00054     // Get all 3d elements in the file
00055     Range elems;
00056     rval = mb.get_entities_by_dimension( 0, 3, elems );MB_CHK_SET_ERR( rval, "Error getting 3d elements" );
00057 
00058     // Create a tree to use for the location service
00059     AdaptiveKDTree tree( &mb );
00060 
00061     // Specify an evaluator based on linear hexes
00062     ElemEvaluator el_eval( &mb );
00063 
00064     // Build the SpatialLocator
00065     SpatialLocator sl( &mb, elems, &tree );
00066 
00067     // Get the box extents
00068     CartVect box_extents, pos;
00069     BoundBox box = sl.local_box();
00070     box_extents  = box.bMax - box.bMin;
00071 
00072     // Query at random places in the tree
00073     CartVect params;
00074     int is_inside  = 0;
00075     int num_inside = 0;
00076     EntityHandle elem;
00077     for( int i = 0; i < num_queries; i++ )
00078     {
00079         pos  = box.bMin + CartVect( box_extents[0] * .01 * ( rand() % 100 ), box_extents[1] * .01 * ( rand() % 100 ),
00080                                     box_extents[2] * .01 * ( rand() % 100 ) );
00081         rval = sl.locate_point( pos.array(), elem, params.array(), &is_inside, 0.0, 0.0 );MB_CHK_ERR( rval );
00082         if( is_inside ) num_inside++;
00083     }
00084 
00085     cout << "Mesh contains " << elems.size() << " elements of type "
00086          << CN::EntityTypeName( mb.type_from_handle( *elems.begin() ) ) << endl;
00087     cout << "Bounding box min-max = (" << box.bMin[0] << "," << box.bMin[1] << "," << box.bMin[2] << ")-("
00088          << box.bMax[0] << "," << box.bMax[1] << "," << box.bMax[2] << ")" << endl;
00089     cout << "Queries inside box = " << num_inside << "/" << num_queries << " = "
00090          << 100.0 * ( (double)num_inside ) / num_queries << "%" << endl;
00091 
00092     return 0;
00093 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines