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