root / trunk / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / Spline2D.java @ 20899
History | View | Annotate | Download (6.57 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; |
42 |
|
43 |
import java.awt.geom.Point2D; |
44 |
import java.util.ArrayList; |
45 |
|
46 |
import org.cresques.cts.IProjection; |
47 |
import org.gvsig.fmap.geom.handler.AbstractHandler; |
48 |
import org.gvsig.fmap.geom.handler.FinalHandler; |
49 |
import org.gvsig.fmap.geom.handler.Handler; |
50 |
|
51 |
|
52 |
/**
|
53 |
* Spline2D.
|
54 |
*
|
55 |
* @author Vicente Caballero Navarro
|
56 |
*/
|
57 |
public class Spline2D extends Curve2D implements Curve { |
58 |
|
59 |
private static final long serialVersionUID = 1L; |
60 |
private Point2D[] points; |
61 |
/**
|
62 |
* Crea un nuevo FPolyline2D.
|
63 |
*
|
64 |
* @param gpx GeneralPathX.
|
65 |
*/
|
66 |
public Spline2D(String id, IProjection projection, Point2D[] ps) { |
67 |
super(id, projection, getGeneralPathX(ps));
|
68 |
points=ps; |
69 |
} |
70 |
|
71 |
private static GeneralPathX getGeneralPathX(Point2D[] ps) { |
72 |
GeneralPathX gpx=new GeneralPathX();
|
73 |
int num=ps.length;
|
74 |
double[] px=new double[num]; |
75 |
double[] py=new double[num]; |
76 |
for (int i=0;i<num;i++) { |
77 |
Point2D p=ps[i];
|
78 |
px[i]=p.getX(); |
79 |
py[i]=p.getY(); |
80 |
|
81 |
} |
82 |
Spline splineX = new Spline(px);
|
83 |
Spline splineY = new Spline(py);
|
84 |
gpx.moveTo(px[0],py[0]); |
85 |
for (int i = 0; i < px.length - 1; i++) { |
86 |
for (int t = 1; t < 31; t++) { |
87 |
double x1 = splineX.fn(i, ((double) t) / 30.0); |
88 |
double y1 = splineY.fn(i, ((double) t) / 30.0); |
89 |
gpx.lineTo(x1,y1); |
90 |
} |
91 |
} |
92 |
if (ps[0].getX()==ps[ps.length-1].getX() && ps[0].getY()==ps[ps.length-1].getY()) |
93 |
gpx.closePath(); |
94 |
return gpx;
|
95 |
} |
96 |
|
97 |
|
98 |
/**
|
99 |
* @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
|
100 |
*/
|
101 |
public int getShapeType() { |
102 |
return FShape.LINE;
|
103 |
} |
104 |
|
105 |
/* (non-Javadoc)
|
106 |
* @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
|
107 |
*/
|
108 |
public FShape cloneFShape() {
|
109 |
return new Spline2D(id, projection, points); |
110 |
} |
111 |
|
112 |
/* (non-Javadoc)
|
113 |
* @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
|
114 |
*/
|
115 |
public Handler[] getStretchingHandlers() { |
116 |
ArrayList handlers = new ArrayList(); |
117 |
for (int i=0;i<points.length;i++) { |
118 |
handlers.add(new PointHandler(i, points[i].getX(), points[i].getY()));
|
119 |
} |
120 |
return (Handler[]) handlers.toArray(new Handler[0]); |
121 |
} |
122 |
/* (non-Javadoc)
|
123 |
* @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
|
124 |
*/
|
125 |
public Handler[] getSelectHandlers() { |
126 |
ArrayList handlers = new ArrayList(); |
127 |
for (int i=0;i<points.length;i++) { |
128 |
Point2D p=points[i];
|
129 |
handlers.add(new PointSelHandler(i, p.getX(), p.getY()));
|
130 |
} |
131 |
return (Handler[]) handlers.toArray(new Handler[0]); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* DOCUMENT ME!
|
136 |
*
|
137 |
* @author Vicente Caballero Navarro
|
138 |
*/
|
139 |
class PointHandler extends AbstractHandler implements FinalHandler{ |
140 |
/**
|
141 |
* Crea un nuevo PointHandler.
|
142 |
*
|
143 |
* @param x DOCUMENT ME!
|
144 |
* @param y DOCUMENT ME!
|
145 |
*/
|
146 |
public PointHandler(int i,double x, double y) { |
147 |
point = new Point2D.Double(x,y); |
148 |
index=i; |
149 |
} |
150 |
|
151 |
/**
|
152 |
* DOCUMENT ME!
|
153 |
*
|
154 |
* @param x DOCUMENT ME!
|
155 |
* @param y DOCUMENT ME!
|
156 |
*
|
157 |
* @return DOCUMENT ME!
|
158 |
*/
|
159 |
public void move(double x, double y) { |
160 |
point.setLocation(point.getX()+x,point.getY()+y); |
161 |
//TODO falta actualizar el GeneralPathX
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
|
166 |
*/
|
167 |
public void set(double x, double y) { |
168 |
point.setLocation(x,y); |
169 |
//TODO falta actualizar el GeneralPathX
|
170 |
} |
171 |
} |
172 |
/**
|
173 |
* DOCUMENT ME!
|
174 |
*
|
175 |
* @author Vicente Caballero Navarro
|
176 |
*/
|
177 |
class PointSelHandler extends AbstractHandler implements FinalHandler{ |
178 |
/**
|
179 |
* Crea un nuevo PointHandler.
|
180 |
*
|
181 |
* @param x DOCUMENT ME!
|
182 |
* @param y DOCUMENT ME!
|
183 |
*/
|
184 |
public PointSelHandler(int i,double x, double y) { |
185 |
point = new Point2D.Double(x,y); |
186 |
index=i; |
187 |
} |
188 |
|
189 |
/**
|
190 |
* DOCUMENT ME!
|
191 |
*
|
192 |
* @param x DOCUMENT ME!
|
193 |
* @param y DOCUMENT ME!
|
194 |
*
|
195 |
* @return DOCUMENT ME!
|
196 |
*/
|
197 |
public void move(double x, double y) { |
198 |
point.setLocation(point.getX()+x,point.getY()+y); |
199 |
points[index]=point; |
200 |
gp=getGeneralPathX(points); |
201 |
} |
202 |
|
203 |
/**
|
204 |
* @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
|
205 |
*/
|
206 |
public void set(double x, double y) { |
207 |
point.setLocation(x,y); |
208 |
points[index]=point; |
209 |
gp=getGeneralPathX(points); |
210 |
} |
211 |
} |
212 |
static class Spline { |
213 |
private double y[]; |
214 |
private double y2[]; |
215 |
|
216 |
/**
|
217 |
* The constructor calculates the second derivatives of the interpolating function
|
218 |
* at the tabulated points xi, with xi = (i, y[i]).
|
219 |
* Based on numerical recipes in C, http://www.library.cornell.edu/nr/bookcpdf/c3-3.pdf .
|
220 |
* @param y Array of y coordinates for cubic-spline interpolation.
|
221 |
*/
|
222 |
public Spline(double y[]) { |
223 |
this.y = y;
|
224 |
int n = y.length;
|
225 |
y2 = new double[n]; |
226 |
double u[] = new double[n]; |
227 |
for (int i = 1; i < n - 1; i++) { |
228 |
y2[i] = -1.0 / (4.0 + y2[i - 1]); |
229 |
u[i] = (6.0 * (y[i + 1] - 2.0 * y[i] + y[i - 1]) - u[i - 1]) / (4.0 + y2[i - 1]); |
230 |
} |
231 |
for (int i = n - 2; i >= 0; i--) { |
232 |
y2[i] = y2[i] * y2[i + 1] + u[i];
|
233 |
} |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Returns a cubic-spline interpolated value y for the point between
|
238 |
* point (n, y[n]) and (n+1, y[n+1), with t ranging from 0 for (n, y[n])
|
239 |
* to 1 for (n+1, y[n+1]).
|
240 |
* @param n The start point.
|
241 |
* @param t The distance to the next point (0..1).
|
242 |
* @return A cubic-spline interpolated value.
|
243 |
*/
|
244 |
public double fn(int n, double t) { |
245 |
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]; |
246 |
} |
247 |
|
248 |
} |
249 |
} |