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 #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
00061
00062
00063
00064
00065
00066
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
00099
00100 int64 xBits;
00101 };
00102
00103
00104
00105
00106
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
00120 Coordinate *pt;
00121 int level;
00122
00123 Envelope *env;
00124 void computeKey(int level,Envelope *itemEnv);
00125 };
00126
00127 class QuadTreeNode;
00128
00129
00130
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
00161
00162
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
00187
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
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 class Quadtree: public SpatialIndex {
00224 public:
00225
00226
00227
00228
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