Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
PointInElementSearch.cpp
Go to the documentation of this file.
1 /** @example PointInElementSearch.cpp
2  * This example demonstrates how to perform local point-in-element searches with MOAB's SpatialLocator
3  * functionality. It shows how to build spatial trees and perform efficient point location queries
4  * over a local or parallel mesh using different tree types and element basis functions.
5  */
6 
7 #include <iostream>
8 #include <cstdlib>
9 #include <cstdio>
10 
11 #include "moab/Core.hpp"
12 #include "moab/Interface.hpp"
13 #include "moab/Range.hpp"
14 #include "moab/AdaptiveKDTree.hpp"
15 #include "moab/ElemEvaluator.hpp"
16 #include "moab/CN.hpp"
17 #include "moab/SpatialLocator.hpp"
18 
19 using namespace moab;
20 using namespace std;
21 
22 #ifdef MOAB_HAVE_HDF5
23 string test_file_name = string( MESH_DIR ) + string( "/64bricks_512hex_256part.h5m" );
24 #else
25 string test_file_name = string( MESH_DIR ) + string( "/mbtest1.vtk" );
26 #endif
27 int main( int argc, char** argv )
28 {
29  int num_queries = 1000000;
30 
31  if( argc > 3 )
32  {
33  cout << "Usage: " << argv[0] << " <filename> [num_queries]" << endl;
34  return 0;
35  }
36  else if( argc == 3 )
37  {
38  test_file_name = argv[1];
39  num_queries = atoi( argv[2] );
40  }
41  else
42  {
43  num_queries = 100;
44  }
45 
46  // Instantiate
47  Core mb;
48 
49  // Load the file
50  MB_CHK_SET_ERR( mb.load_file( test_file_name.c_str() ), "Error loading file" );
51 
52  // Get all 3d elements in the file
53  Range elems;
54  MB_CHK_SET_ERR( mb.get_entities_by_dimension( 0, 3, elems ), "Error getting 3d elements" );
55 
56  // Create a tree to use for the location service
57  AdaptiveKDTree tree( &mb );
58 
59  // Specify an evaluator based on linear hexes
60  ElemEvaluator el_eval( &mb );
61 
62  // Build the SpatialLocator
63  SpatialLocator sl( &mb, elems, &tree );
64 
65  // Get the box extents
66  CartVect box_extents, pos;
67  BoundBox box = sl.local_box();
68  box_extents = box.bMax - box.bMin;
69 
70  // Query at random places in the tree
72  int is_inside = 0;
73  int num_inside = 0;
74  EntityHandle elem;
75  for( int i = 0; i < num_queries; i++ )
76  {
77  pos = box.bMin + CartVect( box_extents[0] * .01 * ( rand() % 100 ), box_extents[1] * .01 * ( rand() % 100 ),
78  box_extents[2] * .01 * ( rand() % 100 ) );
79  MB_CHK_ERR( sl.locate_point( pos.array(), elem, params.array(), &is_inside, 0.0, 0.0 ) );
80  if( is_inside ) num_inside++;
81  }
82 
83  cout << "Mesh contains " << elems.size() << " elements of type "
84  << CN::EntityTypeName( mb.type_from_handle( *elems.begin() ) ) << endl;
85  cout << "Bounding box min-max = (" << box.bMin[0] << "," << box.bMin[1] << "," << box.bMin[2] << ")-("
86  << box.bMax[0] << "," << box.bMax[1] << "," << box.bMax[2] << ")" << endl;
87  cout << "Queries inside box = " << num_inside << "/" << num_queries << " = "
88  << 100.0 * ( (double)num_inside ) / num_queries << "%" << endl;
89 
90  return 0;
91 }