Statistics
| Revision:

root / branches / gvSIG_CAD / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / SplineCadTool.java @ 3513

History | View | Annotate | Download (6.03 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.geom.Point2D;
46
import java.util.ArrayList;
47

    
48
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
49
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
50
import com.iver.cit.gvsig.fmap.edition.cad.Status;
51
import com.iver.cit.gvsig.fmap.layers.FBitSet;
52
import com.iver.cit.gvsig.gui.cad.CadTool;
53
import com.iver.cit.gvsig.gui.cad.automaton.Spline;
54
import com.iver.fsac.Automaton;
55

    
56

    
57
/**
58
 * DOCUMENT ME!
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62
public class SplineCadTool extends AbstractCadTool {
63
        private static Status[] STATUS = {
64
                        new Status("Precise primer punto(x,y):"),
65
                        new Status("Precise siguiente punto(x,y):"),
66
                };
67
        private Spline splineStatus = new Spline();
68
        private Point2D firstPoint;
69
        private Point2D lastPoint;
70
        
71
        int nParts_=-2;
72
        double[] controlPoints_={20,30,0};
73
        ArrayList points=new ArrayList();
74
        /**
75
         * @see com.iver.cit.gvsig.gui.cad.CadTool#transition(java.lang.String,
76
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
77
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double[])
78
         */
79
        public int transition(String text, EditableFeatureSource editingSource,
80
                FBitSet selectedGeometries, double[] values) {
81
                int ret = splineStatus.transition(text);
82
                int status = splineStatus.getStatus();
83
                if (status == 0) {
84
                } else if (status == 1) {
85
                                firstPoint = new Point2D.Double(values[0], values[1]);
86
                                points.add(firstPoint);
87
                } else if (status == 2) {
88
                        lastPoint= new Point2D.Double(values[0], values[1]);
89
                        points.add(lastPoint);
90
                        nParts_++;
91
                } else if (status==3){
92
                        Point2D[] ps=(Point2D[])points.toArray(new Point2D[0]);
93
                        controlPoints_=new double[ps.length*3];
94
                        for (int i=0;i<ps.length;i++){
95
                                controlPoints_[i]=ps[i].getX();
96
                                controlPoints_[i+1]=ps[i].getY();
97
                                controlPoints_[i+2]=0;
98
                        }
99
                        double[] spline=generate();
100
                }
101
                
102
                return 0;
103
        }
104

    
105
        /**
106
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
107
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
108
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
109
         */
110
        public void drawOperation(Graphics g, EditableFeatureSource efs,
111
                FBitSet selectedGeometries, double x, double y) {
112
                int status = splineStatus.getStatus();
113
                if (status == 1) {
114
                        Point2D[] ps=(Point2D[])points.toArray(new Point2D[0]);
115
                        controlPoints_=new double[ps.length*3];
116
                        for (int i=0;i<ps.length;i+=3){
117
                                controlPoints_[i]=ps[i/3].getX();
118
                                controlPoints_[i+1]=ps[i/3].getY();
119
                                controlPoints_[i+2]=0;
120
                        }
121
                        double[] spline=generate();
122
                        Point2D[] ps2=new Point2D[ps.length];
123
                        for (int i=0;i<ps2.length;i+=3){
124
                                ps2[i/3]=new Point2D.Double(spline[i],spline[i+1]);
125
                        
126
                        }
127
                        for (int i=2;i<ps2.length;i++){
128
                                ShapeFactory.createArc(ps2[i-2],ps2[i-1],ps2[i]).draw((Graphics2D) g,
129
                                                getCadToolAdapter().getMapControl().getViewPort(),
130
                                                CadTool.modifySymbol);
131
                        }
132
                        
133
                } 
134
                        
135
        }
136

    
137
        /**
138
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
139
         */
140
        public String getQuestion() {
141
                return STATUS[splineStatus.getStatus()].getQuestion();
142
        }
143

    
144
        /**
145
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
146
         */
147
        public void initializeStatus() {
148
                splineStatus.initialize();
149
        }
150

    
151
        /**
152
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
153
         */
154
        public Automaton getAutomaton() {
155
                return splineStatus;
156
        }
157
        
158
          
159
          /**
160
           * Generate this spline.
161
           * 
162
           * @return  Coordinates of the spline (x0,y0,z0,x1,y1,z1,...)
163
           */
164
          double[] generate()
165
          {
166
            if (controlPoints_.length < 9) {
167
              double[] copy = new double[controlPoints_.length];
168
              System.arraycopy (controlPoints_, 0, copy, 0, controlPoints_.length);
169
              return copy;
170
            }
171
            
172
            int n = controlPoints_.length / 3;
173
            int length = (n - 3) * nParts_ + 1;
174
            double spline[] = new double[length * 3];
175

    
176
            p (0, 0, controlPoints_, spline, 0);
177
              
178
            int index = 3;
179
            for (int i = 0; i < n - 3; i += 3) {
180
              for (int j = 1; j <= nParts_; j++) {
181
                p (i, j / (double) nParts_, controlPoints_, spline, index);
182
                index += 3;
183
              }
184
            }
185
              
186
            return spline;      
187
          }
188

    
189
            
190
            
191
          private void p (int i, double t, double cp[], double spline[], int index)
192
          {
193
            double x = 0.0;
194
            double y = 0.0;
195
            double z = 0.0;
196
              
197
            int k = i;
198
            for (int j = 0; j <= 3; j++) {
199
              double b = blend (j, t);
200
              
201
              x += b * cp[k++];
202
              y += b * cp[k++];
203
              z += b * cp[k++];
204
            }
205
              
206
            spline[index + 0] = x;
207
            spline[index + 1] = y;
208
            spline[index + 2] = z;    
209
          }
210

    
211

    
212

    
213
          private double blend (int i, double t)
214
          {
215
            if      (i == 0) return (1 - t) * (1 - t) * (1 - t);
216
            else if (i == 1) return 3 * t * (1 - t) * (1 - t);
217
            else if (i == 2) return 3 * t * t * (1 - t);
218
            else             return t * t * t;
219
          }
220

    
221
        /**
222
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getName()
223
         */
224
        public String getName() {
225
                return "SPLINE";
226
        }
227
        
228

    
229

    
230
}
231

    
232

    
233

    
234