moab
point_in_elem_search.cpp
Go to the documentation of this file.
00001 
00008 #include <iostream>
00009 #include <cstdlib>
00010 #include <cstdio>
00011 
00012 #include "moab/Core.hpp"
00013 #include "moab/Interface.hpp"
00014 #include "moab/Range.hpp"
00015 #include "moab/AdaptiveKDTree.hpp"
00016 #include "moab/ElemEvaluator.hpp"
00017 #include "moab/LinearHex.hpp"
00018 #include "moab/CN.hpp"
00019 #include "moab/SpatialLocator.hpp"
00020 
00021 using namespace moab;
00022 
00023 #define ERR(s) if (MB_SUCCESS != rval) \
00024     {std::string str;mb.get_last_error(str); std::cerr << s << str << std::endl; return 1;}
00025 
00026 int main(int argc, char **argv) {
00027 
00028   int num_queries = 1000000;
00029   
00030   if (argc < 2 || argc > 3) {
00031     std::cout << "Usage: " << argv[0] << "<filename> [num_queries]" << std::endl;
00032     return 0;
00033   }
00034   else if (argc == 3) num_queries = atoi(argv[2]);
00035 
00036     // instantiate & load a file
00037   moab::Core mb;
00038 
00039     // load the file
00040   ErrorCode rval = mb.load_file(argv[1]); ERR("Error loading file");
00041   
00042     // get all 3d elements in the file
00043   Range elems;
00044   rval = mb.get_entities_by_dimension(0, 3, elems); ERR("Error getting 3d elements");
00045   
00046     // create a tree to use for the location service
00047   AdaptiveKDTree tree(&mb);
00048 
00049     // specify an evaluator based on linear hexes
00050   ElemEvaluator el_eval(&mb);
00051 
00052     // build the SpatialLocator
00053   SpatialLocator sl(&mb, elems, &tree);
00054   
00055     // get the box extents
00056   CartVect box_extents, pos;
00057   BoundBox box = sl.local_box();
00058   box_extents = box.bMax - box.bMin;
00059   
00060     // query at random places in the tree
00061   CartVect params;
00062   int is_inside = 0;
00063   int num_inside = 0;
00064   EntityHandle elem;
00065   for (int i = 0; i < num_queries; i++) {
00066     pos = box.bMin + 
00067         CartVect(box_extents[0]*.01*(rand()%100), box_extents[1]*.01*(rand()%100), box_extents[2]*.01*(rand()%100));
00068     ErrorCode tmp_rval = sl.locate_point(pos.array(), elem, params.array(), &is_inside, 0.0, 0.0);
00069     if (MB_SUCCESS != tmp_rval) rval = tmp_rval;
00070     if (is_inside) num_inside++;
00071   }
00072   
00073   std::cout << "Mesh contains " << elems.size() << " elements of type " 
00074             << CN::EntityTypeName(mb.type_from_handle(*elems.begin())) << std::endl;
00075   std::cout << "Bounding box min-max = (" << box.bMin[0] << "," << box.bMin[1] << "," << box.bMin[2] << ")-("
00076             << box.bMax[0] << "," << box.bMax[1] << "," << box.bMax[2] << ")" << std::endl;
00077   std::cout << "Queries inside box = " << num_inside << "/" << num_queries << " = " 
00078             << 100.0*((double)num_inside)/num_queries << "%" << std::endl;
00079 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines