GEOS 3.10.1
LineIntersector.h
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 * Last port: algorithm/RobustLineIntersector.java r785 (JTS-1.13+)
17 *
18 **********************************************************************/
19
20#ifndef GEOS_ALGORITHM_LINEINTERSECTOR_H
21#define GEOS_ALGORITHM_LINEINTERSECTOR_H
22
23#include <geos/inline.h>
24#include <geos/export.h>
25#include <string>
26
27#include <geos/geom/Coordinate.h>
28
29// Forward declarations
30namespace geos {
31namespace geom {
32class PrecisionModel;
33}
34}
35
36namespace geos {
37namespace algorithm { // geos::algorithm
38
50class GEOS_DLL LineIntersector {
51public:
52
56 static double interpolateZ(const geom::Coordinate& p, const geom::Coordinate& p0, const geom::Coordinate& p1);
57
58
77 static double computeEdgeDistance(const geom::Coordinate& p, const geom::Coordinate& p0, const geom::Coordinate& p1);
78
79 static double nonRobustComputeEdgeDistance(const geom::Coordinate& p, const geom::Coordinate& p1,
80 const geom::Coordinate& p2);
81
82 explicit LineIntersector(const geom::PrecisionModel* initialPrecisionModel = nullptr)
83 :
84 precisionModel(initialPrecisionModel),
85 result(0),
86 isProperVar(false)
87 {}
88
89 ~LineIntersector() = default;
90
99
107 bool isInteriorIntersection(std::size_t inputLineIndex);
108
115 void
117 {
118 precisionModel = newPM;
119 }
120
128
130 static bool hasIntersection(const geom::Coordinate& p, const geom::Coordinate& p1, const geom::Coordinate& p2);
131
132 enum intersection_type : uint8_t {
134 NO_INTERSECTION = 0,
135
137 POINT_INTERSECTION = 1,
138
140 COLLINEAR_INTERSECTION = 2
141 };
142
145 const geom::Coordinate& p3, const geom::Coordinate& p4);
146
147 std::string toString() const;
148
154 bool
156 {
157 return result != NO_INTERSECTION;
158 }
159
160
168 const geom::Coordinate*
169 getEndpoint(std::size_t segmentIndex, std::size_t ptIndex) const
170 {
171 return inputLines[segmentIndex][ptIndex];
172 }
173
178 size_t
180 {
181 return result;
182 }
183
184
191 const geom::Coordinate&
192 getIntersection(std::size_t intIndex) const
193 {
194 return intPt[intIndex];
195 }
196
201 static bool isSameSignAndNonZero(double a, double b);
202
213 bool isIntersection(const geom::Coordinate& pt) const;
214
229 bool
230 isProper() const
231 {
232 return hasIntersection() && isProperVar;
233 }
234
245 const geom::Coordinate& getIntersectionAlongSegment(std::size_t segmentIndex, std::size_t intIndex);
246
256 std::size_t getIndexAlongSegment(std::size_t segmentIndex, std::size_t intIndex);
257
267 double getEdgeDistance(std::size_t geomIndex, std::size_t intIndex) const;
268
269private:
270
275 const geom::PrecisionModel* precisionModel;
276
277 std::size_t result;
278
279 const geom::Coordinate* inputLines[2][2];
280
285 geom::Coordinate intPt[2];
286
291 std::size_t intLineIndex[2][2];
292
293 bool isProperVar;
294 //Coordinate &pa;
295 //Coordinate &pb;
296
297 bool
298 isCollinear() const
299 {
300 return result == COLLINEAR_INTERSECTION;
301 }
302
303 uint8_t computeIntersect(const geom::Coordinate& p1, const geom::Coordinate& p2,
304 const geom::Coordinate& q1, const geom::Coordinate& q2);
305
306 bool
307 isEndPoint() const
308 {
309 return hasIntersection() && !isProperVar;
310 }
311
312 void computeIntLineIndex();
313
314 void computeIntLineIndex(std::size_t segmentIndex);
315
316 uint8_t computeCollinearIntersection(const geom::Coordinate& p1, const geom::Coordinate& p2,
317 const geom::Coordinate& q1, const geom::Coordinate& q2);
318
328 geom::Coordinate intersection(const geom::Coordinate& p1,
329 const geom::Coordinate& p2,
330 const geom::Coordinate& q1,
331 const geom::Coordinate& q2) const;
332
343 bool isInSegmentEnvelopes(const geom::Coordinate& intPt) const;
344
345
358 geom::Coordinate intersectionSafe(const geom::Coordinate& p1, const geom::Coordinate& p2,
359 const geom::Coordinate& q1, const geom::Coordinate& q2) const;
360
380 static geom::Coordinate nearestEndpoint(const geom::Coordinate& p1,
381 const geom::Coordinate& p2,
382 const geom::Coordinate& q1,
383 const geom::Coordinate& q2);
384
385 static double zGet(const geom::Coordinate& p, const geom::Coordinate& q);
386
387 static double zGetOrInterpolate(const geom::Coordinate& p,
388 const geom::Coordinate& p0,
389 const geom::Coordinate& p1);
390
391 static geom::Coordinate zGetOrInterpolateCopy(const geom::Coordinate& p,
392 const geom::Coordinate& p0,
393 const geom::Coordinate& p1);
394
398 static double zInterpolate(const geom::Coordinate& p,
399 const geom::Coordinate& p0,
400 const geom::Coordinate& p1);
401
402 static double zInterpolate(const geom::Coordinate& p,
403 const geom::Coordinate& p1,
404 const geom::Coordinate& p2,
405 const geom::Coordinate& q1,
406 const geom::Coordinate& q2);
407
408};
409
410
411} // namespace geos::algorithm
412} // namespace geos
413
414#ifdef GEOS_INLINE
415# include "geos/algorithm/LineIntersector.inl"
416#endif
417
418#endif // GEOS_ALGORITHM_LINEINTERSECTOR_H
419
A LineIntersector is an algorithm that can both test whether two line segments intersect and compute ...
Definition: LineIntersector.h:50
intersection_type
Definition: LineIntersector.h:132
void computeIntersection(const geom::Coordinate &p1, const geom::Coordinate &p2, const geom::Coordinate &p3, const geom::Coordinate &p4)
Computes the intersection of the lines p1-p2 and p3-p4.
const geom::Coordinate * getEndpoint(std::size_t segmentIndex, std::size_t ptIndex) const
Definition: LineIntersector.h:169
static bool hasIntersection(const geom::Coordinate &p, const geom::Coordinate &p1, const geom::Coordinate &p2)
Same as above but doesn't compute intersection point. Faster.
bool isInteriorIntersection(std::size_t inputLineIndex)
Tests whether either intersection point is an interior point of the specified input segment.
const geom::Coordinate & getIntersectionAlongSegment(std::size_t segmentIndex, std::size_t intIndex)
Computes the intIndex'th intersection point in the direction of a specified input line segment.
void setPrecisionModel(const geom::PrecisionModel *newPM)
Definition: LineIntersector.h:116
bool isInteriorIntersection()
Tests whether either intersection point is an interior point of one of the input segments.
bool hasIntersection() const
Definition: LineIntersector.h:155
static bool isSameSignAndNonZero(double a, double b)
const geom::Coordinate & getIntersection(std::size_t intIndex) const
Definition: LineIntersector.h:192
void computeIntersection(const geom::Coordinate &p, const geom::Coordinate &p1, const geom::Coordinate &p2)
bool isProper() const
Tests whether an intersection is proper.
Definition: LineIntersector.h:230
static double computeEdgeDistance(const geom::Coordinate &p, const geom::Coordinate &p0, const geom::Coordinate &p1)
static double interpolateZ(const geom::Coordinate &p, const geom::Coordinate &p0, const geom::Coordinate &p1)
Return a Z value being the interpolation of Z from p0 and p1 at the given point p.
size_t getIntersectionNum() const
Definition: LineIntersector.h:179
double getEdgeDistance(std::size_t geomIndex, std::size_t intIndex) const
Computes the "edge distance" of an intersection point along the specified input line segment.
std::size_t getIndexAlongSegment(std::size_t segmentIndex, std::size_t intIndex)
Computes the index of the intIndex'th intersection point in the direction of a specified input line s...
bool isIntersection(const geom::Coordinate &pt) const
Test whether a point is a intersection point of two line segments.
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:87
Basic namespace for all GEOS functionalities.
Definition: geos.h:40