File: TaggedLineString.cpp

package info (click to toggle)
geos 3.4.2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,572 kB
  • ctags: 13,989
  • sloc: cpp: 97,030; xml: 32,337; sh: 10,379; ansic: 3,726; php: 1,831; makefile: 1,619; ruby: 1,295; python: 928
file content (253 lines) | stat: -rw-r--r-- 5,057 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**********************************************************************
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.osgeo.org
 *
 * Copyright (C) 2006 Refractions Research Inc.
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Licence as published
 * by the Free Software Foundation. 
 * See the COPYING file for more information.
 *
 **********************************************************************
 *
 * Last port: simplify/TaggedLineString.java rev. 1.2 (JTS-1.7.1)
 *
 **********************************************************************/

#include <geos/simplify/TaggedLineString.h>
#include <geos/simplify/TaggedLineSegment.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/LineString.h>
#include <geos/geom/Geometry.h> // for auto_ptr destructor 
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/CoordinateSequenceFactory.h>

#include <cassert>
#include <memory>
#include <cstddef>

#ifndef GEOS_DEBUG
#define GEOS_DEBUG 0
#endif

#ifdef GEOS_DEBUG
#include <iostream>
#endif

using namespace geos::geom;
using namespace std;

namespace geos {
namespace simplify { // geos::simplify

using namespace std;

/*public*/
TaggedLineString::TaggedLineString(const geom::LineString* nParentLine,
			std::size_t nMinimumSize)
	:
	parentLine(nParentLine),
	minimumSize(nMinimumSize)
{
	init();
}

/*public*/
TaggedLineString::~TaggedLineString()
{
#if GEOS_DEBUG
	cerr << "TaggedLineString[" << this << "] destructor"
	     << endl;
#endif

	for (std::size_t i=0, n=segs.size(); i<n; i++)
		delete segs[i];

	for (std::size_t i=0, n=resultSegs.size(); i<n; i++)
		delete resultSegs[i];
}

/*private*/
void
TaggedLineString::init()
{
	assert(parentLine);
	const CoordinateSequence* pts = parentLine->getCoordinatesRO();

#if GEOS_DEBUG
	cerr << "TaggedLineString[" << this << "] pts.size() " << pts->size()
	     << endl;
#endif

	if ( pts->size() )
	{

		segs.reserve(pts->size()-1);

		for (std::size_t i=0, n=pts->size()-1; i<n; i++)
		{
			TaggedLineSegment* seg = new TaggedLineSegment(
					pts->getAt(i),
					pts->getAt(i+1),
					parentLine, i);

			segs.push_back(seg);
		}

	}

#if GEOS_DEBUG
	cerr << "TaggedLineString[" << this << "] segs.size " << segs.size()
	    << endl;
	cerr << "TaggedLineString[" << this << "] resultSegs.size " << resultSegs.size()
	    << endl;
#endif
}

/*public*/
std::size_t
TaggedLineString::getMinimumSize() const
{
	return minimumSize;
}

/*public*/
const geom::LineString* 
TaggedLineString::getParent() const
{
	return parentLine;
}

/*public*/
const CoordinateSequence*
TaggedLineString::getParentCoordinates() const
{
	assert(parentLine);
	return parentLine->getCoordinatesRO();
}

/*public*/
CoordinateSequence::AutoPtr
TaggedLineString::getResultCoordinates() const
{

#if GEOS_DEBUG
	cerr << __FUNCTION__ << " resultSegs.size: "
	     << resultSegs.size() << endl;
#endif

	CoordVectPtr pts = extractCoordinates(resultSegs);

#if GEOS_DEBUG
	cerr << __FUNCTION__ << " extracted Coords.size: "
	     << pts->size() << endl;
#endif


	CoordVect* v = pts.release();
	return CoordinateSequence::AutoPtr(parentLine->getFactory()->getCoordinateSequenceFactory()->create(v));

}

/*private static*/
TaggedLineString::CoordVectPtr
TaggedLineString::extractCoordinates(
		const std::vector<TaggedLineSegment*>& segs)
{
	CoordVectPtr pts(new CoordVect());

#if GEOS_DEBUG
	cerr << __FUNCTION__ << " segs.size: " << segs.size() << endl;
#endif

	std::size_t i=0, size=segs.size();

	if ( size ) {
		for (; i<size; i++)
		{
			TaggedLineSegment* seg = segs[i];
			assert(seg);
			pts->push_back(seg->p0);
		}

		// add last point
		pts->push_back(segs[size-1]->p1);
	}

	return pts;
}

/*public*/
std::size_t
TaggedLineString::getResultSize() const
{
	unsigned resultSegsSize = resultSegs.size();
	return resultSegsSize == 0 ? 0 : resultSegsSize + 1;
}

/*public*/
TaggedLineSegment*
TaggedLineString::getSegment(std::size_t i) 
{
	return segs[i];
}

/*public*/
const TaggedLineSegment*
TaggedLineString::getSegment(std::size_t i) const
{
	return segs[i];
}

/*public*/
vector<TaggedLineSegment*>&
TaggedLineString::getSegments()
{
	assert(0);
	return segs;
}

/*public*/
const vector<TaggedLineSegment*>&
TaggedLineString::getSegments() const
{
	return segs;
}

/*public*/
auto_ptr<Geometry>
TaggedLineString::asLineString() const
{
	return parentLine->getFactory()->createLineString(
			getResultCoordinates());
}

/*public*/
auto_ptr<Geometry>
TaggedLineString::asLinearRing() const
{
	return parentLine->getFactory()->createLinearRing(
			getResultCoordinates());
}

/*public*/
void
TaggedLineString::addToResult(auto_ptr<TaggedLineSegment> seg)
{
#if GEOS_DEBUG
	cerr << "TaggedLineString[" << this << "] adding "
	     << " seg " << seg.get() << " to result"
	     << endl;
#endif
	resultSegs.push_back(seg.release());
#if GEOS_DEBUG
	cerr << "TaggedLineString[" << this << "] adding "
	     << " seg " << seg.get() << " to result"
	     << endl;
#endif
}

} // namespace geos::simplify
} // namespace geos