Statistics
| Revision:

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

History | View | Annotate | Download (11.8 KB)

1
package org.gvsig.fmap.geom.primitive;
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
import org.gvsig.fmap.geom.handler.AbstractHandler;
14
import org.gvsig.fmap.geom.handler.FinalHandler;
15
import org.gvsig.fmap.geom.handler.Handler;
16
import org.gvsig.fmap.geom.util.Converter;
17

    
18

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

    
73
        private static final long serialVersionUID = 1L;
74
        protected GeneralPathX gp;
75

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

    
84
        /**
85
         * Crea un nuevo FPolyline2D.
86
         *
87
         * @param gpx GeneralPathX.
88
         */
89
        public OrientablePrimitive2D(String id, IProjection projection, GeneralPathX gpx) {
90
                super(id, projection);
91
                gp = gpx;
92
        }
93

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

    
101
        /* (non-Javadoc)
102
         * @see java.awt.Shape#contains(double, double, double, double)
103
         */
104
        public boolean contains(double x, double y, double w, double h) {
105
                return gp.contains(x, y, w, h);
106
        }
107

    
108
        /* (non-Javadoc)
109
         * @see java.awt.Shape#intersects(double, double, double, double)
110
         */
111
        public boolean intersects(double x, double y, double w, double h) {
112
            // M?s r?pido
113
                return gp.intersects(x, y, w, h);
114
        }
115

    
116
        /* (non-Javadoc)
117
         * @see java.awt.Shape#getBounds()
118
         */
119
        public Rectangle getBounds() {
120
                return gp.getBounds();
121
        }
122

    
123
        /* (non-Javadoc)
124
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
125
         */
126
        public boolean contains(Point2D p) {
127
                return gp.contains(p);
128
        }
129

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

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

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

    
167
                       Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
168
                       Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
169
                       Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
170
                       Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
171
                       Line2D l1 = new Line2D.Double(p1,p2);
172
                       Line2D l2 = new Line2D.Double(p2,p3);
173
                       Line2D l3 = new Line2D.Double(p3,p4);
174
                       Line2D l4 = new Line2D.Double(p4,p1);
175

    
176
                           theIterator = this.getPathIterator(null,Converter.FLATNESS);
177
                           arrayCoords = new ArrayList();
178
                           while(!theIterator.isDone()) {
179
                                    theType = theIterator.currentSegment(theData);
180
                           if (theType==PathIterator.SEG_MOVETO) {
181
                                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
182
                           } else if (theType==PathIterator.SEG_LINETO) {
183
                                   arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
184
                                   Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-2);
185
                                   Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),theData[0],theData[1]);
186
                                   if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
187
                                                   || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
188
                                                   || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
189
                                                   || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
190
                                                   || r.intersectsLine(l)) {
191
                                           bool = true;
192
                                   }
193
                           } else if(theType==PathIterator.SEG_CLOSE){
194
                                   Point2D firstPoint=(Point2D)arrayCoords.get(0);
195
                                   Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-1);
196
                           Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),firstPoint.getX(),firstPoint.getY());
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 {
205
                                    System.out.println("Not supported here");
206
                           }
207
                                theIterator.next();
208
                            }
209
                   }
210
                   return bool;
211
        }
212

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

    
220
        /* (non-Javadoc)
221
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
222
         */
223
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
224
                return gp.getPathIterator(at, flatness);
225
        }
226

    
227
        /**
228
         * DOCUMENT ME!
229
         *
230
         * @param at DOCUMENT ME!
231
         */
232
        public void transform(AffineTransform at) {
233

    
234
        // TODO: PRUEBA. BORRAR ESTA LINEA
235
        // gp = FConverter.transformToInts(gp, at);
236

    
237
                gp.transform(at);
238
        }
239

    
240
        /* (non-Javadoc)
241
         * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
242
         */
243
        public void reProject(ICoordTrans ct) {
244
                gp.reProject(ct);
245
        }
246

    
247
        /* (non-Javadoc)
248
         * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
249
         */
250
        public Handler[] getStretchingHandlers() {
251
                ArrayList handlers = new ArrayList();
252
                GeneralPathXIterator gpi = null;
253
                gpi = (GeneralPathXIterator) getPathIterator(null);
254

    
255
                double[] theData = new double[6];
256
                int i=0;
257
                while (!gpi.isDone()) {
258
                        gpi.currentSegment(theData);
259
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
260
                        handlers.add(new PointHandler(i,theData[0], theData[1]));
261
                        i++;
262
                        gpi.next();
263
                }
264

    
265
                return (Handler[]) handlers.toArray(new Handler[0]);
266
        }
267

    
268
        /* (non-Javadoc)
269
         * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
270
         */
271
        public Handler[] getSelectHandlers() {
272
                ArrayList handlers = new ArrayList();
273
                GeneralPathXIterator gpi = null;
274
                gpi = (GeneralPathXIterator) getPathIterator(null);
275

    
276
                double[] theData = new double[6];
277
                int i=0;
278
                //boolean isFirst=true;
279
                while (!gpi.isDone()) {
280
                        int theType = gpi.currentSegment(theData);
281
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
282
                        /* if (!(this instanceof FPolygon2D && isFirst)){
283
                                handlers.add(new PointSelHandler(i,theData[0], theData[1]));
284
                                i++;
285
                        }
286
                        isFirst=false; */
287
                        switch (theType)
288
                        {
289
                        case GeneralPathXIterator.SEG_MOVETO:
290
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
291
                                break;
292
                        case GeneralPathXIterator.SEG_LINETO:
293
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
294
                                break;
295
                        case GeneralPathXIterator.SEG_CLOSE:
296
                                break;
297
                        case GeneralPathXIterator.SEG_QUADTO:
298
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
299
                                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
300
                                break;
301
                        case GeneralPathXIterator.SEG_CUBICTO:
302
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
303
                                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
304
                                handlers.add(new PointSelHandler(i++,theData[4], theData[5]));
305
                                break;
306

    
307
                        }
308
                        gpi.next();
309

    
310

    
311
                }
312

    
313
                return (Handler[]) handlers.toArray(new Handler[0]);
314
        }
315
        /**
316
         * DOCUMENT ME!
317
         *
318
         * @author Vicente Caballero Navarro
319
         */
320
        class PointHandler extends AbstractHandler implements FinalHandler{
321
                /**
322
                 * Crea un nuevo PointHandler.
323
                 *
324
                 * @param x DOCUMENT ME!
325
                 * @param y DOCUMENT ME!
326
                 */
327
                public PointHandler(int i,double x, double y) {
328
                        point = new Point2D.Double(x, y);
329
                        index=i;
330
                }
331

    
332
                /**
333
                 * DOCUMENT ME!
334
                 *
335
                 * @param x DOCUMENT ME!
336
                 * @param y DOCUMENT ME!
337
                 *
338
                 * @return DOCUMENT ME!
339
                 */
340
                public void move(double x, double y) {
341
                        gp.getPointCoords()[index*2]+=x;
342
                        gp.getPointCoords()[index*2+1]+=y;
343
                }
344

    
345
                /**
346
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
347
                 */
348
                public void set(double x, double y) {
349
                        gp.getPointCoords()[index*2]=x;
350
                        gp.getPointCoords()[index*2+1]=y;
351
                }
352
        }
353
        /**
354
         * DOCUMENT ME!
355
         *
356
         * @author Vicente Caballero Navarro
357
         */
358
        class PointSelHandler extends AbstractHandler implements FinalHandler{
359
                /**
360
                 * Crea un nuevo PointHandler.
361
                 *
362
                 * @param x DOCUMENT ME!
363
                 * @param y DOCUMENT ME!
364
                 */
365
                public PointSelHandler(int i,double x, double y) {
366
                        point = new Point2D.Double(x, y);
367
                        index=i;
368
                }
369

    
370
                /**
371
                 * DOCUMENT ME!
372
                 *
373
                 * @param x DOCUMENT ME!
374
                 * @param y DOCUMENT ME!
375
                 *
376
                 * @return DOCUMENT ME!
377
                 */
378
                public void move(double x, double y) {
379
                        gp.getPointCoords()[index*2]+=x;
380
                        gp.getPointCoords()[index*2+1]+=y;
381
                }
382

    
383
                /**
384
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
385
                 */
386
                public void set(double x, double y) {
387
                        gp.getPointCoords()[index*2]=x;
388
                        gp.getPointCoords()[index*2+1]=y;
389
                }
390
        }
391

    
392
        /*
393
         * (non-Javadoc)
394
         * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
395
         */
396
        public int getCoordinateDimension() {
397
                return 2;
398
        }
399

    
400
        public Envelope getEnvelope() {
401
                Rectangle2D r=gp.getBounds2D();
402
                return new DefaultEnvelope(2,new double[]{r.getX(),r.getY()},new double[]{r.getMaxX(),r.getMaxY()});
403
        }
404
}