library of assembled shared sources

http://lass.cocamware.com

aabb_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::AabbTree
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_AABB_TREE_H
00053 #define LASS_GUARDIAN_OF_INCLUSION_SPAT_AABB_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     typename ObjectType,
00069     typename ObjectTraits = DefaultObjectTraits<ObjectType>,
00070     typename SplitHeuristics = DefaultSplitHeuristics<>
00071 >
00072 class AabbTree
00073 {
00074 public:
00075 
00076     typedef AabbTree<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     AabbTree();
00115     AabbTree(TObjectIterator first, TObjectIterator last);
00116 
00117     void reset();
00118     void reset(TObjectIterator first, TObjectIterator last);
00119 
00120     const TAabb aabb() const;
00121 
00122     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     TObjectIterator intersect(const TRay& ray, TReference t, TParam tMin = 0, const TInfo* info = 0) const;
00126     bool intersects(const TRay& ray, TParam tMin = 0, 
00127         TParam tMax = std::numeric_limits<TValue>::infinity(), const TInfo* info = 0) const;
00128     const Neighbour nearestNeighbour(const TPoint& point, const TInfo* info = 0) const;
00129     template <typename RandomIterator>
00130     RandomIterator rangeSearch(const TPoint& center, TParam maxRadius, size_t maxCount,
00131             RandomIterator first, const TInfo* info = 0) const; 
00132 
00133     const bool isEmpty() const;
00134     const TObjectIterator end() const;
00135     void swap(TSelf& other);
00136 
00137 private:
00138 
00139     struct Input
00140     {
00141         TAabb aabb;
00142         TObjectIterator object;
00143         Input(const TAabb& aabb, TObjectIterator object): aabb(aabb), object(object) {}
00144     };
00145     typedef std::vector<Input> TInputs;
00146     typedef typename TInputs::iterator TInputIterator;
00147 
00148     class Node
00149     {
00150     public:
00151         explicit Node(const TAabb& aabb): 
00152             aabb_(aabb), 
00153             first_(-1) 
00154         {
00155         }
00156         Node(const TAabb& aabb, int first, int last): 
00157             aabb_(aabb), 
00158             first_(first)
00159         {
00160             LASS_ASSERT(first >= 0 && last > first);
00161             last_ = last; // do union stuff here
00162         }
00163 
00164         const bool isInternal() const { return first_ < 0; }
00165         const TAabb& aabb() const { return aabb_; }
00166         const int right() const { LASS_ASSERT(isInternal()); return right_; }
00167         int& right() { LASS_ASSERT(isInternal()); return right_; }
00168 
00169         const bool isLeaf() const { return first_ >= 0; }
00170         const int first() const { LASS_ASSERT(isLeaf()); return first_; }
00171         const int last() const { LASS_ASSERT(isLeaf()); return last_; }
00172     private:
00173         TAabb aabb_; // both
00174         int first_; // ==-1:internal, >=0:leaf
00175         union 
00176         {
00177             int right_; // internal
00178             int last_; // leaf
00179         };
00180     };
00181     typedef std::deque<Node> TNodes;
00182 
00183     const int balance(TInputIterator first, TInputIterator last);
00184     const int addLeafNode(const TAabb& aabb, TInputIterator first, TInputIterator last);
00185     const int addInternalNode(const TAabb& aabb);
00186 
00187     bool doContains(int index, const TPoint& point, const TInfo* info) const;
00188     template <typename OutputIterator> 
00189     OutputIterator doFind(int index, const TPoint& point, OutputIterator first, const TInfo* info) const;
00190     TObjectIterator doIntersect(int index, const TRay& ray, TReference t, TParam tMin, const TInfo* info) const;
00191     bool doIntersects(int iIndex, const TRay& ray, TParam tMin, TParam tMax, const TInfo* info) const;
00192     void doNearestNeighbour(int index, const TPoint& point, const TInfo* info, Neighbour& best) const;
00193     template <typename RandomIterator>
00194     RandomIterator doRangeSearch(int index, const TPoint& point, TReference squaredRadius, 
00195         size_t maxCount, RandomIterator first, RandomIterator last, const TInfo* info) const;
00196 
00197     void getChildren(int index, const TPoint& target, int indices[2], TValue squaredDistances[2]) const;
00198 
00199     TObjectIterators objects_;
00200     TNodes nodes_;
00201     TObjectIterator end_;
00202 };
00203 
00204 
00205 }
00206 
00207 }
00208 
00209 #include "aabb_tree.inl"
00210 
00211 #endif
00212 
00213 // 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