Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FPolyline2D.java @ 38563

History | View | Annotate | Download (11 KB)

1 1100 fjp
/* 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 262 fjp
package com.iver.cit.gvsig.fmap.core;
42
43 305 fjp
import java.awt.Rectangle;
44
import java.awt.geom.AffineTransform;
45 2196 vcaballero
import java.awt.geom.Line2D;
46 10627 caballero
import java.awt.geom.PathIterator;
47 305 fjp
import java.awt.geom.Point2D;
48 267 fjp
import java.awt.geom.Rectangle2D;
49 1132 vcaballero
import java.util.ArrayList;
50 885 fjp
51 10627 caballero
import org.cresques.cts.ICoordTrans;
52 262 fjp
53 15842 vcaballero
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
54 10627 caballero
55 15842 vcaballero
56 885 fjp
/**
57
 * DOCUMENT ME!
58
 *
59
 * @author Fernando Gonz?lez Cort?s
60
 */
61 2196 vcaballero
public class FPolyline2D implements FShape {
62 262 fjp
        protected GeneralPathX gp;
63 885 fjp
        /**
64
         * Crea un nuevo FPolyline2D.
65
         *
66 1005 vcaballero
         * @param gpx GeneralPathX.
67 885 fjp
         */
68 266 fjp
        public FPolyline2D(GeneralPathX gpx) {
69 262 fjp
                gp = gpx;
70
        }
71
72 305 fjp
        /* (non-Javadoc)
73
         * @see java.awt.Shape#contains(double, double)
74 262 fjp
         */
75 305 fjp
        public boolean contains(double x, double y) {
76 885 fjp
                return gp.contains(x, y);
77 262 fjp
        }
78
79 305 fjp
        /* (non-Javadoc)
80
         * @see java.awt.Shape#contains(double, double, double, double)
81
         */
82
        public boolean contains(double x, double y, double w, double h) {
83 885 fjp
                return gp.contains(x, y, w, h);
84 305 fjp
        }
85 262 fjp
86 305 fjp
        /* (non-Javadoc)
87
         * @see java.awt.Shape#intersects(double, double, double, double)
88
         */
89
        public boolean intersects(double x, double y, double w, double h) {
90 1298 fjp
            // M?s r?pido
91 885 fjp
                return gp.intersects(x, y, w, h);
92 305 fjp
        }
93 262 fjp
94 305 fjp
        /* (non-Javadoc)
95
         * @see java.awt.Shape#getBounds()
96 262 fjp
         */
97 305 fjp
        public Rectangle getBounds() {
98
                return gp.getBounds();
99 262 fjp
        }
100
101 305 fjp
        /* (non-Javadoc)
102
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
103 262 fjp
         */
104 305 fjp
        public boolean contains(Point2D p) {
105
                return gp.contains(p);
106 262 fjp
        }
107
108 267 fjp
        /* (non-Javadoc)
109 305 fjp
         * @see java.awt.Shape#getBounds2D()
110 267 fjp
         */
111 305 fjp
        public Rectangle2D getBounds2D() {
112
                return gp.getBounds2D();
113
        }
114
115
        /* (non-Javadoc)
116
         * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
117
         */
118
        public boolean contains(Rectangle2D r) {
119
                return gp.contains(r);
120
        }
121
122 790 jmorell
        /**
123 885 fjp
         * El m?todo intersects de java.awt.Shape que define la intersecci?n entre
124
         * una polil?nea y un Rectangle2D considera la polil?nea como un Shape
125
         * gen?rico y se producen errores en la selecci?n de polil?neas. Por este
126
         * motivo se ha modificado este m?todo intersect() de FPolyline2D para que
127
         * realize la intersecci?n estricta entre el Rectangle2D y la polil?nea en
128
         * cuesti?n. El precio es un incremento de tiempo m?ximo del 50%.
129
         *
130 1005 vcaballero
         * @param r Rect?ngulo.
131 885 fjp
         *
132 1005 vcaballero
         * @return True si intersecta con el rectangulo que se pasa como par?metro.
133 305 fjp
         */
134 267 fjp
        public boolean intersects(Rectangle2D r) {
135 1296 jmorell
                //return gp.intersects(r);
136 1298 fjp
            // M?s exacto
137 1296 jmorell
                boolean bool = false;
138 885 fjp
                   if (gp.intersects(r)) {
139
                           ArrayList arrayCoords;
140
                           int theType;
141
                           //Use this array to store segment coordinate data
142
                           double[] theData = new double[6];
143
                           PathIterator theIterator;
144 3833 caballero
145 885 fjp
                       Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
146
                       Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
147
                       Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
148
                       Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
149
                       Line2D l1 = new Line2D.Double(p1,p2);
150
                       Line2D l2 = new Line2D.Double(p2,p3);
151
                       Line2D l3 = new Line2D.Double(p3,p4);
152
                       Line2D l4 = new Line2D.Double(p4,p1);
153 3833 caballero
154 15842 vcaballero
                           theIterator = this.getPathIterator(null,FConverter.FLATNESS);
155 885 fjp
                           arrayCoords = new ArrayList();
156 4846 caballero
                           while(!theIterator.isDone()) {
157 885 fjp
                                    theType = theIterator.currentSegment(theData);
158
                           if (theType==PathIterator.SEG_MOVETO) {
159
                                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
160
                           } else if (theType==PathIterator.SEG_LINETO) {
161
                                   arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
162
                                   Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-2);
163
                                   Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),theData[0],theData[1]);
164
                                   if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
165
                                                   || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
166
                                                   || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
167
                                                   || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
168
                                                   || r.intersectsLine(l)) {
169
                                           bool = true;
170
                                   }
171 4846 caballero
                           } else if(theType==PathIterator.SEG_CLOSE){
172
                                   Point2D firstPoint=(Point2D)arrayCoords.get(0);
173
                                   Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-1);
174
                           Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),firstPoint.getX(),firstPoint.getY());
175
                           if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
176
                                           || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
177
                                           || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
178
                                           || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
179
                                           || r.intersectsLine(l)) {
180
                                   bool = true;
181
                           }
182
                           }else {
183 885 fjp
                                    System.out.println("Not supported here");
184
                           }
185
                                theIterator.next();
186
                            }
187
                   }
188 1296 jmorell
                   return bool;
189 267 fjp
        }
190
191
        /* (non-Javadoc)
192 305 fjp
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
193 267 fjp
         */
194 305 fjp
        public PathIterator getPathIterator(AffineTransform at) {
195
                return gp.getPathIterator(at);
196 267 fjp
        }
197
198 305 fjp
        /* (non-Javadoc)
199
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
200
         */
201
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
202 885 fjp
                return gp.getPathIterator(at, flatness);
203 305 fjp
        }
204
205 885 fjp
        /**
206
         * DOCUMENT ME!
207
         *
208
         * @param at DOCUMENT ME!
209
         */
210
        public void transform(AffineTransform at) {
211 3833 caballero
212 2859 fjp
        // TODO: PRUEBA. BORRAR ESTA LINEA
213
        // gp = FConverter.transformToInts(gp, at);
214 3833 caballero
215 305 fjp
                gp.transform(at);
216
        }
217
218 324 fernando
        /**
219
         * @see com.iver.cit.gvsig.fmap.core.FShape#getShapeType()
220
         */
221
        public int getShapeType() {
222
                return FShape.LINE;
223
        }
224
225 703 fjp
        /* (non-Javadoc)
226
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
227
         */
228
        public FShape cloneFShape() {
229
                return new FPolyline2D((GeneralPathX) gp.clone());
230
        }
231
232 885 fjp
        /* (non-Javadoc)
233
         * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
234
         */
235
        public void reProject(ICoordTrans ct) {
236
                gp.reProject(ct);
237
        }
238 3833 caballero
239
        /* (non-Javadoc)
240
         * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
241
         */
242
        public Handler[] getStretchingHandlers() {
243
                ArrayList handlers = new ArrayList();
244
                GeneralPathXIterator gpi = null;
245
                gpi = (GeneralPathXIterator) getPathIterator(null);
246
247
                double[] theData = new double[6];
248
                int i=0;
249
                while (!gpi.isDone()) {
250
                        int theType = gpi.currentSegment(theData);
251
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
252
                        handlers.add(new PointHandler(i,theData[0], theData[1]));
253
                        i++;
254
                        gpi.next();
255
                }
256
257
                return (Handler[]) handlers.toArray(new Handler[0]);
258
        }
259
260
        /* (non-Javadoc)
261
         * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
262
         */
263
        public Handler[] getSelectHandlers() {
264
                ArrayList handlers = new ArrayList();
265
                GeneralPathXIterator gpi = null;
266
                gpi = (GeneralPathXIterator) getPathIterator(null);
267
268
                double[] theData = new double[6];
269
                int i=0;
270 4120 caballero
                boolean isFirst=true;
271 3833 caballero
                while (!gpi.isDone()) {
272
                        int theType = gpi.currentSegment(theData);
273
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
274 4135 fjp
                        /* if (!(this instanceof FPolygon2D && isFirst)){
275 4120 caballero
                                handlers.add(new PointSelHandler(i,theData[0], theData[1]));
276
                                i++;
277
                        }
278 4135 fjp
                        isFirst=false; */
279
                        switch (theType)
280
                        {
281
                        case GeneralPathXIterator.SEG_MOVETO:
282
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
283
                                break;
284
                        case GeneralPathXIterator.SEG_LINETO:
285
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
286
                                break;
287
                        case GeneralPathXIterator.SEG_CLOSE:
288
                                break;
289
                        case GeneralPathXIterator.SEG_QUADTO:
290
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
291 4846 caballero
                                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
292 4135 fjp
                                break;
293
                        case GeneralPathXIterator.SEG_CUBICTO:
294
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
295
                                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
296
                                handlers.add(new PointSelHandler(i++,theData[4], theData[5]));
297
                                break;
298 4846 caballero
299 4135 fjp
                        }
300 3833 caballero
                        gpi.next();
301 4120 caballero
302
303 3833 caballero
                }
304
305
                return (Handler[]) handlers.toArray(new Handler[0]);
306
        }
307
        /**
308
         * DOCUMENT ME!
309
         *
310
         * @author Vicente Caballero Navarro
311
         */
312 8949 caballero
        class PointHandler extends AbstractHandler implements IFinalHandler{
313 3833 caballero
                /**
314
                 * Crea un nuevo PointHandler.
315
                 *
316
                 * @param x DOCUMENT ME!
317
                 * @param y DOCUMENT ME!
318
                 */
319
                public PointHandler(int i,double x, double y) {
320
                        point = new Point2D.Double(x, y);
321
                        index=i;
322
                }
323
324
                /**
325
                 * DOCUMENT ME!
326
                 *
327
                 * @param x DOCUMENT ME!
328
                 * @param y DOCUMENT ME!
329
                 *
330
                 * @return DOCUMENT ME!
331
                 */
332
                public void move(double x, double y) {
333
                        gp.pointCoords[index*2]+=x;
334
                        gp.pointCoords[index*2+1]+=y;
335
                }
336
337
                /**
338
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
339
                 */
340
                public void set(double x, double y) {
341
                        gp.pointCoords[index*2]=x;
342
                        gp.pointCoords[index*2+1]=y;
343
                }
344
        }
345
        /**
346
         * DOCUMENT ME!
347
         *
348
         * @author Vicente Caballero Navarro
349
         */
350 8949 caballero
        class PointSelHandler extends AbstractHandler implements IFinalHandler{
351 3833 caballero
                /**
352
                 * Crea un nuevo PointHandler.
353
                 *
354
                 * @param x DOCUMENT ME!
355
                 * @param y DOCUMENT ME!
356
                 */
357
                public PointSelHandler(int i,double x, double y) {
358
                        point = new Point2D.Double(x, y);
359
                        index=i;
360
                }
361
362
                /**
363
                 * DOCUMENT ME!
364
                 *
365
                 * @param x DOCUMENT ME!
366
                 * @param y DOCUMENT ME!
367
                 *
368
                 * @return DOCUMENT ME!
369
                 */
370
                public void move(double x, double y) {
371
                        gp.pointCoords[index*2]+=x;
372
                        gp.pointCoords[index*2+1]+=y;
373
                }
374
375
                /**
376
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
377
                 */
378
                public void set(double x, double y) {
379
                        gp.pointCoords[index*2]=x;
380
                        gp.pointCoords[index*2+1]=y;
381
                }
382
        }
383 262 fjp
}