GEOS  3.8.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/export.h>
24 #include <string>
25 
26 #include <geos/geom/Coordinate.h>
27 
28 // Forward declarations
29 namespace geos {
30 namespace geom {
31 class PrecisionModel;
32 }
33 }
34 
35 namespace geos {
36 namespace algorithm { // geos::algorithm
37 
49 class GEOS_DLL LineIntersector {
50 public:
51 
55  static double interpolateZ(const geom::Coordinate& p, const geom::Coordinate& p0, const geom::Coordinate& p1);
56 
57 
59  //
76  static double computeEdgeDistance(const geom::Coordinate& p, const geom::Coordinate& p0, const geom::Coordinate& p1);
77 
78  static double nonRobustComputeEdgeDistance(const geom::Coordinate& p, const geom::Coordinate& p1,
79  const geom::Coordinate& p2);
80 
81  LineIntersector(const geom::PrecisionModel* initialPrecisionModel = nullptr)
82  :
83  precisionModel(initialPrecisionModel),
84  result(0),
85  isProperVar(false)
86  {}
87 
88  ~LineIntersector() {}
89 
98 
106  bool isInteriorIntersection(int inputLineIndex);
107 
109  //
114  void
116  {
117  precisionModel = newPM;
118  }
119 
121  //
127 
129  static bool hasIntersection(const geom::Coordinate& p, const geom::Coordinate& p1, const geom::Coordinate& p2);
130 
131  // These are deprecated, due to ambiguous naming
132  enum {
133  DONT_INTERSECT = 0,
134  DO_INTERSECT = 1,
135  COLLINEAR = 2
136  };
137 
138  enum {
140  NO_INTERSECTION = 0,
141 
143  POINT_INTERSECTION = 1,
144 
146  COLLINEAR_INTERSECTION = 2
147  };
148 
151  const geom::Coordinate& p3, const geom::Coordinate& p4);
152 
153  std::string toString() const;
154 
160  bool
162  {
163  return result != NO_INTERSECTION;
164  }
165 
167  //
170  size_t
172  {
173  return result;
174  }
175 
176 
178  //
183  const geom::Coordinate&
184  getIntersection(size_t intIndex) const
185  {
186  return intPt[intIndex];
187  }
188 
190  //
193  static bool isSameSignAndNonZero(double a, double b);
194 
205  bool isIntersection(const geom::Coordinate& pt) const;
206 
221  bool
222  isProper() const
223  {
224  return hasIntersection() && isProperVar;
225  }
226 
237  const geom::Coordinate& getIntersectionAlongSegment(int segmentIndex, int intIndex);
238 
248  int getIndexAlongSegment(int segmentIndex, int intIndex);
249 
259  double getEdgeDistance(size_t geomIndex, size_t intIndex) const;
260 
261 private:
262 
267  const geom::PrecisionModel* precisionModel;
268 
269  size_t result;
270 
271  const geom::Coordinate* inputLines[2][2];
272 
277  geom::Coordinate intPt[2];
278 
283  int intLineIndex[2][2];
284 
285  bool isProperVar;
286  //Coordinate &pa;
287  //Coordinate &pb;
288 
289  bool
290  isCollinear() const
291  {
292  return result == COLLINEAR_INTERSECTION;
293  }
294 
295  int computeIntersect(const geom::Coordinate& p1, const geom::Coordinate& p2,
296  const geom::Coordinate& q1, const geom::Coordinate& q2);
297 
298  bool
299  isEndPoint() const
300  {
301  return hasIntersection() && !isProperVar;
302  }
303 
304  void computeIntLineIndex();
305 
306  void computeIntLineIndex(int segmentIndex);
307 
308  int computeCollinearIntersection(const geom::Coordinate& p1, const geom::Coordinate& p2,
309  const geom::Coordinate& q1, const geom::Coordinate& q2);
310 
320  geom::Coordinate intersection(const geom::Coordinate& p1,
321  const geom::Coordinate& p2,
322  const geom::Coordinate& q1,
323  const geom::Coordinate& q2) const;
324 
335  bool isInSegmentEnvelopes(const geom::Coordinate& intPt) const;
336 
337 
350  geom::Coordinate intersectionSafe(const geom::Coordinate& p1, const geom::Coordinate& p2,
351  const geom::Coordinate& q1, const geom::Coordinate& q2) const;
352 
353 };
354 
355 } // namespace geos::algorithm
356 } // namespace geos
357 
358 
359 #endif // GEOS_ALGORITHM_LINEINTERSECTOR_H
360 
A LineIntersector is an algorithm that can both test whether two line segments intersect and compute ...
Definition: LineIntersector.h:49
const geom::Coordinate & getIntersection(size_t intIndex) const
Returns the intIndex'th intersection point.
Definition: LineIntersector.h:184
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.
static bool hasIntersection(const geom::Coordinate &p, const geom::Coordinate &p1, const geom::Coordinate &p2)
Same as above but doen's compute intersection point. Faster.
bool isInteriorIntersection(int inputLineIndex)
Tests whether either intersection point is an interior point of the specified input segment.
void setPrecisionModel(const geom::PrecisionModel *newPM)
Force computed intersection to be rounded to a given precision model.
Definition: LineIntersector.h:115
bool isInteriorIntersection()
Tests whether either intersection point is an interior point of one of the input segments.
const geom::Coordinate & getIntersectionAlongSegment(int segmentIndex, int intIndex)
Computes the intIndex'th intersection point in the direction of a specified input line segment.
bool hasIntersection() const
Definition: LineIntersector.h:161
double getEdgeDistance(size_t geomIndex, size_t intIndex) const
Computes the "edge distance" of an intersection point along the specified input line segment.
static bool isSameSignAndNonZero(double a, double b)
Returns false if both numbers are zero.
void computeIntersection(const geom::Coordinate &p, const geom::Coordinate &p1, const geom::Coordinate &p2)
Compute the intersection of a point p and the line p1-p2.
int getIndexAlongSegment(int segmentIndex, int intIndex)
Computes the index of the intIndex'th intersection point in the direction of a specified input line s...
bool isProper() const
Tests whether an intersection is proper.
Definition: LineIntersector.h:222
static double computeEdgeDistance(const geom::Coordinate &p, const geom::Coordinate &p0, const geom::Coordinate &p1)
Computes the "edge distance" of an intersection point p in an edge.
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
Returns the number of intersection points found.
Definition: LineIntersector.h:171
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: IndexedNestedRingTester.h:25