-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathInteriorPointArea.cpp
248 lines (214 loc) · 6 KB
/
InteriorPointArea.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2013 Sandro Santilli <[email protected]>
* 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.
*
**********************************************************************
*
* Last port: algorithm/InteriorPointArea.java r728 (JTS-1.13+)
*
**********************************************************************/
#include <geos/algorithm/InteriorPointArea.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryCollection.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/LineString.h>
#include <geos/geom/Envelope.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <vector>
#include <typeinfo>
#include <memory> // for auto_ptr
using namespace std;
using namespace geos::geom;
namespace geos {
namespace algorithm { // geos.algorithm
// file-statics
namespace {
double avg(double a, double b){return (a+b)/2.0;}
/**
* Finds a safe bisector Y ordinate
* by projecting to the Y axis
* and finding the Y-ordinate interval
* which contains the centre of the Y extent.
* The centre of this interval is returned as the bisector Y-ordinate.
*
* @author mdavis
*
*/
class SafeBisectorFinder
{
public:
static double getBisectorY(const Polygon& poly)
{
SafeBisectorFinder finder(poly);
return finder.getBisectorY();
}
SafeBisectorFinder(const Polygon& nPoly)
: poly(nPoly)
{
// initialize using extremal values
hiY = poly.getEnvelopeInternal()->getMaxY();
loY = poly.getEnvelopeInternal()->getMinY();
centreY = avg(loY, hiY);
}
double getBisectorY()
{
process(*poly.getExteriorRing());
for (size_t i = 0; i < poly.getNumInteriorRing(); i++) {
process(*poly.getInteriorRingN(i));
}
double bisectY = avg(hiY, loY);
return bisectY;
}
private:
const Polygon& poly;
double centreY;
double hiY;
double loY;
void process(const LineString& line) {
const CoordinateSequence* seq = line.getCoordinatesRO();
for (std::size_t i = 0, s = seq->size(); i < s; i++) {
double y = seq->getY(i);
updateInterval(y);
}
}
void updateInterval(double y) {
if (y <= centreY) {
if (y > loY)
loY = y;
}
else if (y > centreY) {
if (y < hiY) {
hiY = y;
}
}
}
SafeBisectorFinder(SafeBisectorFinder const&); /*= delete*/
SafeBisectorFinder& operator=(SafeBisectorFinder const&); /*= delete*/
};
} // anonymous namespace
/*public*/
InteriorPointArea::InteriorPointArea(const Geometry *g)
{
foundInterior=false;
maxWidth=0.0;
factory=g->getFactory();
add(g);
}
/*public*/
InteriorPointArea::~InteriorPointArea()
{
}
/*public*/
bool
InteriorPointArea::getInteriorPoint(Coordinate& ret) const
{
if ( ! foundInterior ) return false;
ret=interiorPoint;
return true;
}
/*public*/
void
InteriorPointArea::add(const Geometry *geom)
{
const Polygon *poly = dynamic_cast<const Polygon*>(geom);
if ( poly ) {
addPolygon(geom);
return;
}
const GeometryCollection *gc = dynamic_cast<const GeometryCollection*>(geom);
if ( gc )
{
for(std::size_t i=0, n=gc->getNumGeometries(); i<n; i++) {
add(gc->getGeometryN(i));
}
}
}
/*private*/
void
InteriorPointArea::addPolygon(const Geometry *geometry)
{
if (geometry->isEmpty()) return;
Coordinate intPt;
double width;
auto_ptr<LineString> bisector ( horizontalBisector(geometry) );
if ( bisector->getLength() == 0.0 ) {
width = 0;
intPt = bisector->getCoordinateN(0);
}
else {
auto_ptr<Geometry> intersections ( bisector->intersection(geometry) );
const Geometry *widestIntersection = widestGeometry(intersections.get());
const Envelope *env = widestIntersection->getEnvelopeInternal();
width=env->getWidth();
env->centre(intPt);
}
if (!foundInterior || width>maxWidth) {
interiorPoint = intPt;
maxWidth = width;
foundInterior=true;
}
}
//@return if geometry is a collection, the widest sub-geometry; otherwise,
//the geometry itself
const Geometry*
InteriorPointArea::widestGeometry(const Geometry *geometry)
{
const GeometryCollection *gc = dynamic_cast<const GeometryCollection*>(geometry);
if ( gc ) {
return widestGeometry(gc);
} else {
return geometry;
}
}
const Geometry*
InteriorPointArea::widestGeometry(const GeometryCollection* gc) {
if (gc->isEmpty()) {
return gc;
}
const Geometry* widestGeometry=gc->getGeometryN(0);
// scan remaining geom components to see if any are wider
for(std::size_t i=1, n=gc->getNumGeometries(); i<n; i++) // start at 1
{
const Envelope *env1(gc->getGeometryN(i)->getEnvelopeInternal());
const Envelope *env2(widestGeometry->getEnvelopeInternal());
if (env1->getWidth()>env2->getWidth()) {
widestGeometry=gc->getGeometryN(i);
}
}
return widestGeometry;
}
/* private */
LineString*
InteriorPointArea::horizontalBisector(const Geometry *geometry)
{
const Envelope *envelope=geometry->getEnvelopeInternal();
/**
* Original algorithm. Fails when geometry contains a horizontal
* segment at the Y midpoint.
*/
// Assert: for areas, minx <> maxx
//double avgY=avg(envelope->getMinY(),envelope->getMaxY());
double bisectY = SafeBisectorFinder::getBisectorY(*dynamic_cast<const Polygon *>(geometry));
vector<Coordinate>*cv=new vector<Coordinate>(2);
(*cv)[0].x = envelope->getMinX();
(*cv)[0].y = bisectY;
(*cv)[1].x = envelope->getMaxX();
(*cv)[1].y = bisectY;
CoordinateSequence *cl = factory->getCoordinateSequenceFactory()->create(cv);
LineString *ret = factory->createLineString(cl);
return ret;
}
} // namespace geos.algorithm
} // namespace geos