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 / Spline2DM.java @ 42441

History | View | Annotate | Download (7.67 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.surface.polygon.Polygon2DM;
41
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
42
import org.gvsig.fmap.geom.jts.util.JTSUtils;
43
import org.gvsig.fmap.geom.jts.util.OpenJUMPUtils;
44
import org.gvsig.fmap.geom.operation.GeometryOperationException;
45
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
46
import org.gvsig.fmap.geom.primitive.Line;
47
import org.gvsig.fmap.geom.primitive.Point;
48
import org.gvsig.fmap.geom.primitive.Polygon;
49
import org.gvsig.fmap.geom.primitive.Spline;
50

    
51

    
52
/**
53
 * @author fdiaz
54
 *
55
 */
56
public class Spline2DM extends BaseSpline2DM implements Spline {
57

    
58
    /**
59
     *
60
     */
61
    private static final long serialVersionUID = 3275115053567971975L;
62

    
63
    /**
64
     * @param subtype
65
     */
66
    public Spline2DM() {
67
        super(Geometry.TYPES.SPLINE);
68
    }
69

    
70
    /**
71
    *
72
    */
73
    public Spline2DM(Coordinate[] coordinates) {
74
        super(Geometry.TYPES.SPLINE, coordinates);
75
    }
76

    
77
    /* (non-Javadoc)
78
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
79
     */
80
    public void addVertex(double x, double y) {
81
        this.addVertex(new Point2DM(x, y, 0));
82
        }
83

    
84
    /* (non-Javadoc)
85
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
86
     */
87
    public void addVertex(double x, double y, double z) {
88
        String message = "Can't add x,y,z coordinate to Polygon2DM.";
89
        notifyDeprecated(message);
90
        throw new UnsupportedOperationException(message);
91
    }
92

    
93
    /* (non-Javadoc)
94
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
95
     */
96
    public Geometry cloneGeometry() {
97
        return new Spline2DM(cloneCoordinates().toCoordinateArray());
98
    }
99

    
100
    /* (non-Javadoc)
101
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#fixPoint(org.gvsig.fmap.geom.primitive.Point)
102
     */
103
    @Override
104
    protected Point fixPoint(Point point) {
105
        if (point instanceof Point2DM) {
106
            return point;
107
        } else {
108
            return new Point2DM(point.getX(), point.getY(), 0);
109
        }
110
    }
111

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

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

    
152
    /* (non-Javadoc)
153
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
154
     */
155
    public MultiPoint toPoints() throws GeometryException {
156
        MultiPoint multiPoint = new MultiPoint2DM();
157
        Coordinate[] coordinates = getJTS().getCoordinates();
158
        multiPoint.ensureCapacity(coordinates.length);
159
        for (int i = 0; i < coordinates.length; i++) {
160
            multiPoint.addPoint(new Point2DM(coordinates[i]));
161
        }
162
        return multiPoint;
163
    }
164

    
165
    /* (non-Javadoc)
166
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
167
     */
168
    public MultiLine toLines() throws GeometryException {
169
        MultiLine multiLine = new MultiLine2DM();
170
        Line line = new Line2DM(getJTS().getCoordinates());
171
        multiLine.addPrimitive(line);
172
        return multiLine;
173
    }
174

    
175
    /* (non-Javadoc)
176
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
177
     */
178
    public MultiPolygon toPolygons() throws GeometryException {
179
        MultiPolygon multiPolygon = new MultiPolygon2DM();
180
        Polygon polygon = new Polygon2DM(getJTS().getCoordinates());
181
        multiPolygon.addPrimitive(polygon);
182
        return multiPolygon;
183
    }
184

    
185

    
186
    /* (non-Javadoc)
187
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
188
     */
189
    public Point getVertex(int index) {
190
        Point2DM vertex = new Point2DM(this.coordinates.get(index));
191
        anyVertex = vertex;
192
        return vertex;
193
    }
194

    
195
    /* (non-Javadoc)
196
     * @see org.gvsig.fmap.geom.Geometry#offset(double)
197
     */
198
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
199
        Coordinate[] coords = getJTS().getCoordinates();
200
        ArrayListCoordinateSequence listCoordSequence = new ArrayListCoordinateSequence(new ArrayList<Coordinate>());
201
        for (int i = 0; i < coords.length; i++) {
202
            listCoordSequence.add(coords[i]);
203
        }
204
        if (isClosed()) {
205
            return JTSUtils.offsetClosedLine(listCoordSequence, distance);
206
        } else {
207
            return OpenJUMPUtils.offsetCleanOpenLine(listCoordSequence, distance);
208
        }
209
    }
210
}