Statistics
| Revision:

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

History | View | Annotate | Download (15.2 KB)

1
package org.gvsig.fmap.geom.primitive.impl;
2

    
3
import java.awt.Rectangle;
4
import java.awt.geom.AffineTransform;
5
import java.awt.geom.Line2D;
6
import java.awt.geom.PathIterator;
7
import java.awt.geom.Point2D;
8
import java.awt.geom.Rectangle2D;
9
import java.util.ArrayList;
10

    
11
import org.cresques.cts.ICoordTrans;
12
import org.cresques.cts.IProjection;
13

    
14
import org.gvsig.fmap.geom.handler.AbstractHandler;
15
import org.gvsig.fmap.geom.handler.FinalHandler;
16
import org.gvsig.fmap.geom.handler.Handler;
17
import org.gvsig.fmap.geom.primitive.Envelope;
18
import org.gvsig.fmap.geom.primitive.GeneralPathX;
19
import org.gvsig.fmap.geom.primitive.GeneralPathXIterator;
20
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
21
import org.gvsig.fmap.geom.primitive.Point;
22
import org.gvsig.fmap.geom.type.GeometryType;
23

    
24

    
25
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
26
 *
27
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
42
 *
43
 * For more information, contact:
44
 *
45
 *  Generalitat Valenciana
46
 *   Conselleria d'Infraestructures i Transport
47
 *   Av. Blasco Ib??ez, 50
48
 *   46010 VALENCIA
49
 *   SPAIN
50
 *
51
 *      +34 963862235
52
 *   gvsig@gva.es
53
 *      www.gvsig.gva.es
54
 *
55
 *    or
56
 *
57
 *   IVER T.I. S.A
58
 *   Salamanca 50
59
 *   46005 Valencia
60
 *   Spain
61
 *
62
 *   +34 963163400
63
 *   dac@iver.es
64
 */
65
/* CVS MESSAGES:
66
 *
67
 * $Id: FOrientablePrimitive2D.java,v 1.1 2008/03/12 08:46:20 cvs Exp $
68
 * $Log: FOrientablePrimitive2D.java,v $
69
 * Revision 1.1  2008/03/12 08:46:20  cvs
70
 * *** empty log message ***
71
 *
72
 *
73
 */
74
/**
75
 * @author Jorge Piera Llodr? (jorge.piera@iver.es)
76
 */
77
public abstract class OrientablePrimitive2D extends AbstractPrimitive implements OrientablePrimitive {
78
    private static final long serialVersionUID = -820881421374434713L;
79
    protected GeneralPathX gp;
80

    
81
    /**
82
     * The constructor with the GeometryType like and argument 
83
     * is used by the {@link GeometryType}{@link #create()}
84
     * to create the geometry
85
     * @param type
86
     * The geometry type
87
     */
88
    public OrientablePrimitive2D(GeometryType geometryType) {
89
        super(geometryType, null, null);
90
    }
91

    
92
    public OrientablePrimitive2D(GeometryType geometryType, String id, IProjection projection, GeneralPathX gp){
93
        super(geometryType, id, projection);
94
        this.gp = gp;                
95
    }
96

    
97
    /**
98
     * TODO m?todo creado para dar visibilidad a gp despues de la refactorizaci?n
99
     * @return
100
     */
101
    public GeneralPathX getGeneralPathX() {
102
        return gp;
103
    }
104

    
105
    /* (non-Javadoc)
106
     * @see java.awt.Shape#contains(double, double)
107
     */
108
    public boolean contains(double x, double y) {
109
        return gp.contains(x, y);
110
    }
111

    
112
    /* (non-Javadoc)
113
     * @see java.awt.Shape#contains(double, double, double, double)
114
     */
115
    public boolean contains(double x, double y, double w, double h) {
116
        return gp.contains(x, y, w, h);
117
    }
118

    
119
    /* (non-Javadoc)
120
     * @see java.awt.Shape#intersects(double, double, double, double)
121
     */
122
    public boolean intersects(double x, double y, double w, double h) {
123
        // M?s r?pido
124
        return gp.intersects(x, y, w, h);
125
    }
126

    
127
    /* (non-Javadoc)
128
     * @see java.awt.Shape#getBounds()
129
     */
130
    public Rectangle getBounds() {
131
        return gp.getBounds();
132
    }
133

    
134
    /* (non-Javadoc)
135
     * @see java.awt.Shape#contains(java.awt.geom.Point2D)
136
     */
137
    public boolean contains(Point2D p) {
138
        return gp.contains(p);
139
    }
140

    
141
    /* (non-Javadoc)
142
     * @see java.awt.Shape#getBounds2D()
143
     */
144
    public Rectangle2D getBounds2D() {
145
        return gp.getBounds2D();
146
    }
147

    
148
    /* (non-Javadoc)
149
     * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
150
     */
151
    public boolean contains(Rectangle2D r) {
152
        return gp.contains(r);
153
    }
154

    
155
    /**
156
     * El m?todo intersects de java.awt.Shape que define la intersecci?n entre
157
     * una polil?nea y un Rectangle2D considera la polil?nea como un Shape
158
     * gen?rico y se producen errores en la selecci?n de polil?neas. Por este
159
     * motivo se ha modificado este m?todo intersect() de FPolyline2D para que
160
     * realize la intersecci?n estricta entre el Rectangle2D y la polil?nea en
161
     * cuesti?n. El precio es un incremento de tiempo m?ximo del 50%.
162
     *
163
     * @param r Rect?ngulo.
164
     *
165
     * @return True si intersecta con el rectangulo que se pasa como par?metro.
166
     */
167
    public boolean intersects(Rectangle2D r) {
168
        //return gp.intersects(r);
169
        // M?s exacto
170
        boolean bool = false;
171
        if (gp.intersects(r)) {
172
            ArrayList arrayCoords;
173
            int theType;
174
            //Use this array to store segment coordinate data
175
            double[] theData = new double[6];
176
            PathIterator theIterator;
177

    
178
            Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
179
            Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
180
            Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
181
            Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
182
            Line2D l1 = new Line2D.Double(p1,p2);
183
            Line2D l2 = new Line2D.Double(p2,p3);
184
            Line2D l3 = new Line2D.Double(p3,p4);
185
            Line2D l4 = new Line2D.Double(p4,p1);
186

    
187
            theIterator = this.getPathIterator(null, geomManager.getFlatness());
188
            arrayCoords = new ArrayList();
189
            while(!theIterator.isDone()) {
190
                theType = theIterator.currentSegment(theData);
191
                if (theType==PathIterator.SEG_MOVETO) {
192
                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
193
                } else if (theType==PathIterator.SEG_LINETO) {
194
                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
195
                    Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-2);
196
                    Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),theData[0],theData[1]);
197
                    if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
198
                        || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
199
                        || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
200
                        || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
201
                        || r.intersectsLine(l)) {
202
                        bool = true;
203
                    }
204
                } else if(theType==PathIterator.SEG_CLOSE){
205
                    Point2D firstPoint=(Point2D)arrayCoords.get(0);
206
                    Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-1);
207
                    Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),firstPoint.getX(),firstPoint.getY());
208
                    if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
209
                        || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
210
                        || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
211
                        || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
212
                        || r.intersectsLine(l)) {
213
                        bool = true;
214
                    }
215
                }else {
216
                    System.out.println("Not supported here");
217
                }
218
                theIterator.next();
219
            }
220
        }
221
        return bool;
222
    }
223

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

    
231
    /* (non-Javadoc)
232
     * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
233
     */
234
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
235
        return gp.getPathIterator(at, flatness);
236
    }
237

    
238
    /**
239
     * DOCUMENT ME!
240
     *
241
     * @param at DOCUMENT ME!
242
     */
243
    public void transform(AffineTransform at) {
244

    
245
        // TODO: PRUEBA. BORRAR ESTA LINEA
246
        // gp = FConverter.transformToInts(gp, at);
247

    
248
        gp.transform(at);
249
    }
250

    
251
    /* (non-Javadoc)
252
     * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
253
     */
254
    public void reProject(ICoordTrans ct) {
255
        gp.reProject(ct);
256
    }
257

    
258
    /* (non-Javadoc)
259
     * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
260
     */
261
    public Handler[] getStretchingHandlers() {
262
        ArrayList handlers = new ArrayList();
263
        GeneralPathXIterator gpi = null;
264
        gpi = (GeneralPathXIterator) getPathIterator(null);
265

    
266
        double[] theData = new double[6];
267
        int i=0;
268
        while (!gpi.isDone()) {
269
            gpi.currentSegment(theData);
270
            //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
271
            handlers.add(new PointHandler(i,theData[0], theData[1]));
272
            i++;
273
            gpi.next();
274
        }
275

    
276
        return (Handler[]) handlers.toArray(new Handler[0]);
277
    }
278

    
279
    /* (non-Javadoc)
280
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
281
     */
282
    public double getCoordinateAt(int index, int dimension) {
283
        GeneralPathXIterator gpi = null;
284
        gpi = (GeneralPathXIterator) getPathIterator(null);
285

    
286
        double[] theData = new double[6];
287
        int i=0;
288
        while (!gpi.isDone()) {
289
            if (index==i){
290
                gpi.currentSegment(theData);
291
                return theData[dimension];
292
            }
293
            i++;
294
            gpi.next();
295
        }
296
        throw new ArrayIndexOutOfBoundsException();
297
    }
298

    
299
    /* (non-Javadoc)
300
     * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
301
     */
302
    public Handler[] getSelectHandlers() {
303
        ArrayList handlers = new ArrayList();
304
        GeneralPathXIterator gpi = null;
305
        gpi = (GeneralPathXIterator) getPathIterator(null);
306

    
307
        double[] theData = new double[6];
308
        int i=0;
309
        //boolean isFirst=true;
310
        while (!gpi.isDone()) {
311
            int theType = gpi.currentSegment(theData);
312
            //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
313
            /* if (!(this instanceof FPolygon2D && isFirst)){
314
                                handlers.add(new PointSelHandler(i,theData[0], theData[1]));
315
                                i++;
316
                        }
317
                        isFirst=false; */
318
            switch (theType)
319
            {
320
            case GeneralPathXIterator.SEG_MOVETO:
321
                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
322
                break;
323
            case GeneralPathXIterator.SEG_LINETO:
324
                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
325
                break;
326
            case GeneralPathXIterator.SEG_CLOSE:
327
                break;
328
            case GeneralPathXIterator.SEG_QUADTO:
329
                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
330
                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
331
                break;
332
            case GeneralPathXIterator.SEG_CUBICTO:
333
                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
334
                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
335
                handlers.add(new PointSelHandler(i++,theData[4], theData[5]));
336
                break;
337

    
338
            }
339
            gpi.next();
340

    
341

    
342
        }
343

    
344
        return (Handler[]) handlers.toArray(new Handler[0]);
345
    }
346
    /**
347
     * DOCUMENT ME!
348
     *
349
     * @author Vicente Caballero Navarro
350
     */
351
    class PointHandler extends AbstractHandler implements FinalHandler{
352
        /**
353
         * Crea un nuevo PointHandler.
354
         *
355
         * @param x DOCUMENT ME!
356
         * @param y DOCUMENT ME!
357
         */
358
        public PointHandler(int i,double x, double y) {
359
            point = new Point2D.Double(x, y);
360
            index=i;
361
        }
362

    
363
        /**
364
         * DOCUMENT ME!
365
         *
366
         * @param x DOCUMENT ME!
367
         * @param y DOCUMENT ME!
368
         *
369
         * @return DOCUMENT ME!
370
         */
371
        public void move(double x, double y) {
372
            gp.getPointCoords()[index*2]+=x;
373
            gp.getPointCoords()[index*2+1]+=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
            gp.getPointCoords()[index*2]=x;
381
            gp.getPointCoords()[index*2+1]=y;
382
        }
383
    }
384
    /**
385
     * DOCUMENT ME!
386
     *
387
     * @author Vicente Caballero Navarro
388
     */
389
    class PointSelHandler extends AbstractHandler implements FinalHandler{
390
        /**
391
         * Crea un nuevo PointHandler.
392
         *
393
         * @param x DOCUMENT ME!
394
         * @param y DOCUMENT ME!
395
         */
396
        public PointSelHandler(int i,double x, double y) {
397
            point = new Point2D.Double(x, y);
398
            index=i;
399
        }
400

    
401
        /**
402
         * DOCUMENT ME!
403
         *
404
         * @param x DOCUMENT ME!
405
         * @param y DOCUMENT ME!
406
         *
407
         * @return DOCUMENT ME!
408
         */
409
        public void move(double x, double y) {
410
            gp.getPointCoords()[index*2]+=x;
411
            gp.getPointCoords()[index*2+1]+=y;
412
        }
413

    
414
        /**
415
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
416
         */
417
        public void set(double x, double y) {
418
            gp.getPointCoords()[index*2]=x;
419
            gp.getPointCoords()[index*2+1]=y;
420
        }
421
    }
422

    
423
    /*
424
     * (non-Javadoc)
425
     * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
426
     */
427
    public int getDimension() {
428
        return 2;
429
    }
430

    
431
    public Envelope getEnvelope() {
432
        Rectangle2D r=gp.getBounds2D();
433
        return new Envelope2D(r.getX(),r.getY(),r.getMaxX(),r.getMaxY());
434
    }
435

    
436
    public GeneralPathX getGeneralPath() {
437
        return gp;
438
    }
439

    
440
    public int getNumVertices() {
441
        return gp.getNumCoords();
442
    }
443

    
444
    public void addVertex(Point point) {
445
        gp.lineTo(point.getX(), point.getY());
446
    }    
447

    
448
    public void setVertex(int index, Point p) {
449
        throw new UnsupportedOperationException("Use setGeneralPathX");     
450
    }
451

    
452
    public Point getVertex(int index) {
453
        throw new UnsupportedOperationException("Method not implemented");
454
    }
455

    
456
    public void insertVertex(int index, Point p) {
457
        throw new UnsupportedOperationException("Method not implemented");      
458
    }
459

    
460
    public void removeVertex(int index) {
461
        throw new UnsupportedOperationException("Method not implemented");      
462
    }
463

    
464
    public void setCoordinateAt(int index, int dimension, double value) {
465
        throw new UnsupportedOperationException("Method not implemented");      
466
    }
467

    
468
    public void setGeneralPath(GeneralPathX generalPathX) {
469
        this.gp = generalPathX;
470
    }
471

    
472
    public void setPoints(Point startPoint, Point endPoint) {
473
        throw new UnsupportedOperationException("Method not implemented");      
474
    }
475
}