MOAB: Mesh Oriented datABase  (version 5.2.1)
ObbTree.cpp
Go to the documentation of this file.
00001 // simple example construct obb tree and ray-tracing the tree
00002 // it reads triangle mesh, construct obb tree and get intersection distances
00003 
00004 #include "moab/Core.hpp"
00005 #include "moab/Range.hpp"
00006 #include "moab/OrientedBoxTreeTool.hpp"
00007 #include <iostream>
00008 #include <math.h>
00009 
00010 int main( int argc, char** argv )
00011 {
00012     if( 1 == argc )
00013     {
00014         std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
00015         return 0;
00016     }
00017 
00018     // instantiate & load a mesh from a file
00019     moab::Core* mb       = new moab::Core();
00020     moab::ErrorCode rval = mb->load_mesh( argv[1] );
00021     if( rval != moab::MB_SUCCESS )
00022     {
00023         std::cerr << "Couldn't load mesh." << std::endl;
00024         delete mb;
00025         return 1;
00026     }
00027 
00028     // get all triangles
00029     moab::EntityHandle tree_root;
00030     moab::Range tris;
00031     // moab::OrientedBoxTreeTool::Settings settings;
00032 
00033     rval = mb->get_entities_by_type( 0, moab::MBTRI, tris );
00034     if( rval != moab::MB_SUCCESS )
00035     {
00036         std::cerr << "Couldn't get triangles." << std::endl;
00037         delete mb;
00038         return 1;
00039     }
00040 
00041     // build OBB trees for all triangles
00042     moab::OrientedBoxTreeTool tool( mb );
00043     // rval = tool.build(tris, tree_root, &settings);
00044     rval = tool.build( tris, tree_root );
00045     if( rval != moab::MB_SUCCESS )
00046     {
00047         std::cerr << "Could'nt build tree." << std::endl;
00048         delete mb;
00049         return 1;
00050     }
00051 
00052     // build box
00053     double box_center[3], box_axis1[3], box_axis2[3], box_axis3[3], pnt_start[3], ray_length;
00054     rval = tool.box( tree_root, box_center, box_axis1, box_axis2, box_axis3 );
00055     if( rval != moab::MB_SUCCESS )
00056     {
00057         std::cerr << "Couldn't get box for tree root set.";
00058         delete mb;
00059         return 1;
00060     }
00061 
00062     ray_length = 2. * sqrt( box_axis1[0] * box_axis1[0] + box_axis1[1] * box_axis1[1] + box_axis1[2] * box_axis1[2] );
00063 
00064     // do ray-tracing from box center side to x direction
00065     std::vector< double > intersections;
00066     std::vector< moab::EntityHandle > intersection_facets;
00067 
00068     for( int i = 0; i < 3; i++ )
00069         pnt_start[i] = box_center[i] - box_axis1[i];
00070 
00071     if( ray_length > 0 )
00072     {  // normalize ray direction
00073         for( int j = 0; j < 3; j++ )
00074             box_axis1[j] = 2 * box_axis1[j] / ray_length;
00075     }
00076     rval = tool.ray_intersect_triangles( intersections, intersection_facets, tree_root, 10e-12, pnt_start, box_axis1,
00077                                          &ray_length );
00078     if( rval != moab::MB_SUCCESS )
00079     {
00080         std::cerr << "Couldn't ray tracing.";
00081         delete mb;
00082         return 1;
00083     }
00084 
00085     std::cout << "ray start point: " << pnt_start[0] << " " << pnt_start[1] << " " << pnt_start[2] << std::endl;
00086     std::cout << " ray direction: " << box_axis1[0] << " " << box_axis1[1] << " " << box_axis1[2] << "\n";
00087     std::cout << "# of intersections : " << intersections.size() << std::endl;
00088     std::cout << "intersection distances are on";
00089     for( unsigned int i = 0; i < intersections.size(); i++ )
00090         std::cout << " " << intersections[i];
00091     std::cout << " of ray length " << ray_length << std::endl;
00092 
00093     delete mb;
00094 
00095     return 0;
00096 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines