Statistics
| Revision:

svn-gvsig-desktop / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FPolyline2D.java @ 1848

History | View | Annotate | Download (6.54 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 java.awt.Rectangle;
44
import java.awt.geom.AffineTransform;
45
import java.awt.geom.Line2D;
46
import java.awt.geom.PathIterator;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.io.Serializable;
50
import java.util.ArrayList;
51

    
52
import org.cresques.cts.ICoordTrans;
53

    
54

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

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

    
78
        /* (non-Javadoc)
79
         * @see java.awt.Shape#contains(double, double, double, double)
80
         */
81
        public boolean contains(double x, double y, double w, double h) {
82
                return gp.contains(x, y, w, h);
83
        }
84

    
85
        /* (non-Javadoc)
86
         * @see java.awt.Shape#intersects(double, double, double, double)
87
         */
88
        public boolean intersects(double x, double y, double w, double h) {
89
            // M?s r?pido
90
                return gp.intersects(x, y, w, h);
91
        }
92

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

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

    
107
        /* (non-Javadoc)
108
         * @see java.awt.Shape#getBounds2D()
109
         */
110
        public Rectangle2D getBounds2D() {
111
                return gp.getBounds2D();
112
        }
113

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

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

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

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

    
193
        /**
194
         * DOCUMENT ME!
195
         *
196
         * @param at DOCUMENT ME!
197
         */
198
        public void transform(AffineTransform at) {
199
                gp.transform(at);
200
        }
201

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

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

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