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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 //internal pair class for the BVH--used instead of std::pair because of alignment
20 template<typename Scalar, int Dim>
21 struct vector_int_pair
22 {
24  typedef Matrix<Scalar, Dim, 1> VectorType;
25 
26  vector_int_pair(const VectorType &v, int i) : first(v), second(i) {}
27 
28  VectorType first;
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>
35 struct get_boxes_helper {
36  void operator()(const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes)
37  {
38  outBoxes.insert(outBoxes.end(), boxBegin, boxEnd);
39  eigen_assert(outBoxes.size() == objects.size());
41  }
42 };
43 
44 template<typename ObjectList, typename VolumeList>
45 struct get_boxes_helper<ObjectList, VolumeList, int> {
46  void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes)
47  {
48  outBoxes.reserve(objects.size());
49  for(int i = 0; i < (int)objects.size(); ++i)
50  outBoxes.push_back(bounding_box(objects[i]));
51  }
52 };
53 
54 } // end namespace internal
55 
56 
70 template<typename Scalar_, int Dim_, typename _Object> class KdBVH
71 {
72 public:
73  enum { Dim = Dim_ };
74  typedef _Object Object;
75  typedef std::vector<Object, aligned_allocator<Object> > ObjectList;
76  typedef Scalar_ Scalar;
78  typedef std::vector<Volume, aligned_allocator<Volume> > VolumeList;
79  typedef int Index;
80  typedef const int *VolumeIterator; //the iterators are just pointers into the tree's vectors
81  typedef const Object *ObjectIterator;
82 
83  KdBVH() {}
84 
86  template<typename Iter> KdBVH(Iter begin, Iter end) { init(begin, end, 0, 0); } //int is recognized by init as not being an iterator type
87 
89  template<typename OIter, typename BIter> KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd) { init(begin, end, boxBegin, boxEnd); }
90 
93  template<typename Iter> void init(Iter begin, Iter end) { init(begin, end, 0, 0); }
94 
97  template<typename OIter, typename BIter> void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
98  {
99  objects.clear();
100  boxes.clear();
101  children.clear();
102 
103  objects.insert(objects.end(), begin, end);
104  int n = static_cast<int>(objects.size());
105 
106  if(n < 2)
107  return; //if we have at most one object, we don't need any internal nodes
108 
109  VolumeList objBoxes;
110  VIPairList objCenters;
111 
112  //compute the bounding boxes depending on BIter type
113  internal::get_boxes_helper<ObjectList, VolumeList, BIter>()(objects, boxBegin, boxEnd, objBoxes);
114 
115  objCenters.reserve(n);
116  boxes.reserve(n - 1);
117  children.reserve(2 * n - 2);
118 
119  for(int i = 0; i < n; ++i)
120  objCenters.push_back(VIPair(objBoxes[i].center(), i));
121 
122  build(objCenters, 0, n, objBoxes, 0); //the recursive part of the algorithm
123 
124  ObjectList tmp(n);
125  tmp.swap(objects);
126  for(int i = 0; i < n; ++i)
127  objects[i] = tmp[objCenters[i].second];
128  }
129 
131  inline Index getRootIndex() const { return (int)boxes.size() - 1; }
132 
135  EIGEN_STRONG_INLINE void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd,
136  ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
137  { //inlining this function should open lots of optimization opportunities to the compiler
138  if(index < 0) {
139  outVBegin = outVEnd;
140  if(!objects.empty())
141  outOBegin = &(objects[0]);
142  outOEnd = outOBegin + objects.size(); //output all objects--necessary when the tree has only one object
143  return;
144  }
145 
146  int numBoxes = static_cast<int>(boxes.size());
147 
148  int idx = index * 2;
149  if(children[idx + 1] < numBoxes) { //second index is always bigger
150  outVBegin = &(children[idx]);
151  outVEnd = outVBegin + 2;
152  outOBegin = outOEnd;
153  }
154  else if(children[idx] >= numBoxes) { //if both children are objects
155  outVBegin = outVEnd;
156  outOBegin = &(objects[children[idx] - numBoxes]);
157  outOEnd = outOBegin + 2;
158  } else { //if the first child is a volume and the second is an object
159  outVBegin = &(children[idx]);
160  outVEnd = outVBegin + 1;
161  outOBegin = &(objects[children[idx + 1] - numBoxes]);
162  outOEnd = outOBegin + 1;
163  }
164  }
165 
167  inline const Volume &getVolume(Index index) const
168  {
169  return boxes[index];
170  }
171 
172 private:
173  typedef internal::vector_int_pair<Scalar, Dim> VIPair;
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  {
188  eigen_assert(to - from > 1);
189  if(to - from == 2) {
190  boxes.push_back(objBoxes[objCenters[from].second].merged(objBoxes[objCenters[from + 1].second]));
191  children.push_back(from + (int)objects.size() - 1); //there are objects.size() - 1 tree nodes
192  children.push_back(from + (int)objects.size());
193  }
194  else if(to - from == 3) {
195  int mid = from + 2;
196  std::nth_element(objCenters.begin() + from, objCenters.begin() + mid,
197  objCenters.begin() + to, VectorComparator(dim)); //partition
198  build(objCenters, from, mid, objBoxes, (dim + 1) % Dim);
199  int idx1 = (int)boxes.size() - 1;
200  boxes.push_back(boxes[idx1].merged(objBoxes[objCenters[mid].second]));
201  children.push_back(idx1);
202  children.push_back(mid + (int)objects.size() - 1);
203  }
204  else {
205  int mid = from + (to - from) / 2;
206  nth_element(objCenters.begin() + from, objCenters.begin() + mid,
207  objCenters.begin() + to, VectorComparator(dim)); //partition
208  build(objCenters, from, mid, objBoxes, (dim + 1) % Dim);
209  int idx1 = (int)boxes.size() - 1;
210  build(objCenters, mid, to, objBoxes, (dim + 1) % Dim);
211  int idx2 = (int)boxes.size() - 1;
212  boxes.push_back(boxes[idx1].merged(boxes[idx2]));
213  children.push_back(idx1);
214  children.push_back(idx2);
215  }
216  }
217 
218  std::vector<int> children; //children of x are children[2x] and children[2x+1], indices bigger than boxes.size() index into objects.
221 };
222 
223 } // end namespace Eigen
224 
225 #endif //KDBVH_H_INCLUDED
Array< int, Dynamic, 1 > v
int n
int i
IndexedView_or_VectorBlock operator()(const Indices &indices)
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
#define eigen_assert(x)
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
A simple bounding volume hierarchy based on AlignedBox.
Definition: KdBVH.h:71
std::vector< VIPair, aligned_allocator< VIPair > > VIPairList
Definition: KdBVH.h:174
const Object * ObjectIterator
Definition: KdBVH.h:81
Matrix< Scalar, Dim, 1 > VectorType
Definition: KdBVH.h:175
std::vector< Object, aligned_allocator< Object > > ObjectList
Definition: KdBVH.h:75
void init(Iter begin, Iter end)
Definition: KdBVH.h:93
AlignedBox< Scalar, Dim > Volume
Definition: KdBVH.h:77
_Object Object
Definition: KdBVH.h:74
KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:89
KdBVH(Iter begin, Iter end)
Definition: KdBVH.h:86
const Volume & getVolume(Index index) const
Definition: KdBVH.h:167
internal::vector_int_pair< Scalar, Dim > VIPair
Definition: KdBVH.h:173
void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd, ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
Definition: KdBVH.h:135
void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:97
ObjectList objects
Definition: KdBVH.h:220
Scalar_ Scalar
Definition: KdBVH.h:76
Index getRootIndex() const
Definition: KdBVH.h:131
const int * VolumeIterator
Definition: KdBVH.h:80
void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
Definition: KdBVH.h:186
int Index
Definition: KdBVH.h:79
VolumeList boxes
Definition: KdBVH.h:219
std::vector< int > children
Definition: KdBVH.h:218
std::vector< Volume, aligned_allocator< Volume > > VolumeList
Definition: KdBVH.h:78
static const lastp1_t end
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
Box2d bounding_box(const Vector2d &v)
Definition: BVH_Example.cpp:9
bool operator()(const VIPair &v1, const VIPair &v2) const
Definition: KdBVH.h:179