GEOS 3.15.0dev
SegmentString.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 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: noding/SegmentString.java r430 (JTS-1.12+)
18 *
19 **********************************************************************/
20
21#pragma once
22
23#include <geos/export.h>
24#include <geos/algorithm/Length.h>
25#include <geos/geom/Coordinate.h>
26#include <geos/geom/CoordinateSequence.h>
27#include <geos/noding/Octant.h>
28#include <geos/noding/PathString.h>
29
30#include <vector>
31
32// Forward declarations
33namespace geos {
34namespace algorithm {
35class LineIntersector;
36}
37}
38
39namespace geos {
40namespace noding { // geos.noding
41
49class GEOS_DLL SegmentString : public PathString {
50public:
51 typedef std::vector<const SegmentString*> ConstVect;
52 typedef std::vector<SegmentString*> NonConstVect;
53
54 friend std::ostream& operator<< (std::ostream& os,
55 const SegmentString& ss);
56
62 SegmentString(const void* newContext, const std::shared_ptr<const geom::CoordinateSequence>& newSeq)
63 : PathString(newContext),
64 seq(newSeq)
65 {}
66
67 ~SegmentString() override = default;
68
69 std::size_t size() const {
70 // FIXME: Remove this method, or make consistent with getSize
71 return seq->size();
72 }
73
74 std::size_t getSize() const override{
75 return seq->size() - 1;
76 }
77
78 template<typename CoordType = geom::Coordinate>
79 const CoordType& getCoordinate(std::size_t i) const {
80 return seq->getAt<CoordType>(i);
81 }
82
83 double getLength() const override {
84 return algorithm::Length::ofLine(seq.get());
85 }
86
90 const std::shared_ptr<const geom::CoordinateSequence>& getCoordinates() const override {
91 return seq;
92 }
93
94 void setCoordinates(const std::shared_ptr<const geom::CoordinateSequence>& newSeq) {
95 seq = newSeq;
96 }
97
105 int getSegmentOctant(std::size_t index) const
106 {
107 if (index >= size() - 1) {
108 return -1;
109 }
110 return safeOctant(seq->getAt<geom::CoordinateXY>(index),
111 seq->getAt<geom::CoordinateXY>(index + 1));
112 };
113
114 static int getSegmentOctant(const SegmentString& ss, std::size_t index) {
115 return ss.getSegmentOctant(index);
116 }
117
126 const geom::CoordinateXY& nextInRing(std::size_t index) const
127 {
128 std::size_t nextIndex = index + 1;
129 if (nextIndex > size() - 1) {
130 nextIndex = 1;
131 }
132 return getCoordinate(nextIndex);
133 }
134
143 const geom::CoordinateXY& prevInRing(std::size_t index) const
144 {
145 std::size_t prevIndex;
146 if (index == 0)
147 prevIndex = size() - 2;
148 else
149 prevIndex = index - 1;
150 return getCoordinate( prevIndex );
151 }
152
153
154 bool isClosed() const {
155 return seq->front<geom::CoordinateXY>().equals(seq->back<geom::CoordinateXY>());
156 }
157
158 static std::vector<SegmentString*> toRawPointerVector(const std::vector<std::unique_ptr<SegmentString>> & segStrings);
159
160 static std::vector<SegmentString*> toRawPointerVector(const std::vector<std::unique_ptr<PathString>> & segStrings);
161
162 virtual std::ostream& print(std::ostream& os) const;
163
164protected:
165 std::shared_ptr<const geom::CoordinateSequence> seq;
166
167private:
168 static int safeOctant(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1)
169 {
170 if(p0.equals2D(p1)) {
171 return 0;
172 }
173 return Octant::octant(p0, p1);
174 };
175
176 // Declare type as noncopyable
177 SegmentString(const SegmentString& other) = delete;
178 SegmentString& operator=(const SegmentString& rhs) = delete;
179};
180
181std::ostream& operator<< (std::ostream& os, const SegmentString& ss);
182
183} // namespace geos.noding
184} // namespace geos
185
Definition PathString.h:31
An interface for classes which represent a sequence of contiguous line segments.
Definition SegmentString.h:49
SegmentString(const void *newContext, const std::shared_ptr< const geom::CoordinateSequence > &newSeq)
Construct a SegmentString.
Definition SegmentString.h:62
const std::shared_ptr< const geom::CoordinateSequence > & getCoordinates() const override
Return a pointer to the CoordinateSequence associated with this SegmentString.
Definition SegmentString.h:90
const geom::CoordinateXY & nextInRing(std::size_t index) const
Definition SegmentString.h:126
int getSegmentOctant(std::size_t index) const
Gets the octant of the segment starting at vertex index.
Definition SegmentString.h:105
const geom::CoordinateXY & prevInRing(std::size_t index) const
Definition SegmentString.h:143
Basic namespace for all GEOS functionalities.
Definition geos.h:38