root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / SplineCADTool.java @ 9243
History | View | Annotate | Download (9.43 KB)
1 | 8937 | caballero | /* 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.geom.Point2D; |
||
47 | import java.util.ArrayList; |
||
48 | |||
49 | import org.apache.log4j.Logger; |
||
50 | |||
51 | import com.iver.andami.PluginServices; |
||
52 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
53 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
54 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
55 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
56 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
57 | import com.iver.cit.gvsig.gui.cad.CADTool; |
||
58 | import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
||
59 | import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
||
60 | import com.iver.cit.gvsig.gui.cad.tools.smc.SplineCADToolContext; |
||
61 | import com.iver.cit.gvsig.gui.cad.tools.smc.SplineCADToolContext.SplineCADToolState; |
||
62 | |||
63 | |||
64 | /**
|
||
65 | * CADTool Spline
|
||
66 | *
|
||
67 | * @author Vicente Caballero Navarro
|
||
68 | */
|
||
69 | public class SplineCADTool extends DefaultCADTool { |
||
70 | private static Logger logger = Logger.getLogger(SplineCADTool.class.getName()); |
||
71 | private SplineCADToolContext _fsm;
|
||
72 | private ArrayList list = new ArrayList(); |
||
73 | private boolean close=false; |
||
74 | |||
75 | /**
|
||
76 | * Crea un nuevo SplineCADTool.
|
||
77 | */
|
||
78 | public SplineCADTool() {
|
||
79 | |||
80 | } |
||
81 | |||
82 | /**
|
||
83 | * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
||
84 | * carga previa a la utilizaci?n de la herramienta.
|
||
85 | */
|
||
86 | public void init() { |
||
87 | _fsm = new SplineCADToolContext(this); |
||
88 | } |
||
89 | |||
90 | public void endGeometry() { |
||
91 | try {
|
||
92 | if (getVLE().getVEA().getShapeType()==FShape.POLYGON && !close){
|
||
93 | closeGeometry(); |
||
94 | } |
||
95 | } catch (DriverIOException e) {
|
||
96 | e.printStackTrace(); |
||
97 | } |
||
98 | |||
99 | // No queremos guardar FGeometryCollections:
|
||
100 | IGeometry newGeom = ShapeFactory.createSpline2D((Point2D[])list.toArray(new Point2D[0])); |
||
101 | addGeometry(newGeom); |
||
102 | _fsm = new SplineCADToolContext(this); |
||
103 | list.clear(); |
||
104 | close=false;
|
||
105 | } |
||
106 | public void closeGeometry(){ |
||
107 | close=true;
|
||
108 | Point2D endPoint=new Point2D.Double(((Point2D)list.get(0)).getX(),((Point2D)list.get(0)).getY()); |
||
109 | list.add(endPoint); |
||
110 | } |
||
111 | /* (non-Javadoc)
|
||
112 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
||
113 | */
|
||
114 | public void transition(double x, double y, InputEvent event) { |
||
115 | _fsm.addPoint(x, y, event); |
||
116 | } |
||
117 | |||
118 | /* (non-Javadoc)
|
||
119 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
||
120 | */
|
||
121 | public void transition(double d) { |
||
122 | _fsm.addValue(d); |
||
123 | } |
||
124 | |||
125 | /* (non-Javadoc)
|
||
126 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
||
127 | */
|
||
128 | public void transition(String s) throws CommandException { |
||
129 | if (!super.changeCommand(s)){ |
||
130 | _fsm.addOption(s); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /**
|
||
135 | * Equivale al transition del prototipo pero sin pasarle como par?metro el
|
||
136 | * editableFeatureSource que ya estar? creado.
|
||
137 | *
|
||
138 | * @param sel Bitset con las geometr?as que est?n seleccionadas.
|
||
139 | * @param x par?metro x del punto que se pase en esta transici?n.
|
||
140 | * @param y par?metro y del punto que se pase en esta transici?n.
|
||
141 | */
|
||
142 | public void addPoint(double x, double y,InputEvent event) { |
||
143 | SplineCADToolState actualState = (SplineCADToolState) _fsm.getPreviousState(); |
||
144 | String status = actualState.getName();
|
||
145 | if (status.equals("Spline.NextPoint") || status.equals("Spline.FirstPoint")) { |
||
146 | list.add(new Point2D.Double(x,y)); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /**
|
||
151 | * M?todo para dibujar lo necesario para el estado en el que nos
|
||
152 | * encontremos.
|
||
153 | *
|
||
154 | * @param g Graphics sobre el que dibujar.
|
||
155 | * @param selectedGeometries BitSet con las geometr?as seleccionadas.
|
||
156 | * @param x par?metro x del punto que se pase para dibujar.
|
||
157 | * @param y par?metro x del punto que se pase para dibujar.
|
||
158 | */
|
||
159 | public void drawOperation(Graphics g, double x, |
||
160 | double y) {
|
||
161 | SplineCADToolState actualState = _fsm.getState(); |
||
162 | String status = actualState.getName();
|
||
163 | if (status.equals("Spline.NextPoint") || status.equals("Spline.FirstPoint")) { |
||
164 | ArrayList points=new ArrayList(); |
||
165 | points.addAll(list); |
||
166 | points.add(new Point2D.Double(x,y)); |
||
167 | IGeometry spline=ShapeFactory.createSpline2D((Point2D[])points.toArray(new Point2D[0]).clone()); |
||
168 | ViewPort vp=getCadToolAdapter().getMapControl().getViewPort(); |
||
169 | spline.draw((Graphics2D)g,vp,CADTool.drawingSymbol);
|
||
170 | } |
||
171 | } |
||
172 | |||
173 | /**
|
||
174 | * Dibuja el arco sobre el graphics.
|
||
175 | *
|
||
176 | * @param point Puntero del rat?n.
|
||
177 | * @param lastp ?ltimo punto de la polilinea.
|
||
178 | * @param antp Punto antepenultimo.
|
||
179 | * @param g Graphics sobre el que se dibuja.
|
||
180 | */
|
||
181 | // private void drawSpline(ArrayList ps, double x, double y, Graphics g) {
|
||
182 | // int num=ps.size()+1;
|
||
183 | // ArrayList points=new ArrayList();
|
||
184 | // points.addAll(list);
|
||
185 | // points.add(new Point2D.Double(x,y));
|
||
186 | // double[] px=new double[num];
|
||
187 | // double[] py=new double[num];
|
||
188 | // for (int i=0;i<num;i++) {
|
||
189 | // Point2D p=(Point2D)points.get(i);
|
||
190 | // px[i]=p.getX();
|
||
191 | // py[i]=p.getY();
|
||
192 | //
|
||
193 | // }
|
||
194 | // Spline splineX = new Spline(px);
|
||
195 | // Spline splineY = new Spline(py);
|
||
196 | // double x0 = 0;
|
||
197 | // double y0 = 0;
|
||
198 | // ViewPort vp=getCadToolAdapter().getMapControl().getViewPort();
|
||
199 | // for (int i = 0; i < px.length - 1; i++) {
|
||
200 | // x0 = splineX.fn(i, 0);
|
||
201 | // y0 = splineY.fn(i, 0);
|
||
202 | // Point2D p0=vp.fromMapPoint(x0,y0);
|
||
203 | // g.fillRect((int)p0.getX() - 4, (int)p0.getY() - 4, 8, 8);
|
||
204 | // for (int t = 1; t < 31; t++) {
|
||
205 | // double x1 = splineX.fn(i, ((double) t) / 30.0);
|
||
206 | // double y1 = splineY.fn(i, ((double) t) / 30.0);
|
||
207 | // Point2D p1=vp.fromMapPoint(x1,y1);
|
||
208 | // g.drawLine((int)p0.getX(), (int)p0.getY(), (int)p1.getX(), (int)p1.getY());
|
||
209 | // p0=p1;
|
||
210 | // //x0 = x1;
|
||
211 | // //y0 = y1;
|
||
212 | // }
|
||
213 | // }
|
||
214 | // }
|
||
215 | |||
216 | /**
|
||
217 | * Add a diferent option.
|
||
218 | *
|
||
219 | * @param sel DOCUMENT ME!
|
||
220 | * @param s Diferent option.
|
||
221 | */
|
||
222 | public void addOption(String s) { |
||
223 | } |
||
224 | |||
225 | /* (non-Javadoc)
|
||
226 | * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
||
227 | */
|
||
228 | public void addValue(double d) { |
||
229 | } |
||
230 | |||
231 | public void cancel(){ |
||
232 | list.clear(); |
||
233 | close=false;
|
||
234 | } |
||
235 | |||
236 | public void end() { |
||
237 | /* CADExtension.setCADTool("Spline");
|
||
238 | PluginServices.getMainFrame().setSelectedTool("Spline"); */
|
||
239 | } |
||
240 | |||
241 | public String getName() { |
||
242 | return PluginServices.getText(this,"Spline_"); |
||
243 | } |
||
244 | |||
245 | public String toString() { |
||
246 | return "_Spline"; |
||
247 | } |
||
248 | public boolean isApplicable(int shapeType) { |
||
249 | switch (shapeType) {
|
||
250 | case FShape.POINT:
|
||
251 | case FShape.MULTIPOINT:
|
||
252 | return false; |
||
253 | } |
||
254 | return true; |
||
255 | } |
||
256 | |||
257 | // private class Spline {
|
||
258 | // private double y[];
|
||
259 | // private double y2[];
|
||
260 | //
|
||
261 | // /**
|
||
262 | // * The constructor calculates the second derivatives of the interpolating function
|
||
263 | // * at the tabulated points xi, with xi = (i, y[i]).
|
||
264 | // * Based on numerical recipes in C, http://www.library.cornell.edu/nr/bookcpdf/c3-3.pdf .
|
||
265 | // * @param y Array of y coordinates for cubic-spline interpolation.
|
||
266 | // */
|
||
267 | // public Spline(double y[]) {
|
||
268 | // this.y = y;
|
||
269 | // int n = y.length;
|
||
270 | // y2 = new double[n];
|
||
271 | // double u[] = new double[n];
|
||
272 | // for (int i = 1; i < n - 1; i++) {
|
||
273 | // y2[i] = -1.0 / (4.0 + y2[i - 1]);
|
||
274 | // u[i] = (6.0 * (y[i + 1] - 2.0 * y[i] + y[i - 1]) - u[i - 1]) / (4.0 + y2[i - 1]);
|
||
275 | // }
|
||
276 | // for (int i = n - 2; i >= 0; i--) {
|
||
277 | // y2[i] = y2[i] * y2[i + 1] + u[i];
|
||
278 | // }
|
||
279 | // }
|
||
280 | //
|
||
281 | // /**
|
||
282 | // * Returns a cubic-spline interpolated value y for the point between
|
||
283 | // * point (n, y[n]) and (n+1, y[n+1), with t ranging from 0 for (n, y[n])
|
||
284 | // * to 1 for (n+1, y[n+1]).
|
||
285 | // * @param n The start point.
|
||
286 | // * @param t The distance to the next point (0..1).
|
||
287 | // * @return A cubic-spline interpolated value.
|
||
288 | // */
|
||
289 | // public double fn(int n, double t) {
|
||
290 | // 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];
|
||
291 | // }
|
||
292 | //
|
||
293 | // }
|
||
294 | |||
295 | |||
296 | } |