Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / Spline2D.java @ 26866

History | View | Annotate | Download (8.22 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.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.type.GeometryType;
54

    
55

    
56
/**
57
 * Spline2D.
58
 *
59
 * @author Vicente Caballero Navarro
60
 */
61
public class Spline2D extends Curve2D implements Spline {
62

    
63
        private static final long serialVersionUID = 1L;
64

    
65
        private static GeometryType geomType = GeometryLocator.getGeometryManager()
66
        .registerGeometryType(Spline2D.class, null, TYPES.SPLINE,  SUBTYPES.GEOM2D);
67
        public static int CODE = geomType.getId();
68

    
69
        private ArrayList points;
70
        
71
        /**
72
         * Constructor without arguments. It is necessary to create
73
         * geometries using the {@link GeometryType}{@link #create()}
74
         * method
75
         */
76
        public Spline2D() {
77
                super();
78
                points = new ArrayList();
79
                gp = new GeneralPathX();
80
        }
81
        /**
82
         * Crea un nuevo FPolyline2D.
83
         *
84
         * @param gpx GeneralPathX.
85
         */
86
        public Spline2D(String id, IProjection projection, Point2D[] ps) {
87
                super(id, projection, getGeneralPathX(ps));
88
                for (int i=0 ; i<ps.length ; i++){
89
                        points.add(ps[i]);
90
                }
91
        }
92

    
93
        private static GeneralPathX getGeneralPathX(Point2D[] ps) {
94
                GeneralPathX gpx=new GeneralPathX();
95
                int num=ps.length;
96
                double[] px=new double[num];
97
            double[] py=new double[num];
98
            for (int i=0;i<num;i++) {
99
                    Point2D p=ps[i];
100
                    px[i]=p.getX();
101
                    py[i]=p.getY();
102

    
103
            }
104
            Spline splineX = new Spline(px);
105
        Spline splineY = new Spline(py);
106
        gpx.moveTo(px[0],py[0]);
107
        for (int i = 0; i < px.length - 1; i++) {
108
            for (int t = 1; t < 31; t++) {
109
                double x1 = splineX.fn(i, ((double) t) / 30.0);
110
                double y1 = splineY.fn(i, ((double) t) / 30.0);
111
                gpx.lineTo(x1,y1);
112
            }
113
        }
114
        if (ps[0].getX()==ps[ps.length-1].getX() && ps[0].getY()==ps[ps.length-1].getY())
115
                gpx.closePath();
116
                return gpx;
117
        }
118
        
119
        private static GeneralPathX getGeneralPathX(ArrayList ps) {
120
                Point2D[] _ps  = new Point2D[ps.size()];
121
                for (int i=0 ; i<ps.size() ; i++){
122
                        _ps[i] = (Point2D)ps.get(i);
123
                }
124
                return getGeneralPathX(_ps);
125
        }
126

    
127

    
128
        /**
129
         * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
130
         */
131
        public int getShapeType() {
132
                return FShape.LINE;
133
        }
134

    
135
        /* (non-Javadoc)
136
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
137
         */
138
        public FShape cloneFShape() {
139
                Spline2D curve = new Spline2D();
140
                for (int i=0;i<points.size();i++){
141
                        curve.setCoordinateAt(i, 0, ((Point)points.get(i)).getX());
142
                        curve.setCoordinateAt(i, 1, ((Point)points.get(i)).getY());
143
                }
144
                return (FShape)curve;
145
        }
146

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

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

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

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

    
224
                /**
225
                 * DOCUMENT ME!
226
                 *
227
                 * @param x DOCUMENT ME!
228
                 * @param y DOCUMENT ME!
229
                 *
230
                 * @return DOCUMENT ME!
231
                 */
232
                public void move(double x, double y) {
233
                        point.setLocation(point.getX()+x,point.getY()+y);
234
                        ((Point)points.get(index)).setX(point.getX());
235
                        ((Point)points.get(index)).setY(point.getY());
236
                        gp = getGeneralPathX(points);
237
                }
238

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

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

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

    
285
            }
286
         public void transform(AffineTransform at) {
287
                        for (int i=0;i<points.size();i++) {
288
                                Point2D p= (Point2D)points.get(i);
289
                                at.transform(p, p);
290
                        }
291
                        gp.transform(at);
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
}