$darkmode
Eigen-unsupported  5.0.1-dev
KdBVH.h
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>
22 struct vector_int_pair {
23  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Dim)
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  outBoxes.insert(outBoxes.end(), boxBegin, boxEnd);
38  eigen_assert(outBoxes.size() == objects.size());
39  EIGEN_ONLY_USED_FOR_DEBUG(objects);
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
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  }
134 
136  inline Index getRootIndex() const { return (int)boxes.size() - 1; }
137 
140  EIGEN_STRONG_INLINE void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd,
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:
173  typedef internal::vector_int_pair<Scalar, Dim> VIPair;
174  typedef std::vector<VIPair, aligned_allocator<VIPair> > VIPairList;
175  typedef Matrix<Scalar, Dim, 1> VectorType;
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.
217  VolumeList boxes;
218  ObjectList objects;
219 };
220 
221 } // end namespace Eigen
222 
223 #endif // KDBVH_H_INCLUDED
static constexpr lastp1_t end
Namespace containing all symbols from the Eigen library.
void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:106
Index getRootIndex() const
Definition: KdBVH.h:136
KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:92
void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd, ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
Definition: KdBVH.h:140
KdBVH(Iter begin, Iter end)
Definition: KdBVH.h:85
void init(Iter begin, Iter end)
Definition: KdBVH.h:99
const Volume & getVolume(Index index) const
Definition: KdBVH.h:170
A simple bounding volume hierarchy based on AlignedBox.
Definition: KdBVH.h:68