00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef LASS_GUARDIAN_OF_INCLUSION_SPAT_AABP_TREE_H
00053 #define LASS_GUARDIAN_OF_INCLUSION_SPAT_AABP_TREE_H
00054
00055
00056
00057 #include "spat_common.h"
00058 #include "default_object_traits.h"
00059 #include "split_heuristics.h"
00060
00061 namespace lass
00062 {
00063 namespace spat
00064 {
00065
00066 template
00067 <
00068 class ObjectType,
00069 class ObjectTraits = DefaultObjectTraits<ObjectType>,
00070 typename SplitHeuristics = DefaultSplitHeuristics<>
00071 >
00072 class AabpTree
00073 {
00074 public:
00075
00076 typedef AabpTree<ObjectType, ObjectTraits, SplitHeuristics> TSelf;
00077
00078 typedef ObjectType TObject;
00079 typedef ObjectTraits TObjectTraits;
00080 typedef SplitHeuristics TSplitHeuristics;
00081
00082 typedef typename TObjectTraits::TObjectIterator TObjectIterator;
00083 typedef typename TObjectTraits::TObjectReference TObjectReference;
00084 typedef typename TObjectTraits::TAabb TAabb;
00085 typedef typename TObjectTraits::TRay TRay;
00086 typedef typename TObjectTraits::TPoint TPoint;
00087 typedef typename TObjectTraits::TVector TVector;
00088 typedef typename TObjectTraits::TValue TValue;
00089 typedef typename TObjectTraits::TParam TParam;
00090 typedef typename TObjectTraits::TReference TReference;
00091 typedef typename TObjectTraits::TConstReference TConstReference;
00092 typedef typename TObjectTraits::TInfo TInfo;
00093
00094 enum { dimension = TObjectTraits::dimension };
00095
00096 typedef std::vector<TObjectIterator> TObjectIterators;
00097
00098 class Neighbour
00099 {
00100 public:
00101 Neighbour() {}
00102 Neighbour(TObjectIterator object, TValue squaredDistance):
00103 object_(object), squaredDistance_(squaredDistance) {}
00104 TObjectIterator object() const { return object_; }
00105 TValue squaredDistance() const { return squaredDistance_; }
00106 TObjectIterator operator->() const { return object_; }
00107 TObjectReference operator*() const { return TObjectTraits::object(object_); }
00108 bool operator<(const Neighbour& other) const { return squaredDistance_ < other.squaredDistance_; }
00109 private:
00110 TObjectIterator object_;
00111 TValue squaredDistance_;
00112 };
00113
00114 AabpTree();
00115 AabpTree(TObjectIterator first, TObjectIterator last);
00116
00117 void reset();
00118 void reset(TObjectIterator first, TObjectIterator last);
00119
00120 const TAabb& aabb() const;
00121
00122 const bool contains(const TPoint& point, const TInfo* info = 0) const;
00123 template <typename OutputIterator>
00124 OutputIterator find(const TPoint& point, OutputIterator result, const TInfo* info = 0) const;
00125 const TObjectIterator intersect(const TRay& ray, TReference t, TParam minT = 0,
00126 const TInfo* info = 0) const;
00127 const bool intersects(const TRay& ray, TParam minT = 0,
00128 TParam maxT = std::numeric_limits<TValue>::infinity(), const TInfo* info = 0) const;
00129 const Neighbour nearestNeighbour(const TPoint& point, const TInfo* info = 0) const;
00130 template <typename RandomIterator>
00131 RandomIterator rangeSearch(const TPoint& center, TParam maxRadius, size_t maxCount,
00132 RandomIterator first, const TInfo* info = 0) const;
00133
00134 void swap(TSelf& other);
00135 const bool isEmpty() const;
00136 const TObjectIterator end() const;
00137
00138 private:
00139
00140 struct Input
00141 {
00142 TAabb aabb;
00143 TObjectIterator object;
00144 Input(const TAabb& aabb, TObjectIterator object): aabb(aabb), object(object) {}
00145 };
00146 typedef std::vector<Input> TInputs;
00147 typedef typename TInputs::iterator TInputIterator;
00148
00149 class Node
00150 {
00151 public:
00152 Node(int axis)
00153 {
00154 LASS_ASSERT(axis >= 0 && axis < dimension);
00155 axis_ = axis;
00156 }
00157 Node(int first, int last)
00158 {
00159 LASS_ASSERT(first >= 0 && last > first);
00160 first_ = first;
00161 last_ = -last - 1;
00162 LASS_ASSERT(last_ < 0);
00163 }
00164
00165 const bool isInternal() const { return axis_ >= 0; }
00166 TParam leftBound() const { LASS_ASSERT(isInternal()); return leftBound_; }
00167 TReference leftBound() { LASS_ASSERT(isInternal()); return leftBound_; }
00168 TParam rightBound() const { LASS_ASSERT(isInternal()); return rightBound_; }
00169 TReference rightBound() { LASS_ASSERT(isInternal()); return rightBound_; }
00170 const int axis() const { LASS_ASSERT(isInternal()); return axis_; }
00171 const int right() const { LASS_ASSERT(isInternal()); return right_; }
00172 int& right() { LASS_ASSERT(isInternal()); return right_; }
00173
00174 const bool isLeaf() const { return last_ < 0; }
00175 const int first() const { LASS_ASSERT(isLeaf()); return first_; }
00176 const int last() const { LASS_ASSERT(isLeaf()); return -last_ - 1; }
00177
00178 private:
00179 TValue leftBound_;
00180 TValue rightBound_;
00181 union
00182 {
00183 int right_;
00184 int first_;
00185 };
00186 union
00187 {
00188 int axis_;
00189 int last_;
00190 };
00191 };
00192 typedef std::vector<Node> TNodes;
00193
00194 struct BalanceResult
00195 {
00196 TAabb aabb;
00197 int index;
00198 BalanceResult(const TAabb& aabb, int index): aabb(aabb), index(index) {}
00199 };
00200
00201 const BalanceResult balance(TInputIterator iFirst, TInputIterator iLast);
00202 const int addLeafNode(TInputIterator iFirst, TInputIterator iLast);
00203 const int addInternalNode(int iAxis);
00204
00205 const bool doContains(int index, const TPoint& point, const TInfo* info) const;
00206 template <typename OutputIterator>
00207 OutputIterator doFind(int index, const TPoint& point,
00208 OutputIterator iResult, const TInfo* info) const;
00209 const TObjectIterator doIntersect(int index, const TRay& ray, TReference t, TParam tMin,
00210 const TInfo* info, const TVector& reciprocalDirection, TParam tNear, TParam tFar) const;
00211 const bool doIntersects(int index, const TRay& ray, TParam tMin, TParam tMax,
00212 const TInfo* info, const TVector& reciprocalDirection, TParam tNear, TParam tFar) const;
00213 void doNearestNeighbour(int index, const TPoint& point, const TInfo* info, Neighbour& best) const;
00214 template <typename RandomIterator>
00215 RandomIterator doRangeSearch(int index, const TPoint& point, TReference squaredRadius,
00216 size_t maxCount, RandomIterator first, RandomIterator last, const TInfo* info) const;
00217
00218 void getChildren(int index, const TPoint& target, int indices[2], TValue signedDistances[2]) const;
00219
00220 TAabb aabb_;
00221 TObjectIterators objects_;
00222 TNodes nodes_;
00223 TObjectIterator end_;
00224 };
00225
00226 }
00227
00228 }
00229
00230 #include "aabp_tree.inl"
00231
00232 #endif
00233
00234