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 @ 42271

History | View | Annotate | Download (6.34 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.GeometryException;
29
import org.gvsig.fmap.geom.aggregate.MultiLine;
30
import org.gvsig.fmap.geom.aggregate.MultiPoint;
31
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
32
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2D;
33
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D;
34
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D;
35
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D;
36
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
37
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D;
38
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
39
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
40
import org.gvsig.fmap.geom.primitive.Line;
41
import org.gvsig.fmap.geom.primitive.Point;
42
import org.gvsig.fmap.geom.primitive.Polygon;
43

    
44

    
45
/**
46
 * @author fdiaz
47
 *
48
 */
49
public class Spline2D extends AbstractSpline {
50

    
51
    /**
52
     *
53
     */
54
    private static final long serialVersionUID = -4618430296292660668L;
55

    
56
    /**
57
     * @param subtype
58
     */
59
    protected Spline2D() {
60
        super(Geometry.SUBTYPES.GEOM2D);
61
    }
62

    
63
    /**
64
    *
65
    */
66
    public Spline2D(Coordinate[] coordinates) {
67
        this();
68
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
69
        if (coordinates.length < 1) {
70
            anyVertex = new Point2D(0, 0);
71
        } else {
72
            anyVertex = new Point2D(coordinates[0].x, coordinates[0].y);
73
        }
74
    }
75

    
76
    /* (non-Javadoc)
77
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
78
     */
79
    public void addVertex(double x, double y) {
80
        this.addVertex(new Point2D(x, y));
81
        }
82

    
83
    /* (non-Javadoc)
84
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
85
     */
86
    public void addVertex(double x, double y, double z) {
87
        String message = "Can't add x,y,z coordinate to SPLine2D.";
88
        notifyDeprecated(message);
89
        throw new UnsupportedOperationException(message);
90
    }
91

    
92
    /* (non-Javadoc)
93
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
94
     */
95
    public Geometry cloneGeometry() {
96
        return new Spline2D((Coordinate[]) coordinates.clone());
97
        }
98

    
99
    /* (non-Javadoc)
100
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#fixPoint(org.gvsig.fmap.geom.primitive.Point)
101
     */
102
    @Override
103
    protected Point fixPoint(Point point) {
104
        if (point instanceof Point2D) {
105
            return point;
106
        } else {
107
            return new Point2D(point.getX(), point.getY());
108
        }
109
    }
110

    
111
    /* (non-Javadoc)
112
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#getSplineCoordinates()
113
     */
114
    @Override
115
    protected ArrayListCoordinateSequence getSplineCoordinates() {
116
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
117

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

    
146
    }
147

    
148

    
149
    /* (non-Javadoc)
150
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
151
     */
152
    public MultiPoint toPoints() throws GeometryException {
153
        MultiPoint multiPoint = new MultiPoint2D();
154
        Coordinate[] coordinates = getJTS().getCoordinates();
155
        multiPoint.ensureCapacity(coordinates.length);
156
        for (int i = 0; i < coordinates.length; i++) {
157
            multiPoint.addPoint(new Point2D(coordinates[i]));
158
        }
159
        return multiPoint;
160
    }
161

    
162
    /* (non-Javadoc)
163
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
164
     */
165
    public MultiLine toLines() throws GeometryException {
166
        MultiLine multiLine = new MultiLine2D();
167
        Line line = new Line2D(getJTS().getCoordinates());
168
        multiLine.addPrimitive(line);
169
        return multiLine;
170
    }
171

    
172
    /* (non-Javadoc)
173
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
174
     */
175
    public MultiPolygon toPolygons() throws GeometryException {
176
        MultiPolygon multiPolygon = new MultiPolygon2D();
177
        Polygon polygon = new Polygon2D(getJTS().getCoordinates());
178
        multiPolygon.addPrimitive(polygon);
179
        return multiPolygon;
180
    }
181

    
182
}