Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / PolylineCADTool.java @ 39280

History | View | Annotate | Download (23 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.editing.gui.cad.tools;
23

    
24
import java.awt.event.InputEvent;
25
import java.awt.event.MouseEvent;
26
import java.awt.geom.Point2D;
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import javax.swing.JOptionPane;
31

    
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.app.ApplicationLocator;
37
import org.gvsig.editing.gui.cad.exception.CommandException;
38
import org.gvsig.editing.gui.cad.tools.smc.PolylineCADToolContext;
39
import org.gvsig.editing.gui.cad.tools.smc.PolylineCADToolContext.PolylineCADToolState;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.fmap.geom.exception.CreateGeometryException;
43
import org.gvsig.fmap.geom.handler.Handler;
44
import org.gvsig.fmap.geom.primitive.Curve;
45
import org.gvsig.fmap.geom.primitive.GeneralPathX;
46
import org.gvsig.fmap.geom.primitive.Point;
47
import org.gvsig.fmap.geom.primitive.Surface;
48
import org.gvsig.fmap.geom.util.UtilFunctions;
49
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
50
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
51
import org.gvsig.i18n.Messages;
52

    
53
/**
54
 * DOCUMENT ME!
55
 * 
56
 * @author Vicente Caballero Navarro
57
 */
58
public class PolylineCADTool extends AbstractCurveSurfaceCADTool {
59

    
60
    private static final Logger LOG = LoggerFactory.getLogger(PolylineCADTool.class);
61

    
62
    protected PolylineCADToolContext _fsm;
63
    protected Point2D firstPoint;
64
    protected Point2D antPoint;
65
    protected Point2D antantPoint;
66
    protected Point2D antCenter;
67
    protected Point2D antInter;
68
    protected List<Curve> list = new ArrayList<Curve>();
69
    protected boolean close = false;
70

    
71
    /**
72
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
73
     * carga previa a la utilizaci?n de la herramienta.
74
     */
75
    public void init() {
76
        _fsm = new PolylineCADToolContext(this);
77
    }
78

    
79
    public Geometry getGeometry() {
80
        Geometry newGeom = null;
81
        newGeom = concatenateCurves(list, false);
82
        return newGeom;
83
    }
84

    
85

    
86

    
87
    public int getLinesCount() {
88
        return list.size();
89
    }
90

    
91
    public boolean isPolygonLayer() {
92
        try {
93
            return (((FLyrVect) getVLE().getLayer()).getShapeType() == Geometry.TYPES.SURFACE);
94
        } catch (ReadException e) {
95
            return false;
96
        }
97
    }
98

    
99
    public void endGeometry() {
100
        
101
        /*
102
        if (gpx == null) {
103
            gpx = new GeneralPathX();
104
            Geometry[] geoms = (Geometry[]) list.toArray(new Geometry[0]);
105
            MultiPrimitive fgc = createMultiPrimitive(geoms);
106
            // No queremos guardar FGeometryCollections:
107
            gpx.append(fgc.getPathIterator(null, geomManager.getFlatness()),
108
                true);
109
        }
110
        
111
        try {
112
            if (((FLyrVect) getVLE().getLayer()).getShapeType() == Geometry.TYPES.SURFACE
113
                && !close) {
114
                closeGeometry();
115
            }
116
        } catch (ReadException e) {
117
            NotificationManager.addError(e.getMessage(), e);
118
        }
119
        */
120
        
121

    
122
        Geometry newGeom = null;
123
        int type = getCadToolAdapter().getActiveLayerType();
124
        
125
        if ((type == Geometry.TYPES.SURFACE)
126
            || (type == Geometry.TYPES.MULTISURFACE)) {
127
            
128
            newGeom = concatenateCurves(list, true);
129
            closeSurfaceIfNecessary((Surface) newGeom);
130
            // newGeom = createSurface(gpx);
131
        } else {
132
            newGeom = concatenateCurves(list, false);
133
            // newGeom = createCurve(gpx);
134
        }
135
        insertAndSelectGeometry(newGeom);
136
        _fsm = new PolylineCADToolContext(this);
137
        list.clear();
138
        clearTemporalCache();
139
        close = false;
140
        antantPoint = antCenter = antInter = antPoint = firstPoint = null;
141
    }
142

    
143
    /**
144
     * @param newGeom
145
     */
146
    private void closeSurfaceIfNecessary(Surface geo) {
147
        
148
        if (!close && geo != null && geo.getNumVertices() > 1) {
149
            Point firstp = geo.getVertex(0);
150
            firstp = (Point) firstp.cloneGeometry();
151
            geo.addVertex(firstp);
152
        }
153
        // TODO Auto-generated method stub
154
        
155
    }
156

    
157
    public void closeGeometry() {
158
        
159
        int ng = list.size();
160
        if (ng > 0) {
161
            Curve firstcurve = list.get(0);
162
            Curve lastcurve = list.get(ng - 1);
163
            Point firstp = firstcurve.getVertex(0);
164
            firstp = (Point) firstp.cloneGeometry();
165
            lastcurve.addVertex(firstp);
166
        }
167
        close = true;
168
    }
169

    
170
    public void transition(double x, double y, InputEvent event) {
171
        _fsm.addPoint(x, y, event);
172
    }
173

    
174
    public void transition(double d) {
175
        _fsm.addValue(d);
176
    }
177

    
178
    public void transition(String s) throws CommandException {
179
        if (!super.changeCommand(s)) {
180
            _fsm.addOption(s);
181
        }
182
    }
183

    
184
    /**
185
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
186
     * editableFeatureSource que ya estar? creado.
187
     * 
188
     * @param sel
189
     *            Bitset con las geometr?as que est?n seleccionadas.
190
     * @param x
191
     *            par?metro x del punto que se pase en esta transici?n.
192
     * @param y
193
     *            par?metro y del punto que se pase en esta transici?n.
194
     */
195
    public void addPoint(double x, double y, InputEvent event) {
196
        PolylineCADToolState actualState =
197
            (PolylineCADToolState) _fsm.getPreviousState();
198
        String status = actualState.getName();
199
        if (status.equals("Polyline.NextPointOrArcOrClose")
200
            || status.equals("Polyline.FirstPoint")) {
201

    
202
            if (firstPoint == null) {
203
                firstPoint = new Point2D.Double(x, y);
204
            }
205
            Point2D point = new Point2D.Double(x, y);
206

    
207
            if (antPoint != null
208
                && (antPoint.getX() != point.getX() || antPoint.getY() != point
209
                    .getY())) {
210
                GeneralPathX elShape =
211
                    new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
212
                elShape.moveTo(antPoint.getX(), antPoint.getY());
213
                elShape.lineTo(point.getX(), point.getY());
214
                Curve geom = createCurve(elShape);
215
                if (geom != null) {
216
                    list.add(geom);
217
                    addTemporalCache(geom);
218
                }
219
            }
220
            if (antPoint == null)
221
                antPoint = (Point2D) firstPoint.clone();
222

    
223
            if (antPoint != null) {
224
                antantPoint = antPoint;
225
            }
226

    
227
            antPoint = point;
228
        } else
229
            if (status.equals("Polyline.NextPointOrLineOrClose")) {
230
                Point2D point = new Point2D.Double(x, y);
231
                Point2D lastp = antPoint; // (Point2D)points.get(i-1);
232

    
233
                if (antantPoint == null) {
234
                    antantPoint =
235
                        new Point2D.Double(lastp.getX() + (point.getX() / 2),
236
                            lastp.getY() + (point.getY() / 2));
237
                }
238

    
239
                if (((point.getY() == lastp.getY()) && (point.getX() < lastp
240
                    .getX()))
241
                    || ((point.getX() == lastp.getX()) && (point.getY() < lastp
242
                        .getY()))) {
243
                } else {
244
                    if (point.getX() == lastp.getX()) {
245
                        point =
246
                            new Point2D.Double(point.getX() + 0.00000001,
247
                                point.getY());
248
                    } else
249
                        if (point.getY() == lastp.getY()) {
250
                            point =
251
                                new Point2D.Double(point.getX(),
252
                                    point.getY() + 0.00000001);
253
                        }
254

    
255
                    if (point.getX() == antantPoint.getX()) {
256
                        point =
257
                            new Point2D.Double(point.getX() + 0.00000001,
258
                                point.getY());
259
                    } else
260
                        if (point.getY() == antantPoint.getY()) {
261
                            point =
262
                                new Point2D.Double(point.getX(),
263
                                    point.getY() + 0.00000001);
264
                        }
265

    
266
                    if (!(list.size() > 0)
267
                        || (((Geometry) list.get(list.size() - 1)).getType() == Geometry.TYPES.CURVE)) {
268
                        Point2D[] ps1 =
269
                            UtilFunctions.getPerpendicular(antantPoint, lastp,
270
                                lastp);
271
                        Point2D mediop =
272
                            new Point2D.Double(
273
                                (point.getX() + lastp.getX()) / 2,
274
                                (point.getY() + lastp.getY()) / 2);
275
                        Point2D[] ps2 =
276
                            UtilFunctions
277
                                .getPerpendicular(lastp, point, mediop);
278
                        Point2D interp =
279
                            UtilFunctions.getIntersection(ps1[0], ps1[1],
280
                                ps2[0], ps2[1]);
281
                        antInter = interp;
282

    
283
                        double radio = interp.distance(lastp);
284

    
285
                        if (UtilFunctions.isLowAngle(antantPoint, lastp,
286
                            interp, point)) {
287
                            radio = -radio;
288
                        }
289

    
290
                        Point2D centerp =
291
                            UtilFunctions.getPoint(interp, mediop, radio);
292
                        antCenter = centerp;
293

    
294
                        Curve ig = createArc(lastp, centerp, point);
295

    
296
                        if (ig != null) {
297
                            list.add(ig);
298
                            addTemporalCache(ig);
299
                        } else {
300
                            
301
                            ApplicationLocator.getManager().message(
302
                                Messages.getText("_Unable_to_create_arc"),
303
                                JOptionPane.ERROR_MESSAGE);
304

    
305
                        }
306
                    } else {
307
                        Point2D[] ps1 =
308
                            UtilFunctions.getPerpendicular(lastp, antInter,
309
                                lastp);
310
                        double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
311
                        double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
312
                        double angle = UtilFunctions.getAngle(antCenter, lastp);
313
                        Point2D ini1 = null;
314
                        Point2D ini2 = null;
315

    
316
                        if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions
317
                            .absoluteAngleDistance(angle, a2)) {
318
                            ini1 = ps1[0];
319
                            ini2 = ps1[1];
320
                        } else {
321
                            ini1 = ps1[1];
322
                            ini2 = ps1[0];
323
                        }
324

    
325
                        Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
326
                        Point2D correct =
327
                            new Point2D.Double(lastp.getX() + unit.getX(),
328
                                lastp.getY() + unit.getY());
329
                        Point2D[] ps =
330
                            UtilFunctions.getPerpendicular(lastp, correct,
331
                                lastp);
332
                        Point2D mediop =
333
                            new Point2D.Double(
334
                                (point.getX() + lastp.getX()) / 2,
335
                                (point.getY() + lastp.getY()) / 2);
336
                        Point2D[] ps2 =
337
                            UtilFunctions
338
                                .getPerpendicular(lastp, point, mediop);
339
                        Point2D interp =
340
                            UtilFunctions.getIntersection(ps[0], ps[1], ps2[0],
341
                                ps2[1]);
342
                        antInter = interp;
343

    
344
                        double radio = interp.distance(lastp);
345

    
346
                        if (UtilFunctions.isLowAngle(correct, lastp, interp,
347
                            point)) {
348
                            radio = -radio;
349
                        }
350

    
351
                        Point2D centerp =
352
                            UtilFunctions.getPoint(interp, mediop, radio);
353
                        antCenter = centerp;
354
                        Curve geom = createArc(lastp, centerp, point);
355
                        
356
                        if (geom != null) {
357
                            list.add(geom);
358
                            addTemporalCache(geom);
359
                        }
360
                    }
361

    
362
                    antantPoint = antPoint;
363
                    antPoint = point;
364
                }
365
            }
366
    }
367

    
368
    /**
369
     * M?todo para dibujar la lo necesario para el estado en el que nos
370
     * encontremos.
371
     * 
372
     * @param g
373
     *            Graphics sobre el que dibujar.
374
     * @param selectedGeometries
375
     *            BitSet con las geometr?as seleccionadas.
376
     * @param x
377
     *            par?metro x del punto que se pase para dibujar.
378
     * @param y
379
     *            par?metro x del punto que se pase para dibujar.
380
     */
381
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
382
        PolylineCADToolState actualState = _fsm.getState();
383
        String status = actualState.getName();
384

    
385
        if (status.equals("Polyline.NextPointOrArcOrClose")
386
            || status.equals("Polyline.FirstPoint")) {
387
            for (int i = 0; i < list.size(); i++) {
388
                renderer.draw((Geometry) list.get(i),
389
                    mapControlManager.getGeometrySelectionSymbol());
390
            }
391
            if (antPoint != null)
392
                renderer.drawLine(antPoint, new Point2D.Double(x, y),
393
                    mapControlManager.getGeometrySelectionSymbol());
394

    
395
        } else
396
            if ((status.equals("Polyline.NextPointOrLineOrClose"))) {
397
                for (int i = 0; i < list.size(); i++) {
398
                    renderer.draw((Geometry) list.get(i),
399
                        mapControlManager.getGeometrySelectionSymbol());
400
                }
401

    
402
                Point2D point = new Point2D.Double(x, y);
403
                Point2D lastp = antPoint;
404

    
405
                if (!(list.size() > 0)
406
                    || (((Geometry) list.get(list.size() - 1)).getType() == Geometry.TYPES.CURVE)) {
407
                    if (antantPoint == null) {
408
                        drawArc(point, lastp, new Point2D.Double(lastp.getX()
409
                            + (point.getX() / 2), lastp.getY()
410
                            + (point.getY() / 2)), renderer);
411
                    } else {
412
                        drawArc(point, lastp, antantPoint, renderer);
413
                    }
414
                } else {
415
                    if (antInter != null) {
416
                        Point2D[] ps1 =
417
                            UtilFunctions.getPerpendicular(lastp, antInter,
418
                                lastp);
419
                        double a1 = UtilFunctions.getAngle(ps1[0], ps1[1]);
420
                        double a2 = UtilFunctions.getAngle(ps1[1], ps1[0]);
421
                        double angle = UtilFunctions.getAngle(antCenter, lastp);
422
                        Point2D ini1 = null;
423
                        Point2D ini2 = null;
424

    
425
                        if (UtilFunctions.absoluteAngleDistance(angle, a1) > UtilFunctions
426
                            .absoluteAngleDistance(angle, a2)) {
427
                            ini1 = ps1[0];
428
                            ini2 = ps1[1];
429
                        } else {
430
                            ini1 = ps1[1];
431
                            ini2 = ps1[0];
432
                        }
433

    
434
                        Point2D unit = UtilFunctions.getUnitVector(ini1, ini2);
435
                        Point2D correct =
436
                            new Point2D.Double(lastp.getX() + unit.getX(),
437
                                lastp.getY() + unit.getY());
438
                        drawArc(point, lastp, correct, renderer);
439
                    }
440
                }
441
            }
442
        try {
443
            if (((FLyrVect) getVLE().getLayer()).getShapeType() == Geometry.TYPES.SURFACE
444
                && !list.isEmpty()) {
445
                Handler handler1 =
446
                    ((Geometry) list.get(0))
447
                        .getHandlers(Geometry.SELECTHANDLER)[0];
448
                GeneralPathX gpx = new GeneralPathX();
449
                gpx.moveTo(x, y);
450
                Point2D p1 = handler1.getPoint();
451
                gpx.lineTo(p1.getX(), p1.getY());
452
                Curve curve = createCurve(gpx);
453
                renderer.draw(curve,
454
                    mapControlManager.getGeometrySelectionSymbol());
455
            }
456
        } catch (ReadException e) {
457
            e.printStackTrace();
458
        }
459
    }
460

    
461
    /**
462
     * Dibuja el arco sobre el graphics.
463
     * 
464
     * @param point
465
     *            Puntero del rat?n.
466
     * @param lastp
467
     *            ?ltimo punto de la polilinea.
468
     * @param antp
469
     *            Punto antepenultimo.
470
     * @param g
471
     *            Graphics sobre el que se dibuja.
472
     */
473
    private void drawArc(Point2D point, Point2D lastp, Point2D antp,
474
        MapControlDrawer renderer) {
475
        if (((point.getY() == lastp.getY()) && (point.getX() < lastp.getX()))
476
            || ((point.getX() == lastp.getX()) && (point.getY() < lastp.getY()))) {
477
        } else {
478
            if (point.getX() == lastp.getX()) {
479
                point =
480
                    new Point2D.Double(point.getX() + 0.00000001, point.getY());
481
            } else
482
                if (point.getY() == lastp.getY()) {
483
                    point =
484
                        new Point2D.Double(point.getX(),
485
                            point.getY() + 0.00000001);
486
                }
487

    
488
            if (point.getX() == antp.getX()) {
489
                point =
490
                    new Point2D.Double(point.getX() + 0.00000001, point.getY());
491
            } else
492
                if (point.getY() == antp.getY()) {
493
                    point =
494
                        new Point2D.Double(point.getX(),
495
                            point.getY() + 0.00000001);
496
                }
497

    
498
            Point2D[] ps1 = UtilFunctions.getPerpendicular(lastp, antp, lastp);
499
            Point2D mediop =
500
                new Point2D.Double((point.getX() + lastp.getX()) / 2,
501
                    (point.getY() + lastp.getY()) / 2);
502
            Point2D[] ps2 =
503
                UtilFunctions.getPerpendicular(lastp, point, mediop);
504
            Point2D interp =
505
                UtilFunctions.getIntersection(ps1[0], ps1[1], ps2[0], ps2[1]);
506

    
507
            double radio = interp.distance(lastp);
508

    
509
            if (UtilFunctions.isLowAngle(antp, lastp, interp, point)) {
510
                radio = -radio;
511
            }
512

    
513
            Point2D centerp = UtilFunctions.getPoint(interp, mediop, radio);
514
            renderer.drawLine(lastp, point,
515
                mapControlManager.getGeometrySelectionSymbol());
516

    
517
            Geometry ig = createArc(lastp, centerp, point);
518

    
519
            if (ig != null) {
520
                renderer.draw(ig,
521
                    mapControlManager.getGeometrySelectionSymbol());
522
            }
523
        }
524
    }
525

    
526
    /**
527
     * Add a diferent option.
528
     * 
529
     * @param sel
530
     *            DOCUMENT ME!
531
     * @param s
532
     *            Diferent option.
533
     */
534
    public void addOption(String s) {
535
        // Nothing to do
536
    }
537

    
538
    public void addValue(double d) {
539
        // Nothing to do
540
    }
541

    
542
    public void cancel() {
543
        list.clear();
544
        clearTemporalCache();
545
        antantPoint = antCenter = antInter = antPoint = firstPoint = null;
546
    }
547

    
548
    public void end() {
549
        // Nothing to do
550
    }
551

    
552
    public String getName() {
553
        return PluginServices.getText(this, "polyline_");
554
    }
555

    
556
    public String toString() {
557
        return "_polyline";
558
    }
559

    
560
    public void endTransition(double x, double y, MouseEvent event) {
561
        _fsm.endPoint(x, y, event);
562
    }
563
    
564
    /**
565
     * @param geoms
566
     * @return
567
     * @throws CreateGeometryException 
568
     */
569
    private Geometry concatenateCurves(List<Curve> cus, boolean surf) {
570
        
571
        Curve _curv = null;
572
        Surface _surf = null;
573
        
574
        try {
575
            if (surf) {
576
                _surf = (Surface) geomManager.create(
577
                    Geometry.TYPES.SURFACE, Geometry.SUBTYPES.GEOM2D);
578
            } else {
579
                _curv = (Curve) geomManager.create(
580
                    Geometry.TYPES.CURVE, Geometry.SUBTYPES.GEOM2D);
581
            }
582
        } catch (CreateGeometryException e) {
583
            LOG.info("Unable to create geometry: " + e.getMessage());
584
            ApplicationLocator.getManager().message(
585
                Messages.getText("_Unable_to_create_geometry"),
586
                JOptionPane.ERROR_MESSAGE);
587
            return null;
588
        }
589
        
590
        
591
        if (cus == null || cus.size() == 0) {
592
            if (surf) {
593
                return _surf;
594
            } else {
595
                return _curv;
596
            }
597
        }
598
        
599
        Curve item_cu = cus.get(0);
600
        int len = item_cu.getNumVertices();
601
        Point po = null;
602
        /*
603
         * All from first curve
604
         */
605
        for (int i=0; i<len; i++) {
606
            po = item_cu.getVertex(i);
607
            if (surf) {
608
                _surf.addVertex(po);
609
            } else {
610
                _curv.addVertex(po);
611
            }
612
            
613
        }
614
        
615
        for (int n=1; n<cus.size(); n++) {
616
            
617
            item_cu = cus.get(n);
618
            len = item_cu.getNumVertices();
619
            for (int i=0; i<len; i++) {
620
                po = item_cu.getVertex(i);
621
                /*
622
                 * Add only if not same coords
623
                 */
624
                if (surf) {
625
                    if (differentCoordinates(po,
626
                        _surf.getVertex(
627
                        _surf.getNumVertices()-1))) {
628
                        _surf.addVertex(po);
629
                    }
630
                } else {
631
                    if (differentCoordinates(po,
632
                        _curv.getVertex(
633
                        _curv.getNumVertices()-1))) {
634
                        _curv.addVertex(po);
635
                    }
636
                }
637
            }
638
        }
639
        
640
        if (surf) {
641
            return _surf;
642
        } else {
643
            return _curv;
644
        }
645
        
646
    }
647

    
648
    /**
649
     * @param po
650
     * @param vertex
651
     * @return
652
     */
653
    private boolean differentCoordinates(Point p1, Point p2) {
654
        return !p1.equals(p2);
655
    }
656

    
657
}