Statistics
| Revision:

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

History | View | Annotate | Download (14.1 KB)

1
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.handler.AbstractHandler;
16
import org.gvsig.fmap.geom.handler.FinalHandler;
17
import org.gvsig.fmap.geom.handler.Handler;
18
import org.gvsig.fmap.geom.impl.DrawGeometryShape;
19
import org.gvsig.fmap.geom.primitive.Envelope;
20
import org.gvsig.fmap.geom.primitive.GeneralPathX;
21
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
22
import org.gvsig.fmap.geom.primitive.Point;
23
import org.gvsig.fmap.geom.type.GeometryType;
24

    
25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
250
        gp.transform(at);
251
    }
252

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

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

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

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

    
280
    /* (non-Javadoc)
281
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
282
     */
283
    public double getCoordinateAt(int index, int dimension) {
284
        if (index > gp.getNumCoords()){
285
            throw new ArrayIndexOutOfBoundsException();
286
        }
287
        return gp.getPointAt(index).getCoordinateAt(dimension);         
288
    }
289

    
290
    /* (non-Javadoc)
291
     * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
292
     */
293
    public Handler[] getSelectHandlers() {
294
        Handler[] handlers = new Handler[gp.getNumCoords()];
295
        for (int i=0 ; i<gp.getNumCoords() ; i++){
296
            handlers[i] = new PointSelHandler(i, gp.getPointAt(i).getX(), gp.getPointAt(i).getY());
297
        } 
298
        return handlers;        
299
    }
300
    
301
    /**
302
     * DOCUMENT ME!
303
     *
304
     * @author Vicente Caballero Navarro
305
     */
306
    class PointHandler extends AbstractHandler implements FinalHandler{
307
        /**
308
         * Crea un nuevo PointHandler.
309
         *
310
         * @param x DOCUMENT ME!
311
         * @param y DOCUMENT ME!
312
         */
313
        public PointHandler(int i,double x, double y) {
314
            point = new Point2D.Double(x, y);
315
            index = i;
316
        }
317

    
318
        /**
319
         * DOCUMENT ME!
320
         *
321
         * @param x DOCUMENT ME!
322
         * @param y DOCUMENT ME!
323
         *
324
         * @return DOCUMENT ME!
325
         */
326
        public void move(double x, double y) {
327
            Point point = gp.getPointAt(index);
328
            point.setX(point.getX() + x);
329
            point.setY(point.getY() + y);
330
        }
331

    
332
        /**
333
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
334
         */
335
        public void set(double x, double y) {
336
            Point point = gp.getPointAt(index);
337
            point.setX(x);
338
            point.setY(y);
339
        }
340
    }
341
    /**
342
     * DOCUMENT ME!
343
     *
344
     * @author Vicente Caballero Navarro
345
     */
346
    class PointSelHandler extends AbstractHandler implements FinalHandler{
347
        /**
348
         * Crea un nuevo PointHandler.
349
         *
350
         * @param x DOCUMENT ME!
351
         * @param y DOCUMENT ME!
352
         */
353
        public PointSelHandler(int i,double x, double y) {
354
            point = new Point2D.Double(x, y);
355
            index = i;
356
        }
357

    
358
        /**
359
         * DOCUMENT ME!
360
         *
361
         * @param x DOCUMENT ME!
362
         * @param y DOCUMENT ME!
363
         *
364
         * @return DOCUMENT ME!
365
         */
366
        public void move(double x, double y) {
367
            Point point = gp.getPointAt(index);
368
            point.setX(point.getX() + x);
369
            point.setY(point.getY() + y);
370
        }
371

    
372
        /**
373
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
374
         */
375
        public void set(double x, double y) {
376
            Point point = gp.getPointAt(index);
377
            point.setX(x);
378
            point.setY(y);
379
        }
380
    }
381

    
382
    /*
383
     * (non-Javadoc)
384
     * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
385
     */
386
    public int getDimension() {
387
        return 2;
388
    }
389

    
390
    public Envelope getEnvelope() {
391
        Rectangle2D r=gp.getBounds2D();
392
        return new Envelope2D(r.getX(),r.getY(),r.getMaxX(),r.getMaxY());
393
    }
394

    
395
    public GeneralPathX getGeneralPath() {
396
        return gp;
397
    }
398

    
399
    public int getNumVertices() {
400
        return gp.getNumCoords();
401
    }
402

    
403
    public void addVertex(Point point) { 
404
            if( this.getNumVertices()== 0 ) {
405
                    gp.moveTo(point);
406
            } else {
407
                    gp.lineTo(point);
408
            }
409
    }  
410
    
411
    public void addMoveToVertex(Point point) {
412
        gp.moveTo(point);        
413
    }
414

    
415
    public void closePrimitive() {
416
        gp.closePath();        
417
    }
418

    
419
    public void setVertex(int index, Point p) {
420
        throw new UnsupportedOperationException("Method not implemented");     
421
    }
422

    
423
    public Point getVertex(int index) {
424
        return gp.getPointAt(index);
425
    }
426

    
427
    public void insertVertex(int index, Point p) {
428
        throw new UnsupportedOperationException("Method not implemented");      
429
    }
430

    
431
    public void removeVertex(int index) {
432
        throw new UnsupportedOperationException("Method not implemented");      
433
    }
434

    
435
    public void setCoordinateAt(int index, int dimension, double value) {
436
        gp.getPointAt(index).setCoordinateAt(dimension, value);      
437
    }
438

    
439
    public void setGeneralPath(GeneralPathX generalPathX) {
440
        this.gp = generalPathX;
441
    }
442

    
443
    public void setPoints(Point startPoint, Point endPoint) {
444
        throw new UnsupportedOperationException("Method not implemented");      
445
    }
446

    
447
    // /*
448
    public Shape getShape() {
449
        return new DrawGeometryShape(this);
450
    }
451

    
452
    public Shape getShape(AffineTransform affineTransform) {
453
        return new DrawGeometryShape(this, affineTransform);
454
    }
455
    // */
456
}