BVH_Example.cpp
Go to the documentation of this file.
00001 #include <Eigen/StdVector>
00002 #include <unsupported/Eigen/BVH>
00003 #include <iostream>
00004 
00005 using namespace Eigen;
00006 typedef AlignedBox<double, 2> Box2d;
00007 
00008 namespace Eigen {
00009     namespace internal {
00010         Box2d bounding_box(const Vector2d &v) { return Box2d(v, v); } //compute the bounding box of a single point
00011     }
00012 }
00013 
00014 struct PointPointMinimizer //how to compute squared distances between points and rectangles
00015 {
00016   PointPointMinimizer() : calls(0) {}
00017   typedef double Scalar;
00018 
00019   double minimumOnVolumeVolume(const Box2d &r1, const Box2d &r2) { ++calls; return r1.squaredExteriorDistance(r2); }
00020   double minimumOnVolumeObject(const Box2d &r, const Vector2d &v) { ++calls; return r.squaredExteriorDistance(v); }
00021   double minimumOnObjectVolume(const Vector2d &v, const Box2d &r) { ++calls; return r.squaredExteriorDistance(v); }
00022   double minimumOnObjectObject(const Vector2d &v1, const Vector2d &v2) { ++calls; return (v1 - v2).squaredNorm(); }
00023 
00024   int calls;
00025 };
00026 
00027 int main()
00028 {
00029   typedef std::vector<Vector2d, aligned_allocator<Vector2d> > StdVectorOfVector2d;
00030   StdVectorOfVector2d redPoints, bluePoints;
00031   for(int i = 0; i < 100; ++i) { //initialize random set of red points and blue points
00032     redPoints.push_back(Vector2d::Random());
00033     bluePoints.push_back(Vector2d::Random());
00034   }
00035 
00036   PointPointMinimizer minimizer;
00037   double minDistSq = std::numeric_limits<double>::max();
00038 
00039   //brute force to find closest red-blue pair
00040   for(int i = 0; i < (int)redPoints.size(); ++i)
00041     for(int j = 0; j < (int)bluePoints.size(); ++j)
00042       minDistSq = std::min(minDistSq, minimizer.minimumOnObjectObject(redPoints[i], bluePoints[j]));
00043   std::cout << "Brute force distance = " << sqrt(minDistSq) << ", calls = " << minimizer.calls << std::endl;
00044 
00045   //using BVH to find closest red-blue pair
00046   minimizer.calls = 0;
00047   KdBVH<double, 2, Vector2d> redTree(redPoints.begin(), redPoints.end()), blueTree(bluePoints.begin(), bluePoints.end()); //construct the trees
00048   minDistSq = BVMinimize(redTree, blueTree, minimizer); //actual BVH minimization call
00049   std::cout << "BVH distance         = " << sqrt(minDistSq) << ", calls = " << minimizer.calls << std::endl;
00050 
00051   return 0;
00052 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:31