diff options
author | Gael Guennebaud <g.gael@free.fr> | 2009-03-18 20:06:06 +0000 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2009-03-18 20:06:06 +0000 |
commit | 4bb5221d229703a906c6fe805b73fac2496c8bea (patch) | |
tree | f318f150d2051ce3d2c5912a6d243659e7bdc0c5 /unsupported/doc/examples | |
parent | 3d385ae9680fdd5093401ace8a5588444434d194 (diff) |
Add BVH module in unsupported (patch from Ilya Baran)
(I thought I committed it a week ago but it seems the command failed)
Diffstat (limited to 'unsupported/doc/examples')
-rw-r--r-- | unsupported/doc/examples/BVH_Example.cpp | 45 | ||||
-rw-r--r-- | unsupported/doc/examples/CMakeLists.txt | 17 |
2 files changed, 62 insertions, 0 deletions
diff --git a/unsupported/doc/examples/BVH_Example.cpp b/unsupported/doc/examples/BVH_Example.cpp new file mode 100644 index 000000000..890eb535b --- /dev/null +++ b/unsupported/doc/examples/BVH_Example.cpp @@ -0,0 +1,45 @@ +#include <unsupported/Eigen/BVH> + +using namespace Eigen; +typedef AlignedBox<double, 2> Box2d; + +Box2d ei_bounding_box(const Vector2d &v) { return Box2d(v, v); } //compute the bounding box of a single point + +struct PointPointMinimizer //how to compute squared distances between points and rectangles +{ + PointPointMinimizer() : calls(0) {} + typedef double Scalar; + + double minimumOnVolumeVolume(const Box2d &r1, const Box2d &r2) { ++calls; return r1.squaredExteriorDistance(r2); } + double minimumOnVolumeObject(const Box2d &r, const Vector2d &v) { ++calls; return r.squaredExteriorDistance(v); } + double minimumOnObjectVolume(const Vector2d &v, const Box2d &r) { ++calls; return r.squaredExteriorDistance(v); } + double minimumOnObjectObject(const Vector2d &v1, const Vector2d &v2) { ++calls; return (v1 - v2).squaredNorm(); } + + int calls; +}; + +int main() +{ + std::vector<Vector2d> redPoints, bluePoints; + for(int i = 0; i < 100; ++i) { //initialize random set of red points and blue points + redPoints.push_back(Vector2d::Random()); + bluePoints.push_back(Vector2d::Random()); + } + + PointPointMinimizer minimizer; + double minDistSq = std::numeric_limits<double>::max(); + + //brute force to find closest red-blue pair + for(int i = 0; i < (int)redPoints.size(); ++i) + for(int j = 0; j < (int)bluePoints.size(); ++j) + minDistSq = std::min(minDistSq, minimizer.minimumOnObjectObject(redPoints[i], bluePoints[j])); + std::cout << "Brute force distance = " << sqrt(minDistSq) << ", calls = " << minimizer.calls << std::endl; + + //using BVH to find closest red-blue pair + minimizer.calls = 0; + KdBVH<double, 2, Vector2d> redTree(redPoints.begin(), redPoints.end()), blueTree(bluePoints.begin(), bluePoints.end()); //construct the trees + minDistSq = BVMinimize(redTree, blueTree, minimizer); //actual BVH minimization call + std::cout << "BVH distance = " << sqrt(minDistSq) << ", calls = " << minimizer.calls << std::endl; + + return 0; +} diff --git a/unsupported/doc/examples/CMakeLists.txt b/unsupported/doc/examples/CMakeLists.txt new file mode 100644 index 000000000..af4de4b0d --- /dev/null +++ b/unsupported/doc/examples/CMakeLists.txt @@ -0,0 +1,17 @@ +FILE(GLOB examples_SRCS "*.cpp") + +ADD_CUSTOM_TARGET(unsupported_examples) + +FOREACH(example_src ${examples_SRCS}) +GET_FILENAME_COMPONENT(example ${example_src} NAME_WE) +ADD_EXECUTABLE(${example} ${example_src}) +GET_TARGET_PROPERTY(example_executable + ${example} LOCATION) +ADD_CUSTOM_COMMAND( + TARGET ${example} + POST_BUILD + COMMAND ${example_executable} + ARGS >${CMAKE_CURRENT_BINARY_DIR}/${example}.out +) +ADD_DEPENDENCIES(unsupported_examples ${example}) +ENDFOREACH(example_src) |