Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ObbTree.cpp
Go to the documentation of this file.
1 /** @example ObbTree.cpp
2  * This example demonstrates oriented bounding box (OBB) tree construction and ray tracing.
3  * It shows how to load a triangular mesh, build an oriented bounding box tree for spatial queries,
4  * and perform ray tracing through the mesh to calculate intersection distances and facets.
5  * The OBB tree provides efficient spatial partitioning for ray tracing and collision detection applications.
6  */
7 // simple example construct obb tree and ray-tracing the tree
8 // it reads triangle mesh, construct obb tree and get intersection distances
9 
10 #include "moab/Core.hpp"
11 #include "moab/Range.hpp"
13 #include <iostream>
14 #include <cmath>
15 
16 int main( int argc, char** argv )
17 {
18  if( 1 == argc )
19  {
20  std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
21  return 0;
22  }
23 
24  // instantiate & load a mesh from a file
25  moab::Core* mb = new moab::Core();
26  MB_CHK_SET_ERR( mb->load_mesh( argv[1] ), "Couldn't load mesh" );
27 
28  // get all triangles
29  moab::EntityHandle tree_root;
30  moab::Range tris;
31  // moab::OrientedBoxTreeTool::Settings settings;
32 
33  MB_CHK_SET_ERR( mb->get_entities_by_type( 0, moab::MBTRI, tris ), "Couldn't get triangles" );
34 
35  // build OBB trees for all triangles
37  // rval = tool.build(tris, tree_root, &settings);
38  MB_CHK_SET_ERR( tool.build( tris, tree_root ), "Couldn't build tree" );
39 
40  // build box
41  double box_center[3], box_axis1[3], box_axis2[3], box_axis3[3], pnt_start[3], ray_length;
42  MB_CHK_SET_ERR( tool.box( tree_root, box_center, box_axis1, box_axis2, box_axis3 ),
43  "Couldn't get box for tree root set" );
44 
45  ray_length = 2. * sqrt( box_axis1[0] * box_axis1[0] + box_axis1[1] * box_axis1[1] + box_axis1[2] * box_axis1[2] );
46 
47  // do ray-tracing from box center side to x direction
48  std::vector< double > intersections;
49  std::vector< moab::EntityHandle > intersection_facets;
50 
51  for( int i = 0; i < 3; i++ )
52  pnt_start[i] = box_center[i] - box_axis1[i];
53 
54  if( ray_length > 0 )
55  { // normalize ray direction
56  for( int j = 0; j < 3; j++ )
57  box_axis1[j] = 2 * box_axis1[j] / ray_length;
58  }
59  MB_CHK_SET_ERR( tool.ray_intersect_triangles( intersections, intersection_facets, tree_root, 10e-12, pnt_start,
60  box_axis1, &ray_length ),
61  "Couldn't perform ray tracing" );
62 
63  std::cout << "ray start point: " << pnt_start[0] << " " << pnt_start[1] << " " << pnt_start[2] << std::endl;
64  std::cout << " ray direction: " << box_axis1[0] << " " << box_axis1[1] << " " << box_axis1[2] << "\n";
65  std::cout << "# of intersections : " << intersections.size() << std::endl;
66  std::cout << "intersection distances are on";
67  for( unsigned int i = 0; i < intersections.size(); i++ )
68  std::cout << " " << intersections[i];
69  std::cout << " of ray length " << ray_length << std::endl;
70 
71  delete mb;
72 
73  return 0;
74 }