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 / Spline3DM.java @ 42267

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

    
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.fmap.geom.jts.MCoordinate;
30
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D;
31
import org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline.Spline;
32
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
33
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
34
import org.gvsig.fmap.geom.jts.primitive.point.Point3DM;
35
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
36
import org.gvsig.fmap.geom.jts.util.JTSUtils;
37
import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates;
38
import org.gvsig.fmap.geom.primitive.Point;
39

    
40

    
41
/**
42
 * @author fdiaz
43
 *
44
 */
45
public class Spline3DM extends AbstractSpline {
46

    
47
    /**
48
     * @param subtype
49
     */
50
    protected Spline3DM() {
51
        super(Geometry.SUBTYPES.GEOM3DM);
52
    }
53

    
54
    /**
55
    *
56
    */
57
    public Spline3DM(Coordinate[] coordinates) {
58
        this();
59
        this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates));
60
        if (coordinates.length < 1) {
61
            anyVertex = new Point3DM(0, 0, 0, 0);
62
        } else {
63
            Coordinate coordinate = coordinates[0];
64
            anyVertex = new Point3DM(coordinate.x, coordinate.y, coordinate.z, coordinate.getOrdinate(CoordinateSequence.M));
65
        }
66
    }
67

    
68
    /* (non-Javadoc)
69
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
70
     */
71
    public void addVertex(double x, double y) {
72
        this.addVertex(new Point3DM(x, y, 0, 0));
73
        }
74

    
75
    /* (non-Javadoc)
76
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
77
     */
78
    public void addVertex(double x, double y, double z) {
79
        this.addVertex(new Point3DM(x, y, z, 0));
80
    }
81

    
82
    /* (non-Javadoc)
83
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
84
     */
85
    public Geometry cloneGeometry() {
86
        return new Spline3DM((Coordinate[]) coordinates.clone());
87
    }
88

    
89
    /* (non-Javadoc)
90
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#fixPoint(org.gvsig.fmap.geom.primitive.Point)
91
     */
92
    @Override
93
    protected Point fixPoint(Point point) {
94
        if (point instanceof Point3DM) {
95
            return point;
96
        } else if (point instanceof Point3D) {
97
            return new Point3DM(point.getX(), point.getY(), ((Point3D)point).getZ(), 0);
98
        } else {
99
            return new Point3DM(point.getX(), point.getY(), 0, 0);
100
        }
101
    }
102

    
103
    /* (non-Javadoc)
104
     * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#getSplineCoordinates()
105
     */
106
    @Override
107
    protected ArrayListCoordinateSequence getSplineCoordinates() {
108
        ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
109

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