GEOS  3.8.0dev
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.
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
263  void intersectionWithNormalization(const geom::Coordinate& p1,
264  const geom::Coordinate& p2,
265  const geom::Coordinate& q1,
266  const geom::Coordinate& q2,
267  geom::Coordinate& ret) const;
268
273  const geom::PrecisionModel* precisionModel;
274
275  size_t result;
276
277  const geom::Coordinate* inputLines[2][2];
278
283  geom::Coordinate intPt[2];
284
289  int intLineIndex[2][2];
290
291  bool isProperVar;
292  //Coordinate &pa;
293  //Coordinate &pb;
294
295  bool
296  isCollinear() const
297  {
298  return result == COLLINEAR_INTERSECTION;
299  }
300
301  int computeIntersect(const geom::Coordinate& p1, const geom::Coordinate& p2, const geom::Coordinate& q1,
302  const geom::Coordinate& q2);
303
304  bool
305  isEndPoint() const
306  {
307  return hasIntersection() && !isProperVar;
308  }
309
310  void computeIntLineIndex();
311
312  void computeIntLineIndex(int segmentIndex);
313
314  int computeCollinearIntersection(const geom::Coordinate& p1,
315  const geom::Coordinate& p2, const geom::Coordinate& q1,
316  const geom::Coordinate& q2);
317
327  void intersection(const geom::Coordinate& p1,
328  const geom::Coordinate& p2,
329  const geom::Coordinate& q1,
330  const geom::Coordinate& q2,
331  geom::Coordinate& ret) const;
332
333  double smallestInAbsValue(double x1, double x2,
334  double x3, double x4) const;
335
346  bool isInSegmentEnvelopes(const geom::Coordinate& intPt) const;
347
359  void normalizeToEnvCentre(geom::Coordinate& n00, geom::Coordinate& n01,
360  geom::Coordinate& n10, geom::Coordinate& n11,
361  geom::Coordinate& normPt) const;
362
375  void safeHCoordinateIntersection(const geom::Coordinate& p1,
376  const geom::Coordinate& p2,
377  const geom::Coordinate& q1,
378  const geom::Coordinate& q2,
379  geom::Coordinate& intPt) const;
380
381 };
382
383 } // namespace geos::algorithm
384 } // namespace geos
385
386
387 #endif // GEOS_ALGORITHM_LINEINTERSECTOR_H
388
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
bool isProper() const
Tests whether an intersection is proper.
Definition: LineIntersector.h:222
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
const geom::Coordinate & getIntersection(size_t intIndex) const
Returns the intIndex'th intersection point.
Definition: LineIntersector.h:184
void setPrecisionModel(const geom::PrecisionModel *newPM)
Force computed intersection to be rounded to a given precision model.
Definition: LineIntersector.h:115
bool hasIntersection() const
Definition: LineIntersector.h:161
size_t getIntersectionNum() const
Returns the number of intersection points found.
Definition: LineIntersector.h:171