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 namespace Eigen {
14 
15 namespace internal {
16 
17 //internal pair class for the BVH--used instead of std::pair because of alignment
18 template<typename Scalar, int Dim>
20 {
22  typedef Matrix<Scalar, Dim, 1> VectorType;
23 
24  vector_int_pair(const VectorType &v, int i) : first(v), second(i) {}
25 
27  int second;
28 };
29 
30 //these templates help the tree initializer get the bounding boxes either from a provided
31 //iterator range or using bounding_box in a unified way
32 template<typename ObjectList, typename VolumeList, typename BoxIter>
34  void operator()(const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes)
35  {
36  outBoxes.insert(outBoxes.end(), boxBegin, boxEnd);
37  eigen_assert(outBoxes.size() == objects.size());
38  }
39 };
40 
41 template<typename ObjectList, typename VolumeList>
42 struct get_boxes_helper<ObjectList, VolumeList, int> {
43  void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes)
44  {
45  outBoxes.reserve(objects.size());
46  for(int i = 0; i < (int)objects.size(); ++i)
47  outBoxes.push_back(bounding_box(objects[i]));
48  }
49 };
50 
51 } // end namespace internal
52 
53 
67 template<typename _Scalar, int _Dim, typename _Object> class KdBVH
68 {
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 
83  template<typename Iter> KdBVH(Iter begin, Iter end) { init(begin, end, 0, 0); } //int is recognized by init as not being an iterator type
84 
86  template<typename OIter, typename BIter> KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd) { init(begin, end, boxBegin, boxEnd); }
87 
90  template<typename Iter> void init(Iter begin, Iter end) { init(begin, end, 0, 0); }
91 
94  template<typename OIter, typename BIter> void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
95  {
96  objects.clear();
97  boxes.clear();
98  children.clear();
99 
100  objects.insert(objects.end(), begin, end);
101  int n = static_cast<int>(objects.size());
102 
103  if(n < 2)
104  return; //if we have at most one object, we don't need any internal nodes
105 
106  VolumeList objBoxes;
107  VIPairList objCenters;
108 
109  //compute the bounding boxes depending on BIter type
110  internal::get_boxes_helper<ObjectList, VolumeList, BIter>()(objects, boxBegin, boxEnd, objBoxes);
111 
112  objCenters.reserve(n);
113  boxes.reserve(n - 1);
114  children.reserve(2 * n - 2);
115 
116  for(int i = 0; i < n; ++i)
117  objCenters.push_back(VIPair(objBoxes[i].center(), i));
118 
119  build(objCenters, 0, n, objBoxes, 0); //the recursive part of the algorithm
120 
121  ObjectList tmp(n);
122  tmp.swap(objects);
123  for(int i = 0; i < n; ++i)
124  objects[i] = tmp[objCenters[i].second];
125  }
126 
128  inline Index getRootIndex() const { return (int)boxes.size() - 1; }
129 
132  EIGEN_STRONG_INLINE void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd,
133  ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
134  { //inlining this function should open lots of optimization opportunities to the compiler
135  if(index < 0) {
136  outVBegin = outVEnd;
137  if(!objects.empty())
138  outOBegin = &(objects[0]);
139  outOEnd = outOBegin + objects.size(); //output all objects--necessary when the tree has only one object
140  return;
141  }
142 
143  int numBoxes = static_cast<int>(boxes.size());
144 
145  int idx = index * 2;
146  if(children[idx + 1] < numBoxes) { //second index is always bigger
147  outVBegin = &(children[idx]);
148  outVEnd = outVBegin + 2;
149  outOBegin = outOEnd;
150  }
151  else if(children[idx] >= numBoxes) { //if both children are objects
152  outVBegin = outVEnd;
153  outOBegin = &(objects[children[idx] - numBoxes]);
154  outOEnd = outOBegin + 2;
155  } else { //if the first child is a volume and the second is an object
156  outVBegin = &(children[idx]);
157  outVEnd = outVBegin + 1;
158  outOBegin = &(objects[children[idx + 1] - numBoxes]);
159  outOEnd = outOBegin + 1;
160  }
161  }
162 
164  inline const Volume &getVolume(Index index) const
165  {
166  return boxes[index];
167  }
168 
169 private:
171  typedef std::vector<VIPair, aligned_allocator<VIPair> > VIPairList;
173  struct VectorComparator //compares vectors, or, more specificall, VIPairs along a particular dimension
174  {
175  VectorComparator(int inDim) : dim(inDim) {}
176  inline bool operator()(const VIPair &v1, const VIPair &v2) const { return v1.first[dim] < v2.first[dim]; }
177  int dim;
178  };
179 
180  //Build the part of the tree between objects[from] and objects[to] (not including objects[to]).
181  //This routine partitions the objCenters in [from, to) along the dimension dim, recursively constructs
182  //the two halves, and adds their parent node. TODO: a cache-friendlier layout
183  void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
184  {
185  eigen_assert(to - from > 1);
186  if(to - from == 2) {
187  boxes.push_back(objBoxes[objCenters[from].second].merged(objBoxes[objCenters[from + 1].second]));
188  children.push_back(from + (int)objects.size() - 1); //there are objects.size() - 1 tree nodes
189  children.push_back(from + (int)objects.size());
190  }
191  else if(to - from == 3) {
192  int mid = from + 2;
193  std::nth_element(objCenters.begin() + from, objCenters.begin() + mid,
194  objCenters.begin() + to, VectorComparator(dim)); //partition
195  build(objCenters, from, mid, objBoxes, (dim + 1) % Dim);
196  int idx1 = (int)boxes.size() - 1;
197  boxes.push_back(boxes[idx1].merged(objBoxes[objCenters[mid].second]));
198  children.push_back(idx1);
199  children.push_back(mid + (int)objects.size() - 1);
200  }
201  else {
202  int mid = from + (to - from) / 2;
203  nth_element(objCenters.begin() + from, objCenters.begin() + mid,
204  objCenters.begin() + to, 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() index into objects.
216  VolumeList boxes;
217  ObjectList objects;
218 };
219 
220 } // end namespace Eigen
221 
222 #endif //KDBVH_H_INCLUDED
_Scalar Scalar
Definition: KdBVH.h:73
void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes)
Definition: KdBVH.h:43
#define EIGEN_STRONG_INLINE
Definition: Macros.h:493
ObjectList objects
Definition: KdBVH.h:217
KdBVH(Iter begin, Iter end)
Definition: KdBVH.h:83
KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:86
Box2d bounding_box(const Vector2d &v)
Definition: BVH_Example.cpp:9
const Object * ObjectIterator
Definition: KdBVH.h:78
Definition: LDLT.h:16
void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:94
std::vector< Volume, aligned_allocator< Volume > > VolumeList
Definition: KdBVH.h:75
_Object Object
Definition: KdBVH.h:71
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
Definition: Memory.h:691
An axis aligned box.
void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
Definition: KdBVH.h:183
Index getRootIndex() const
Definition: KdBVH.h:128
EIGEN_STRONG_INLINE void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd, ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
Definition: KdBVH.h:132
Matrix< Scalar, Dim, 1 > VectorType
Definition: KdBVH.h:172
#define eigen_assert(x)
Definition: Macros.h:577
const int * VolumeIterator
Definition: KdBVH.h:77
bool operator()(const VIPair &v1, const VIPair &v2) const
Definition: KdBVH.h:176
internal::vector_int_pair< Scalar, Dim > VIPair
Definition: KdBVH.h:170
AlignedBox< Scalar, Dim > Volume
Definition: KdBVH.h:74
const Volume & getVolume(Index index) const
Definition: KdBVH.h:164
const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t r=mpreal::get_default_rnd())
Definition: mpreal.h:2201
VolumeList boxes
Definition: KdBVH.h:216
int Index
Definition: KdBVH.h:76
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
A simple bounding volume hierarchy based on AlignedBox.
Definition: KdBVH.h:67
void operator()(const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes)
Definition: KdBVH.h:34
std::vector< VIPair, aligned_allocator< VIPair > > VIPairList
Definition: KdBVH.h:171
void init(Iter begin, Iter end)
Definition: KdBVH.h:90
std::vector< Object, aligned_allocator< Object > > ObjectList
Definition: KdBVH.h:72
std::vector< int > children
Definition: KdBVH.h:215


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:18