Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / primitive / curve / spline / AbstractSpline.java @ 42441

History | View | Annotate | Download (16.9 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.geom.jts.primitive.curve.spline;
24

    
25
import java.awt.Shape;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.PathIterator;
28
import java.util.Collections;
29
import java.util.Iterator;
30

    
31
import com.vividsolutions.jts.geom.Coordinate;
32

    
33
import org.apache.commons.lang3.StringUtils;
34
import org.cresques.cts.ICoordTrans;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.fmap.geom.exception.ReprojectionRuntimeException;
39
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
40
import org.gvsig.fmap.geom.jts.gputils.GeneralPathXIterator;
41
import org.gvsig.fmap.geom.jts.primitive.curve.AbstractCurve;
42
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
43
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
44
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
45
import org.gvsig.fmap.geom.jts.util.JTSUtils;
46
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
47
import org.gvsig.fmap.geom.operation.GeometryOperationException;
48
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
49
import org.gvsig.fmap.geom.primitive.GeneralPathX;
50
import org.gvsig.fmap.geom.primitive.IGeneralPathX;
51
import org.gvsig.fmap.geom.primitive.Point;
52

    
53

    
54
/**
55
 * @author fdiaz
56
 *
57
 */
58
public abstract class AbstractSpline extends AbstractCurve {
59

    
60
    /**
61
     *
62
     */
63
    private static final long serialVersionUID = -1562503359430991082L;
64

    
65
    private static final Logger logger = LoggerFactory.getLogger(AbstractSpline.class);
66

    
67
    protected ArrayListCoordinateSequence coordinates;
68
    protected PointJTS anyVertex;
69
    protected static final double SUBSEGMENTS = 30.0;
70

    
71
    /**
72
     * @param type
73
     * @param subtype
74
     */
75
    public AbstractSpline(int type, int subtype) {
76
        super(type, subtype);
77
    }
78

    
79
    /**
80
    *
81
    */
82
   public AbstractSpline(int type, int subtype, Coordinate[] coordinates, PointJTS aVertex) {
83
       this(type, subtype);
84
       this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
85
       anyVertex = aVertex;
86
   }
87

    
88
    /*
89
     * (non-Javadoc)
90
     *
91
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
92
     */
93
    public com.vividsolutions.jts.geom.Geometry getJTS() {
94
        return JTSUtils.createJTSLineString(getSplineCoordinates());
95
    }
96

    
97
    protected abstract ArrayListCoordinateSequence getSplineCoordinates();
98

    
99

    
100
    static class Spline {
101
        private double y[];
102
        private double y2[];
103

    
104
        /**
105
         * The constructor calculates the second derivatives of the interpolating function
106
         * at the tabulated points xi, with xi = (i, y[i]).
107
         * Based on numerical recipes in C, http://www.library.cornell.edu/nr/bookcpdf/c3-3.pdf .
108
         * @param y Array of y coordinates for cubic-spline interpolation.
109
         */
110
        public Spline(double y[]) {
111
            this.y = y;
112
            int n = y.length;
113
            y2 = new double[n];
114
            double u[] = new double[n];
115
            for (int i = 1; i < n - 1; i++) {
116
                y2[i] = -1.0 / (4.0 + y2[i - 1]);
117
                u[i] = (6.0 * (y[i + 1] - 2.0 * y[i] + y[i - 1]) - u[i - 1]) / (4.0 + y2[i - 1]);
118
            }
119
            for (int i = n - 2; i >= 0; i--) {
120
                y2[i] = y2[i] * y2[i + 1] + u[i];
121
            }
122
        }
123

    
124
        /**
125
         * Returns a cubic-spline interpolated value y for the point between
126
         * point (n, y[n]) and (n+1, y[n+1), with t ranging from 0 for (n, y[n])
127
         * to 1 for (n+1, y[n+1]).
128
         * @param n The start point.
129
         * @param t The distance to the next point (0..1).
130
         * @return A cubic-spline interpolated value.
131
         */
132
        public double fn(int n, double t) {
133
            return t * y[n + 1] - ((t - 1.0) * t * ((t - 2.0) * y2[n] - (t + 1.0) * y2[n + 1])) / 6.0 + y[n] - t * y[n];
134
        }
135

    
136
    }
137

    
138
    /*
139
     * (non-Javadoc)
140
     *
141
     * @see
142
     * org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(org.gvsig
143
     * .fmap.geom.primitive.Point)
144
     */
145
    public void addVertex(Point point) {
146
        point = fixPoint(point);
147
        coordinates.add(((PointJTS) point).getJTSCoordinate());
148
        anyVertex = (PointJTS) point;
149
    }
150

    
151
    /*
152
     * (non-Javadoc)
153
     *
154
     * @see
155
     * org.gvsig.fmap.geom.primitive.Curve#setPoints(org.gvsig.fmap.geom.primitive
156
     * .Point, org.gvsig.fmap.geom.primitive.Point)
157
     */
158
    public void setPoints(Point initialPoint, Point endPoint) {
159
        initialPoint = fixPoint(initialPoint);
160
        endPoint = fixPoint(endPoint);
161
        coordinates.clear();
162
        addVertex(initialPoint);
163
        addVertex(endPoint);
164
        anyVertex = (PointJTS) endPoint;
165
    }
166

    
167
    /**
168
     * @param initialPoint
169
     * @return
170
     */
171
    protected abstract Point fixPoint(Point point);
172

    
173
    /*
174
     * (non-Javadoc)
175
     *
176
     * @see
177
     * org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int,
178
     * int)
179
     */
180
    public double getCoordinateAt(int index, int dimension) {
181
        return coordinates.getOrdinate(index, dimension);
182
    }
183

    
184
    /*
185
     * (non-Javadoc)
186
     *
187
     * @see
188
     * org.gvsig.fmap.geom.primitive.OrientablePrimitive#setCoordinateAt(int,
189
     * int, double)
190
     */
191
    public void setCoordinateAt(int index, int dimension, double value) {
192
        coordinates.setOrdinate(index, dimension, value);
193
    }
194

    
195
    /*
196
     * (non-Javadoc)
197
     *
198
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#removeVertex(int)
199
     */
200
    public void removeVertex(int index) {
201
        coordinates.remove(index);
202
    }
203

    
204
    /*
205
     * (non-Javadoc)
206
     *
207
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getNumVertices()
208
     */
209
    public int getNumVertices() {
210
        return coordinates.size();
211
    }
212

    
213
    /*
214
     * (non-Javadoc)
215
     *
216
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#insertVertex(int,
217
     * org.gvsig.fmap.geom.primitive.Point)
218
     */
219
    public void insertVertex(int index, Point p) {
220
        p = fixPoint(p);
221
        coordinates.add(index, ((PointJTS) p).getJTSCoordinate());
222
    }
223

    
224
    /*
225
     * (non-Javadoc)
226
     *
227
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setVertex(int,
228
     * org.gvsig.fmap.geom.primitive.Point)
229
     */
230
    public void setVertex(int index, Point p) {
231
        p = fixPoint(p);
232
        coordinates.set(index, ((PointJTS) p).getJTSCoordinate());
233
    }
234

    
235
    /*
236
     * (non-Javadoc)
237
     *
238
     * @see
239
     * org.gvsig.fmap.geom.primitive.OrientablePrimitive#setGeneralPath(org.
240
     * gvsig.fmap.geom.primitive.GeneralPathX)
241
     */
242
    public void setGeneralPath(GeneralPathX generalPathX) {
243

    
244
        PathIterator it = generalPathX.getPathIterator(null);
245
        double[] segment = new double[6];
246
        int i = 0;
247
        while(!it.isDone()){
248
            int type = it.currentSegment(segment);
249
            if(i==0){
250
                switch (type) {
251
                case IGeneralPathX.SEG_MOVETO:
252
                    Point p = new Point2D(segment[0], segment[1]);
253
                    p = fixPoint(p);
254
                    coordinates.add(((PointJTS)p).getJTSCoordinate());
255
                    break;
256
                default:
257
                    String message = StringUtils.replace("Type of segment %(segment)s isn't SEG_MOVETO.","%(segment)s",String.valueOf(i));
258
                    logger.warn(message);
259
                    throw new RuntimeException(message);
260
                }
261
            } else {
262
                //Dudo de que los casos SEG_QUADTO y SEG_CUBICTO est?n bien pero se hac?a lo mismo en la librer?a de geometr?as vieja.
263
                Point p;
264
                switch (type) {
265
                case IGeneralPathX.SEG_LINETO:
266
                    p = new Point2D(segment[0], segment[1]);
267
                    p = fixPoint(p);
268
                    coordinates.add(((PointJTS)p).getJTSCoordinate());
269
                    break;
270
                case IGeneralPathX.SEG_QUADTO:
271
                    for (int j = 0; j <= 1; j++) {
272
                        p = new Point2D(segment[i], segment[i+1]);
273
                        p = fixPoint(p);
274
                        coordinates.add(((PointJTS) p).getJTSCoordinate());
275
                    }
276
                    break;
277
                case IGeneralPathX.SEG_CUBICTO:
278
                    for (int j = 0; j <= 2; j++) {
279
                        p = new Point2D(segment[i], segment[i+1]);
280
                        p = fixPoint(p);
281
                        coordinates.add(((PointJTS) p).getJTSCoordinate());
282
                    }
283
                    break;
284
                case IGeneralPathX.SEG_CLOSE:
285
                    coordinates.add(coordinates.get(0));
286
                    break;
287
                default:
288
                    String message = StringUtils.replace("The general path has a gap in segment %(segment)s.","%(segment)s",String.valueOf(i));
289
                    logger.warn(message);
290
                    throw new RuntimeException(message);
291
                }
292
            }
293
            it.next();
294
            i++;
295
        }
296
    }
297

    
298
    /*
299
     * (non-Javadoc)
300
     *
301
     * @see
302
     * org.gvsig.fmap.geom.primitive.OrientablePrimitive#addMoveToVertex(org
303
     * .gvsig.fmap.geom.primitive.Point)
304
     */
305
    public void addMoveToVertex(Point point) {
306
        throw new UnsupportedOperationException();
307
    }
308

    
309
    /*
310
     * (non-Javadoc)
311
     *
312
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#closePrimitive()
313
     */
314
    public void closePrimitive() {
315
        if (!coordinates.get(0).equals(coordinates.get(coordinates.size()))) {
316
            coordinates.add(coordinates.get(coordinates.size()));
317
        }
318
    }
319

    
320
    /*
321
     * (non-Javadoc)
322
     *
323
     * @see
324
     * org.gvsig.fmap.geom.primitive.OrientablePrimitive#ensureCapacity(int)
325
     */
326
    public void ensureCapacity(int capacity) {
327
        coordinates.ensureCapacity(capacity);
328
    }
329

    
330
    /*
331
     * (non-Javadoc)
332
     *
333
     * @see org.gvsig.fmap.geom.Geometry#reProject(org.cresques.cts.ICoordTrans)
334
     */
335
    public void reProject(ICoordTrans ct) {
336
        if (ct == null) {
337
            return;
338
        }
339
        for (Iterator<Coordinate> iterator = coordinates.iterator(); iterator.hasNext();) {
340
            Coordinate coordinate = (Coordinate) iterator.next();
341

    
342
            java.awt.geom.Point2D p = new java.awt.geom.Point2D.Double(coordinate.x, coordinate.y);
343
            try {
344
                p = ct.convert(p, p);
345
                coordinate.x = p.getX();
346
                coordinate.y = p.getY();
347
            } catch (Exception exc) {
348
                /*
349
                 * This can happen when the reprojection lib is unable
350
                 * to reproject (for example the source point
351
                 * is out of the valid range and some computing
352
                 * problem happens)
353
                 */
354
                throw new ReprojectionRuntimeException(ct.getPOrig(), ct.getPDest(), p, exc);
355
            }
356
        }
357
    }
358

    
359
    /*
360
     * (non-Javadoc)
361
     *
362
     * @see
363
     * org.gvsig.fmap.geom.Geometry#transform(java.awt.geom.AffineTransform)
364
     */
365
    public void transform(AffineTransform at) {
366
        if (at == null) {
367
            return;
368
        }
369

    
370
        for (Iterator<Coordinate> iterator = coordinates.iterator(); iterator.hasNext();) {
371
            Coordinate coordinate = (Coordinate) iterator.next();
372
            java.awt.geom.Point2D p = new java.awt.geom.Point2D.Double(coordinate.x, coordinate.y);
373

    
374
            at.transform(p, p);
375
            coordinate.x = p.getX();
376
            coordinate.y = p.getY();
377
        }
378
    }
379

    
380
    /*
381
     * (non-Javadoc)
382
     *
383
     * @see org.gvsig.fmap.geom.Geometry#getDimension()
384
     */
385
    public int getDimension() {
386
        return anyVertex.getDimension();
387
    }
388

    
389
    /*
390
     * (non-Javadoc)
391
     *
392
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
393
     */
394
    public Shape getShape(AffineTransform affineTransform) {
395
        return new DefaultGeneralPathX(new SplineIterator(affineTransform),false,0);
396
    }
397

    
398
    /*
399
     * (non-Javadoc)
400
     *
401
     * @see org.gvsig.fmap.geom.Geometry#getShape()
402
     */
403
    public Shape getShape() {
404
        return getShape(null);
405
    }
406

    
407
    /*
408
     * (non-Javadoc)
409
     *
410
     * @see
411
     * org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
412
     * )
413
     */
414
    public PathIterator getPathIterator(AffineTransform at) {
415
        SplineIterator pi = new SplineIterator(at);
416
        return pi;
417
    }
418

    
419
    /*
420
     * (non-Javadoc)
421
     *
422
     * @see
423
     * org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
424
     * , double)
425
     */
426
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
427
        return getPathIterator(at);
428
    }
429

    
430
    /*
431
     * (non-Javadoc)
432
     *
433
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
434
     */
435
    public GeneralPathX getGeneralPath() {
436
        return new DefaultGeneralPathX(new SplineIterator(null),false,0);
437
    }
438

    
439
    public class SplineIterator extends GeneralPathXIterator {
440

    
441
        /** Transform applied on the coordinates during iteration */
442
        private AffineTransform at;
443

    
444
        /** True when the point has been read once */
445
        private boolean done;
446
        private int index = 0;
447

    
448
        /**
449
         * Creates a new PointIterator object.
450
         *
451
         * @param p
452
         *            The polygon
453
         * @param at
454
         *            The affine transform applied to coordinates during
455
         *            iteration
456
         */
457
        public SplineIterator(AffineTransform at) {
458
            super(new GeneralPathX());
459
            if (at == null) {
460
                at = new AffineTransform();
461
            }
462

    
463
            this.at = at;
464
            done = false;
465
        }
466

    
467
        /**
468
         * Return the winding rule for determining the interior of the path.
469
         *
470
         * @return <code>WIND_EVEN_ODD</code> by default.
471
         */
472
        public int getWindingRule() {
473
            return PathIterator.WIND_EVEN_ODD;
474
        }
475

    
476
        /**
477
         * @see java.awt.geom.PathIterator#next()
478
         */
479
        public void next() {
480
            done = (getJTS().getCoordinates().length == ++index);
481
        }
482

    
483
        /**
484
         * @see java.awt.geom.PathIterator#isDone()
485
         */
486
        public boolean isDone() {
487
            return done;
488
        }
489

    
490
        /**
491
         * @see java.awt.geom.PathIterator#currentSegment(double[])
492
         */
493
        public int currentSegment(double[] coords) {
494
            Coordinate[] jtsCoordinates = getJTS().getCoordinates();
495
            coords[0] = jtsCoordinates[index].x;
496
            coords[1] = jtsCoordinates[index].y;
497
            at.transform(coords, 0, coords, 0, 1);
498

    
499
            if (index == 0) {
500
                return PathIterator.SEG_MOVETO;
501
            } else {
502
                return PathIterator.SEG_LINETO;
503
            }
504
        }
505

    
506
        /*
507
         * (non-Javadoc)
508
         *
509
         * @see java.awt.geom.PathIterator#currentSegment(float[])
510
         */
511
        public int currentSegment(float[] coords) {
512
            Coordinate[] jtsCoordinates = getJTS().getCoordinates();
513
            coords[0] = (float)jtsCoordinates[index].x;
514
            coords[1] = (float)jtsCoordinates[index].y;
515
            at.transform(coords, 0, coords, 0, 1);
516

    
517
            if (index == 0) {
518
                return PathIterator.SEG_MOVETO;
519
            } else {
520
                return PathIterator.SEG_LINETO;
521
            }
522
        }
523
    }
524

    
525

    
526
    /*
527
     * (non-Javadoc)
528
     *
529
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
530
     */
531
    public boolean is3D() {
532
        return anyVertex.is3D();
533
    }
534

    
535

    
536
    /* (non-Javadoc)
537
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
538
     */
539
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
540
        Collections.reverse(coordinates);
541
    }
542

    
543
    protected ArrayListCoordinateSequence cloneCoordinates() {
544
        ArrayListCoordinateSequence cloned = new ArrayListCoordinateSequence();
545
        cloned.ensureCapacity(coordinates.size());
546
        for (Iterator iterator = coordinates.iterator(); iterator.hasNext();) {
547
            Coordinate coordinate = (Coordinate) iterator.next();
548
            cloned.add((Coordinate)coordinate.clone());
549
        }
550
        return cloned;
551
    }
552

    
553

    
554
    protected boolean isClosed(){
555
        return coordinates.get(0).equals(coordinates.get(coordinates.size()-1));
556
    }
557

    
558
}