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

History | View | Annotate | Download (7.74 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.Point2D;
39
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
40
import org.gvsig.fmap.geom.jts.primitive.point.Point3DM;
41
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon3D;
42
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
43
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
44
import org.gvsig.fmap.geom.primitive.Line;
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 polygon
70
     */
71
    public BaseSpline3D(int type) {
72
        super(type, Geometry.SUBTYPES.GEOM3D);
73
        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<Coordinate>());
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
    /* (non-Javadoc)
106
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
107
     */
108
    public void addVertex(double x, double y) {
109
        this.addVertex(new Point3D(x, y, 0));
110
        }
111

    
112
    /* (non-Javadoc)
113
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
114
     */
115
    public void addVertex(double x, double y, double z) {
116
        this.addVertex(new Point3D(x, y, z));
117
    }
118

    
119
    /* (non-Javadoc)
120
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#fixPoint(org.gvsig.fmap.geom.primitive.Point)
121
     */
122
    @Override
123
    protected Point fixPoint(Point point) {
124
        if (point instanceof Point3D) {
125
            return point;
126
        } else if (point instanceof Point3DM) {
127
            return new Point3D(point.getX(), point.getY(), ((Point3DM) point).getM());
128
        } else {
129
            return new Point3D(point.getX(), point.getY(), 0);
130
        }
131
    }
132

    
133
    /* (non-Javadoc)
134
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#getSplineCoordinates()
135
     */
136
    @Override
137
    protected ArrayListCoordinateSequence getSplineCoordinates() {
138
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
139

    
140
        if (splineCoordinates == null || splineCoordinates.size() == 0) {
141
            int num = coordinates.size();
142
            double[] px = new double[num];
143
            double[] py = new double[num];
144
            double[] pz = new double[num];
145
            for (int i = 0; i < num; i++) {
146
                Coordinate coord = coordinates.get(i);
147
                px[i] = coord.x;
148
                py[i] = coord.y;
149
                pz[i] = coord.z;
150
            }
151
            Spline splineX = new Spline(px);
152
            Spline splineY = new Spline(py);
153
            Spline splineZ = new Spline(pz);
154
            splineCoordinates.add(coordinates.get(0));
155
            for (int i = 0; i < coordinates.size() - 1; i++) {
156
                for (int t = 1; t <= SUBSEGMENTS; t++) {
157
                    if ((t == SUBSEGMENTS) && (i == (coordinates.size() - 2))) {
158
                        // We don't calculate the last point to avoid a possible
159
                        // error precision with floating point numbers.
160
                        splineCoordinates.add(new Coordinate(px[px.length - 1], py[px.length - 1], pz[px.length - 1]));
161
                    } else {
162
                        double x1 = splineX.fn(i, ((double) t) / SUBSEGMENTS);
163
                        double y1 = splineY.fn(i, ((double) t) / SUBSEGMENTS);
164
                        double z1 = splineZ.fn(i, ((double) t) / SUBSEGMENTS);
165
                        splineCoordinates.add(new Coordinate(x1, y1, z1));
166
                    }
167
                }
168
            }
169
        }
170
        return splineCoordinates;
171
    }
172

    
173
    /* (non-Javadoc)
174
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
175
     */
176
    public MultiPoint toPoints() throws GeometryException {
177
        MultiPoint multiPoint = new MultiPoint3D();
178
        Coordinate[] coordinates = getJTS().getCoordinates();
179
        multiPoint.ensureCapacity(coordinates.length);
180
        for (int i = 0; i < coordinates.length; i++) {
181
            multiPoint.addPoint(new Point3D(this.getProjection(), coordinates[i]));
182
        }
183
        return multiPoint;
184
    }
185

    
186
    /* (non-Javadoc)
187
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
188
     */
189
    public MultiLine toLines() throws GeometryException {
190
        MultiLine multiLine = new MultiLine3D();
191
        Line line = new Line3D(getJTS().getCoordinates());
192
        multiLine.addPrimitive(line);
193
        return multiLine;
194
    }
195

    
196
    /* (non-Javadoc)
197
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
198
     */
199
    public MultiPolygon toPolygons() throws GeometryException {
200
        MultiPolygon multiPolygon = new MultiPolygon3D();
201
        Polygon polygon = new Polygon3D(getJTS().getCoordinates());
202
        multiPolygon.addPrimitive(polygon);
203
        return multiPolygon;
204
    }
205

    
206
    /* (non-Javadoc)
207
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
208
     */
209
    public Point getVertex(int index) {
210
        Point3D vertex = new Point3D(this.getProjection(), this.coordinates.get(index));
211
        anyVertex = vertex;
212
        return vertex;
213
    }
214
}