svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / TangentPointSnapper.java @ 40558
History | View | Annotate | Download (4.99 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers; |
25 |
|
26 |
import java.awt.geom.PathIterator; |
27 |
import java.awt.geom.Point2D; |
28 |
|
29 |
import org.gvsig.fmap.geom.Geometry; |
30 |
import org.gvsig.fmap.geom.primitive.Arc; |
31 |
import org.gvsig.fmap.geom.primitive.Circle; |
32 |
import org.gvsig.fmap.geom.primitive.Ellipse; |
33 |
import org.gvsig.fmap.geom.primitive.Spline; |
34 |
import org.gvsig.fmap.mapcontrol.PrimitivesDrawer; |
35 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperVectorial; |
36 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper; |
37 |
import org.gvsig.i18n.Messages; |
38 |
|
39 |
import com.vividsolutions.jts.geom.Coordinate; |
40 |
import com.vividsolutions.jts.geom.LineSegment; |
41 |
|
42 |
|
43 |
/**
|
44 |
* Tangent point snapper.
|
45 |
*
|
46 |
* @author Vicente Caballero Navarro
|
47 |
*/
|
48 |
public class TangentPointSnapper extends AbstractSnapper |
49 |
implements ISnapperVectorial {
|
50 |
/* (non-Javadoc)
|
51 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
|
52 |
* IGeometry geom,double tolerance, Point2D lastPointEntered)
|
53 |
*/
|
54 |
public Point2D getSnapPoint(Point2D point, Geometry geom, |
55 |
double tolerance, Point2D lastPointEntered) { |
56 |
if (!(geom instanceof Circle || |
57 |
geom instanceof Arc ||
|
58 |
geom instanceof Ellipse ||
|
59 |
geom instanceof Spline)) {
|
60 |
return null; |
61 |
} |
62 |
|
63 |
Point2D resul = null; |
64 |
Coordinate c = new Coordinate(point.getX(), point.getY());
|
65 |
|
66 |
PathIterator theIterator = geom.getPathIterator(null, |
67 |
geomManager.getFlatness()); |
68 |
double[] theData = new double[6]; |
69 |
double minDist = tolerance;
|
70 |
Coordinate from = null;
|
71 |
Coordinate first = null;
|
72 |
|
73 |
while (!theIterator.isDone()) {
|
74 |
//while not done
|
75 |
int theType = theIterator.currentSegment(theData);
|
76 |
|
77 |
switch (theType) {
|
78 |
case PathIterator.SEG_MOVETO: |
79 |
from = new Coordinate(theData[0], theData[1]); |
80 |
first = from; |
81 |
|
82 |
break;
|
83 |
|
84 |
case PathIterator.SEG_LINETO: |
85 |
|
86 |
// System.out.println("SEG_LINETO");
|
87 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
88 |
LineSegment line = new LineSegment(from, to);
|
89 |
Coordinate closestPoint = line.closestPoint(c); |
90 |
double dist = c.distance(closestPoint);
|
91 |
|
92 |
if ((dist < minDist)) {
|
93 |
resul = new Point2D.Double(closestPoint.x, closestPoint.y); |
94 |
minDist = dist; |
95 |
} |
96 |
|
97 |
from = to; |
98 |
|
99 |
break;
|
100 |
|
101 |
case PathIterator.SEG_CLOSE: |
102 |
line = new LineSegment(from, first);
|
103 |
closestPoint = line.closestPoint(c); |
104 |
dist = c.distance(closestPoint); |
105 |
|
106 |
if ((dist < minDist)) {
|
107 |
resul = new Point2D.Double(closestPoint.x, closestPoint.y); |
108 |
minDist = dist; |
109 |
} |
110 |
|
111 |
from = first; |
112 |
|
113 |
break;
|
114 |
} //end switch
|
115 |
|
116 |
theIterator.next(); |
117 |
} |
118 |
|
119 |
return resul;
|
120 |
} |
121 |
|
122 |
/* (non-Javadoc)
|
123 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
|
124 |
*/
|
125 |
public String getToolTipText() { |
126 |
return Messages.getText("Tangent_point"); |
127 |
} |
128 |
|
129 |
/*
|
130 |
* (non-Javadoc)
|
131 |
* @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
|
132 |
*/
|
133 |
public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) { |
134 |
primitivesDrawer.setColor(getColor()); |
135 |
|
136 |
int half = getSizePixels() / 2; |
137 |
primitivesDrawer.drawLine((int) (pPixels.getX() - half),
|
138 |
(int) (pPixels.getY() - half), (int) (pPixels.getX() + half), |
139 |
(int) (pPixels.getY() - half));
|
140 |
primitivesDrawer.drawOval((int) (pPixels.getX() - half),
|
141 |
(int) (pPixels.getY() - half), getSizePixels(), getSizePixels());
|
142 |
} |
143 |
|
144 |
|
145 |
} |