10 #ifndef KDBVH_H_INCLUDED 11 #define KDBVH_H_INCLUDED 14 #include "./InternalHeaderCheck.h" 21 template <
typename Scalar,
int Dim>
22 struct vector_int_pair {
23 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Dim)
26 vector_int_pair(
const VectorType &v,
int i) : first(v), second(i) {}
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);
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]));
67 template <
typename Scalar_,
int Dim_,
typename Object_>
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;
77 typedef const int *VolumeIterator;
78 typedef const Object *ObjectIterator;
84 template <
typename Iter>
91 template <
typename OIter,
typename BIter>
92 KdBVH(OIter begin, OIter
end, BIter boxBegin, BIter boxEnd) {
93 init(begin,
end, boxBegin, boxEnd);
98 template <
typename Iter>
105 template <
typename OIter,
typename BIter>
106 void init(OIter begin, OIter
end, BIter boxBegin, BIter boxEnd) {
111 objects.insert(objects.end(), begin,
end);
112 int n =
static_cast<int>(objects.size());
117 VIPairList objCenters;
120 internal::get_boxes_helper<ObjectList, VolumeList, BIter>()(objects, boxBegin, boxEnd, objBoxes);
122 objCenters.reserve(n);
123 boxes.reserve(n - 1);
124 children.reserve(2 * n - 2);
126 for (
int i = 0; i < n; ++i) objCenters.push_back(VIPair(objBoxes[i].center(), i));
128 build(objCenters, 0, n, objBoxes, 0);
132 for (
int i = 0; i < n; ++i) objects[i] = tmp[objCenters[i].second];
140 EIGEN_STRONG_INLINE
void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd,
141 ObjectIterator &outOBegin, ObjectIterator &outOEnd)
145 if (!objects.empty()) outOBegin = &(objects[0]);
146 outOEnd = outOBegin + objects.size();
150 int numBoxes =
static_cast<int>(boxes.size());
153 if (children[idx + 1] < numBoxes) {
154 outVBegin = &(children[idx]);
155 outVEnd = outVBegin + 2;
157 }
else if (children[idx] >= numBoxes) {
159 outOBegin = &(objects[children[idx] - numBoxes]);
160 outOEnd = outOBegin + 2;
162 outVBegin = &(children[idx]);
163 outVEnd = outVBegin + 1;
164 outOBegin = &(objects[children[idx + 1] - numBoxes]);
165 outOEnd = outOBegin + 1;
173 typedef internal::vector_int_pair<Scalar, Dim> VIPair;
174 typedef std::vector<VIPair, aligned_allocator<VIPair> > VIPairList;
176 struct VectorComparator
178 VectorComparator(
int inDim) : dim(inDim) {}
179 inline bool operator()(
const VIPair &v1,
const VIPair &v2)
const {
return v1.first[dim] < v2.first[dim]; }
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);
191 children.push_back(from + (
int)objects.size());
192 }
else if (to - from == 3) {
194 std::nth_element(objCenters.begin() + from, objCenters.begin() + mid, objCenters.begin() + to,
195 VectorComparator(dim));
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);
202 int mid = from + (to - from) / 2;
203 nth_element(objCenters.begin() + from, objCenters.begin() + mid, objCenters.begin() + to,
204 VectorComparator(dim));
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);
215 std::vector<int> children;
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