Statistics
| Revision:

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

History | View | Annotate | Download (9.97 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;
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.SUBTYPES;
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.type.GeometryType;
58

    
59
/**
60
 * Punto 2D.
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class Point2D extends AbstractPrimitive implements Point, DirectPosition {
65
        private static final long serialVersionUID = 1L;
66

    
67
        protected double[] coordinates;
68
        
69
        /** Instancia de GeometryType obtenida al registrar esta clase */
70
        private static final GeometryType geomType = GeometryLocator.getGeometryManager()
71
                        .registerGeometryType(Point2D.class, null, TYPES.POINT, SUBTYPES.GEOM2D);
72

    
73
        public static final int CODE = geomType.getId();
74

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

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

    
97
        public Point2D(java.awt.geom.Point2D p) {
98
                this(null, null, p);
99
        }
100

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

    
110
        public Point2D(double x, double y) {
111
                this(null, null, x, y);
112
        }
113

    
114
        /*
115
         * private void setPoint(double x, double y){ p = new
116
         * java.awt.geom.Point2D.Double(x, y); }
117
         */
118

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

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

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

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

    
160
                return rAux.contains(coordinates[0], coordinates[1]);
161
        }
162

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

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

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

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

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

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

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

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

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

    
246
        private java.awt.geom.Point2D getPoint2D()  {
247
                return new java.awt.geom.Point2D.Double(coordinates[0], coordinates[1]);
248
        }
249
        
250
        /**
251
         * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
252
         */
253
        public int getShapeType() {
254
                return FShape.POINT;
255
        }
256

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

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

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

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

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

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

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

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

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

    
359
        public GeometryType getGeometryType() {
360
                return geomType;
361
        }
362

    
363
        public Envelope getEnvelope() {
364
                return new DefaultEnvelope(2,new double[]{coordinates[0] - 0.01, coordinates[1] - 0.01},new double[]{ coordinates[0]+0.02,coordinates[1]+
365
                                0.02});
366
        }
367

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

    
373
        public DirectPosition getDirectPosition() {
374
                return this;
375
        }
376

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

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

    
400
        }
401

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

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

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

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

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

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

    
444
}