Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef KDBVH_H_INCLUDED
12#define KDBVH_H_INCLUDED
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21// internal pair class for the BVH--used instead of std::pair because of alignment
22template <typename Scalar, int Dim>
23struct vector_int_pair {
24 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Dim)
25 typedef Matrix<Scalar, Dim, 1> VectorType;
26
27 vector_int_pair(const VectorType &v, int i) : first(v), second(i) {}
28
29 VectorType first;
30 int second;
31};
32
33// these templates help the tree initializer get the bounding boxes either from a provided
34// iterator range or using bounding_box in a unified way
35template <typename ObjectList, typename VolumeList, typename BoxIter>
36struct get_boxes_helper {
37 void operator()(const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes) const {
38 outBoxes.insert(outBoxes.end(), boxBegin, boxEnd);
39 eigen_assert(outBoxes.size() == objects.size());
40 EIGEN_ONLY_USED_FOR_DEBUG(objects);
41 }
42};
43
44template <typename ObjectList, typename VolumeList>
45struct get_boxes_helper<ObjectList, VolumeList, int> {
46 void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes) const {
47 outBoxes.reserve(objects.size());
48 for (int i = 0; i < (int)objects.size(); ++i) outBoxes.push_back(bounding_box(objects[i]));
49 }
50};
51
52} // end namespace internal
53
68template <typename Scalar_, int Dim_, typename Object_>
69class KdBVH {
70 public:
71 enum { Dim = Dim_ };
72 typedef Object_ Object;
73 typedef std::vector<Object, aligned_allocator<Object> > ObjectList;
74 typedef Scalar_ Scalar;
75 typedef AlignedBox<Scalar, Dim> Volume;
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() = default;
82
85 template <typename Iter>
86 KdBVH(Iter begin, Iter end) {
87 init(begin, end, 0, 0);
88 } // int is recognized by init as not being an iterator type
89
92 template <typename OIter, typename BIter>
93 KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd) {
94 init(begin, end, boxBegin, boxEnd);
95 }
96
99 template <typename Iter>
100 void init(Iter begin, Iter end) {
101 init(begin, end, 0, 0);
102 }
103
106 template <typename OIter, typename BIter>
107 void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd) {
108 objects.clear();
109 boxes.clear();
110 children.clear();
111
112 objects.insert(objects.end(), begin, end);
113 int n = static_cast<int>(objects.size());
114
115 if (n < 2) return; // if we have at most one object, we don't need any internal nodes
116
117 VolumeList objBoxes;
118 VIPairList objCenters;
119
120 // compute the bounding boxes depending on BIter type
121 internal::get_boxes_helper<ObjectList, VolumeList, BIter>()(objects, boxBegin, boxEnd, objBoxes);
122
123 objCenters.reserve(n);
124 boxes.reserve(n - 1);
125 children.reserve(2 * n - 2);
126
127 for (int i = 0; i < n; ++i) objCenters.push_back(VIPair(objBoxes[i].center(), i));
128
129 build(objCenters, 0, n, objBoxes, 0); // the recursive part of the algorithm
130
131 ObjectList tmp(n);
132 tmp.swap(objects);
133 for (int i = 0; i < n; ++i) objects[i] = tmp[objCenters[i].second];
134 }
135
137 inline Index getRootIndex() const { return (int)boxes.size() - 1; }
138
141 EIGEN_STRONG_INLINE void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd,
142 ObjectIterator &outOBegin, ObjectIterator &outOEnd)
143 const { // inlining this function should open lots of optimization opportunities to the compiler
144 if (index < 0) {
145 outVBegin = outVEnd;
146 if (!objects.empty()) outOBegin = &(objects[0]);
147 outOEnd = outOBegin + objects.size(); // output all objects--necessary when the tree has only one object
148 return;
149 }
150
151 int numBoxes = static_cast<int>(boxes.size());
152
153 int idx = index * 2;
154 if (children[idx + 1] < numBoxes) { // second index is always bigger
155 outVBegin = &(children[idx]);
156 outVEnd = outVBegin + 2;
157 outOBegin = outOEnd;
158 } else if (children[idx] >= numBoxes) { // if both children are objects
159 outVBegin = outVEnd;
160 outOBegin = &(objects[children[idx] - numBoxes]);
161 outOEnd = outOBegin + 2;
162 } else { // if the first child is a volume and the second is an object
163 outVBegin = &(children[idx]);
164 outVEnd = outVBegin + 1;
165 outOBegin = &(objects[children[idx + 1] - numBoxes]);
166 outOEnd = outOBegin + 1;
167 }
168 }
169
171 inline const Volume &getVolume(Index index) const { return boxes[index]; }
172
173 private:
174 typedef internal::vector_int_pair<Scalar, Dim> VIPair;
175 typedef std::vector<VIPair, aligned_allocator<VIPair> > VIPairList;
176 typedef Matrix<Scalar, Dim, 1> VectorType;
177 struct VectorComparator // compares vectors, or more specifically, VIPairs along a particular dimension
178 {
179 VectorComparator(int inDim) : dim(inDim) {}
180 inline bool operator()(const VIPair &v1, const VIPair &v2) const { return v1.first[dim] < v2.first[dim]; }
181 int dim;
182 };
183
184 // Build the part of the tree between objects[from] and objects[to] (not including objects[to]).
185 // This routine partitions the objCenters in [from, to) along the dimension dim, recursively constructs
186 // the two halves, and adds their parent node. TODO: a cache-friendlier layout
187 void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim) {
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 } else if (to - from == 3) {
194 int mid = from + 2;
195 std::nth_element(objCenters.begin() + from, objCenters.begin() + mid, objCenters.begin() + to,
196 VectorComparator(dim)); // partition
197 build(objCenters, from, mid, objBoxes, (dim + 1) % Dim);
198 int idx1 = (int)boxes.size() - 1;
199 boxes.push_back(boxes[idx1].merged(objBoxes[objCenters[mid].second]));
200 children.push_back(idx1);
201 children.push_back(mid + (int)objects.size() - 1);
202 } else {
203 int mid = from + (to - from) / 2;
204 nth_element(objCenters.begin() + from, objCenters.begin() + mid, objCenters.begin() + to,
205 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()
217 // index into objects.
218 VolumeList boxes;
219 ObjectList objects;
220};
221
222} // end namespace Eigen
223
224#endif // KDBVH_H_INCLUDED
void init(Iter begin, Iter end)
Definition KdBVH.h:100
const Volume & getVolume(Index index) const
Definition KdBVH.h:171
KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition KdBVH.h:93
void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Definition KdBVH.h:107
KdBVH(Iter begin, Iter end)
Definition KdBVH.h:86
void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd, ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
Definition KdBVH.h:141
Index getRootIndex() const
Definition KdBVH.h:137
Namespace containing all symbols from the Eigen library.