svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.snapping.app / org.gvsig.snapping.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / MediumPointSnapper.java @ 44135
History | View | Annotate | Download (4.99 KB)
1 | 42037 | fdiaz | /**
|
---|---|---|---|
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 | |||
41 | |||
42 | /**
|
||
43 | * Medium point snapper.
|
||
44 | *
|
||
45 | * @author Vicente Caballero Navarro
|
||
46 | */
|
||
47 | public class MediumPointSnapper extends AbstractSnapper |
||
48 | implements ISnapperVectorial {
|
||
49 | /* (non-Javadoc)
|
||
50 | * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
|
||
51 | * IGeometry geom,double tolerance, Point2D lastPointEntered)
|
||
52 | */
|
||
53 | public Point2D getSnapPoint(Point2D point, Geometry geom, |
||
54 | double tolerance, Point2D lastPointEntered) { |
||
55 | if (geom instanceof Circle || |
||
56 | geom instanceof Arc ||
|
||
57 | geom instanceof Ellipse ||
|
||
58 | geom instanceof Spline) {
|
||
59 | return null; |
||
60 | } |
||
61 | |||
62 | Point2D resul = null; |
||
63 | Coordinate c = new Coordinate(point.getX(), point.getY());
|
||
64 | |||
65 | PathIterator theIterator = geom.getPathIterator(null, |
||
66 | geomManager.getFlatness()); //polyLine.getPathIterator(null, flatness);
|
||
67 | double[] theData = new double[6]; |
||
68 | double minDist = tolerance;
|
||
69 | Coordinate from = null;
|
||
70 | Coordinate first = null;
|
||
71 | |||
72 | while (!theIterator.isDone()) {
|
||
73 | //while not done
|
||
74 | int theType = theIterator.currentSegment(theData);
|
||
75 | |||
76 | switch (theType) {
|
||
77 | case PathIterator.SEG_MOVETO: |
||
78 | from = new Coordinate(theData[0], theData[1]); |
||
79 | first = from; |
||
80 | |||
81 | break;
|
||
82 | |||
83 | case PathIterator.SEG_LINETO: |
||
84 | |||
85 | Coordinate to = new Coordinate(theData[0], theData[1]); |
||
86 | Coordinate mediumPoint = new Coordinate((to.x + from.x) / 2, |
||
87 | (to.y + from.y) / 2);
|
||
88 | double dist = c.distance(mediumPoint);
|
||
89 | |||
90 | if ((dist < minDist)) {
|
||
91 | resul = new Point2D.Double(mediumPoint.x, mediumPoint.y); |
||
92 | minDist = dist; |
||
93 | } |
||
94 | |||
95 | from = to; |
||
96 | |||
97 | break;
|
||
98 | |||
99 | case PathIterator.SEG_CLOSE: |
||
100 | mediumPoint = new Coordinate((first.x + from.x) / 2, |
||
101 | (first.y + from.y) / 2);
|
||
102 | dist = c.distance(mediumPoint); |
||
103 | |||
104 | if ((dist < minDist)) {
|
||
105 | resul = new Point2D.Double(mediumPoint.x, mediumPoint.y); |
||
106 | minDist = dist; |
||
107 | } |
||
108 | |||
109 | from = first; |
||
110 | |||
111 | break;
|
||
112 | } //end switch
|
||
113 | |||
114 | theIterator.next(); |
||
115 | } |
||
116 | |||
117 | return resul;
|
||
118 | } |
||
119 | |||
120 | /* (non-Javadoc)
|
||
121 | * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
|
||
122 | */
|
||
123 | public String getToolTipText() { |
||
124 | return Messages.getText("Medium_point"); |
||
125 | } |
||
126 | |||
127 | /*
|
||
128 | * (non-Javadoc)
|
||
129 | * @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
|
||
130 | */
|
||
131 | public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) { |
||
132 | primitivesDrawer.setColor(getColor()); |
||
133 | |||
134 | int half = getSizePixels() / 2; |
||
135 | int x1 = (int) (pPixels.getX() - half); |
||
136 | int x2 = (int) (pPixels.getX() + half); |
||
137 | int x3 = (int) pPixels.getX(); |
||
138 | int y1 = (int) (pPixels.getY() - half); |
||
139 | int y2 = (int) (pPixels.getY() + half); |
||
140 | |||
141 | primitivesDrawer.drawLine(x1, y2, x2, y2); |
||
142 | primitivesDrawer.drawLine(x1, y2, x3, y1); |
||
143 | primitivesDrawer.drawLine(x2, y2, x3, y1); |
||
144 | } |
||
145 | |||
146 | } |