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 @ 42307
History | View | Annotate | Download (7.7 KB)
1 | 42304 | fdiaz | /* 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.MCoordinate; |
||
36 | import org.gvsig.fmap.geom.jts.aggregate.MultiLine3DM; |
||
37 | import org.gvsig.fmap.geom.jts.aggregate.MultiPoint3DM; |
||
38 | import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon3DM; |
||
39 | import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3DM; |
||
40 | import org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline.Spline; |
||
41 | import org.gvsig.fmap.geom.jts.primitive.point.Point2DM; |
||
42 | import org.gvsig.fmap.geom.jts.primitive.point.Point3D; |
||
43 | import org.gvsig.fmap.geom.jts.primitive.point.Point3DM; |
||
44 | import org.gvsig.fmap.geom.jts.primitive.point.PointJTS; |
||
45 | import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon3DM; |
||
46 | import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
||
47 | import org.gvsig.fmap.geom.jts.util.JTSUtils; |
||
48 | import org.gvsig.fmap.geom.jts.util.ReadOnlyCoordinates; |
||
49 | import org.gvsig.fmap.geom.primitive.Line; |
||
50 | import org.gvsig.fmap.geom.primitive.Point; |
||
51 | import org.gvsig.fmap.geom.primitive.Polygon; |
||
52 | |||
53 | |||
54 | /**
|
||
55 | * @author fdiaz
|
||
56 | *
|
||
57 | */
|
||
58 | public abstract class BaseSpline3DM extends AbstractSpline { |
||
59 | |||
60 | /**
|
||
61 | *
|
||
62 | */
|
||
63 | private static final long serialVersionUID = -5436087623278868159L; |
||
64 | |||
65 | |||
66 | /**
|
||
67 | * @param polygon
|
||
68 | */
|
||
69 | public BaseSpline3DM(int type) { |
||
70 | super(type, Geometry.SUBTYPES.GEOM3DM);
|
||
71 | this.coordinates = new ArrayListCoordinateSequence(new ArrayList<Coordinate>()); |
||
72 | } |
||
73 | |||
74 | /**
|
||
75 | * @param subtype
|
||
76 | * @param coordinates
|
||
77 | * @param aVertex
|
||
78 | */
|
||
79 | public BaseSpline3DM(int type, Coordinate[] coordinates) { |
||
80 | super(type, Geometry.SUBTYPES.GEOM3DM);
|
||
81 | this.coordinates = new ArrayListCoordinateSequence(new ReadOnlyCoordinates(coordinates)); |
||
82 | if (coordinates.length < 1) { |
||
83 | anyVertex = new Point3DM(0, 0, 0, 0); |
||
84 | } else {
|
||
85 | Coordinate coordinate = coordinates[0];
|
||
86 | anyVertex = new Point3DM(coordinate.x, coordinate.y, coordinate.z, coordinate.getOrdinate(CoordinateSequence.M));
|
||
87 | } |
||
88 | } |
||
89 | |||
90 | /* (non-Javadoc)
|
||
91 | * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
|
||
92 | */
|
||
93 | public void addVertex(double x, double y) { |
||
94 | this.addVertex(new Point3DM(x, y, 0, 0)); |
||
95 | } |
||
96 | |||
97 | /* (non-Javadoc)
|
||
98 | * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
|
||
99 | */
|
||
100 | public void addVertex(double x, double y, double z) { |
||
101 | this.addVertex(new Point3DM(x, y, z, 0)); |
||
102 | } |
||
103 | |||
104 | /* (non-Javadoc)
|
||
105 | * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#fixPoint(org.gvsig.fmap.geom.primitive.Point)
|
||
106 | */
|
||
107 | @Override
|
||
108 | protected Point fixPoint(Point point) { |
||
109 | if (point instanceof Point3DM) { |
||
110 | return point;
|
||
111 | } else if (point instanceof Point3D) { |
||
112 | return new Point3DM(point.getX(), point.getY(), ((Point3D)point).getZ(), 0); |
||
113 | } else {
|
||
114 | return new Point3DM(point.getX(), point.getY(), 0, 0); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /* (non-Javadoc)
|
||
119 | * @see org.gvsig.fmap.geom.jts.primitive.curve.spline.AbstractSpline#getSplineCoordinates()
|
||
120 | */
|
||
121 | @Override
|
||
122 | protected ArrayListCoordinateSequence getSplineCoordinates() {
|
||
123 | ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
|
||
124 | |||
125 | if (splineCoordinates == null || splineCoordinates.size() == 0) { |
||
126 | int num = coordinates.size();
|
||
127 | double[] px = new double[num]; |
||
128 | double[] py = new double[num]; |
||
129 | double[] pz = new double[num]; |
||
130 | double[] pm = new double[num]; |
||
131 | for (int i = 0; i < num; i++) { |
||
132 | Coordinate coord = coordinates.get(i); |
||
133 | px[i] = coord.x; |
||
134 | py[i] = coord.y; |
||
135 | pz[i] = coord.z; |
||
136 | pm[i] = coord.getOrdinate(CoordinateSequence.M); |
||
137 | } |
||
138 | Spline splineX = new Spline(px);
|
||
139 | Spline splineY = new Spline(py);
|
||
140 | Spline splineZ = new Spline(pz);
|
||
141 | Spline splineM = new Spline(pm);
|
||
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(JTSUtils.createMCoordinate(px[px.length - 1], py[px.length - 1], pz[px.length - 1], pm[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 | double m1 = splineM.fn(i, ((double) t) / SUBSEGMENTS); |
||
154 | splineCoordinates.add(JTSUtils.createMCoordinate(x1, y1, z1,m1)); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | return splineCoordinates;
|
||
160 | } |
||
161 | |||
162 | /* (non-Javadoc)
|
||
163 | * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
|
||
164 | */
|
||
165 | public MultiPoint toPoints() throws GeometryException { |
||
166 | MultiPoint multiPoint = new MultiPoint3DM();
|
||
167 | Coordinate[] coordinates = getJTS().getCoordinates();
|
||
168 | multiPoint.ensureCapacity(coordinates.length); |
||
169 | for (int i = 0; i < coordinates.length; i++) { |
||
170 | multiPoint.addPoint(new Point3DM(coordinates[i]));
|
||
171 | } |
||
172 | return multiPoint;
|
||
173 | } |
||
174 | |||
175 | /* (non-Javadoc)
|
||
176 | * @see org.gvsig.fmap.geom.primitive.Line#toLines()
|
||
177 | */
|
||
178 | public MultiLine toLines() throws GeometryException { |
||
179 | MultiLine multiLine = new MultiLine3DM();
|
||
180 | Line line = new Line3DM(getJTS().getCoordinates()); |
||
181 | multiLine.addPrimitive(line); |
||
182 | return multiLine;
|
||
183 | } |
||
184 | |||
185 | /* (non-Javadoc)
|
||
186 | * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
|
||
187 | */
|
||
188 | public MultiPolygon toPolygons() throws GeometryException { |
||
189 | MultiPolygon multiPolygon = new MultiPolygon3DM();
|
||
190 | Polygon polygon = new Polygon3DM(getJTS().getCoordinates()); |
||
191 | multiPolygon.addPrimitive(polygon); |
||
192 | return multiPolygon;
|
||
193 | } |
||
194 | |||
195 | |||
196 | /* (non-Javadoc)
|
||
197 | * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
|
||
198 | */
|
||
199 | public Point getVertex(int index) { |
||
200 | Point3DM vertex = new Point3DM(this.coordinates.get(index)); |
||
201 | anyVertex = vertex; |
||
202 | return vertex;
|
||
203 | } |
||
204 | } |