Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FPolyline2D.java @ 3886

History | View | Annotate | Download (9.26 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
package com.iver.cit.gvsig.fmap.core;
42

    
43
import org.cresques.cts.ICoordTrans;
44

    
45
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
46

    
47
import java.awt.Rectangle;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.PathIterator;
50
import java.awt.geom.Line2D;
51
import java.awt.geom.Point2D;
52
import java.awt.geom.Rectangle2D;
53
import java.util.ArrayList;
54

    
55

    
56
/**
57
 * DOCUMENT ME!
58
 *
59
 * @author Fernando Gonz?lez Cort?s
60
 */
61
public class FPolyline2D implements FShape {
62
        protected GeneralPathX gp;
63
        /**
64
         * Crea un nuevo FPolyline2D.
65
         *
66
         * @param gpx GeneralPathX.
67
         */
68
        public FPolyline2D(GeneralPathX gpx) {
69
                gp = gpx;
70
        }
71

    
72
        /* (non-Javadoc)
73
         * @see java.awt.Shape#contains(double, double)
74
         */
75
        public boolean contains(double x, double y) {
76
                return gp.contains(x, y);
77
        }
78

    
79
        /* (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
                return gp.contains(x, y, w, h);
84
        }
85

    
86
        /* (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
            // M?s r?pido
91
                return gp.intersects(x, y, w, h);
92
        }
93

    
94
        /* (non-Javadoc)
95
         * @see java.awt.Shape#getBounds()
96
         */
97
        public Rectangle getBounds() {
98
                return gp.getBounds();
99
        }
100

    
101
        /* (non-Javadoc)
102
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
103
         */
104
        public boolean contains(Point2D p) {
105
                return gp.contains(p);
106
        }
107

    
108
        /* (non-Javadoc)
109
         * @see java.awt.Shape#getBounds2D()
110
         */
111
        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
        /**
123
         * 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
         * @param r Rect?ngulo.
131
         *
132
         * @return True si intersecta con el rectangulo que se pasa como par?metro.
133
         */
134
        public boolean intersects(Rectangle2D r) {
135
                //return gp.intersects(r);
136
            // M?s exacto
137
                boolean bool = false;
138
                   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

    
145
                       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

    
154
                           theIterator = this.getPathIterator(null);
155
                           arrayCoords = new ArrayList();
156
                            while(!theIterator.isDone()) {
157
                                    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
                           } else {
172
                                    System.out.println("Not supported here");
173
                           }
174
                                theIterator.next();
175
                            }
176
                   }
177
                   return bool;
178
        }
179

    
180
        /* (non-Javadoc)
181
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
182
         */
183
        public PathIterator getPathIterator(AffineTransform at) {
184
                return gp.getPathIterator(at);
185
        }
186

    
187
        /* (non-Javadoc)
188
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
189
         */
190
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
191
                return gp.getPathIterator(at, flatness);
192
        }
193

    
194
        /**
195
         * DOCUMENT ME!
196
         *
197
         * @param at DOCUMENT ME!
198
         */
199
        public void transform(AffineTransform at) {
200

    
201
        // TODO: PRUEBA. BORRAR ESTA LINEA
202
        // gp = FConverter.transformToInts(gp, at);
203

    
204
                gp.transform(at);
205
        }
206

    
207
        /**
208
         * @see com.iver.cit.gvsig.fmap.core.FShape#getShapeType()
209
         */
210
        public int getShapeType() {
211
                return FShape.LINE;
212
        }
213

    
214
        /* (non-Javadoc)
215
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
216
         */
217
        public FShape cloneFShape() {
218
                return new FPolyline2D((GeneralPathX) gp.clone());
219
        }
220

    
221
        /* (non-Javadoc)
222
         * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
223
         */
224
        public void reProject(ICoordTrans ct) {
225
                gp.reProject(ct);
226
        }
227

    
228
        /* (non-Javadoc)
229
         * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
230
         */
231
        public Handler[] getStretchingHandlers() {
232
                ArrayList handlers = new ArrayList();
233
                GeneralPathXIterator gpi = null;
234
                gpi = (GeneralPathXIterator) getPathIterator(null);
235

    
236
                double[] theData = new double[6];
237
                int i=0;
238
                while (!gpi.isDone()) {
239
                        int theType = gpi.currentSegment(theData);
240
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
241
                        handlers.add(new PointHandler(i,theData[0], theData[1]));
242
                        i++;
243
                        gpi.next();
244
                }
245

    
246
                return (Handler[]) handlers.toArray(new Handler[0]);
247
        }
248

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

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

    
267
                return (Handler[]) handlers.toArray(new Handler[0]);
268
        }
269
        /**
270
         * DOCUMENT ME!
271
         *
272
         * @author Vicente Caballero Navarro
273
         */
274
        class PointHandler extends AbstractHandler {
275
                /**
276
                 * Crea un nuevo PointHandler.
277
                 *
278
                 * @param x DOCUMENT ME!
279
                 * @param y DOCUMENT ME!
280
                 */
281
                public PointHandler(int i,double x, double y) {
282
                        point = new Point2D.Double(x, y);
283
                        index=i;
284
                }
285

    
286
                /**
287
                 * DOCUMENT ME!
288
                 *
289
                 * @param x DOCUMENT ME!
290
                 * @param y DOCUMENT ME!
291
                 *
292
                 * @return DOCUMENT ME!
293
                 */
294
                public void move(double x, double y) {
295
                        gp.pointCoords[index*2]+=x;
296
                        gp.pointCoords[index*2+1]+=y;
297
                }
298

    
299
                /**
300
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
301
                 */
302
                public void set(double x, double y) {
303
                        gp.pointCoords[index*2]=x;
304
                        gp.pointCoords[index*2+1]=y;
305
                }
306
        }
307
        /**
308
         * DOCUMENT ME!
309
         *
310
         * @author Vicente Caballero Navarro
311
         */
312
        class PointSelHandler extends AbstractHandler {
313
                /**
314
                 * Crea un nuevo PointHandler.
315
                 *
316
                 * @param x DOCUMENT ME!
317
                 * @param y DOCUMENT ME!
318
                 */
319
                public PointSelHandler(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
}