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 / BaseSpline2D.java @ 44099

History | View | Annotate | Download (7.48 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.point.PointJTS;
40
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D;
41
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
42
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
43
import org.gvsig.fmap.geom.primitive.Line;
44
import org.gvsig.fmap.geom.primitive.Point;
45
import org.gvsig.fmap.geom.primitive.Polygon;
46

    
47

    
48
/**
49
 * @author fdiaz
50
 *
51
 */
52
public abstract class BaseSpline2D extends AbstractSpline {
53

    
54
    /**
55
     *
56
     */
57
    private static final long serialVersionUID = -8257006501866486625L;
58

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

    
67
    /**
68
     * @param polygon
69
     */
70
    public BaseSpline2D(int type) {
71
        super(type, Geometry.SUBTYPES.GEOM2D);
72
        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<Coordinate>());
73
    }
74

    
75
//    /**
76
//     * @param subtype
77
//     * @param coordinates
78
//     * @param aVertex
79
//     */
80
//    public BaseSpline2D(Coordinate[] coordinates) {
81
//        this();
82
//        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
83
//        if (coordinates.length < 1) {
84
//            anyVertex = new Point2D(0, 0);
85
//        } else {
86
//            anyVertex = new Point2D(coordinates[0].x, coordinates[0].y);
87
//        }
88
//    }
89

    
90
    /**
91
     * @param type
92
     * @param coordinates
93
     */
94
    public BaseSpline2D(int type, Coordinate[] coordinates) {
95
        super(type, Geometry.SUBTYPES.GEOM2D);
96
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
97
        if (coordinates.length < 1) {
98
            anyVertex = new Point2D(0, 0);
99
        } else {
100
            anyVertex = new Point2D(coordinates[0].x, coordinates[0].y);
101
        }
102
    }
103

    
104
    /* (non-Javadoc)
105
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
106
     */
107
    public void addVertex(double x, double y) {
108
        this.addVertex(new Point2D(x, y));
109
    }
110

    
111
    /* (non-Javadoc)
112
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
113
     */
114
    public void addVertex(double x, double y, double z) {
115
        String message = "Can't add x,y,z coordinate to SPLine2D.";
116
        notifyDeprecated(message);
117
        throw new UnsupportedOperationException(message);
118
    }
119

    
120
    /* (non-Javadoc)
121
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#getSplineCoordinates()
122
     */
123
    @Override
124
    protected ArrayListCoordinateSequence getSplineCoordinates() {
125
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
126

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

    
155
    }
156

    
157
    /* (non-Javadoc)
158
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#fixPoint(org.gvsig.fmap.geom.primitive.Point)
159
     */
160
    @Override
161
    protected Point fixPoint(Point point) {
162
        if (point instanceof Point2D) {
163
            return point;
164
        } else {
165
            return new Point2D(point.getX(), point.getY());
166
        }
167
    }
168

    
169
    /* (non-Javadoc)
170
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
171
     */
172
    public MultiPoint toPoints() throws GeometryException {
173
        MultiPoint multiPoint = new MultiPoint2D();
174
        Coordinate[] coordinates = getSplineCoordinates().toCoordinateArray();
175
        multiPoint.ensureCapacity(coordinates.length);
176
        for (int i = 0; i < coordinates.length; i++) {
177
            multiPoint.addPoint(new Point2D(this.getProjection(), coordinates[i]));
178
        }
179
        return multiPoint;
180
    }
181

    
182
    /* (non-Javadoc)
183
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
184
     */
185
    public MultiLine toLines() throws GeometryException {
186
        MultiLine multiLine = new MultiLine2D();
187
        Line line = new Line2D(getSplineCoordinates().toCoordinateArray()); //getJTS().getCoordinates());
188
        multiLine.addPrimitive(line);
189
        return multiLine;
190
    }
191

    
192
    /* (non-Javadoc)
193
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
194
     */
195
    public MultiPolygon toPolygons() throws GeometryException {
196
        MultiPolygon multiPolygon = new MultiPolygon2D();
197
        Polygon polygon = new Polygon2D(getSplineCoordinates().toCoordinateArray());
198
        multiPolygon.addPrimitive(polygon);
199
        return multiPolygon;
200
    }
201

    
202

    
203
    /* (non-Javadoc)
204
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
205
     */
206
    public Point getVertex(int index) {
207
        Point2D vertex = new Point2D(this.getProjection(), this.coordinates.get(index));
208
        anyVertex = vertex;
209
        return vertex;
210
    }
211

    
212

    
213
}