Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / OrientablePrimitive2D.java @ 33603

History | View | Annotate | Download (12.7 KB)

1 27029 jpiera
package org.gvsig.fmap.geom.primitive.impl;
2 20761 jmvivo
3
import java.awt.Rectangle;
4
import java.awt.geom.AffineTransform;
5
import java.awt.geom.Line2D;
6
import java.awt.geom.PathIterator;
7
import java.awt.geom.Point2D;
8
import java.awt.geom.Rectangle2D;
9
import java.util.ArrayList;
10
11 21870 vcaballero
import org.cresques.cts.ICoordTrans;
12
import org.cresques.cts.IProjection;
13 20761 jmvivo
import org.gvsig.fmap.geom.handler.AbstractHandler;
14
import org.gvsig.fmap.geom.handler.FinalHandler;
15
import org.gvsig.fmap.geom.handler.Handler;
16 27029 jpiera
import org.gvsig.fmap.geom.primitive.Envelope;
17
import org.gvsig.fmap.geom.primitive.GeneralPathX;
18
import org.gvsig.fmap.geom.primitive.GeneralPathXIterator;
19
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
20 29097 jpiera
import org.gvsig.fmap.geom.type.GeometryType;
21 20861 jiyarza
import org.gvsig.fmap.geom.util.Converter;
22 20761 jmvivo
23
24 21308 jiyarza
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
25 20761 jmvivo
 *
26
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
41
 *
42
 * For more information, contact:
43
 *
44
 *  Generalitat Valenciana
45
 *   Conselleria d'Infraestructures i Transport
46 21308 jiyarza
 *   Av. Blasco Ib??ez, 50
47 20761 jmvivo
 *   46010 VALENCIA
48
 *   SPAIN
49
 *
50
 *      +34 963862235
51
 *   gvsig@gva.es
52
 *      www.gvsig.gva.es
53
 *
54
 *    or
55
 *
56
 *   IVER T.I. S.A
57
 *   Salamanca 50
58
 *   46005 Valencia
59
 *   Spain
60
 *
61
 *   +34 963163400
62
 *   dac@iver.es
63
 */
64
/* CVS MESSAGES:
65
 *
66
 * $Id: FOrientablePrimitive2D.java,v 1.1 2008/03/12 08:46:20 cvs Exp $
67
 * $Log: FOrientablePrimitive2D.java,v $
68
 * Revision 1.1  2008/03/12 08:46:20  cvs
69
 * *** empty log message ***
70
 *
71
 *
72
 */
73
/**
74 21308 jiyarza
 * @author Jorge Piera Llodr? (jorge.piera@iver.es)
75 20761 jmvivo
 */
76
public abstract class OrientablePrimitive2D extends AbstractPrimitive implements OrientablePrimitive {
77 29097 jpiera
        private static final long serialVersionUID = -820881421374434713L;
78 20761 jmvivo
        protected GeneralPathX gp;
79 21425 vcaballero
80 20761 jmvivo
        /**
81 29097 jpiera
         * The constructor with the GeometryType like and argument
82
         * is used by the {@link GeometryType}{@link #create()}
83
         * to create the geometry
84
         * @param type
85
         * The geometry type
86 20761 jmvivo
         */
87 29097 jpiera
        public OrientablePrimitive2D(GeometryType geometryType) {
88
                super(geometryType, null, null);
89 20761 jmvivo
        }
90 29097 jpiera
91
        public OrientablePrimitive2D(GeometryType geometryType, String id, IProjection projection, GeneralPathX gp){
92
                super(geometryType, id, projection);
93
                this.gp = gp;
94 26788 jpiera
        }
95
96 20761 jmvivo
        /**
97 29097 jpiera
         * TODO m?todo creado para dar visibilidad a gp despues de la refactorizaci?n
98
         * @return
99 20761 jmvivo
         */
100 29097 jpiera
        public GeneralPathX getGeneralPathX() {
101
                return gp;
102 20761 jmvivo
        }
103
104
        /* (non-Javadoc)
105
         * @see java.awt.Shape#contains(double, double)
106
         */
107
        public boolean contains(double x, double y) {
108
                return gp.contains(x, y);
109
        }
110
111
        /* (non-Javadoc)
112
         * @see java.awt.Shape#contains(double, double, double, double)
113
         */
114
        public boolean contains(double x, double y, double w, double h) {
115
                return gp.contains(x, y, w, h);
116
        }
117
118
        /* (non-Javadoc)
119
         * @see java.awt.Shape#intersects(double, double, double, double)
120
         */
121
        public boolean intersects(double x, double y, double w, double h) {
122
            // M?s r?pido
123
                return gp.intersects(x, y, w, h);
124
        }
125
126
        /* (non-Javadoc)
127
         * @see java.awt.Shape#getBounds()
128
         */
129
        public Rectangle getBounds() {
130
                return gp.getBounds();
131
        }
132
133
        /* (non-Javadoc)
134
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
135
         */
136
        public boolean contains(Point2D p) {
137
                return gp.contains(p);
138
        }
139
140
        /* (non-Javadoc)
141
         * @see java.awt.Shape#getBounds2D()
142
         */
143
        public Rectangle2D getBounds2D() {
144
                return gp.getBounds2D();
145
        }
146
147
        /* (non-Javadoc)
148
         * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
149
         */
150
        public boolean contains(Rectangle2D r) {
151
                return gp.contains(r);
152
        }
153
154
        /**
155
         * El m?todo intersects de java.awt.Shape que define la intersecci?n entre
156
         * una polil?nea y un Rectangle2D considera la polil?nea como un Shape
157
         * gen?rico y se producen errores en la selecci?n de polil?neas. Por este
158
         * motivo se ha modificado este m?todo intersect() de FPolyline2D para que
159
         * realize la intersecci?n estricta entre el Rectangle2D y la polil?nea en
160
         * cuesti?n. El precio es un incremento de tiempo m?ximo del 50%.
161
         *
162
         * @param r Rect?ngulo.
163
         *
164
         * @return True si intersecta con el rectangulo que se pasa como par?metro.
165
         */
166
        public boolean intersects(Rectangle2D r) {
167
                //return gp.intersects(r);
168
            // M?s exacto
169
                boolean bool = false;
170
                   if (gp.intersects(r)) {
171
                           ArrayList arrayCoords;
172
                           int theType;
173
                           //Use this array to store segment coordinate data
174
                           double[] theData = new double[6];
175
                           PathIterator theIterator;
176
177
                       Point2D p1 = new Point2D.Double(r.getMinX(),r.getMinY());
178
                       Point2D p2 = new Point2D.Double(r.getMinX(),r.getMaxY());
179
                       Point2D p3 = new Point2D.Double(r.getMaxX(),r.getMaxY());
180
                       Point2D p4 = new Point2D.Double(r.getMaxX(),r.getMinY());
181
                       Line2D l1 = new Line2D.Double(p1,p2);
182
                       Line2D l2 = new Line2D.Double(p2,p3);
183
                       Line2D l3 = new Line2D.Double(p3,p4);
184
                       Line2D l4 = new Line2D.Double(p4,p1);
185
186 30323 jpiera
                           theIterator = this.getPathIterator(null, geomManager.getFlatness());
187 20761 jmvivo
                           arrayCoords = new ArrayList();
188
                           while(!theIterator.isDone()) {
189
                                    theType = theIterator.currentSegment(theData);
190
                           if (theType==PathIterator.SEG_MOVETO) {
191
                                    arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
192
                           } else if (theType==PathIterator.SEG_LINETO) {
193
                                   arrayCoords.add(new Point2D.Double(theData[0], theData[1]));
194
                                   Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-2);
195
                                   Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),theData[0],theData[1]);
196
                                   if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
197
                                                   || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
198
                                                   || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
199
                                                   || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
200
                                                   || r.intersectsLine(l)) {
201
                                           bool = true;
202
                                   }
203
                           } else if(theType==PathIterator.SEG_CLOSE){
204
                                   Point2D firstPoint=(Point2D)arrayCoords.get(0);
205
                                   Point2D pAnt = (Point2D)arrayCoords.get(arrayCoords.size()-1);
206
                           Line2D l = new Line2D.Double(pAnt.getX(),pAnt.getY(),firstPoint.getX(),firstPoint.getY());
207
                           if (l.intersectsLine(l1.getX1(),l1.getY1(),l1.getX2(),l1.getY2())
208
                                           || l.intersectsLine(l2.getX1(),l2.getY1(),l2.getX2(),l2.getY2())
209
                                           || l.intersectsLine(l3.getX1(),l3.getY1(),l3.getX2(),l3.getY2())
210
                                           || l.intersectsLine(l4.getX1(),l4.getY1(),l4.getX2(),l4.getY2())
211
                                           || r.intersectsLine(l)) {
212
                                   bool = true;
213
                           }
214
                           }else {
215
                                    System.out.println("Not supported here");
216
                           }
217
                                theIterator.next();
218
                            }
219
                   }
220
                   return bool;
221
        }
222
223
        /* (non-Javadoc)
224
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
225
         */
226
        public PathIterator getPathIterator(AffineTransform at) {
227
                return gp.getPathIterator(at);
228
        }
229
230
        /* (non-Javadoc)
231
         * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)
232
         */
233
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
234
                return gp.getPathIterator(at, flatness);
235
        }
236
237
        /**
238
         * DOCUMENT ME!
239
         *
240
         * @param at DOCUMENT ME!
241
         */
242
        public void transform(AffineTransform at) {
243
244
        // TODO: PRUEBA. BORRAR ESTA LINEA
245
        // gp = FConverter.transformToInts(gp, at);
246
247
                gp.transform(at);
248
        }
249
250
        /* (non-Javadoc)
251
         * @see com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans)
252
         */
253
        public void reProject(ICoordTrans ct) {
254
                gp.reProject(ct);
255
        }
256
257
        /* (non-Javadoc)
258
         * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
259
         */
260
        public Handler[] getStretchingHandlers() {
261
                ArrayList handlers = new ArrayList();
262
                GeneralPathXIterator gpi = null;
263
                gpi = (GeneralPathXIterator) getPathIterator(null);
264
265
                double[] theData = new double[6];
266
                int i=0;
267
                while (!gpi.isDone()) {
268
                        gpi.currentSegment(theData);
269
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
270
                        handlers.add(new PointHandler(i,theData[0], theData[1]));
271
                        i++;
272
                        gpi.next();
273
                }
274
275
                return (Handler[]) handlers.toArray(new Handler[0]);
276
        }
277 26788 jpiera
278
        /* (non-Javadoc)
279
         * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
280
         */
281
        public double getCoordinateAt(int index, int dimension) {
282
                GeneralPathXIterator gpi = null;
283
                gpi = (GeneralPathXIterator) getPathIterator(null);
284 20761 jmvivo
285 26788 jpiera
                double[] theData = new double[6];
286
                int i=0;
287
                while (!gpi.isDone()) {
288
                        if (index==i){
289
                                gpi.currentSegment(theData);
290
                                return theData[dimension];
291
                        }
292
                        i++;
293
                        gpi.next();
294
                }
295
                throw new ArrayIndexOutOfBoundsException();
296
        }
297
298 20761 jmvivo
        /* (non-Javadoc)
299
         * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
300
         */
301
        public Handler[] getSelectHandlers() {
302
                ArrayList handlers = new ArrayList();
303
                GeneralPathXIterator gpi = null;
304
                gpi = (GeneralPathXIterator) getPathIterator(null);
305
306
                double[] theData = new double[6];
307
                int i=0;
308
                //boolean isFirst=true;
309
                while (!gpi.isDone()) {
310
                        int theType = gpi.currentSegment(theData);
311
                        //g.fillRect((int)(theData[0]-3),(int)(theData[1]-3),6,6);
312
                        /* if (!(this instanceof FPolygon2D && isFirst)){
313
                                handlers.add(new PointSelHandler(i,theData[0], theData[1]));
314
                                i++;
315
                        }
316
                        isFirst=false; */
317
                        switch (theType)
318
                        {
319
                        case GeneralPathXIterator.SEG_MOVETO:
320
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
321
                                break;
322
                        case GeneralPathXIterator.SEG_LINETO:
323
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
324
                                break;
325
                        case GeneralPathXIterator.SEG_CLOSE:
326
                                break;
327
                        case GeneralPathXIterator.SEG_QUADTO:
328
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
329
                                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
330
                                break;
331
                        case GeneralPathXIterator.SEG_CUBICTO:
332
                                handlers.add(new PointSelHandler(i++,theData[0], theData[1]));
333
                                handlers.add(new PointSelHandler(i++,theData[2], theData[3]));
334
                                handlers.add(new PointSelHandler(i++,theData[4], theData[5]));
335
                                break;
336
337
                        }
338
                        gpi.next();
339
340
341
                }
342
343
                return (Handler[]) handlers.toArray(new Handler[0]);
344
        }
345
        /**
346
         * DOCUMENT ME!
347
         *
348
         * @author Vicente Caballero Navarro
349
         */
350
        class PointHandler extends AbstractHandler implements FinalHandler{
351
                /**
352
                 * Crea un nuevo PointHandler.
353
                 *
354
                 * @param x DOCUMENT ME!
355
                 * @param y DOCUMENT ME!
356
                 */
357
                public PointHandler(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 21382 csanchez
                        gp.getPointCoords()[index*2]+=x;
372
                        gp.getPointCoords()[index*2+1]+=y;
373 20761 jmvivo
                }
374
375
                /**
376
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
377
                 */
378
                public void set(double x, double y) {
379 21382 csanchez
                        gp.getPointCoords()[index*2]=x;
380
                        gp.getPointCoords()[index*2+1]=y;
381 20761 jmvivo
                }
382
        }
383
        /**
384
         * DOCUMENT ME!
385
         *
386
         * @author Vicente Caballero Navarro
387
         */
388
        class PointSelHandler extends AbstractHandler implements FinalHandler{
389
                /**
390
                 * Crea un nuevo PointHandler.
391
                 *
392
                 * @param x DOCUMENT ME!
393
                 * @param y DOCUMENT ME!
394
                 */
395
                public PointSelHandler(int i,double x, double y) {
396
                        point = new Point2D.Double(x, y);
397
                        index=i;
398
                }
399
400
                /**
401
                 * DOCUMENT ME!
402
                 *
403
                 * @param x DOCUMENT ME!
404
                 * @param y DOCUMENT ME!
405
                 *
406
                 * @return DOCUMENT ME!
407
                 */
408
                public void move(double x, double y) {
409 21382 csanchez
                        gp.getPointCoords()[index*2]+=x;
410
                        gp.getPointCoords()[index*2+1]+=y;
411 20761 jmvivo
                }
412
413
                /**
414
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
415
                 */
416
                public void set(double x, double y) {
417 21382 csanchez
                        gp.getPointCoords()[index*2]=x;
418
                        gp.getPointCoords()[index*2+1]=y;
419 20761 jmvivo
                }
420
        }
421 21425 vcaballero
422 20761 jmvivo
        /*
423
         * (non-Javadoc)
424
         * @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
425
         */
426 26788 jpiera
        public int getDimension() {
427 20761 jmvivo
                return 2;
428
        }
429 21425 vcaballero
430
        public Envelope getEnvelope() {
431
                Rectangle2D r=gp.getBounds2D();
432 27019 jpiera
                return new Envelope2D(r.getX(),r.getY(),r.getMaxX(),r.getMaxY());
433 21425 vcaballero
        }
434 20761 jmvivo
}