moab
|
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 if (1 == argc) { 00012 std::cout << "Usage: " << argv[0] << " <filename>" << std::endl; 00013 return 0; 00014 } 00015 00016 // instantiate & load a mesh from a file 00017 moab::Core *mb = new moab::Core(); 00018 moab::ErrorCode rval = mb->load_mesh(argv[1]); 00019 00020 // get all triangles 00021 moab::EntityHandle tree_root; 00022 moab::Range tris; 00023 //moab::OrientedBoxTreeTool::Settings settings; 00024 00025 rval = mb->get_entities_by_type(0, moab::MBTRI, tris); 00026 if (rval != moab::MB_SUCCESS) { 00027 std::cerr << "Couldn't get triangles." << std::endl; 00028 delete mb; 00029 return 1; 00030 } 00031 00032 // build OBB trees for all triangles 00033 moab::OrientedBoxTreeTool tool(mb); 00034 //rval = tool.build(tris, tree_root, &settings); 00035 rval = tool.build(tris, tree_root); 00036 if (rval != moab::MB_SUCCESS) { 00037 std::cerr << "Could'nt build tree." << std::endl; 00038 delete mb; 00039 return 1; 00040 } 00041 00042 // build box 00043 double box_center[3], box_axis1[3], box_axis2[3], box_axis3[3], pnt_start[3], ray_length; 00044 rval = tool.box(tree_root, box_center, box_axis1, box_axis2, box_axis3); 00045 if (rval != moab::MB_SUCCESS) { 00046 std::cerr << "Couldn't get box for tree root set."; 00047 delete mb; 00048 return 1; 00049 } 00050 00051 ray_length = 2.*sqrt(box_axis1[0]*box_axis1[0] + box_axis1[1]*box_axis1[1] + 00052 box_axis1[2]*box_axis1[2]); 00053 00054 // do ray-tracing from box center side to x direction 00055 std::vector<double> intersections; 00056 std::vector<moab::EntityHandle> intersection_facets; 00057 00058 for (int i = 0; i < 3; i++) 00059 pnt_start[i] = box_center[i] - box_axis1[i]; 00060 00061 if (ray_length > 0) { // normalize ray direction 00062 for (int j = 0; j < 3; j++) 00063 box_axis1[j] = 2 * box_axis1[j] / ray_length; 00064 } 00065 rval = tool.ray_intersect_triangles(intersections, intersection_facets, 00066 tree_root, 10e-12, pnt_start, box_axis1, 00067 &ray_length); 00068 if (rval != moab::MB_SUCCESS) { 00069 std::cerr << "Couldn't ray tracing."; 00070 delete mb; 00071 return 1; 00072 } 00073 00074 std::cout << "ray start point: " << pnt_start[0] << " " 00075 << pnt_start[1] << " " << pnt_start[2] << std::endl; 00076 std::cout << " ray direction: " << box_axis1[0] << " " << box_axis1[1] << " " << box_axis1[2] << "\n"; 00077 std::cout << "# of intersections : " << intersections.size() << std::endl; 00078 std::cout << "intersection distances are on"; 00079 for (unsigned int i = 0; i < intersections.size(); i++) 00080 std::cout << " " << intersections[i]; 00081 std::cout << " of ray length " << ray_length << std::endl; 00082 00083 delete mb; 00084 00085 return 0; 00086 }