library of assembled shared sources

http://lass.cocamware.com

aabp_tree.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @class lass::spat::AabpTree
00046  *  @brief an AABB bounding volume tree that looks similar to a KdTree
00047  *  @author Bram de Greve [BdG]
00048  *
00049  *  the AabbTree does NOT own the objects.  You must keep them yourself!
00050  */
00051 
00052 #ifndef LASS_GUARDIAN_OF_INCLUSION_SPAT_AABP_TREE_H
00053 #define LASS_GUARDIAN_OF_INCLUSION_SPAT_AABP_TREE_H
00054 
00055 //#define LASS_SPAT_AABB_TREE_DIAGNOSTICS
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_;  // internal
00180         TValue rightBound_; // internal
00181         union
00182         {
00183             int right_; // internal
00184             int first_; // leaf
00185         };
00186         union
00187         {
00188             int axis_; // internal
00189             int last_; // leaf
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 // EOF

Generated on Mon Nov 10 14:19:59 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo