Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Point2D.java @ 29018

History | View | Annotate | Download (10.1 KB)

1
/* 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 org.gvsig.fmap.geom.primitive.impl;
42

    
43
import java.awt.Rectangle;
44
import java.awt.geom.AffineTransform;
45
import java.awt.geom.PathIterator;
46
import java.awt.geom.Rectangle2D;
47
import java.util.ArrayList;
48

    
49
import org.cresques.cts.ICoordTrans;
50
import org.cresques.cts.IProjection;
51
import org.gvsig.fmap.geom.DirectPosition;
52
import org.gvsig.fmap.geom.GeometryLocator;
53
import org.gvsig.fmap.geom.Geometry.TYPES;
54
import org.gvsig.fmap.geom.handler.AbstractHandler;
55
import org.gvsig.fmap.geom.handler.FinalHandler;
56
import org.gvsig.fmap.geom.handler.Handler;
57
import org.gvsig.fmap.geom.primitive.Envelope;
58
import org.gvsig.fmap.geom.primitive.FShape;
59
import org.gvsig.fmap.geom.primitive.GeneralPathX;
60
import org.gvsig.fmap.geom.primitive.Point;
61
import org.gvsig.fmap.geom.primitive.PointIterator;
62
import org.gvsig.fmap.geom.type.GeometryType;
63

    
64
/**
65
 * Punto 2D.
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class Point2D extends AbstractPrimitive implements Point, DirectPosition {
70
        private static final long serialVersionUID = 1L;
71

    
72
        protected double[] coordinates;
73

    
74
        /** Instancia de GeometryType obtenida al registrar esta clase */
75
        private static final GeometryType geomType = GeometryLocator.getGeometryManager()
76
                        .registerGeometryType(Point2D.class, "Point2D", TYPES.POINT, SUBTYPES.GEOM2D);
77

    
78
        /**
79
         * Crea un nuevo Point2D.
80
         *
81
         * @param x
82
         *            Coordenada x del punto.
83
         * @param y
84
         *            Coordenada y del punto.
85
         */
86
        public Point2D(String id, IProjection projection, double x, double y) {
87
                super(id, projection);
88
                coordinates = new double[getDimension()];
89
                coordinates[0] = x;
90
                coordinates[1] = y;
91
        }
92

    
93
        public Point2D(String id, IProjection projection, java.awt.geom.Point2D p) {
94
                super(id, projection);
95
                coordinates = new double[getDimension()];
96
                coordinates[0] = p.getX();
97
                coordinates[1] = p.getY();
98
        }
99

    
100
        public Point2D(java.awt.geom.Point2D p) {
101
                this(null, null, p);
102
        }
103

    
104
        /**
105
         * Constructor without arguments. It is necessary to create
106
         * geometries using the {@link GeometryType}{@link #create()}
107
         * method
108
         */
109
        public Point2D() {
110
                this(null, null, 0.0, 0.0);
111
        }
112

    
113
        public Point2D(double x, double y) {
114
                this(null, null, x, y);
115
        }
116

    
117
        /**
118
         * Aplica la transformaci?nn de la matriz de transformaci?n que se pasa como
119
         * par?metro.
120
         *
121
         * @param at
122
         *            Matriz de transformaci?n.
123
         */
124
        public void transform(AffineTransform at) {
125
                at.transform(coordinates, 0, coordinates, 0, 1);
126
        }
127

    
128
        /*
129
         * (non-Javadoc)
130
         *
131
         * @see java.awt.Shape#contains(double, double)
132
         */
133
        public boolean contains(double x, double y) {
134
                if ((x == coordinates[0]) || (y == coordinates[1])) {
135
                        return true;
136
                } else {
137
                        return false;
138
                }
139
        }
140

    
141
        /*
142
         * (non-Javadoc)
143
         *
144
         * @see java.awt.Shape#contains(double, double, double, double)
145
         */
146
        public boolean contains(double x, double y, double w, double h) {
147
                return false;
148
        }
149

    
150
        /*
151
         * (non-Javadoc)
152
         *
153
         * @see java.awt.Shape#intersects(double, double, double, double)
154
         */
155
        public boolean intersects(double x, double y, double w, double h) {
156
                Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h);
157

    
158
                return rAux.contains(coordinates[0], coordinates[1]);
159
        }
160

    
161
        /*
162
         * (non-Javadoc)
163
         *
164
         * @see java.awt.Shape#getBounds()
165
         */
166
        public Rectangle getBounds() {
167
                return new Rectangle((int) coordinates[0], (int) coordinates[1], 0, 0);
168
        }
169

    
170
        /**
171
         * Devuelve la coordenada x del punto.
172
         *
173
         * @return Coordenada x.
174
         */
175
        public double getX() {
176
                return coordinates[0];
177
        }
178

    
179
        /**
180
         * Devuelve la coordenada y del punto.
181
         *
182
         * @return Coordenada y.
183
         */
184
        public double getY() {
185
                return coordinates[1];
186
        }
187

    
188
        /*
189
         * (non-Javadoc)
190
         *
191
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
192
         */
193
        public boolean contains(java.awt.geom.Point2D p) {
194
                return false;
195
        }
196

    
197
        /*
198
         * (non-Javadoc)
199
         *
200
         * @see java.awt.Shape#getBounds2D()
201
         */
202
        public Rectangle2D getBounds2D() {
203
                return new Rectangle2D.Double(coordinates[0] - 0.01, coordinates[1] - 0.01, 0.02,
204
                                0.02);
205
        }
206

    
207
        /*
208
         * (non-Javadoc)
209
         *
210
         * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
211
         */
212
        public boolean contains(Rectangle2D r) {
213
                return false;
214
        }
215

    
216
        /*
217
         * (non-Javadoc)
218
         *
219
         * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
220
         */
221
        public boolean intersects(Rectangle2D r) {
222
                return r.contains(coordinates[0], coordinates[1]);
223
        }
224

    
225
        /*
226
         * (non-Javadoc)
227
         *
228
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
229
         */
230
        public PathIterator getPathIterator(AffineTransform at) {
231
                return new PointIterator(getPoint2D(), at);
232
        }
233

    
234
        /*
235
         * (non-Javadoc)
236
         *
237
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform,
238
         *      double)
239
         */
240
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
241
                return new PointIterator(getPoint2D(), at);
242
        }
243

    
244
        private java.awt.geom.Point2D getPoint2D()  {
245
                return new java.awt.geom.Point2D.Double(coordinates[0], coordinates[1]);
246
        }
247

    
248
        /**
249
         * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
250
         */
251
        public int getShapeType() {
252
                return TYPES.POINT;
253
        }
254

    
255
        /**
256
         * Devuelve el tipo de geometr??a (cte definido en interfaz)
257
         */
258
        public int getType() {
259
                return geomType.getType();
260
        }
261

    
262
        /*
263
         * (non-Javadoc)
264
         *
265
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
266
         */
267
        public FShape cloneFShape() {
268
                return new Point2D(id, projection, coordinates[0], coordinates[1]);
269
        }
270

    
271
        /*
272
         * (non-Javadoc)
273
         *
274
         * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
275
         */
276
        public void reProject(ICoordTrans ct) {
277
                java.awt.geom.Point2D p = getPoint2D();
278
                p = ct.convert(p, p);
279
                coordinates[0] = p.getX();
280
                coordinates[1] = p.getY();
281
        }
282

    
283
        /*
284
         * (non-Javadoc)
285
         *
286
         * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
287
         */
288
        public Handler[] getStretchingHandlers() {
289
                ArrayList handlers = new ArrayList();
290
                handlers.add(new PointHandler(0, coordinates[0], coordinates[1]));
291
                return (Handler[]) handlers.toArray(new Handler[0]);
292
        }
293

    
294
        /*
295
         * (non-Javadoc)
296
         *
297
         * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
298
         */
299
        public Handler[] getSelectHandlers() {
300
                ArrayList handlers = new ArrayList();
301
                handlers.add(new PointHandler(0, coordinates[0], coordinates[1]));
302
                return (Handler[]) handlers.toArray(new Handler[0]);
303
        }
304

    
305
        /**
306
         * DOCUMENT ME!
307
         *
308
         * @author Vicente Caballero Navarro
309
         */
310
        class PointHandler extends AbstractHandler implements FinalHandler {
311
                /**
312
                 * Crea un nuevo PointHandler.
313
                 *
314
                 * @param x
315
                 *            DOCUMENT ME!
316
                 * @param y
317
                 *            DOCUMENT ME!
318
                 */
319
                public PointHandler(int i, double x, double y) {
320
                        point = new java.awt.geom.Point2D.Double(x, y);
321
                        index = i;
322
                }
323

    
324
                /**
325
                 * DOCUMENT ME!
326
                 *
327
                 * @param x
328
                 *            DOCUMENT ME!
329
                 * @param y
330
                 *            DOCUMENT ME!
331
                 *
332
                 * @return DOCUMENT ME!
333
                 */
334
                public void move(double x, double y) {
335
                        coordinates[0] = coordinates[0] + x;
336
                        coordinates[1] = coordinates[1] + y;
337
                }
338

    
339
                /**
340
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
341
                 */
342
                public void set(double x, double y) {
343
                        coordinates[0] = x;
344
                        coordinates[1] = y;
345
                }
346
        }
347

    
348
        /*
349
         * (non-Javadoc)
350
         *
351
         * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
352
         */
353
        public int getDimension() {
354
                return 2;
355
        }
356

    
357
        public GeometryType getGeometryType() {
358
                return geomType;
359
        }
360

    
361
        public Envelope getEnvelope() {
362
                return new Envelope2D(coordinates[0] - 0.01, coordinates[1] - 0.01,coordinates[0]+0.02,coordinates[1]+
363
                                0.02);
364
        }
365

    
366
        public GeneralPathX getGeneralPath() {
367
                // TODO Auto-generated method stub
368
                return null;
369
        }
370

    
371
        public DirectPosition getDirectPosition() {
372
                return this;
373
        }
374

    
375
        public double getOrdinate(int dim) {
376
                if (dim == 0) {
377
                        return getX();
378
                } else if (dim == 1) {
379
                        return getY();
380
                } else {
381
                        return 0;
382
                }
383
        }
384

    
385
        public boolean equals(Object other) {
386
                if (!super.equals(other)) {
387
                        return false;
388
                }
389
                Point2D pother = (Point2D) other;
390
                if (Math.abs(this.getX() - pother.getX()) > 0.0000001) {
391
                        return false;
392
                }
393
                if (Math.abs(this.getY() - pother.getY()) > 0.0000001) {
394
                        return false;
395
                }
396
                return true;
397

    
398
        }
399

    
400
        /* (non-Javadoc)
401
         * @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
402
         */
403
        public double[] getCoordinates() {
404
                return coordinates;
405
        }
406

    
407
        /* (non-Javadoc)
408
         * @see org.gvsig.fmap.geom.primitive.Point#getDoordinateAt(int)
409
         */
410
        public double getCoordinateAt(int dimension) {
411
                return coordinates[dimension];
412
        }
413

    
414
        /* (non-Javadoc)
415
         * @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
416
         */
417
        public void setCoordinateAt(int dimension, double value) {
418
                coordinates[dimension] = value;
419
        }
420

    
421
        /* (non-Javadoc)
422
         * @see org.gvsig.fmap.geom.primitive.Point#setCoordinates(double[])
423
         */
424
        public void setCoordinates(double[] values) {
425
                coordinates = values;
426
        }
427

    
428
        /* (non-Javadoc)
429
         * @see org.gvsig.fmap.geom.primitive.Point#setX(double)
430
         */
431
        public void setX(double x) {
432
                coordinates[0] = x;
433
        }
434

    
435
        /* (non-Javadoc)
436
         * @see org.gvsig.fmap.geom.primitive.Point#setY(double)
437
         */
438
        public void setY(double y) {
439
                coordinates[1] = y;
440
        }
441

    
442
        public String toString() {
443
                return "Point2D(" + coordinates[0] + "," + coordinates[1] + ")";
444
        }
445

    
446

    
447

    
448
}