Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
ObbTree.cpp
Go to the documentation of this file.
1 // simple example construct obb tree and ray-tracing the tree
2 // it reads triangle mesh, construct obb tree and get intersection distances
3 
4 #include "moab/Core.hpp"
5 #include "moab/Range.hpp"
7 #include <iostream>
8 #include <cmath>
9 
10 int main( int argc, char** argv )
11 {
12  if( 1 == argc )
13  {
14  std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
15  return 0;
16  }
17 
18  // instantiate & load a mesh from a file
19  moab::Core* mb = new moab::Core();
20  moab::ErrorCode rval = mb->load_mesh( argv[1] );
21  if( rval != moab::MB_SUCCESS )
22  {
23  std::cerr << "Couldn't load mesh." << std::endl;
24  delete mb;
25  return 1;
26  }
27 
28  // get all triangles
29  moab::EntityHandle tree_root;
30  moab::Range tris;
31  // moab::OrientedBoxTreeTool::Settings settings;
32 
33  rval = mb->get_entities_by_type( 0, moab::MBTRI, tris );
34  if( rval != moab::MB_SUCCESS )
35  {
36  std::cerr << "Couldn't get triangles." << std::endl;
37  delete mb;
38  return 1;
39  }
40 
41  // build OBB trees for all triangles
43  // rval = tool.build(tris, tree_root, &settings);
44  rval = tool.build( tris, tree_root );
45  if( rval != moab::MB_SUCCESS )
46  {
47  std::cerr << "Could'nt build tree." << std::endl;
48  delete mb;
49  return 1;
50  }
51 
52  // build box
53  double box_center[3], box_axis1[3], box_axis2[3], box_axis3[3], pnt_start[3], ray_length;
54  rval = tool.box( tree_root, box_center, box_axis1, box_axis2, box_axis3 );
55  if( rval != moab::MB_SUCCESS )
56  {
57  std::cerr << "Couldn't get box for tree root set.";
58  delete mb;
59  return 1;
60  }
61 
62  ray_length = 2. * sqrt( box_axis1[0] * box_axis1[0] + box_axis1[1] * box_axis1[1] + box_axis1[2] * box_axis1[2] );
63 
64  // do ray-tracing from box center side to x direction
65  std::vector< double > intersections;
66  std::vector< moab::EntityHandle > intersection_facets;
67 
68  for( int i = 0; i < 3; i++ )
69  pnt_start[i] = box_center[i] - box_axis1[i];
70 
71  if( ray_length > 0 )
72  { // normalize ray direction
73  for( int j = 0; j < 3; j++ )
74  box_axis1[j] = 2 * box_axis1[j] / ray_length;
75  }
76  rval = tool.ray_intersect_triangles( intersections, intersection_facets, tree_root, 10e-12, pnt_start, box_axis1,
77  &ray_length );
78  if( rval != moab::MB_SUCCESS )
79  {
80  std::cerr << "Couldn't ray tracing.";
81  delete mb;
82  return 1;
83  }
84 
85  std::cout << "ray start point: " << pnt_start[0] << " " << pnt_start[1] << " " << pnt_start[2] << std::endl;
86  std::cout << " ray direction: " << box_axis1[0] << " " << box_axis1[1] << " " << box_axis1[2] << "\n";
87  std::cout << "# of intersections : " << intersections.size() << std::endl;
88  std::cout << "intersection distances are on";
89  for( unsigned int i = 0; i < intersections.size(); i++ )
90  std::cout << " " << intersections[i];
91  std::cout << " of ray length " << ray_length << std::endl;
92 
93  delete mb;
94 
95  return 0;
96 }