GEOS  3.14.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/geom/Coordinate.h>
25 #include <geos/geom/CoordinateSequence.h>
26 #include <geos/noding/Octant.h>
27 
28 #include <vector>
29 
30 // Forward declarations
31 namespace geos {
32 namespace algorithm {
33 class LineIntersector;
34 }
35 }
36 
37 namespace geos {
38 namespace noding { // geos.noding
39 
47 class GEOS_DLL SegmentString {
48 public:
49  typedef std::vector<const SegmentString*> ConstVect;
50  typedef std::vector<SegmentString*> NonConstVect;
51 
52  friend std::ostream& operator<< (std::ostream& os,
53  const SegmentString& ss);
54 
60  SegmentString(const void* newContext, geom::CoordinateSequence* newSeq)
61  :
62  seq(newSeq),
63  context(newContext)
64  {}
65 
66  virtual
67  ~SegmentString() {}
68 
74  const void*
75  getData() const
76  {
77  return context;
78  }
79 
85  void
86  setData(const void* data)
87  {
88  context = data;
89  }
90 
91  std::size_t size() const {
92  return seq->size();
93  }
94 
95  template<typename CoordType = geom::Coordinate>
96  const CoordType& getCoordinate(std::size_t i) const {
97  return seq->getAt<CoordType>(i);
98  }
99 
107  return seq;
108  }
109 
110  geom::CoordinateSequence* getCoordinates() {
111  return seq;
112  }
113 
121  int getSegmentOctant(std::size_t index) const
122  {
123  if (index >= size() - 1) {
124  return -1;
125  }
126  return safeOctant(seq->getAt<geom::CoordinateXY>(index),
127  seq->getAt<geom::CoordinateXY>(index + 1));
128  };
129 
130  static int getSegmentOctant(const SegmentString& ss, std::size_t index) {
131  return ss.getSegmentOctant(index);
132  }
133 
142  const geom::CoordinateXY& nextInRing(std::size_t index) const
143  {
144  std::size_t nextIndex = index + 1;
145  if (nextIndex > size() - 1) {
146  nextIndex = 1;
147  }
148  return getCoordinate(nextIndex);
149  }
150 
159  const geom::CoordinateXY& prevInRing(std::size_t index) const
160  {
161  std::size_t prevIndex;
162  if (index == 0)
163  prevIndex = size() - 2;
164  else
165  prevIndex = index - 1;
166  return getCoordinate( prevIndex );
167  }
168 
169 
170  bool isClosed() const {
171  return seq->front<geom::CoordinateXY>().equals(seq->back<geom::CoordinateXY>());
172  }
173 
174  virtual std::ostream& print(std::ostream& os) const;
175 
176 protected:
178 
179 private:
180  const void* context;
181 
182  static int safeOctant(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1)
183  {
184  if(p0.equals2D(p1)) {
185  return 0;
186  }
187  return Octant::octant(p0, p1);
188  };
189 
190  // Declare type as noncopyable
191  SegmentString(const SegmentString& other) = delete;
192  SegmentString& operator=(const SegmentString& rhs) = delete;
193 };
194 
195 std::ostream& operator<< (std::ostream& os, const SegmentString& ss);
196 
197 } // namespace geos.noding
198 } // namespace geos
199 
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
static int octant(double dx, double dy)
An interface for classes which represent a sequence of contiguous line segments.
Definition: SegmentString.h:47
SegmentString(const void *newContext, geom::CoordinateSequence *newSeq)
Construct a SegmentString.
Definition: SegmentString.h:60
const geom::CoordinateXY & nextInRing(std::size_t index) const
Definition: SegmentString.h:142
int getSegmentOctant(std::size_t index) const
Gets the octant of the segment starting at vertex index.
Definition: SegmentString.h:121
const geom::CoordinateXY & prevInRing(std::size_t index) const
Definition: SegmentString.h:159
const geom::CoordinateSequence * getCoordinates() const
Return a pointer to the CoordinateSequence associated with this SegmentString.
Definition: SegmentString.h:106
const void * getData() const
Gets the user-defined data for this segment string.
Definition: SegmentString.h:75
void setData(const void *data)
Sets the user-defined data for this segment string.
Definition: SegmentString.h:86
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25