/src/geos/include/geos/noding/SegmentString.h
Line | Count | Source |
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 |
33 | | namespace geos { |
34 | | namespace algorithm { |
35 | | class LineIntersector; |
36 | | } |
37 | | } |
38 | | |
39 | | namespace geos { |
40 | | namespace noding { // geos.noding |
41 | | |
42 | | /** \brief |
43 | | * An interface for classes which represent a sequence of contiguous |
44 | | * line segments. |
45 | | * |
46 | | * SegmentStrings can carry a context object, which is useful |
47 | | * for preserving topological or parentage information. |
48 | | */ |
49 | | class GEOS_DLL SegmentString : public PathString { |
50 | | public: |
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 | | |
57 | | /// \brief Construct a SegmentString. |
58 | | /// |
59 | | /// @param newContext the context associated to this SegmentString |
60 | | /// @param newSeq coordinates of this SegmentString |
61 | | /// |
62 | | SegmentString(const void* newContext, const std::shared_ptr<const geom::CoordinateSequence>& newSeq) |
63 | 0 | : PathString(newContext), |
64 | 0 | seq(newSeq) |
65 | 0 | {} |
66 | | |
67 | 0 | ~SegmentString() override = default; |
68 | | |
69 | 0 | std::size_t size() const { |
70 | | // FIXME: Remove this method, or make consistent with getSize |
71 | 0 | return seq->size(); |
72 | 0 | } |
73 | | |
74 | 0 | std::size_t getSize() const override{ |
75 | 0 | return seq->size() - 1; |
76 | 0 | } |
77 | | |
78 | | template<typename CoordType = geom::Coordinate> |
79 | 0 | const CoordType& getCoordinate(std::size_t i) const { |
80 | 0 | return seq->getAt<CoordType>(i); |
81 | 0 | } Unexecuted instantiation: geos::geom::Coordinate const& geos::noding::SegmentString::getCoordinate<geos::geom::Coordinate>(unsigned long) const Unexecuted instantiation: geos::geom::CoordinateXY const& geos::noding::SegmentString::getCoordinate<geos::geom::CoordinateXY>(unsigned long) const |
82 | | |
83 | 0 | double getLength() const override { |
84 | 0 | return algorithm::Length::ofLine(seq.get()); |
85 | 0 | } |
86 | | |
87 | | /// \brief |
88 | | /// Return a pointer to the CoordinateSequence associated |
89 | | /// with this SegmentString. |
90 | 0 | const std::shared_ptr<const geom::CoordinateSequence>& getCoordinates() const override { |
91 | 0 | return seq; |
92 | 0 | } |
93 | | |
94 | 0 | void setCoordinates(const std::shared_ptr<const geom::CoordinateSequence>& newSeq) { |
95 | 0 | seq = newSeq; |
96 | 0 | } |
97 | | |
98 | | /** \brief |
99 | | * Gets the octant of the segment starting at vertex index. |
100 | | * |
101 | | * @param index the index of the vertex starting the segment. |
102 | | * Must not be the last index in the vertex list |
103 | | * @return the octant of the segment at the vertex |
104 | | */ |
105 | | int getSegmentOctant(std::size_t index) const |
106 | 0 | { |
107 | 0 | if (index >= size() - 1) { |
108 | 0 | return -1; |
109 | 0 | } |
110 | 0 | return safeOctant(seq->getAt<geom::CoordinateXY>(index), |
111 | 0 | seq->getAt<geom::CoordinateXY>(index + 1)); |
112 | 0 | }; |
113 | | |
114 | 0 | static int getSegmentOctant(const SegmentString& ss, std::size_t index) { |
115 | 0 | return ss.getSegmentOctant(index); |
116 | 0 | } |
117 | | |
118 | | /** |
119 | | * Gets the next vertex in a ring from a vertex index. |
120 | | * |
121 | | * @param index the vertex index |
122 | | * @return the next vertex in the ring |
123 | | * |
124 | | * @see isClosed |
125 | | */ |
126 | | const geom::CoordinateXY& nextInRing(std::size_t index) const |
127 | 0 | { |
128 | 0 | std::size_t nextIndex = index + 1; |
129 | 0 | if (nextIndex > size() - 1) { |
130 | 0 | nextIndex = 1; |
131 | 0 | } |
132 | 0 | return getCoordinate(nextIndex); |
133 | 0 | } |
134 | | |
135 | | /** |
136 | | * Gets the previous vertex in a ring from a vertex index. |
137 | | * |
138 | | * @param index the vertex index |
139 | | * @return the previous vertex in the ring |
140 | | * |
141 | | * @see isClosed |
142 | | */ |
143 | | const geom::CoordinateXY& prevInRing(std::size_t index) const |
144 | 0 | { |
145 | 0 | std::size_t prevIndex; |
146 | 0 | if (index == 0) |
147 | 0 | prevIndex = size() - 2; |
148 | 0 | else |
149 | 0 | prevIndex = index - 1; |
150 | 0 | return getCoordinate( prevIndex ); |
151 | 0 | } |
152 | | |
153 | | |
154 | 0 | bool isClosed() const { |
155 | 0 | return seq->front<geom::CoordinateXY>().equals(seq->back<geom::CoordinateXY>()); |
156 | 0 | } |
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 | | |
164 | | protected: |
165 | | std::shared_ptr<const geom::CoordinateSequence> seq; |
166 | | |
167 | | private: |
168 | | static int safeOctant(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1) |
169 | 0 | { |
170 | 0 | if(p0.equals2D(p1)) { |
171 | 0 | return 0; |
172 | 0 | } |
173 | 0 | return Octant::octant(p0, p1); |
174 | 0 | }; |
175 | | |
176 | | // Declare type as noncopyable |
177 | | SegmentString(const SegmentString& other) = delete; |
178 | | SegmentString& operator=(const SegmentString& rhs) = delete; |
179 | | }; |
180 | | |
181 | | std::ostream& operator<< (std::ostream& os, const SegmentString& ss); |
182 | | |
183 | | } // namespace geos.noding |
184 | | } // namespace geos |
185 | | |