Statistics
| Revision:

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

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