Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / beans / canvas / layers / functions / LogaritmicExponentialLine.java @ 19945

History | View | Annotate | Download (6.37 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.RasterToolsUtil;
28
/**
29
 * Representa una funcion con posibilidad de arrastre para las funciones
30
 * logaritmicas y exponenciales. Con el raton se puede pasar de una a otra
31
 * directamente.
32
 * 
33
 * Las formas mas logicas de uso seria pasandole:
34
 * 1.0: Para una funcion logaritmica
35
 * -1.0: Para una funcion exponencial
36
 * 0.0: Para una funcion lineal
37
 * 
38
 * @version 02/04/2008
39
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
40
 */
41
public class LogaritmicExponentialLine extends StraightLine {
42
        /**
43
         * Numero de puntos que contiene esta funci?n
44
         */
45
        double num = 50.0;
46
        
47
        /**
48
         * Constructor. Asigna el color y establece la posicion de la funcion.
49
         * Los valores normales son 1.0 para logaritmica y -1.0 para exponencial.
50
         * El rango va desde -2.0 hasta 2.0. Siendo 0.0 una funcion lineal.
51
         * @param c
52
         */
53
        public LogaritmicExponentialLine(Color c, double point) {
54
                super(c);
55
                setShowSquares(false);
56

    
57
                recalcList(point);
58
        }
59

    
60
        /**
61
         * Actualiza la barra informativa para saber en que estado se encuentra el
62
         * componente.
63
         * Cuando el porcentaje es mayor a 0 siempre estamos en la Logaritmica
64
         * Cuando el porcentaje es menor a 0 siempre estamos en la exponencial
65
         * @param perc
66
         */
67
        private void setInfoPoint(Double perc) {
68
                if (canvas != null) {
69
                        ArrayList list = canvas.getDrawableElements(InfoLayer.class);
70
                        if (list.size() > 0) {
71
                                InfoLayer infoLayer = (InfoLayer) list.get(0);
72
                                
73
                                if (perc == null) {
74
                                        infoLayer.setStatusLeft(null);
75
                                        infoLayer.setStatusRight(null);
76
                                        canvas.repaint();
77
                                        return;
78
                                }
79

    
80
                                if (perc.doubleValue() > 0.0)
81
                                        infoLayer.setStatusLeft(RasterToolsUtil.getText(this, "logarithmical"));
82
                                else
83
                                        infoLayer.setStatusLeft(RasterToolsUtil.getText(this, "exponential"));
84
                                infoLayer.setStatusRight("");
85
                        }
86
                }
87
        }
88
        
89
        /**
90
         * Recalcula todos los puntos de la funcion
91
         * Posibles rangos para perc:
92
         * ( 0.0 a  1.0) - Funcion logaritmica con aproximacion al centro
93
         * ( 1.0 a  2.0) - Funcion logaritmica con aproximacion al borde
94
         * ( 0.0 a -1.0) - Funcion exponencial con aproximacion al centro
95
         * (-1.0 a -2.0) - Funcion exponencial con aproximacion al borde
96
         * 
97
         * @param perc
98
         */
99
        private void recalcList(double perc) {
100
                double x, y;
101
                
102
                setInfoPoint(new Double(perc));
103

    
104
                if (perc > 2.0) perc = 2.0;
105
                if (perc < -2.0) perc = -2.0;
106
                
107
                this.listSquare.clear();
108
                this.listSquare.add(new Square(0.0, 0.0));
109
                do {
110
                        // Aproximacion al centro de una funcion logaritmica
111
                        if (perc >= 0 && perc <= 1.0) {
112
                                for (int i = 1; i < num; i++) {
113
                                        x = i / num;
114
                                        y = Math.log10(1.0 + (i * 99.0 / num)) / 2.0;
115
        
116
                                        y = x + ((y - x) * perc);
117
        
118
                                        this.listSquare.add(new Square(x, y));
119
                                }
120
                                break;
121
                        }
122
                        // Aproximacion al borde de una funcion logaritmica
123
                        if (perc >= 1.0 && perc <= 2.0) {
124
                                for (int i = 1; i < num; i++) {
125
                                        x = i / num;
126
                                        y = Math.log10(1.0 + (i * 99.0 / num)) / 2.0;
127
        
128
                                        y = y + ((1.0 - y) * (perc - 1.0));
129
        
130
                                        this.listSquare.add(new Square(x, y));
131
                                }
132
                                break;
133
                        }
134
                        // Aproximacion al centro de una funcion exponencial
135
                        if (perc >= -1.0 && perc <= 0.0) {
136
                                for (int i = 1; i < num; i++) {
137
                                        x = i / num;
138
                                        double b = 6.0;
139
                                        y = (Math.exp(((i - 1.0) / num) * b) - 1.0) / (Math.exp(b) - 1.0);
140
        
141
                                        y = x - ((x - y) * Math.abs(perc));
142
        
143
                                        this.listSquare.add(new Square(x, y));
144
                                }
145
                                break;
146
                        }
147
                        // Aproximacion al borde de una funcion exponencial
148
                        if (perc >= -2.0 && perc <= -1.0) {
149
                                for (int i = 1; i < num; i++) {
150
                                        x = i / num;
151
                                        double b = 6.0;
152
                                        y = (Math.exp(((i - 1.0) / num) * b) - 1.0) / (Math.exp(b) - 1.0);
153
        
154
                                        y = y * (1.0 - (Math.abs(perc) - 1.0));
155
        
156
                                        this.listSquare.add(new Square(x, y));
157
                                }
158
                                break;
159
                        }
160
                } while (false);
161
                this.listSquare.add(new Square(1.0, 1.0));
162
        }
163
        
164
        /* (non-Javadoc)
165
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseDragged(java.awt.event.MouseEvent)
166
         */
167
        public boolean mouseDragged(MouseEvent e) {
168
                if (canvas.getCursor().getType() != Cursor.DEFAULT_CURSOR)
169
                        return true;
170

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

    
200
        /* (non-Javadoc)
201
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseMoved(java.awt.event.MouseEvent)
202
         */
203
        public boolean mouseMoved(MouseEvent e) {
204
                return true;
205
        }
206

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

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