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

History | View | Annotate | Download (7 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
import com.vividsolutions.jts.geom.CoordinateSequence;
29

    
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.GeometryException;
32
import org.gvsig.fmap.geom.aggregate.MultiLine;
33
import org.gvsig.fmap.geom.aggregate.MultiPoint;
34
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
35
import org.gvsig.fmap.geom.jts.aggregate.MultiLine3DM;
36
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint3DM;
37
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon3DM;
38
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3DM;
39
import org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline.Spline;
40
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
41
import org.gvsig.fmap.geom.jts.primitive.point.Point3DM;
42
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon3DM;
43
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
44
import org.gvsig.fmap.geom.jts.util.JTSUtils;
45
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
46
import org.gvsig.fmap.geom.primitive.Line;
47
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.fmap.geom.primitive.Polygon;
50

    
51

    
52
/**
53
 * @author fdiaz
54
 *
55
 */
56
public abstract class BaseSpline3DM extends AbstractSpline {
57

    
58
    /**
59
     *
60
     */
61
    private static final long serialVersionUID = -5436087623278868159L;
62

    
63

    
64
    /**
65
     * @param type
66
     */
67
    public BaseSpline3DM(int type) {
68
        super(type, Geometry.SUBTYPES.GEOM3DM);
69
        this.coordinates = new ArrayListCoordinateSequence(new ArrayList<>());
70
    }
71

    
72
    /**
73
     * @param type
74
     * @param coordinates
75
     */
76
    public BaseSpline3DM(int type, Coordinate[] coordinates) {
77
        super(type, Geometry.SUBTYPES.GEOM3DM);
78
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
79
        if (coordinates.length < 1) {
80
            anyVertex = new Point3DM(0, 0, 0, 0);
81
        } else {
82
            Coordinate coordinate = coordinates[0];
83
            anyVertex = new Point3DM(coordinate.x, coordinate.y, coordinate.z, coordinate.getOrdinate(CoordinateSequence.M));
84
        }
85
    }
86

    
87
    public BaseSpline3DM(int type, ArrayListCoordinateSequence coordinates) {
88
        super(type, Geometry.SUBTYPES.GEOM3DM);
89
        this.coordinates = coordinates;
90
    }
91
    
92
    public OrientablePrimitive addVertex(double x, double y) {
93
        return this.addVertex(new Point3DM(x, y, 0, 0));
94
    }
95

    
96
    public OrientablePrimitive addVertex(double x, double y, double z) {
97
        return this.addVertex(new Point3DM(x, y, z, 0));
98
    }
99

    
100
    @Override
101
    protected Point fixPoint(Point point) {
102
        if (point instanceof Point3DM) {
103
            return point;
104
        } else if (point instanceof Point3D) {
105
            return new Point3DM(point.getX(), point.getY(), ((Point3D)point).getZ(), 0);
106
        } else {
107
            return new Point3DM(point.getX(), point.getY(), 0, 0);
108
        }
109
    }
110

    
111
    @Override
112
    protected ArrayListCoordinateSequence getSplineCoordinates() {
113
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
114

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

    
152
    @Override
153
    public MultiPoint toPoints() throws GeometryException {
154
        MultiPoint multiPoint = new MultiPoint3DM();
155
        Coordinate[] theCoordinates = getJTS().getCoordinates();
156
        multiPoint.ensureCapacity(theCoordinates.length);
157
        for (Coordinate theCoordinate : theCoordinates) {
158
            multiPoint.addPoint(new Point3DM(this.getProjection(), theCoordinate));
159
        }
160
        return multiPoint;
161
    }
162

    
163
    @Override
164
    public MultiLine toLines() throws GeometryException {
165
        MultiLine multiLine = new MultiLine3DM();
166
        Line line = new Line3DM(getJTS().getCoordinates());
167
        multiLine.addPrimitive(line);
168
        return multiLine;
169
    }
170

    
171
    @Override
172
    public MultiPolygon toPolygons() throws GeometryException {
173
        MultiPolygon multiPolygon = new MultiPolygon3DM();
174
        Polygon polygon = new Polygon3DM(getJTS().getCoordinates());
175
        multiPolygon.addPrimitive(polygon);
176
        return multiPolygon;
177
    }
178

    
179
    public Point getVertex(int index) {
180
        Point3DM vertex = new Point3DM(this.getProjection(), this.coordinates.get(index));
181
        anyVertex = vertex;
182
        return vertex;
183
    }
184
}