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 / BaseSpline2DM.java @ 47432

History | View | Annotate | Download (7.35 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
import com.vividsolutions.jts.geom.CoordinateSequence;
29

    
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.GeometryException;
32
import org.gvsig.fmap.geom.aggregate.MultiLine;
33
import org.gvsig.fmap.geom.aggregate.MultiPoint;
34
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
35
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2DM;
36
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2DM;
37
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2DM;
38
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2DM;
39
import org.gvsig.fmap.geom.jts.primitive.point.Point2DM;
40
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
41
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2DM;
42
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
43
import org.gvsig.fmap.geom.jts.util.JTSUtils;
44
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
45
import org.gvsig.fmap.geom.primitive.Line;
46
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
47
import org.gvsig.fmap.geom.primitive.Point;
48
import org.gvsig.fmap.geom.primitive.Polygon;
49

    
50

    
51
/**
52
 * @author fdiaz
53
 *
54
 */
55
public abstract class BaseSpline2DM extends AbstractSpline {
56

    
57
    /**
58
     *
59
     */
60
    private static final long serialVersionUID = 3466337030657231939L;
61

    
62

    
63
//    /**
64
//     * @param subtype
65
//     */
66
//    public BaseSpline2DM() {
67
//        super(Geometry.SUBTYPES.GEOM2DM);
68
//        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<Coordinate>());
69
//    }
70

    
71
    /**
72
     * @param type
73
     */
74
    public BaseSpline2DM(int type) {
75
        super(type, Geometry.SUBTYPES.GEOM2DM);
76
        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<>());
77
    }
78

    
79
//    /**
80
//     * @param subtype
81
//     * @param coordinates
82
//     * @param aVertex
83
//     */
84
//    public BaseSpline2DM(Coordinate[] coordinates) {
85
//        this();
86
//        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
87
//        if (coordinates.length < 1) {
88
//            anyVertex = new Point2DM(0, 0, 0);
89
//        } else {
90
//            anyVertex = new Point2DM(coordinates[0].x, coordinates[0].y, coordinates[0].z);
91
//        }
92
//    }
93

    
94
    /**
95
     * @param type
96
     * @param coordinates
97
     */
98
    public BaseSpline2DM(int type, Coordinate[] coordinates) {
99
        super(type, Geometry.SUBTYPES.GEOM2DM);
100
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
101
        if (coordinates.length < 1) {
102
            anyVertex = new Point3D(0, 0, 0);
103
        } else {
104
            Coordinate coordinate = coordinates[0];
105
            anyVertex = new Point3D(coordinate.x, coordinate.y, coordinate.getOrdinate(CoordinateSequence.M));
106
        }
107
    }
108

    
109
    public BaseSpline2DM(int type, ArrayListCoordinateSequence coordinates) {
110
        super(type, Geometry.SUBTYPES.GEOM2DM);
111
        this.coordinates = coordinates;
112
    }
113

    
114
    public OrientablePrimitive addVertex(double x, double y) {
115
        return this.addVertex(new Point2DM(x, y, 0));
116
    }
117

    
118
    public OrientablePrimitive addVertex(double x, double y, double z) {
119
        String message = "Can't add x,y,z coordinate to Polygon2DM.";
120
        notifyDeprecated(message);
121
        throw new UnsupportedOperationException(message);
122
    }
123

    
124
    @Override
125
    protected Point fixPoint(Point point) {
126
        if (point instanceof Point2DM) {
127
            return point;
128
        } else {
129
            return new Point2DM(point.getX(), point.getY(), 0);
130
        }
131
    }
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
            double[] pm = new double[num];
142
            for (int i = 0; i < num; i++) {
143
                Coordinate coord = coordinates.get(i);
144
                px[i] = coord.x;
145
                py[i] = coord.y;
146
                pm[i] = coord.getOrdinate(CoordinateSequence.M);
147
            }
148
            Spline splineX = new Spline(px);
149
            Spline splineY = new Spline(py);
150
            Spline splineM = new Spline(pm);
151
            splineCoordinates.add(coordinates.get(0));
152
            for (int i = 0; i < coordinates.size() - 1; i++) {
153
                for (int t = 1; t <= SUBSEGMENTS; t++) {
154
                    if ((t == SUBSEGMENTS) && (i == (coordinates.size() - 2))) {
155
                        // We don't calculate the last point to avoid a possible
156
                        // error precision with floating point numbers.
157
                        splineCoordinates.add(JTSUtils.createMCoordinate(px[px.length - 1], py[px.length - 1], pm[px.length - 1]));
158
                    } else {
159
                        double x1 = splineX.fn(i, ((double) t) / SUBSEGMENTS);
160
                        double y1 = splineY.fn(i, ((double) t) / SUBSEGMENTS);
161
                        double m1 = splineM.fn(i, ((double) t) / SUBSEGMENTS);
162
                        splineCoordinates.add(JTSUtils.createMCoordinate(x1, y1, m1));
163
                    }
164
                }
165
            }
166
        }
167
        return splineCoordinates;
168
    }
169

    
170
    @Override
171
    public MultiPoint toPoints() throws GeometryException {
172
        MultiPoint multiPoint = new MultiPoint2DM();
173
        Coordinate[] theCoordinates = getJTS().getCoordinates();
174
        multiPoint.ensureCapacity(theCoordinates.length);
175
        for (Coordinate theCoordinate : theCoordinates) {
176
            multiPoint.addPoint(new Point2DM(this.getProjection(), theCoordinate));
177
        }
178
        return multiPoint;
179
    }
180

    
181
    @Override
182
    public MultiLine toLines() throws GeometryException {
183
        MultiLine multiLine = new MultiLine2DM();
184
        Line line = new Line2DM(getJTS().getCoordinates());
185
        multiLine.addPrimitive(line);
186
        return multiLine;
187
    }
188

    
189
    @Override
190
    public MultiPolygon toPolygons() throws GeometryException {
191
        MultiPolygon multiPolygon = new MultiPolygon2DM();
192
        Polygon polygon = new Polygon2DM(getJTS().getCoordinates());
193
        multiPolygon.addPrimitive(polygon);
194
        return multiPolygon;
195
    }
196

    
197

    
198
    public Point getVertex(int index) {
199
        Point2DM vertex = new Point2DM(this.getProjection(), this.coordinates.get(index));
200
        anyVertex = vertex;
201
        return vertex;
202
    }
203
}