Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / primitive / impl / OrientablePrimitive2D.java @ 40435

History | View | Annotate | Download (14.9 KB)

1 40435 jjdelcerro
package org.gvsig.fmap.geom.primitive.impl;
2
3
import java.awt.Rectangle;
4
import java.awt.Shape;
5
import java.awt.geom.AffineTransform;
6
import java.awt.geom.Line2D;
7
import java.awt.geom.PathIterator;
8
import java.awt.geom.Point2D;
9
import java.awt.geom.Rectangle2D;
10
import java.util.ArrayList;
11
12
import org.cresques.cts.ICoordTrans;
13
import org.cresques.cts.IProjection;
14
15
import org.gvsig.fmap.geom.GeometryException;
16
import org.gvsig.fmap.geom.GeometryLocator;
17
import org.gvsig.fmap.geom.exception.CreateGeometryException;
18
import org.gvsig.fmap.geom.handler.AbstractHandler;
19
import org.gvsig.fmap.geom.handler.FinalHandler;
20
import org.gvsig.fmap.geom.handler.Handler;
21
import org.gvsig.fmap.geom.impl.DrawGeometryShape;
22
import org.gvsig.fmap.geom.primitive.Envelope;
23
import org.gvsig.fmap.geom.primitive.GeneralPathX;
24
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
25
import org.gvsig.fmap.geom.primitive.Point;
26
import org.gvsig.fmap.geom.type.GeometryType;
27
import org.gvsig.tools.locator.LocatorException;
28
29
30
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
31
 *
32
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
33
 *
34
 * This program is free software; you can redistribute it and/or
35
 * modify it under the terms of the GNU General Public License
36
 * as published by the Free Software Foundation; either version 2
37
 * of the License, or (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
47
 *
48
 * For more information, contact:
49
 *
50
 *  Generalitat Valenciana
51
 *   Conselleria d'Infraestructures i Transport
52
 *   Av. Blasco Ib��ez, 50
53
 *   46010 VALENCIA
54
 *   SPAIN
55
 *
56
 *      +34 963862235
57
 *   gvsig@gva.es
58
 *      www.gvsig.gva.es
59
 *
60
 *    or
61
 *
62
 *   IVER T.I. S.A
63
 *   Salamanca 50
64
 *   46005 Valencia
65
 *   Spain
66
 *
67
 *   +34 963163400
68
 *   dac@iver.es
69
 */
70
/* CVS MESSAGES:
71
 *
72
 * $Id: FOrientablePrimitive2D.java,v 1.1 2008/03/12 08:46:20 cvs Exp $
73
 * $Log: FOrientablePrimitive2D.java,v $
74
 * Revision 1.1  2008/03/12 08:46:20  cvs
75
 * *** empty log message ***
76
 *
77
 *
78
 */
79
/**
80
 * @author Jorge Piera Llodr� (jorge.piera@iver.es)
81
 */
82
public abstract class OrientablePrimitive2D extends AbstractPrimitive implements OrientablePrimitive {
83
    private static final long serialVersionUID = -820881421374434713L;
84
    protected GeneralPathX gp;
85
86
    /**
87
     * The constructor with the GeometryType like and argument
88
     * is used by the {@link GeometryType}{@link #create()}
89
     * to create the geometry
90
     * @param type
91
     * The geometry type
92
     */
93
    public OrientablePrimitive2D(GeometryType geometryType) {
94
        super(geometryType, null, null);
95
        gp = new GeneralPathX();
96
    }
97
98
    public OrientablePrimitive2D(GeometryType geometryType, String id, IProjection projection, GeneralPathX gp){
99
        super(geometryType, id, projection);
100
        this.gp = gp;
101
    }
102
103
    /**
104
     * TODO método creado para dar visibilidad a gp despues de la refactorización
105
     * @return
106
     */
107
    public GeneralPathX getGeneralPathX() {
108
        return gp;
109
    }
110
111
    /* (non-Javadoc)
112
     * @see java.awt.Shape#contains(double, double)
113
     */
114
    public boolean contains(double x, double y) {
115
        return gp.contains(x, y);
116
    }
117
118
    /* (non-Javadoc)
119
     * @see java.awt.Shape#contains(double, double, double, double)
120
     */
121
    public boolean contains(double x, double y, double w, double h) {
122
        return gp.contains(x, y, w, h);
123
    }
124
125
    /* (non-Javadoc)
126
     * @see java.awt.Shape#intersects(double, double, double, double)
127
     */
128
    public boolean intersects(double x, double y, double w, double h) {
129
        // M�s r�pido
130
        return gp.intersects(x, y, w, h);
131
    }
132
133
    /* (non-Javadoc)
134
     * @see java.awt.Shape#getBounds()
135
     */
136
    public Rectangle getBounds() {
137
        return gp.getBounds();
138
    }
139
140
    /* (non-Javadoc)
141
     * @see java.awt.Shape#contains(java.awt.geom.Point2D)
142
     */
143
    public boolean contains(Point2D p) {
144
        return gp.contains(p);
145
    }
146
147
    /* (non-Javadoc)
148
     * @see java.awt.Shape#getBounds2D()
149
     */
150
    public Rectangle2D getBounds2D() {
151
        return gp.getBounds2D();
152
    }
153
154
    /* (non-Javadoc)
155
     * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
156
     */
157
    public boolean contains(Rectangle2D r) {
158
        return gp.contains(r);
159
    }
160
161
    /**
162
     * El m�todo intersects de java.awt.Shape que define la intersecci�n entre
163
     * una polil�nea y un Rectangle2D considera la polil�nea como un Shape
164
     * gen�rico y se producen errores en la selecci�n de polil�neas. Por este
165
     * motivo se ha modificado este m�todo intersect() de FPolyline2D para que
166
     * realize la intersecci�n estricta entre el Rectangle2D y la polil�nea en
167
     * cuesti�n. El precio es un incremento de tiempo m�ximo del 50%.
168
     *
169
     * @param r Rect�ngulo.
170
     *
171
     * @return True si intersecta con el rectangulo que se pasa como par�metro.
172
     */
173
    public boolean intersects(Rectangle2D r) {
174
        //return gp.intersects(r);
175
        // M�s exacto
176
        boolean bool = false;
177
        if (gp.intersects(r)) {
178
            ArrayList arrayCoords;
179
            int theType;
180
            //Use this array to store segment coordinate data
181
            double[] theData = new double[6];
182
            PathIterator theIterator;
183
184
            Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
185
            Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
186
            Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
187
            Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
188
            Line2D l1 = new Line2D.Double(p1,p2);
189
            Line2D l2 = new Line2D.Double(p2,p3);
190
            Line2D l3 = new Line2D.Double(p3,p4);
191
            Line2D l4 = new Line2D.Double(p4,p1);
192
193
            theIterator = this.getPathIterator(null, geomManager.getFlatness());
194
            arrayCoords = new ArrayList();
195
            while(!theIterator.isDone()) {
196
                theType = theIterator.currentSegment(theData);
197
                if (theType==PathIterator.SEG_MOVETO) {
198
                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
199
                } else if (theType==PathIterator.SEG_LINETO) {
200
                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
201
                    Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-2);
202
                    Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),theData[0],theData[1]);
203
                    if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
204
                        || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
205
                        || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
206
                        || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
207
                        || r.intersectsLine(l)) {
208
                        bool = true;
209
                    }
210
                } else if(theType==PathIterator.SEG_CLOSE){
211
                    Point2D firstPoint=(Point2D)arrayCoords.get(0);
212
                    Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-1);
213
                    Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),firstPoint.getX(),firstPoint.getY());
214
                    if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
215
                        || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
216
                        || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
217
                        || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
218
                        || r.intersectsLine(l)) {
219
                        bool = true;
220
                    }
221
                }else {
222
                    System.out.println("Not supported here");
223
                }
224
                theIterator.next();
225
            }
226
        }
227
        return bool;
228
    }
229
230
    /* (non-Javadoc)
231
     * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
232
     */
233
    public PathIterator getPathIterator(AffineTransform at) {
234
        return gp.getPathIterator(at);
235
    }
236
237
    /* (non-Javadoc)
238
     * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
239
     */
240
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
241
        return gp.getPathIterator(at, flatness);
242
    }
243
244
    /**
245
     * DOCUMENT ME!
246
     *
247
     * @param at DOCUMENT ME!
248
     */
249
    public void transform(AffineTransform at) {
250
251
        // TODO: PRUEBA. BORRAR ESTA LINEA
252
        // gp = FConverter.transformToInts(gp, at);
253
254
        gp.transform(at);
255
    }
256
257
    /* (non-Javadoc)
258
     * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
259
     */
260
    public void reProject(ICoordTrans ct) {
261
        gp.reProject(ct);
262
    }
263
264
    /* (non-Javadoc)
265
     * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
266
     */
267
    public Handler[] getStretchingHandlers() {
268
        ArrayList handlers = new ArrayList();
269
        PathIterator gpi = getPathIterator(null);
270
271
        double[] theData = new double[6];
272
        int i=0;
273
        while (!gpi.isDone()) {
274
            gpi.currentSegment(theData);
275
            //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
276
            handlers.add(new PointHandler(i,theData[0], theData[1]));
277
            i++;
278
            gpi.next();
279
        }
280
281
        return (Handler[]) handlers.toArray(new Handler[0]);
282
    }
283
284
    /* (non-Javadoc)
285
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
286
     */
287
    public double getCoordinateAt(int index, int dimension) {
288
        if (index > gp.getNumCoords()){
289
            throw new ArrayIndexOutOfBoundsException();
290
        }
291
        return gp.getPointAt(index).getCoordinateAt(dimension);
292
    }
293
294
    /* (non-Javadoc)
295
     * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
296
     */
297
    public Handler[] getSelectHandlers() {
298
        Handler[] handlers = new Handler[gp.getNumCoords()];
299
        for (int i=0 ; i<gp.getNumCoords() ; i++){
300
            handlers[i] = new PointSelHandler(i, gp.getPointAt(i).getX(), gp.getPointAt(i).getY());
301
        }
302
        return handlers;
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 DOCUMENT ME!
315
         * @param y DOCUMENT ME!
316
         */
317
        public PointHandler(int i,double x, double y) {
318
            point = new Point2D.Double(x, y);
319
            index = i;
320
        }
321
322
        /**
323
         * DOCUMENT ME!
324
         *
325
         * @param x DOCUMENT ME!
326
         * @param y DOCUMENT ME!
327
         *
328
         * @return DOCUMENT ME!
329
         */
330
        public void move(double x, double y) {
331
            Point point = gp.getPointAt(index);
332
            point.setX(point.getX() + x);
333
            point.setY(point.getY() + y);
334
        }
335
336
        /**
337
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
338
         */
339
        public void set(double x, double y) {
340
            Point point = gp.getPointAt(index);
341
            point.setX(x);
342
            point.setY(y);
343
        }
344
    }
345
    /**
346
     * DOCUMENT ME!
347
     *
348
     * @author Vicente Caballero Navarro
349
     */
350
    class PointSelHandler extends AbstractHandler implements FinalHandler{
351
        /**
352
         * Crea un nuevo PointHandler.
353
         *
354
         * @param x DOCUMENT ME!
355
         * @param y DOCUMENT ME!
356
         */
357
        public PointSelHandler(int i,double x, double y) {
358
            point = new Point2D.Double(x, y);
359
            index = i;
360
        }
361
362
        /**
363
         * DOCUMENT ME!
364
         *
365
         * @param x DOCUMENT ME!
366
         * @param y DOCUMENT ME!
367
         *
368
         * @return DOCUMENT ME!
369
         */
370
        public void move(double x, double y) {
371
            Point point = gp.getPointAt(index);
372
            point.setX(point.getX() + x);
373
            point.setY(point.getY() + y);
374
        }
375
376
        /**
377
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
378
         */
379
        public void set(double x, double y) {
380
            Point point = gp.getPointAt(index);
381
            point.setX(x);
382
            point.setY(y);
383
        }
384
    }
385
386
    /*
387
     * (non-Javadoc)
388
     * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
389
     */
390
    public int getDimension() {
391
        return 2;
392
    }
393
394
    public Envelope getEnvelope() {
395
        Rectangle2D r=gp.getBounds2D();
396
        return new Envelope2D(r.getX(),r.getY(),r.getMaxX(),r.getMaxY());
397
    }
398
399
    public GeneralPathX getGeneralPath() {
400
        return gp;
401
    }
402
403
    public int getNumVertices() {
404
        return gp.getNumCoords();
405
    }
406
407
    public void addVertex(Point point) {
408
            if( this.getNumVertices()== 0 ) {
409
                    gp.moveTo(point);
410
            } else {
411
                    gp.lineTo(point);
412
            }
413
    }
414
415
    public void addVertex(double x, double y)  {
416
                try {
417
                    Point point;
418
                        point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D);
419
                    addVertex(point);
420
                } catch (CreateGeometryException e) {
421
                        throw new RuntimeException(e.getMessage());
422
                }
423
    }
424
425
    public void addVertex(double x, double y, double z) {
426
                try {
427
                    Point point;
428
                        point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM3D);
429
                        point.setCoordinateAt(DIMENSIONS.Z, z);
430
                    addVertex(point);
431
                } catch (CreateGeometryException e) {
432
                        throw new RuntimeException(e.getMessage());
433
                }
434
    }
435
436
    public void addMoveToVertex(Point point) {
437
        gp.moveTo(point);
438
    }
439
440
    public void closePrimitive() {
441
        gp.closePath();
442
    }
443
444
    public void setVertex(int index, Point p) {
445
        throw new UnsupportedOperationException("Method not implemented");
446
    }
447
448
    public Point getVertex(int index) {
449
        return gp.getPointAt(index);
450
    }
451
452
    public void insertVertex(int index, Point p) {
453
        throw new UnsupportedOperationException("Method not implemented");
454
    }
455
456
    public void removeVertex(int index) {
457
        throw new UnsupportedOperationException("Method not implemented");
458
    }
459
460
    public void setCoordinateAt(int index, int dimension, double value) {
461
        gp.getPointAt(index).setCoordinateAt(dimension, value);
462
    }
463
464
    public void setGeneralPath(GeneralPathX generalPathX) {
465
        this.gp = generalPathX;
466
    }
467
468
    public void setPoints(Point startPoint, Point endPoint) {
469
        throw new UnsupportedOperationException("Method not implemented");
470
    }
471
472
    public Shape getShape() {
473
        return new DrawGeometryShape(this);
474
    }
475
476
    public Shape getShape(AffineTransform affineTransform) {
477
        return new DrawGeometryShape(this, affineTransform);
478
    }
479
}