Statistics
| Revision:

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

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

    
52
import org.gvsig.fmap.geom.DirectPosition;
53
import org.gvsig.fmap.geom.Geometry;
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
        
71
        protected static final String COORDINATES_FIELD = "coordinates";
72
        
73
        private static final long serialVersionUID = 1836257305083881299L;
74
        protected double[] coordinates;
75
                
76
        /**
77
         * The constructor with the GeometryType like and argument 
78
         * is used by the {@link GeometryType}{@link #create()}
79
         * to create the geometry
80
         * @param type
81
         * The geometry type
82
         */
83
        public Point2D(GeometryType geometryType) {
84
                this(geometryType, null, null, 0.0, 0.0);
85
        }        
86
                
87
        /**
88
         * Constructor used in the {@link Geometry#cloneGeometry()} method
89
         */
90
        Point2D(GeometryType geometryType, String id, IProjection projection, double x, double y) {
91
                super(geometryType, id, projection);
92
                coordinates = new double[getDimension()];
93
                coordinates[0] = x;
94
                coordinates[1] = y;
95
        }
96
        
97
        public Point2D (double x, double y) {
98
                super(TYPES.POINT, SUBTYPES.GEOM2D);
99
                coordinates = new double[getDimension()];
100
                coordinates[0] = x;
101
                coordinates[1] = y;
102
        }
103
        
104
   public Point2D (int type, int subtype) {
105
        super(type, subtype);    
106
        coordinates = new double[getDimension()];
107
    }
108
        
109
        public Point2D (java.awt.geom.Point2D point) {
110
                super(TYPES.POINT, SUBTYPES.GEOM2D);
111
                coordinates = new double[getDimension()];
112
                coordinates[0] = point.getX();
113
                coordinates[1] = point.getY();
114
        }
115

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
254
        /*
255
         * (non-Javadoc)
256
         *
257
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
258
         */
259
        public FShape cloneFShape() {
260
                return new Point2D(getGeometryType(), id, projection, coordinates[0], coordinates[1]);
261
        }
262

    
263
        /*
264
         * (non-Javadoc)
265
         *
266
         * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
267
         */
268
        public void reProject(ICoordTrans ct) {
269
                java.awt.geom.Point2D p = getPoint2D();
270
                p = ct.convert(p, p);
271
                coordinates[0] = p.getX();
272
                coordinates[1] = p.getY();
273
        }
274

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

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

    
297
        /**
298
         * DOCUMENT ME!
299
         *
300
         * @author Vicente Caballero Navarro
301
         */
302
        class PointHandler extends AbstractHandler implements FinalHandler {
303
                /**
304
                 * Crea un nuevo PointHandler.
305
                 *
306
                 * @param x
307
                 *            DOCUMENT ME!
308
                 * @param y
309
                 *            DOCUMENT ME!
310
                 */
311
                public PointHandler(int i, double x, double y) {
312
                        point = new java.awt.geom.Point2D.Double(x, y);
313
                        index = i;
314
                }
315

    
316
                /**
317
                 * DOCUMENT ME!
318
                 *
319
                 * @param x
320
                 *            DOCUMENT ME!
321
                 * @param y
322
                 *            DOCUMENT ME!
323
                 *
324
                 * @return DOCUMENT ME!
325
                 */
326
                public void move(double x, double y) {
327
                        coordinates[0] = coordinates[0] + x;
328
                        coordinates[1] = coordinates[1] + y;
329
                }
330

    
331
                /**
332
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
333
                 */
334
                public void set(double x, double y) {
335
                        coordinates[0] = x;
336
                        coordinates[1] = y;
337
                }
338
        }
339

    
340
        /*
341
         * (non-Javadoc)
342
         *
343
         * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
344
         */
345
        public int getDimension() {
346
                return 2;
347
        }
348

    
349
        public Envelope getEnvelope() {
350
                return new Envelope2D(coordinates[0] - 0.01, coordinates[1] - 0.01,coordinates[0]+0.02,coordinates[1]+
351
                                0.02);
352
        }
353

    
354
        public GeneralPathX getGeneralPath() {
355
                // TODO Auto-generated method stub
356
                return null;
357
        }
358

    
359
        public DirectPosition getDirectPosition() {
360
                return this;
361
        }
362

    
363
        public double getOrdinate(int dim) {
364
                if (dim == 0) {
365
                        return getX();
366
                } else if (dim == 1) {
367
                        return getY();
368
                } else {
369
                        return 0;
370
                }
371
        }
372

    
373
        public boolean equals(Object other) {
374
                if (!super.equals(other)) {
375
                        return false;
376
                }
377
                Point2D pother = (Point2D) other;
378
                if (Math.abs(this.getX() - pother.getX()) > 0.0000001) {
379
                        return false;
380
                }
381
                if (Math.abs(this.getY() - pother.getY()) > 0.0000001) {
382
                        return false;
383
                }
384
                return true;
385

    
386
        }
387

    
388
        /* (non-Javadoc)
389
         * @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
390
         */
391
        public double[] getCoordinates() {
392
                return coordinates;
393
        }
394

    
395
        /* (non-Javadoc)
396
         * @see org.gvsig.fmap.geom.primitive.Point#getDoordinateAt(int)
397
         */
398
        public double getCoordinateAt(int dimension) {
399
                return coordinates[dimension];
400
        }
401

    
402
        /* (non-Javadoc)
403
         * @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
404
         */
405
        public void setCoordinateAt(int dimension, double value) {
406
                coordinates[dimension] = value;
407
        }
408

    
409
        /* (non-Javadoc)
410
         * @see org.gvsig.fmap.geom.primitive.Point#setCoordinates(double[])
411
         */
412
        public void setCoordinates(double[] values) {
413
                coordinates = values;
414
        }
415

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

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

    
430
        public String toString() {
431
                StringBuffer buffer = new StringBuffer();
432
                buffer.append("Point2D(").append(coordinates[0]);
433
                for(int i=1 ; i<coordinates.length; i++ ) {
434
                        buffer.append(",").append(coordinates[i]);
435
                }
436
                buffer.append(")");
437
                return buffer.toString();
438
        }        
439
}