Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2060 / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Point2D.java @ 39365

History | View | Annotate | Download (8.9 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * 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
 * 
16
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.fmap.geom.primitive.impl;
23

    
24
import java.awt.Rectangle;
25
import java.awt.Shape;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.PathIterator;
28
import java.awt.geom.Rectangle2D;
29

    
30
import org.cresques.cts.ICoordTrans;
31
import org.cresques.cts.IProjection;
32

    
33
import org.gvsig.fmap.geom.DirectPosition;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.handler.AbstractHandler;
36
import org.gvsig.fmap.geom.handler.FinalHandler;
37
import org.gvsig.fmap.geom.handler.Handler;
38
import org.gvsig.fmap.geom.primitive.Envelope;
39
import org.gvsig.fmap.geom.primitive.FShape;
40
import org.gvsig.fmap.geom.primitive.GeneralPathX;
41
import org.gvsig.fmap.geom.primitive.Point;
42
import org.gvsig.fmap.geom.primitive.PointIterator;
43
import org.gvsig.fmap.geom.type.GeometryType;
44

    
45
/**
46
 * 2D point implementation.
47
 * 
48
 * @author Vicente Caballero Navarro
49
 * @author gvSIG team
50
 */
51
public class Point2D extends AbstractPrimitive implements Point, DirectPosition {
52
        
53
        private static final long serialVersionUID = 1836257305083881299L;
54

    
55
    protected static final String COORDINATES_FIELD = "coordinates";
56
    protected double x;
57
    protected double y;
58

    
59
    /**
60
     * The constructor with the GeometryType like and argument
61
     * is used by the {@link GeometryType}{@link #create()} to create the
62
     * geometry
63
     * 
64
     * @param type
65
     *            The geometry type
66
     */
67
    public Point2D(GeometryType geometryType) {
68
            super(geometryType, null, null);
69
        this.x = 0.0d;
70
        this.y = 0.0d;
71
    }
72

    
73
    /**
74
     * Constructor used in the {@link Geometry#cloneGeometry()} method
75
     */
76
    Point2D(GeometryType geometryType, String id, IProjection projection,
77
        double x, double y) {
78
        super(geometryType, id, projection);
79
        this.x = x;
80
        this.y = y;
81
    }
82

    
83
    public Point2D(double x, double y) {
84
        super(TYPES.POINT, SUBTYPES.GEOM2D);
85
        this.x = x;
86
        this.y = y;
87
    }
88

    
89
    public Point2D(GeometryType geometryType, double x, double y) {
90
        super(geometryType, null, null);
91
        this.x = x;
92
        this.y = y;
93
    }
94

    
95
    public Point2D(int type, int subtype) {
96
        super(type, subtype);
97
        this.x = 0.0d;
98
        this.y = 0.0d;
99
    }
100

    
101
    public Point2D(java.awt.geom.Point2D point) {
102
        super(TYPES.POINT, SUBTYPES.GEOM2D);
103
        this.x = point.getX();
104
        this.y = 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
        
116
        if (at == null) {
117
            return;
118
        }
119
        
120
        double[] coordinates = getCoordinates();
121
        at.transform(coordinates, 0, coordinates, 0, 1);
122
        setCoordinates(coordinates);
123
    }
124

    
125
    public boolean contains(double x, double y) {
126
        if ((this.x == x) || (this.y == y)) {
127
            return true;
128
        } else {
129
            return false;
130
        }
131
    }
132

    
133
    public boolean contains(double x, double y, double w, double h) {
134
        return false;
135
    }
136

    
137
    public boolean intersects(double x, double y, double w, double h) {
138
        Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h);
139

    
140
        return rAux.contains(this.x, this.y);
141
    }
142

    
143
    public Rectangle getBounds() {
144
        return new Rectangle((int) x, (int) y, 0, 0);
145
    }
146

    
147
    public double getX() {
148
        return x;
149
    }
150

    
151
    public double getY() {
152
        return y;
153
    }
154

    
155
    public boolean contains(java.awt.geom.Point2D p) {
156
        return false;
157
    }
158

    
159
    public Rectangle2D getBounds2D() {
160
        return new Rectangle2D.Double(x - 0.01, y - 0.01, 0.02, 0.02);
161
    }
162

    
163
    public boolean contains(Rectangle2D r) {
164
        return false;
165
    }
166

    
167
    public boolean intersects(Rectangle2D r) {
168
        return r.contains(x, y);
169
    }
170

    
171
    public Shape getShape(AffineTransform affineTransform) {
172
        return this;
173
    }
174

    
175
    public PathIterator getPathIterator(AffineTransform at) {
176
        return new PointIterator(getPoint2D(), at);
177
    }
178

    
179
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
180
        return new PointIterator(getPoint2D(), at);
181
    }
182

    
183
    private java.awt.geom.Point2D getPoint2D() {
184
        return new java.awt.geom.Point2D.Double(x, y);
185
    }
186

    
187
    public int getShapeType() {
188
        return TYPES.POINT;
189
    }
190

    
191
    public FShape cloneFShape() {
192
        return new Point2D(getGeometryType(), id, projection, x, y);
193
    }
194

    
195
    public void reProject(ICoordTrans ct) {
196
        java.awt.geom.Point2D p = getPoint2D();
197
        p = ct.convert(p, p);
198
        this.x = p.getX();
199
        this.y = p.getY();
200
    }
201

    
202
    public Handler[] getStretchingHandlers() {
203
        return new Handler[] { new PointHandler(0, x, y, this) };
204
    }
205

    
206
    public Handler[] getSelectHandlers() {
207
        return new Handler[] { new PointHandler(0, x, y, this) };
208
    }
209

    
210
    /**
211
     * 
212
     * @author Vicente Caballero Navarro
213
     * @author gvSIG team
214
     */
215
    class PointHandler extends AbstractHandler implements FinalHandler {
216

    
217
        private final Point gvSIGPoint;
218

    
219
        public PointHandler(int i, double x, double y, Point gvSIGPoint) {
220
            this.gvSIGPoint = gvSIGPoint;
221
            point = new java.awt.geom.Point2D.Double(x, y);
222
            index = i;
223
        }
224

    
225
        public void move(double movex, double movey) {
226
            gvSIGPoint.setX(gvSIGPoint.getX() + movex);
227
            gvSIGPoint.setY(gvSIGPoint.getY() + movey);
228
        }
229

    
230
        public void set(double setx, double sety) {
231
            gvSIGPoint.setX(setx);
232
            gvSIGPoint.setY(sety);
233
        }
234
    }
235

    
236
    public int getDimension() {
237
        return 2;
238
    }
239

    
240
    public Envelope getEnvelope() {
241
        return new Envelope2D(x - 0.01, y - 0.01, x + 0.02, y + 0.02);
242
    }
243

    
244
    public GeneralPathX getGeneralPath() {
245
        return null;
246
    }
247

    
248
    public DirectPosition getDirectPosition() {
249
        return this;
250
    }
251

    
252
    public double getOrdinate(int dim) {
253
        if (dim == Geometry.DIMENSIONS.X) {
254
            return getX();
255
        } else
256
            if (dim == Geometry.DIMENSIONS.Y) {
257
                return getY();
258
            } else {
259
                return 0;
260
            }
261
    }
262

    
263
    public boolean equals(Object other) {
264
        if (!super.equals(other)) {
265
            return false;
266
        }
267
        Point2D pother = (Point2D) other;
268
        if (Math.abs(this.getX() - pother.getX()) > 0.0000001) {
269
            return false;
270
        }
271
        if (Math.abs(this.getY() - pother.getY()) > 0.0000001) {
272
            return false;
273
        }
274
        return true;
275

    
276
    }
277

    
278
    public double[] getCoordinates() {
279
        return new double[] { x, y };
280
    }
281

    
282
    public double getCoordinateAt(int dimension) {
283
        switch (dimension) {
284
        case Geometry.DIMENSIONS.X:
285
            return x;
286
        case Geometry.DIMENSIONS.Y:
287
            return y;
288
        default:
289
            throw new ArrayIndexOutOfBoundsException(dimension);
290
        }
291
    }
292

    
293
    public void setCoordinateAt(int dimension, double value) {
294
        switch (dimension) {
295
        case Geometry.DIMENSIONS.X:
296
            this.x = value;
297
            break;
298
        case Geometry.DIMENSIONS.Y:
299
            this.y = value;
300
            break;
301
        default:
302
            throw new ArrayIndexOutOfBoundsException(dimension);
303
        }
304
    }
305

    
306
    public void setCoordinates(double[] values) {
307
        this.x = values[Geometry.DIMENSIONS.X];
308
        this.y = values[Geometry.DIMENSIONS.Y];
309
    }
310

    
311
    public void setX(double x) {
312
        this.x = x;
313
    }
314

    
315
    public void setY(double y) {
316
        this.y = y;
317
    }
318

    
319
    public String toString() {
320
        StringBuffer buffer = new StringBuffer();
321
        double[] coordinates = getCoordinates();
322
        buffer.append(getFullTypeName()).append("(").append(coordinates[0]);
323
        for (int i = 1; i < coordinates.length; i++) {
324
            buffer.append(",").append(coordinates[i]);
325
        }
326
        buffer.append(")");
327
        return buffer.toString();
328
    }
329

    
330
    protected String getFullTypeName() {
331
        return "Point2D";
332
    }
333
    
334
    public int getType() {
335
        return TYPES.POINT;
336
    }
337
}