Fix MCIndexSnapRounder use of provided precision model.
[geos.git] / src / noding / snapround / MCIndexSnapRounder.cpp
blobfd307819b6f68db8cec9816f03a91793186c77ea
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.refractions.net
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: noding/snapround/MCIndexSnapRounder.java r480 (JTS-1.12)
17 **********************************************************************/
19 #include <geos/noding/MCIndexNoder.h>
20 #include <geos/noding/snapround/MCIndexSnapRounder.h>
21 #include <geos/noding/snapround/HotPixel.h>
22 #include <geos/noding/IntersectionFinderAdder.h>
23 #include <geos/noding/NodingValidator.h>
24 #include <geos/geom/Coordinate.h>
25 #include <geos/geom/CoordinateSequence.h>
27 #include <geos/inline.h>
29 #include <functional> // std::mem_fun, std::bind1st
30 #include <algorithm> // std::for_each
31 #include <vector>
34 using namespace std;
35 using namespace geos::geom;
37 namespace geos {
38 namespace noding { // geos.noding
39 namespace snapround { // geos.noding.snapround
41 /*private*/
42 void
43 MCIndexSnapRounder::findInteriorIntersections(MCIndexNoder& noder,
44 NodedSegmentString::NonConstVect* segStrings,
45 vector<Coordinate>& intersections)
47 IntersectionFinderAdder intFinderAdder(li, intersections);
48 noder.setSegmentIntersector(&intFinderAdder);
49 noder.computeNodes(segStrings);
52 /* private */
53 void
54 MCIndexSnapRounder::computeIntersectionSnaps(vector<Coordinate>& snapPts)
56 for (vector<Coordinate>::iterator
57 it=snapPts.begin(), itEnd=snapPts.end();
58 it!=itEnd;
59 ++it)
61 Coordinate& snapPt = *it;
62 HotPixel hotPixel(snapPt, scaleFactor, li);
63 pointSnapper->snap(hotPixel);
67 /*private*/
68 void
69 MCIndexSnapRounder::computeVertexSnaps(NodedSegmentString* e)
71 CoordinateSequence& pts0 = *(e->getCoordinates());
72 for (unsigned int i=0, n=pts0.size()-1; i<n; ++i)
74 HotPixel hotPixel(pts0[i], scaleFactor, li);
75 bool isNodeAdded = pointSnapper->snap(hotPixel, e, i);
76 // if a node is created for a vertex, that vertex must be noded too
77 if (isNodeAdded) {
78 e->addIntersection(pts0[i], i);
83 /*public*/
84 void
85 MCIndexSnapRounder::computeVertexSnaps(SegmentString::NonConstVect& edges)
87 SegmentString::NonConstVect::iterator i=edges.begin(), e=edges.end();
88 for (; i!=e; ++i)
90 NodedSegmentString* edge0 =
91 dynamic_cast<NodedSegmentString*>(*i);
92 assert(edge0);
93 computeVertexSnaps(edge0);
97 /*private*/
98 void
99 MCIndexSnapRounder::snapRound(MCIndexNoder& noder,
100 SegmentString::NonConstVect* segStrings)
102 vector<Coordinate> intersections;
103 findInteriorIntersections(noder, segStrings, intersections);
104 computeIntersectionSnaps(intersections);
105 computeVertexSnaps(*segStrings);
109 /*public*/
110 void
111 MCIndexSnapRounder::computeNodes(SegmentString::NonConstVect* inputSegmentStrings)
113 nodedSegStrings = inputSegmentStrings;
114 MCIndexNoder noder;
115 pointSnapper.release(); // let it leak
116 pointSnapper.reset(new MCIndexPointSnapper(noder.getIndex()));
117 snapRound(noder, inputSegmentStrings);
119 // testing purposes only - remove in final version
120 assert(nodedSegStrings == inputSegmentStrings);
121 //checkCorrectness(*inputSegmentStrings);
124 /*private*/
125 void
126 MCIndexSnapRounder::checkCorrectness(
127 SegmentString::NonConstVect& inputSegmentStrings)
129 auto_ptr<SegmentString::NonConstVect> resultSegStrings(
130 NodedSegmentString::getNodedSubstrings(inputSegmentStrings)
133 NodingValidator nv(*(resultSegStrings.get()));
134 try {
135 nv.checkValid();
136 } catch (const std::exception &ex) {
137 std::cerr << ex.what() << std::endl;
138 throw;
143 } // namespace geos.noding.snapround
144 } // namespace geos.noding
145 } // namespace geos
147 /**********************************************************************
148 * $Log$
149 * Revision 1.11 2006/05/05 15:40:39 strk
150 * Had nodind validation error throw an exception for SimpleSnapRounder
151 * and MCIndexSnapRounder
153 * Revision 1.10 2006/04/03 10:44:19 strk
154 * Added missing headers
156 * Revision 1.9 2006/03/24 09:52:41 strk
157 * USE_INLINE => GEOS_INLINE
159 * Revision 1.8 2006/03/22 18:12:32 strk
160 * indexChain.h header split.
162 * Revision 1.7 2006/03/14 12:55:56 strk
163 * Headers split: geomgraphindex.h, nodingSnapround.h
165 * Revision 1.6 2006/03/03 10:46:21 strk
166 * Removed 'using namespace' from headers, added missing headers in .cpp files, removed useless includes in headers (bug#46)
168 * Revision 1.5 2006/02/23 11:54:20 strk
169 * - MCIndexPointSnapper
170 * - MCIndexSnapRounder
171 * - SnapRounding BufferOp
172 * - ScaledNoder
173 * - GEOSException hierarchy cleanups
174 * - SpatialIndex memory-friendly query interface
175 * - GeometryGraph::getBoundaryNodes memory-friendly
176 * - NodeMap::getBoundaryNodes memory-friendly
177 * - Cleanups in geomgraph::Edge
178 * - Added an XML test for snaprounding buffer (shows leaks, working on it)
180 * Revision 1.4 2006/02/21 16:53:49 strk
181 * MCIndexPointSnapper, MCIndexSnapRounder
183 * Revision 1.3 2006/02/19 19:46:49 strk
184 * Packages <-> namespaces mapping for most GEOS internal code (uncomplete, but working). Dir-level libs for index/ subdirs.
186 * Revision 1.2 2006/02/18 21:08:09 strk
187 * - new CoordinateSequence::applyCoordinateFilter method (slow but useful)
188 * - SegmentString::getCoordinates() doesn't return a clone anymore.
189 * - SegmentString::getCoordinatesRO() obsoleted.
190 * - SegmentString constructor does not promises constness of passed
191 * CoordinateSequence anymore.
192 * - NEW ScaledNoder class
193 * - Stubs for MCIndexPointSnapper and MCIndexSnapRounder
194 * - Simplified internal interaces of OffsetCurveBuilder and OffsetCurveSetBuilder
196 * Revision 1.1 2006/02/14 13:28:26 strk
197 * New SnapRounding code ported from JTS-1.7 (not complete yet).
198 * Buffer op optimized by using new snaprounding code.
199 * Leaks fixed in XMLTester.
201 **********************************************************************/