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

History | View | Annotate | Download (6.68 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.OrientablePrimitive;
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 type
69
     */
70
    public BaseSpline2D(int type) {
71
        super(type, Geometry.SUBTYPES.GEOM2D);
72
        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<>());
73
    }
74
    
75
    /**
76
     * @param type
77
     * @param coordinates
78
     */
79
    public BaseSpline2D(int type, Coordinate[] coordinates) {
80
        super(type, Geometry.SUBTYPES.GEOM2D);
81
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
82
        if (coordinates.length < 1) {
83
            anyVertex = new Point2D(0, 0);
84
        } else {
85
            anyVertex = new Point2D(coordinates[0].x, coordinates[0].y);
86
        }
87
    }
88

    
89
    public BaseSpline2D(int type, ArrayListCoordinateSequence coordinates) {
90
        super(type, Geometry.SUBTYPES.GEOM2D);
91
        this.coordinates = coordinates;
92
        if (this.coordinates.isEmpty()) {
93
            anyVertex = new Point2D(0, 0);
94
        } else {
95
            anyVertex = new Point2D(coordinates.getX(0), coordinates.getY(0));
96
        }
97
    }
98

    
99
    public OrientablePrimitive addVertex(double x, double y) {
100
        return this.addVertex(new Point2D(x, y));
101
    }
102

    
103
    public OrientablePrimitive addVertex(double x, double y, double z) {
104
        String message = "Can't add x,y,z coordinate to SPLine2D.";
105
        notifyDeprecated(message);
106
        throw new UnsupportedOperationException(message);
107
    }
108

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

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

    
144
    }
145

    
146
    @Override
147
    protected Point fixPoint(Point point) {
148
        if (point instanceof Point2D) {
149
            return point;
150
        } else {
151
            return new Point2D(point.getX(), point.getY());
152
        }
153
    }
154

    
155
    @Override
156
    public MultiPoint toPoints() throws GeometryException {
157
        MultiPoint multiPoint = new MultiPoint2D();
158
        Coordinate[] theCoordinates = getSplineCoordinates().toCoordinateArray();
159
        multiPoint.ensureCapacity(theCoordinates.length);
160
        for (Coordinate coordinate : theCoordinates) {
161
            multiPoint.addPoint(new Point2D(this.getProjection(), coordinate));
162
        }
163
        return multiPoint;
164
    }
165

    
166
    @Override
167
    public MultiLine toLines() throws GeometryException {
168
        MultiLine multiLine = new MultiLine2D();
169
        Line line = new Line2D(getSplineCoordinates().toCoordinateArray()); //getJTS().getCoordinates());
170
        multiLine.addPrimitive(line);
171
        return multiLine;
172
    }
173

    
174
    @Override
175
    public MultiPolygon toPolygons() throws GeometryException {
176
        MultiPolygon multiPolygon = new MultiPolygon2D();
177
        Polygon polygon = new Polygon2D(getSplineCoordinates().toCoordinateArray());
178
        multiPolygon.addPrimitive(polygon);
179
        return multiPolygon;
180
    }
181

    
182

    
183
    public Point getVertex(int index) {
184
        Point2D vertex = new Point2D(this.getProjection(), this.coordinates.get(index));
185
        anyVertex = vertex;
186
        return vertex;
187
    }
188

    
189

    
190
}