Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

indexQuadtree.h

00001 /**********************************************************************
00002  * $Id: indexQuadtree.h,v 1.3 2004/07/27 16:35:46 strk Exp $
00003  *
00004  * GEOS - Geometry Engine Open Source
00005  * http://geos.refractions.net
00006  *
00007  * Copyright (C) 2001-2002 Vivid Solutions Inc.
00008  *
00009  * This is free software; you can redistribute and/or modify it under
00010  * the terms of the GNU Lesser General Public Licence as published
00011  * by the Free Software Foundation. 
00012  * See the COPYING file for more information.
00013  *
00014  **********************************************************************
00015  * $Log: indexQuadtree.h,v $
00016  * Revision 1.3  2004/07/27 16:35:46  strk
00017  * Geometry::getEnvelopeInternal() changed to return a const Envelope *.
00018  * This should reduce object copies as once computed the envelope of a
00019  * geometry remains the same.
00020  *
00021  * Revision 1.2  2004/07/19 13:19:31  strk
00022  * Documentation fixes
00023  *
00024  * Revision 1.1  2004/07/02 13:20:42  strk
00025  * Header files moved under geos/ dir.
00026  *
00027  * Revision 1.16  2004/05/06 16:30:58  strk
00028  * Kept track of newly allocated objects by ensureExtent for Bintree and Quadtree,
00029  * deleted at destruction time. doc/example.cpp runs with no leaks.
00030  *
00031  * Revision 1.15  2004/04/19 15:14:45  strk
00032  * Added missing virtual destructor in SpatialIndex class.
00033  * Memory leaks fixes. Const and throw specifications added.
00034  *
00035  * Revision 1.14  2004/03/25 02:23:55  ybychkov
00036  * All "index/*" packages upgraded to JTS 1.4
00037  *
00038  * Revision 1.13  2003/11/07 01:23:42  pramsey
00039  * Add standard CVS headers licence notices and copyrights to all cpp and h
00040  * files.
00041  *
00042  *
00043  **********************************************************************/
00044 
00045 
00046 #ifndef GEOS_INDEXQUADTREE_H
00047 #define GEOS_INDEXQUADTREE_H
00048 
00049 #include <memory>
00050 #include <vector>
00051 #include <geos/platform.h>
00052 #include <geos/geom.h>
00053 #include <geos/spatialIndex.h>
00054 
00055 using namespace std;
00056 
00057 namespace geos {
00058 
00059 /*
00060 * Provides a test for whether an interval is
00061 * so small it should be considered as zero for the purposes of
00062 * inserting it into a binary tree.
00063 * The reason this check is necessary is that round-off error can
00064 * cause the algorithm used to subdivide an interval to fail, by
00065 * computing a midpoint value which does not lie strictly between the
00066 * endpoints.
00067 */
00068 class IntervalSize {
00069 public:
00076         static const int MIN_BINARY_EXPONENT=-50;
00077         static bool isZeroWidth(double min, double max);
00078 };
00079 
00080 class DoubleBits {
00081 public:
00082         static const int EXPONENT_BIAS=1023;
00083         static double powerOf2(int exp);
00084         static int exponent(double d);
00085         static double truncateToPowerOfTwo(double d);
00086         static string toBinaryString(double d);
00087         static double maximumCommonMantissa(double d1, double d2);
00088         DoubleBits(double nx);
00089         double getDouble();
00090         int64 biasedExponent();
00091         int getExponent();
00092         void zeroLowerBits(int nBits);
00093         int getBit(int i);
00094         int numCommonMantissaBits(DoubleBits *db);
00095         string toString();
00096 private:
00097         double x;
00098 //      long long xBits;
00099 //      long xBits;
00100         int64 xBits;
00101 };
00102 
00103 /*
00104  * A QuadTreeKey is a unique identifier for a node in a quadtree.
00105  * It contains a lower-left point and a level number. The level number
00106  * is the power of two for the size of the node envelope
00107  */
00108 class QuadTreeKey {
00109 public:
00110         static int computeQuadLevel(Envelope *env);
00111         QuadTreeKey(Envelope *itemEnv);
00112         virtual ~QuadTreeKey();
00113         Coordinate* getPoint();
00114         int getLevel();
00115         Envelope* getEnvelope();
00116         Coordinate* getCentre();
00117         void computeKey(Envelope *itemEnv);
00118 private:        
00119         // the fields which make up the key
00120         Coordinate *pt;
00121         int level;
00122         // auxiliary data which is derived from the key for use in computation
00123         Envelope *env;
00124         void computeKey(int level,Envelope *itemEnv);
00125 };
00126 
00127 class QuadTreeNode;
00128 
00129 /*
00130  * The base class for nodes in a {@link Quadtree}.
00131  *
00132  */
00133 class QuadTreeNodeBase {
00134 public:
00135         static int getSubnodeIndex(const Envelope *env, const Coordinate *centre);
00136         QuadTreeNodeBase();
00137         virtual ~QuadTreeNodeBase();
00138         virtual vector<void*>* getItems();
00139         virtual void add(void* item);
00140         virtual vector<void*>* addAllItems(vector<void*> *resultItems);
00141         virtual void addAllItemsFromOverlapping(const Envelope *searchEnv,vector<void*> *resultItems);
00142         virtual int depth();
00143         virtual int size();
00144         virtual int nodeCount();
00145 protected:
00146         vector<void*> *items;
00155         QuadTreeNode* subnode[4];
00156         virtual bool isSearchMatch(const Envelope *searchEnv)=0;
00157 };
00158 
00159 /*
00160  * Represents a node of a {@link Quadtree}.  Nodes contain
00161  * items which have a spatial extent corresponding to the node's position
00162  * in the quadtree.
00163  *
00164  */
00165 class QuadTreeNode: public QuadTreeNodeBase {
00166 public:
00167         static QuadTreeNode* createNode(Envelope *env);
00168         static QuadTreeNode* createExpanded(QuadTreeNode *node, const Envelope *addEnv);
00169         QuadTreeNode(Envelope *nenv,int nlevel);
00170         virtual ~QuadTreeNode();
00171         Envelope* getEnvelope();
00172         QuadTreeNode* getNode(const Envelope *searchEnv);
00173         QuadTreeNodeBase* find(const Envelope *searchEnv);
00174         void insertNode(QuadTreeNode *node);
00175 private:
00176         Envelope *env;
00177         Coordinate *centre;
00178         int level;
00179         QuadTreeNode* getSubnode(int index);
00180         QuadTreeNode* createSubnode(int index);
00181 protected:
00182         bool isSearchMatch(const Envelope *searchEnv);
00183 };
00184 
00185 /*
00186  * QuadRoot is the root of a single Quadtree.  It is centred at the origin,
00187  * and does not have a defined extent.
00188  */
00189 class QuadTreeRoot: public QuadTreeNodeBase {
00190 friend class Unload;
00191 private:
00192         static Coordinate *origin;
00193         void insertContained(QuadTreeNode *tree, const Envelope *itemEnv, void* item);
00194 public:
00195         QuadTreeRoot();
00196         virtual ~QuadTreeRoot();
00197         void insert(const Envelope *itemEnv,void* item);
00198 protected:
00199         bool isSearchMatch(const Envelope *searchEnv);
00200 };
00201 
00202 /*
00203  * A Quadtree is a spatial index structure for efficient querying
00204  * of 2D rectangles.  If other kinds of spatial objects
00205  * need to be indexed they can be represented by their
00206  * envelopes
00207  * 
00208  * The quadtree structure is used to provide a primary filter
00209  * for range rectangle queries.  The query() method returns a list of
00210  * all objects which <i>may</i> intersect the query rectangle.  Note that
00211  * it may return objects which do not in fact intersect.
00212  * A secondary filter is required to test for exact intersection.
00213  * Of course, this secondary filter may consist of other tests besides
00214  * intersection, such as testing other kinds of spatial relationships.
00215  *
00216  * This implementation does not require specifying the extent of the inserted
00217  * items beforehand.  It will automatically expand to accomodate any extent
00218  * of dataset.
00219  * 
00220  * This data structure is also known as an <i>MX-CIF quadtree</i>
00221  * following the usage of Samet and others.
00222  */
00223 class Quadtree: public SpatialIndex {
00224 public:
00225         /*
00226          * Ensure that the envelope for the inserted item has non-zero extents.
00227          * Use the current minExtent to pad the envelope, if necessary.
00228          * Can return a new Envelope or the given one (casted to non-const).
00229          */
00230         static Envelope* ensureExtent(const Envelope *itemEnv, double minExtent);
00234         Quadtree();
00235         virtual ~Quadtree();
00239         int depth();
00243         int size();
00244         
00245         void insert(const Envelope *itemEnv, void *item);
00246 
00247         vector<void*>* query(const Envelope *searchEnv);
00248         vector<void*>* queryAll();
00249 private:
00250         vector<Envelope *>newEnvelopes;
00251         void collectStats(const Envelope *itemEnv);
00252         QuadTreeRoot *root;
00263         double minExtent;
00264 };
00265 }
00266 #endif
00267 

Generated on Fri Nov 26 21:30:45 2004 for GEOS by  doxygen 1.3.9.1