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

History | View | Annotate | Download (7.18 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

    
110
    public OrientablePrimitive addVertex(double x, double y) {
111
        return this.addVertex(new Point2DM(x, y, 0));
112
    }
113

    
114
    public OrientablePrimitive addVertex(double x, double y, double z) {
115
        String message = "Can't add x,y,z coordinate to Polygon2DM.";
116
        notifyDeprecated(message);
117
        throw new UnsupportedOperationException(message);
118
    }
119

    
120
    @Override
121
    protected Point fixPoint(Point point) {
122
        if (point instanceof Point2DM) {
123
            return point;
124
        } else {
125
            return new Point2DM(point.getX(), point.getY(), 0);
126
        }
127
    }
128

    
129
    @Override
130
    protected ArrayListCoordinateSequence getSplineCoordinates() {
131
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
132

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

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

    
177
    @Override
178
    public MultiLine toLines() throws GeometryException {
179
        MultiLine multiLine = new MultiLine2DM();
180
        Line line = new Line2DM(getJTS().getCoordinates());
181
        multiLine.addPrimitive(line);
182
        return multiLine;
183
    }
184

    
185
    @Override
186
    public MultiPolygon toPolygons() throws GeometryException {
187
        MultiPolygon multiPolygon = new MultiPolygon2DM();
188
        Polygon polygon = new Polygon2DM(getJTS().getCoordinates());
189
        multiPolygon.addPrimitive(polygon);
190
        return multiPolygon;
191
    }
192

    
193

    
194
    public Point getVertex(int index) {
195
        Point2DM vertex = new Point2DM(this.getProjection(), this.coordinates.get(index));
196
        anyVertex = vertex;
197
        return vertex;
198
    }
199
}