Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / DefaultGeneralPathX.java @ 40412

History | View | Annotate | Download (30.5 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 org.gvsig.fmap.geom.primitive;
42

    
43
/*
44
 * Based on portions of code from java.awt.geom.GeneralPath of the
45
 * OpenJDK project (Copyright (c) 1996, 2006, Oracle and/or its affiliates)
46
 */
47
import java.awt.Shape;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.FlatteningPathIterator;
50
import java.awt.geom.IllegalPathStateException;
51
import java.awt.geom.PathIterator;
52
import java.awt.geom.Point2D;
53
import java.awt.geom.Rectangle2D;
54
import java.util.ArrayList;
55
import java.util.List;
56

    
57
import org.cresques.cts.ICoordTrans;
58
import org.gvsig.fmap.geom.Geometry;
59
import org.gvsig.fmap.geom.GeometryLocator;
60
import org.gvsig.fmap.geom.GeometryManager;
61
import org.gvsig.fmap.geom.exception.CreateGeometryException;
62
import org.gvsig.jdk.GeomUtilities;
63
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
65

    
66
import com.vividsolutions.jts.algorithm.CGAlgorithms;
67
import com.vividsolutions.jts.geom.Coordinate;
68
import com.vividsolutions.jts.geom.CoordinateList;
69
import com.vividsolutions.jts.geom.CoordinateSequences;
70
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
71

    
72
/**
73
 * The <code>GeneralPathX</code> class represents a geometric path
74
 * constructed from straight lines, and quadratic and cubic
75
 * (B&eacute;zier) curves. It can contain multiple subpaths.
76
 * <p>
77
 * The winding rule specifies how the interior of a path is determined. There
78
 * are two types of winding rules: EVEN_ODD and NON_ZERO.
79
 * <p>
80
 * An EVEN_ODD winding rule means that enclosed regions of the path alternate
81
 * between interior and exterior areas as traversed from the outside of the path
82
 * towards a point inside the region.
83
 * <p>
84
 * A NON_ZERO winding rule means that if a ray is drawn in any direction from a
85
 * given point to infinity and the places where the path intersects the ray are
86
 * examined, the point is inside of the path if and only if the number of times
87
 * that the path crosses the ray from left to right does not equal the number of
88
 * times that the path crosses the ray from right to left.
89
 * 
90
 * @version 1.58, 01/23/03
91
 * @author Jim Graham
92
 * @deprecated
93
 *             use the geometry methods
94
 */
95
public class DefaultGeneralPathX extends GeneralPathX { 
96

    
97
    /**
98
     * Default serial version ID
99
     */
100
    private static final long serialVersionUID = 1L;
101

    
102
    private static final Logger LOG = LoggerFactory
103
        .getLogger(DefaultGeneralPathX.class);
104

    
105
    protected static GeometryManager geomManager = GeometryLocator
106
        .getGeometryManager();
107

    
108
    private List pointTypes = new ArrayList();
109
    private List pointCoords = new ArrayList();
110

    
111
    int windingRule;
112

    
113
    private boolean isSimple = true;
114

    
115
    static final int EXPAND_MAX = 500;
116

    
117
    private  DefaultGeneralPathX() {
118
            super(true);
119
        setWindingRule(WIND_EVEN_ODD);
120
    }
121
    /**
122
     * Constructs a new <code>GeneralPathX</code> object with the specified
123
     * winding rule to control operations that require the interior of the
124
     * path to be defined.
125
     * 
126
     * @param rule
127
     *            the winding rule
128
     * @see #WIND_EVEN_ODD
129
     * @see #WIND_NON_ZERO
130
     */
131
    public DefaultGeneralPathX(int rule) {
132
            super(true);
133
        setWindingRule(rule);
134
    }
135

    
136
    /**
137
     * Constructs a new <code>GeneralPathX</code> object from an arbitrary
138
     * {@link Shape} object.
139
     * All of the initial geometry and the winding rule for this path are
140
     * taken from the specified <code>Shape</code> object.
141
     * 
142
     * @param s
143
     *            the specified <code>Shape</code> object
144
     */
145
    public DefaultGeneralPathX(PathIterator piter) {
146
        this(WIND_EVEN_ODD);
147
        setWindingRule(piter.getWindingRule());
148
        append(piter, false);
149
    }
150

    
151
    private void needRoom(int newTypes, int newCoords, boolean needMove) {
152
        if (needMove && getNumTypes() == 0) {
153
            throw new IllegalPathStateException("missing initial moveto "
154
                + "in path definition");
155
        }
156
    }
157

    
158
    /**
159
     * Adds a point to the path by moving to the specified
160
     * coordinates.
161
     * 
162
     * @param x
163
     *            ,&nbsp;y the specified coordinates
164
     * @deprecated
165
     *             use moveTo(Point)
166
     */
167
    public synchronized void moveTo(double x, double y) {
168
        int numtypes = getNumTypes();
169
        if (numtypes > 0 && getTypeAt(numtypes - 1) == SEG_MOVETO) {
170
            Point point = getPointAt(getNumCoords() - 1);
171
            point.setX(x);
172
            point.setY(y);
173
        } else {
174
            needRoom(1, 2, false);
175
            pointTypes.add(new Byte(SEG_MOVETO));
176
            addPoint(x, y);
177
        }
178
    }
179

    
180
    public synchronized void moveTo(Point point) {
181
        int numtypes = getNumTypes();
182
        if (numtypes > 0 && getTypeAt(numtypes - 1) == SEG_MOVETO) {
183
            pointCoords.remove(getNumCoords() - 1);
184
            addPoint(point);
185
        } else {
186
            needRoom(1, 2, false);
187
            pointTypes.add(new Byte(SEG_MOVETO));
188
            addPoint(point);
189
        }
190
    }
191

    
192
    /**
193
     * Adds a point to the path by drawing a straight line from the
194
     * current coordinates to the new specified coordinates.
195
     * 
196
     * @param x
197
     *            ,&nbsp;y the specified coordinates
198
     * @deprecated
199
     *             use lineTo(Point)
200
     */
201
    public synchronized void lineTo(double x, double y) {
202
        needRoom(1, 2, true);
203
        pointTypes.add(new Byte(SEG_LINETO));
204
        addPoint(x, y);
205
    }
206

    
207
    public synchronized void lineTo(Point point) {
208
        needRoom(1, 2, true);
209
        pointTypes.add(new Byte(SEG_LINETO));
210
        addPoint(point);
211
    }
212

    
213
    public synchronized void addSegment(Point[] segment) {
214
        if (segment != null && segment.length > 0) {
215
            needRoom(segment.length, 2 * segment.length, true);
216
            for (int i = 0; i < segment.length; i++) {
217
                pointTypes.add(new Byte(SEG_LINETO));
218
                addPoint(segment[i]);
219
            }
220
        }
221
    }
222

    
223
    private void addPoint(double x, double y) {
224
        try {
225
            pointCoords.add(geomManager.createPoint(x, y,
226
                Geometry.SUBTYPES.GEOM2D));
227
        } catch (CreateGeometryException e) {
228
            LOG.error("Error creating a point", e);
229
        }
230
    }
231

    
232
    private void addPoint(Point point) {
233
        pointCoords.add(point);
234
    }
235

    
236
    /**
237
     * Adds a curved segment, defined by two new points, to the path by
238
     * drawing a Quadratic curve that intersects both the current
239
     * coordinates and the coordinates (x2,&nbsp;y2), using the
240
     * specified point (x1,&nbsp;y1) as a quadratic parametric control
241
     * point.
242
     * 
243
     * @param x1
244
     *            ,&nbsp;y1 the coordinates of the first quadratic control
245
     *            point
246
     * @param x2
247
     *            ,&nbsp;y2 the coordinates of the final endpoint
248
     * @deprecated
249
     *             use quadTo(Point, Point)
250
     */
251
    public synchronized void quadTo(double x1, double y1, double x2, double y2) {
252
        needRoom(1, 4, true);
253
        pointTypes.add(new Byte(SEG_QUADTO));
254
        addPoint(x1, y1);
255
        addPoint(x2, y2);
256
        isSimple = false;
257
    }
258

    
259
    public synchronized void quadTo(Point point1, Point point2) {
260
        needRoom(1, 4, true);
261
        pointTypes.add(new Byte(SEG_QUADTO));
262
        addPoint(point1);
263
        addPoint(point2);
264
        isSimple = false;
265
    }
266

    
267
    /**
268
     * Adds a curved segment, defined by three new points, to the path by
269
     * drawing a B&eacute;zier curve that intersects both the current
270
     * coordinates and the coordinates (x3,&nbsp;y3), using the
271
     * specified points (x1,&nbsp;y1) and (x2,&nbsp;y2) as
272
     * B&eacute;zier control points.
273
     * 
274
     * @param x1
275
     *            ,&nbsp;y1 the coordinates of the first B&eacute;ezier
276
     *            control point
277
     * @param x2
278
     *            ,&nbsp;y2 the coordinates of the second B&eacute;zier
279
     *            control point
280
     * @param x3
281
     *            ,&nbsp;y3 the coordinates of the final endpoint
282
     * @deprecated
283
     *             use curveTo(Point, Point, Point)
284
     */
285
    public synchronized void curveTo(double x1, double y1, double x2,
286
        double y2, double x3, double y3) {
287
        needRoom(1, 6, true);
288
        pointTypes.add(new Byte(SEG_CUBICTO));
289
        addPoint(x1, y1);
290
        addPoint(x2, y2);
291
        addPoint(x3, y3);
292
        isSimple = false;
293
    }
294

    
295
    public synchronized void curveTo(Point point1, Point point2, Point point3) {
296
        needRoom(1, 6, true);
297
        pointTypes.add(new Byte(SEG_CUBICTO));
298
        addPoint(point1);
299
        addPoint(point2);
300
        addPoint(point3);
301
        isSimple = false;
302
    }
303

    
304
    /**
305
     * Closes the current subpath by drawing a straight line back to
306
     * the coordinates of the last <code>moveTo</code>. If the path is already
307
     * closed then this method has no effect.
308
     */
309
    public synchronized void closePath() {
310
        if (getNumTypes() == 0 || getTypeAt(getNumTypes() - 1) != SEG_CLOSE) {
311
            needRoom(1, 0, true);
312
            // Adding a geometry like the last geometry
313
            // addPoint(100, 100);
314
            pointTypes.add(new Byte(SEG_CLOSE));
315
        }
316
    }
317

    
318
    /**
319
     * Check if the first part is closed.
320
     * 
321
     * @return
322
     */
323
    public boolean isClosed() {
324
        PathIterator theIterator =
325
            getPathIterator(null, geomManager.getFlatness());
326
        double[] theData = new double[6];
327
        double xFinal = 0;
328
        double yFinal = 0;
329
        double xIni = 0;
330
        double yIni = 0;
331
        boolean first = true;
332

    
333
        while (!theIterator.isDone()) {
334
            // while not done
335
            int theType = theIterator.currentSegment(theData);
336

    
337
            switch (theType) {
338
            case PathIterator.SEG_MOVETO:
339
                xIni = theData[0];
340
                yIni = theData[1];
341
                if (!first) {
342
                    break;
343
                }
344
                first = false;
345
                break;
346

    
347
            case PathIterator.SEG_LINETO:
348
                xFinal = theData[0];
349
                yFinal = theData[1];
350
                break;
351
            case PathIterator.SEG_CLOSE:
352
                return true;
353

    
354
            } // end switch
355

    
356
            theIterator.next();
357
        }
358
        if ((xFinal == xIni) && (yFinal == yIni))
359
            return true;
360
        return false;
361
    }
362

    
363
    /**
364
     * Appends the geometry of the specified {@link PathIterator} object
365
     * to the path, possibly connecting the new geometry to the existing
366
     * path segments with a line segment.
367
     * If the <code>connect</code> parameter is <code>true</code> and the
368
     * path is not empty then any initial <code>moveTo</code> in the
369
     * geometry of the appended <code>Shape</code> is turned into a
370
     * <code>lineTo</code> segment.
371
     * If the destination coordinates of such a connecting <code>lineTo</code>
372
     * segment match the ending coordinates of a currently open
373
     * subpath then the segment is omitted as superfluous.
374
     * The winding rule of the specified <code>Shape</code> is ignored
375
     * and the appended geometry is governed by the winding
376
     * rule specified for this path.
377
     * 
378
     * @param pi
379
     *            the <code>PathIterator</code> whose geometry is appended to
380
     *            this path
381
     * @param connect
382
     *            a boolean to control whether or not to turn an
383
     *            initial <code>moveTo</code> segment into a <code>lineTo</code>
384
     *            segment
385
     *            to connect the new geometry to the existing path
386
     */
387
    public void append(PathIterator pi, boolean connect) {
388
        double coords[] = new double[6];
389
        while (!pi.isDone()) {
390
            switch (pi.currentSegment(coords)) {
391
            case SEG_MOVETO:
392
                if (!connect || getNumTypes() < 1 || getNumCoords() < 2) {
393
                    moveTo(coords[0], coords[1]);
394
                    break;
395
                }
396
                if (getTypeAt(getNumTypes() - 1) != SEG_CLOSE
397
                    && getPointAt(getNumCoords() - 1).getX() == coords[0]
398
                    && getPointAt(getNumCoords() - 1).getY() == coords[1]) {
399
                    // Collapse out initial moveto/lineto
400
                    break;
401
                }
402
                // NO BREAK;
403
            case SEG_LINETO:
404
                lineTo(coords[0], coords[1]);
405
                break;
406
            case SEG_QUADTO:
407
                quadTo(coords[0], coords[1], coords[2], coords[3]);
408
                break;
409
            case SEG_CUBICTO:
410
                curveTo(coords[0], coords[1], coords[2], coords[3], coords[4],
411
                    coords[5]);
412
                break;
413
            case SEG_CLOSE:
414
                closePath();
415
                break;
416
            }
417
            pi.next();
418
            connect = false;
419
        }
420
    }
421

    
422
    /**
423
     * Returns the fill style winding rule.
424
     * 
425
     * @return an integer representing the current winding rule.
426
     * @see #WIND_EVEN_ODD
427
     * @see #WIND_NON_ZERO
428
     * @see #setWindingRule
429
     */
430
    public synchronized int getWindingRule() {
431
        return windingRule;
432
    }
433

    
434
    /**
435
     * Sets the winding rule for this path to the specified value.
436
     * 
437
     * @param rule
438
     *            an integer representing the specified
439
     *            winding rule
440
     * @exception <code>IllegalArgumentException</code> if <code>rule</code> is
441
     *            not either <code>WIND_EVEN_ODD</code> or
442
     *            <code>WIND_NON_ZERO</code>
443
     * @see #WIND_EVEN_ODD
444
     * @see #WIND_NON_ZERO
445
     * @see #getWindingRule
446
     */
447
    public void setWindingRule(int rule) {
448
        if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO) {
449
            throw new IllegalArgumentException("winding rule must be "
450
                + "WIND_EVEN_ODD or " + "WIND_NON_ZERO");
451
        }
452
        windingRule = rule;
453
    }
454

    
455
    /**
456
     * Returns the coordinates most recently added to the end of the path
457
     * as a {@link Point2D} object.
458
     * 
459
     * @return a <code>Point2D</code> object containing the ending
460
     *         coordinates of the path or <code>null</code> if there are no
461
     *         points
462
     *         in the path.
463
     */
464
    public synchronized Point2D getCurrentPoint() {
465
        if (getNumTypes() < 1 || getNumCoords() < 1) {
466
            return null;
467
        }
468
        int index = getNumCoords();
469
        if (getTypeAt(getNumTypes() - 1) == SEG_CLOSE) {
470
            loop: for (int i = getNumTypes() - 2; i > 0; i--) {
471
                switch (getTypeAt(i)) {
472
                case SEG_MOVETO:
473
                    break loop;
474
                case SEG_LINETO:
475
                    index -= 2;
476
                    break;
477
                case SEG_QUADTO:
478
                    index -= 4;
479
                    break;
480
                case SEG_CUBICTO:
481
                    index -= 6;
482
                    break;
483
                case SEG_CLOSE:
484
                    break;
485
                }
486
            }
487
        }
488
        return new Point2D.Double(getPointAt(index - 1).getX(), getPointAt(
489
            index - 1).getY());
490
    }
491

    
492
    /**
493
     * Resets the path to empty. The append position is set back to the
494
     * beginning of the path and all coordinates and point types are
495
     * forgotten.
496
     */
497
    public synchronized void reset() {
498
        pointCoords.clear();
499
        pointTypes.clear();
500
    }
501

    
502
    /**
503
     * Transforms the geometry of this path using the specified
504
     * {@link AffineTransform}.
505
     * The geometry is transformed in place, which permanently changes the
506
     * boundary defined by this object.
507
     * 
508
     * @param at
509
     *            the <code>AffineTransform</code> used to transform the area
510
     */
511
    public void transform(AffineTransform at) {
512
        for (int i = 0; i < getNumCoords(); i++) {
513
            getPointAt(i).transform(at);
514
        }
515
    }
516

    
517
    public void reProject(ICoordTrans ct) {
518
        for (int i = 0; i < getNumCoords(); i++) {
519
            getPointAt(i).reProject(ct);
520
        }
521
    }
522

    
523
    /**
524
     * Returns a new transformed <code>Shape</code>.
525
     * 
526
     * @param at
527
     *            the <code>AffineTransform</code> used to transform a
528
     *            new <code>Shape</code>.
529
     * @return a new <code>Shape</code>, transformed with the specified
530
     *         <code>AffineTransform</code>.
531
     */
532
    public synchronized Shape createTransformedShape(AffineTransform at) {
533
        DefaultGeneralPathX gp = (DefaultGeneralPathX) clone();
534
        if (at != null) {
535
            gp.transform(at);
536
        }
537
        return gp;
538
    }
539

    
540
    /**
541
     * Return the bounding box of the path.
542
     * 
543
     * @return a {@link java.awt.Rectangle} object that
544
     *         bounds the current path.
545
     */
546
    public java.awt.Rectangle getBounds() {
547
        return getBounds2D().getBounds();
548
    }
549

    
550
    /**
551
     * Returns the bounding box of the path.
552
     * 
553
     * @return a {@link Rectangle2D} object that
554
     *         bounds the current path.
555
     */
556
    public synchronized Rectangle2D getBounds2D() {
557
        double x1, y1, x2, y2;
558
        int i = getNumCoords();
559
        if (i > 0) {
560
            y1 = y2 = getPointAt(--i).getY();
561
            x1 = x2 = getPointAt(i).getX();
562
            while (i > 0) {
563
                double y = getPointAt(--i).getY();
564
                double x = getPointAt(i).getX();
565
                if (x < x1)
566
                    x1 = x;
567
                if (y < y1)
568
                    y1 = y;
569
                if (x > x2)
570
                    x2 = x;
571
                if (y > y2)
572
                    y2 = y;
573
            }
574
        } else {
575
            x1 = y1 = x2 = y2 = 0.0f;
576
        }
577
        return new Rectangle2D.Double(x1, y1, x2 - x1, y2 - y1);
578
    }
579

    
580
    /**
581
     * Tests if the specified coordinates are inside the boundary of
582
     * this <code>Shape</code>.
583
     * 
584
     * @param x
585
     *            ,&nbsp;y the specified coordinates
586
     * @return <code>true</code> if the specified coordinates are inside this
587
     *         <code>Shape</code>; <code>false</code> otherwise
588
     */
589
    public boolean contains(double x, double y) {
590
        if (pointTypes.size() < 2) {
591
            return false;
592
        }
593
        int cross =
594
            GeomUtilities.pointCrossingsForPath(getPathIterator(null), x, y);
595
        if (windingRule == WIND_NON_ZERO) {
596
            return (cross != 0);
597
        } else {
598
            return ((cross & 1) != 0);
599
        }
600
    }
601

    
602
    /**
603
     * Tests if the specified <code>Point2D</code> is inside the boundary
604
     * of this <code>Shape</code>.
605
     * 
606
     * @param p
607
     *            the specified <code>Point2D</code>
608
     * @return <code>true</code> if this <code>Shape</code> contains the
609
     *         specified <code>Point2D</code>, <code>false</code> otherwise.
610
     */
611
    public boolean contains(Point2D p) {
612
        return contains(p.getX(), p.getY());
613
    }
614

    
615
    /**
616
     * Tests if the specified rectangular area is inside the boundary of
617
     * this <code>Shape</code>.
618
     * 
619
     * @param x
620
     *            ,&nbsp;y the specified coordinates
621
     * @param w
622
     *            the width of the specified rectangular area
623
     * @param h
624
     *            the height of the specified rectangular area
625
     * @return <code>true</code> if this <code>Shape</code> contains
626
     *         the specified rectangluar area; <code>false</code> otherwise.
627
     */
628
    public boolean contains(double x, double y, double w, double h) {
629
        return GeomUtilities
630
            .contains(getPathIterator(null), x, y, x + w, y + h);
631
    }
632

    
633
    /**
634
     * Tests if the specified <code>Rectangle2D</code> is inside the boundary of
635
     * this <code>Shape</code>.
636
     * 
637
     * @param r
638
     *            a specified <code>Rectangle2D</code>
639
     * @return <code>true</code> if this <code>Shape</code> bounds the
640
     *         specified <code>Rectangle2D</code>; <code>false</code> otherwise.
641
     */
642
    public boolean contains(Rectangle2D r) {
643
        return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
644
    }
645

    
646
    /**
647
     * Tests if the interior of this <code>Shape</code> intersects the
648
     * interior of a specified set of rectangular coordinates.
649
     * 
650
     * @param x
651
     *            ,&nbsp;y the specified coordinates
652
     * @param w
653
     *            the width of the specified rectangular coordinates
654
     * @param h
655
     *            the height of the specified rectangular coordinates
656
     * @return <code>true</code> if this <code>Shape</code> and the
657
     *         interior of the specified set of rectangular coordinates
658
     *         intersect
659
     *         each other; <code>false</code> otherwise.
660
     */
661
    public boolean intersects(double x, double y, double w, double h) {
662
        return GeomUtilities.intersects(getPathIterator(null), x, y, w, h);
663
    }
664

    
665
    /**
666
     * Tests if the interior of this <code>Shape</code> intersects the
667
     * interior of a specified <code>Rectangle2D</code>.
668
     * 
669
     * @param r
670
     *            the specified <code>Rectangle2D</code>
671
     * @return <code>true</code> if this <code>Shape</code> and the interior
672
     *         of the specified <code>Rectangle2D</code> intersect each
673
     *         other; <code>false</code> otherwise.
674
     */
675
    public boolean intersects(Rectangle2D r) {
676
        return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
677
    }
678

    
679
    /**
680
     * Returns a <code>PathIterator</code> object that iterates along the
681
     * boundary of this <code>Shape</code> and provides access to the
682
     * geometry of the outline of this <code>Shape</code>.
683
     * The iterator for this class is not multi-threaded safe,
684
     * which means that this <code>GeneralPathX</code> class does not
685
     * guarantee that modifications to the geometry of this
686
     * <code>GeneralPathX</code> object do not affect any iterations of
687
     * that geometry that are already in process.
688
     * 
689
     * @param at
690
     *            an <code>AffineTransform</code>
691
     * @return a new <code>PathIterator</code> that iterates along the
692
     *         boundary of this <code>Shape</code> and provides access to the
693
     *         geometry of this <code>Shape</code>'s outline
694
     */
695
    public PathIterator getPathIterator(AffineTransform at) {
696
        if (isSimple) {
697
            return new GeneralPathXIteratorSimple(this, at);
698
        } else {
699
            return new GeneralPathXIterator(this, at);
700
        }
701
    }
702

    
703
    /**
704
     * Returns a <code>PathIterator</code> object that iterates along the
705
     * boundary of the flattened <code>Shape</code> and provides access to the
706
     * geometry of the outline of the <code>Shape</code>.
707
     * The iterator for this class is not multi-threaded safe,
708
     * which means that this <code>GeneralPathX</code> class does not
709
     * guarantee that modifications to the geometry of this
710
     * <code>GeneralPathX</code> object do not affect any iterations of
711
     * that geometry that are already in process.
712
     * 
713
     * @param at
714
     *            an <code>AffineTransform</code>
715
     * @param flatness
716
     *            the maximum distance that the line segments used to
717
     *            approximate the curved segments are allowed to deviate
718
     *            from any point on the original curve
719
     * @return a new <code>PathIterator</code> that iterates along the flattened
720
     *         <code>Shape</code> boundary.
721
     */
722
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
723
        return new FlatteningPathIterator(getPathIterator(at), flatness);
724
    }
725

    
726
    /**
727
     * Creates a new object of the same class as this object.
728
     * 
729
     * @return a clone of this instance.
730
     * @exception OutOfMemoryError
731
     *                if there is not enough memory.
732
     * @see java.lang.Cloneable
733
     * @since 1.2
734
     */
735
    public Object clone() {
736
        DefaultGeneralPathX copy = new DefaultGeneralPathX();
737
        copy.windingRule = windingRule;
738
        copy.isSimple = isSimple;
739
        for (int i = 0; i < getNumTypes(); i++) {
740
            copy.pointTypes.add(pointTypes.get(i));
741
        }
742
        for (int i = 0; i < getNumCoords(); i++) {
743
            copy.addPoint((Point) getPointAt(i).cloneGeometry());
744
        }
745
        return copy;
746

    
747
    }
748

    
749
    DefaultGeneralPathX(int windingRule, byte[] pointTypes, int numTypes,
750
        double[] pointCoords, int numCoords) {
751

    
752
        // used to construct from native
753

    
754
        this.windingRule = windingRule;
755
        this.setPointTypes(pointTypes);
756
        this.setNumTypes(numTypes);
757
        this.setPointCoords(pointCoords);
758
        this.setNumCoords(numCoords);
759
    }
760

    
761
    public void setNumTypes(int numTypes) {
762

    
763
    }
764

    
765
    public int getNumTypes() {
766
        return pointTypes.size();
767
    }
768

    
769
    public int setNumCoords(int numCoords) {
770
        return pointCoords.size();
771
    }
772

    
773
    public int getNumCoords() {
774
        return pointCoords.size();
775
    }
776

    
777
    public byte getTypeAt(int index) {
778
        return ((Byte) pointTypes.get(index)).byteValue();
779
    }
780

    
781
    /**
782
     * @deprecated
783
     *             use the geometry methods.
784
     */
785
    public void setPointTypes(byte[] pointTypes) {
786
        this.pointTypes.clear();
787
        for (int i = 0; i < pointTypes.length; i++) {
788
            this.pointTypes.add(new Byte(pointTypes[i]));
789
        }
790
    }
791

    
792
    /**
793
     * @deprecated
794
     *             use the geometry methods.
795
     */
796
    public byte[] getPointTypes() {
797
        byte[] bytes = new byte[pointTypes.size()];
798
        for (int i = 0; i < pointTypes.size(); i++) {
799
            bytes[i] = ((Byte) pointTypes.get(i)).byteValue();
800
        }
801
        return bytes;
802
    }
803

    
804
    /**
805
     * @param pointCoords
806
     * @deprecated
807
     *             use the geometry methods.
808
     */
809
    public void setPointCoords(double[] pointCoords) {
810
        this.pointCoords.clear();
811
        for (int i = 0; i < pointCoords.length; i = i + 2) {
812
            try {
813
                addPoint(geomManager.createPoint(pointCoords[i],
814
                    pointCoords[i + 1], Geometry.SUBTYPES.GEOM2D));
815
            } catch (CreateGeometryException e) {
816
                LOG.error("Error creating a point", e);
817
            }
818
        }
819
    }
820

    
821
    /**
822
     * @deprecated
823
     *             use the geometry methods.
824
     */
825
    public double[] getPointCoords() {
826
        double[] doubles = new double[pointCoords.size() * 2];
827
        for (int i = 0; i < getNumCoords(); i++) {
828
            doubles[i * 2] = getPointAt(i).getX();
829
            doubles[(i * 2) + 1] = getPointAt(i).getY();
830
        }
831
        return doubles;
832
    }
833

    
834
    public Point getPointAt(int index) {
835
        return (Point) pointCoords.get(index);
836
    }
837

    
838
    public double[] getCoordinatesAt(int index) {
839
        return getPointAt(index).getCoordinates();
840
    }
841

    
842
    /**
843
     * Convertimos el path a puntos y luego le damos la vuelta.
844
     */
845
    public void flip() {
846
        PathIterator theIterator =
847
            getPathIterator(null, geomManager.getFlatness());
848
        double[] theData = new double[6];
849
        CoordinateList coordList = new CoordinateList();
850
        Coordinate c1;
851
        DefaultGeneralPathX newGp = new DefaultGeneralPathX();
852
        ArrayList listOfParts = new ArrayList();
853
        while (!theIterator.isDone()) {
854
            // while not done
855
            int type = theIterator.currentSegment(theData);
856
            switch (type) {
857
            case SEG_MOVETO:
858
                coordList = new CoordinateList();
859
                listOfParts.add(coordList);
860
                c1 = new Coordinate(theData[0], theData[1]);
861
                coordList.add(c1, true);
862
                break;
863
            case SEG_LINETO:
864
                c1 = new Coordinate(theData[0], theData[1]);
865
                coordList.add(c1, true);
866
                break;
867

    
868
            case SEG_CLOSE:
869
                coordList.add(coordList.getCoordinate(0));
870
                break;
871

    
872
            }
873
            theIterator.next();
874
        }
875

    
876
        for (int i = listOfParts.size() - 1; i >= 0; i--) {
877
            coordList = (CoordinateList) listOfParts.get(i);
878
            Coordinate[] coords = coordList.toCoordinateArray();
879
            CoordinateArraySequence seq = new CoordinateArraySequence(coords);
880
            CoordinateSequences.reverse(seq);
881
            coords = seq.toCoordinateArray();
882
            newGp.moveTo(coords[0].x, coords[0].y);
883
            for (int j = 1; j < coords.length; j++) {
884
                newGp.lineTo(coords[j].x, coords[j].y);
885
            }
886
        }
887
        reset();
888
        append(newGp.getPathIterator(null), false);
889
    }
890

    
891
    /**
892
     * Check if the first part is CCW.
893
     * 
894
     * @return
895
     */
896
    public boolean isCCW() {
897
        PathIterator theIterator =
898
            getPathIterator(null, geomManager.getFlatness()); // polyLine.getPathIterator(null,
899
                                                              // flatness);
900
        double[] theData = new double[6];
901
        Coordinate first = null;
902
        CoordinateList coordList = new CoordinateList();
903
        Coordinate c1;
904
        boolean bFirst = true;
905
        while (!theIterator.isDone()) {
906
            // while not done
907
            int type = theIterator.currentSegment(theData);
908
            switch (type) {
909
            case SEG_MOVETO:
910
                c1 = new Coordinate(theData[0], theData[1]);
911
                if (bFirst == false) // Ya tenemos la primera parte.
912
                    break;
913
                if (bFirst) {
914
                    bFirst = false;
915
                    first = c1;
916
                }
917
                coordList.add(c1, true);
918
                break;
919
            case SEG_LINETO:
920
                c1 = new Coordinate(theData[0], theData[1]);
921
                coordList.add(c1, true);
922
                break;
923

    
924
            }
925
            theIterator.next();
926
        }
927
        coordList.add(first, true);
928
        return CGAlgorithms.isCCW(coordList.toCoordinateArray());
929
    }
930

    
931
    /**
932
     * @return the isSimple
933
     */
934
    public boolean isSimple() {
935
        return isSimple;
936
    }
937
}