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 / Spline3D.java @ 42283

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

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryException;
31
import org.gvsig.fmap.geom.aggregate.MultiLine;
32
import org.gvsig.fmap.geom.aggregate.MultiPoint;
33
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
34
import org.gvsig.fmap.geom.jts.aggregate.MultiLine3D;
35
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint3D;
36
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon3D;
37
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3D;
38
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
39
import org.gvsig.fmap.geom.jts.primitive.point.Point3DM;
40
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon3D;
41
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
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 Spline3D extends AbstractSpline {
53

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

    
59
    /**
60
     * @param subtype
61
     */
62
    public Spline3D() {
63
        super(Geometry.SUBTYPES.GEOM3D);
64
        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<Coordinate>());
65
    }
66

    
67
    /**
68
    *
69
    */
70
    public Spline3D(Coordinate[] coordinates) {
71
        this();
72
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
73
        if (coordinates.length < 1) {
74
            anyVertex = new Point3D(0, 0, 0);
75
        } else {
76
            anyVertex = new Point3D(coordinates[0].x, coordinates[0].y, coordinates[0].z);
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 Point3D(x, y, 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 Point3D(x, y, z));
92
    }
93

    
94
    /* (non-Javadoc)
95
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
96
     */
97
    public Geometry cloneGeometry() {
98
        return new Spline3D(cloneCoordinates().toCoordinateArray());
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 Point3D) {
107
            return point;
108
        } else if (point instanceof Point3DM) {
109
            return new Point3D(point.getX(), point.getY(), ((Point3DM) point).getM());
110
        } else {
111
            return new Point3D(point.getX(), point.getY(), 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
            for (int i = 0; i < num; i++) {
128
                Coordinate coord = coordinates.get(i);
129
                px[i] = coord.x;
130
                py[i] = coord.y;
131
                pz[i] = coord.z;
132
            }
133
            Spline splineX = new Spline(px);
134
            Spline splineY = new Spline(py);
135
            Spline splineZ = new Spline(pz);
136
            splineCoordinates.add(coordinates.get(0));
137
            for (int i = 0; i < coordinates.size() - 1; i++) {
138
                for (int t = 1; t <= SUBSEGMENTS; t++) {
139
                    if ((t == SUBSEGMENTS) && (i == (coordinates.size() - 2))) {
140
                        // We don't calculate the last point to avoid a possible
141
                        // error precision with floating point numbers.
142
                        splineCoordinates.add(new Coordinate(px[px.length - 1], py[px.length - 1], pz[px.length - 1]));
143
                    } else {
144
                        double x1 = splineX.fn(i, ((double) t) / SUBSEGMENTS);
145
                        double y1 = splineY.fn(i, ((double) t) / SUBSEGMENTS);
146
                        double z1 = splineZ.fn(i, ((double) t) / SUBSEGMENTS);
147
                        splineCoordinates.add(new Coordinate(x1, y1, z1));
148
                    }
149
                }
150
            }
151
        }
152
        return splineCoordinates;
153
    }
154

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

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

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

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