xref: /aosp_15_r20/external/eigen/unsupported/Eigen/BVH (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li// This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li// for linear algebra.
3*bf2c3715SXin Li//
4*bf2c3715SXin Li// Copyright (C) 2009 Ilya Baran <[email protected]>
5*bf2c3715SXin Li//
6*bf2c3715SXin Li// This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li// Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li
10*bf2c3715SXin Li#ifndef EIGEN_BVH_MODULE_H
11*bf2c3715SXin Li#define EIGEN_BVH_MODULE_H
12*bf2c3715SXin Li
13*bf2c3715SXin Li#include "../../Eigen/Core"
14*bf2c3715SXin Li#include "../../Eigen/Geometry"
15*bf2c3715SXin Li#include "../../Eigen/StdVector"
16*bf2c3715SXin Li#include <algorithm>
17*bf2c3715SXin Li#include <queue>
18*bf2c3715SXin Li
19*bf2c3715SXin Linamespace Eigen {
20*bf2c3715SXin Li
21*bf2c3715SXin Li/**
22*bf2c3715SXin Li  * \defgroup BVH_Module BVH module
23*bf2c3715SXin Li  * \brief This module provides generic bounding volume hierarchy algorithms
24*bf2c3715SXin Li  * and reference tree implementations.
25*bf2c3715SXin Li  *
26*bf2c3715SXin Li  *
27*bf2c3715SXin Li  * \code
28*bf2c3715SXin Li  * #include <unsupported/Eigen/BVH>
29*bf2c3715SXin Li  * \endcode
30*bf2c3715SXin Li  *
31*bf2c3715SXin Li  * A bounding volume hierarchy (BVH) can accelerate many geometric queries.  This module provides a generic implementation
32*bf2c3715SXin Li  * of the two basic algorithms over a BVH: intersection of a query object against all objects in the hierarchy and minimization
33*bf2c3715SXin Li  * of a function over the objects in the hierarchy.  It also provides intersection and minimization over a cartesian product of
34*bf2c3715SXin Li  * two BVH's.  A BVH accelerates intersection by using the fact that if a query object does not intersect a volume, then it cannot
35*bf2c3715SXin Li  * intersect any object contained in that volume.  Similarly, a BVH accelerates minimization because the minimum of a function
36*bf2c3715SXin Li  * over a volume is no greater than the minimum of a function over any object contained in it.
37*bf2c3715SXin Li  *
38*bf2c3715SXin Li  * Some sample queries that can be written in terms of intersection are:
39*bf2c3715SXin Li  *   - Determine all points where a ray intersects a triangle mesh
40*bf2c3715SXin Li  *   - Given a set of points, determine which are contained in a query sphere
41*bf2c3715SXin Li  *   - Given a set of spheres, determine which contain the query point
42*bf2c3715SXin Li  *   - Given a set of disks, determine if any is completely contained in a query rectangle (represent each 2D disk as a point \f$(x,y,r)\f$
43*bf2c3715SXin Li  *     in 3D and represent the rectangle as a pyramid based on the original rectangle and shrinking in the \f$r\f$ direction)
44*bf2c3715SXin Li  *   - Given a set of points, count how many pairs are \f$d\pm\epsilon\f$ apart (done by looking at the cartesian product of the set
45*bf2c3715SXin Li  *     of points with itself)
46*bf2c3715SXin Li  *
47*bf2c3715SXin Li  * Some sample queries that can be written in terms of function minimization over a set of objects are:
48*bf2c3715SXin Li  *   - Find the intersection between a ray and a triangle mesh closest to the ray origin (function is infinite off the ray)
49*bf2c3715SXin Li  *   - Given a polyline and a query point, determine the closest point on the polyline to the query
50*bf2c3715SXin Li  *   - Find the diameter of a point cloud (done by looking at the cartesian product and using negative distance as the function)
51*bf2c3715SXin Li  *   - Determine how far two meshes are from colliding (this is also a cartesian product query)
52*bf2c3715SXin Li  *
53*bf2c3715SXin Li  * This implementation decouples the basic algorithms both from the type of hierarchy (and the types of the bounding volumes) and
54*bf2c3715SXin Li  * from the particulars of the query.  To enable abstraction from the BVH, the BVH is required to implement a generic mechanism
55*bf2c3715SXin Li  * for traversal.  To abstract from the query, the query is responsible for keeping track of results.
56*bf2c3715SXin Li  *
57*bf2c3715SXin Li  * To be used in the algorithms, a hierarchy must implement the following traversal mechanism (see KdBVH for a sample implementation): \code
58*bf2c3715SXin Li      typedef Volume  //the type of bounding volume
59*bf2c3715SXin Li      typedef Object  //the type of object in the hierarchy
60*bf2c3715SXin Li      typedef Index   //a reference to a node in the hierarchy--typically an int or a pointer
61*bf2c3715SXin Li      typedef VolumeIterator //an iterator type over node children--returns Index
62*bf2c3715SXin Li      typedef ObjectIterator //an iterator over object (leaf) children--returns const Object &
63*bf2c3715SXin Li      Index getRootIndex() const //returns the index of the hierarchy root
64*bf2c3715SXin Li      const Volume &getVolume(Index index) const //returns the bounding volume of the node at given index
65*bf2c3715SXin Li      void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd,
66*bf2c3715SXin Li                      ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
67*bf2c3715SXin Li      //getChildren takes a node index and makes [outVBegin, outVEnd) range over its node children
68*bf2c3715SXin Li      //and [outOBegin, outOEnd) range over its object children
69*bf2c3715SXin Li    \endcode
70*bf2c3715SXin Li  *
71*bf2c3715SXin Li  * To use the hierarchy, call BVIntersect or BVMinimize, passing it a BVH (or two, for cartesian product) and a minimizer or intersector.
72*bf2c3715SXin Li  * For an intersection query on a single BVH, the intersector encapsulates the query and must provide two functions:
73*bf2c3715SXin Li  * \code
74*bf2c3715SXin Li      bool intersectVolume(const Volume &volume) //returns true if the query intersects the volume
75*bf2c3715SXin Li      bool intersectObject(const Object &object) //returns true if the intersection search should terminate immediately
76*bf2c3715SXin Li    \endcode
77*bf2c3715SXin Li  * The guarantee that BVIntersect provides is that intersectObject will be called on every object whose bounding volume
78*bf2c3715SXin Li  * intersects the query (but possibly on other objects too) unless the search is terminated prematurely.  It is the
79*bf2c3715SXin Li  * responsibility of the intersectObject function to keep track of the results in whatever manner is appropriate.
80*bf2c3715SXin Li  * The cartesian product intersection and the BVMinimize queries are similar--see their individual documentation.
81*bf2c3715SXin Li  *
82*bf2c3715SXin Li  * The following is a simple but complete example for how to use the BVH to accelerate the search for a closest red-blue point pair:
83*bf2c3715SXin Li  * \include BVH_Example.cpp
84*bf2c3715SXin Li  * Output: \verbinclude BVH_Example.out
85*bf2c3715SXin Li  */
86*bf2c3715SXin Li}
87*bf2c3715SXin Li
88*bf2c3715SXin Li//@{
89*bf2c3715SXin Li
90*bf2c3715SXin Li#include "src/BVH/BVAlgorithms.h"
91*bf2c3715SXin Li#include "src/BVH/KdBVH.h"
92*bf2c3715SXin Li
93*bf2c3715SXin Li//@}
94*bf2c3715SXin Li
95*bf2c3715SXin Li#endif // EIGEN_BVH_MODULE_H
96