GEOS  3.9.1dev
LineSegment.h
Go to the documentation of this file.
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 
60 public:
61 
62  friend std::ostream& operator<< (std::ostream& o, const LineSegment& l);
63 
65 
67 
68  LineSegment();
69 
71  LineSegment(const Coordinate& c0, const Coordinate& c1);
72 
73  LineSegment(double x0, double y0, double x1, double y1);
74 
75  void setCoordinates(const Coordinate& c0, const Coordinate& c1);
76 
77  // obsoleted, use operator[] instead
78  //const Coordinate& getCoordinate(std::size_t i) const;
79 
80  const Coordinate& operator[](std::size_t i) const;
81  Coordinate& operator[](std::size_t i);
82 
83  void setCoordinates(const LineSegment& ls);
84 
86  double getLength() const;
87 
92  bool isHorizontal() const;
93 
98  bool isVertical() const;
99 
121  int orientationIndex(const LineSegment& seg) const;
122 
123  // TODO: deprecate this
124  int orientationIndex(const LineSegment* seg) const;
125 
142  int orientationIndex(const Coordinate& p) const;
143 
145  void reverse();
146 
148  //
152  void normalize();
153 
155  double angle() const;
156 
158  //
161  void midPoint(Coordinate& ret) const;
162 
164  double distance(const LineSegment& ls) const;
165 
167  double distance(const Coordinate& p) const;
168 
173  double distancePerpendicular(const Coordinate& p) const;
174 
189  void pointAlong(double segmentLengthFraction, Coordinate& ret) const;
190 
215  void pointAlongOffset(double segmentLengthFraction,
216  double offsetDistance,
217  Coordinate& ret) const;
218 
236  double projectionFactor(const Coordinate& p) const;
237 
253  double segmentFraction(const Coordinate& inputPt) const;
254 
263  void project(const Coordinate& p, Coordinate& ret) const;
264 
280  bool project(const LineSegment& seg, LineSegment& ret) const;
281 
283  //
288  void closestPoint(const Coordinate& p, Coordinate& ret) const;
289 
301  int compareTo(const LineSegment& other) const;
302 
312  bool equalsTopo(const LineSegment& other) const;
313 
320  std::array<Coordinate, 2> closestPoints(const LineSegment& line);
321 
322  std::array<Coordinate, 2> closestPoints(const LineSegment* line);
323 
336  Coordinate intersection(const LineSegment& line) const;
337 
354  Coordinate lineIntersection(const LineSegment& line) const;
355 
362  std::unique_ptr<LineString> toGeometry(const GeometryFactory& gf) const;
363 
364  struct HashCode {
365  size_t operator()(const LineSegment & s) const {
366  size_t h = std::hash<double>{}(s.p0.x);
367  h ^= (std::hash<double>{}(s.p0.y) << 1);
368  h ^= (std::hash<double>{}(s.p1.x) << 1);
369  return h ^ (std::hash<double>{}(s.p1.y) << 1);
370  }
371  };
372 };
373 
374 std::ostream& operator<< (std::ostream& o, const LineSegment& l);
375 
377 bool operator==(const LineSegment& a, const LineSegment& b);
378 
379 
380 } // namespace geos::geom
381 } // namespace geos
382 
383 #ifdef GEOS_INLINE
384 # include "geos/geom/LineSegment.inl"
385 #endif
386 
387 #endif // ndef GEOS_GEOM_LINESEGMENT_H
Coordinate p1
Segment start.
Definition: LineSegment.h:66
#define GEOS_DLL
Definition: export.h:28
double y
y-coordinate
Definition: Coordinate.h:83
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
size_t operator()(const LineSegment &s) const
Definition: LineSegment.h:365
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...
Basic namespace for all GEOS functionalities.
double x
x-coordinate
Definition: Coordinate.h:80
bool operator==(const Coordinate &a, const Coordinate &b)
Equality operator for Coordinate. 2D only.