Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / beans / canvas / layers / functions / LogaritmicExponentialLine.java @ 19969

History | View | Annotate | Download (6.48 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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
package org.gvsig.raster.beans.canvas.layers.functions;
20

    
21
import java.awt.Color;
22
import java.awt.Cursor;
23
import java.awt.event.MouseEvent;
24
import java.util.ArrayList;
25

    
26
import org.gvsig.raster.beans.canvas.layers.InfoLayer;
27
import org.gvsig.raster.util.MathUtils;
28
import org.gvsig.raster.util.RasterToolsUtil;
29
/**
30
 * Representa una funcion con posibilidad de arrastre para las funciones
31
 * logaritmicas y exponenciales. Con el raton se puede pasar de una a otra
32
 * directamente.
33
 * 
34
 * Las formas mas logicas de uso seria pasandole:
35
 * 1.0: Para una funcion logaritmica
36
 * -1.0: Para una funcion exponencial
37
 * 0.0: Para una funcion lineal
38
 * 
39
 * @version 02/04/2008
40
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
41
 */
42
public class LogaritmicExponentialLine extends StraightLine {
43
        /**
44
         * Numero de puntos que contiene esta funci?n
45
         */
46
        private double num = 40.0;
47
        
48
        /**
49
         * Base de la exponencial
50
         */
51
        private double baseExp = 6.0;
52
        
53
        private double perc = 1.0;
54
        
55
        /**
56
         * Constructor. Asigna el color y establece la posicion de la funcion.
57
         * Los valores normales son 1.0 para logaritmica y -1.0 para exponencial.
58
         * El rango va desde -2.0 hasta 2.0. Siendo 0.0 una funcion lineal.
59
         * @param c
60
         */
61
        public LogaritmicExponentialLine(Color c, double point) {
62
                super(c);
63
                setShowSquares(false);
64

    
65
                perc = point;
66
                recalcList();
67
        }
68

    
69
        /**
70
         * Actualiza la barra informativa para saber en que estado se encuentra el
71
         * componente.
72
         * Cuando el porcentaje es mayor a 0 siempre estamos en la Logaritmica
73
         * Cuando el porcentaje es menor a 0 siempre estamos en la exponencial
74
         * @param perc
75
         */
76
        private void setInfoPoint(Double perc) {
77
                if (canvas != null) {
78
                        ArrayList list = canvas.getDrawableElements(InfoLayer.class);
79
                        if (list.size() > 0) {
80
                                InfoLayer infoLayer = (InfoLayer) list.get(0);
81
                                
82
                                if (perc == null) {
83
                                        infoLayer.setStatusLeft(null);
84
                                        infoLayer.setStatusRight(null);
85
                                        canvas.repaint();
86
                                        return;
87
                                }
88

    
89
                                if (perc.doubleValue() > 0.0)
90
                                        infoLayer.setStatusLeft(RasterToolsUtil.getText(this, "logarithmical"));
91
                                else
92
                                        infoLayer.setStatusLeft(RasterToolsUtil.getText(this, "exponential"));
93

    
94
                                infoLayer.setStatusRight(MathUtils.clipDecimals(Math.abs(perc.doubleValue()*100.0), 2) + "%");
95
                        }
96
                }
97
        }
98
        
99
        /**
100
         * Recalcula todos los puntos de la funcion
101
         * ( 0.0 a  1.0) - Funcion logaritmica con aproximacion al centro
102
         * ( 1.0 a  2.0) - Funcion logaritmica con aproximacion al borde
103
         * ( 0.0 a -1.0) - Funcion exponencial con aproximacion al centro
104
         * (-1.0 a -2.0) - Funcion exponencial con aproximacion al borde
105
         */
106
        private void recalcList() {
107
                double x, y = 0.0;
108

    
109
                setInfoPoint(new Double(perc));
110

    
111
                this.listSquare.clear();
112

    
113
                for (int i = 0; i <= num; i++) {
114
                        x = ((double) i) / num;
115

    
116
                        // Aproximacion al centro de una funcion logaritmica
117
                        if (perc >= 0.0 && perc <= 1.0) {
118
                                y = logFunction(x);
119
                                y = x + ((y - x) * perc);
120
                        }
121

    
122
                        // Aproximacion al borde de una funcion logaritmica
123
                        if (perc > 1.0 && perc <= 2.0) {
124
                                y = logFunction(x);
125
                                y = y + ((1.0 - y) * (perc - 1.0));
126
                        }
127

    
128
                        // Aproximacion al centro de una funcion exponencial
129
                        if (perc >= -1.0 && perc < 0.0) {
130
                                y = expFunction(x);
131
                                y = x - ((x - y) * Math.abs(perc));
132
                        }
133

    
134
                        // Aproximacion al borde de una funcion exponencial
135
                        if (perc >= -2.0 && perc < -1.0) {
136
                                y = expFunction(x);
137
                                y = y * (1.0 - (Math.abs(perc) - 1.0));
138
                        }
139

    
140
                        if (y < 0.0)
141
                                y = 0.0;
142
                        if (y > 1.0)
143
                                y = 1.0;
144

    
145
                        this.listSquare.add(new Square(x, y));
146
                }
147
        }
148
        
149
        /**
150
         * Formula para calcular el valor en y de una funcion logaritmica en x
151
         * @param x
152
         * @return
153
         */
154
        private double logFunction(double x) {
155
                return Math.log10(1.0 + (x * 99.0)) / 2.0;
156
        }
157
        
158
        /**
159
         * Formula para calcular el valor en y de una funcion exponencial en x
160
         * @param x
161
         * @return
162
         */
163
        private double expFunction(double x) {
164
                return (Math.exp(x * baseExp) - 1.0) / (Math.exp(baseExp) - 1.0);
165
        }
166
        
167
        /* (non-Javadoc)
168
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseDragged(java.awt.event.MouseEvent)
169
         */
170
        public boolean mouseDragged(MouseEvent e) {
171
                if (canvas.getCursor().getType() != Cursor.DEFAULT_CURSOR)
172
                        return true;
173

    
174
                double x = pixelToValueX(e.getX());
175
                double y = pixelToValueY(e.getY());
176
                
177
                double y2 = 0.0;
178
                
179
                // Localizamos el punto del raton para pasar un porcentaje correcto y que 
180
                // asi coincida la funcion con el raton
181
                if (y >= x) {
182
                        y2 = logFunction(x);
183
                        if (y < y2)
184
                                perc = (y - x) / (y2 - x);
185
                        else
186
                                perc = ((y - y2) / (1.0 - y2)) + 1.0;
187
                } else {
188
                        y2 = expFunction(x);
189
                        
190
                        if (y > y2)
191
                                perc = -Math.abs((y - x) / (y2 - x));
192
                        else
193
                                perc = -Math.abs((y2 - y) / y2)  - 1.0;
194
                }
195
                
196
                if (perc > 2.0)
197
                        perc = 2.0;
198
                if (perc < -2.0)
199
                        perc = -2.0;
200
                
201
                recalcList();
202
                canvas.repaint();
203
                canvas.callDataDragged("line", this);
204
                return false;
205
        }
206

    
207
        /* (non-Javadoc)
208
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseMoved(java.awt.event.MouseEvent)
209
         */
210
        public boolean mouseMoved(MouseEvent e) {
211
                return true;
212
        }
213

    
214
        /* (non-Javadoc)
215
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mousePressed(java.awt.event.MouseEvent)
216
         */
217
        public boolean mousePressed(MouseEvent e) {
218
                return mouseDragged(e);
219
        }
220

    
221
        /* (non-Javadoc)
222
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseReleased(java.awt.event.MouseEvent)
223
         */
224
        public boolean mouseReleased(MouseEvent e) {
225
                setInfoPoint(null);
226
                return true;
227
        }
228
        
229
        /**
230
         * Devuelve si esta usando la funcion logaritmica
231
         * @return
232
         */
233
        public boolean isLogaritmical() {
234
                return (perc > 0.0);
235
        }
236

    
237
        /**
238
         * Devuelve si esta usando la funcion exponencial
239
         * @return
240
         */
241
        public boolean isExponencial() {
242
                return (perc < 0.0);
243
        }
244
}