gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / canvas / layer / function / DefaultLogaritmicExponentialLine.java @ 2585
History | View | Annotate | Download (6.7 KB)
1 | 2443 | nbrodin | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | package org.gvsig.raster.swing.impl.canvas.layer.function; |
||
23 | |||
24 | import java.awt.Color; |
||
25 | import java.awt.Component; |
||
26 | import java.awt.Cursor; |
||
27 | import java.awt.event.MouseEvent; |
||
28 | 2584 | nbrodin | import java.util.List; |
29 | 2443 | nbrodin | |
30 | 2584 | nbrodin | import org.gvsig.raster.swing.gcanvas.DrawableElement; |
31 | 2443 | nbrodin | import org.gvsig.raster.swing.gcanvas.LogaritmicExponentialLine; |
32 | import org.gvsig.raster.swing.impl.DefaultRasterSwingManager; |
||
33 | import org.gvsig.raster.swing.impl.canvas.layer.DefaultInfoLayer; |
||
34 | /**
|
||
35 | * Representa una funcion con posibilidad de arrastre para las funciones
|
||
36 | * logaritmicas y exponenciales. Con el raton se puede pasar de una a otra
|
||
37 | * directamente.
|
||
38 | *
|
||
39 | * Las formas mas logicas de uso seria pasandole:
|
||
40 | * 1.0: Para una funcion logaritmica
|
||
41 | * -1.0: Para una funcion exponencial
|
||
42 | * 0.0: Para una funcion lineal
|
||
43 | *
|
||
44 | 2584 | nbrodin | * @author BorSanZa - Borja S?nchez Zamorano
|
45 | 2443 | nbrodin | */
|
46 | public class DefaultLogaritmicExponentialLine extends DefaultStraightLine implements LogaritmicExponentialLine { |
||
47 | /**
|
||
48 | * Numero de puntos que contiene esta funci?n
|
||
49 | */
|
||
50 | private double num = 40.0; |
||
51 | |||
52 | /**
|
||
53 | * Base de la exponencial
|
||
54 | */
|
||
55 | private double baseExp = 6.0; |
||
56 | |||
57 | private double valueFunction = 1.0; |
||
58 | |||
59 | /**
|
||
60 | * Constructor. Asigna el color y establece la posicion de la funcion.
|
||
61 | * Los valores normales son 1.0 para logaritmica y -1.0 para exponencial.
|
||
62 | * El rango va desde -2.0 hasta 2.0. Siendo 0.0 una funcion lineal.
|
||
63 | * @param c
|
||
64 | */
|
||
65 | public DefaultLogaritmicExponentialLine(Color c, double point) { |
||
66 | super(c);
|
||
67 | setShowSquares(false);
|
||
68 | |||
69 | valueFunction = point; |
||
70 | recalcList(); |
||
71 | } |
||
72 | |||
73 | /**
|
||
74 | * Actualiza la barra informativa para saber en que estado se encuentra el
|
||
75 | * componente.
|
||
76 | * Cuando el porcentaje es mayor a 0 siempre estamos en la Logaritmica
|
||
77 | * Cuando el porcentaje es menor a 0 siempre estamos en la exponencial
|
||
78 | * @param perc
|
||
79 | */
|
||
80 | private void setInfoPoint(Double perc) { |
||
81 | if (canvas != null) { |
||
82 | 2584 | nbrodin | List<DrawableElement> list = canvas.getDrawableElements(DefaultInfoLayer.class);
|
83 | 2443 | nbrodin | if (list.size() > 0) { |
84 | DefaultInfoLayer infoLayer = (DefaultInfoLayer) list.get(0);
|
||
85 | |||
86 | if (perc == null) { |
||
87 | infoLayer.setStatusLeft(null);
|
||
88 | infoLayer.setStatusRight(null);
|
||
89 | ((Component)canvas).repaint();
|
||
90 | return;
|
||
91 | } |
||
92 | |||
93 | if (perc.doubleValue() > 0.0) |
||
94 | infoLayer.setStatusLeft(DefaultRasterSwingManager.getText(this, "logarithmical")); |
||
95 | else
|
||
96 | infoLayer.setStatusLeft(DefaultRasterSwingManager.getText(this, "exponential")); |
||
97 | |||
98 | infoLayer.setStatusRight(clipDecimals(Math.abs(perc.doubleValue()*100.0), 2) + "%"); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public double clipDecimals(double num, int n) { |
||
104 | long m = (long) Math.pow(10, n); |
||
105 | long aux = Math.round(num * m); |
||
106 | return (double) aux / (double) m; |
||
107 | } |
||
108 | |||
109 | /**
|
||
110 | * Recalcula todos los puntos de la funcion
|
||
111 | * ( 0.0 a 1.0) - Funcion logaritmica con aproximacion al centro
|
||
112 | * ( 1.0 a 2.0) - Funcion logaritmica con aproximacion al borde
|
||
113 | * ( 0.0 a -1.0) - Funcion exponencial con aproximacion al centro
|
||
114 | * (-1.0 a -2.0) - Funcion exponencial con aproximacion al borde
|
||
115 | */
|
||
116 | private void recalcList() { |
||
117 | double x, y = 0.0; |
||
118 | |||
119 | setInfoPoint(new Double(valueFunction)); |
||
120 | |||
121 | this.listSquare.clear();
|
||
122 | |||
123 | for (int i = 0; i <= num; i++) { |
||
124 | x = ((double) i) / num;
|
||
125 | |||
126 | // Aproximacion al centro de una funcion logaritmica
|
||
127 | if (valueFunction >= 0.0 && valueFunction <= 1.0) { |
||
128 | y = logFunction(x); |
||
129 | y = x + ((y - x) * valueFunction); |
||
130 | } |
||
131 | |||
132 | // Aproximacion al borde de una funcion logaritmica
|
||
133 | if (valueFunction > 1.0 && valueFunction <= 2.0) { |
||
134 | y = logFunction(x); |
||
135 | y = y + ((1.0 - y) * (valueFunction - 1.0)); |
||
136 | } |
||
137 | |||
138 | // Aproximacion al centro de una funcion exponencial
|
||
139 | if (valueFunction >= -1.0 && valueFunction < 0.0) { |
||
140 | y = expFunction(x); |
||
141 | y = x - ((x - y) * Math.abs(valueFunction));
|
||
142 | } |
||
143 | |||
144 | // Aproximacion al borde de una funcion exponencial
|
||
145 | if (valueFunction >= -2.0 && valueFunction < -1.0) { |
||
146 | y = expFunction(x); |
||
147 | y = y * (1.0 - (Math.abs(valueFunction) - 1.0)); |
||
148 | } |
||
149 | |||
150 | if (y < 0.0) |
||
151 | y = 0.0;
|
||
152 | if (y > 1.0) |
||
153 | y = 1.0;
|
||
154 | |||
155 | this.listSquare.add(new Square(x, y)); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /**
|
||
160 | * Formula para calcular el valor en y de una funcion logaritmica en x
|
||
161 | * @param x
|
||
162 | * @return
|
||
163 | */
|
||
164 | private double logFunction(double x) { |
||
165 | return Math.log10(1.0 + (x * 99.0)) / 2.0; |
||
166 | } |
||
167 | |||
168 | /**
|
||
169 | * Formula para calcular el valor en y de una funcion exponencial en x
|
||
170 | * @param x
|
||
171 | * @return
|
||
172 | */
|
||
173 | private double expFunction(double x) { |
||
174 | return (Math.exp(x * baseExp) - 1.0) / (Math.exp(baseExp) - 1.0); |
||
175 | } |
||
176 | |||
177 | /*
|
||
178 | * (non-Javadoc)
|
||
179 | * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseDragged(java.awt.event.MouseEvent)
|
||
180 | */
|
||
181 | public boolean mouseDragged(MouseEvent e) { |
||
182 | if (((Component)canvas).getCursor().getType() != Cursor.DEFAULT_CURSOR) |
||
183 | return true; |
||
184 | |||
185 | double x = pixelToValueX(e.getX());
|
||
186 | double y = pixelToValueY(e.getY());
|
||
187 | |||
188 | double y2 = 0.0; |
||
189 | |||
190 | // Localizamos el punto del raton para pasar un porcentaje correcto y que
|
||
191 | // asi coincida la funcion con el raton
|
||
192 | if (y >= x) {
|
||
193 | y2 = logFunction(x); |
||
194 | if (y < y2)
|
||
195 | valueFunction = (y - x) / (y2 - x); |
||
196 | else
|
||
197 | valueFunction = ((y - y2) / (1.0 - y2)) + 1.0; |
||
198 | } else {
|
||
199 | y2 = expFunction(x); |
||
200 | |||
201 | if (y > y2)
|
||
202 | valueFunction = -Math.abs((y - x) / (y2 - x));
|
||
203 | else
|
||
204 | valueFunction = -Math.abs((y2 - y) / y2) - 1.0; |
||
205 | } |
||
206 | |||
207 | if (valueFunction > 2.0) |
||
208 | valueFunction = 2.0;
|
||
209 | if (valueFunction < -2.0) |
||
210 | valueFunction = -2.0;
|
||
211 | |||
212 | recalcList(); |
||
213 | ((Component)canvas).repaint();
|
||
214 | canvas.callDataDragged("line", this); |
||
215 | return false; |
||
216 | } |
||
217 | |||
218 | public boolean mouseMoved(MouseEvent e) { |
||
219 | return true; |
||
220 | } |
||
221 | |||
222 | public boolean mousePressed(MouseEvent e) { |
||
223 | return mouseDragged(e);
|
||
224 | } |
||
225 | |||
226 | public boolean mouseReleased(MouseEvent e) { |
||
227 | setInfoPoint(null);
|
||
228 | return true; |
||
229 | } |
||
230 | |||
231 | public boolean isLogaritmical() { |
||
232 | return (valueFunction > 0.0); |
||
233 | } |
||
234 | |||
235 | public boolean isExponencial() { |
||
236 | return (valueFunction < 0.0); |
||
237 | } |
||
238 | |||
239 | public int getFunctionType() { |
||
240 | return 1; |
||
241 | } |
||
242 | |||
243 | public double getValueFunction() { |
||
244 | return valueFunction;
|
||
245 | } |
||
246 | } |