GEOS 3.15.0dev
operation/overlayng/Edge.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************/
14
15#pragma once
16
17#include <geos/geom/Coordinate.h>
18#include <geos/geom/CoordinateSequence.h>
19#include <geos/geom/Dimension.h>
20#include <geos/operation/overlayng/OverlayLabel.h>
21#include <geos/operation/overlayng/EdgeSourceInfo.h>
22#include <geos/util/GEOSException.h>
23#include <geos/export.h>
24
25
26#include <memory>
27
28
29namespace geos { // geos.
30namespace operation { // geos.operation
31namespace overlayng { // geos.operation.overlayng
32
54class GEOS_DLL Edge {
56
57private:
58
59 // Members
60 int aDim = OverlayLabel::DIM_UNKNOWN;
61 int aDepthDelta = 0;
62 bool aIsHole = false;
63 int bDim = OverlayLabel::DIM_UNKNOWN;
64 int bDepthDelta = 0;
65 bool bIsHole = false;
66 std::shared_ptr<const geom::CoordinateSequence> pts;
67
68 // Methods
69
85 static void initLabel(OverlayLabel& lbl, uint8_t geomIndex, int dim, int depthDelta, bool p_isHole);
86
87 static int labelDim(int dim, int depthDelta)
88 {
89 if (dim == geom::Dimension::False)
90 return OverlayLabel::DIM_NOT_PART;
91
92 if (dim == geom::Dimension::L)
93 return OverlayLabel::DIM_LINE;
94
95 // assert: dim is A
96 bool isCollapse = (depthDelta == 0);
97 if (isCollapse)
98 return OverlayLabel::DIM_COLLAPSE;
99
100 return OverlayLabel::DIM_BOUNDARY;
101 };
102
103 bool isHole(int index) const
104 {
105 if (index == 0)
106 return aIsHole;
107 return bIsHole;
108 };
109
110 bool isBoundary(int geomIndex) const
111 {
112 if (geomIndex == 0)
113 return aDim == OverlayLabel::DIM_BOUNDARY;
114 return bDim == OverlayLabel::DIM_BOUNDARY;
115 };
116
121 bool isShell(int geomIndex) const
122 {
123 if (geomIndex == 0) {
124 return (aDim == OverlayLabel::DIM_BOUNDARY && ! aIsHole);
125 }
126 return (bDim == OverlayLabel::DIM_BOUNDARY && ! bIsHole);
127 };
128
129 static Location locationRight(int depthDelta)
130 {
131 int sgn = delSign(depthDelta);
132 switch (sgn) {
133 case 0: return Location::NONE;
134 case 1: return Location::INTERIOR;
135 case -1: return Location::EXTERIOR;
136 }
137 return Location::NONE;
138 };
139
140 static Location locationLeft(int depthDelta)
141 {
142 // TODO: is it always safe to ignore larger depth deltas?
143 int sgn = delSign(depthDelta);
144 switch (sgn) {
145 case 0: return Location::NONE;
146 case 1: return Location::EXTERIOR;
147 case -1: return Location::INTERIOR;
148 }
149 return Location::NONE;
150 };
151
152 static int delSign(int depthDel)
153 {
154 if (depthDel > 0) return 1;
155 if (depthDel < 0) return -1;
156 return 0;
157 };
158
159 void copyInfo(const EdgeSourceInfo* info)
160 {
161 if (info->getIndex() == 0) {
162 aDim = info->getDimension();
163 aIsHole = info->isHole();
164 aDepthDelta = info->getDepthDelta();
165 }
166 else {
167 bDim = info->getDimension();
168 bIsHole = info->isHole();
169 bDepthDelta = info->getDepthDelta();
170 }
171 };
172
173 static bool isHoleMerged(int geomIndex, const Edge* edge1, const Edge* edge2)
174 {
175 // TOD: this might be clearer with tri-state logic for isHole?
176 bool isShell1 = edge1->isShell(geomIndex);
177 bool isShell2 = edge2->isShell(geomIndex);
178 bool isShellMerged = isShell1 || isShell2;
179 // flip since isHole is stored
180 return !isShellMerged;
181 };
182
183
184public:
185
186 Edge()
187 : aDim(OverlayLabel::DIM_UNKNOWN)
188 , aDepthDelta(0)
189 , aIsHole(false)
190 , bDim(OverlayLabel::DIM_UNKNOWN)
191 , bDepthDelta(0)
192 , bIsHole(false)
193 , pts(nullptr)
194 {};
195
196 friend std::ostream& operator<<(std::ostream& os, const Edge& e);
197
198 static bool isCollapsed(const geom::CoordinateSequence* pts);
199
200 Edge(const std::shared_ptr<const geom::CoordinateSequence>& p_pts, const EdgeSourceInfo* info);
201
202 // return a clone of the underlying points
203 std::unique_ptr<geom::CoordinateSequence> getCoordinates()
204 {
205 return pts->clone();
206 };
207
208 // return a read-only pointer to the underlying points
209 const geom::CoordinateSequence* getCoordinatesRO() const
210 {
211 return pts.get();
212 };
213
214 // release the underlying points to the caller
215 std::shared_ptr<const geom::CoordinateSequence> releaseCoordinates()
216 {
217 auto cs = pts;
218 pts.reset();
219 return cs;
220 };
221
222 const geom::CoordinateXY& getCoordinate(std::size_t index) const
223 {
224 return pts->getAt(index);
225 };
226
227 std::size_t size() const
228 {
229 return pts->size();
230 };
231
232 bool direction() const
233 {
234 if (pts->size() < 2) {
235 throw util::GEOSException("Edge must have >= 2 points");
236 }
237
238 const geom::Coordinate& p0 = pts->getAt(0);
239 const geom::Coordinate& p1 = pts->getAt(1);
240 const geom::Coordinate& pn0 = pts->getAt(pts->size() - 1);
241 const geom::Coordinate& pn1 = pts->getAt(pts->size() - 2);
242
243 int cmp = 0;
244 int cmp0 = p0.compareTo(pn0);
245 if (cmp0 != 0) cmp = cmp0;
246
247 if (cmp == 0) {
248 int cmp1 = p1.compareTo(pn1);
249 if (cmp1 != 0) cmp = cmp1;
250 }
251
252 if (cmp == 0) {
253 throw util::GEOSException("Edge direction cannot be determined because endpoints are equal");
254 }
255
256 return cmp == -1;
257 };
258
263 bool relativeDirection(const Edge* edge2) const
264 {
265 // assert: the edges match (have the same coordinates up to direction)
266 if (!getCoordinate(0).equals2D(edge2->getCoordinate(0))) {
267 return false;
268 }
269 if (!getCoordinate(1).equals2D(edge2->getCoordinate(1))) {
270 return false;
271 }
272 return true;
273 };
274
275 int dimension(int geomIndex) const
276 {
277 if (geomIndex == 0) return aDim;
278 return bDim;
279 };
280
285 void merge(const Edge* edge)
286 {
292 aIsHole = isHoleMerged(0, this, edge);
293 bIsHole = isHoleMerged(1, this, edge);
294
295 if (edge->aDim > aDim) aDim = edge->aDim;
296 if (edge->bDim > bDim) bDim = edge->bDim;
297
298 bool relDir = relativeDirection(edge);
299 int flipFactor = relDir ? 1 : -1;
300 aDepthDelta += flipFactor * edge->aDepthDelta;
301 bDepthDelta += flipFactor * edge->bDepthDelta;
302 };
303
304 void populateLabel(OverlayLabel &lbl) const
305 {
306 initLabel(lbl, 0, aDim, aDepthDelta, aIsHole);
307 initLabel(lbl, 1, bDim, bDepthDelta, bIsHole);
308 };
309
310 bool compareTo(const Edge& e) const
311 {
312 const geom::CoordinateXY& ca = getCoordinate(0);
313 const geom::CoordinateXY& cb = e.getCoordinate(0);
314 if(ca.compareTo(cb) < 0) {
315 return true;
316 }
317 else if (ca.compareTo(cb) > 0) {
318 return false;
319 }
320 else {
321 const geom::CoordinateXY& cca = getCoordinate(1);
322 const geom::CoordinateXY& ccb = e.getCoordinate(1);
323 if(cca.compareTo(ccb) < 0) {
324 return true;
325 }
326 else if (cca.compareTo(ccb) > 0) {
327 return false;
328 }
329 else {
330 return false;
331 }
332 }
333 }
334
335};
336
337bool EdgeComparator(const Edge* a, const Edge* b);
338
339
340
341} // namespace geos.operation.overlayng
342} // namespace geos.operation
343} // namespace geos
344
The internal representation of a list of coordinates inside a Geometry.
Definition CoordinateSequence.h:56
Coordinate is the lightweight class used to store coordinates.
Definition Coordinate.h:220
Definition EdgeSourceInfo.h:38
Definition operation/overlayng/Edge.h:54
bool relativeDirection(const Edge *edge2) const
Definition operation/overlayng/Edge.h:263
void merge(const Edge *edge)
Definition operation/overlayng/Edge.h:285
Definition OverlayLabel.h:86
Base class for all GEOS exceptions.
Definition GEOSException.h:37
const T & getAt(std::size_t i) const
Returns a read-only reference to Coordinate at position i.
Definition CoordinateSequence.h:249
Location
Constants representing the location of a point relative to a geometry.
Definition Location.h:32
Basic namespace for all GEOS functionalities.
Definition geos.h:38