/src/geos/src/noding/snap/SnappingIntersectionAdder.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca> |
7 | | * |
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. |
12 | | * |
13 | | **********************************************************************/ |
14 | | |
15 | | #include <geos/noding/snap/SnappingIntersectionAdder.h> |
16 | | #include <geos/noding/snap/SnappingPointIndex.h> |
17 | | #include <geos/noding/SegmentString.h> |
18 | | #include <geos/noding/NodedSegmentString.h> |
19 | | #include <geos/geom/Coordinate.h> |
20 | | #include <geos/geom/CoordinateSequence.h> |
21 | | #include <geos/algorithm/LineIntersector.h> |
22 | | #include <geos/algorithm/Distance.h> |
23 | | #include <geos/util.h> |
24 | | |
25 | | #include <vector> |
26 | | #include <exception> |
27 | | #include <cmath> |
28 | | #include <iostream> |
29 | | |
30 | | using namespace geos::algorithm; |
31 | | using namespace geos::geom; |
32 | | |
33 | | namespace geos { |
34 | | namespace noding { // geos.noding |
35 | | namespace snap { // geos.noding.snap |
36 | | |
37 | | |
38 | | SnappingIntersectionAdder::SnappingIntersectionAdder(double p_snapTolerance, SnappingPointIndex& p_snapPointIndex) |
39 | 337k | : SegmentIntersector() |
40 | 337k | , snapTolerance(p_snapTolerance) |
41 | 337k | , snapPointIndex(p_snapPointIndex) {} |
42 | | |
43 | | |
44 | | /*public*/ |
45 | | void |
46 | | SnappingIntersectionAdder::processIntersections(SegmentString* seg0, std::size_t segIndex0, SegmentString* seg1, std::size_t segIndex1) |
47 | 104M | { |
48 | | // don't bother intersecting a segment with itself |
49 | 104M | if (seg0 == seg1 && segIndex0 == segIndex1) return; |
50 | | |
51 | 104M | const Coordinate& p00 = seg0->getCoordinate(segIndex0); |
52 | 104M | const Coordinate& p01 = seg0->getCoordinate(segIndex0 + 1); |
53 | 104M | const Coordinate& p10 = seg1->getCoordinate(segIndex1); |
54 | 104M | const Coordinate& p11 = seg1->getCoordinate(segIndex1 + 1); |
55 | | |
56 | | /** |
57 | | * Don't node intersections which are just |
58 | | * due to the shared vertex of adjacent segments. |
59 | | */ |
60 | 104M | if (!isAdjacent(seg0, segIndex0, seg1, segIndex1)) { |
61 | 102M | li.computeIntersection(p00, p01, p10, p11); |
62 | | /** |
63 | | * Process single point intersections only. |
64 | | * Two-point (collinear) ones are handled by the near-vertex code |
65 | | */ |
66 | 102M | if (li.hasIntersection() && li.getIntersectionNum() == 1) { |
67 | | |
68 | 35.7M | const auto& intPt = li.getIntersection(0); |
69 | 35.7M | const auto& snapPt = snapPointIndex.snap(intPt); |
70 | | |
71 | 35.7M | static_cast<NodedSegmentString*>(seg0)->addIntersection(snapPt, segIndex0); |
72 | 35.7M | static_cast<NodedSegmentString*>(seg1)->addIntersection(snapPt, segIndex1); |
73 | 35.7M | } |
74 | 102M | } |
75 | | |
76 | | /** |
77 | | * The segments must also be snapped to the other segment endpoints. |
78 | | */ |
79 | 104M | processNearVertex(seg0, segIndex0, p00, seg1, segIndex1, p10, p11 ); |
80 | 104M | processNearVertex(seg0, segIndex0, p01, seg1, segIndex1, p10, p11 ); |
81 | 104M | processNearVertex(seg1, segIndex1, p10, seg0, segIndex0, p00, p01 ); |
82 | 104M | processNearVertex(seg1, segIndex1, p11, seg0, segIndex0, p00, p01 ); |
83 | 104M | } |
84 | | |
85 | | |
86 | | /*private*/ |
87 | | void |
88 | | SnappingIntersectionAdder::processNearVertex(SegmentString* srcSS, std::size_t srcIndex, const geom::Coordinate& p, |
89 | | SegmentString* ss, std::size_t segIndex, const geom::Coordinate& p0, const geom::Coordinate& p1) |
90 | 419M | { |
91 | | /** |
92 | | * Don't add intersection if candidate vertex is near endpoints of segment. |
93 | | * This avoids creating "zig-zag" linework |
94 | | * (since the vertex could actually be outside the segment envelope). |
95 | | * Also, this should have already been snapped. |
96 | | */ |
97 | 419M | if (p.distance(p0) < snapTolerance) return; |
98 | 310M | if (p.distance(p1) < snapTolerance) return; |
99 | | |
100 | 200M | double distSeg = algorithm::Distance::pointToSegment(p, p0, p1); |
101 | 200M | if (distSeg < snapTolerance) { |
102 | | // add node to target segment |
103 | 8.97M | static_cast<NodedSegmentString*>(ss)->addIntersection(p, segIndex); |
104 | | // add node at vertex to source SS |
105 | 8.97M | static_cast<NodedSegmentString*>(srcSS)->addIntersection(p, srcIndex); |
106 | 8.97M | } |
107 | 200M | } |
108 | | |
109 | | |
110 | | /*private static*/ |
111 | | bool |
112 | | SnappingIntersectionAdder::isAdjacent(SegmentString* ss0, std::size_t segIndex0, SegmentString* ss1, std::size_t segIndex1) |
113 | 104M | { |
114 | 104M | if (ss0 != ss1) return false; |
115 | 87.2M | long l0 = static_cast<long>(segIndex0); |
116 | 87.2M | long l1 = static_cast<long>(segIndex1); |
117 | | |
118 | 87.2M | bool isAdjacent = (std::abs(l0 - l1) == 1); |
119 | 87.2M | if (isAdjacent) { |
120 | 2.58M | return true; |
121 | 2.58M | } |
122 | 84.6M | if (ss0->isClosed()) { |
123 | 81.4M | std::size_t maxSegIndex = ss0->size(); |
124 | 81.4M | if ((segIndex0 == 0 && (segIndex1 + 1) == maxSegIndex) || |
125 | 81.4M | (segIndex1 == 0 && (segIndex0 + 1) == maxSegIndex)) { |
126 | 0 | return true; |
127 | 0 | } |
128 | 81.4M | } |
129 | 84.6M | return false; |
130 | 84.6M | } |
131 | | |
132 | | |
133 | | |
134 | | |
135 | | } // namespace geos.noding.snap |
136 | | } // namespace geos.noding |
137 | | } // namespace geos |