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