10 #ifndef KDBVH_H_INCLUDED 11 #define KDBVH_H_INCLUDED 18 template<
typename Scalar,
int Dim>
32 template<
typename ObjectList,
typename VolumeList,
typename BoxIter>
34 void operator()(
const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes)
36 outBoxes.insert(outBoxes.end(), boxBegin, boxEnd);
42 template<
typename ObjectList,
typename VolumeList>
44 void operator()(
const ObjectList &objects,
int,
int, VolumeList &outBoxes)
46 outBoxes.reserve(objects.size());
47 for(
int i = 0;
i < (
int)objects.size(); ++
i)
68 template<
typename _Scalar,
int _Dim,
typename _Object>
class KdBVH 73 typedef std::vector<Object, aligned_allocator<Object> >
ObjectList;
76 typedef std::vector<Volume, aligned_allocator<Volume> >
VolumeList;
84 template<
typename Iter>
KdBVH(Iter begin, Iter
end) {
init(begin, end, 0, 0); }
87 template<
typename OIter,
typename BIter>
KdBVH(OIter begin, OIter
end, BIter boxBegin, BIter boxEnd) {
init(begin, end, boxBegin, boxEnd); }
91 template<
typename Iter>
void init(Iter begin, Iter
end) {
init(begin, end, 0, 0); }
95 template<
typename OIter,
typename BIter>
void init(OIter begin, OIter
end, BIter boxBegin, BIter boxEnd)
101 objects.insert(objects.end(), begin,
end);
102 int n =
static_cast<int>(objects.size());
113 objCenters.reserve(n);
114 boxes.reserve(n - 1);
115 children.reserve(2 * n - 2);
117 for(
int i = 0;
i <
n; ++
i)
118 objCenters.push_back(
VIPair(objBoxes[
i].center(),
i));
120 build(objCenters, 0, n, objBoxes, 0);
124 for(
int i = 0;
i <
n; ++
i)
125 objects[
i] = tmp[objCenters[
i].
second];
134 ObjectIterator &outOBegin, ObjectIterator &outOEnd)
const 139 outOBegin = &(objects[0]);
140 outOEnd = outOBegin + objects.size();
144 int numBoxes =
static_cast<int>(boxes.size());
147 if(children[idx + 1] < numBoxes) {
148 outVBegin = &(children[idx]);
149 outVEnd = outVBegin + 2;
152 else if(children[idx] >= numBoxes) {
154 outOBegin = &(objects[children[idx] - numBoxes]);
155 outOEnd = outOBegin + 2;
157 outVBegin = &(children[idx]);
158 outVEnd = outVBegin + 1;
159 outOBegin = &(objects[children[idx + 1] - numBoxes]);
160 outOEnd = outOBegin + 1;
172 typedef std::vector<VIPair, aligned_allocator<VIPair> >
VIPairList;
184 void build(VIPairList &objCenters,
int from,
int to,
const VolumeList &objBoxes,
int dim)
188 boxes.push_back(objBoxes[objCenters[from].
second].merged(objBoxes[objCenters[from + 1].
second]));
189 children.push_back(from + (
int)objects.size() - 1);
190 children.push_back(from + (
int)objects.size());
192 else if(to - from == 3) {
194 std::nth_element(objCenters.begin() + from, objCenters.begin() + mid,
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);
203 int mid = from + (to - from) / 2;
204 nth_element(objCenters.begin() + from, objCenters.begin() + mid,
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);
223 #endif //KDBVH_H_INCLUDED
void operator()(const ObjectList &objects, int, int, VolumeList &outBoxes)
#define EIGEN_STRONG_INLINE
KdBVH(Iter begin, Iter end)
KdBVH(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
Box2d bounding_box(const Vector2d &v)
const Object * ObjectIterator
Namespace containing all symbols from the Eigen library.
void init(OIter begin, OIter end, BIter boxBegin, BIter boxEnd)
std::vector< Volume, aligned_allocator< Volume > > VolumeList
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
VectorComparator(int inDim)
void build(VIPairList &objCenters, int from, int to, const VolumeList &objBoxes, int dim)
Index getRootIndex() const
EIGEN_STRONG_INLINE void getChildren(Index index, VolumeIterator &outVBegin, VolumeIterator &outVEnd, ObjectIterator &outOBegin, ObjectIterator &outOEnd) const
Matrix< Scalar, Dim, 1 > VectorType
const int * VolumeIterator
bool operator()(const VIPair &v1, const VIPair &v2) const
internal::vector_int_pair< Scalar, Dim > VIPair
AlignedBox< Scalar, Dim > Volume
const Volume & getVolume(Index index) const
const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t r=mpreal::get_default_rnd())
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
The matrix class, also used for vectors and row-vectors.
A simple bounding volume hierarchy based on AlignedBox.
void operator()(const ObjectList &objects, BoxIter boxBegin, BoxIter boxEnd, VolumeList &outBoxes)
std::vector< VIPair, aligned_allocator< VIPair > > VIPairList
void init(Iter begin, Iter end)
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
std::vector< Object, aligned_allocator< Object > > ObjectList
std::vector< int > children