Eigen::KdBVH< Scalar_, Dim_, _Object > Class Template Reference

A simple bounding volume hierarchy based on AlignedBox. More...

#include <KdBVH.h>

Classes

struct  VectorComparator
 

Public Types

enum  { Dim = Dim_ }
 
typedef _Object Object
 
typedef std::vector< Object, aligned_allocator< Object > > ObjectList
 
typedef Scalar_ Scalar
 
typedef AlignedBox< Scalar, DimVolume
 
typedef std::vector< Volume, aligned_allocator< Volume > > VolumeList
 
typedef int Index
 
typedef const intVolumeIterator
 
typedef const ObjectObjectIterator
 

Public Member Functions

 KdBVH ()
 
template<typename Iter >
 KdBVH (Iter begin, Iter end)
 
template<typename OIter , typename BIter >
 KdBVH (OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
 
template<typename Iter >
void init (Iter begin, Iter end)
 
template<typename OIter , typename BIter >
void init (OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
 
Index getRootIndex () const
 
EIGEN_STRONG_INLINE void getChildren (Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd, ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
 
const VolumegetVolume (Index index) const
 

Private Types

typedef internal::vector_int_pair< Scalar, DimVIPair
 
typedef std::vector< VIPair, aligned_allocator< VIPair > > VIPairList
 
typedef Matrix< Scalar, Dim, 1 > VectorType
 

Private Member Functions

void build (VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
 

Private Attributes

std::vector< intchildren
 
VolumeList boxes
 
ObjectList objects
 

Detailed Description

template<typename Scalar_, int Dim_, typename _Object>
class Eigen::KdBVH< Scalar_, Dim_, _Object >

A simple bounding volume hierarchy based on AlignedBox.

Parameters
Scalar_The underlying scalar type of the bounding boxes
Dim_The dimension of the space in which the hierarchy lives
<em>ObjectThe object type that lives in the hierarchy. It must have value semantics. Either bounding_box(_Object) must be defined and return an AlignedBox<Scalar, Dim_> or bounding boxes must be provided to the tree initializer.

This class provides a simple (as opposed to optimized) implementation of a bounding volume hierarchy analogous to a Kd-tree. Given a sequence of objects, it computes their bounding boxes, constructs a Kd-tree of their centers and builds a BVH with the structure of that Kd-tree. When the elements of the tree are too expensive to be copied around, it is useful for _Object to be a pointer.

Member Typedef Documentation

◆ Index

template<typename Scalar_ , int Dim_, typename _Object >
typedef int Eigen::KdBVH< Scalar_, Dim_, _Object >::Index

◆ Object

template<typename Scalar_ , int Dim_, typename _Object >
typedef _Object Eigen::KdBVH< Scalar_, Dim_, _Object >::Object

◆ ObjectIterator

template<typename Scalar_ , int Dim_, typename _Object >
typedef const Object* Eigen::KdBVH< Scalar_, Dim_, _Object >::ObjectIterator

◆ ObjectList

template<typename Scalar_ , int Dim_, typename _Object >
typedef std::vector<Object, aligned_allocator<Object> > Eigen::KdBVH< Scalar_, Dim_, _Object >::ObjectList

◆ Scalar

template<typename Scalar_ , int Dim_, typename _Object >
typedef Scalar_ Eigen::KdBVH< Scalar_, Dim_, _Object >::Scalar

◆ VectorType

template<typename Scalar_ , int Dim_, typename _Object >
typedef Matrix<Scalar, Dim, 1> Eigen::KdBVH< Scalar_, Dim_, _Object >::VectorType
private

◆ VIPair

template<typename Scalar_ , int Dim_, typename _Object >
typedef internal::vector_int_pair<Scalar, Dim> Eigen::KdBVH< Scalar_, Dim_, _Object >::VIPair
private

◆ VIPairList

template<typename Scalar_ , int Dim_, typename _Object >
typedef std::vector<VIPair, aligned_allocator<VIPair> > Eigen::KdBVH< Scalar_, Dim_, _Object >::VIPairList
private

◆ Volume

template<typename Scalar_ , int Dim_, typename _Object >
typedef AlignedBox<Scalar, Dim> Eigen::KdBVH< Scalar_, Dim_, _Object >::Volume

◆ VolumeIterator

template<typename Scalar_ , int Dim_, typename _Object >
typedef const int* Eigen::KdBVH< Scalar_, Dim_, _Object >::VolumeIterator

◆ VolumeList

template<typename Scalar_ , int Dim_, typename _Object >
typedef std::vector<Volume, aligned_allocator<Volume> > Eigen::KdBVH< Scalar_, Dim_, _Object >::VolumeList

Member Enumeration Documentation

◆ anonymous enum

template<typename Scalar_ , int Dim_, typename _Object >
anonymous enum
Enumerator
Dim 
70 { Dim = Dim_ };
@ Dim
Definition: KdBVH.h:70

Constructor & Destructor Documentation

◆ KdBVH() [1/3]

template<typename Scalar_ , int Dim_, typename _Object >
Eigen::KdBVH< Scalar_, Dim_, _Object >::KdBVH ( )
inline
80 {}

◆ KdBVH() [2/3]

template<typename Scalar_ , int Dim_, typename _Object >
template<typename Iter >
Eigen::KdBVH< Scalar_, Dim_, _Object >::KdBVH ( Iter  begin,
Iter  end 
)
inline

Given an iterator range over Object references, constructs the BVH. Requires that bounding_box(Object) return a Volume.

85  {
86  init(begin, end, 0, 0);
87  } // int is recognized by init as not being an iterator type
void init(Iter begin, Iter end)
Definition: KdBVH.h:99
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79

References Eigen::placeholders::end, and Eigen::KdBVH< Scalar_, Dim_, _Object >::init().

◆ KdBVH() [3/3]

template<typename Scalar_ , int Dim_, typename _Object >
template<typename OIter , typename BIter >
Eigen::KdBVH< Scalar_, Dim_, _Object >::KdBVH ( OIter  begin,
OIter  end,
BIter  boxBegin,
BIter  boxEnd 
)
inline

Given an iterator range over Object references and an iterator range over their bounding boxes, constructs the BVH

92  {
93  init(begin, end, boxBegin, boxEnd);
94  }

References Eigen::placeholders::end, and Eigen::KdBVH< Scalar_, Dim_, _Object >::init().

Member Function Documentation

◆ build()

template<typename Scalar_ , int Dim_, typename _Object >
void Eigen::KdBVH< Scalar_, Dim_, _Object >::build ( VIPairList objCenters,
int  from,
int  to,
const VolumeList objBoxes,
int  dim 
)
inlineprivate
186  {
187  eigen_assert(to - from > 1);
188  if (to - from == 2) {
189  boxes.push_back(objBoxes[objCenters[from].second].merged(objBoxes[objCenters[from + 1].second]));
190  children.push_back(from + (int)objects.size() - 1); // there are objects.size() - 1 tree nodes
191  children.push_back(from + (int)objects.size());
192  } else if (to - from == 3) {
193  int mid = from + 2;
194  std::nth_element(objCenters.begin() + from, objCenters.begin() + mid, objCenters.begin() + to,
195  VectorComparator(dim)); // partition
196  build(objCenters, from, mid, objBoxes, (dim + 1) % Dim);
197  int idx1 = (int)boxes.size() - 1;
198  boxes.push_back(boxes[idx1].merged(objBoxes[objCenters[mid].second]));
199  children.push_back(idx1);
200  children.push_back(mid + (int)objects.size() - 1);
201  } else {
202  int mid = from + (to - from) / 2;
203  nth_element(objCenters.begin() + from, objCenters.begin() + mid, objCenters.begin() + to,
204  VectorComparator(dim)); // partition
205  build(objCenters, from, mid, objBoxes, (dim + 1) % Dim);
206  int idx1 = (int)boxes.size() - 1;
207  build(objCenters, mid, to, objBoxes, (dim + 1) % Dim);
208  int idx2 = (int)boxes.size() - 1;
209  boxes.push_back(boxes[idx1].merged(boxes[idx2]));
210  children.push_back(idx1);
211  children.push_back(idx2);
212  }
213  }
#define eigen_assert(x)
Definition: Macros.h:910
ObjectList objects
Definition: KdBVH.h:218
void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
Definition: KdBVH.h:186
VolumeList boxes
Definition: KdBVH.h:217
std::vector< int > children
Definition: KdBVH.h:215
return int(ret)+1

References Eigen::KdBVH< Scalar_, Dim_, _Object >::boxes, Eigen::KdBVH< Scalar_, Dim_, _Object >::children, Eigen::KdBVH< Scalar_, Dim_, _Object >::Dim, eigen_assert, int(), and Eigen::KdBVH< Scalar_, Dim_, _Object >::objects.

Referenced by Eigen::KdBVH< Scalar_, Dim_, _Object >::init().

◆ getChildren()

template<typename Scalar_ , int Dim_, typename _Object >
EIGEN_STRONG_INLINE void Eigen::KdBVH< Scalar_, Dim_, _Object >::getChildren ( Index  index,
VolumeIterator outVBegin,
VolumeIterator outVEnd,
ObjectIterator outOBegin,
ObjectIterator outOEnd 
) const
inline

Given an index of a node, on exit, outVBegin and outVEnd range over the indices of the volume children of the node and outOBegin and outOEnd range over the object children of the node

142  { // inlining this function should open lots of optimization opportunities to the compiler
143  if (index < 0) {
144  outVBegin = outVEnd;
145  if (!objects.empty()) outOBegin = &(objects[0]);
146  outOEnd = outOBegin + objects.size(); // output all objects--necessary when the tree has only one object
147  return;
148  }
149 
150  int numBoxes = static_cast<int>(boxes.size());
151 
152  int idx = index * 2;
153  if (children[idx + 1] < numBoxes) { // second index is always bigger
154  outVBegin = &(children[idx]);
155  outVEnd = outVBegin + 2;
156  outOBegin = outOEnd;
157  } else if (children[idx] >= numBoxes) { // if both children are objects
158  outVBegin = outVEnd;
159  outOBegin = &(objects[children[idx] - numBoxes]);
160  outOEnd = outOBegin + 2;
161  } else { // if the first child is a volume and the second is an object
162  outVBegin = &(children[idx]);
163  outVEnd = outVBegin + 1;
164  outOBegin = &(objects[children[idx + 1] - numBoxes]);
165  outOEnd = outOBegin + 1;
166  }
167  }

References Eigen::KdBVH< Scalar_, Dim_, _Object >::boxes, Eigen::KdBVH< Scalar_, Dim_, _Object >::children, and Eigen::KdBVH< Scalar_, Dim_, _Object >::objects.

◆ getRootIndex()

template<typename Scalar_ , int Dim_, typename _Object >
Index Eigen::KdBVH< Scalar_, Dim_, _Object >::getRootIndex ( ) const
inline
Returns
the index of the root of the hierarchy
136 { return (int)boxes.size() - 1; }

References Eigen::KdBVH< Scalar_, Dim_, _Object >::boxes.

◆ getVolume()

template<typename Scalar_ , int Dim_, typename _Object >
const Volume& Eigen::KdBVH< Scalar_, Dim_, _Object >::getVolume ( Index  index) const
inline
Returns
the bounding box of the node at index
170 { return boxes[index]; }

References Eigen::KdBVH< Scalar_, Dim_, _Object >::boxes.

◆ init() [1/2]

template<typename Scalar_ , int Dim_, typename _Object >
template<typename Iter >
void Eigen::KdBVH< Scalar_, Dim_, _Object >::init ( Iter  begin,
Iter  end 
)
inline

Given an iterator range over Object references, constructs the BVH, overwriting whatever is in there currently. Requires that bounding_box(Object) return a Volume.

99  {
100  init(begin, end, 0, 0);
101  }

References Eigen::placeholders::end.

Referenced by Eigen::KdBVH< Scalar_, Dim_, _Object >::KdBVH().

◆ init() [2/2]

template<typename Scalar_ , int Dim_, typename _Object >
template<typename OIter , typename BIter >
void Eigen::KdBVH< Scalar_, Dim_, _Object >::init ( OIter  begin,
OIter  end,
BIter  boxBegin,
BIter  boxEnd 
)
inline

Given an iterator range over Object references and an iterator range over their bounding boxes, constructs the BVH, overwriting whatever is in there currently.

106  {
107  objects.clear();
108  boxes.clear();
109  children.clear();
110 
111  objects.insert(objects.end(), begin, end);
112  int n = static_cast<int>(objects.size());
113 
114  if (n < 2) return; // if we have at most one object, we don't need any internal nodes
115 
116  VolumeList objBoxes;
117  VIPairList objCenters;
118 
119  // compute the bounding boxes depending on BIter type
120  internal::get_boxes_helper<ObjectList, VolumeList, BIter>()(objects, boxBegin, boxEnd, objBoxes);
121 
122  objCenters.reserve(n);
123  boxes.reserve(n - 1);
124  children.reserve(2 * n - 2);
125 
126  for (int i = 0; i < n; ++i) objCenters.push_back(VIPair(objBoxes[i].center(), i));
127 
128  build(objCenters, 0, n, objBoxes, 0); // the recursive part of the algorithm
129 
130  ObjectList tmp(n);
131  tmp.swap(objects);
132  for (int i = 0; i < n; ++i) objects[i] = tmp[objCenters[i].second];
133  }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
std::vector< VIPair, aligned_allocator< VIPair > > VIPairList
Definition: KdBVH.h:174
std::vector< Object, aligned_allocator< Object > > ObjectList
Definition: KdBVH.h:72
internal::vector_int_pair< Scalar, Dim > VIPair
Definition: KdBVH.h:173
std::vector< Volume, aligned_allocator< Volume > > VolumeList
Definition: KdBVH.h:75
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void swap(DenseBase< OtherDerived > &other)
Override DenseBase::swap() since for dynamic-sized matrices of same type it is enough to swap the dat...
Definition: PlainObjectBase.h:898
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365

References Eigen::KdBVH< Scalar_, Dim_, _Object >::boxes, Eigen::KdBVH< Scalar_, Dim_, _Object >::build(), Eigen::KdBVH< Scalar_, Dim_, _Object >::children, Eigen::placeholders::end, i, n, Eigen::KdBVH< Scalar_, Dim_, _Object >::objects, Eigen::PlainObjectBase< Derived >::swap(), and tmp.

Member Data Documentation

◆ boxes

◆ children

template<typename Scalar_ , int Dim_, typename _Object >
std::vector<int> Eigen::KdBVH< Scalar_, Dim_, _Object >::children
private

◆ objects

template<typename Scalar_ , int Dim_, typename _Object >
ObjectList Eigen::KdBVH< Scalar_, Dim_, _Object >::objects
private

The documentation for this class was generated from the following file: