svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / snapping / snappers / IntersectionPointSnapper.java @ 10626
History | View | Annotate | Download (6.7 KB)
1 |
package com.iver.cit.gvsig.project.documents.view.snapping.snappers; |
---|---|
2 |
|
3 |
import java.awt.Graphics; |
4 |
import java.awt.geom.PathIterator; |
5 |
import java.awt.geom.Point2D; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import com.iver.andami.PluginServices; |
9 |
import com.iver.cit.gvsig.fmap.core.FArc2D; |
10 |
import com.iver.cit.gvsig.fmap.core.FCircle2D; |
11 |
import com.iver.cit.gvsig.fmap.core.FEllipse2D; |
12 |
import com.iver.cit.gvsig.fmap.core.FSpline2D; |
13 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
14 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
15 |
import com.iver.cit.gvsig.project.documents.view.snapping.AbstractSnapper; |
16 |
import com.iver.cit.gvsig.project.documents.view.snapping.ISnapperGeometriesVectorial; |
17 |
import com.vividsolutions.jts.geom.Coordinate; |
18 |
import com.vividsolutions.jts.geom.LineSegment; |
19 |
|
20 |
|
21 |
/**
|
22 |
* Intersection point snapper.
|
23 |
*
|
24 |
* @author Vicente Caballero Navarro
|
25 |
*/
|
26 |
public class IntersectionPointSnapper extends AbstractSnapper |
27 |
implements ISnapperGeometriesVectorial {
|
28 |
private IGeometry[] geometries; |
29 |
public IntersectionPointSnapper() {
|
30 |
System.err.println("Construido IntersectionPoinSnapper"); |
31 |
} |
32 |
|
33 |
/* (non-Javadoc)
|
34 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
|
35 |
* IGeometry geom,double tolerance, Point2D lastPointEntered)
|
36 |
*/
|
37 |
public Point2D getSnapPoint(Point2D point, IGeometry geom, |
38 |
double tolerance, Point2D lastPointEntered) { |
39 |
if (geom.getInternalShape() instanceof FCircle2D || |
40 |
geom.getInternalShape() instanceof FArc2D ||
|
41 |
geom.getInternalShape() instanceof FEllipse2D ||
|
42 |
geom.getInternalShape() instanceof FSpline2D){
|
43 |
return null; |
44 |
} |
45 |
Point2D result = null; |
46 |
|
47 |
if (geometries == null) { |
48 |
return null; |
49 |
} |
50 |
|
51 |
for (int i = 0; i < geometries.length; i++) { |
52 |
Point2D r = intersects(geom, geometries[i], point, tolerance);
|
53 |
|
54 |
if (r != null) { |
55 |
result = r; |
56 |
} |
57 |
} |
58 |
|
59 |
return result;
|
60 |
} |
61 |
|
62 |
/**
|
63 |
* DOCUMENT ME!
|
64 |
*
|
65 |
* @param g1 DOCUMENT ME!
|
66 |
* @param g2 DOCUMENT ME!
|
67 |
* @param point DOCUMENT ME!
|
68 |
* @param tolerance DOCUMENT ME!
|
69 |
*
|
70 |
* @return DOCUMENT ME!
|
71 |
*/
|
72 |
private Point2D intersects(IGeometry g1, IGeometry g2, Point2D point, |
73 |
double tolerance) {
|
74 |
Point2D resul = null; |
75 |
Coordinate c = new Coordinate(point.getX(), point.getY());
|
76 |
PathIterator theIterator = g1.getPathIterator(null, FConverter.FLATNESS); |
77 |
double[] theData = new double[6]; |
78 |
Coordinate from = null;
|
79 |
Coordinate first = null;
|
80 |
LineSegment[] lines = getLines(g2);
|
81 |
while (!theIterator.isDone()) {
|
82 |
int theType = theIterator.currentSegment(theData);
|
83 |
|
84 |
switch (theType) {
|
85 |
case PathIterator.SEG_MOVETO: |
86 |
from = new Coordinate(theData[0], theData[1]); |
87 |
first = from; |
88 |
|
89 |
break;
|
90 |
|
91 |
case PathIterator.SEG_LINETO: |
92 |
|
93 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
94 |
LineSegment segmentLine = new LineSegment(from,to);
|
95 |
for (int i = 0; i < lines.length; i++) { |
96 |
// if (lines[i].equals(segmentLine)) {
|
97 |
// continue;
|
98 |
// }
|
99 |
Coordinate intersects = segmentLine.intersection(lines[i]); |
100 |
if (intersects == null) { |
101 |
continue;
|
102 |
} |
103 |
|
104 |
double dist = c.distance(intersects);
|
105 |
|
106 |
if ((dist < tolerance)) {
|
107 |
resul = new Point2D.Double(intersects.x, intersects.y); |
108 |
return resul;
|
109 |
} |
110 |
} |
111 |
|
112 |
from = to; |
113 |
|
114 |
break;
|
115 |
|
116 |
case PathIterator.SEG_CLOSE: |
117 |
LineSegment segment = new LineSegment(from,first);
|
118 |
|
119 |
for (int i = 0; i < lines.length; i++) { |
120 |
// if (lines[i].equals(segment)) {
|
121 |
// continue;
|
122 |
// }
|
123 |
|
124 |
Coordinate intersects = segment.intersection(lines[i]); |
125 |
|
126 |
if (intersects == null) { |
127 |
continue;
|
128 |
} |
129 |
|
130 |
double dist = c.distance(intersects);
|
131 |
|
132 |
if ((dist < tolerance)) {
|
133 |
resul = new Point2D.Double(intersects.x, intersects.y); |
134 |
return resul;
|
135 |
} |
136 |
} |
137 |
|
138 |
from = first; |
139 |
|
140 |
break;
|
141 |
} //end switch
|
142 |
|
143 |
theIterator.next(); |
144 |
} |
145 |
return resul;
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* DOCUMENT ME!
|
150 |
*
|
151 |
* @param g DOCUMENT ME!
|
152 |
*
|
153 |
* @return DOCUMENT ME!
|
154 |
*/
|
155 |
private LineSegment[] getLines(IGeometry g) { |
156 |
ArrayList lines = new ArrayList(); |
157 |
PathIterator theIterator = g.getPathIterator(null, FConverter.FLATNESS); |
158 |
double[] theData = new double[6]; |
159 |
Coordinate from = null;
|
160 |
Coordinate first = null;
|
161 |
|
162 |
while (!theIterator.isDone()) {
|
163 |
//while not done
|
164 |
int theType = theIterator.currentSegment(theData);
|
165 |
|
166 |
switch (theType) {
|
167 |
case PathIterator.SEG_MOVETO: |
168 |
from = new Coordinate(theData[0], theData[1]); |
169 |
first = from; |
170 |
|
171 |
break;
|
172 |
|
173 |
case PathIterator.SEG_LINETO: |
174 |
|
175 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
176 |
LineSegment line = new LineSegment(from, to);
|
177 |
lines.add(line); |
178 |
from = to; |
179 |
|
180 |
break;
|
181 |
|
182 |
case PathIterator.SEG_CLOSE: |
183 |
line = new LineSegment(from, first);
|
184 |
lines.add(line); |
185 |
from = first; |
186 |
|
187 |
break;
|
188 |
} //end switch
|
189 |
|
190 |
theIterator.next(); |
191 |
} |
192 |
|
193 |
return (LineSegment[]) lines.toArray(new LineSegment[0]); |
194 |
} |
195 |
|
196 |
/* (non-Javadoc)
|
197 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D)
|
198 |
*/
|
199 |
public void draw(Graphics g, Point2D pPixels) { |
200 |
g.setColor(getColor()); |
201 |
|
202 |
int half = getSizePixels() / 2; |
203 |
int x1 = (int) (pPixels.getX() - half); |
204 |
int x2 = (int) (pPixels.getX() + half); |
205 |
int y1 = (int) (pPixels.getY() - half); |
206 |
int y2 = (int) (pPixels.getY() + half); |
207 |
|
208 |
g.drawLine(x1, y1, x2, y2); |
209 |
g.drawLine(x1, y2, x2, y1); |
210 |
} |
211 |
/**
|
212 |
* DOCUMENT ME!
|
213 |
*
|
214 |
* @param geoms DOCUMENT ME!
|
215 |
*/
|
216 |
public void setGeometries(IGeometry[] geoms) { |
217 |
this.geometries = geoms;
|
218 |
} |
219 |
|
220 |
/* (non-Javadoc)
|
221 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
|
222 |
*/
|
223 |
public String getToolTipText() { |
224 |
return PluginServices.getText(this, "intersection_point"); |
225 |
} |
226 |
} |