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 / Spline2D.java @ 42267

History | View | Annotate | Download (4.78 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 com.vividsolutions.jts.geom.Coordinate;
26

    
27
import org.gvsig.fmap.geom.Geometry;
28
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D;
29
import org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline.Spline;
30
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
31
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
32
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
33
import org.gvsig.fmap.geom.primitive.Point;
34

    
35

    
36
/**
37
 * @author fdiaz
38
 *
39
 */
40
public class Spline2D extends AbstractSpline {
41

    
42
    /**
43
     *
44
     */
45
    private static final long serialVersionUID = -4618430296292660668L;
46

    
47
    /**
48
     * @param subtype
49
     */
50
    protected Spline2D() {
51
        super(Geometry.SUBTYPES.GEOM2D);
52
    }
53

    
54
    /**
55
    *
56
    */
57
    public Spline2D(Coordinate[] coordinates) {
58
        this();
59
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
60
        if (coordinates.length < 1) {
61
            anyVertex = new Point2D(0, 0);
62
        } else {
63
            anyVertex = new Point2D(coordinates[0].x, coordinates[0].y);
64
        }
65
    }
66

    
67
    /* (non-Javadoc)
68
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
69
     */
70
    public void addVertex(double x, double y) {
71
        this.addVertex(new Point2D(x, y));
72
        }
73

    
74
    /* (non-Javadoc)
75
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
76
     */
77
    public void addVertex(double x, double y, double z) {
78
        String message = "Can't add x,y,z coordinate to SPLine2D.";
79
        notifyDeprecated(message);
80
        throw new UnsupportedOperationException(message);
81
    }
82

    
83
    /* (non-Javadoc)
84
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
85
     */
86
    public Geometry cloneGeometry() {
87
        return new Spline2D((Coordinate[]) coordinates.clone());
88
        }
89

    
90
    /* (non-Javadoc)
91
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#fixPoint(org.gvsig.fmap.geom.primitive.Point)
92
     */
93
    @Override
94
    protected Point fixPoint(Point point) {
95
        if (point instanceof Point2D) {
96
            return point;
97
        } else {
98
            return new Point2D(point.getX(), point.getY());
99
        }
100
    }
101

    
102
    /* (non-Javadoc)
103
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#getSplineCoordinates()
104
     */
105
    @Override
106
    protected ArrayListCoordinateSequence getSplineCoordinates() {
107
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
108

    
109
        if (splineCoordinates == null || splineCoordinates.size() == 0) {
110
            int num = coordinates.size();
111
            double[] px = new double[num];
112
            double[] py = new double[num];
113
            for (int i = 0; i < num; i++) {
114
                Coordinate coord = coordinates.get(i);
115
                px[i] = coord.x;
116
                py[i] = coord.y;
117
            }
118
            Spline splineX = new Spline(px);
119
            Spline splineY = new Spline(py);
120
            splineCoordinates.add(coordinates.get(0));
121
            for (int i = 0; i < coordinates.size() - 1; i++) {
122
                for (int t = 1; t <= SUBSEGMENTS; t++) {
123
                    if ((t == SUBSEGMENTS) && (i == (coordinates.size() - 2))) {
124
                        // We don't calculate the last point to avoid a possible
125
                        // error precision with floating point numbers.
126
                        splineCoordinates.add(new Coordinate(px[px.length - 1], py[px.length - 1]));
127
                    } else {
128
                        double x1 = splineX.fn(i, ((double) t) / SUBSEGMENTS);
129
                        double y1 = splineY.fn(i, ((double) t) / SUBSEGMENTS);
130
                        splineCoordinates.add(new Coordinate(x1, y1));
131
                    }
132
                }
133
            }
134
        }
135
        return splineCoordinates;
136

    
137
    }
138

    
139

    
140
}