svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Spline2D.java @ 28996
History | View | Annotate | Download (8.41 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.fmap.geom.primitive.impl; |
42 |
|
43 |
import java.awt.geom.AffineTransform; |
44 |
import java.awt.geom.Point2D; |
45 |
import java.util.ArrayList; |
46 |
|
47 |
import org.cresques.cts.IProjection; |
48 |
import org.gvsig.fmap.geom.GeometryLocator; |
49 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
50 |
import org.gvsig.fmap.geom.handler.AbstractHandler; |
51 |
import org.gvsig.fmap.geom.handler.FinalHandler; |
52 |
import org.gvsig.fmap.geom.handler.Handler; |
53 |
import org.gvsig.fmap.geom.primitive.FShape; |
54 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
55 |
import org.gvsig.fmap.geom.primitive.Point; |
56 |
import org.gvsig.fmap.geom.primitive.Spline; |
57 |
import org.gvsig.fmap.geom.type.GeometryType; |
58 |
|
59 |
|
60 |
/**
|
61 |
* Spline2D.
|
62 |
*
|
63 |
* @author Vicente Caballero Navarro
|
64 |
*/
|
65 |
public class Spline2D extends Curve2D implements Spline { |
66 |
private static final long serialVersionUID = -8109393343595560984L; |
67 |
|
68 |
private static GeometryType geomType = GeometryLocator.getGeometryManager() |
69 |
.registerGeometryType(Spline2D.class, null, TYPES.SPLINE, SUBTYPES.GEOM2D);
|
70 |
|
71 |
private ArrayList points; |
72 |
|
73 |
/**
|
74 |
* Constructor without arguments. It is necessary to create
|
75 |
* geometries using the {@link GeometryType}{@link #create()}
|
76 |
* method
|
77 |
*/
|
78 |
public Spline2D() {
|
79 |
super();
|
80 |
points = new ArrayList(); |
81 |
gp = new GeneralPathX();
|
82 |
} |
83 |
/**
|
84 |
* Crea un nuevo FPolyline2D.
|
85 |
*
|
86 |
* @param gpx GeneralPathX.
|
87 |
*/
|
88 |
public Spline2D(String id, IProjection projection, Point2D[] ps) { |
89 |
super(id, projection, getGeneralPathX(ps));
|
90 |
for (int i=0 ; i<ps.length ; i++){ |
91 |
points.add(ps[i]); |
92 |
} |
93 |
} |
94 |
|
95 |
private static GeneralPathX getGeneralPathX(Point2D[] ps) { |
96 |
GeneralPathX gpx=new GeneralPathX();
|
97 |
int num=ps.length;
|
98 |
double[] px=new double[num]; |
99 |
double[] py=new double[num]; |
100 |
for (int i=0;i<num;i++) { |
101 |
Point2D p=ps[i];
|
102 |
px[i]=p.getX(); |
103 |
py[i]=p.getY(); |
104 |
|
105 |
} |
106 |
Spline splineX = new Spline(px);
|
107 |
Spline splineY = new Spline(py);
|
108 |
gpx.moveTo(px[0],py[0]); |
109 |
for (int i = 0; i < px.length - 1; i++) { |
110 |
for (int t = 1; t < 31; t++) { |
111 |
double x1 = splineX.fn(i, ((double) t) / 30.0); |
112 |
double y1 = splineY.fn(i, ((double) t) / 30.0); |
113 |
gpx.lineTo(x1,y1); |
114 |
} |
115 |
} |
116 |
if (ps[0].getX()==ps[ps.length-1].getX() && ps[0].getY()==ps[ps.length-1].getY()) |
117 |
gpx.closePath(); |
118 |
return gpx;
|
119 |
} |
120 |
|
121 |
private static GeneralPathX getGeneralPathX(ArrayList ps) { |
122 |
Point2D[] _ps = new Point2D[ps.size()]; |
123 |
for (int i=0 ; i<ps.size() ; i++){ |
124 |
_ps[i] = (Point2D)ps.get(i);
|
125 |
} |
126 |
return getGeneralPathX(_ps);
|
127 |
} |
128 |
|
129 |
/*
|
130 |
* (non-Javadoc)
|
131 |
* @see org.gvsig.fmap.geom.primitive.impl.Curve2D#getShapeType()
|
132 |
*/
|
133 |
public int getShapeType() { |
134 |
return TYPES.CURVE;
|
135 |
} |
136 |
|
137 |
/* (non-Javadoc)
|
138 |
* @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
|
139 |
*/
|
140 |
public FShape cloneFShape() {
|
141 |
Spline2D curve = new Spline2D();
|
142 |
for (int i=0;i<points.size();i++){ |
143 |
curve.addVertex(new org.gvsig.fmap.geom.primitive.impl.Point2D((Point2D)points.get(i))); |
144 |
} |
145 |
return (FShape)curve;
|
146 |
} |
147 |
|
148 |
/* (non-Javadoc)
|
149 |
* @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
|
150 |
*/
|
151 |
public Handler[] getStretchingHandlers() { |
152 |
ArrayList handlers = new ArrayList(); |
153 |
for (int i=0;i<points.size();i++) { |
154 |
handlers.add(new PointHandler(i, ((Point)points.get(i)).getX(), ((Point)points.get(i)).getY())); |
155 |
} |
156 |
return (Handler[]) handlers.toArray(new Handler[0]); |
157 |
} |
158 |
|
159 |
/* (non-Javadoc)
|
160 |
* @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
|
161 |
*/
|
162 |
public Handler[] getSelectHandlers() { |
163 |
ArrayList handlers = new ArrayList(); |
164 |
for (int i=0;i<points.size();i++) { |
165 |
Point2D p=((Point2D)points.get(i)); |
166 |
handlers.add(new PointSelHandler(i, p.getX(), p.getY()));
|
167 |
} |
168 |
return (Handler[]) handlers.toArray(new Handler[0]); |
169 |
} |
170 |
|
171 |
/**
|
172 |
* DOCUMENT ME!
|
173 |
*
|
174 |
* @author Vicente Caballero Navarro
|
175 |
*/
|
176 |
class PointHandler extends AbstractHandler implements FinalHandler{ |
177 |
/**
|
178 |
* Crea un nuevo PointHandler.
|
179 |
*
|
180 |
* @param x DOCUMENT ME!
|
181 |
* @param y DOCUMENT ME!
|
182 |
*/
|
183 |
public PointHandler(int i,double x, double y) { |
184 |
point = new Point2D.Double(x,y); |
185 |
index=i; |
186 |
} |
187 |
|
188 |
/**
|
189 |
* DOCUMENT ME!
|
190 |
*
|
191 |
* @param x DOCUMENT ME!
|
192 |
* @param y DOCUMENT ME!
|
193 |
*
|
194 |
* @return DOCUMENT ME!
|
195 |
*/
|
196 |
public void move(double x, double y) { |
197 |
point.setLocation(point.getX()+x,point.getY()+y); |
198 |
//TODO falta actualizar el GeneralPathX
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
|
203 |
*/
|
204 |
public void set(double x, double y) { |
205 |
point.setLocation(x,y); |
206 |
//TODO falta actualizar el GeneralPathX
|
207 |
} |
208 |
} |
209 |
/**
|
210 |
* DOCUMENT ME!
|
211 |
*
|
212 |
* @author Vicente Caballero Navarro
|
213 |
*/
|
214 |
class PointSelHandler extends AbstractHandler implements FinalHandler{ |
215 |
/**
|
216 |
* Crea un nuevo PointHandler.
|
217 |
*
|
218 |
* @param x DOCUMENT ME!
|
219 |
* @param y DOCUMENT ME!
|
220 |
*/
|
221 |
public PointSelHandler(int i,double x, double y) { |
222 |
point = new Point2D.Double(x,y); |
223 |
index=i; |
224 |
} |
225 |
|
226 |
/**
|
227 |
* DOCUMENT ME!
|
228 |
*
|
229 |
* @param x DOCUMENT ME!
|
230 |
* @param y DOCUMENT ME!
|
231 |
*
|
232 |
* @return DOCUMENT ME!
|
233 |
*/
|
234 |
public void move(double x, double y) { |
235 |
point.setLocation(point.getX()+x,point.getY()+y); |
236 |
((Point)points.get(index)).setX(point.getX());
|
237 |
((Point)points.get(index)).setY(point.getY());
|
238 |
gp = getGeneralPathX(points); |
239 |
} |
240 |
|
241 |
/**
|
242 |
* @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
|
243 |
*/
|
244 |
public void set(double x, double y) { |
245 |
point.setLocation(x,y); |
246 |
((Point)points.get(index)).setX(point.getX());
|
247 |
((Point)points.get(index)).setY(point.getY());
|
248 |
gp=getGeneralPathX(points); |
249 |
} |
250 |
} |
251 |
static class Spline { |
252 |
private double y[]; |
253 |
private double y2[]; |
254 |
|
255 |
/**
|
256 |
* The constructor calculates the second derivatives of the interpolating function
|
257 |
* at the tabulated points xi, with xi = (i, y[i]).
|
258 |
* Based on numerical recipes in C, http://www.library.cornell.edu/nr/bookcpdf/c3-3.pdf .
|
259 |
* @param y Array of y coordinates for cubic-spline interpolation.
|
260 |
*/
|
261 |
public Spline(double y[]) { |
262 |
this.y = y;
|
263 |
int n = y.length;
|
264 |
y2 = new double[n]; |
265 |
double u[] = new double[n]; |
266 |
for (int i = 1; i < n - 1; i++) { |
267 |
y2[i] = -1.0 / (4.0 + y2[i - 1]); |
268 |
u[i] = (6.0 * (y[i + 1] - 2.0 * y[i] + y[i - 1]) - u[i - 1]) / (4.0 + y2[i - 1]); |
269 |
} |
270 |
for (int i = n - 2; i >= 0; i--) { |
271 |
y2[i] = y2[i] * y2[i + 1] + u[i];
|
272 |
} |
273 |
} |
274 |
|
275 |
/**
|
276 |
* Returns a cubic-spline interpolated value y for the point between
|
277 |
* point (n, y[n]) and (n+1, y[n+1), with t ranging from 0 for (n, y[n])
|
278 |
* to 1 for (n+1, y[n+1]).
|
279 |
* @param n The start point.
|
280 |
* @param t The distance to the next point (0..1).
|
281 |
* @return A cubic-spline interpolated value.
|
282 |
*/
|
283 |
public double fn(int n, double t) { |
284 |
return t * y[n + 1] - ((t - 1.0) * t * ((t - 2.0) * y2[n] - (t + 1.0) * y2[n + 1])) / 6.0 + y[n] - t * y[n]; |
285 |
} |
286 |
|
287 |
} |
288 |
|
289 |
/*
|
290 |
* (non-Javadoc)
|
291 |
* @see org.gvsig.fmap.geom.primitive.impl.OrientablePrimitive2D#transform(java.awt.geom.AffineTransform)
|
292 |
*/
|
293 |
public void transform(AffineTransform at) { |
294 |
for (int i=0;i<points.size();i++) { |
295 |
Point2D p= (Point2D)points.get(i); |
296 |
at.transform(p, p); |
297 |
} |
298 |
gp.transform(at); |
299 |
} |
300 |
|
301 |
/* (non-Javadoc)
|
302 |
* @see org.gvsig.fmap.geom.primitive.Curve2D#addPoint(org.gvsig.fmap.geom.primitive.Point)
|
303 |
*/
|
304 |
public void addVertex(Point point) { |
305 |
points.add(new java.awt.geom.Point2D.Double(point.getX(), point.getY()));
|
306 |
gp = getGeneralPathX(points); |
307 |
} |
308 |
|
309 |
|
310 |
} |