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 / Spline3DM.java @ 42271

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

    
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.fmap.geom.GeometryException;
30
import org.gvsig.fmap.geom.aggregate.MultiLine;
31
import org.gvsig.fmap.geom.aggregate.MultiPoint;
32
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
33
import org.gvsig.fmap.geom.jts.aggregate.MultiLine3DM;
34
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint3DM;
35
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon3DM;
36
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3DM;
37
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
38
import org.gvsig.fmap.geom.jts.primitive.point.Point3DM;
39
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon3DM;
40
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
41
import org.gvsig.fmap.geom.jts.util.JTSUtils;
42
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
43
import org.gvsig.fmap.geom.primitive.Line;
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 class Spline3DM extends AbstractSpline {
53

    
54
    /**
55
     *
56
     */
57
    private static final long serialVersionUID = 6068987767873860480L;
58

    
59
    /**
60
     * @param subtype
61
     */
62
    protected Spline3DM() {
63
        super(Geometry.SUBTYPES.GEOM3DM);
64
    }
65

    
66
    /**
67
    *
68
    */
69
    public Spline3DM(Coordinate[] coordinates) {
70
        this();
71
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
72
        if (coordinates.length < 1) {
73
            anyVertex = new Point3DM(0, 0, 0, 0);
74
        } else {
75
            Coordinate coordinate = coordinates[0];
76
            anyVertex = new Point3DM(coordinate.x, coordinate.y, coordinate.z, coordinate.getOrdinate(CoordinateSequence.M));
77
        }
78
    }
79

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

    
87
    /* (non-Javadoc)
88
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
89
     */
90
    public void addVertex(double x, double y, double z) {
91
        this.addVertex(new Point3DM(x, y, z, 0));
92
    }
93

    
94
    /* (non-Javadoc)
95
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
96
     */
97
    public Geometry cloneGeometry() {
98
        return new Spline3DM((Coordinate[]) coordinates.clone());
99
    }
100

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

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

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

    
159
    /* (non-Javadoc)
160
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
161
     */
162
    public MultiPoint toPoints() throws GeometryException {
163
        MultiPoint multiPoint = new MultiPoint3DM();
164
        Coordinate[] coordinates = getJTS().getCoordinates();
165
        multiPoint.ensureCapacity(coordinates.length);
166
        for (int i = 0; i < coordinates.length; i++) {
167
            multiPoint.addPoint(new Point3DM(coordinates[i]));
168
        }
169
        return multiPoint;
170
    }
171

    
172
    /* (non-Javadoc)
173
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
174
     */
175
    public MultiLine toLines() throws GeometryException {
176
        MultiLine multiLine = new MultiLine3DM();
177
        Line line = new Line3DM(getJTS().getCoordinates());
178
        multiLine.addPrimitive(line);
179
        return multiLine;
180
    }
181

    
182
    /* (non-Javadoc)
183
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
184
     */
185
    public MultiPolygon toPolygons() throws GeometryException {
186
        MultiPolygon multiPolygon = new MultiPolygon3DM();
187
        Polygon polygon = new Polygon3DM(getJTS().getCoordinates());
188
        multiPolygon.addPrimitive(polygon);
189
        return multiPolygon;
190
    }
191

    
192

    
193
}