GEOS 3.9.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#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
35namespace geos {
36namespace geom {
37class CoordinateSequence;
38class GeometryFactory;
39class LineString;
40}
41}
42
43namespace geos {
44namespace geom { // geos::geom
45
59class GEOS_DLL LineSegment {
60public:
61
62 friend std::ostream& operator<< (std::ostream& o, const LineSegment& l);
63
64 Coordinate p0;
65
67
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
337
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
374std::ostream& operator<< (std::ostream& o, const LineSegment& l);
375
377bool 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 is the lightweight class used to store coordinates.
Definition Coordinate.h:60
double y
y-coordinate
Definition Coordinate.h:83
double x
x-coordinate
Definition Coordinate.h:80
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:68
Definition LineSegment.h:59
void closestPoint(const Coordinate &p, Coordinate &ret) const
Computes the closest point on this line segment to another point.
LineSegment(const Coordinate &c0, const Coordinate &c1)
Constructs a LineSegment with the given start and end Coordinates.
double distancePerpendicular(const Coordinate &p) const
Computes the perpendicular distance between the (infinite) line defined by this line segment and a po...
void midPoint(Coordinate &ret) const
Computes the midpoint of the segment.
int compareTo(const LineSegment &other) const
Compares this object with the specified object for order.
void pointAlongOffset(double segmentLengthFraction, double offsetDistance, Coordinate &ret) const
Computes the Coordinate that lies a given fraction along the line defined by this segment and offset ...
bool project(const LineSegment &seg, LineSegment &ret) const
Project a line segment onto this line segment and return the resulting line segment.
double projectionFactor(const Coordinate &p) const
Compute the projection factor for the projection of the point p onto this LineSegment.
void pointAlong(double segmentLengthFraction, Coordinate &ret) const
Computes the Coordinate that lies a given fraction along the line defined by this segment.
void project(const Coordinate &p, Coordinate &ret) const
Compute the projection of a point onto the line determined by this line segment.
bool equalsTopo(const LineSegment &other) const
Returns true if other is topologically equal to this LineSegment (e.g. irrespective of orientation).
void reverse()
Reverses the direction of the line segment.
double distance(const Coordinate &p) const
Computes the distance between this line segment and a point.
double distance(const LineSegment &ls) const
Computes the distance between this line segment and another one.
int orientationIndex(const LineSegment &seg) const
std::unique_ptr< LineString > toGeometry(const GeometryFactory &gf) const
void normalize()
Puts the line segment into a normalized form.
std::array< Coordinate, 2 > closestPoints(const LineSegment &line)
bool isHorizontal() const
double getLength() const
Computes the length of the line segment.
Coordinate p1
Segment start.
Definition LineSegment.h:66
Coordinate lineIntersection(const LineSegment &line) const
Computes the intersection point of the lines defined by two segments, if there is one.
int orientationIndex(const Coordinate &p) const
Determines the orientation index of a Coordinate relative to this segment.
double angle() const
LineSegment()
Segment end.
Coordinate intersection(const LineSegment &line) const
double segmentFraction(const Coordinate &inputPt) const
Computes the fraction of distance (in [0.0, 1.0]) that the projection of a point occurs along this li...
bool operator==(const Coordinate &a, const Coordinate &b)
Equality operator for Coordinate. 2D only.
std::ostream & operator<<(std::ostream &os, const Coordinate &c)
Output function.
Basic namespace for all GEOS functionalities.
Definition IndexedNestedRingTester.h:26