library of assembled shared sources

http://lass.cocamware.com

quad_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::QuadTree
00046  *  A spatial container for generic objects.  The object needs a traits class which
00047  *  contains the necessary functions to perform the quad tree management for the
00048  *  particular ObjectType.  The traits class needs as a basis the following interface:
00049  *  <tt>
00050  *      static TAabb aabb(const TSimplePolygon3D& iP);
00051  *      static bool contains( const TSimplePolygon3D& iP, const TPoint& point)
00052  *  </tt>
00053  *  The above functions are only examples.  The dimensionality of the primitives must
00054  *  match but can be of any order.  So the quad tree can be used to classify in
00055  *  2 and 3 dimensions.  In three dimensions the more common name is OctTree.
00056  *
00057  *  Higher level divisions can in theory be supported but the dimensional specific
00058  *  part must be reimplemented.  Altough this is only 2 functions and could be written
00059  *  generally this is not yet available.
00060  *
00061  *  @brief a Quad tree for general objects
00062  *  @author Tom De Muer [TDM]
00063  */
00064 
00065 #ifndef LASS_GUARDIAN_OF_INCLUSION_SPAT_QUAD_TREE_H
00066 #define LASS_GUARDIAN_OF_INCLUSION_SPAT_QUAD_TREE_H
00067 
00068 #include "spat_common.h"
00069 #include "impl/quad_tree_helper.h"
00070 
00071 namespace lass
00072 {
00073 namespace spat
00074 {
00075 
00076 template
00077 <
00078     class ObjectType,
00079     class ObjectTraits = DefaultObjectTraits<ObjectType>
00080 >
00081 class QuadTree: private impl::QuadTreeHelper<ObjectTraits, ObjectTraits::dimension>
00082 {
00083 public:
00084 
00085     typedef QuadTree<ObjectType, ObjectTraits> TSelf;
00086     typedef ObjectType TObjectType;
00087     typedef ObjectTraits TObjectTraits;
00088 
00089     typedef typename TObjectTraits::TObjectIterator TObjectIterator;
00090     typedef typename TObjectTraits::TObjectReference TObjectReference;
00091     typedef typename TObjectTraits::TAabb TAabb;
00092     typedef typename TObjectTraits::TRay TRay;
00093     typedef typename TObjectTraits::TPoint TPoint;
00094     typedef typename TObjectTraits::TVector TVector;
00095     typedef typename TObjectTraits::TValue TValue;
00096     typedef typename TObjectTraits::TParam TParam;
00097     typedef typename TObjectTraits::TReference TReference;
00098     typedef typename TObjectTraits::TConstReference TConstReference;
00099     typedef typename TObjectTraits::TInfo TInfo;
00100 
00101     enum { dimension = TObjectTraits::dimension };
00102 
00103     class Neighbour
00104     {
00105     public:
00106         Neighbour() {}
00107         Neighbour(TObjectIterator object, TValue squaredDistance): 
00108             object_(object), squaredDistance_(squaredDistance) {}
00109         TObjectIterator object() const { return object_; }
00110         TValue squaredDistance() const { return squaredDistance_; }
00111         TObjectIterator operator->() const { return object_; }
00112         TObjectReference operator*() const { return TObjectTraits::object(object_); }
00113         bool operator<(const Neighbour& other) const { return squaredDistance_ < other.squaredDistance_; }
00114         bool operator==(const Neighbour& other) const { return object_ == other.object_; }
00115     private:
00116         TObjectIterator object_;
00117         TValue squaredDistance_;
00118     };
00119 
00120     //typedef typename TObjectTraits::TSeparator TSeparator; // ???
00121 
00122     /** constructor that computes bounding box automatically from objects
00123      */
00124     QuadTree(size_t maxSize = 100, size_t maxLevel = 10);
00125 
00126     /** constructor that computes bounding box automatically from objects
00127      */
00128     QuadTree(TObjectIterator first, TObjectIterator last, size_t maxSize = 10, size_t maxLevel = 10);
00129 
00130     /** constructor.
00131     *  @param center       the center of the spatial tree
00132     *  @param extents      the extents of the spatial tree in each direction,
00133     *                       thus the total width of the tree is twice the extents
00134     */
00135     //QuadTree(TObjectIterator first, TObjectIterator last, const TPoint& center, const TVector& extents, size_t maxSize = 100, size_t maxLevel = 10);
00136 
00137     /** constructor.
00138     *  @param box          the bounding box of the spatial tree
00139     */
00140     //QuadTree(TObjectIterator first, TObjectIterator last, const TAabb& box, size_t maxSize = 100, size_t maxLevel = 10);
00141 
00142     ~QuadTree();
00143 
00144     void reset();
00145     void reset(TObjectIterator first, TObjectIterator last);
00146 
00147     /** return true if any of the objects contains point.
00148     *
00149     * Required :
00150     * <tt>static bool ObjectTypeTraits::contains( const Object& object, const TPoint& point, const TInfo* info );</tt>
00151     */
00152     const bool contains(const TPoint& p, const TInfo* info = 0) const;
00153 
00154     /** find objects containing point and write them to the output iterator.
00155     *
00156     * Required :
00157     * <tt>static bool ObjectTypeTraits::contains( const Object& object, const TPoint& point, const TInfo* info );</tt>
00158     */
00159     template <typename OutputIterator>
00160     OutputIterator find(const TPoint& p, OutputIterator result, const TInfo* info = 0) const;
00161 
00162     const TObjectIterator intersect(const TRay& ray, TReference t, TParam tMin = 0, 
00163         const TInfo* info = 0) const;
00164     
00165     const bool intersects(const TRay& ray, TParam tMin = 0, 
00166         TParam maxT = std::numeric_limits<TValue>::infinity(), const TInfo* info = 0) const;
00167 
00168     const Neighbour nearestNeighbour(const TPoint& point, const TInfo* info = 0) const; 
00169 
00170     template <typename RandomIterator>
00171     RandomIterator rangeSearch(const TPoint& center, TParam maxRadius, size_t maxCount,
00172             RandomIterator first, const TInfo* info = 0) const; 
00173 
00174     /** contains.  Returns the number of object that returned sFront on all the lines
00175     *   provided in the iFrustum vector and _adds_ them to the vector oObjects.  An example
00176     *   of use is frustum culling.
00177     *
00178     * Required :
00179     *  <tt>static bool ObjectTypeTraits::contains( const Object& object, const TPoint& point );</tt>
00180     */
00181     //size_t visible( const std::vector<TSeparator>& iFrustum, std::vector<ObjectType*>& oObjects ) const;
00182 
00183     void add(TObjectIterator object);
00184     size_t objectCount() const;
00185 
00186     const TAabb& aabb() const;
00187 
00188     /** depth. Returns the depth of the tree */
00189     const size_t depth() const;
00190     const TValue averageDepth() const;
00191 
00192     void swap(QuadTree& other);
00193     const TObjectIterator end() const;
00194 
00195 private:
00196     enum { subNodeCount = 1 << dimension };
00197     typedef std::vector<TObjectIterator> TObjectIterators;
00198     struct QuadNode
00199     {
00200         QuadNode *node[subNodeCount];   /**< 0 = NW, 1 = NE, 2 = SE, 3 = SW for quadtrees*/
00201         TPoint center;                  /**< center of quadnode */
00202         TVector extents;                /**< x = half of widht, y = half of height */
00203         TObjectIterators data;          /**< the list containing the data */
00204         bool leaf;                      /**< true for leaf nodes */
00205 
00206         QuadNode(const TPoint& aCenter, const TVector& aExtents);
00207         ~QuadNode();
00208 
00209         void add(TObjectIterator object, size_t maxSize, size_t maxLevel, size_t level = 0, bool mayDecompose = true);
00210 
00211         size_t objectCount() const;            /**< number of objects in this node and all its children */
00212         size_t depth() const;                   /**< depth of the child tree */
00213         const TValue averageDepth() const;
00214         void decompose(size_t maxSize, size_t maxLevel, size_t level = 0);  /**< split the current node into 4 children */
00215         void absorb();                      /**< absorb all children into the current node */
00216 
00217         TAabb aabb() const;
00218     };
00219     
00220     const TObjectIterator doIntersect(const QuadNode* node, const TRay& ray, TReference t, TParam tMin, 
00221         const TInfo* info, const TVector& tNear, const TVector& tFar, size_t flipMask) const;
00222     const bool doIntersects(const QuadNode* node, const TRay& ray, TParam tMin, TParam tMax, 
00223         const TInfo* info, const TVector& tNear, const TVector& tFar, size_t flipMask) const;
00224     void doNearestNeighbour(const QuadNode* node, const TPoint& point, const TInfo* info,
00225         Neighbour& best) const;
00226     template <typename RandomIterator>
00227     RandomIterator doRangeSearch(const QuadNode* node, const TPoint& target, TReference squaredRadius, 
00228         size_t maxCount, RandomIterator first, RandomIterator last, const TInfo* info) const;
00229 
00230     TAabb aabb_;
00231     QuadNode*   root_;
00232     TObjectIterator end_;
00233     size_t maxSize_;
00234     size_t maxLevel_;
00235 };
00236 
00237 
00238 
00239 }
00240 }
00241 
00242 #include "quad_tree.inl"
00243 
00244 #endif
00245 
00246 // EOF

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