GEOS  3.11.1
LineSegment.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2009 2011 Sandro Santilli <strk@kbt.io>
7  * Copyright (C) 2005-2006 Refractions Research Inc.
8  * Copyright (C) 2001-2002 Vivid Solutions Inc.
9  *
10  * This is free software; you can redistribute and/or modify it under
11  * the terms of the GNU Lesser General Public Licence as published
12  * by the Free Software Foundation.
13  * See the COPYING file for more information.
14  *
15  **********************************************************************
16  *
17  * Last port: geom/LineSegment.java r18 (JTS-1.11)
18  *
19  **********************************************************************/
20 
21 #pragma once
22 
23 #include <geos/export.h>
24 #include <geos/geom/Coordinate.h> // for composition
25 #include <geos/geom/LineSegment.h>
26 #include <geos/algorithm/Distance.h>
27 #include <geos/algorithm/Orientation.h>
28 
29 #include <array>
30 #include <iostream> // for ostream
31 #include <functional> // for std::hash
32 #include <memory> // for unique_ptr
33 #include <cassert>
34 
35 // Forward declarations
36 namespace geos {
37 namespace geom {
38 class CoordinateSequence;
39 class GeometryFactory;
40 class LineString;
41 }
42 }
43 
44 namespace geos {
45 namespace geom { // geos::geom
46 
60 class GEOS_DLL LineSegment {
61 public:
62 
63  Coordinate p0;
65 
66  friend std::ostream& operator<< (std::ostream& o, const LineSegment& l);
67 
69  friend bool operator==(const LineSegment& a, const LineSegment& b)
70  {
71  return a.p0 == b.p0 && a.p1 == b.p1;
72  };
73 
74  LineSegment(const Coordinate& c0, const Coordinate& c1)
75  : p0(c0)
76  , p1(c1)
77  {};
78 
79  LineSegment(double x0, double y0, double x1, double y1)
80  : p0(x0, y0)
81  , p1(x1, y1)
82  {};
83 
84  LineSegment() {};
85 
86 
87  void setCoordinates(const Coordinate& c0, const Coordinate& c1)
88  {
89  p0 = c0;
90  p1 = c1;
91  };
92 
93  void setCoordinates(const LineSegment& ls)
94  {
95  setCoordinates(ls.p0, ls.p1);
96  };
97 
98  // obsoleted, use operator[] instead
99  //const Coordinate& getCoordinate(std::size_t i) const;
100 
101  const Coordinate& operator[](std::size_t i) const
102  {
103  if(i == 0) {
104  return p0;
105  }
106  assert(i == 1);
107  return p1;
108  };
109 
110  Coordinate& operator[](std::size_t i)
111  {
112  if(i == 0) {
113  return p0;
114  }
115  assert(i == 1);
116  return p1;
117  };
118 
120  double minX() const
121  {
122  return std::min(p0.x, p1.x);
123  };
124 
126  double maxX() const
127  {
128  return std::max(p0.x, p1.x);
129  };
130 
132  double minY() const
133  {
134  return std::min(p0.y, p1.y);
135  };
136 
138  double maxY() const
139  {
140  return std::max(p0.y, p1.y);
141  };
142 
144  double getLength() const
145  {
146  return p0.distance(p1);
147  };
148 
153  bool isHorizontal() const
154  {
155  return p0.y == p1.y;
156  };
157 
162  bool isVertical() const
163  {
164  return p0.x == p1.x;
165  };
166 
188  int orientationIndex(const LineSegment& seg) const;
189 
190  // TODO deprecate this
191  int orientationIndex(const LineSegment* seg) const
192  {
193  assert(seg);
194  return orientationIndex(*seg);
195  };
196 
197 
198 
215  int orientationIndex(const Coordinate& p) const
216  {
217  return algorithm::Orientation::index(p0, p1, p);
218  };
219 
221  void reverse();
222 
224  //
228  void normalize()
229  {
230  if(p1.compareTo(p0) < 0) {
231  reverse();
232  }
233  };
234 
236  double angle() const
237  {
238  return std::atan2(p1.y - p0.y, p1.x - p0.x);
239  };
240 
242  //
245  void midPoint(Coordinate& ret) const
246  {
247  ret = Coordinate(
248  (p0.x + p1.x) / 2,
249  (p0.y + p1.y) / 2);
250  };
251 
253  double distance(const LineSegment& ls) const
254  {
255  return algorithm::Distance::segmentToSegment(p0, p1, ls.p0, ls.p1);
256  };
257 
259  double distance(const Coordinate& p) const
260  {
261  return algorithm::Distance::pointToSegment(p, p0, p1);
262  };
263 
268  double distancePerpendicular(const Coordinate& p) const
269  {
271  };
272 
287  void pointAlong(double segmentLengthFraction, Coordinate& ret) const
288  {
289  ret = Coordinate(
290  p0.x + segmentLengthFraction * (p1.x - p0.x),
291  p0.y + segmentLengthFraction * (p1.y - p0.y));
292  };
293 
318  void pointAlongOffset(double segmentLengthFraction,
319  double offsetDistance,
320  Coordinate& ret) const;
321 
322 
335  LineSegment offset(double offsetDistance);
336 
337 
355  double projectionFactor(const Coordinate& p) const;
356 
372  double segmentFraction(const Coordinate& inputPt) const;
373 
382  void project(const Coordinate& p, Coordinate& ret) const;
383 
399  bool project(const LineSegment& seg, LineSegment& ret) const;
400 
402  //
407  void closestPoint(const Coordinate& p, Coordinate& ret) const;
408 
420  int compareTo(const LineSegment& other) const;
421 
431  bool equalsTopo(const LineSegment& other) const;
432 
439  std::array<Coordinate, 2> closestPoints(const LineSegment& line);
440 
441  std::array<Coordinate, 2> closestPoints(const LineSegment* line)
442  {
443  assert(line);
444  return closestPoints(*line);
445  }
446 
459  Coordinate intersection(const LineSegment& line) const;
460 
477  Coordinate lineIntersection(const LineSegment& line) const;
478 
485  std::unique_ptr<LineString> toGeometry(const GeometryFactory& gf) const;
486 
487  struct HashCode {
488  std::size_t operator()(const LineSegment & s) const {
489  std::size_t h = std::hash<double>{}(s.p0.x);
490  h ^= (std::hash<double>{}(s.p0.y) << 1);
491  h ^= (std::hash<double>{}(s.p1.x) << 1);
492  return h ^ (std::hash<double>{}(s.p1.y) << 1);
493  }
494  };
495 
496 private:
497  void project(double factor, Coordinate& ret) const;
498 
499 };
500 
501 // std::ostream& operator<< (std::ostream& o, const LineSegment& l);
502 
503 
504 
505 } // namespace geos::geom
506 } // namespace geos
int orientationIndex(const Coordinate &p) const
Determines the orientation index of a Coordinate relative to this segment.
Definition: LineSegment.h:215
Definition: LineSegment.h:60
Coordinate p1
Segment start.
Definition: LineSegment.h:64
double distance(const Coordinate &p) const
Computes the distance between this line segment and a point.
Definition: LineSegment.h:259
double y
y-coordinate
Definition: Coordinate.h:81
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
bool isVertical() const
Definition: LineSegment.h:162
static double pointToSegment(const geom::Coordinate &p, const geom::Coordinate &A, const geom::Coordinate &B)
double minX() const
gets the minimum X ordinate value
Definition: LineSegment.h:120
static double pointToLinePerpendicular(const geom::Coordinate &p, const geom::Coordinate &A, const geom::Coordinate &B)
double distancePerpendicular(const Coordinate &p) const
Computes the perpendicular distance between the (infinite) line defined by this line segment and a po...
Definition: LineSegment.h:268
bool isHorizontal() const
Definition: LineSegment.h:153
static double segmentToSegment(const geom::Coordinate &A, const geom::Coordinate &B, const geom::Coordinate &C, const geom::Coordinate &D)
double minY() const
gets the minimum Y ordinate value
Definition: LineSegment.h:132
double maxY() const
gets the maximum Y ordinate value
Definition: LineSegment.h:138
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25
double getLength() const
Computes the length of the line segment.
Definition: LineSegment.h:144
static int index(const geom::Coordinate &p1, const geom::Coordinate &p2, const geom::Coordinate &q)
Returns the orientation index of the direction of the point q relative to a directed infinite line sp...
double distance(const LineSegment &ls) const
Computes the distance between this line segment and another one.
Definition: LineSegment.h:253
void pointAlong(double segmentLengthFraction, Coordinate &ret) const
Computes the Coordinate that lies a given fraction along the line defined by this segment...
Definition: LineSegment.h:287
void midPoint(Coordinate &ret) const
Computes the midpoint of the segment.
Definition: LineSegment.h:245
double maxX() const
gets the maximum X ordinate value
Definition: LineSegment.h:126
double distance(const Coordinate &p) const
Definition: Coordinate.h:191
double x
x-coordinate
Definition: Coordinate.h:78
int compareTo(const Coordinate &other) const
TODO: deprecate this, move logic to CoordinateLessThen instead.
Definition: Coordinate.h:161
double angle() const
Definition: LineSegment.h:236
void normalize()
Puts the line segment into a normalized form.
Definition: LineSegment.h:228
friend bool operator==(const LineSegment &a, const LineSegment &b)
Checks if two LineSegment are equal (2D only check)
Definition: LineSegment.h:69