1 | /*
|
---|
2 | * The JTS Topology Suite is a collection of Java classes that
|
---|
3 | * implement the fundamental operations required to validate a given
|
---|
4 | * geo-spatial data set to a known topological specification.
|
---|
5 | *
|
---|
6 | * Copyright (C) 2001 Vivid Solutions
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Lesser General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2.1 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Lesser General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Lesser General Public
|
---|
19 | * License along with this library; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
21 | *
|
---|
22 | * For more information, contact:
|
---|
23 | *
|
---|
24 | * Vivid Solutions
|
---|
25 | * Suite #1A
|
---|
26 | * 2328 Government Street
|
---|
27 | * Victoria BC V8T 5G5
|
---|
28 | * Canada
|
---|
29 | *
|
---|
30 | * (250)385-6040
|
---|
31 | * www.vividsolutions.com
|
---|
32 | */
|
---|
33 | package com.vividsolutions.jts.noding;
|
---|
34 |
|
---|
35 | import java.util.ArrayList;
|
---|
36 | import java.util.List;
|
---|
37 |
|
---|
38 | import com.vividsolutions.jts.geom.*;
|
---|
39 | import com.vividsolutions.jts.algorithm.LineIntersector;
|
---|
40 | //import com.vividsolutions.jts.util.Debug;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Finds an interior intersection in a set of {@link SegmentString}s,
|
---|
44 | * if one exists. Only the first intersection found is reported.
|
---|
45 | *
|
---|
46 | * @version 1.7
|
---|
47 | */
|
---|
48 | public class InteriorIntersectionFinder
|
---|
49 | implements SegmentIntersector
|
---|
50 | {
|
---|
51 | private boolean findAllIntersections = false;
|
---|
52 | private boolean isCheckEndSegmentsOnly = false;
|
---|
53 | private LineIntersector li;
|
---|
54 | private Coordinate interiorIntersection = null;
|
---|
55 | private Coordinate[] intSegments = null;
|
---|
56 | private List intersections = new ArrayList();
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Creates an intersection finder which finds an interior intersection
|
---|
60 | * if one exists
|
---|
61 | *
|
---|
62 | * @param li the LineIntersector to use
|
---|
63 | */
|
---|
64 | public InteriorIntersectionFinder(LineIntersector li)
|
---|
65 | {
|
---|
66 | this.li = li;
|
---|
67 | interiorIntersection = null;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void setFindAllIntersections(boolean findAllIntersections)
|
---|
71 | {
|
---|
72 | this.findAllIntersections = findAllIntersections;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Tests whether an intersection was found.
|
---|
78 | *
|
---|
79 | * @return true if an intersection was found
|
---|
80 | */
|
---|
81 | public boolean hasIntersection()
|
---|
82 | {
|
---|
83 | return interiorIntersection != null;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Gets the computed location of the intersection.
|
---|
88 | * Due to round-off, the location may not be exact.
|
---|
89 | *
|
---|
90 | * @return the coordinate for the intersection location
|
---|
91 | */
|
---|
92 | public Coordinate getInteriorIntersection()
|
---|
93 | {
|
---|
94 | return interiorIntersection;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Gets the endpoints of the intersecting segments.
|
---|
99 | *
|
---|
100 | * @return an array of the segment endpoints (p00, p01, p10, p11)
|
---|
101 | */
|
---|
102 | public Coordinate[] getIntersectionSegments()
|
---|
103 | {
|
---|
104 | return intSegments;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * This method is called by clients
|
---|
109 | * of the {@link SegmentIntersector} class to process
|
---|
110 | * intersections for two segments of the {@link SegmentString}s being intersected.
|
---|
111 | * Note that some clients (such as {@link MonotoneChain}s) may optimize away
|
---|
112 | * this call for segment pairs which they have determined do not intersect
|
---|
113 | * (e.g. by an disjoint envelope test).
|
---|
114 | */
|
---|
115 | public void processIntersections(
|
---|
116 | SegmentString e0, int segIndex0,
|
---|
117 | SegmentString e1, int segIndex1
|
---|
118 | )
|
---|
119 | {
|
---|
120 | // short-circuit if intersection already found
|
---|
121 | if (hasIntersection())
|
---|
122 | return;
|
---|
123 |
|
---|
124 | // don't bother intersecting a segment with itself
|
---|
125 | if (e0 == e1 && segIndex0 == segIndex1) return;
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * If enabled, only test end segments (on either segString).
|
---|
129 | *
|
---|
130 | */
|
---|
131 | if (isCheckEndSegmentsOnly) {
|
---|
132 | boolean isEndSegPresent = isEndSegment(e0, segIndex0) || isEndSegment(e1, segIndex1);
|
---|
133 | if (! isEndSegPresent)
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | Coordinate p00 = e0.getCoordinates()[segIndex0];
|
---|
138 | Coordinate p01 = e0.getCoordinates()[segIndex0 + 1];
|
---|
139 | Coordinate p10 = e1.getCoordinates()[segIndex1];
|
---|
140 | Coordinate p11 = e1.getCoordinates()[segIndex1 + 1];
|
---|
141 |
|
---|
142 | li.computeIntersection(p00, p01, p10, p11);
|
---|
143 | //if (li.hasIntersection() && li.isProper()) Debug.println(li);
|
---|
144 |
|
---|
145 | if (li.hasIntersection()) {
|
---|
146 | if (li.isInteriorIntersection()) {
|
---|
147 | intSegments = new Coordinate[4];
|
---|
148 | intSegments[0] = p00;
|
---|
149 | intSegments[1] = p01;
|
---|
150 | intSegments[2] = p10;
|
---|
151 | intSegments[3] = p11;
|
---|
152 |
|
---|
153 | interiorIntersection = li.getIntersection(0);
|
---|
154 | intersections.add(interiorIntersection);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Tests whether a segment in a {@link SegmentString} is an end segment.
|
---|
161 | * (either the first or last).
|
---|
162 | *
|
---|
163 | * @param segStr a segment string
|
---|
164 | * @param index the index of a segment in the segment string
|
---|
165 | * @return true if the segment is an end segment
|
---|
166 | */
|
---|
167 | private boolean isEndSegment(SegmentString segStr, int index)
|
---|
168 | {
|
---|
169 | if (index == 0) return true;
|
---|
170 | if (index >= segStr.size() - 2) return true;
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public boolean isDone()
|
---|
175 | {
|
---|
176 | if (findAllIntersections) return false;
|
---|
177 | return interiorIntersection != null;
|
---|
178 | }
|
---|
179 | }
|
---|