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 / BaseSpline3D.java @ 47432

History | View | Annotate | Download (7.12 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.OrientablePrimitive;
45
import org.gvsig.fmap.geom.primitive.Point;
46
import org.gvsig.fmap.geom.primitive.Polygon;
47

    
48

    
49
/**
50
 * @author fdiaz
51
 *
52
 */
53
public abstract class BaseSpline3D extends AbstractSpline {
54

    
55
    /**
56
     *
57
     */
58
    private static final long serialVersionUID = 6699898811455152758L;
59

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

    
68
    /**
69
     * @param type
70
     */
71
    public BaseSpline3D(int type) {
72
        super(type, Geometry.SUBTYPES.GEOM3D);
73
        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<>());
74
    }
75

    
76
//    /**
77
//     * @param subtype
78
//     * @param coordinates
79
//     * @param aVertex
80
//     */
81
//    public BaseSpline3D(Coordinate[] coordinates) {
82
//        this();
83
//        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
84
//        if (coordinates.length < 1) {
85
//            anyVertex = new Point3D(0, 0, 0);
86
//        } else {
87
//            anyVertex = new Point3D(coordinates[0].x, coordinates[0].y, coordinates[0].z);
88
//        }
89
//    }
90

    
91
    /**
92
     * @param type
93
     * @param coordinates
94
     */
95
    public BaseSpline3D(int type, Coordinate[] coordinates) {
96
        super(type, Geometry.SUBTYPES.GEOM3D);
97
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
98
        if (coordinates.length < 1) {
99
            anyVertex = new Point3D(0, 0, 0);
100
        } else {
101
            anyVertex = new Point3D(coordinates[0].x, coordinates[0].y, coordinates[0].z);
102
        }
103
    }
104

    
105
    public BaseSpline3D(int type, ArrayListCoordinateSequence coordinates) {
106
        super(type, Geometry.SUBTYPES.GEOM3D);
107
        this.coordinates = coordinates;
108
    }
109
    
110
    public OrientablePrimitive addVertex(double x, double y) {
111
        return this.addVertex(new Point3D(x, y, 0));
112
    }
113

    
114
    public OrientablePrimitive addVertex(double x, double y, double z) {
115
        return this.addVertex(new Point3D(x, y, z));
116
    }
117

    
118
    @Override
119
    protected Point fixPoint(Point point) {
120
        if (point instanceof Point3D) {
121
            return point;
122
        } else if (point instanceof Point3DM) {
123
            return new Point3D(point.getX(), point.getY(), ((Point3DM) point).getM());
124
        } else {
125
            return new Point3D(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[] pz = 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
                pz[i] = coord.z;
143
            }
144
            Spline splineX = new Spline(px);
145
            Spline splineY = new Spline(py);
146
            Spline splineZ = new Spline(pz);
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(new Coordinate(px[px.length - 1], py[px.length - 1], pz[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 z1 = splineZ.fn(i, ((double) t) / SUBSEGMENTS);
158
                        splineCoordinates.add(new Coordinate(x1, y1, z1));
159
                    }
160
                }
161
            }
162
        }
163
        return splineCoordinates;
164
    }
165

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

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

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

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