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

History | View | Annotate | Download (7.83 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
    public BaseSpline2D(int type, ArrayListCoordinateSequence coordinates) {
105
        super(type, Geometry.SUBTYPES.GEOM2D);
106
        this.coordinates = coordinates;
107
        if (this.coordinates.isEmpty()) {
108
            anyVertex = new Point2D(0, 0);
109
        } else {
110
            anyVertex = new Point2D(coordinates.getX(0), coordinates.getY(0));
111
        }
112
    }
113

    
114
    /* (non-Javadoc)
115
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
116
     */
117
    public void addVertex(double x, double y) {
118
        this.addVertex(new Point2D(x, y));
119
    }
120

    
121
    /* (non-Javadoc)
122
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
123
     */
124
    public void addVertex(double x, double y, double z) {
125
        String message = "Can't add x,y,z coordinate to SPLine2D.";
126
        notifyDeprecated(message);
127
        throw new UnsupportedOperationException(message);
128
    }
129

    
130
    /* (non-Javadoc)
131
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#getSplineCoordinates()
132
     */
133
    @Override
134
    protected ArrayListCoordinateSequence getSplineCoordinates() {
135
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
136

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

    
165
    }
166

    
167
    /* (non-Javadoc)
168
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#fixPoint(org.gvsig.fmap.geom.primitive.Point)
169
     */
170
    @Override
171
    protected Point fixPoint(Point point) {
172
        if (point instanceof Point2D) {
173
            return point;
174
        } else {
175
            return new Point2D(point.getX(), point.getY());
176
        }
177
    }
178

    
179
    /* (non-Javadoc)
180
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
181
     */
182
    public MultiPoint toPoints() throws GeometryException {
183
        MultiPoint multiPoint = new MultiPoint2D();
184
        Coordinate[] coordinates = getSplineCoordinates().toCoordinateArray();
185
        multiPoint.ensureCapacity(coordinates.length);
186
        for (int i = 0; i < coordinates.length; i++) {
187
            multiPoint.addPoint(new Point2D(this.getProjection(), coordinates[i]));
188
        }
189
        return multiPoint;
190
    }
191

    
192
    /* (non-Javadoc)
193
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
194
     */
195
    public MultiLine toLines() throws GeometryException {
196
        MultiLine multiLine = new MultiLine2D();
197
        Line line = new Line2D(getSplineCoordinates().toCoordinateArray()); //getJTS().getCoordinates());
198
        multiLine.addPrimitive(line);
199
        return multiLine;
200
    }
201

    
202
    /* (non-Javadoc)
203
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
204
     */
205
    public MultiPolygon toPolygons() throws GeometryException {
206
        MultiPolygon multiPolygon = new MultiPolygon2D();
207
        Polygon polygon = new Polygon2D(getSplineCoordinates().toCoordinateArray());
208
        multiPolygon.addPrimitive(polygon);
209
        return multiPolygon;
210
    }
211

    
212

    
213
    /* (non-Javadoc)
214
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
215
     */
216
    public Point getVertex(int index) {
217
        Point2D vertex = new Point2D(this.getProjection(), this.coordinates.get(index));
218
        anyVertex = vertex;
219
        return vertex;
220
    }
221

    
222

    
223
}