Statistics
| Revision:

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

History | View | Annotate | Download (9.76 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.Geometry;
53
import org.gvsig.fmap.geom.handler.AbstractHandler;
54
import org.gvsig.fmap.geom.handler.FinalHandler;
55
import org.gvsig.fmap.geom.handler.Handler;
56
import org.gvsig.fmap.geom.primitive.Envelope;
57
import org.gvsig.fmap.geom.primitive.FShape;
58
import org.gvsig.fmap.geom.primitive.GeneralPathX;
59
import org.gvsig.fmap.geom.primitive.Point;
60
import org.gvsig.fmap.geom.primitive.PointIterator;
61
import org.gvsig.fmap.geom.type.GeometryType;
62

    
63
/**
64
 * Punto 2D.
65
 *
66
 * @author Vicente Caballero Navarro
67
 */
68
public class Point2D extends AbstractPrimitive implements Point, DirectPosition {
69
        private static final long serialVersionUID = 1836257305083881299L;
70
        protected double[] coordinates;
71
                
72
        /**
73
         * The constructor with the GeometryType like and argument 
74
         * is used by the {@link GeometryType}{@link #create()}
75
         * to create the geometry
76
         * @param type
77
         * The geometry type
78
         */
79
        public Point2D(GeometryType geometryType) {
80
                this(geometryType, null, null, 0.0, 0.0);
81
        }        
82
                
83
        /**
84
         * Constructor used in the {@link Geometry#cloneGeometry()} method
85
         */
86
        Point2D(GeometryType geometryType, String id, IProjection projection, double x, double y) {
87
                super(geometryType, id, projection);
88
                coordinates = new double[getDimension()];
89
                coordinates[0] = x;
90
                coordinates[1] = y;
91
        }
92
        
93
        public Point2D (double x, double y) {
94
                super(TYPES.POINT, SUBTYPES.GEOM2D);
95
                coordinates = new double[getDimension()];
96
                coordinates[0] = x;
97
                coordinates[1] = y;
98
        }
99
        
100
        public Point2D (java.awt.geom.Point2D point) {
101
                super(TYPES.POINT, SUBTYPES.GEOM2D);
102
                coordinates = new double[getDimension()];
103
                coordinates[0] = point.getX();
104
                coordinates[1] = point.getY();
105
        }
106

    
107
        /**
108
         * Aplica la transformaci?nn de la matriz de transformaci?n que se pasa como
109
         * par?metro.
110
         *
111
         * @param at
112
         *            Matriz de transformaci?n.
113
         */
114
        public void transform(AffineTransform at) {
115
                at.transform(coordinates, 0, coordinates, 0, 1);
116
        }
117

    
118
        /*
119
         * (non-Javadoc)
120
         *
121
         * @see java.awt.Shape#contains(double, double)
122
         */
123
        public boolean contains(double x, double y) {
124
                if ((x == coordinates[0]) || (y == coordinates[1])) {
125
                        return true;
126
                } else {
127
                        return false;
128
                }
129
        }
130

    
131
        /*
132
         * (non-Javadoc)
133
         *
134
         * @see java.awt.Shape#contains(double, double, double, double)
135
         */
136
        public boolean contains(double x, double y, double w, double h) {
137
                return false;
138
        }
139

    
140
        /*
141
         * (non-Javadoc)
142
         *
143
         * @see java.awt.Shape#intersects(double, double, double, double)
144
         */
145
        public boolean intersects(double x, double y, double w, double h) {
146
                Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h);
147

    
148
                return rAux.contains(coordinates[0], coordinates[1]);
149
        }
150

    
151
        /*
152
         * (non-Javadoc)
153
         *
154
         * @see java.awt.Shape#getBounds()
155
         */
156
        public Rectangle getBounds() {
157
                return new Rectangle((int) coordinates[0], (int) coordinates[1], 0, 0);
158
        }
159

    
160
        /**
161
         * Devuelve la coordenada x del punto.
162
         *
163
         * @return Coordenada x.
164
         */
165
        public double getX() {
166
                return coordinates[0];
167
        }
168

    
169
        /**
170
         * Devuelve la coordenada y del punto.
171
         *
172
         * @return Coordenada y.
173
         */
174
        public double getY() {
175
                return coordinates[1];
176
        }
177

    
178
        /*
179
         * (non-Javadoc)
180
         *
181
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
182
         */
183
        public boolean contains(java.awt.geom.Point2D p) {
184
                return false;
185
        }
186

    
187
        /*
188
         * (non-Javadoc)
189
         *
190
         * @see java.awt.Shape#getBounds2D()
191
         */
192
        public Rectangle2D getBounds2D() {
193
                return new Rectangle2D.Double(coordinates[0] - 0.01, coordinates[1] - 0.01, 0.02,
194
                                0.02);
195
        }
196

    
197
        /*
198
         * (non-Javadoc)
199
         *
200
         * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
201
         */
202
        public boolean contains(Rectangle2D r) {
203
                return false;
204
        }
205

    
206
        /*
207
         * (non-Javadoc)
208
         *
209
         * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
210
         */
211
        public boolean intersects(Rectangle2D r) {
212
                return r.contains(coordinates[0], coordinates[1]);
213
        }
214

    
215
        /*
216
         * (non-Javadoc)
217
         *
218
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
219
         */
220
        public PathIterator getPathIterator(AffineTransform at) {
221
                return new PointIterator(getPoint2D(), at);
222
        }
223

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

    
234
        private java.awt.geom.Point2D getPoint2D()  {
235
                return new java.awt.geom.Point2D.Double(coordinates[0], coordinates[1]);
236
        }
237

    
238
        /**
239
         * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
240
         */
241
        public int getShapeType() {
242
                return TYPES.POINT;
243
        }
244

    
245
        /*
246
         * (non-Javadoc)
247
         *
248
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
249
         */
250
        public FShape cloneFShape() {
251
                return new Point2D(getGeometryType(), id, projection, coordinates[0], coordinates[1]);
252
        }
253

    
254
        /*
255
         * (non-Javadoc)
256
         *
257
         * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
258
         */
259
        public void reProject(ICoordTrans ct) {
260
                java.awt.geom.Point2D p = getPoint2D();
261
                p = ct.convert(p, p);
262
                coordinates[0] = p.getX();
263
                coordinates[1] = p.getY();
264
        }
265

    
266
        /*
267
         * (non-Javadoc)
268
         *
269
         * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
270
         */
271
        public Handler[] getStretchingHandlers() {
272
                ArrayList handlers = new ArrayList();
273
                handlers.add(new PointHandler(0, coordinates[0], coordinates[1]));
274
                return (Handler[]) handlers.toArray(new Handler[0]);
275
        }
276

    
277
        /*
278
         * (non-Javadoc)
279
         *
280
         * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
281
         */
282
        public Handler[] getSelectHandlers() {
283
                ArrayList handlers = new ArrayList();
284
                handlers.add(new PointHandler(0, coordinates[0], coordinates[1]));
285
                return (Handler[]) handlers.toArray(new Handler[0]);
286
        }
287

    
288
        /**
289
         * DOCUMENT ME!
290
         *
291
         * @author Vicente Caballero Navarro
292
         */
293
        class PointHandler extends AbstractHandler implements FinalHandler {
294
                /**
295
                 * Crea un nuevo PointHandler.
296
                 *
297
                 * @param x
298
                 *            DOCUMENT ME!
299
                 * @param y
300
                 *            DOCUMENT ME!
301
                 */
302
                public PointHandler(int i, double x, double y) {
303
                        point = new java.awt.geom.Point2D.Double(x, y);
304
                        index = i;
305
                }
306

    
307
                /**
308
                 * DOCUMENT ME!
309
                 *
310
                 * @param x
311
                 *            DOCUMENT ME!
312
                 * @param y
313
                 *            DOCUMENT ME!
314
                 *
315
                 * @return DOCUMENT ME!
316
                 */
317
                public void move(double x, double y) {
318
                        coordinates[0] = coordinates[0] + x;
319
                        coordinates[1] = coordinates[1] + y;
320
                }
321

    
322
                /**
323
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
324
                 */
325
                public void set(double x, double y) {
326
                        coordinates[0] = x;
327
                        coordinates[1] = y;
328
                }
329
        }
330

    
331
        /*
332
         * (non-Javadoc)
333
         *
334
         * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
335
         */
336
        public int getDimension() {
337
                return 2;
338
        }
339

    
340
        public Envelope getEnvelope() {
341
                return new Envelope2D(coordinates[0] - 0.01, coordinates[1] - 0.01,coordinates[0]+0.02,coordinates[1]+
342
                                0.02);
343
        }
344

    
345
        public GeneralPathX getGeneralPath() {
346
                // TODO Auto-generated method stub
347
                return null;
348
        }
349

    
350
        public DirectPosition getDirectPosition() {
351
                return this;
352
        }
353

    
354
        public double getOrdinate(int dim) {
355
                if (dim == 0) {
356
                        return getX();
357
                } else if (dim == 1) {
358
                        return getY();
359
                } else {
360
                        return 0;
361
                }
362
        }
363

    
364
        public boolean equals(Object other) {
365
                if (!super.equals(other)) {
366
                        return false;
367
                }
368
                Point2D pother = (Point2D) other;
369
                if (Math.abs(this.getX() - pother.getX()) > 0.0000001) {
370
                        return false;
371
                }
372
                if (Math.abs(this.getY() - pother.getY()) > 0.0000001) {
373
                        return false;
374
                }
375
                return true;
376

    
377
        }
378

    
379
        /* (non-Javadoc)
380
         * @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
381
         */
382
        public double[] getCoordinates() {
383
                return coordinates;
384
        }
385

    
386
        /* (non-Javadoc)
387
         * @see org.gvsig.fmap.geom.primitive.Point#getDoordinateAt(int)
388
         */
389
        public double getCoordinateAt(int dimension) {
390
                return coordinates[dimension];
391
        }
392

    
393
        /* (non-Javadoc)
394
         * @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
395
         */
396
        public void setCoordinateAt(int dimension, double value) {
397
                coordinates[dimension] = value;
398
        }
399

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

    
407
        /* (non-Javadoc)
408
         * @see org.gvsig.fmap.geom.primitive.Point#setX(double)
409
         */
410
        public void setX(double x) {
411
                coordinates[0] = x;
412
        }
413

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

    
421
        public String toString() {
422
                return "Point2D(" + coordinates[0] + "," + coordinates[1] + ")";
423
        }
424

    
425

    
426

    
427
}