GEOS  3.8.0dev
OffsetSegmentString.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) 2007 Refractions Research Inc.
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU Lesser General Public Licence as published
11  * by the Free Software Foundation.
12  * See the COPYING file for more information.
13  *
14  **********************************************************************
15  *
16  * Last port: operation/buffer/OffsetSegmentString.java r378 (JTS-1.12)
17  *
18  **********************************************************************/
19 
20 #ifndef GEOS_OP_BUFFER_OFFSETSEGMENTSTRING_H
21 #define GEOS_OP_BUFFER_OFFSETSEGMENTSTRING_H
22 
23 #include <geos/geom/Coordinate.h> // for inlines
24 #include <geos/geom/CoordinateSequence.h> // for inlines
25 #include <geos/geom/CoordinateArraySequence.h> // for composition
26 #include <geos/geom/PrecisionModel.h> // for inlines
27 
28 #include <vector>
29 #include <memory>
30 #include <cassert>
31 
32 namespace geos {
33 namespace operation { // geos.operation
34 namespace buffer { // geos.operation.buffer
35 
37 //
44 
45 private:
46 
48 
49  const geom::PrecisionModel* precisionModel;
50 
57  double minimumVertexDistance;
58 
66  bool
67  isRedundant(const geom::Coordinate& pt) const
68  {
69  if(ptList->size() < 1) {
70  return false;
71  }
72  const geom::Coordinate& lastPt = ptList->back();
73  double ptDist = pt.distance(lastPt);
74  if(ptDist < minimumVertexDistance) {
75  return true;
76  }
77  return false;
78  }
79 
80 
81 public:
82 
83  friend std::ostream& operator<< (std::ostream& os, const OffsetSegmentString& node);
84 
86  :
87  ptList(new geom::CoordinateArraySequence()),
88  precisionModel(nullptr),
89  minimumVertexDistance(0.0)
90  {
91  }
92 
94  {
95  delete ptList;
96  }
97 
98  void
99  reset()
100  {
101  if(ptList) {
102  ptList->clear();
103  }
104  else {
105  ptList = new geom::CoordinateArraySequence();
106  }
107 
108  precisionModel = nullptr;
109  minimumVertexDistance = 0.0;
110  }
111 
112  void
113  setPrecisionModel(const geom::PrecisionModel* nPrecisionModel)
114  {
115  precisionModel = nPrecisionModel;
116  }
117 
118  void
119  setMinimumVertexDistance(double nMinVertexDistance)
120  {
121  minimumVertexDistance = nMinVertexDistance;
122  }
123 
124  void
125  addPt(const geom::Coordinate& pt)
126  {
127  assert(precisionModel);
128 
129  geom::Coordinate bufPt = pt;
130  precisionModel->makePrecise(bufPt);
131  // don't add duplicate (or near-duplicate) points
132  if(isRedundant(bufPt)) {
133  return;
134  }
135  // we ask to allow repeated as we checked this ourself
136  // (JTS uses a vector for ptList, not a CoordinateSequence,
137  // we should do the same)
138  ptList->add(bufPt, true);
139  }
140 
141  void
142  addPts(const geom::CoordinateSequence& pts, bool isForward)
143  {
144  if(isForward) {
145  for(size_t i = 0, n = pts.size(); i < n; ++i) {
146  addPt(pts[i]);
147  }
148  }
149  else {
150  for(size_t i = pts.size(); i > 0; --i) {
151  addPt(pts[i - 1]);
152  }
153  }
154  }
155 
157  //
159  void
161  {
162  if(ptList->size() < 1) {
163  return;
164  }
165  const geom::Coordinate& startPt = ptList->front();
166  const geom::Coordinate& lastPt = ptList->back();
167  if(startPt.equals(lastPt)) {
168  return;
169  }
170  // we ask to allow repeated as we checked this ourself
171  ptList->add(startPt, true);
172  }
173 
175  //
184  {
185  closeRing();
186  geom::CoordinateSequence* ret = ptList;
187  ptList = nullptr;
188  return ret;
189  }
190 
191  inline size_t
192  size() const
193  {
194  return ptList ? ptList->size() : 0 ;
195  }
196 
197 };
198 
199 inline std::ostream&
200 operator<< (std::ostream& os,
201  const OffsetSegmentString& lst)
202 {
203  if(lst.ptList) {
204  os << *(lst.ptList);
205  }
206  else {
207  os << "empty (consumed?)";
208  }
209  return os;
210 }
211 
212 } // namespace geos.operation.buffer
213 } // namespace geos.operation
214 } // namespace geos
215 
216 
217 #endif // ndef GEOS_OP_BUFFER_OFFSETSEGMENTSTRING_H
218 
The default implementation of CoordinateSequence.
Definition: CoordinateArraySequence.h:37
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
A dynamic list of the vertices in a constructed offset curve.
Definition: OffsetSegmentString.h:43
const Coordinate & back() const
Return last Coordinate in the sequence.
Definition: CoordinateSequence.h:88
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:87
void closeRing()
Check that points are a ring.
Definition: OffsetSegmentString.h:160
geom::CoordinateSequence * getCoordinates()
Get coordinates by taking ownership of them.
Definition: OffsetSegmentString.h:183
void add(const Coordinate &c)
Add a Coordinate to the list.
bool equals(const Coordinate &other) const
2D only
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
double makePrecise(double val) const
Rounds a numeric value to the PrecisionModel grid.
double distance(const Coordinate &p) const
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
void clear()
Reset this CoordinateArraySequence to the empty state.
Definition: CoordinateArraySequence.h:82
const Coordinate & front() const
Return first Coordinate in the sequence.
Definition: CoordinateSequence.h:95