Library of Assembled Shared Sources
aabp_tree.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::spat::AabpTree
46 * @brief an AABB bounding volume tree that looks similar to a KdTree
47 * @author Bram de Greve [BdG]
48 *
49 * the AabbTree does NOT own the objects. You must keep them yourself!
50 */
51
52#ifndef LASS_GUARDIAN_OF_INCLUSION_SPAT_AABP_TREE_H
53#define LASS_GUARDIAN_OF_INCLUSION_SPAT_AABP_TREE_H
54
55//#define LASS_SPAT_AABB_TREE_DIAGNOSTICS
56
57#include "spat_common.h"
59#include "split_heuristics.h"
60#include "../util/shared_ptr.h"
61
62namespace lass
63{
64namespace spat
65{
66
67template
68<
69 class ObjectType,
70 class ObjectTraits = DefaultObjectTraits<ObjectType>,
71 typename SplitHeuristics = DefaultSplitHeuristics
72>
73class AabpTree: public SplitHeuristics
74{
75public:
76
77 typedef AabpTree<ObjectType, ObjectTraits, SplitHeuristics> TSelf;
78
79 typedef ObjectType TObject;
80 typedef ObjectTraits TObjectTraits;
81 typedef SplitHeuristics TSplitHeuristics;
82
83 typedef typename TObjectTraits::TObjectIterator TObjectIterator;
84 typedef typename TObjectTraits::TObjectReference TObjectReference;
85 typedef typename TObjectTraits::TAabb TAabb;
86 typedef typename TObjectTraits::TRay TRay;
87 typedef typename TObjectTraits::TPoint TPoint;
88 typedef typename TObjectTraits::TVector TVector;
89 typedef typename TObjectTraits::TValue TValue;
90 typedef typename TObjectTraits::TParam TParam;
91 typedef typename TObjectTraits::TReference TReference;
92 typedef typename TObjectTraits::TConstReference TConstReference;
93 typedef typename TObjectTraits::TInfo TInfo;
94
95 enum
96 {
97 dimension = TObjectTraits::dimension,
98 defaultMaxObjectsPerLeaf = 1,
99 defaultMaxDepth = 20
100 };
101
102 typedef std::vector<TObjectIterator> TObjectIterators;
103
104 class Neighbour
105 {
106 public:
107 Neighbour() {}
108 Neighbour(TObjectIterator object, TValue squaredDistance):
109 object_(object), squaredDistance_(squaredDistance) {}
110 TObjectIterator object() const { return object_; }
111 TValue squaredDistance() const { return squaredDistance_; }
112 TObjectIterator operator->() const { return object_; }
113 TObjectReference operator*() const { return TObjectTraits::object(object_); }
114 bool operator<(const Neighbour& other) const { return squaredDistance_ < other.squaredDistance_; }
115 private:
116 TObjectIterator object_;
117 TValue squaredDistance_;
118 };
119
120 AabpTree(const TSplitHeuristics& heuristics = TSplitHeuristics(defaultMaxObjectsPerLeaf, defaultMaxDepth));
121 AabpTree(TObjectIterator first, TObjectIterator last, const TSplitHeuristics& heuristics = TSplitHeuristics(defaultMaxObjectsPerLeaf, defaultMaxDepth));
122 AabpTree(TSelf&& other) noexcept;
123
124 TSelf& operator=(TSelf&& other) noexcept;
125
126 void reset();
127 void reset(TObjectIterator first, TObjectIterator last);
128
129 const TAabb& aabb() const;
130
131 bool contains(const TPoint& point, const TInfo* info = 0) const;
132
133 template <typename OutputIterator>
134 OutputIterator find(const TPoint& point, OutputIterator result, const TInfo* info = 0) const;
135 template <typename OutputIterator>
136 OutputIterator find(const TAabb& box, OutputIterator result, const TInfo* info = 0) const;
137 template <typename OutputIterator>
138 OutputIterator find(const TRay& ray, TParam minT, TParam maxT, OutputIterator result, const TInfo* info = 0) const;
139
140 const TObjectIterator intersect(const TRay& ray, TReference t, TParam minT = 0, const TInfo* info = 0) const;
141 bool intersects(const TRay& ray, TParam minT = 0, TParam maxT = std::numeric_limits<TValue>::infinity(), const TInfo* info = 0) const;
142
143 const Neighbour nearestNeighbour(const TPoint& point, const TInfo* info = 0) const;
144 template <typename RandomIterator>
145 RandomIterator rangeSearch(const TPoint& center, TParam maxRadius, size_t maxCount, RandomIterator first, const TInfo* info = 0) const;
146
147 void swap(TSelf& other);
148 bool isEmpty() const;
149 const TObjectIterator end() const;
150
151private:
152
153 struct Input
154 {
155 TAabb aabb;
156 TObjectIterator object;
157 Input(const TAabb& aabb, TObjectIterator object): aabb(aabb), object(object) {}
158 };
159 typedef std::vector<Input> TInputs;
160 typedef typename TInputs::iterator TInputIterator;
161
162 typedef unsigned TIndex;
163 typedef num::NumTraits<TIndex>::signedType TSignedIndex;
164 static constexpr size_t maxSize = static_cast<size_t>(num::NumTraits<TSignedIndex>::max) + 1;
165
166 class Node
167 {
168 public:
169 Node(size_t axis)
170 {
171 LASS_ASSERT(axis < dimension);
172 axis_ = static_cast<TSignedIndex>(axis);
173 }
174 Node(TIndex first, TIndex last)
175 {
176 LASS_ASSERT(last > first);
177 first_ = first;
178 last_ = -static_cast<TSignedIndex>(last) - 1;
179 LASS_ASSERT(last_ < 0);
180 }
181
182 bool isInternal() const { return axis_ >= 0; }
183 TParam leftBound() const { LASS_ASSERT(isInternal()); return leftBound_; }
184 void setLeftBound(TParam x) { LASS_ASSERT(isInternal()); leftBound_ = x; }
185 TParam rightBound() const { LASS_ASSERT(isInternal()); return rightBound_; }
186 void setRightBound(TParam x) { LASS_ASSERT(isInternal()); rightBound_ = x; }
187 size_t axis() const { LASS_ASSERT(isInternal()); return static_cast<size_t>(axis_); }
188 TIndex right() const { LASS_ASSERT(isInternal()); return right_; }
189 void setRight(TIndex right) { LASS_ASSERT(isInternal()); right_ = right; }
190
191 bool isLeaf() const { return last_ < 0; }
192 TIndex first() const { LASS_ASSERT(isLeaf()); return first_; }
193 TIndex last() const { LASS_ASSERT(isLeaf()); return static_cast<TIndex>(-(last_ + 1)); }
194
195 private:
196 TValue leftBound_; // internal
197 TValue rightBound_; // internal
198 union
199 {
200 TIndex right_; // internal
201 TIndex first_; // leaf
202 };
203 union
204 {
205 TSignedIndex axis_; // internal
206 TSignedIndex last_; // leaf
207 };
208 };
209 typedef std::vector<Node> TNodes;
210
211 struct BalanceResult
212 {
213 TAabb aabb;
214 TIndex index;
215 BalanceResult(const TAabb& aabb, TIndex index): aabb(aabb), index(index) {}
216 };
217
218 const BalanceResult balance(TInputIterator iFirst, TInputIterator iLast);
219 TIndex addLeafNode(TInputIterator iFirst, TInputIterator iLast);
220 TIndex addInternalNode(size_t iAxis);
221
222 bool doContains(TIndex index, const TPoint& point, const TInfo* info) const;
223
224 template <typename OutputIterator>
225 OutputIterator doFind(TIndex index, const TPoint& point, OutputIterator iResult, const TInfo* info) const;
226 template <typename OutputIterator>
227 OutputIterator doFind(TIndex index, const TAabb& box, OutputIterator iResult, const TInfo* info) const;
228 template <typename OutputIterator>
229 OutputIterator doFind(TIndex index, const TRay& ray, TParam tMin, TParam tMax, OutputIterator iResult, const TInfo* info,
230 const TVector& reciprocalDirection, TParam tNear, TParam tFar) const;
231
232 const TObjectIterator doIntersect(
233 TIndex index, const TRay& ray, TReference t, TParam tMin, const TInfo* info,
234 const TVector& reciprocalDirection, TParam tNear, TParam tFar) const;
235 bool doIntersects(TIndex index, const TRay& ray, TParam tMin, TParam tMax, const TInfo* info,
236 const TVector& reciprocalDirection, TParam tNear, TParam tFar) const;
237
238 void doNearestNeighbour(TIndex index, const TPoint& point, const TInfo* info, Neighbour& best) const;
239 template <typename RandomIterator>
240 RandomIterator doRangeSearch(TIndex index, const TPoint& point, TReference squaredRadius,
241 size_t maxCount, RandomIterator first, RandomIterator last, const TInfo* info) const;
242
243 void getChildren(TIndex index, const TPoint& target, TIndex indices[2], TValue signedDistances[2]) const;
244
245 TAabb aabb_;
246 TObjectIterators objects_;
247 TNodes nodes_;
248 std::unique_ptr<TObjectIterator> end_;
249};
250
251}
252
253}
254
255#include "aabp_tree.inl"
256
257#endif
258
259// EOF
spatial subdivisions, quadtrees, octrees, meshes in 2D and 3D, triangulators, ...
Definition aabb8_tree.h:80
Library for Assembled Shared Sources.
Definition config.h:53
default traits for objects to be stored in spatial subdivision trees