svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / primitive / impl / Point2D.java @ 40559
History | View | Annotate | Download (9.45 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40559 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40559 | jjdelcerro | *
|
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 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.fmap.geom.primitive.impl; |
||
25 | |||
26 | import java.awt.Rectangle; |
||
27 | import java.awt.Shape; |
||
28 | import java.awt.geom.AffineTransform; |
||
29 | import java.awt.geom.PathIterator; |
||
30 | import java.awt.geom.Rectangle2D; |
||
31 | |||
32 | import org.cresques.cts.ICoordTrans; |
||
33 | import org.cresques.cts.IProjection; |
||
34 | |||
35 | import org.gvsig.fmap.geom.DirectPosition; |
||
36 | import org.gvsig.fmap.geom.Geometry; |
||
37 | import org.gvsig.fmap.geom.exception.ReprojectionRuntimeException; |
||
38 | import org.gvsig.fmap.geom.handler.AbstractHandler; |
||
39 | import org.gvsig.fmap.geom.handler.FinalHandler; |
||
40 | import org.gvsig.fmap.geom.handler.Handler; |
||
41 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
42 | import org.gvsig.fmap.geom.primitive.FShape; |
||
43 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
44 | import org.gvsig.fmap.geom.primitive.Point; |
||
45 | import org.gvsig.fmap.geom.primitive.PointIterator; |
||
46 | import org.gvsig.fmap.geom.type.GeometryType; |
||
47 | |||
48 | /**
|
||
49 | * 2D point implementation.
|
||
50 | *
|
||
51 | * @author Vicente Caballero Navarro
|
||
52 | * @author gvSIG team
|
||
53 | */
|
||
54 | public class Point2D extends AbstractPrimitive implements Point, DirectPosition { |
||
55 | |||
56 | private static final long serialVersionUID = 1836257305083881299L; |
||
57 | |||
58 | protected static final String COORDINATES_FIELD = "coordinates"; |
||
59 | protected double x; |
||
60 | protected double y; |
||
61 | |||
62 | /**
|
||
63 | * The constructor with the GeometryType like and argument
|
||
64 | * is used by the {@link GeometryType}{@link #create()} to create the
|
||
65 | * geometry
|
||
66 | *
|
||
67 | * @param type
|
||
68 | * The geometry type
|
||
69 | */
|
||
70 | public Point2D(GeometryType geometryType) { |
||
71 | super(geometryType, null, null); |
||
72 | this.x = 0.0d; |
||
73 | this.y = 0.0d; |
||
74 | } |
||
75 | |||
76 | /**
|
||
77 | * Constructor used in the {@link Geometry#cloneGeometry()} method
|
||
78 | */
|
||
79 | Point2D(GeometryType geometryType, String id, IProjection projection, |
||
80 | double x, double y) { |
||
81 | super(geometryType, id, projection);
|
||
82 | this.x = x;
|
||
83 | this.y = y;
|
||
84 | } |
||
85 | |||
86 | public Point2D(double x, double y) { |
||
87 | super(TYPES.POINT, SUBTYPES.GEOM2D);
|
||
88 | this.x = x;
|
||
89 | this.y = y;
|
||
90 | } |
||
91 | |||
92 | public Point2D(GeometryType geometryType, double x, double y) { |
||
93 | super(geometryType, null, null); |
||
94 | this.x = x;
|
||
95 | this.y = y;
|
||
96 | } |
||
97 | |||
98 | public Point2D(int type, int subtype) { |
||
99 | super(type, subtype);
|
||
100 | this.x = 0.0d; |
||
101 | this.y = 0.0d; |
||
102 | } |
||
103 | |||
104 | public Point2D(java.awt.geom.Point2D point) { |
||
105 | super(TYPES.POINT, SUBTYPES.GEOM2D);
|
||
106 | this.x = point.getX();
|
||
107 | this.y = point.getY();
|
||
108 | } |
||
109 | |||
110 | /**
|
||
111 | * Aplica la transformaci?nn de la matriz de transformaci?n que se pasa como
|
||
112 | * par?metro.
|
||
113 | *
|
||
114 | * @param at
|
||
115 | * Matriz de transformaci?n.
|
||
116 | */
|
||
117 | public void transform(AffineTransform at) { |
||
118 | |||
119 | if (at == null) { |
||
120 | return;
|
||
121 | } |
||
122 | |||
123 | double[] coordinates = getCoordinates(); |
||
124 | at.transform(coordinates, 0, coordinates, 0, 1); |
||
125 | setCoordinates(coordinates); |
||
126 | } |
||
127 | |||
128 | public boolean contains(double x, double y) { |
||
129 | if ((this.x == x) || (this.y == y)) { |
||
130 | return true; |
||
131 | } else {
|
||
132 | return false; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | public boolean contains(double x, double y, double w, double h) { |
||
137 | return false; |
||
138 | } |
||
139 | |||
140 | public boolean intersects(double x, double y, double w, double h) { |
||
141 | Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h); |
||
142 | |||
143 | return rAux.contains(this.x, this.y); |
||
144 | } |
||
145 | |||
146 | public Rectangle getBounds() { |
||
147 | return new Rectangle((int) x, (int) y, 0, 0); |
||
148 | } |
||
149 | |||
150 | public double getX() { |
||
151 | return x;
|
||
152 | } |
||
153 | |||
154 | public double getY() { |
||
155 | return y;
|
||
156 | } |
||
157 | |||
158 | public boolean contains(java.awt.geom.Point2D p) { |
||
159 | return false; |
||
160 | } |
||
161 | |||
162 | public Rectangle2D getBounds2D() { |
||
163 | return new Rectangle2D.Double(x - 0.01, y - 0.01, 0.02, 0.02); |
||
164 | } |
||
165 | |||
166 | public boolean contains(Rectangle2D r) { |
||
167 | return false; |
||
168 | } |
||
169 | |||
170 | public boolean intersects(Rectangle2D r) { |
||
171 | return r.contains(x, y);
|
||
172 | } |
||
173 | |||
174 | public Shape getShape(AffineTransform affineTransform) { |
||
175 | return this; |
||
176 | } |
||
177 | |||
178 | public PathIterator getPathIterator(AffineTransform at) { |
||
179 | return new PointIterator(getPoint2D(), at); |
||
180 | } |
||
181 | |||
182 | public PathIterator getPathIterator(AffineTransform at, double flatness) { |
||
183 | return new PointIterator(getPoint2D(), at); |
||
184 | } |
||
185 | |||
186 | private java.awt.geom.Point2D getPoint2D() {
|
||
187 | return new java.awt.geom.Point2D.Double(x, y); |
||
188 | } |
||
189 | |||
190 | public int getShapeType() { |
||
191 | return TYPES.POINT;
|
||
192 | } |
||
193 | |||
194 | public FShape cloneFShape() {
|
||
195 | return new Point2D(getGeometryType(), id, projection, x, y); |
||
196 | } |
||
197 | |||
198 | public void reProject(ICoordTrans ct) { |
||
199 | java.awt.geom.Point2D p = getPoint2D(); |
||
200 | |||
201 | try {
|
||
202 | p = ct.convert(p, p); |
||
203 | this.x = p.getX();
|
||
204 | this.y = p.getY();
|
||
205 | } catch (Exception exc) { |
||
206 | /*
|
||
207 | * This can happen when the reprojection lib is unable
|
||
208 | * to reproject (for example the source point
|
||
209 | * is out of the valid range and some computing
|
||
210 | * problem happens)
|
||
211 | */
|
||
212 | throw new ReprojectionRuntimeException( |
||
213 | ct.getPOrig(), ct.getPDest(), getPoint2D(), exc); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | public Handler[] getStretchingHandlers() { |
||
218 | return new Handler[] { new PointHandler(0, x, y, this) }; |
||
219 | } |
||
220 | |||
221 | public Handler[] getSelectHandlers() { |
||
222 | return new Handler[] { new PointHandler(0, x, y, this) }; |
||
223 | } |
||
224 | |||
225 | /**
|
||
226 | *
|
||
227 | * @author Vicente Caballero Navarro
|
||
228 | * @author gvSIG team
|
||
229 | */
|
||
230 | class PointHandler extends AbstractHandler implements FinalHandler { |
||
231 | |||
232 | private final Point gvSIGPoint; |
||
233 | |||
234 | public PointHandler(int i, double x, double y, Point gvSIGPoint) { |
||
235 | this.gvSIGPoint = gvSIGPoint;
|
||
236 | point = new java.awt.geom.Point2D.Double(x, y);
|
||
237 | index = i; |
||
238 | } |
||
239 | |||
240 | public void move(double movex, double movey) { |
||
241 | gvSIGPoint.setX(gvSIGPoint.getX() + movex); |
||
242 | gvSIGPoint.setY(gvSIGPoint.getY() + movey); |
||
243 | } |
||
244 | |||
245 | public void set(double setx, double sety) { |
||
246 | gvSIGPoint.setX(setx); |
||
247 | gvSIGPoint.setY(sety); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | public int getDimension() { |
||
252 | return 2; |
||
253 | } |
||
254 | |||
255 | public Envelope getEnvelope() {
|
||
256 | return new Envelope2D(x - 0.01, y - 0.01, x + 0.02, y + 0.02); |
||
257 | } |
||
258 | |||
259 | public GeneralPathX getGeneralPath() {
|
||
260 | return null; |
||
261 | } |
||
262 | |||
263 | public DirectPosition getDirectPosition() {
|
||
264 | return this; |
||
265 | } |
||
266 | |||
267 | public double getOrdinate(int dim) { |
||
268 | if (dim == Geometry.DIMENSIONS.X) {
|
||
269 | return getX();
|
||
270 | } else
|
||
271 | if (dim == Geometry.DIMENSIONS.Y) {
|
||
272 | return getY();
|
||
273 | } else {
|
||
274 | return 0; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | public boolean equals(Object other) { |
||
279 | if (!super.equals(other)) { |
||
280 | return false; |
||
281 | } |
||
282 | Point2D pother = (Point2D) other; |
||
283 | if (Math.abs(this.getX() - pother.getX()) > 0.0000001) { |
||
284 | return false; |
||
285 | } |
||
286 | if (Math.abs(this.getY() - pother.getY()) > 0.0000001) { |
||
287 | return false; |
||
288 | } |
||
289 | return true; |
||
290 | |||
291 | } |
||
292 | |||
293 | public double[] getCoordinates() { |
||
294 | return new double[] { x, y }; |
||
295 | } |
||
296 | |||
297 | public double getCoordinateAt(int dimension) { |
||
298 | switch (dimension) {
|
||
299 | case Geometry.DIMENSIONS.X:
|
||
300 | return x;
|
||
301 | case Geometry.DIMENSIONS.Y:
|
||
302 | return y;
|
||
303 | default:
|
||
304 | throw new ArrayIndexOutOfBoundsException(dimension); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | public void setCoordinateAt(int dimension, double value) { |
||
309 | switch (dimension) {
|
||
310 | case Geometry.DIMENSIONS.X:
|
||
311 | this.x = value;
|
||
312 | break;
|
||
313 | case Geometry.DIMENSIONS.Y:
|
||
314 | this.y = value;
|
||
315 | break;
|
||
316 | default:
|
||
317 | throw new ArrayIndexOutOfBoundsException(dimension); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | public void setCoordinates(double[] values) { |
||
322 | this.x = values[Geometry.DIMENSIONS.X];
|
||
323 | this.y = values[Geometry.DIMENSIONS.Y];
|
||
324 | } |
||
325 | |||
326 | public void setX(double x) { |
||
327 | this.x = x;
|
||
328 | } |
||
329 | |||
330 | public void setY(double y) { |
||
331 | this.y = y;
|
||
332 | } |
||
333 | |||
334 | public String toString() { |
||
335 | StringBuffer buffer = new StringBuffer(); |
||
336 | double[] coordinates = getCoordinates(); |
||
337 | buffer.append(getFullTypeName()).append("(").append(coordinates[0]); |
||
338 | for (int i = 1; i < coordinates.length; i++) { |
||
339 | buffer.append(",").append(coordinates[i]);
|
||
340 | } |
||
341 | buffer.append(")");
|
||
342 | return buffer.toString();
|
||
343 | } |
||
344 | |||
345 | protected String getFullTypeName() { |
||
346 | return "Point2D"; |
||
347 | } |
||
348 | |||
349 | public int getType() { |
||
350 | return TYPES.POINT;
|
||
351 | } |
||
352 | } |