/src/geos/src/index/quadtree/Key.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2009 Sandro Santilli <strk@kbt.io> |
7 | | * Copyright (C) 2006 Refractions Research Inc. |
8 | | * Copyright (C) 2001-2002 Vivid Solutions Inc. |
9 | | * |
10 | | * This is free software; you can redistribute and/or modify it under |
11 | | * the terms of the GNU Lesser General Public Licence as published |
12 | | * by the Free Software Foundation. |
13 | | * See the COPYING file for more information. |
14 | | * |
15 | | ********************************************************************** |
16 | | * |
17 | | * Last port: index/quadtree/Key.java rev 1.8 (JTS-1.10) |
18 | | * |
19 | | **********************************************************************/ |
20 | | |
21 | | #include <geos/index/quadtree/Key.h> |
22 | | #include <geos/geom/Envelope.h> |
23 | | #include <geos/geom/Coordinate.h> |
24 | | |
25 | | #include <cmath> |
26 | | |
27 | | #ifndef GEOS_DEBUG |
28 | | #define GEOS_DEBUG 0 |
29 | | #endif |
30 | | |
31 | | #if GEOS_DEBUG |
32 | | #include <iostream> |
33 | | #endif |
34 | | |
35 | | using namespace geos::geom; |
36 | | |
37 | | namespace geos { |
38 | | namespace index { // geos.index |
39 | | namespace quadtree { // geos.index.quadtree |
40 | | |
41 | | /* static public */ |
42 | | int |
43 | | Key::computeQuadLevel(const Envelope& env) |
44 | 0 | { |
45 | 0 | double dx = env.getWidth(); |
46 | 0 | double dy = env.getHeight(); |
47 | 0 | double dMax = dx > dy ? dx : dy; |
48 | 0 | int level; |
49 | 0 | frexp(dMax, &level); |
50 | | #if GEOS_DEBUG |
51 | | std::cerr << "Maxdelta:" << dMax << " exponent:" << (level - 1) << std::endl; |
52 | | #endif |
53 | 0 | return level; |
54 | 0 | } |
55 | | |
56 | | Key::Key(const Envelope& itemEnv) |
57 | | : |
58 | 0 | pt(), |
59 | 0 | level(0), |
60 | 0 | env() |
61 | 0 | { |
62 | 0 | computeKey(itemEnv); |
63 | 0 | } |
64 | | |
65 | | const Coordinate& |
66 | | Key::getPoint() const |
67 | 0 | { |
68 | 0 | return pt; |
69 | 0 | } |
70 | | |
71 | | int |
72 | | Key::getLevel() const |
73 | 0 | { |
74 | 0 | return level; |
75 | 0 | } |
76 | | |
77 | | const Envelope& |
78 | | Key::getEnvelope() const |
79 | 0 | { |
80 | 0 | return env; |
81 | 0 | } |
82 | | |
83 | | Coordinate* |
84 | | Key::getCentre() const |
85 | 0 | { |
86 | 0 | return new Coordinate( |
87 | 0 | (env.getMinX() + env.getMaxX()) / 2, |
88 | 0 | (env.getMinY() + env.getMaxY()) / 2 |
89 | 0 | ); |
90 | 0 | } |
91 | | |
92 | | /*public*/ |
93 | | void |
94 | | Key::computeKey(const Envelope& itemEnv) |
95 | 0 | { |
96 | 0 | level = computeQuadLevel(itemEnv); |
97 | 0 | env.init(); // reset to null |
98 | 0 | computeKey(level, itemEnv); |
99 | | // MD - would be nice to have a non-iterative form of this algorithm |
100 | 0 | while(!env.contains(itemEnv)) { |
101 | 0 | level += 1; |
102 | 0 | computeKey(level, itemEnv); |
103 | 0 | } |
104 | | #if GEOS_DEBUG |
105 | | std::cerr << "Key::computeKey:" << std::endl; |
106 | | std::cerr << " itemEnv: " << itemEnv.toString() << std::endl; |
107 | | std::cerr << " keyEnv: " << env.toString() << std::endl; |
108 | | std::cerr << " keyLvl: " << level << std::endl; |
109 | | |
110 | | #endif |
111 | 0 | } |
112 | | |
113 | | void |
114 | | Key::computeKey(int p_level, const Envelope& itemEnv) |
115 | 0 | { |
116 | 0 | double quadSize = exp2(p_level); |
117 | 0 | pt.x = std::floor(itemEnv.getMinX() / quadSize) * quadSize; |
118 | 0 | pt.y = std::floor(itemEnv.getMinY() / quadSize) * quadSize; |
119 | 0 | env.init(pt.x, pt.x + quadSize, pt.y, pt.y + quadSize); |
120 | 0 | } |
121 | | |
122 | | } // namespace geos.index.quadtree |
123 | | } // namespace geos.index |
124 | | } // namespace geos |