Statistics
| Revision:

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 / PerpendicularPointSnapper.java @ 40558

History | View | Annotate | Download (5.44 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.mapcontrol.PrimitivesDrawer;
31
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperVectorial;
32
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper;
33
import org.gvsig.i18n.Messages;
34

    
35
import com.vividsolutions.jts.geom.Coordinate;
36
import com.vividsolutions.jts.geom.LineSegment;
37

    
38

    
39
/**
40
 * Perpendicular point snapper.
41
 *
42
 * @author Vicente Caballero Navarro
43
 */
44
public class PerpendicularPointSnapper extends AbstractSnapper
45
    implements ISnapperVectorial {
46
        /* (non-Javadoc)
47
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
48
     * IGeometry geom,double tolerance, Point2D lastPointEntered)
49
     */
50
    public Point2D getSnapPoint(Point2D point, Geometry geom,
51
        double tolerance, Point2D lastPointEntered) {
52
        Point2D resul = null;
53
        Coordinate c = new Coordinate(point.getX(), point.getY());
54

    
55
        if (lastPointEntered == null) {
56
            return null;
57
        }
58

    
59
        Coordinate cLastPoint = new Coordinate(lastPointEntered.getX(),
60
                lastPointEntered.getY());
61
        PathIterator theIterator = geom.getPathIterator(null,
62
                geomManager.getFlatness()); //polyLine.getPathIterator(null, flatness);
63
        double[] theData = new double[6];
64
        double minDist = tolerance;
65
        Coordinate from = null;
66
        Coordinate first = null;
67

    
68
        while (!theIterator.isDone()) {
69
            //while not done
70
            int theType = theIterator.currentSegment(theData);
71

    
72
            switch (theType) {
73
            case PathIterator.SEG_MOVETO:
74
                from = new Coordinate(theData[0], theData[1]);
75
                first = from;
76

    
77
                break;
78

    
79
            case PathIterator.SEG_LINETO:
80

    
81
                // System.out.println("SEG_LINETO");
82
                Coordinate to = new Coordinate(theData[0], theData[1]);
83
                LineSegment line = new LineSegment(from, to);
84
                Coordinate closestPoint = line.closestPoint(cLastPoint);
85
                double dist = c.distance(closestPoint);
86

    
87
                if (!(line.getCoordinate(0).equals2D(closestPoint) ||
88
                        line.getCoordinate(1).equals2D(closestPoint))) {
89
                    if ((dist < minDist)) {
90
                        resul = new Point2D.Double(closestPoint.x,
91
                                closestPoint.y);
92
                        minDist = dist;
93
                    }
94
                }
95

    
96
                from = to;
97

    
98
                break;
99

    
100
            case PathIterator.SEG_CLOSE:
101
                line = new LineSegment(from, first);
102
                closestPoint = line.closestPoint(cLastPoint);
103
                dist = c.distance(closestPoint);
104

    
105
                if (!(line.getCoordinate(0).equals2D(closestPoint) ||
106
                        line.getCoordinate(1).equals2D(closestPoint))) {
107
                    if ((dist < minDist)) {
108
                        resul = new Point2D.Double(closestPoint.x,
109
                                closestPoint.y);
110
                        minDist = dist;
111
                    }
112
                }
113

    
114
                from = first;
115

    
116
                break;
117
            } //end switch
118

    
119
            theIterator.next();
120
        }
121

    
122
        return resul;
123
    }
124

    
125
    /* (non-Javadoc)
126
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
127
     */
128
    public String getToolTipText() {
129
        return Messages.getText("Perpendicular_point");
130
    }
131

    
132
    /*
133
     * (non-Javadoc)
134
     * @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
135
     */
136
    public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) {
137
            primitivesDrawer.setColor(getColor());
138

    
139
        int half = getSizePixels() / 2;
140
        int x1 = (int) (pPixels.getX() - half);
141
        int x2 = (int) (pPixels.getX() + half);
142
        int x3 = (int) pPixels.getX();
143
        int y1 = (int) (pPixels.getY() - half);
144
        int y2 = (int) (pPixels.getY() + half);
145
        int y3 = (int) pPixels.getY();
146

    
147
        primitivesDrawer.drawLine(x1, y2, x2, y2);
148
        primitivesDrawer.drawLine(x1, y2, x1, y1);
149
        primitivesDrawer.drawLine(x1, y3, x3, y3);
150
        primitivesDrawer.drawLine(x3, y3, x3, y2);
151
    }
152

    
153
}