GEOS 3.8.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
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
359
366 std::unique_ptr<LineString> toGeometry(const GeometryFactory& gf) const;
367
368 struct HashCode {
369 size_t operator()(const LineSegment & s) const {
370 size_t h = std::hash<double>{}(s.p0.x);
371 h ^= (std::hash<double>{}(s.p0.y) << 1);
372 h ^= (std::hash<double>{}(s.p1.x) << 1);
373 return h ^ (std::hash<double>{}(s.p1.y) << 1);
374 }
375 };
376};
377
378std::ostream& operator<< (std::ostream& o, const LineSegment& l);
379
381bool operator==(const LineSegment& a, const LineSegment& b);
382
383
384} // namespace geos::geom
385} // namespace geos
386
387#ifdef GEOS_INLINE
388# include "geos/geom/LineSegment.inl"
389#endif
390
391#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.
bool isVertical() const
Tests whether the segment is vertical.
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
Tests whether the segment is horizontal.
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:25