Statistics
| Revision:

root / branches / FMap_CAD / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FPolyline2D.java @ 3513

History | View | Annotate | Download (9.41 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#getBounds()
88
         */
89
        public Rectangle getBounds() {
90
                return gp.getBounds();
91
        }
92

    
93
        /* (non-Javadoc)
94
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
95
         */
96
        public boolean contains(Point2D p) {
97
                return gp.contains(p);
98
        }
99

    
100
        /* (non-Javadoc)
101
         * @see java.awt.Shape#getBounds2D()
102
         */
103
        public Rectangle2D getBounds2D() {
104
                Rectangle2D r=gp.getBounds2D();
105
                if (r.getWidth()==0)r.setRect(r.getX(),r.getY(),0.1,r.getHeight());
106
                if (r.getHeight()==0)r.setRect(r.getX(),r.getY(),r.getWidth(),0.1);
107
                
108
                return r;
109
        }
110

    
111
        /* (non-Javadoc)
112
         * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
113
         */
114
        public boolean contains(Rectangle2D r) {
115
                return gp.contains(r);
116
        }
117

    
118
        /**
119
         * El m?todo intersects de java.awt.Shape que define la intersecci?n entre
120
         * una polil?nea y un Rectangle2D considera la polil?nea como un Shape
121
         * gen?rico y se producen errores en la selecci?n de polil?neas. Por este
122
         * motivo se ha modificado este m?todo intersect() de FPolyline2D para que
123
         * realize la intersecci?n estricta entre el Rectangle2D y la polil?nea en
124
         * cuesti?n. El precio es un incremento de tiempo m?ximo del 50%.
125
         *
126
         * @param r Rect?ngulo.
127
         *
128
         * @return True si intersecta con el rectangulo que se pasa como par?metro.
129
         */
130
        public boolean intersects(Rectangle2D r, double flatness) {
131
                //return gp.intersects(r);
132
            // M?s exacto
133
                boolean bool = false;
134
                   if (gp.intersects(r)) {
135
                           ArrayList arrayCoords;
136
                           int theType;
137
                           //Use this array to store segment coordinate data
138
                           double[] theData = new double[6];
139
                           PathIterator theIterator;
140
                
141
                       Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
142
                       Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
143
                       Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
144
                       Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
145
                       Line2D l1 = new Line2D.Double(p1,p2);
146
                       Line2D l2 = new Line2D.Double(p2,p3);
147
                       Line2D l3 = new Line2D.Double(p3,p4);
148
                       Line2D l4 = new Line2D.Double(p4,p1);
149
                
150
                           theIterator = this.getPathIterator(null, flatness);
151
                           arrayCoords = new ArrayList();
152
                            while(!theIterator.isDone()) {
153
                                    theType = theIterator.currentSegment(theData);
154
                           if (theType==PathIterator.SEG_MOVETO) {
155
                                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
156
                           } else if (theType==PathIterator.SEG_LINETO) {
157
                                   arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
158
                                   Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-2);
159
                                   Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),theData[0],theData[1]);
160
                                   if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
161
                                                   || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
162
                                                   || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
163
                                                   || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
164
                                                   || r.intersectsLine(l)) {
165
                                           bool = true;
166
                                   }
167
                           } else {
168
                                    System.out.println("Not supported here");
169
                           }
170
                                theIterator.next();
171
                            }
172
                   }
173
                   return bool;
174
        }
175

    
176
        /* (non-Javadoc)
177
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
178
         */
179
        public PathIterator getPathIterator(AffineTransform at) {
180
                return gp.getPathIterator(at);
181
        }
182

    
183
        /* (non-Javadoc)
184
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
185
         */
186
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
187
                return gp.getPathIterator(at, flatness);
188
        }
189

    
190
        /**
191
         * DOCUMENT ME!
192
         *
193
         * @param at DOCUMENT ME!
194
         */
195
        public void transform(AffineTransform at) {
196
        
197
        // TODO: PRUEBA. BORRAR ESTA LINEA
198
        // gp = FConverter.transformToInts(gp, at);
199
        
200
                gp.transform(at);
201
        }
202

    
203
        /**
204
         * @see com.iver.cit.gvsig.fmap.core.FShape#getShapeType()
205
         */
206
        public int getShapeType() {
207
                return FShape.LINE;
208
        }
209

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

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

    
224
        /**
225
         * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
226
         */
227
        public Handler[] getStretchingHandlers() {
228
                ArrayList handlers = new ArrayList();
229
                GeneralPathXIterator gpi = null;
230
                gpi = (GeneralPathXIterator) getPathIterator(null);
231

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

    
242
                return (Handler[]) handlers.toArray(new Handler[0]);
243
        }
244
        /**
245
         * DOCUMENT ME!
246
         *
247
         * @author Vicente Caballero Navarro
248
         */
249
        class PointHandler extends AbstractHandler {
250
                /**
251
                 * Crea un nuevo PointHandler.
252
                 *
253
                 * @param x DOCUMENT ME!
254
                 * @param y DOCUMENT ME!
255
                 */
256
                public PointHandler(int i,double x, double y) {
257
                        point = new Point2D.Double(x, y);
258
                        index=i;
259
                }
260

    
261
                /**
262
                 * DOCUMENT ME!
263
                 *
264
                 * @param x DOCUMENT ME!
265
                 * @param y DOCUMENT ME!
266
                 *
267
                 * @return DOCUMENT ME!
268
                 */
269
                public void move(double x, double y) {
270
                        gp.pointCoords[index*2]+=x;
271
                        gp.pointCoords[index*2+1]+=y;
272
                }
273

    
274
                /**
275
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
276
                 */
277
                public void set(double x, double y) {
278
                        gp.pointCoords[index*2]=x;
279
                        gp.pointCoords[index*2+1]=y;
280
                }
281
        }
282
        /**
283
         * DOCUMENT ME!
284
         *
285
         * @author Vicente Caballero Navarro
286
         */
287
        class PointSelHandler extends AbstractHandler {
288
                /**
289
                 * Crea un nuevo PointHandler.
290
                 *
291
                 * @param x DOCUMENT ME!
292
                 * @param y DOCUMENT ME!
293
                 */
294
                public PointSelHandler(int i,double x, double y) {
295
                        point = new Point2D.Double(x, y);
296
                        index=i;
297
                }
298

    
299
                /**
300
                 * DOCUMENT ME!
301
                 *
302
                 * @param x DOCUMENT ME!
303
                 * @param y DOCUMENT ME!
304
                 *
305
                 * @return DOCUMENT ME!
306
                 */
307
                public void move(double x, double y) {
308
                        gp.pointCoords[index*2]+=x;
309
                        gp.pointCoords[index*2+1]+=y;
310
                }
311

    
312
                /**
313
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
314
                 */
315
                public void set(double x, double y) {
316
                        gp.pointCoords[index*2]=x;
317
                        gp.pointCoords[index*2+1]=y;
318
                }
319
        }
320
        /**
321
         * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
322
         */
323
        public Handler[] getSelectHandlers() {
324
                ArrayList handlers = new ArrayList();
325
                GeneralPathXIterator gpi = null;
326
                gpi = (GeneralPathXIterator) getPathIterator(null);
327

    
328
                double[] theData = new double[6];
329
                int i=0;
330
                while (!gpi.isDone()) {
331
                        int theType = gpi.currentSegment(theData);
332
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
333
                        handlers.add(new PointSelHandler(i,theData[0], theData[1]));
334
                        i++;
335
                        gpi.next();
336
                }
337

    
338
                return (Handler[]) handlers.toArray(new Handler[0]);
339
        }
340

    
341
        public boolean intersects(double x, double y, double w, double h) {
342
                return gp.intersects(x, y, w, h);
343
        }
344

    
345
        public boolean intersects(Rectangle2D r) {
346
                return gp.intersects(r);
347
        }
348
}