Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_905 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / snapping / PixelSnapper.java @ 10767

History | View | Annotate | Download (4.38 KB)

1 6771 fjp
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.cad.snapping;
42
43
import java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.geom.Point2D;
46
import java.awt.image.BufferedImage;
47
import java.awt.image.ColorModel;
48
49
import com.iver.andami.PluginServices;
50
import com.iver.cit.gvsig.fmap.MapControl;
51
import com.iver.cit.gvsig.fmap.ViewPort;
52
import com.vividsolutions.jts.geom.Coordinate;
53
54
public class PixelSnapper extends AbstractSnapper implements ISnapperRaster {
55
56
        private Color refColor = Color.BLACK;
57
        private int tolColorR = 100;
58
        private int tolColorG = 100;
59
        private int tolColorB = 100;
60
        /* (non-Javadoc)
61
         * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapperRaster#getSnapPoint(java.awt.image.BufferedImage, java.awt.geom.Point2D, double, java.awt.geom.Point2D)
62
         */
63
        public Point2D getSnapPoint(MapControl mapControl, Point2D point,
64
                        double mapTolerance, Point2D lastPoint) {
65
                // Miramos dentro del rect?ngulo que define la tolerancia
66
                // y nos quedamos con el pixel que m?s se acerca al que buscamos
67
                BufferedImage img = mapControl.getImage();
68
                ViewPort vp = mapControl.getViewPort();
69
                Point2D pPixel = vp.fromMapPoint(point);
70
                int xPixel = (int) pPixel.getX();
71
                int yPixel = (int) pPixel.getY();
72
//                int centerRGB = img.getRGB(xPixel, yPixel);
73
                int centerRGB = refColor.getRGB();
74
                double x1 = ColorModel.getRGBdefault().getRed(centerRGB);
75
                double y1 = ColorModel.getRGBdefault().getGreen(centerRGB);
76
                double z1 = ColorModel.getRGBdefault().getBlue(centerRGB);
77
                Coordinate c = new Coordinate(x1, y1, z1);
78
79
                int half = vp.fromMapDistance(mapTolerance) / 2;
80
                double minDistColor = Double.MAX_VALUE;
81
                int xSnapped = -1;
82
                int ySnapped = -1;
83
                int fromX =xPixel -half;
84
                if (fromX <0) fromX = 0;
85
                int fromY =yPixel -half;
86
                if (fromY <0) fromY = 0;
87
88
                int toX =xPixel + half;
89
                if (toX > vp.getImageWidth()) toX = vp.getImageWidth();
90
                int toY =yPixel + half;
91
                if (toY > vp.getImageHeight()) toY = vp.getImageHeight();
92
93
                for (int testX= fromX; testX< toX; testX++)
94
                {
95
                        for (int testY= fromY; testY< toY; testY++)
96
                        {
97
                                // System.out.println("Testing: " + testX + ", " + testY);
98
                                int testRGB = img.getRGB(testX, testY);
99
                                // TODO: Aqu? deber?amos trabajar con un ColorSpace y usar toCIEXYZ. Por ahora lo calculo con RGB.
100
                                int r = ColorModel.getRGBdefault().getRed(testRGB);
101
                                int g = ColorModel.getRGBdefault().getGreen(testRGB);
102
                                int b = ColorModel.getRGBdefault().getBlue(testRGB);
103
                                Coordinate cAux = new Coordinate(r, g, b);
104
105
                                if (Math.abs(r-x1) < tolColorR)
106
                                        if (Math.abs(g-y1) < tolColorG)
107
                                                if (Math.abs(b-z1) < tolColorB)
108
                                                {
109
                                                        double dist = c.distance(cAux);
110
                                                        if (dist < minDistColor)
111
                                                        {
112
                                                                minDistColor = dist;
113
                                                                xSnapped = testX;
114
                                                                ySnapped = testY;
115
                                                        }
116
                                                }
117
                        }
118
                }
119
                Point2D pResul = null;
120
                if (xSnapped != -1)
121
                {
122
                        pResul = vp.toMapPoint(xSnapped, ySnapped);
123
                }
124
                return pResul;
125
        }
126
127
        public void draw(Graphics g, Point2D pPixels) {
128
                g.setColor(getColor());
129
                int half = getSizePixels() / 2;
130
                g.drawOval((int) (pPixels.getX() - half),
131
                                (int) (pPixels.getY() - half),
132
                                getSizePixels(), getSizePixels());
133
        }
134
135
        public String getToolTipText() {
136
                return PluginServices.getText(this, "pixel_point");
137
        }
138
139
        public int getPriority() {
140
                return 3;
141
        }
142
143
}
144