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 :
64 seq(newSeq),
65 context(newContext)
66 {}
67
68 virtual
70
76 const void*
77 getData() const
78 {
79 return context;
80 }
81
87 void
88 setData(const void* data)
89 {
90 context = data;
91 }
92
93 std::size_t size() const {
94 // FIXME: Remove this method, or make consistent with getSize
95 return seq->size();
96 }
97
98 std::size_t getSize() const override{
99 return seq->size() - 1;
100 }
101
102 template<typename CoordType = geom::Coordinate>
103 const CoordType& getCoordinate(std::size_t i) const {
104 return seq->getAt<CoordType>(i);
105 }
106
107 double getLength() const override {
108 return algorithm::Length::ofLine(seq.get());
109 }
110
114 const std::shared_ptr<const geom::CoordinateSequence>& getCoordinates() const override {
115 return seq;
116 }
117
118 void setCoordinates(const std::shared_ptr<const geom::CoordinateSequence>& newSeq) {
119 seq = newSeq;
120 }
121
129 int getSegmentOctant(std::size_t index) const
130 {
131 if (index >= size() - 1) {
132 return -1;
133 }
134 return safeOctant(seq->getAt<geom::CoordinateXY>(index),
135 seq->getAt<geom::CoordinateXY>(index + 1));
136 };
137
138 static int getSegmentOctant(const SegmentString& ss, std::size_t index) {
139 return ss.getSegmentOctant(index);
140 }
141
150 const geom::CoordinateXY& nextInRing(std::size_t index) const
151 {
152 std::size_t nextIndex = index + 1;
153 if (nextIndex > size() - 1) {
154 nextIndex = 1;
155 }
156 return getCoordinate(nextIndex);
157 }
158
167 const geom::CoordinateXY& prevInRing(std::size_t index) const
168 {
169 std::size_t prevIndex;
170 if (index == 0)
171 prevIndex = size() - 2;
172 else
173 prevIndex = index - 1;
174 return getCoordinate( prevIndex );
175 }
176
177
178 bool isClosed() const {
179 return seq->front<geom::CoordinateXY>().equals(seq->back<geom::CoordinateXY>());
180 }
181
182 static std::vector<SegmentString*> toRawPointerVector(const std::vector<std::unique_ptr<SegmentString>> & segStrings);
183
184 static std::vector<SegmentString*> toRawPointerVector(const std::vector<std::unique_ptr<PathString>> & segStrings);
185
186 virtual std::ostream& print(std::ostream& os) const;
187
188protected:
189 std::shared_ptr<const geom::CoordinateSequence> seq;
190
191private:
192 const void* context;
193
194 static int safeOctant(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1)
195 {
196 if(p0.equals2D(p1)) {
197 return 0;
198 }
199 return Octant::octant(p0, p1);
200 };
201
202 // Declare type as noncopyable
203 SegmentString(const SegmentString& other) = delete;
204 SegmentString& operator=(const SegmentString& rhs) = delete;
205};
206
207std::ostream& operator<< (std::ostream& os, const SegmentString& ss);
208
209} // namespace geos.noding
210} // namespace geos
211
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:114
const geom::CoordinateXY & nextInRing(std::size_t index) const
Definition SegmentString.h:150
const void * getData() const
Gets the user-defined data for this segment string.
Definition SegmentString.h:77
int getSegmentOctant(std::size_t index) const
Gets the octant of the segment starting at vertex index.
Definition SegmentString.h:129
const geom::CoordinateXY & prevInRing(std::size_t index) const
Definition SegmentString.h:167
void setData(const void *data)
Sets the user-defined data for this segment string.
Definition SegmentString.h:88
Basic namespace for all GEOS functionalities.
Definition geos.h:38