Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Spline2D.java @ 38596

History | View | Annotate | Download (8.23 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.handler.AbstractHandler;
49
import org.gvsig.fmap.geom.handler.FinalHandler;
50
import org.gvsig.fmap.geom.handler.Handler;
51
import org.gvsig.fmap.geom.primitive.FShape;
52
import org.gvsig.fmap.geom.primitive.GeneralPathX;
53
import org.gvsig.fmap.geom.primitive.Point;
54
import org.gvsig.fmap.geom.primitive.Spline;
55
import org.gvsig.fmap.geom.type.GeometryType;
56

    
57

    
58
/**
59
 * Spline2D.
60
 *
61
 * @author Vicente Caballero Navarro
62
 */
63
public class Spline2D extends Curve2D implements Spline {
64
        private static final long serialVersionUID = -8109393343595560984L;
65
        private ArrayList points;
66

    
67
        /**
68
         * The constructor with the GeometryType like and argument 
69
         * is used by the {@link GeometryType}{@link #create()}
70
         * to create the geometry
71
         * @param type
72
         * The geometry type
73
         */
74
        public Spline2D(GeometryType geometryType) {
75
                super(geometryType);
76
                points = new ArrayList();
77
                gp = new GeneralPathX();
78
        }        
79
        
80
        Spline2D(GeometryType geometryType, String id, IProjection projection, Point2D[] ps) {
81
                super(geometryType, id, projection, getGeneralPathX(ps));
82
                for (int i=0 ; i<ps.length ; i++){
83
                        points.add(ps[i]);
84
                }
85
        }
86

    
87
        private static GeneralPathX getGeneralPathX(Point2D[] ps) {
88
                GeneralPathX gpx=new GeneralPathX();
89
                int num=ps.length;
90
                double[] px=new double[num];
91
                double[] py=new double[num];
92
                for (int i=0;i<num;i++) {
93
                        Point2D p=ps[i];
94
                        px[i]=p.getX();
95
                        py[i]=p.getY();
96

    
97
                }
98
                Spline splineX = new Spline(px);
99
                Spline splineY = new Spline(py);
100
                gpx.moveTo(px[0],py[0]);
101
                for (int i = 0; i < px.length - 1; i++) {
102
                        for (int t = 1; t < 31; t++) {
103
                                double x1 = splineX.fn(i, ((double) t) / 30.0);
104
                                double y1 = splineY.fn(i, ((double) t) / 30.0);
105
                                gpx.lineTo(x1,y1);
106
                        }
107
                }
108
                if (ps[0].getX()==ps[ps.length-1].getX() && ps[0].getY()==ps[ps.length-1].getY())
109
                        gpx.closePath();
110
                return gpx;
111
        }
112

    
113
        private static GeneralPathX getGeneralPathX(ArrayList ps) {
114
                Point2D[] _ps  = new Point2D[ps.size()];
115
                for (int i=0 ; i<ps.size() ; i++){
116
                        _ps[i] = (Point2D)ps.get(i);
117
                }
118
                return getGeneralPathX(_ps);
119
        }
120

    
121
        /*
122
         * (non-Javadoc)
123
         * @see org.gvsig.fmap.geom.primitive.impl.Curve2D#getShapeType()
124
         */
125
         public int getShapeType() {
126
                 return TYPES.CURVE;
127
         }
128

    
129
         /* (non-Javadoc)
130
          * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
131
          */
132
         public FShape cloneFShape() {
133
                 Spline2D curve = new Spline2D(getGeometryType());
134
                 for (int i=0;i<points.size();i++){
135
                         curve.addVertex(new org.gvsig.fmap.geom.primitive.impl.Point2D((Point2D)points.get(i)));
136
                 }
137
                 return (FShape)curve;
138
         }
139

    
140
         /* (non-Javadoc)
141
          * @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
142
          */
143
         public Handler[] getStretchingHandlers() {
144
                 ArrayList handlers = new ArrayList();
145
                 for (int i=0;i<points.size();i++) {
146
                         handlers.add(new PointHandler(i, ((Point)points.get(i)).getX(), ((Point)points.get(i)).getY()));
147
                 }
148
                 return (Handler[]) handlers.toArray(new Handler[0]);
149
         }
150

    
151
         /* (non-Javadoc)
152
          * @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
153
          */
154
         public Handler[] getSelectHandlers() {
155
                 ArrayList handlers = new ArrayList();
156
                 for (int i=0;i<points.size();i++) {
157
                         Point2D p=((Point2D)points.get(i));
158
                         handlers.add(new PointSelHandler(i, p.getX(), p.getY()));
159
                 }
160
                 return (Handler[]) handlers.toArray(new Handler[0]);
161
         }
162

    
163
         /**
164
          * DOCUMENT ME!
165
          *
166
          * @author Vicente Caballero Navarro
167
          */
168
         class PointHandler extends AbstractHandler implements FinalHandler{
169
                 /**
170
                  * Crea un nuevo PointHandler.
171
                  *
172
                  * @param x DOCUMENT ME!
173
                  * @param y DOCUMENT ME!
174
                  */
175
                 public PointHandler(int i,double x, double y) {
176
                         point = new Point2D.Double(x,y);
177
                         index=i;
178
                 }
179

    
180
                 /**
181
                  * DOCUMENT ME!
182
                  *
183
                  * @param x DOCUMENT ME!
184
                  * @param y DOCUMENT ME!
185
                  *
186
                  * @return DOCUMENT ME!
187
                  */
188
                 public void move(double x, double y) {
189
                         point.setLocation(point.getX()+x,point.getY()+y);
190
                         //TODO falta actualizar el GeneralPathX
191
                 }
192

    
193
                 /**
194
                  * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
195
                  */
196
                 public void set(double x, double y) {
197
                         point.setLocation(x,y);
198
                         //TODO falta actualizar el GeneralPathX
199
                 }
200
         }
201
         /**
202
          * DOCUMENT ME!
203
          *
204
          * @author Vicente Caballero Navarro
205
          */
206
         class PointSelHandler extends AbstractHandler implements FinalHandler{
207
                 /**
208
                  * Crea un nuevo PointHandler.
209
                  *
210
                  * @param x DOCUMENT ME!
211
                  * @param y DOCUMENT ME!
212
                  */
213
                 public PointSelHandler(int i,double x, double y) {
214
                         point = new Point2D.Double(x,y);
215
                         index=i;
216
                 }
217

    
218
                 /**
219
                  * DOCUMENT ME!
220
                  *
221
                  * @param x DOCUMENT ME!
222
                  * @param y DOCUMENT ME!
223
                  *
224
                  * @return DOCUMENT ME!
225
                  */
226
                 public void move(double x, double y) {
227
                         point.setLocation(point.getX()+x,point.getY()+y);
228
                         ((Point)points.get(index)).setX(point.getX());
229
                         ((Point)points.get(index)).setY(point.getY());
230
                         gp = getGeneralPathX(points);
231
                 }
232

    
233
                 /**
234
                  * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
235
                  */
236
                 public void set(double x, double y) {
237
                         point.setLocation(x,y);
238
                         ((Point)points.get(index)).setX(point.getX());
239
                         ((Point)points.get(index)).setY(point.getY());
240
                         gp=getGeneralPathX(points);
241
                 }
242
         }
243
         static class Spline {
244
                 private double y[];
245
                 private double y2[];
246

    
247
                 /**
248
                  * The constructor calculates the second derivatives of the interpolating function
249
                  * at the tabulated points xi, with xi = (i, y[i]).
250
                  * Based on numerical recipes in C, http://www.library.cornell.edu/nr/bookcpdf/c3-3.pdf .
251
                  * @param y Array of y coordinates for cubic-spline interpolation.
252
                  */
253
                 public Spline(double y[]) {
254
                         this.y = y;
255
                         int n = y.length;
256
                         y2 = new double[n];
257
                         double u[] = new double[n];
258
                         for (int i = 1; i < n - 1; i++) {
259
                                 y2[i] = -1.0 / (4.0 + y2[i - 1]);
260
                                 u[i] = (6.0 * (y[i + 1] - 2.0 * y[i] + y[i - 1]) - u[i - 1]) / (4.0 + y2[i - 1]);
261
                         }
262
                         for (int i = n - 2; i >= 0; i--) {
263
                                 y2[i] = y2[i] * y2[i + 1] + u[i];
264
                         }
265
                 }
266

    
267
                 /**
268
                  * Returns a cubic-spline interpolated value y for the point between
269
                  * point (n, y[n]) and (n+1, y[n+1), with t ranging from 0 for (n, y[n])
270
                  * to 1 for (n+1, y[n+1]).
271
                  * @param n The start point.
272
                  * @param t The distance to the next point (0..1).
273
                  * @return A cubic-spline interpolated value.
274
                  */
275
                 public double fn(int n, double t) {
276
                         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];
277
                 }
278

    
279
         }
280

    
281
         /*
282
          * (non-Javadoc)
283
          * @see org.gvsig.fmap.geom.primitive.impl.OrientablePrimitive2D#transform(java.awt.geom.AffineTransform)
284
          */
285
         public void transform(AffineTransform at) {
286
                 for (int i=0;i<points.size();i++) {
287
                         Point2D p= (Point2D)points.get(i);
288
                         at.transform(p, p);
289
                 }
290
                 gp.transform(at);
291
         }
292

    
293
         /* (non-Javadoc)
294
          * @see org.gvsig.fmap.geom.primitive.Curve2D#addPoint(org.gvsig.fmap.geom.primitive.Point)
295
          */
296
         public void addVertex(Point point) {
297
                 points.add(new java.awt.geom.Point2D.Double(point.getX(), point.getY()));
298
                 gp = getGeneralPathX(points);
299
         }
300

    
301

    
302
}