KdBVH.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Ilya Baran <ibaran@mit.edu>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef KDBVH_H_INCLUDED
11 #define KDBVH_H_INCLUDED
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 // internal pair class for the BVH--used instead of std::pair because of alignment
21 template <typename Scalar, int Dim>
24  typedef Matrix<Scalar, Dim, 1> VectorType;
25 
26  vector_int_pair(const VectorType &v, int i) : first(v), second(i) {}
27 
29  int second;
30 };
31 
32 // these templates help the tree initializer get the bounding boxes either from a provided
33 // iterator range or using bounding_box in a unified way
34 template <typename ObjectList, typename VolumeList, typename BoxIter>
36  void operator()(const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes) {
37  outBoxes.insert(outBoxes.end(), boxBegin, boxEnd);
38  eigen_assert(outBoxes.size() == objects.size());
40  }
41 };
42 
43 template <typename ObjectList, typename VolumeList>
44 struct get_boxes_helper<ObjectList, VolumeList, int> {
45  void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes) {
46  outBoxes.reserve(objects.size());
47  for (int i = 0; i < (int)objects.size(); ++i) outBoxes.push_back(bounding_box(objects[i]));
48  }
49 };
50 
51 } // end namespace internal
52 
67 template <typename Scalar_, int Dim_, typename _Object>
68 class KdBVH {
69  public:
70  enum { Dim = Dim_ };
71  typedef _Object Object;
72  typedef std::vector<Object, aligned_allocator<Object> > ObjectList;
73  typedef Scalar_ Scalar;
75  typedef std::vector<Volume, aligned_allocator<Volume> > VolumeList;
76  typedef int Index;
77  typedef const int *VolumeIterator; // the iterators are just pointers into the tree's vectors
78  typedef const Object *ObjectIterator;
79 
80  KdBVH() {}
81 
84  template <typename Iter>
85  KdBVH(Iter begin, Iter end) {
86  init(begin, end, 0, 0);
87  } // int is recognized by init as not being an iterator type
88 
91  template <typename OIter, typename BIter>
92  KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd) {
93  init(begin, end, boxBegin, boxEnd);
94  }
95 
98  template <typename Iter>
99  void init(Iter begin, Iter end) {
100  init(begin, end, 0, 0);
101  }
102 
105  template <typename OIter, typename BIter>
106  void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd) {
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
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  }
134 
136  inline Index getRootIndex() const { return (int)boxes.size() - 1; }
137 
141  ObjectIterator &outOBegin, ObjectIterator &outOEnd)
142  const { // 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  }
168 
170  inline const Volume &getVolume(Index index) const { return boxes[index]; }
171 
172  private:
174  typedef std::vector<VIPair, aligned_allocator<VIPair> > VIPairList;
176  struct VectorComparator // compares vectors, or more specifically, VIPairs along a particular dimension
177  {
178  VectorComparator(int inDim) : dim(inDim) {}
179  inline bool operator()(const VIPair &v1, const VIPair &v2) const { return v1.first[dim] < v2.first[dim]; }
180  int dim;
181  };
182 
183  // Build the part of the tree between objects[from] and objects[to] (not including objects[to]).
184  // This routine partitions the objCenters in [from, to) along the dimension dim, recursively constructs
185  // the two halves, and adds their parent node. TODO: a cache-friendlier layout
186  void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim) {
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  }
214 
215  std::vector<int> children; // children of x are children[2x] and children[2x+1], indices bigger than boxes.size()
216  // index into objects.
219 };
220 
221 } // end namespace Eigen
222 
223 #endif // KDBVH_H_INCLUDED
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
Definition: Macros.h:922
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
Definition: Memory.h:880
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
SCALAR Scalar
Definition: bench_gemm.cpp:45
An axis aligned box.
Definition: AlignedBox.h:69
A simple bounding volume hierarchy based on AlignedBox.
Definition: KdBVH.h:68
std::vector< VIPair, aligned_allocator< VIPair > > VIPairList
Definition: KdBVH.h:174
const Object * ObjectIterator
Definition: KdBVH.h:78
Matrix< Scalar, Dim, 1 > VectorType
Definition: KdBVH.h:175
std::vector< Object, aligned_allocator< Object > > ObjectList
Definition: KdBVH.h:72
void init(Iter begin, Iter end)
Definition: KdBVH.h:99
AlignedBox< Scalar, Dim > Volume
Definition: KdBVH.h:74
_Object Object
Definition: KdBVH.h:71
KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:92
KdBVH(Iter begin, Iter end)
Definition: KdBVH.h:85
const Volume & getVolume(Index index) const
Definition: KdBVH.h:170
KdBVH()
Definition: KdBVH.h:80
internal::vector_int_pair< Scalar, Dim > VIPair
Definition: KdBVH.h:173
void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:106
ObjectList objects
Definition: KdBVH.h:218
Scalar_ Scalar
Definition: KdBVH.h:73
Index getRootIndex() const
Definition: KdBVH.h:136
const int * VolumeIterator
Definition: KdBVH.h:77
void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
Definition: KdBVH.h:186
int Index
Definition: KdBVH.h:76
@ Dim
Definition: KdBVH.h:70
EIGEN_STRONG_INLINE void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd, ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
Definition: KdBVH.h:140
VolumeList boxes
Definition: KdBVH.h:217
std::vector< int > children
Definition: KdBVH.h:215
std::vector< Volume, aligned_allocator< Volume > > VolumeList
Definition: KdBVH.h:75
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
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
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
return int(ret)+1
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
Box2d bounding_box(const Vector2d &v)
Definition: BVH_Example.cpp:9
static const unsigned Dim
Problem dimension.
Definition: two_d_tilted_square.cc:62
Definition: Eigen_Colamd.h:49
Definition: KdBVH.h:177
int dim
Definition: KdBVH.h:180
VectorComparator(int inDim)
Definition: KdBVH.h:178
bool operator()(const VIPair &v1, const VIPair &v2) const
Definition: KdBVH.h:179
void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes)
Definition: KdBVH.h:45
Definition: KdBVH.h:35
void operator()(const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes)
Definition: KdBVH.h:36
Definition: KdBVH.h:22
VectorType first
Definition: KdBVH.h:28
int second
Definition: KdBVH.h:29