GEOS  3.13.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 #pragma once
21 
22 #include <geos/geom/Coordinate.h> // for inlines
23 #include <geos/geom/CoordinateSequence.h> // for inlines
24 #include <geos/geom/PrecisionModel.h> // for inlines
25 
26 #include <vector>
27 #include <memory>
28 #include <cassert>
29 
30 namespace geos {
31 namespace operation { // geos.operation
32 namespace buffer { // geos.operation.buffer
33 
42 
43 private:
44 
46 
47  const geom::PrecisionModel* precisionModel;
48 
55  double minimumVertexDistance;
56 
64  bool
65  isRedundant(const geom::Coordinate& pt) const
66  {
67  if(ptList->size() < 1) {
68  return false;
69  }
70  const geom::Coordinate& lastPt = ptList->back();
71  double ptDist = pt.distance(lastPt);
72  if(ptDist < minimumVertexDistance) {
73  return true;
74  }
75  return false;
76  }
77 
79  OffsetSegmentString& operator=(const OffsetSegmentString&) = delete;
80 
81 public:
82 
83  friend std::ostream& operator<< (std::ostream& os, const OffsetSegmentString& node);
84 
86  :
87  ptList(new geom::CoordinateSequence()),
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::CoordinateSequence();
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(std::size_t i = 0, n = pts.size(); i < n; ++i) {
146  addPt(pts[i]);
147  }
148  }
149  else {
150  for(std::size_t i = pts.size(); i > 0; --i) {
151  addPt(pts[i - 1]);
152  }
153  }
154  }
155 
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 
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 
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:216
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:88
double makePrecise(double val) const
Rounds a numeric value to the PrecisionModel grid.
Definition: OffsetSegmentString.h:41
void closeRing()
Definition: OffsetSegmentString.h:160
geom::CoordinateSequence * getCoordinates()
Definition: OffsetSegmentString.h:183
const T & front() const
Return first Coordinate in the sequence.
Definition: CoordinateSequence.h:356
const T & back() const
Return last Coordinate in the sequence.
Definition: CoordinateSequence.h:342
void add(const T &c)
Definition: CoordinateSequence.h:412
size_t size() const
Returns the number of Coordinates.
Definition: CoordinateSequence.h:185
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25