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

History | View | Annotate | Download (6.95 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 OrientablePrimitive addVertex(double x, double y) {
106
        return this.addVertex(new Point3D(x, y, 0));
107
    }
108

    
109
    public OrientablePrimitive addVertex(double x, double y, double z) {
110
        return this.addVertex(new Point3D(x, y, z));
111
    }
112

    
113
    @Override
114
    protected Point fixPoint(Point point) {
115
        if (point instanceof Point3D) {
116
            return point;
117
        } else if (point instanceof Point3DM) {
118
            return new Point3D(point.getX(), point.getY(), ((Point3DM) point).getM());
119
        } else {
120
            return new Point3D(point.getX(), point.getY(), 0);
121
        }
122
    }
123

    
124
    @Override
125
    protected ArrayListCoordinateSequence getSplineCoordinates() {
126
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
127

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

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

    
172
    @Override
173
    public MultiLine toLines() throws GeometryException {
174
        MultiLine multiLine = new MultiLine3D();
175
        Line line = new Line3D(getJTS().getCoordinates());
176
        multiLine.addPrimitive(line);
177
        return multiLine;
178
    }
179

    
180
    @Override
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
    public Point getVertex(int index) {
189
        Point3D vertex = new Point3D(this.getProjection(), this.coordinates.get(index));
190
        anyVertex = vertex;
191
        return vertex;
192
    }
193
}