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

History | View | Annotate | Download (6.8 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.util.ArrayList;
26

    
27
import com.vividsolutions.jts.geom.Coordinate;
28

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryException;
31
import org.gvsig.fmap.geom.aggregate.MultiLine;
32
import org.gvsig.fmap.geom.aggregate.MultiPoint;
33
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
34
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2D;
35
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D;
36
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D;
37
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D;
38
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
39
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D;
40
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
41
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
42
import org.gvsig.fmap.geom.primitive.Line;
43
import org.gvsig.fmap.geom.primitive.Point;
44
import org.gvsig.fmap.geom.primitive.Polygon;
45

    
46

    
47
/**
48
 * @author fdiaz
49
 *
50
 */
51
public class Spline2D extends AbstractSpline {
52

    
53
    /**
54
     *
55
     */
56
    private static final long serialVersionUID = -4618430296292660668L;
57

    
58
    /**
59
     * @param subtype
60
     */
61
    public Spline2D() {
62
        super(Geometry.SUBTYPES.GEOM2D);
63
        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<Coordinate>());
64
    }
65

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

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

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

    
95
    /* (non-Javadoc)
96
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
97
     */
98
    public Geometry cloneGeometry() {
99
        return new Spline2D(cloneCoordinates().toCoordinateArray());
100
        }
101

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

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

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

    
149
    }
150

    
151

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

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

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

    
185

    
186
    /* (non-Javadoc)
187
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
188
     */
189
    public Point getVertex(int index) {
190
        Point2D vertex = new Point2D(this.coordinates.get(index));
191
        anyVertex = vertex;
192
        return vertex;
193
    }
194

    
195
}