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 const bool ptsCurved;
68
69 // Methods
70
86 static void initLabel(OverlayLabel& lbl, uint8_t geomIndex, int dim, int depthDelta, bool p_isHole);
87
88 static int labelDim(int dim, int depthDelta)
89 {
90 if (dim == geom::Dimension::False)
91 return OverlayLabel::DIM_NOT_PART;
92
93 if (dim == geom::Dimension::L)
94 return OverlayLabel::DIM_LINE;
95
96 // assert: dim is A
97 bool isCollapse = (depthDelta == 0);
98 if (isCollapse)
99 return OverlayLabel::DIM_COLLAPSE;
100
101 return OverlayLabel::DIM_BOUNDARY;
102 };
103
104 bool isHole(int index) const
105 {
106 if (index == 0)
107 return aIsHole;
108 return bIsHole;
109 };
110
111 bool isBoundary(int geomIndex) const
112 {
113 if (geomIndex == 0)
114 return aDim == OverlayLabel::DIM_BOUNDARY;
115 return bDim == OverlayLabel::DIM_BOUNDARY;
116 };
117
122 bool isShell(int geomIndex) const
123 {
124 if (geomIndex == 0) {
125 return (aDim == OverlayLabel::DIM_BOUNDARY && ! aIsHole);
126 }
127 return (bDim == OverlayLabel::DIM_BOUNDARY && ! bIsHole);
128 };
129
130 static Location locationRight(int depthDelta)
131 {
132 int sgn = delSign(depthDelta);
133 switch (sgn) {
134 case 0: return Location::NONE;
135 case 1: return Location::INTERIOR;
136 case -1: return Location::EXTERIOR;
137 }
138 return Location::NONE;
139 };
140
141 static Location locationLeft(int depthDelta)
142 {
143 // TODO: is it always safe to ignore larger depth deltas?
144 int sgn = delSign(depthDelta);
145 switch (sgn) {
146 case 0: return Location::NONE;
147 case 1: return Location::EXTERIOR;
148 case -1: return Location::INTERIOR;
149 }
150 return Location::NONE;
151 };
152
153 static int delSign(int depthDel)
154 {
155 if (depthDel > 0) return 1;
156 if (depthDel < 0) return -1;
157 return 0;
158 };
159
160 void copyInfo(const EdgeSourceInfo* info)
161 {
162 if (info->getIndex() == 0) {
163 aDim = info->getDimension();
164 aIsHole = info->isHole();
165 aDepthDelta = info->getDepthDelta();
166 }
167 else {
168 bDim = info->getDimension();
169 bIsHole = info->isHole();
170 bDepthDelta = info->getDepthDelta();
171 }
172 };
173
174 static bool isHoleMerged(int geomIndex, const Edge* edge1, const Edge* edge2)
175 {
176 // TOD: this might be clearer with tri-state logic for isHole?
177 bool isShell1 = edge1->isShell(geomIndex);
178 bool isShell2 = edge2->isShell(geomIndex);
179 bool isShellMerged = isShell1 || isShell2;
180 // flip since isHole is stored
181 return !isShellMerged;
182 };
183
184
185public:
186
187 Edge(const std::shared_ptr<const geom::CoordinateSequence>& p_pts, const EdgeSourceInfo* info, bool isCurved);
188
189 friend std::ostream& operator<<(std::ostream& os, const Edge& e);
190
191 static bool isCollapsed(const geom::CoordinateSequence* pts);
192
193 bool isCurved() const {
194 return ptsCurved;
195 }
196
197 // return a clone of the underlying points
198 std::unique_ptr<geom::CoordinateSequence> getCoordinates() const
199 {
200 return pts->clone();
201 };
202
203 // return a read-only pointer to the underlying points
204 const geom::CoordinateSequence* getCoordinatesRO() const
205 {
206 return pts.get();
207 };
208
209 // release the underlying points to the caller
210 std::shared_ptr<const geom::CoordinateSequence> releaseCoordinates()
211 {
212 auto cs = pts;
213 pts.reset();
214 return cs;
215 };
216
217 const geom::CoordinateXY& getCoordinate(std::size_t index) const
218 {
219 return pts->getAt(index);
220 };
221
222 std::size_t size() const
223 {
224 return pts->size();
225 };
226
227 bool direction() const
228 {
229 if (pts->size() < 2) {
230 throw util::GEOSException("Edge must have >= 2 points");
231 }
232
233 const geom::CoordinateXY& p0 = getCoordinate(0);
234 const geom::CoordinateXY& pn0 = getCoordinate(pts->size() - 1);
235
236 const int cmp0 = p0.compareTo(pn0);
237 if (cmp0 != 0) {
238 return cmp0 == -1;
239 }
240
241 const geom::CoordinateXY& p1 = getCoordinate(1);
242 const geom::CoordinateXY& pn1 = getCoordinate(pts->size() - 2);
243 const int cmp1 = p1.compareTo(pn1);
244 if (cmp1 != 0) {
245 return cmp1 == -1;
246 }
247
248 throw util::GEOSException("Edge direction cannot be determined because endpoints are equal");
249 };
250
255 bool relativeDirection(const Edge* edge2) const
256 {
257 // assert: the edges match (have the same coordinates up to direction)
258 if (!getCoordinate(0).equals2D(edge2->getCoordinate(0))) {
259 return false;
260 }
261 if (!getCoordinate(1).equals2D(edge2->getCoordinate(1))) {
262 return false;
263 }
264 return true;
265 };
266
267 int dimension(int geomIndex) const
268 {
269 if (geomIndex == 0) return aDim;
270 return bDim;
271 };
272
277 void merge(const Edge* edge)
278 {
284 aIsHole = isHoleMerged(0, this, edge);
285 bIsHole = isHoleMerged(1, this, edge);
286
287 if (edge->aDim > aDim) aDim = edge->aDim;
288 if (edge->bDim > bDim) bDim = edge->bDim;
289
290 bool relDir = relativeDirection(edge);
291 int flipFactor = relDir ? 1 : -1;
292 aDepthDelta += flipFactor * edge->aDepthDelta;
293 bDepthDelta += flipFactor * edge->bDepthDelta;
294 };
295
296 void populateLabel(OverlayLabel &lbl) const
297 {
298 initLabel(lbl, 0, aDim, aDepthDelta, aIsHole);
299 initLabel(lbl, 1, bDim, bDepthDelta, bIsHole);
300 };
301
302 bool compareTo(const Edge& e) const
303 {
304 const geom::CoordinateXY& ca = getCoordinate(0);
305 const geom::CoordinateXY& cb = e.getCoordinate(0);
306 if(ca.compareTo(cb) < 0) {
307 return true;
308 }
309 else if (ca.compareTo(cb) > 0) {
310 return false;
311 }
312 else {
313 const geom::CoordinateXY& cca = getCoordinate(1);
314 const geom::CoordinateXY& ccb = e.getCoordinate(1);
315 if(cca.compareTo(ccb) < 0) {
316 return true;
317 }
318 else if (cca.compareTo(ccb) > 0) {
319 return false;
320 }
321 else {
322 return false;
323 }
324 }
325 }
326
327};
328
329bool EdgeComparator(const Edge* a, const Edge* b);
330
331
332
333} // namespace geos.operation.overlayng
334} // namespace geos.operation
335} // namespace geos
336
The internal representation of a list of coordinates inside a Geometry.
Definition CoordinateSequence.h:56
Definition EdgeSourceInfo.h:38
Definition operation/overlayng/Edge.h:54
bool relativeDirection(const Edge *edge2) const
Definition operation/overlayng/Edge.h:255
void merge(const Edge *edge)
Definition operation/overlayng/Edge.h:277
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:256
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