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

History | View | Annotate | Download (14.4 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
/**
54
 * @author Jorge Piera Llodr� (jorge.piera@iver.es)
55
 */
56
public abstract class OrientablePrimitive2D extends AbstractPrimitive implements OrientablePrimitive {
57
    private static final long serialVersionUID = -820881421374434713L;
58
    protected GeneralPathX gp;
59

    
60
    /**
61
     * The constructor with the GeometryType like and argument 
62
     * is used by the {@link GeometryType}{@link #create()}
63
     * to create the geometry
64
     * @param type
65
     * The geometry type
66
     */
67
    public OrientablePrimitive2D(GeometryType geometryType) {
68
        super(geometryType, null, null);
69
        gp = new GeneralPathX();
70
    }
71

    
72
    public OrientablePrimitive2D(GeometryType geometryType, String id, IProjection projection, GeneralPathX gp){
73
        super(geometryType, id, projection);
74
        this.gp = gp;                
75
    }
76

    
77
    /**
78
     * TODO método creado para dar visibilidad a gp despues de la refactorización
79
     * @return
80
     */
81
    public GeneralPathX getGeneralPathX() {
82
        return gp;
83
    }
84

    
85
    /* (non-Javadoc)
86
     * @see java.awt.Shape#contains(double, double)
87
     */
88
    public boolean contains(double x, double y) {
89
        return gp.contains(x, y);
90
    }
91

    
92
    /* (non-Javadoc)
93
     * @see java.awt.Shape#contains(double, double, double, double)
94
     */
95
    public boolean contains(double x, double y, double w, double h) {
96
        return gp.contains(x, y, w, h);
97
    }
98

    
99
    /* (non-Javadoc)
100
     * @see java.awt.Shape#intersects(double, double, double, double)
101
     */
102
    public boolean intersects(double x, double y, double w, double h) {
103
        // M�s r�pido
104
        return gp.intersects(x, y, w, h);
105
    }
106

    
107
    /* (non-Javadoc)
108
     * @see java.awt.Shape#getBounds()
109
     */
110
    public Rectangle getBounds() {
111
        return gp.getBounds();
112
    }
113

    
114
    /* (non-Javadoc)
115
     * @see java.awt.Shape#contains(java.awt.geom.Point2D)
116
     */
117
    public boolean contains(Point2D p) {
118
        return gp.contains(p);
119
    }
120

    
121
    /* (non-Javadoc)
122
     * @see java.awt.Shape#getBounds2D()
123
     */
124
    public Rectangle2D getBounds2D() {
125
        return gp.getBounds2D();
126
    }
127

    
128
    /* (non-Javadoc)
129
     * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
130
     */
131
    public boolean contains(Rectangle2D r) {
132
        return gp.contains(r);
133
    }
134

    
135
    /**
136
     * El m�todo intersects de java.awt.Shape que define la intersecci�n entre
137
     * una polil�nea y un Rectangle2D considera la polil�nea como un Shape
138
     * gen�rico y se producen errores en la selecci�n de polil�neas. Por este
139
     * motivo se ha modificado este m�todo intersect() de FPolyline2D para que
140
     * realize la intersecci�n estricta entre el Rectangle2D y la polil�nea en
141
     * cuesti�n. El precio es un incremento de tiempo m�ximo del 50%.
142
     *
143
     * @param r Rect�ngulo.
144
     *
145
     * @return True si intersecta con el rectangulo que se pasa como par�metro.
146
     */
147
    public boolean intersects(Rectangle2D r) {
148
        //return gp.intersects(r);
149
        // M�s exacto
150
        boolean bool = false;
151
        if (gp.intersects(r)) {
152
            ArrayList arrayCoords;
153
            int theType;
154
            //Use this array to store segment coordinate data
155
            double[] theData = new double[6];
156
            PathIterator theIterator;
157

    
158
            Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
159
            Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
160
            Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
161
            Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
162
            Line2D l1 = new Line2D.Double(p1,p2);
163
            Line2D l2 = new Line2D.Double(p2,p3);
164
            Line2D l3 = new Line2D.Double(p3,p4);
165
            Line2D l4 = new Line2D.Double(p4,p1);
166

    
167
            theIterator = this.getPathIterator(null, geomManager.getFlatness());
168
            arrayCoords = new ArrayList();
169
            while(!theIterator.isDone()) {
170
                theType = theIterator.currentSegment(theData);
171
                if (theType==PathIterator.SEG_MOVETO) {
172
                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
173
                } else if (theType==PathIterator.SEG_LINETO) {
174
                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
175
                    Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-2);
176
                    Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),theData[0],theData[1]);
177
                    if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
178
                        || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
179
                        || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
180
                        || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
181
                        || r.intersectsLine(l)) {
182
                        bool = true;
183
                    }
184
                } else if(theType==PathIterator.SEG_CLOSE){
185
                    Point2D firstPoint=(Point2D)arrayCoords.get(0);
186
                    Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-1);
187
                    Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),firstPoint.getX(),firstPoint.getY());
188
                    if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
189
                        || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
190
                        || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
191
                        || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
192
                        || r.intersectsLine(l)) {
193
                        bool = true;
194
                    }
195
                }else {
196
                    System.out.println("Not supported here");
197
                }
198
                theIterator.next();
199
            }
200
        }
201
        return bool;
202
    }
203

    
204
    /* (non-Javadoc)
205
     * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
206
     */
207
    public PathIterator getPathIterator(AffineTransform at) {
208
        return gp.getPathIterator(at);
209
    }
210

    
211
    /* (non-Javadoc)
212
     * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
213
     */
214
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
215
        return gp.getPathIterator(at, flatness);
216
    }
217

    
218
    /**
219
     * DOCUMENT ME!
220
     *
221
     * @param at DOCUMENT ME!
222
     */
223
    public void transform(AffineTransform at) {
224

    
225
        // TODO: PRUEBA. BORRAR ESTA LINEA
226
        // gp = FConverter.transformToInts(gp, at);
227

    
228
        gp.transform(at);
229
    }
230

    
231
    /* (non-Javadoc)
232
     * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
233
     */
234
    public void reProject(ICoordTrans ct) {
235
        gp.reProject(ct);
236
    }
237

    
238
    /* (non-Javadoc)
239
     * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
240
     */
241
    public Handler[] getStretchingHandlers() {
242
        ArrayList handlers = new ArrayList();
243
        PathIterator gpi = getPathIterator(null);
244

    
245
        double[] theData = new double[6];
246
        int i=0;
247
        while (!gpi.isDone()) {
248
            gpi.currentSegment(theData);
249
            //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
250
            handlers.add(new PointHandler(i,theData[0], theData[1]));
251
            i++;
252
            gpi.next();
253
        }
254

    
255
        return (Handler[]) handlers.toArray(new Handler[0]);
256
    }
257

    
258
    /* (non-Javadoc)
259
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
260
     */
261
    public double getCoordinateAt(int index, int dimension) {
262
        if (index > gp.getNumCoords()){
263
            throw new ArrayIndexOutOfBoundsException();
264
        }
265
        return gp.getPointAt(index).getCoordinateAt(dimension);         
266
    }
267

    
268
    /* (non-Javadoc)
269
     * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
270
     */
271
    public Handler[] getSelectHandlers() {
272
        Handler[] handlers = new Handler[gp.getNumCoords()];
273
        for (int i=0 ; i<gp.getNumCoords() ; i++){
274
            handlers[i] = new PointSelHandler(i, gp.getPointAt(i).getX(), gp.getPointAt(i).getY());
275
        } 
276
        return handlers;        
277
    }
278
    
279
    /**
280
     * DOCUMENT ME!
281
     *
282
     * @author Vicente Caballero Navarro
283
     */
284
    class PointHandler extends AbstractHandler implements FinalHandler{
285
        /**
286
         * Crea un nuevo PointHandler.
287
         *
288
         * @param x DOCUMENT ME!
289
         * @param y DOCUMENT ME!
290
         */
291
        public PointHandler(int i,double x, double y) {
292
            point = new Point2D.Double(x, y);
293
            index = i;
294
        }
295

    
296
        /**
297
         * DOCUMENT ME!
298
         *
299
         * @param x DOCUMENT ME!
300
         * @param y DOCUMENT ME!
301
         *
302
         * @return DOCUMENT ME!
303
         */
304
        public void move(double x, double y) {
305
            Point point = gp.getPointAt(index);
306
            point.setX(point.getX() + x);
307
            point.setY(point.getY() + y);
308
        }
309

    
310
        /**
311
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
312
         */
313
        public void set(double x, double y) {
314
            Point point = gp.getPointAt(index);
315
            point.setX(x);
316
            point.setY(y);
317
        }
318
    }
319
    /**
320
     * DOCUMENT ME!
321
     *
322
     * @author Vicente Caballero Navarro
323
     */
324
    class PointSelHandler extends AbstractHandler implements FinalHandler{
325
        /**
326
         * Crea un nuevo PointHandler.
327
         *
328
         * @param x DOCUMENT ME!
329
         * @param y DOCUMENT ME!
330
         */
331
        public PointSelHandler(int i,double x, double y) {
332
            point = new Point2D.Double(x, y);
333
            index = i;
334
        }
335

    
336
        /**
337
         * DOCUMENT ME!
338
         *
339
         * @param x DOCUMENT ME!
340
         * @param y DOCUMENT ME!
341
         *
342
         * @return DOCUMENT ME!
343
         */
344
        public void move(double x, double y) {
345
            Point point = gp.getPointAt(index);
346
            point.setX(point.getX() + x);
347
            point.setY(point.getY() + y);
348
        }
349

    
350
        /**
351
         * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
352
         */
353
        public void set(double x, double y) {
354
            Point point = gp.getPointAt(index);
355
            point.setX(x);
356
            point.setY(y);
357
        }
358
    }
359

    
360
    /*
361
     * (non-Javadoc)
362
     * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
363
     */
364
    public int getDimension() {
365
        return 2;
366
    }
367

    
368
    public Envelope getEnvelope() {
369
        Rectangle2D r=gp.getBounds2D();
370
        return new Envelope2D(r.getX(),r.getY(),r.getMaxX(),r.getMaxY());
371
    }
372

    
373
    public GeneralPathX getGeneralPath() {
374
        return gp;
375
    }
376

    
377
    public int getNumVertices() {
378
        return gp.getNumCoords();
379
    }
380

    
381
    public void addVertex(Point point) { 
382
            if( this.getNumVertices()== 0 ) {
383
                    gp.moveTo(point);
384
            } else {
385
                    gp.lineTo(point);
386
            }
387
    }  
388

    
389
    public void addVertex(double x, double y)  { 
390
                try {
391
                    Point point;
392
                        point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D);
393
                    addVertex(point);
394
                } catch (CreateGeometryException e) {
395
                        throw new RuntimeException(e.getMessage());
396
                }
397
    }
398
    
399
    public void addVertex(double x, double y, double z) { 
400
                try {
401
                    Point point;
402
                        point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM3D);
403
                        point.setCoordinateAt(DIMENSIONS.Z, z);
404
                    addVertex(point);
405
                } catch (CreateGeometryException e) {
406
                        throw new RuntimeException(e.getMessage());
407
                }
408
    }
409
    
410
    public void addMoveToVertex(Point point) {
411
        gp.moveTo(point);        
412
    }
413

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

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

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

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

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

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

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

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

    
446
    public Shape getShape() {
447
        return new DrawGeometryShape(this);
448
    }
449

    
450
    public Shape getShape(AffineTransform affineTransform) {
451
        return new DrawGeometryShape(this, affineTransform);
452
    }
453
}