Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / SplineCADTool.java @ 23993

History | View | Annotate | Download (9.81 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 com.iver.cit.gvsig.gui.cad.tools;
42

    
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.event.MouseEvent;
47
import java.awt.geom.Point2D;
48
import java.util.ArrayList;
49

    
50
import org.apache.log4j.Logger;
51

    
52
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
53
import com.iver.andami.PluginServices;
54
import com.iver.andami.messages.NotificationManager;
55
import com.iver.cit.gvsig.fmap.ViewPort;
56
import com.iver.cit.gvsig.fmap.core.FShape;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
59
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
60
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
61
import com.iver.cit.gvsig.gui.cad.tools.smc.SplineCADToolContext;
62
import com.iver.cit.gvsig.gui.cad.tools.smc.SplineCADToolContext.SplineCADToolState;
63

    
64

    
65
/**
66
 * CADTool Spline
67
 *
68
 * @author Vicente Caballero Navarro
69
 */
70
public class SplineCADTool extends DefaultCADTool {
71
        private static Logger logger = Logger.getLogger(SplineCADTool.class.getName());
72
    protected SplineCADToolContext _fsm;
73
    protected ArrayList list = new ArrayList();
74
        protected boolean close=false;
75

    
76
    /**
77
     * Crea un nuevo SplineCADTool.
78
     */
79
    public SplineCADTool() {
80

    
81
    }
82

    
83
    /**
84
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
85
     * carga previa a la utilizaci?n de la herramienta.
86
     */
87
    public void init() {
88
        _fsm = new SplineCADToolContext(this);
89
    }
90

    
91
    public void endGeometry() {
92
        try {
93
                        if (getVLE().getVEA().getShapeType()==FShape.POLYGON && !close){
94
                                closeGeometry();
95
                        }
96
                } catch (ReadDriverException e) {
97
                        NotificationManager.addError(e.getMessage(),e);
98
                }
99

    
100
        // No queremos guardar FGeometryCollections:
101
        IGeometry newGeom = ShapeFactory.createSpline2D((Point2D[])list.toArray(new Point2D[0]));
102
        addGeometry(newGeom);
103
        _fsm = new SplineCADToolContext(this);
104
        list.clear();
105
        clearTemporalCache();
106
        close=false;
107
    }
108
    public void closeGeometry(){
109
            close=true;
110
        Point2D endPoint=new Point2D.Double(((Point2D)list.get(0)).getX(),((Point2D)list.get(0)).getY());
111
        list.add(endPoint);
112
    }
113
    /* (non-Javadoc)
114
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
115
     */
116
    public void transition(double x, double y, InputEvent event) {
117
        _fsm.addPoint(x, y, event);
118
    }
119

    
120
    /* (non-Javadoc)
121
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
122
     */
123
    public void transition(double d) {
124
        _fsm.addValue(d);
125
    }
126

    
127
    /* (non-Javadoc)
128
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
129
     */
130
    public void transition(String s) throws CommandException {
131
        if (!super.changeCommand(s)){
132
            _fsm.addOption(s);
133
        }
134
    }
135

    
136
    /**
137
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
138
     * editableFeatureSource que ya estar? creado.
139
     *
140
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
141
     * @param x par?metro x del punto que se pase en esta transici?n.
142
     * @param y par?metro y del punto que se pase en esta transici?n.
143
     */
144
    public void addPoint(double x, double y,InputEvent event) {
145
        SplineCADToolState actualState = (SplineCADToolState) _fsm.getPreviousState();
146
        String status = actualState.getName();
147
        if (status.equals("Spline.NextPoint") || status.equals("Spline.FirstPoint")) {
148
           list.add(new Point2D.Double(x,y));
149
           IGeometry spline=ShapeFactory.createSpline2D((Point2D[])list.toArray(new Point2D[0]));
150
           addTemporalCache(spline);
151
        }
152
    }
153

    
154
    /**
155
     * M?todo para dibujar lo necesario para el estado en el que nos
156
     * encontremos.
157
     *
158
     * @param g Graphics sobre el que dibujar.
159
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
160
     * @param x par?metro x del punto que se pase para dibujar.
161
     * @param y par?metro x del punto que se pase para dibujar.
162
     */
163
    public void drawOperation(Graphics g, double x,
164
        double y) {
165
        SplineCADToolState actualState = _fsm.getState();
166
        String status = actualState.getName();
167
        if (status.equals("Spline.NextPoint") || status.equals("Spline.FirstPoint")) {
168
                ArrayList points=new ArrayList();
169
                points.addAll(list);
170
                points.add(new Point2D.Double(x,y));
171
                IGeometry spline=ShapeFactory.createSpline2D((Point2D[])points.toArray(new Point2D[0]).clone());
172
                ViewPort vp=getCadToolAdapter().getMapControl().getViewPort();
173
                spline.draw((Graphics2D)g,vp,DefaultCADTool.selectionSymbol);
174
        }
175
    }
176

    
177
    /**
178
     * Dibuja el arco sobre el graphics.
179
     *
180
     * @param point Puntero del rat?n.
181
     * @param lastp ?ltimo punto de la polilinea.
182
     * @param antp Punto antepenultimo.
183
     * @param g Graphics sobre el que se dibuja.
184
     */
185
//    private void drawSpline(ArrayList ps, double x, double y, Graphics g) {
186
//            int num=ps.size()+1;
187
//            ArrayList points=new ArrayList();
188
//            points.addAll(list);
189
//            points.add(new Point2D.Double(x,y));
190
//            double[] px=new double[num];
191
//            double[] py=new double[num];
192
//            for (int i=0;i<num;i++) {
193
//                    Point2D p=(Point2D)points.get(i);
194
//                    px[i]=p.getX();
195
//                    py[i]=p.getY();
196
//
197
//            }
198
//            Spline splineX = new Spline(px);
199
//        Spline splineY = new Spline(py);
200
//        double x0 = 0;
201
//        double y0 = 0;
202
//        ViewPort vp=getCadToolAdapter().getMapControl().getViewPort();
203
//        for (int i = 0; i < px.length - 1; i++) {
204
//            x0 = splineX.fn(i, 0);
205
//            y0 = splineY.fn(i, 0);
206
//            Point2D p0=vp.fromMapPoint(x0,y0);
207
//            g.fillRect((int)p0.getX() - 4, (int)p0.getY() - 4, 8, 8);
208
//            for (int t = 1; t < 31; t++) {
209
//                double x1 = splineX.fn(i, ((double) t) / 30.0);
210
//                double y1 = splineY.fn(i, ((double) t) / 30.0);
211
//                Point2D p1=vp.fromMapPoint(x1,y1);
212
//                g.drawLine((int)p0.getX(), (int)p0.getY(), (int)p1.getX(), (int)p1.getY());
213
//                p0=p1;
214
//                //x0 = x1;
215
//                //y0 = y1;
216
//            }
217
//        }
218
//    }
219

    
220
    /**
221
     * Add a diferent option.
222
     *
223
     * @param sel DOCUMENT ME!
224
     * @param s Diferent option.
225
     */
226
    public void addOption(String s) {
227
    }
228

    
229
    /* (non-Javadoc)
230
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
231
     */
232
    public void addValue(double d) {
233
    }
234

    
235
    public void cancel(){
236
        list.clear();
237
        clearTemporalCache();
238
        close=false;
239
    }
240

    
241
    public void end() {
242
        /* CADExtension.setCADTool("Spline");
243
        PluginServices.getMainFrame().setSelectedTool("Spline"); */
244
    }
245

    
246
    public String getName() {
247
        return PluginServices.getText(this,"Spline_");
248
    }
249

    
250
    public String toString() {
251
        return "_Spline";
252
    }
253
    public boolean isApplicable(int shapeType) {
254
        switch (shapeType) {
255
        case FShape.POINT:
256
        case FShape.MULTIPOINT:
257
            return false;
258
        }
259
        return true;
260
    }
261
    public void endTransition(double x, double y, MouseEvent event) {
262
                _fsm.endPoint(x, y, event);
263
        }
264
//    private class Spline {
265
//            private double y[];
266
//            private double y2[];
267
//
268
//            /**
269
//             * The constructor calculates the second derivatives of the interpolating function
270
//             * at the tabulated points xi, with xi = (i, y[i]).
271
//             * Based on numerical recipes in C, http://www.library.cornell.edu/nr/bookcpdf/c3-3.pdf .
272
//             * @param y Array of y coordinates for cubic-spline interpolation.
273
//             */
274
//            public Spline(double y[]) {
275
//                    this.y = y;
276
//                    int n = y.length;
277
//                    y2 = new double[n];
278
//                    double u[] = new double[n];
279
//                    for (int i = 1; i < n - 1; i++) {
280
//                            y2[i] = -1.0 / (4.0 + y2[i - 1]);
281
//                            u[i] = (6.0 * (y[i + 1] - 2.0 * y[i] + y[i - 1]) - u[i - 1]) / (4.0 + y2[i - 1]);
282
//                    }
283
//                    for (int i = n - 2; i >= 0; i--) {
284
//                            y2[i] = y2[i] * y2[i + 1] + u[i];
285
//                    }
286
//            }
287
//
288
//            /**
289
//             * Returns a cubic-spline interpolated value y for the point between
290
//             * point (n, y[n]) and (n+1, y[n+1), with t ranging from 0 for (n, y[n])
291
//             * to 1 for (n+1, y[n+1]).
292
//             * @param n The start point.
293
//             * @param t The distance to the next point (0..1).
294
//             * @return A cubic-spline interpolated value.
295
//             */
296
//            public double fn(int n, double t) {
297
//                    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];
298
//            }
299
//
300
//    }
301

    
302

    
303
}