Fix TopologyPreservingSimplifier invalid output on closed line (#508)
[geos.git] / include / geos / simplify / TaggedLineStringSimplifier.h
bloba0d77f4495336b49028d9bebee410abb629a747d
1 /**********************************************************************
2 * $Id$
4 * GEOS - Geometry Engine Open Source
5 * http://geos.refractions.net
7 * Copyright (C) 2006 Refractions Research Inc.
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
14 **********************************************************************
16 * Last port: simplify/TaggedLineStringSimplifier.java r536 (JTS-1.12+)
18 **********************************************************************
20 * NOTES: This class can be optimized to work with vector<Coordinate*>
21 * rather then with CoordinateSequence
23 **********************************************************************/
25 #ifndef GEOS_SIMPLIFY_TAGGEDLINESTRINGSIMPLIFIER_H
26 #define GEOS_SIMPLIFY_TAGGEDLINESTRINGSIMPLIFIER_H
28 #include <geos/export.h>
29 #include <cstddef>
30 #include <vector>
31 #include <memory>
33 #ifdef _MSC_VER
34 #pragma warning(push)
35 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
36 #endif
38 // Forward declarations
39 namespace geos {
40 namespace algorithm {
41 class LineIntersector;
43 namespace geom {
44 class CoordinateSequence;
45 class LineSegment;
47 namespace simplify {
48 class TaggedLineSegment;
49 class TaggedLineString;
50 class LineSegmentIndex;
54 namespace geos {
55 namespace simplify { // geos::simplify
58 /** \brief
59 * Simplifies a TaggedLineString, preserving topology
60 * (in the sense that no new intersections are introduced).
61 * Uses the recursive Douglas-Peucker algorithm.
64 class GEOS_DLL TaggedLineStringSimplifier {
66 public:
68 TaggedLineStringSimplifier(LineSegmentIndex* inputIndex,
69 LineSegmentIndex* outputIndex);
71 /** \brief
72 * Sets the distance tolerance for the simplification.
74 * All vertices in the simplified geometry will be within this
75 * distance of the original geometry.
77 * @param d the approximation tolerance to use
79 void setDistanceTolerance(double d);
81 /**
82 * Simplifies the given {@link TaggedLineString}
83 * using the distance tolerance specified.
85 * @param line the linestring to simplify
87 void simplify(TaggedLineString* line);
90 private:
92 // externally owned
93 LineSegmentIndex* inputIndex;
95 // externally owned
96 LineSegmentIndex* outputIndex;
98 std::auto_ptr<algorithm::LineIntersector> li;
100 /// non-const as segments are possibly added to it
101 TaggedLineString* line;
103 const geom::CoordinateSequence* linePts;
105 double distanceTolerance;
107 void simplifySection(std::size_t i, std::size_t j,
108 std::size_t depth);
110 static std::size_t findFurthestPoint(
111 const geom::CoordinateSequence* pts,
112 std::size_t i, std::size_t j,
113 double& maxDistance);
115 bool hasBadIntersection(const TaggedLineString* parentLine,
116 const std::vector<std::size_t>& sectionIndex,
117 const geom::LineSegment& candidateSeg);
119 bool hasBadInputIntersection(const TaggedLineString* parentLine,
120 const std::vector<std::size_t>& sectionIndex,
121 const geom::LineSegment& candidateSeg);
123 bool hasBadOutputIntersection(const geom::LineSegment& candidateSeg);
125 bool hasInteriorIntersection(const geom::LineSegment& seg0,
126 const geom::LineSegment& seg1) const;
128 std::auto_ptr<TaggedLineSegment> flatten(
129 std::size_t start, std::size_t end);
131 /** \brief
132 * Tests whether a segment is in a section of a TaggedLineString
134 * @param line
135 * @param sectionIndex
136 * @param seg
137 * @return
139 static bool isInLineSection(
140 const TaggedLineString* parentLine,
141 const std::vector<std::size_t>& sectionIndex,
142 const TaggedLineSegment* seg);
144 /** \brief
145 * Remove the segs in the section of the line
147 * @param line
148 * @param pts
149 * @param sectionStartIndex
150 * @param sectionEndIndex
152 void remove(const TaggedLineString* line,
153 std::size_t start,
154 std::size_t end);
158 inline void
159 TaggedLineStringSimplifier::setDistanceTolerance(double d)
161 distanceTolerance = d;
164 } // namespace geos::simplify
165 } // namespace geos
167 #ifdef _MSC_VER
168 #pragma warning(pop)
169 #endif
171 #endif // GEOS_SIMPLIFY_TAGGEDLINESTRINGSIMPLIFIER_H