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 / Spline2DM.java @ 47432
History | View | Annotate | Download (8.73 KB)
1 | 42267 | 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 com.vividsolutions.jts.geom.Coordinate; |
||
26 | import com.vividsolutions.jts.geom.CoordinateSequence; |
||
27 | 45762 | fdiaz | import com.vividsolutions.jts.operation.buffer.BufferParameters; |
28 | import java.util.ArrayList; |
||
29 | 44617 | jjdelcerro | import java.util.Arrays; |
30 | 42267 | fdiaz | import org.gvsig.fmap.geom.Geometry; |
31 | 42269 | fdiaz | 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.MultiLine2DM; |
||
36 | import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2DM; |
||
37 | import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2DM; |
||
38 | import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2DM; |
||
39 | 42267 | fdiaz | import org.gvsig.fmap.geom.jts.primitive.point.Point2DM; |
40 | 42269 | fdiaz | import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2DM; |
41 | 42267 | fdiaz | import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
42 | import org.gvsig.fmap.geom.jts.util.JTSUtils; |
||
43 | 42441 | fdiaz | import org.gvsig.fmap.geom.jts.util.OpenJUMPUtils; |
44 | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
||
45 | import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
||
46 | 42269 | fdiaz | import org.gvsig.fmap.geom.primitive.Line; |
47 | 44617 | jjdelcerro | import org.gvsig.fmap.geom.primitive.OrientablePrimitive; |
48 | 42267 | fdiaz | import org.gvsig.fmap.geom.primitive.Point; |
49 | 42269 | fdiaz | import org.gvsig.fmap.geom.primitive.Polygon; |
50 | 42304 | fdiaz | import org.gvsig.fmap.geom.primitive.Spline; |
51 | 42267 | fdiaz | |
52 | |||
53 | /**
|
||
54 | * @author fdiaz
|
||
55 | *
|
||
56 | */
|
||
57 | 42304 | fdiaz | public class Spline2DM extends BaseSpline2DM implements Spline { |
58 | 42267 | fdiaz | |
59 | /**
|
||
60 | 42271 | fdiaz | *
|
61 | */
|
||
62 | private static final long serialVersionUID = 3275115053567971975L; |
||
63 | |||
64 | 42283 | fdiaz | public Spline2DM() {
|
65 | 42304 | fdiaz | super(Geometry.TYPES.SPLINE);
|
66 | 42267 | fdiaz | } |
67 | |||
68 | /**
|
||
69 | *
|
||
70 | 44617 | jjdelcerro | * @param coordinates
|
71 | 42267 | fdiaz | */
|
72 | public Spline2DM(Coordinate[] coordinates) { |
||
73 | 42304 | fdiaz | super(Geometry.TYPES.SPLINE, coordinates);
|
74 | 42267 | fdiaz | } |
75 | |||
76 | 47432 | fdiaz | public Spline2DM(ArrayListCoordinateSequence coordinates) {
|
77 | super(Geometry.TYPES.SPLINE, coordinates);
|
||
78 | } |
||
79 | |||
80 | 44617 | jjdelcerro | @Override
|
81 | public OrientablePrimitive addVertex(double x, double y) { |
||
82 | return this.addVertex(new Point2DM(x, y, 0)); |
||
83 | } |
||
84 | 42267 | fdiaz | |
85 | 44617 | jjdelcerro | @Override
|
86 | public OrientablePrimitive addVertex(double x, double y, double z) { |
||
87 | 42267 | fdiaz | String message = "Can't add x,y,z coordinate to Polygon2DM."; |
88 | notifyDeprecated(message); |
||
89 | throw new UnsupportedOperationException(message); |
||
90 | } |
||
91 | |||
92 | 44617 | jjdelcerro | @Override
|
93 | 42267 | fdiaz | public Geometry cloneGeometry() {
|
94 | 47346 | fdiaz | Spline2DM clone = new Spline2DM(cloneCoordinates().toCoordinateArray());
|
95 | clone.setProjection(this.getProjection());
|
||
96 | return clone;
|
||
97 | 42267 | fdiaz | } |
98 | |||
99 | @Override
|
||
100 | protected Point fixPoint(Point point) { |
||
101 | if (point instanceof Point2DM) { |
||
102 | return point;
|
||
103 | } else {
|
||
104 | return new Point2DM(point.getX(), point.getY(), 0); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | @Override
|
||
109 | protected ArrayListCoordinateSequence getSplineCoordinates() {
|
||
110 | ArrayListCoordinateSequence splineCoordinates = new ArrayListCoordinateSequence();
|
||
111 | |||
112 | if (splineCoordinates == null || splineCoordinates.size() == 0) { |
||
113 | int num = coordinates.size();
|
||
114 | double[] px = new double[num]; |
||
115 | double[] py = new double[num]; |
||
116 | double[] pm = new double[num]; |
||
117 | for (int i = 0; i < num; i++) { |
||
118 | Coordinate coord = coordinates.get(i); |
||
119 | px[i] = coord.x; |
||
120 | py[i] = coord.y; |
||
121 | pm[i] = coord.getOrdinate(CoordinateSequence.M); |
||
122 | } |
||
123 | Spline splineX = new Spline(px);
|
||
124 | Spline splineY = new Spline(py);
|
||
125 | Spline splineM = new Spline(pm);
|
||
126 | splineCoordinates.add(coordinates.get(0));
|
||
127 | for (int i = 0; i < coordinates.size() - 1; i++) { |
||
128 | for (int t = 1; t <= SUBSEGMENTS; t++) { |
||
129 | if ((t == SUBSEGMENTS) && (i == (coordinates.size() - 2))) { |
||
130 | // We don't calculate the last point to avoid a possible
|
||
131 | // error precision with floating point numbers.
|
||
132 | splineCoordinates.add(JTSUtils.createMCoordinate(px[px.length - 1], py[px.length - 1], pm[px.length - 1])); |
||
133 | } else {
|
||
134 | double x1 = splineX.fn(i, ((double) t) / SUBSEGMENTS); |
||
135 | double y1 = splineY.fn(i, ((double) t) / SUBSEGMENTS); |
||
136 | double m1 = splineM.fn(i, ((double) t) / SUBSEGMENTS); |
||
137 | splineCoordinates.add(JTSUtils.createMCoordinate(x1, y1, m1)); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | return splineCoordinates;
|
||
143 | } |
||
144 | 42269 | fdiaz | |
145 | 44617 | jjdelcerro | @Override
|
146 | 42269 | fdiaz | public MultiPoint toPoints() throws GeometryException { |
147 | MultiPoint multiPoint = new MultiPoint2DM();
|
||
148 | 44617 | jjdelcerro | Coordinate[] theCoordinates = getJTS().getCoordinates();
|
149 | multiPoint.ensureCapacity(theCoordinates.length); |
||
150 | for (Coordinate theCoordinate : theCoordinates) {
|
||
151 | multiPoint.addPoint(new Point2DM(this.getProjection(), theCoordinate)); |
||
152 | 42269 | fdiaz | } |
153 | return multiPoint;
|
||
154 | } |
||
155 | |||
156 | 44617 | jjdelcerro | @Override
|
157 | 42269 | fdiaz | public MultiLine toLines() throws GeometryException { |
158 | MultiLine multiLine = new MultiLine2DM();
|
||
159 | Line line = new Line2DM(getJTS().getCoordinates()); |
||
160 | multiLine.addPrimitive(line); |
||
161 | return multiLine;
|
||
162 | } |
||
163 | |||
164 | 44617 | jjdelcerro | @Override
|
165 | 42269 | fdiaz | public MultiPolygon toPolygons() throws GeometryException { |
166 | MultiPolygon multiPolygon = new MultiPolygon2DM();
|
||
167 | Polygon polygon = new Polygon2DM(getJTS().getCoordinates()); |
||
168 | multiPolygon.addPrimitive(polygon); |
||
169 | return multiPolygon;
|
||
170 | } |
||
171 | |||
172 | |||
173 | 44617 | jjdelcerro | @Override
|
174 | 42283 | fdiaz | public Point getVertex(int index) { |
175 | 44099 | jjdelcerro | Point2DM vertex = new Point2DM(this.getProjection(), this.coordinates.get(index)); |
176 | 42283 | fdiaz | anyVertex = vertex; |
177 | return vertex;
|
||
178 | } |
||
179 | 42441 | fdiaz | |
180 | 44617 | jjdelcerro | @Override
|
181 | 42441 | fdiaz | public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
182 | Coordinate[] coords = getJTS().getCoordinates();
|
||
183 | 44617 | jjdelcerro | ArrayListCoordinateSequence listCoordSequence = new ArrayListCoordinateSequence(new ArrayList<>()); |
184 | listCoordSequence.addAll(Arrays.asList(coords));
|
||
185 | 42441 | fdiaz | if (isClosed()) {
|
186 | 44099 | jjdelcerro | return JTSUtils.offsetClosedLine(this.getProjection(), listCoordSequence, distance); |
187 | 42441 | fdiaz | } else {
|
188 | 44099 | jjdelcerro | return OpenJUMPUtils.offsetCleanOpenLine(this.getProjection(), listCoordSequence, distance); |
189 | 42441 | fdiaz | } |
190 | } |
||
191 | 42875 | fdiaz | |
192 | @Override
|
||
193 | 45762 | fdiaz | public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
194 | Coordinate[] coords = getJTS().getCoordinates();
|
||
195 | ArrayListCoordinateSequence listCoordSequence = new ArrayListCoordinateSequence(new ArrayList<Coordinate>()); |
||
196 | for (int i = 0; i < coords.length; i++) { |
||
197 | listCoordSequence.add(coords[i]); |
||
198 | } |
||
199 | if (isClosed()) {
|
||
200 | return JTSUtils.offsetClosedLine(this.getProjection(), listCoordSequence, joinStyle, distance); |
||
201 | } else {
|
||
202 | BufferParameters bufParams = JTSUtils.getBufferParameters(joinStyle, BufferParameters.CAP_FLAT); |
||
203 | return OpenJUMPUtils.offsetCleanOpenLine(this.getProjection(), listCoordSequence, bufParams, distance); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | @Override
|
||
208 | 42875 | fdiaz | public boolean equals(Object obj) { |
209 | boolean res = super.equals(obj); |
||
210 | if(res && obj instanceof Spline2DM){ |
||
211 | Spline2DM other = (Spline2DM)obj; |
||
212 | if(this.getNumVertices() != other.getNumVertices()){ |
||
213 | return false; |
||
214 | } |
||
215 | for(int i=0; i < this.getNumVertices(); i++){ |
||
216 | if(other.coordinates.get(i).getOrdinate(2)!=this.coordinates.get(i).getOrdinate(2)){ |
||
217 | return false; |
||
218 | 44617 | jjdelcerro | } |
219 | 42875 | fdiaz | } |
220 | return true; |
||
221 | } else {
|
||
222 | return false; |
||
223 | } |
||
224 | } |
||
225 | 47432 | fdiaz | |
226 | @Override
|
||
227 | public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
228 | Geometry c = this.cloneGeometry();
|
||
229 | return c;
|
||
230 | } |
||
231 | 42267 | fdiaz | } |