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());
39  }
40 };
41 
42 template<typename ObjectList, typename VolumeList>
43 struct get_boxes_helper<ObjectList, VolumeList, int> {
44  void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes)
45  {
46  outBoxes.reserve(objects.size());
47  for(int i = 0; i < (int)objects.size(); ++i)
48  outBoxes.push_back(bounding_box(objects[i]));
49  }
50 };
51 
52 } // end namespace internal
53 
54 
68 template<typename _Scalar, int _Dim, typename _Object> class KdBVH
69 {
70 public:
71  enum { Dim = _Dim };
72  typedef _Object Object;
73  typedef std::vector<Object, aligned_allocator<Object> > ObjectList;
74  typedef _Scalar Scalar;
76  typedef std::vector<Volume, aligned_allocator<Volume> > VolumeList;
77  typedef int Index;
78  typedef const int *VolumeIterator; //the iterators are just pointers into the tree's vectors
79  typedef const Object *ObjectIterator;
80 
81  KdBVH() {}
82 
84  template<typename Iter> KdBVH(Iter begin, Iter end) { init(begin, end, 0, 0); } //int is recognized by init as not being an iterator type
85 
87  template<typename OIter, typename BIter> KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd) { init(begin, end, boxBegin, boxEnd); }
88 
91  template<typename Iter> void init(Iter begin, Iter end) { init(begin, end, 0, 0); }
92 
95  template<typename OIter, typename BIter> void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
96  {
97  objects.clear();
98  boxes.clear();
99  children.clear();
100 
101  objects.insert(objects.end(), begin, end);
102  int n = static_cast<int>(objects.size());
103 
104  if(n < 2)
105  return; //if we have at most one object, we don't need any internal nodes
106 
107  VolumeList objBoxes;
108  VIPairList objCenters;
109 
110  //compute the bounding boxes depending on BIter type
112 
113  objCenters.reserve(n);
114  boxes.reserve(n - 1);
115  children.reserve(2 * n - 2);
116 
117  for(int i = 0; i < n; ++i)
118  objCenters.push_back(VIPair(objBoxes[i].center(), i));
119 
120  build(objCenters, 0, n, objBoxes, 0); //the recursive part of the algorithm
121 
122  ObjectList tmp(n);
123  tmp.swap(objects);
124  for(int i = 0; i < n; ++i)
125  objects[i] = tmp[objCenters[i].second];
126  }
127 
129  inline Index getRootIndex() const { return (int)boxes.size() - 1; }
130 
134  ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
135  { //inlining this function should open lots of optimization opportunities to the compiler
136  if(index < 0) {
137  outVBegin = outVEnd;
138  if(!objects.empty())
139  outOBegin = &(objects[0]);
140  outOEnd = outOBegin + objects.size(); //output all objects--necessary when the tree has only one object
141  return;
142  }
143 
144  int numBoxes = static_cast<int>(boxes.size());
145 
146  int idx = index * 2;
147  if(children[idx + 1] < numBoxes) { //second index is always bigger
148  outVBegin = &(children[idx]);
149  outVEnd = outVBegin + 2;
150  outOBegin = outOEnd;
151  }
152  else if(children[idx] >= numBoxes) { //if both children are objects
153  outVBegin = outVEnd;
154  outOBegin = &(objects[children[idx] - numBoxes]);
155  outOEnd = outOBegin + 2;
156  } else { //if the first child is a volume and the second is an object
157  outVBegin = &(children[idx]);
158  outVEnd = outVBegin + 1;
159  outOBegin = &(objects[children[idx + 1] - numBoxes]);
160  outOEnd = outOBegin + 1;
161  }
162  }
163 
165  inline const Volume &getVolume(Index index) const
166  {
167  return boxes[index];
168  }
169 
170 private:
172  typedef std::vector<VIPair, aligned_allocator<VIPair> > VIPairList;
174  struct VectorComparator //compares vectors, or more specifically, VIPairs along a particular dimension
175  {
176  VectorComparator(int inDim) : dim(inDim) {}
177  inline bool operator()(const VIPair &v1, const VIPair &v2) const { return v1.first[dim] < v2.first[dim]; }
178  int dim;
179  };
180 
181  //Build the part of the tree between objects[from] and objects[to] (not including objects[to]).
182  //This routine partitions the objCenters in [from, to) along the dimension dim, recursively constructs
183  //the two halves, and adds their parent node. TODO: a cache-friendlier layout
184  void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
185  {
186  eigen_assert(to - from > 1);
187  if(to - from == 2) {
188  boxes.push_back(objBoxes[objCenters[from].second].merged(objBoxes[objCenters[from + 1].second]));
189  children.push_back(from + (int)objects.size() - 1); //there are objects.size() - 1 tree nodes
190  children.push_back(from + (int)objects.size());
191  }
192  else if(to - from == 3) {
193  int mid = from + 2;
194  std::nth_element(objCenters.begin() + from, objCenters.begin() + mid,
195  objCenters.begin() + to, 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  }
202  else {
203  int mid = from + (to - from) / 2;
204  nth_element(objCenters.begin() + from, objCenters.begin() + mid,
205  objCenters.begin() + to, VectorComparator(dim)); //partition
206  build(objCenters, from, mid, objBoxes, (dim + 1) % Dim);
207  int idx1 = (int)boxes.size() - 1;
208  build(objCenters, mid, to, objBoxes, (dim + 1) % Dim);
209  int idx2 = (int)boxes.size() - 1;
210  boxes.push_back(boxes[idx1].merged(boxes[idx2]));
211  children.push_back(idx1);
212  children.push_back(idx2);
213  }
214  }
215 
216  std::vector<int> children; //children of x are children[2x] and children[2x+1], indices bigger than boxes.size() index into objects.
219 };
220 
221 } // end namespace Eigen
222 
223 #endif //KDBVH_H_INCLUDED
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::KdBVH::init
void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:95
Eigen::KdBVH::VectorComparator::VectorComparator
VectorComparator(int inDim)
Definition: KdBVH.h:176
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::KdBVH::KdBVH
KdBVH(Iter begin, Iter end)
Definition: KdBVH.h:84
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::bounding_box
Box2d bounding_box(const Vector2d &v)
Definition: BVH_Example.cpp:9
Eigen::KdBVH
A simple bounding volume hierarchy based on AlignedBox.
Definition: KdBVH.h:68
Eigen::internal::get_boxes_helper::operator()
void operator()(const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes)
Definition: KdBVH.h:34
Eigen::KdBVH::getRootIndex
Index getRootIndex() const
Definition: KdBVH.h:129
Eigen::KdBVH::objects
ObjectList objects
Definition: KdBVH.h:218
Eigen::KdBVH::getChildren
EIGEN_STRONG_INLINE void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd, ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
Definition: KdBVH.h:133
Eigen::KdBVH::VectorComparator
Definition: KdBVH.h:174
Eigen::KdBVH::Scalar
_Scalar Scalar
Definition: KdBVH.h:74
Eigen::KdBVH::VectorType
Matrix< Scalar, Dim, 1 > VectorType
Definition: KdBVH.h:173
Eigen::KdBVH::ObjectIterator
const typedef Object * ObjectIterator
Definition: KdBVH.h:79
Eigen::KdBVH::getVolume
const Volume & getVolume(Index index) const
Definition: KdBVH.h:165
Eigen::KdBVH::Volume
AlignedBox< Scalar, Dim > Volume
Definition: KdBVH.h:75
Eigen::AlignedBox
An axis aligned box.
Definition: ForwardDeclarations.h:292
Eigen::internal::vector_int_pair
Definition: KdBVH.h:19
Eigen::internal::get_boxes_helper
Definition: KdBVH.h:33
EIGEN_ONLY_USED_FOR_DEBUG
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
Definition: Macros.h:1049
Eigen::internal::vector_int_pair::first
VectorType first
Definition: KdBVH.h:26
Eigen::KdBVH::Index
int Index
Definition: KdBVH.h:77
n
int n
Definition: BiCGSTAB_simple.cpp:1
Eigen::internal::get_boxes_helper< ObjectList, VolumeList, int >::operator()
void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes)
Definition: KdBVH.h:44
Eigen::KdBVH::VolumeIterator
const typedef int * VolumeIterator
Definition: KdBVH.h:78
Eigen::internal::vector_int_pair::second
int second
Definition: KdBVH.h:27
Object
Reference counted object base class.
Definition: object.h:9
Eigen::KdBVH::VolumeList
std::vector< Volume, aligned_allocator< Volume > > VolumeList
Definition: KdBVH.h:76
Eigen::KdBVH::boxes
VolumeList boxes
Definition: KdBVH.h:217
Eigen::KdBVH::Object
_Object Object
Definition: KdBVH.h:72
Eigen::KdBVH::KdBVH
KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition: KdBVH.h:87
Eigen::KdBVH::VectorComparator::dim
int dim
Definition: KdBVH.h:178
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::KdBVH::KdBVH
KdBVH()
Definition: KdBVH.h:81
Eigen::KdBVH::VectorComparator::operator()
bool operator()(const VIPair &v1, const VIPair &v2) const
Definition: KdBVH.h:177
Eigen::KdBVH::build
void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
Definition: KdBVH.h:184
Eigen::KdBVH::Dim
@ Dim
Definition: KdBVH.h:71
Eigen::KdBVH::init
void init(Iter begin, Iter end)
Definition: KdBVH.h:91
Eigen::KdBVH::children
std::vector< int > children
Definition: KdBVH.h:216
v2
Vector v2
Definition: testSerializationBase.cpp:39
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
internal
Definition: BandTriangularSolver.h:13
Eigen::KdBVH::VIPairList
std::vector< VIPair, aligned_allocator< VIPair > > VIPairList
Definition: KdBVH.h:172
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
Eigen::KdBVH::ObjectList
std::vector< Object, aligned_allocator< Object > > ObjectList
Definition: KdBVH.h:73
Eigen::KdBVH::VIPair
internal::vector_int_pair< Scalar, Dim > VIPair
Definition: KdBVH.h:171
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
Definition: Memory.h:842
v1
Vector v1
Definition: testSerializationBase.cpp:38
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:01:27