GEOS  3.8.0dev
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 #ifndef GEOS_GEOM_LINESEGMENT_H
22 #define GEOS_GEOM_LINESEGMENT_H
23 
24 #include <geos/export.h>
25 #include <geos/geom/Coordinate.h> // for composition
26 
27 #include <geos/inline.h>
28 
29 #include <array>
30 #include <iostream> // for ostream
31 #include <functional> // for std::hash
32 #include <memory> // for unique_ptr
33 
34 // Forward declarations
35 namespace geos {
36 namespace geom {
37 class CoordinateSequence;
38 class GeometryFactory;
39 class LineString;
40 }
41 }
42 
43 namespace geos {
44 namespace geom { // geos::geom
45 
59 class GEOS_DLL LineSegment {
60 public:
61 
62  friend std::ostream& operator<< (std::ostream& o, const LineSegment& l);
63 
64  Coordinate p0;
65 
67 
68  LineSegment();
69 
70  LineSegment(const LineSegment& ls);
71 
73  LineSegment(const Coordinate& c0, const Coordinate& c1);
74 
75  LineSegment(double x0, double y0, double x1, double y1);
76 
77  virtual ~LineSegment();
78 
79  void setCoordinates(const Coordinate& c0, const Coordinate& c1);
80 
81  // obsoleted, use operator[] instead
82  //const Coordinate& getCoordinate(std::size_t i) const;
83 
84  const Coordinate& operator[](std::size_t i) const;
85  Coordinate& operator[](std::size_t i);
86 
87  void setCoordinates(const LineSegment& ls);
88 
90  double getLength() const;
91 
93  //
96  bool isHorizontal() const;
97 
99  //
102  bool isVertical() const;
103 
125  int orientationIndex(const LineSegment& seg) const;
126 
127  // TODO: deprecate this
128  int orientationIndex(const LineSegment* seg) const;
129 
146  int orientationIndex(const Coordinate& p) const;
147 
149  void reverse();
150 
152  //
156  void normalize();
157 
159  double angle() const;
160 
162  //
165  void midPoint(Coordinate& ret) const;
166 
168  double distance(const LineSegment& ls) const;
169 
171  double distance(const Coordinate& p) const;
172 
177  double distancePerpendicular(const Coordinate& p) const;
178 
193  void pointAlong(double segmentLengthFraction, Coordinate& ret) const;
194 
219  void pointAlongOffset(double segmentLengthFraction,
220  double offsetDistance,
221  Coordinate& ret) const;
222 
240  double projectionFactor(const Coordinate& p) const;
241 
257  double segmentFraction(const Coordinate& inputPt) const;
258 
267  void project(const Coordinate& p, Coordinate& ret) const;
268 
284  bool project(const LineSegment& seg, LineSegment& ret) const;
285 
287  //
292  void closestPoint(const Coordinate& p, Coordinate& ret) const;
293 
305  int compareTo(const LineSegment& other) const;
306 
316  bool equalsTopo(const LineSegment& other) const;
317 
324  std::array<Coordinate, 2> closestPoints(const LineSegment& line);
325 
326  std::array<Coordinate, 2> closestPoints(const LineSegment* line);
327 
341  bool intersection(const LineSegment& line, Coordinate& coord) const;
342 
360  bool lineIntersection(const LineSegment& line, Coordinate& ret) const;
361 
368  std::unique_ptr<LineString> toGeometry(const GeometryFactory& gf) const;
369 
370  struct HashCode {
371  size_t operator()(const LineSegment & s) const {
372  size_t h = std::hash<double>{}(s.p0.x);
373  h ^= (std::hash<double>{}(s.p0.y) << 1);
374  h ^= (std::hash<double>{}(s.p1.x) << 1);
375  return h ^ (std::hash<double>{}(s.p1.y) << 1);
376  }
377  };
378 };
379 
380 std::ostream& operator<< (std::ostream& o, const LineSegment& l);
381 
383 bool operator==(const LineSegment& a, const LineSegment& b);
384 
385 
386 } // namespace geos::geom
387 } // namespace geos
388 
389 #ifdef GEOS_INLINE
390 # include "geos/geom/LineSegment.inl"
391 #endif
392 
393 #endif // ndef GEOS_GEOM_LINESEGMENT_H
Definition: LineSegment.h:59
Coordinate p1
Segment start.
Definition: LineSegment.h:66
double y
y-coordinate
Definition: Coordinate.h:83
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
GEOS_DLL std::ostream & operator<<(std::ostream &os, const Coordinate &c)
Output function.
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:66
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
GEOS_DLL bool operator==(const Coordinate &a, const Coordinate &b)
Equality operator for Coordinate. 2D only.
double x
x-coordinate
Definition: Coordinate.h:80