Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / core / FPolyline2D.java @ 21606

History | View | Annotate | Download (11.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
/************************************************
42
 *                                                                                                *
43
 *   Modfied By:                                                                *
44
 *   Prodevelop Integraci?n de Tecnolog?as SL        *
45
 *   Conde Salvatierra de ?lava , 34-10                        *
46
 *   46004 Valencia                                                                *
47
 *   Spain                                                                                *
48
 *                                                                                                *
49
 *   +34 963 510 612                                                        *
50
 *   +34 963 510 968                                                        *
51
 *   gis@prodevelop.es                                                        *
52
 *   http://www.prodevelop.es                                        *
53
 *                                                                                                *
54
 *   gvSIG Mobile Team 2006                                         *
55
 *                                                                                          *         
56
 ************************************************/
57

    
58
package es.prodevelop.gvsig.mobile.fmap.core;
59

    
60
import java.awt.Rectangle;
61
import java.awt.geom.AffineTransform;
62
import java.awt.geom.Line2D;
63
import java.awt.geom.PathIterator;
64
import java.awt.geom.Point2D;
65
import java.awt.geom.Rectangle2D;
66
import java.util.ArrayList;
67

    
68
import org.apache.log4j.Logger;
69

    
70
import es.prodevelop.gvsig.mobile.fmap.proj.ICoordTrans;
71

    
72

    
73
/**
74
 * 
75
 *
76
 * @author Fernando Gonz?lez Cort?s
77
 */
78
public class FPolyline2D implements FShape {
79
        protected GeneralPathX gp;
80
        private static Logger logger = Logger.getLogger(FPolyline2D.class);
81
        /**
82
         * Crea un nuevo FPolyline2D.
83
         *
84
         * @param gpx GeneralPathX.
85
         */
86
        public FPolyline2D(GeneralPathX gpx) {
87
                gp = gpx;
88
        }
89

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

    
97
        /* (non-Javadoc)
98
         * @see java.awt.Shape#contains(double, double, double, double)
99
         */
100
        public boolean contains(double x, double y, double w, double h) {
101
                return gp.contains(x, y, w, h);
102
        }
103

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

    
112
        /* (non-Javadoc)
113
         * @see java.awt.Shape#getBounds()
114
         */
115
        public Rectangle getBounds() {
116
                return gp.getBounds();
117
        }
118

    
119
        /* (non-Javadoc)
120
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
121
         */
122
        public boolean contains(Point2D p) {
123
                return gp.contains(p);
124
        }
125

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

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

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

    
163
                       Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
164
                       Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
165
                       Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
166
                       Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
167
                       Line2D l1 = new Line2D.Double(p1,p2);
168
                       Line2D l2 = new Line2D.Double(p2,p3);
169
                       Line2D l3 = new Line2D.Double(p3,p4);
170
                       Line2D l4 = new Line2D.Double(p4,p1);
171

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

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

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

    
224
        /**
225
         * 
226
         *
227
         * @param at 
228
         */
229
        public void transform(AffineTransform at) {
230

    
231
        // TODO: PRUEBA. BORRAR ESTA LINEA
232
        // gp = FConverter.transformToInts(gp, at);
233

    
234
                gp.transform(at);
235
        }
236

    
237
        /**
238
         * @see com.iver.cit.gvsig.fmap.core.FShape#getShapeType()
239
         */
240
        public int getShapeType() {
241
                return FShape.LINE;
242
        }
243

    
244
        /* (non-Javadoc)
245
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
246
         */
247
        public FShape cloneFShape() {
248
                return new FPolyline2D((GeneralPathX) gp.clone());
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
                        int theType = 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 com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
281
         */
282
        public Handler[] getSelectHandlers() {
283
                ArrayList handlers = new ArrayList();
284
                GeneralPathXIterator gpi = null;
285
                gpi = (GeneralPathXIterator) getPathIterator(null);
286

    
287
                double[] theData = new double[6];
288
                int i=0;
289
                boolean isFirst=true;
290
                while (!gpi.isDone()) {
291
                        int theType = gpi.currentSegment(theData);
292
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
293
                        /* if (!(this instanceof FPolygon2D && isFirst)){
294
                                handlers.add(new PointSelHandler(i,theData[0], theData[1]));
295
                                i++;
296
                        }
297
                        isFirst=false; */
298
                        switch (theType)
299
                        {
300
                        case GeneralPathXIterator.SEG_MOVETO:
301
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
302
                                break;
303
                        case GeneralPathXIterator.SEG_LINETO:
304
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
305
                                break;
306
                        case GeneralPathXIterator.SEG_CLOSE:
307
                                break;
308
                        case GeneralPathXIterator.SEG_QUADTO:
309
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
310
                                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
311
                                break;
312
                        case GeneralPathXIterator.SEG_CUBICTO:
313
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
314
                                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
315
                                handlers.add(new PointSelHandler(i++,theData[4], theData[5]));
316
                                break;
317

    
318
                        }
319
                        gpi.next();
320

    
321

    
322
                }
323

    
324
                return (Handler[]) handlers.toArray(new Handler[0]);
325
        }
326
        /**
327
         * 
328
         *
329
         * @author Vicente Caballero Navarro
330
         */
331
        class PointHandler extends AbstractHandler {
332
                /**
333
                 * Crea un nuevo PointHandler.
334
                 *
335
                 * @param x 
336
                 * @param y 
337
                 */
338
                public PointHandler(int i,double x, double y) {
339
                        point = new Point2D.Double(x, y);
340
                        index=i;
341
                }
342

    
343
                /**
344
                 * 
345
                 *
346
                 * @param x 
347
                 * @param y 
348
                 *
349
                 * @return 
350
                 */
351
                public void move(double x, double y) {
352
                        gp.pointCoords[index*2]+=x;
353
                        gp.pointCoords[index*2+1]+=y;
354
                }
355

    
356
                /**
357
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
358
                 */
359
                public void set(double x, double y) {
360
                        gp.pointCoords[index*2]=x;
361
                        gp.pointCoords[index*2+1]=y;
362
                }
363
        }
364
        /**
365
         * 
366
         *
367
         * @author Vicente Caballero Navarro
368
         */
369
        class PointSelHandler extends AbstractHandler {
370
                /**
371
                 * Crea un nuevo PointHandler.
372
                 *
373
                 * @param x 
374
                 * @param y 
375
                 */
376
                public PointSelHandler(int i,double x, double y) {
377
                        point = new Point2D.Double(x, y);
378
                        index=i;
379
                }
380

    
381
                /**
382
                 * 
383
                 *
384
                 * @param x 
385
                 * @param y 
386
                 *
387
                 * @return 
388
                 */
389
                public void move(double x, double y) {
390
                        gp.pointCoords[index*2]+=x;
391
                        gp.pointCoords[index*2+1]+=y;
392
                }
393

    
394
                /**
395
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
396
                 */
397
                public void set(double x, double y) {
398
                        gp.pointCoords[index*2]=x;
399
                        gp.pointCoords[index*2+1]=y;
400
                }
401
        }
402
}