MOAB: Mesh Oriented datABase  (version 5.5.0)
spatial_locator_test.cpp
Go to the documentation of this file.
1 #include "moab/Core.hpp"
3 #include "moab/Tree.hpp"
4 #include "moab/HomXform.hpp"
5 #include "moab/ScdInterface.hpp"
6 #include "moab/CartVect.hpp"
8 #include "moab/BVHTree.hpp"
9 #include "moab/ElemEvaluator.hpp"
10 #include "moab/ProgOptions.hpp"
11 #include "moab/CpuTimer.hpp"
12 
13 #ifdef MOAB_HAVE_MPI
14 #include "moab_mpi.h"
15 #endif
16 
17 #include "TestUtil.hpp"
18 
19 #include <cstdlib>
20 #include <sstream>
21 
22 using namespace moab;
23 
24 void test_kd_tree();
25 void test_bvh_tree();
26 void test_locator( SpatialLocator* sl );
27 
28 ErrorCode create_hex_mesh( Interface& mb, Range& elems, int n, int dim );
29 
30 int max_depth = 30;
31 int npoints = 1000;
32 int leaf = 6;
33 bool print_tree = false;
34 int ints = 10;
35 
36 int main( int argc, char** argv )
37 {
38 #ifdef MOAB_HAVE_MPI
39  int fail = MPI_Init( &argc, &argv );
40  if( fail ) return fail;
41 #else
42  // silence the warning of parameters not used, in serial; there should be a smarter way :(
43  argv[0] = argv[argc - argc];
44 #endif
45 
46  ProgOptions po( "spatial_locator_test options" );
47  po.addOpt< int >( "ints,i", "Number of intervals on each side of scd mesh", &ints );
48  po.addOpt< int >( "leaf,l", "Maximum number of elements per leaf", &leaf );
49  po.addOpt< int >( "max_depth,m", "Maximum depth of tree", &max_depth );
50  po.addOpt< int >( "npoints,n", "Number of query points", &npoints );
51  po.addOpt< void >( "print,p", "Print tree details", &print_tree );
52  po.parseCommandLine( argc, argv );
53 
56 
57 #ifdef MOAB_HAVE_MPI
58  fail = MPI_Finalize();
59  if( fail ) return fail;
60 #endif
61 
62  return 0;
63 }
64 
66 {
67  ErrorCode rval;
68  Core mb;
69 
70  // create a simple mesh to test
71  Range elems;
72  rval = create_hex_mesh( mb, elems, ints, 3 );CHECK_ERR( rval );
73 
74  // initialize spatial locator with the elements and KDtree
75  AdaptiveKDTree kd( &mb );
76  std::ostringstream opts;
77  opts << "MAX_DEPTH=" << max_depth << ";MAX_PER_LEAF=" << leaf;
78  FileOptions fo( opts.str().c_str() );
79  rval = kd.parse_options( fo );
80  SpatialLocator* sl = new SpatialLocator( &mb, elems, &kd );
81 
82  test_locator( sl );
83 
84  // test with an evaluator
85  ElemEvaluator eval( &mb );
86  kd.set_eval( &eval );
87  test_locator( sl );
88 
89  // destroy spatial locator, and tree along with it
90  delete sl;
91 }
92 
94 {
95  ErrorCode rval;
96  Core mb;
97 
98  // create a simple mesh to test
99  Range elems;
100  rval = create_hex_mesh( mb, elems, ints, 3 );CHECK_ERR( rval );
101 
102  // initialize spatial locator with the elements and a BVH tree
103  BVHTree bvh( &mb );
104  std::ostringstream opts;
105  opts << "MAX_DEPTH=" << max_depth << ";MAX_PER_LEAF=" << leaf;
106  FileOptions fo( opts.str().c_str() );
107  rval = bvh.parse_options( fo );
108  SpatialLocator* sl = new SpatialLocator( &mb, elems, &bvh );
109  test_locator( sl );
110 
111  // test with an evaluator
112  ElemEvaluator eval( &mb );
113  bvh.set_eval( &eval );
114  test_locator( sl );
115 
116  // destroy spatial locator, and tree along with it
117  delete sl;
118 }
119 
121 {
122  CartVect box_del, test_pt, test_res;
123  BoundBox box = sl->local_box();
124  box_del = box.bMax - box.bMin;
125 
126  double denom = 1.0 / (double)RAND_MAX;
127  int is_in;
128  EntityHandle ent = 0;
129  ErrorCode rval;
130  for( int i = 0; i < npoints; i++ )
131  {
132  // generate a small number of random point to test
133  double rx = (double)rand() * denom, ry = (double)rand() * denom, rz = (double)rand() * denom;
134  test_pt = box.bMin + CartVect( rx * box_del[0], ry * box_del[1], rz * box_del[2] );
135 
136  // call spatial locator to locate points
137  rval = sl->locate_points( test_pt.array(), 1, &ent, test_res.array(), &is_in );CHECK_ERR( rval );
138 
139  // verify that the point was found
140  CHECK_EQUAL( is_in, true );
141  }
142 
143  std::cout << "Traversal stats:" << std::endl;
145 
146  if( print_tree )
147  {
148  std::cout << "Tree information: " << std::endl;
149  rval = sl->get_tree()->print();CHECK_ERR( rval );
150  }
151 }
152 
154 {
155  ScdInterface* scdi;
156  ErrorCode rval = mb.query_interface( scdi );CHECK_ERR( rval );
157  HomCoord high( n - 1, -1, -1 );
158  if( dim > 1 ) high[1] = n - 1;
159  if( dim > 2 ) high[2] = n - 1;
160  ScdBox* new_box;
161  rval = scdi->construct_box( HomCoord( 0, 0, 0 ), high, NULL, 0, new_box );CHECK_ERR( rval );
162  rval = mb.release_interface( scdi );CHECK_ERR( rval );
163 
164  rval = mb.get_entities_by_dimension( 0, dim, elems );CHECK_ERR( rval );
165 
166  return rval;
167 }