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 @ 40559

History | View | Annotate | Download (15.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.geom.primitive.impl;
25

    
26
import java.awt.Rectangle;
27
import java.awt.Shape;
28
import java.awt.geom.AffineTransform;
29
import java.awt.geom.Line2D;
30
import java.awt.geom.PathIterator;
31
import java.awt.geom.Point2D;
32
import java.awt.geom.Rectangle2D;
33
import java.util.ArrayList;
34

    
35
import org.cresques.cts.ICoordTrans;
36
import org.cresques.cts.IProjection;
37

    
38
import org.gvsig.fmap.geom.GeometryException;
39
import org.gvsig.fmap.geom.GeometryLocator;
40
import org.gvsig.fmap.geom.exception.CreateGeometryException;
41
import org.gvsig.fmap.geom.handler.AbstractHandler;
42
import org.gvsig.fmap.geom.handler.FinalHandler;
43
import org.gvsig.fmap.geom.handler.Handler;
44
import org.gvsig.fmap.geom.impl.DrawGeometryShape;
45
import org.gvsig.fmap.geom.primitive.Envelope;
46
import org.gvsig.fmap.geom.primitive.GeneralPathX;
47
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.fmap.geom.type.GeometryType;
50
import org.gvsig.tools.locator.LocatorException;
51

    
52

    
53
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
54
 *
55
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
56
 *
57
 * This program is free software; you can redistribute it and/or
58
 * modify it under the terms of the GNU General Public License
59
 * as published by the Free Software Foundation; either version 2
60
 * of the License, or (at your option) any later version.
61
 *
62
 * This program is distributed in the hope that it will be useful,
63
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
64
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
65
 * GNU General Public License for more details.
66
 *
67
 * You should have received a copy of the GNU General Public License
68
 * along with this program; if not, write to the Free Software
69
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
70
 *
71
 * For more information, contact:
72
 *
73
 *  Generalitat Valenciana
74
 *   Conselleria d'Infraestructures i Transport
75
 *   Av. Blasco Ib��ez, 50
76
 *   46010 VALENCIA
77
 *   SPAIN
78
 *
79
 *      +34 963862235
80
 *   gvsig@gva.es
81
 *      www.gvsig.gva.es
82
 *
83
 *    or
84
 *
85
 *   IVER T.I. S.A
86
 *   Salamanca 50
87
 *   46005 Valencia
88
 *   Spain
89
 *
90
 *   +34 963163400
91
 *   dac@iver.es
92
 */
93
/* CVS MESSAGES:
94
 *
95
 * $Id: FOrientablePrimitive2D.java,v 1.1 2008/03/12 08:46:20 cvs Exp $
96
 * $Log: FOrientablePrimitive2D.java,v $
97
 * Revision 1.1  2008/03/12 08:46:20  cvs
98
 * *** empty log message ***
99
 *
100
 *
101
 */
102
/**
103
 * @author Jorge Piera Llodr� (jorge.piera@iver.es)
104
 */
105
public abstract class OrientablePrimitive2D extends AbstractPrimitive implements OrientablePrimitive {
106
    private static final long serialVersionUID = -820881421374434713L;
107
    protected GeneralPathX gp;
108

    
109
    /**
110
     * The constructor with the GeometryType like and argument 
111
     * is used by the {@link GeometryType}{@link #create()}
112
     * to create the geometry
113
     * @param type
114
     * The geometry type
115
     */
116
    public OrientablePrimitive2D(GeometryType geometryType) {
117
        super(geometryType, null, null);
118
        gp = new GeneralPathX();
119
    }
120

    
121
    public OrientablePrimitive2D(GeometryType geometryType, String id, IProjection projection, GeneralPathX gp){
122
        super(geometryType, id, projection);
123
        this.gp = gp;                
124
    }
125

    
126
    /**
127
     * TODO método creado para dar visibilidad a gp despues de la refactorización
128
     * @return
129
     */
130
    public GeneralPathX getGeneralPathX() {
131
        return gp;
132
    }
133

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

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

    
148
    /* (non-Javadoc)
149
     * @see java.awt.Shape#intersects(double, double, double, double)
150
     */
151
    public boolean intersects(double x, double y, double w, double h) {
152
        // M�s r�pido
153
        return gp.intersects(x, y, w, h);
154
    }
155

    
156
    /* (non-Javadoc)
157
     * @see java.awt.Shape#getBounds()
158
     */
159
    public Rectangle getBounds() {
160
        return gp.getBounds();
161
    }
162

    
163
    /* (non-Javadoc)
164
     * @see java.awt.Shape#contains(java.awt.geom.Point2D)
165
     */
166
    public boolean contains(Point2D p) {
167
        return gp.contains(p);
168
    }
169

    
170
    /* (non-Javadoc)
171
     * @see java.awt.Shape#getBounds2D()
172
     */
173
    public Rectangle2D getBounds2D() {
174
        return gp.getBounds2D();
175
    }
176

    
177
    /* (non-Javadoc)
178
     * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
179
     */
180
    public boolean contains(Rectangle2D r) {
181
        return gp.contains(r);
182
    }
183

    
184
    /**
185
     * El m�todo intersects de java.awt.Shape que define la intersecci�n entre
186
     * una polil�nea y un Rectangle2D considera la polil�nea como un Shape
187
     * gen�rico y se producen errores en la selecci�n de polil�neas. Por este
188
     * motivo se ha modificado este m�todo intersect() de FPolyline2D para que
189
     * realize la intersecci�n estricta entre el Rectangle2D y la polil�nea en
190
     * cuesti�n. El precio es un incremento de tiempo m�ximo del 50%.
191
     *
192
     * @param r Rect�ngulo.
193
     *
194
     * @return True si intersecta con el rectangulo que se pasa como par�metro.
195
     */
196
    public boolean intersects(Rectangle2D r) {
197
        //return gp.intersects(r);
198
        // M�s exacto
199
        boolean bool = false;
200
        if (gp.intersects(r)) {
201
            ArrayList arrayCoords;
202
            int theType;
203
            //Use this array to store segment coordinate data
204
            double[] theData = new double[6];
205
            PathIterator theIterator;
206

    
207
            Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
208
            Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
209
            Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
210
            Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
211
            Line2D l1 = new Line2D.Double(p1,p2);
212
            Line2D l2 = new Line2D.Double(p2,p3);
213
            Line2D l3 = new Line2D.Double(p3,p4);
214
            Line2D l4 = new Line2D.Double(p4,p1);
215

    
216
            theIterator = this.getPathIterator(null, geomManager.getFlatness());
217
            arrayCoords = new ArrayList();
218
            while(!theIterator.isDone()) {
219
                theType = theIterator.currentSegment(theData);
220
                if (theType==PathIterator.SEG_MOVETO) {
221
                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
222
                } else if (theType==PathIterator.SEG_LINETO) {
223
                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
224
                    Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-2);
225
                    Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),theData[0],theData[1]);
226
                    if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
227
                        || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
228
                        || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
229
                        || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
230
                        || r.intersectsLine(l)) {
231
                        bool = true;
232
                    }
233
                } else if(theType==PathIterator.SEG_CLOSE){
234
                    Point2D firstPoint=(Point2D)arrayCoords.get(0);
235
                    Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-1);
236
                    Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),firstPoint.getX(),firstPoint.getY());
237
                    if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
238
                        || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
239
                        || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
240
                        || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
241
                        || r.intersectsLine(l)) {
242
                        bool = true;
243
                    }
244
                }else {
245
                    System.out.println("Not supported here");
246
                }
247
                theIterator.next();
248
            }
249
        }
250
        return bool;
251
    }
252

    
253
    /* (non-Javadoc)
254
     * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
255
     */
256
    public PathIterator getPathIterator(AffineTransform at) {
257
        return gp.getPathIterator(at);
258
    }
259

    
260
    /* (non-Javadoc)
261
     * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
262
     */
263
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
264
        return gp.getPathIterator(at, flatness);
265
    }
266

    
267
    /**
268
     * DOCUMENT ME!
269
     *
270
     * @param at DOCUMENT ME!
271
     */
272
    public void transform(AffineTransform at) {
273

    
274
        // TODO: PRUEBA. BORRAR ESTA LINEA
275
        // gp = FConverter.transformToInts(gp, at);
276

    
277
        gp.transform(at);
278
    }
279

    
280
    /* (non-Javadoc)
281
     * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
282
     */
283
    public void reProject(ICoordTrans ct) {
284
        gp.reProject(ct);
285
    }
286

    
287
    /* (non-Javadoc)
288
     * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
289
     */
290
    public Handler[] getStretchingHandlers() {
291
        ArrayList handlers = new ArrayList();
292
        PathIterator gpi = getPathIterator(null);
293

    
294
        double[] theData = new double[6];
295
        int i=0;
296
        while (!gpi.isDone()) {
297
            gpi.currentSegment(theData);
298
            //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
299
            handlers.add(new PointHandler(i,theData[0], theData[1]));
300
            i++;
301
            gpi.next();
302
        }
303

    
304
        return (Handler[]) handlers.toArray(new Handler[0]);
305
    }
306

    
307
    /* (non-Javadoc)
308
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
309
     */
310
    public double getCoordinateAt(int index, int dimension) {
311
        if (index > gp.getNumCoords()){
312
            throw new ArrayIndexOutOfBoundsException();
313
        }
314
        return gp.getPointAt(index).getCoordinateAt(dimension);         
315
    }
316

    
317
    /* (non-Javadoc)
318
     * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
319
     */
320
    public Handler[] getSelectHandlers() {
321
        Handler[] handlers = new Handler[gp.getNumCoords()];
322
        for (int i=0 ; i<gp.getNumCoords() ; i++){
323
            handlers[i] = new PointSelHandler(i, gp.getPointAt(i).getX(), gp.getPointAt(i).getY());
324
        } 
325
        return handlers;        
326
    }
327
    
328
    /**
329
     * DOCUMENT ME!
330
     *
331
     * @author Vicente Caballero Navarro
332
     */
333
    class PointHandler extends AbstractHandler implements FinalHandler{
334
        /**
335
         * Crea un nuevo PointHandler.
336
         *
337
         * @param x DOCUMENT ME!
338
         * @param y DOCUMENT ME!
339
         */
340
        public PointHandler(int i,double x, double y) {
341
            point = new Point2D.Double(x, y);
342
            index = i;
343
        }
344

    
345
        /**
346
         * DOCUMENT ME!
347
         *
348
         * @param x DOCUMENT ME!
349
         * @param y DOCUMENT ME!
350
         *
351
         * @return DOCUMENT ME!
352
         */
353
        public void move(double x, double y) {
354
            Point point = gp.getPointAt(index);
355
            point.setX(point.getX() + x);
356
            point.setY(point.getY() + y);
357
        }
358

    
359
        /**
360
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
361
         */
362
        public void set(double x, double y) {
363
            Point point = gp.getPointAt(index);
364
            point.setX(x);
365
            point.setY(y);
366
        }
367
    }
368
    /**
369
     * DOCUMENT ME!
370
     *
371
     * @author Vicente Caballero Navarro
372
     */
373
    class PointSelHandler extends AbstractHandler implements FinalHandler{
374
        /**
375
         * Crea un nuevo PointHandler.
376
         *
377
         * @param x DOCUMENT ME!
378
         * @param y DOCUMENT ME!
379
         */
380
        public PointSelHandler(int i,double x, double y) {
381
            point = new Point2D.Double(x, y);
382
            index = i;
383
        }
384

    
385
        /**
386
         * DOCUMENT ME!
387
         *
388
         * @param x DOCUMENT ME!
389
         * @param y DOCUMENT ME!
390
         *
391
         * @return DOCUMENT ME!
392
         */
393
        public void move(double x, double y) {
394
            Point point = gp.getPointAt(index);
395
            point.setX(point.getX() + x);
396
            point.setY(point.getY() + y);
397
        }
398

    
399
        /**
400
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
401
         */
402
        public void set(double x, double y) {
403
            Point point = gp.getPointAt(index);
404
            point.setX(x);
405
            point.setY(y);
406
        }
407
    }
408

    
409
    /*
410
     * (non-Javadoc)
411
     * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
412
     */
413
    public int getDimension() {
414
        return 2;
415
    }
416

    
417
    public Envelope getEnvelope() {
418
        Rectangle2D r=gp.getBounds2D();
419
        return new Envelope2D(r.getX(),r.getY(),r.getMaxX(),r.getMaxY());
420
    }
421

    
422
    public GeneralPathX getGeneralPath() {
423
        return gp;
424
    }
425

    
426
    public int getNumVertices() {
427
        return gp.getNumCoords();
428
    }
429

    
430
    public void addVertex(Point point) { 
431
            if( this.getNumVertices()== 0 ) {
432
                    gp.moveTo(point);
433
            } else {
434
                    gp.lineTo(point);
435
            }
436
    }  
437

    
438
    public void addVertex(double x, double y)  { 
439
                try {
440
                    Point point;
441
                        point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D);
442
                    addVertex(point);
443
                } catch (CreateGeometryException e) {
444
                        throw new RuntimeException(e.getMessage());
445
                }
446
    }
447
    
448
    public void addVertex(double x, double y, double z) { 
449
                try {
450
                    Point point;
451
                        point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM3D);
452
                        point.setCoordinateAt(DIMENSIONS.Z, z);
453
                    addVertex(point);
454
                } catch (CreateGeometryException e) {
455
                        throw new RuntimeException(e.getMessage());
456
                }
457
    }
458
    
459
    public void addMoveToVertex(Point point) {
460
        gp.moveTo(point);        
461
    }
462

    
463
    public void closePrimitive() {
464
        gp.closePath();        
465
    }
466

    
467
    public void setVertex(int index, Point p) {
468
        throw new UnsupportedOperationException("Method not implemented");     
469
    }
470

    
471
    public Point getVertex(int index) {
472
        return gp.getPointAt(index);
473
    }
474

    
475
    public void insertVertex(int index, Point p) {
476
        throw new UnsupportedOperationException("Method not implemented");      
477
    }
478

    
479
    public void removeVertex(int index) {
480
        throw new UnsupportedOperationException("Method not implemented");      
481
    }
482

    
483
    public void setCoordinateAt(int index, int dimension, double value) {
484
        gp.getPointAt(index).setCoordinateAt(dimension, value);      
485
    }
486

    
487
    public void setGeneralPath(GeneralPathX generalPathX) {
488
        this.gp = generalPathX;
489
    }
490

    
491
    public void setPoints(Point startPoint, Point endPoint) {
492
        throw new UnsupportedOperationException("Method not implemented");      
493
    }
494

    
495
    public Shape getShape() {
496
        return new DrawGeometryShape(this);
497
    }
498

    
499
    public Shape getShape(AffineTransform affineTransform) {
500
        return new DrawGeometryShape(this, affineTransform);
501
    }
502
}