/src/geos/src/precision/CommonBitsRemover.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2005-2006 Refractions Research Inc. |
7 | | * Copyright (C) 2001-2002 Vivid Solutions Inc. |
8 | | * |
9 | | * This is free software; you can redistribute and/or modify it under |
10 | | * the terms of the GNU Lesser General Public Licence as published |
11 | | * by the Free Software Foundation. |
12 | | * See the COPYING file for more information. |
13 | | * |
14 | | **********************************************************************/ |
15 | | |
16 | | #include <geos/precision/CommonBitsRemover.h> |
17 | | #include <geos/precision/CommonBits.h> |
18 | | // for CommonCoordinateFilter inheritance |
19 | | #include <geos/geom/CoordinateFilter.h> |
20 | | #include <geos/geom/Coordinate.h> |
21 | | #include <geos/geom/Geometry.h> |
22 | | #include <geos/util.h> |
23 | | |
24 | | #include <cassert> |
25 | | |
26 | | #ifndef GEOS_DEBUG |
27 | | #define GEOS_DEBUG 0 |
28 | | #endif |
29 | | |
30 | | #if GEOS_DEBUG |
31 | | #include <iostream> |
32 | | #endif |
33 | | |
34 | | using namespace geos::geom; |
35 | | |
36 | | namespace geos { |
37 | | namespace precision { // geos.precision |
38 | | |
39 | | class Translater: public geom::CoordinateFilter { |
40 | | |
41 | | private: |
42 | | |
43 | | geom::Coordinate trans; |
44 | | |
45 | | public: |
46 | | |
47 | | Translater(geom::Coordinate& newTrans) |
48 | | : |
49 | 0 | trans(newTrans) |
50 | 0 | {} |
51 | | |
52 | | void |
53 | | filter_ro(const geom::Coordinate* coord) override //Not used |
54 | 0 | { |
55 | 0 | ::geos::ignore_unused_variable_warning(coord); |
56 | 0 | assert(0); |
57 | 0 | } |
58 | | |
59 | | void |
60 | | filter_rw(geom::Coordinate* coord) const override |
61 | 0 | { |
62 | 0 | coord->x += trans.x; |
63 | 0 | coord->y += trans.y; |
64 | 0 | } |
65 | | }; |
66 | | |
67 | | |
68 | | class CommonCoordinateFilter: public geom::CoordinateFilter { |
69 | | private: |
70 | | CommonBits commonBitsX; |
71 | | CommonBits commonBitsY; |
72 | | public: |
73 | | |
74 | | void |
75 | | filter_rw(geom::Coordinate* coord) const override |
76 | 0 | { |
77 | | // CommonCoordinateFilter is a read-only filter |
78 | 0 | ::geos::ignore_unused_variable_warning(coord); |
79 | 0 | assert(0); |
80 | 0 | } |
81 | | |
82 | | void |
83 | | filter_ro(const geom::Coordinate* coord) override |
84 | 0 | { |
85 | 0 | commonBitsX.add(coord->x); |
86 | 0 | commonBitsY.add(coord->y); |
87 | 0 | } |
88 | | |
89 | | void |
90 | | getCommonCoordinate(geom::Coordinate& c) |
91 | 0 | { |
92 | 0 | c = Coordinate(commonBitsX.getCommon(), |
93 | 0 | commonBitsY.getCommon()); |
94 | 0 | } |
95 | | |
96 | | }; |
97 | | |
98 | | |
99 | | CommonBitsRemover::CommonBitsRemover() |
100 | 0 | { |
101 | 0 | ccFilter = new CommonCoordinateFilter(); |
102 | 0 | } |
103 | | |
104 | | CommonBitsRemover::~CommonBitsRemover() |
105 | 0 | { |
106 | 0 | delete ccFilter; |
107 | 0 | } |
108 | | |
109 | | /** |
110 | | * Add a geometry to the set of geometries whose common bits are |
111 | | * being computed. After this method has executed the |
112 | | * common coordinate reflects the common bits of all added |
113 | | * geometries. |
114 | | * |
115 | | * @param geom a Geometry to test for common bits |
116 | | */ |
117 | | void |
118 | | CommonBitsRemover::add(const Geometry* geom) |
119 | 0 | { |
120 | 0 | geom->apply_ro(ccFilter); |
121 | 0 | ccFilter->getCommonCoordinate(commonCoord); |
122 | 0 | } |
123 | | |
124 | | /** |
125 | | * The common bits of the Coordinates in the supplied Geometries. |
126 | | */ |
127 | | Coordinate& |
128 | | CommonBitsRemover::getCommonCoordinate() |
129 | 0 | { |
130 | 0 | return commonCoord; |
131 | 0 | } |
132 | | |
133 | | /** |
134 | | * Removes the common coordinate bits from a Geometry. |
135 | | * The coordinates of the Geometry are changed. |
136 | | * |
137 | | * @param geom the Geometry from which to remove the common coordinate bits |
138 | | * @return the shifted Geometry |
139 | | */ |
140 | | void |
141 | | CommonBitsRemover::removeCommonBits(Geometry* geom) |
142 | 0 | { |
143 | 0 | if(commonCoord.x == 0.0 && commonCoord.y == 0.0) { |
144 | 0 | return; |
145 | 0 | } |
146 | | |
147 | 0 | Coordinate invCoord(commonCoord); |
148 | 0 | invCoord.x = -invCoord.x; |
149 | 0 | invCoord.y = -invCoord.y; |
150 | |
|
151 | 0 | Translater trans(invCoord); |
152 | 0 | geom->apply_rw(&trans); |
153 | 0 | geom->geometryChanged(); |
154 | |
|
155 | | #if GEOS_DEBUG |
156 | | std::cerr << "CommonBits removed: " << *geom << std::endl; |
157 | | #endif |
158 | 0 | } |
159 | | |
160 | | /** |
161 | | * Adds the common coordinate bits back into a Geometry. |
162 | | * The coordinates of the Geometry are changed. |
163 | | * |
164 | | * @param geom the Geometry to which to add the common coordinate bits |
165 | | * @return the shifted Geometry |
166 | | */ |
167 | | Geometry* |
168 | | CommonBitsRemover::addCommonBits(Geometry* geom) |
169 | 0 | { |
170 | | #if GEOS_DEBUG |
171 | | std::cerr << "CommonBits before add: " << *geom << std::endl; |
172 | | #endif |
173 | |
|
174 | 0 | Translater trans(commonCoord); |
175 | |
|
176 | 0 | geom->apply_rw(&trans); |
177 | 0 | geom->geometryChanged(); |
178 | |
|
179 | | #if GEOS_DEBUG |
180 | | std::cerr << "CommonBits added: " << *geom << std::endl; |
181 | | #endif |
182 | |
|
183 | 0 | return geom; |
184 | 0 | } |
185 | | |
186 | | } // namespace geos.precision |
187 | | } // namespace geos |
188 | | |