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

History | View | Annotate | Download (8.73 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 com.vividsolutions.jts.geom.Coordinate;
26
import com.vividsolutions.jts.geom.CoordinateSequence;
27
import com.vividsolutions.jts.operation.buffer.BufferParameters;
28
import java.util.ArrayList;
29
import java.util.Arrays;
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.OrientablePrimitive;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.fmap.geom.primitive.Polygon;
50
import org.gvsig.fmap.geom.primitive.Spline;
51

    
52

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

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

    
64
    public Spline2DM() {
65
        super(Geometry.TYPES.SPLINE);
66
    }
67

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

    
76
    public Spline2DM(ArrayListCoordinateSequence coordinates) {
77
        super(Geometry.TYPES.SPLINE, coordinates);
78
    }
79
    
80
    @Override
81
    public OrientablePrimitive addVertex(double x, double y) {
82
        return this.addVertex(new Point2DM(x, y, 0));
83
    }
84

    
85
    @Override
86
    public OrientablePrimitive addVertex(double x, double y, double z) {
87
        String message = "Can't add x,y,z coordinate to Polygon2DM.";
88
        notifyDeprecated(message);
89
        throw new UnsupportedOperationException(message);
90
    }
91

    
92
    @Override
93
    public Geometry cloneGeometry() {
94
        Spline2DM clone = new Spline2DM(cloneCoordinates().toCoordinateArray());
95
        clone.setProjection(this.getProjection());
96
        return clone;
97
    }
98

    
99
    @Override
100
    protected Point fixPoint(Point point) {
101
        if (point instanceof Point2DM) {
102
            return point;
103
        } else {
104
            return new Point2DM(point.getX(), point.getY(), 0);
105
        }
106
    }
107

    
108
    @Override
109
    protected ArrayListCoordinateSequence getSplineCoordinates() {
110
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
111

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

    
145
    @Override
146
    public MultiPoint toPoints() throws GeometryException {
147
        MultiPoint multiPoint = new MultiPoint2DM();
148
        Coordinate[] theCoordinates = getJTS().getCoordinates();
149
        multiPoint.ensureCapacity(theCoordinates.length);
150
        for (Coordinate theCoordinate : theCoordinates) {
151
            multiPoint.addPoint(new Point2DM(this.getProjection(), theCoordinate));
152
        }
153
        return multiPoint;
154
    }
155

    
156
    @Override
157
    public MultiLine toLines() throws GeometryException {
158
        MultiLine multiLine = new MultiLine2DM();
159
        Line line = new Line2DM(getJTS().getCoordinates());
160
        multiLine.addPrimitive(line);
161
        return multiLine;
162
    }
163

    
164
    @Override
165
    public MultiPolygon toPolygons() throws GeometryException {
166
        MultiPolygon multiPolygon = new MultiPolygon2DM();
167
        Polygon polygon = new Polygon2DM(getJTS().getCoordinates());
168
        multiPolygon.addPrimitive(polygon);
169
        return multiPolygon;
170
    }
171

    
172

    
173
    @Override
174
    public Point getVertex(int index) {
175
        Point2DM vertex = new Point2DM(this.getProjection(), this.coordinates.get(index));
176
        anyVertex = vertex;
177
        return vertex;
178
    }
179

    
180
    @Override
181
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
182
        Coordinate[] coords = getJTS().getCoordinates();
183
        ArrayListCoordinateSequence listCoordSequence = new ArrayListCoordinateSequence(new ArrayList<>());
184
        listCoordSequence.addAll(Arrays.asList(coords));
185
        if (isClosed()) {
186
            return JTSUtils.offsetClosedLine(this.getProjection(), listCoordSequence, distance);
187
        } else {
188
            return OpenJUMPUtils.offsetCleanOpenLine(this.getProjection(), listCoordSequence, distance);
189
        }
190
    }
191

    
192
    @Override
193
    public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
194
        Coordinate[] coords = getJTS().getCoordinates();
195
        ArrayListCoordinateSequence listCoordSequence = new ArrayListCoordinateSequence(new ArrayList<Coordinate>());
196
        for (int i = 0; i < coords.length; i++) {
197
            listCoordSequence.add(coords[i]);
198
        }
199
        if (isClosed()) {
200
            return JTSUtils.offsetClosedLine(this.getProjection(), listCoordSequence, joinStyle, distance);
201
        } else {
202
            BufferParameters bufParams = JTSUtils.getBufferParameters(joinStyle, BufferParameters.CAP_FLAT);
203
            return OpenJUMPUtils.offsetCleanOpenLine(this.getProjection(), listCoordSequence, bufParams, distance);
204
        }
205
    }
206
    
207
    @Override
208
    public boolean equals(Object obj) {
209
        boolean res = super.equals(obj);
210
        if(res && obj instanceof Spline2DM){
211
            Spline2DM other = (Spline2DM)obj;
212
            if(this.getNumVertices() != other.getNumVertices()){
213
                return false;
214
            }
215
            for(int i=0; i < this.getNumVertices(); i++){
216
                if(other.coordinates.get(i).getOrdinate(2)!=this.coordinates.get(i).getOrdinate(2)){
217
                    return false;
218
                }
219
            }
220
            return true;
221
        } else {
222
            return false;
223
        }
224
    }
225

    
226
    @Override
227
    public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
228
        Geometry c = this.cloneGeometry();
229
        return c;
230
    }
231
}