Quotes around otherwise ambiguous (underline containing) name
[geos.git] / src / simplify / TaggedLineString.cpp
blob1aa7c7ede85b76129a31969369673a7f3d976452
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2006 Refractions Research Inc.
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
13 **********************************************************************
15 * Last port: simplify/TaggedLineString.java rev. 1.2 (JTS-1.7.1)
17 **********************************************************************/
19 #include <geos/simplify/TaggedLineString.h>
20 #include <geos/simplify/TaggedLineSegment.h>
21 #include <geos/geom/CoordinateSequence.h>
22 #include <geos/geom/LineString.h>
23 #include <geos/geom/Geometry.h> // for auto_ptr destructor
24 #include <geos/geom/GeometryFactory.h>
25 #include <geos/geom/CoordinateSequenceFactory.h>
27 #include <cassert>
28 #include <memory>
30 #ifndef GEOS_DEBUG
31 #define GEOS_DEBUG 0
32 #endif
34 #ifdef GEOS_DEBUG
35 #include <iostream>
36 #endif
38 using namespace geos::geom;
39 using namespace std;
41 namespace geos {
42 namespace simplify { // geos::simplify
44 /*public*/
45 TaggedLineString::TaggedLineString(const geom::LineString* nParentLine,
46 std::size_t nMinimumSize)
48 parentLine(nParentLine),
49 minimumSize(nMinimumSize)
51 init();
54 /*public*/
55 TaggedLineString::~TaggedLineString()
57 #if GEOS_DEBUG
58 cerr << "TaggedLineString[" << this << "] destructor"
59 << endl;
60 #endif
62 for (std::size_t i=0, n=segs.size(); i<n; i++)
63 delete segs[i];
65 for (std::size_t i=0, n=resultSegs.size(); i<n; i++)
66 delete resultSegs[i];
69 /*private*/
70 void
71 TaggedLineString::init()
73 assert(parentLine);
74 const CoordinateSequence* pts = parentLine->getCoordinatesRO();
76 #if GEOS_DEBUG
77 cerr << "TaggedLineString[" << this << "] pts.size() " << pts->size()
78 << endl;
79 #endif
81 if ( pts->size() )
84 segs.reserve(pts->size()-1);
86 for (std::size_t i=0, n=pts->size()-1; i<n; i++)
88 TaggedLineSegment* seg = new TaggedLineSegment(
89 pts->getAt(i),
90 pts->getAt(i+1),
91 parentLine, i);
93 segs.push_back(seg);
98 #if GEOS_DEBUG
99 cerr << "TaggedLineString[" << this << "] segs.size " << segs.size()
100 << endl;
101 cerr << "TaggedLineString[" << this << "] resultSegs.size " << resultSegs.size()
102 << endl;
103 #endif
106 /*public*/
107 std::size_t
108 TaggedLineString::getMinimumSize() const
110 return minimumSize;
113 /*public*/
114 const geom::LineString*
115 TaggedLineString::getParent() const
117 return parentLine;
120 /*public*/
121 const CoordinateSequence*
122 TaggedLineString::getParentCoordinates() const
124 assert(parentLine);
125 return parentLine->getCoordinatesRO();
128 /*public*/
129 CoordinateSequence::AutoPtr
130 TaggedLineString::getResultCoordinates() const
133 #if GEOS_DEBUG
134 cerr << __FUNCTION__ << " resultSegs.size: "
135 << resultSegs.size() << endl;
136 #endif
138 CoordVectPtr pts = extractCoordinates(resultSegs);
140 #if GEOS_DEBUG
141 cerr << __FUNCTION__ << " extracted Coords.size: "
142 << pts->size() << endl;
143 #endif
146 CoordVect* v = pts.release();
147 return CoordinateSequence::AutoPtr(parentLine->getFactory()->getCoordinateSequenceFactory()->create(v));
151 /*private static*/
152 TaggedLineString::CoordVectPtr
153 TaggedLineString::extractCoordinates(
154 const std::vector<TaggedLineSegment*>& segs)
156 CoordVectPtr pts(new CoordVect());
158 #if GEOS_DEBUG
159 cerr << __FUNCTION__ << " segs.size: " << segs.size() << endl;
160 #endif
162 std::size_t i=0, size=segs.size();
164 if ( size ) {
165 for (; i<size; i++)
167 TaggedLineSegment* seg = segs[i];
168 assert(seg);
169 pts->push_back(seg->p0);
172 // add last point
173 pts->push_back(segs[size-1]->p1);
176 return pts;
179 /*public*/
180 std::size_t
181 TaggedLineString::getResultSize() const
183 unsigned resultSegsSize = resultSegs.size();
184 return resultSegsSize == 0 ? 0 : resultSegsSize + 1;
187 /*public*/
188 TaggedLineSegment*
189 TaggedLineString::getSegment(std::size_t i)
191 return segs[i];
194 /*public*/
195 const TaggedLineSegment*
196 TaggedLineString::getSegment(std::size_t i) const
198 return segs[i];
201 /*public*/
202 vector<TaggedLineSegment*>&
203 TaggedLineString::getSegments()
205 assert(0);
206 return segs;
209 /*public*/
210 const vector<TaggedLineSegment*>&
211 TaggedLineString::getSegments() const
213 return segs;
216 /*public*/
217 auto_ptr<Geometry>
218 TaggedLineString::asLineString() const
220 return parentLine->getFactory()->createLineString(
221 getResultCoordinates());
224 /*public*/
225 auto_ptr<Geometry>
226 TaggedLineString::asLinearRing() const
228 return parentLine->getFactory()->createLinearRing(
229 getResultCoordinates());
232 /*public*/
233 void
234 TaggedLineString::addToResult(auto_ptr<TaggedLineSegment> seg)
236 #if GEOS_DEBUG
237 cerr << "TaggedLineString[" << this << "] adding "
238 << " seg " << seg.get() << " to result"
239 << endl;
240 #endif
241 resultSegs.push_back(seg.release());
242 #if GEOS_DEBUG
243 cerr << "TaggedLineString[" << this << "] adding "
244 << " seg " << seg.get() << " to result"
245 << endl;
246 #endif
249 } // namespace geos::simplify
250 } // namespace geos