Revision 30313
branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/app/project/documents/view/toolListeners/snapping/snappers/IntersectionPointSnapper.java | ||
---|---|---|
24 | 24 |
*/ |
25 | 25 |
public class IntersectionPointSnapper extends AbstractSnapper |
26 | 26 |
implements ISnapperGeometriesVectorial { |
27 |
private Geometry[] geometries; |
|
27 |
private static int maxPointsGeom=200; |
|
28 |
private Geometry[] geometries; |
|
28 | 29 |
public IntersectionPointSnapper() { |
29 | 30 |
System.err.println("Construido IntersectionPoinSnapper"); |
30 | 31 |
} |
... | ... | |
45 | 46 |
} |
46 | 47 |
|
47 | 48 |
for (int i = 0; i < geometries.length; i++) { |
48 |
Point2D r = intersects2(geom, geometries[i], point, tolerance);
|
|
49 |
Point2D r = intersects(geom, geometries[i], point, tolerance); |
|
49 | 50 |
|
50 | 51 |
if (r != null) { |
51 | 52 |
result = r; |
... | ... | |
55 | 56 |
return result; |
56 | 57 |
} |
57 | 58 |
|
58 |
private Point2D intersects2(Geometry g1, Geometry g2, Point2D point,
|
|
59 |
private Point2D intersects(Geometry g1, Geometry g2, Point2D point, |
|
59 | 60 |
double tolerance) { |
60 |
com.vividsolutions.jts.geom.Geometry intersection=Converter.geometryToJts(g1).intersection(Converter.geometryToJts(g2)); |
|
61 |
com.vividsolutions.jts.geom.Geometry g1JTS=Converter.geometryToJts(g1); |
|
62 |
com.vividsolutions.jts.geom.Geometry g2JTS=Converter.geometryToJts(g2); |
|
63 |
if (g1JTS.getNumPoints()>maxPointsGeom || g2JTS.getNumPoints()>maxPointsGeom){ |
|
64 |
return null; |
|
65 |
} |
|
66 |
com.vividsolutions.jts.geom.Geometry intersection=g1JTS.intersection(g2JTS); |
|
61 | 67 |
Geometry g =null; |
62 | 68 |
try { |
63 | 69 |
g = Converter.jtsToGeometry(intersection); |
... | ... | |
70 | 76 |
return null; |
71 | 77 |
} |
72 | 78 |
|
73 |
/** |
|
74 |
* DOCUMENT ME! |
|
75 |
* |
|
76 |
* @param g1 DOCUMENT ME! |
|
77 |
* @param g2 DOCUMENT ME! |
|
78 |
* @param point DOCUMENT ME! |
|
79 |
* @param tolerance DOCUMENT ME! |
|
80 |
* |
|
81 |
* @return DOCUMENT ME! |
|
82 |
*/ |
|
83 |
private Point2D intersects(Geometry g1, Geometry g2, Point2D point, |
|
84 |
double tolerance) { |
|
85 |
Point2D resul = null; |
|
86 |
Coordinate c = new Coordinate(point.getX(), point.getY()); |
|
87 |
PathIterator theIterator = g1.getPathIterator(null, Converter.FLATNESS); |
|
88 |
double[] theData = new double[6]; |
|
89 |
Coordinate from = null; |
|
90 |
Coordinate first = null; |
|
91 |
LineSegment[] lines = getLines(g2); |
|
92 |
while (!theIterator.isDone()) { |
|
93 |
int theType = theIterator.currentSegment(theData); |
|
94 |
|
|
95 |
switch (theType) { |
|
96 |
case PathIterator.SEG_MOVETO: |
|
97 |
from = new Coordinate(theData[0], theData[1]); |
|
98 |
first = from; |
|
99 |
|
|
100 |
break; |
|
101 |
|
|
102 |
case PathIterator.SEG_LINETO: |
|
103 |
|
|
104 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
|
105 |
LineSegment segmentLine = new LineSegment(from,to); |
|
106 |
for (int i = 0; i < lines.length; i++) { |
|
107 |
// if (lines[i].equals(segmentLine)) { |
|
108 |
// continue; |
|
109 |
// } |
|
110 |
Coordinate intersects = segmentLine.intersection(lines[i]); |
|
111 |
if (intersects == null || lines[i].equals(segmentLine)) { |
|
112 |
continue; |
|
113 |
} |
|
114 |
|
|
115 |
double dist = c.distance(intersects); |
|
116 |
|
|
117 |
if ((dist < tolerance)) { |
|
118 |
resul = new Point2D.Double(intersects.x, intersects.y); |
|
119 |
return resul; |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 |
from = to; |
|
124 |
|
|
125 |
break; |
|
126 |
|
|
127 |
case PathIterator.SEG_CLOSE: |
|
128 |
LineSegment segment = new LineSegment(from,first); |
|
129 |
|
|
130 |
for (int i = 0; i < lines.length; i++) { |
|
131 |
// if (lines[i].equals(segment)) { |
|
132 |
// continue; |
|
133 |
// } |
|
134 |
|
|
135 |
Coordinate intersects = segment.intersection(lines[i]); |
|
136 |
|
|
137 |
if (intersects == null) { |
|
138 |
continue; |
|
139 |
} |
|
140 |
|
|
141 |
double dist = c.distance(intersects); |
|
142 |
|
|
143 |
if ((dist < tolerance)) { |
|
144 |
resul = new Point2D.Double(intersects.x, intersects.y); |
|
145 |
return resul; |
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
from = first; |
|
150 |
|
|
151 |
break; |
|
152 |
} //end switch |
|
153 |
|
|
154 |
theIterator.next(); |
|
155 |
} |
|
156 |
return resul; |
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* DOCUMENT ME! |
|
161 |
* |
|
162 |
* @param g DOCUMENT ME! |
|
163 |
* |
|
164 |
* @return DOCUMENT ME! |
|
165 |
*/ |
|
166 |
private LineSegment[] getLines(Geometry g) { |
|
167 |
ArrayList lines = new ArrayList(); |
|
168 |
PathIterator theIterator = g.getPathIterator(null, Converter.FLATNESS); |
|
169 |
double[] theData = new double[6]; |
|
170 |
Coordinate from = null; |
|
171 |
Coordinate first = null; |
|
172 |
|
|
173 |
while (!theIterator.isDone()) { |
|
174 |
//while not done |
|
175 |
int theType = theIterator.currentSegment(theData); |
|
176 |
|
|
177 |
switch (theType) { |
|
178 |
case PathIterator.SEG_MOVETO: |
|
179 |
from = new Coordinate(theData[0], theData[1]); |
|
180 |
first = from; |
|
181 |
|
|
182 |
break; |
|
183 |
|
|
184 |
case PathIterator.SEG_LINETO: |
|
185 |
|
|
186 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
|
187 |
LineSegment line = new LineSegment(from, to); |
|
188 |
lines.add(line); |
|
189 |
from = to; |
|
190 |
|
|
191 |
break; |
|
192 |
|
|
193 |
case PathIterator.SEG_CLOSE: |
|
194 |
line = new LineSegment(from, first); |
|
195 |
lines.add(line); |
|
196 |
from = first; |
|
197 |
|
|
198 |
break; |
|
199 |
} //end switch |
|
200 |
|
|
201 |
theIterator.next(); |
|
202 |
} |
|
203 |
|
|
204 |
return (LineSegment[]) lines.toArray(new LineSegment[0]); |
|
205 |
} |
|
206 |
|
|
207 | 79 |
/* (non-Javadoc) |
208 | 80 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D) |
209 | 81 |
*/ |
... | ... | |
219 | 91 |
g.drawLine(x1, y1, x2, y2); |
220 | 92 |
g.drawLine(x1, y2, x2, y1); |
221 | 93 |
} |
222 |
/** |
|
223 |
* DOCUMENT ME! |
|
224 |
* |
|
225 |
* @param geoms DOCUMENT ME! |
|
226 |
*/ |
|
227 |
public void setGeometries(Geometry[] geoms) { |
|
228 |
this.geometries = geoms; |
|
229 |
} |
|
230 |
|
|
231 |
/* (non-Javadoc) |
|
94 |
/* (non-Javadoc) |
|
232 | 95 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText() |
233 | 96 |
*/ |
234 | 97 |
public String getToolTipText() { |
Also available in: Unified diff