Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2057 / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / Spline2D.java @ 39171

History | View | Annotate | Download (8.35 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 points2d;
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
                points2d = 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
                    points2d.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<points2d.size();i++){
135
                         curve.addVertex(new org.gvsig.fmap.geom.primitive.impl.Point2D((Point2D)points2d.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<points2d.size();i++) {
146
                         handlers.add(new PointHandler(i,
147
                             ((Point2D)points2d.get(i)).getX(),
148
                             ((Point2D)points2d.get(i)).getY()));
149
                 }
150
                 return (Handler[]) handlers.toArray(new Handler[0]);
151
         }
152

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

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

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

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

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

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

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

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

    
282
         }
283

    
284
         /*
285
          * (non-Javadoc)
286
          * @see org.gvsig.fmap.geom.primitive.impl.OrientablePrimitive2D#transform(java.awt.geom.AffineTransform)
287
          */
288
         public void transform(AffineTransform at) {
289
             
290
              if (at == null) {
291
                    return;
292
                }
293

    
294
                 for (int i=0;i<points2d.size();i++) {
295
                         Point2D p= (Point2D)points2d.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
             points2d.add(new java.awt.geom.Point2D.Double(point.getX(), point.getY()));
306
                 gp = getGeneralPathX(points2d);
307
         }
308

    
309

    
310
}