GEOS  3.8.0
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 
97  bool isInteriorIntersection();
98 
106  bool isInteriorIntersection(int inputLineIndex);
107 
109  //
114  void
116  {
117  precisionModel = newPM;
118  }
119 
121  //
126  void computeIntersection(const geom::Coordinate& p, const geom::Coordinate& p1, const geom::Coordinate& p2);
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 
150  void computeIntersection(const geom::Coordinate& p1, const geom::Coordinate& p2,
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 
size_t getIntersectionNum() const
Returns the number of intersection points found.
Definition: LineIntersector.h:171
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
A LineIntersector is an algorithm that can both test whether two line segments intersect and compute ...
Definition: LineIntersector.h:49
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
bool isProper() const
Tests whether an intersection is proper.
Definition: LineIntersector.h:222
bool hasIntersection() const
Definition: LineIntersector.h:161
void setPrecisionModel(const geom::PrecisionModel *newPM)
Force computed intersection to be rounded to a given precision model.
Definition: LineIntersector.h:115
const geom::Coordinate & getIntersection(size_t intIndex) const
Returns the intIndex'th intersection point.
Definition: LineIntersector.h:184