1 /* 2 * Copyright (c) 2003-2008 by Cosylab d. d. 3 * 4 * This file is part of CosyBeans-Common. 5 * 6 * CosyBeans-Common is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * CosyBeans-Common 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 CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package com.cosylab.gui.components.range2; 21 22 /** 23 * Implementation of logarithmic range. 24 * 25 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a> 26 * @version $id$ 27 */ 28 public class LogarithmicRange implements Range 29 { 30 31 private TickCalculator defaultTickCalculator; 32 /** 33 * Returns absolute value. Function is only guaranteed to return for values 34 * larger than 0. 35 * 36 * @param relative value in 0.0 to 1.0 range. 37 * 38 * @return absolute value. 39 * 40 * @see com.cosylab.gui.components.range.Range#toAbsolute(double) 41 */ 42 public double toAbsolute(double relative, RangedValue val) 43 { 44 double logMin = Math.log(val.getMinimum()); 45 double logMax = Math.log(val.getMaximum()); 46 47 return Math.exp(logMin + relative * (logMax - logMin)); 48 } 49 50 /** 51 * Returns relative value in 0.0 to 1.0 range inclusively. 52 * 53 * @param absolute value, must be larger than 0.0. 54 * 55 * @return value in 0.0 to 1.0 range. 56 * 57 * @see com.cosylab.gui.components.range.Range#toRelative(double) 58 */ 59 public double toRelative(double absolute, RangedValue val) 60 { 61 double logMin = Math.log(val.getMinimum()); 62 double logMax = Math.log(val.getMaximum()); 63 64 double range = logMax - logMin; 65 66 if (range == 0.0) { 67 return 0.5; 68 } 69 70 return (Math.log(absolute) - logMin) / range; 71 } 72 73 /** Constant defining smallest number that can be defined within range. */ 74 public static final double SMALLEST = 1e-8; 75 76 /** 77 * Checks whether value is positive. If value is smaller than value defined 78 * by <code>SMALLEST</code> constant, this constant is returned. This 79 * prevents setting of values that cannot be representated on logarithmic 80 * scale. 81 * 82 * @param value to check. 83 * 84 * @return same as value parameter if valid, <code>SMALLEST</code> 85 * otherwise. 86 */ 87 public final double validate(double value) 88 { 89 return (value > SMALLEST) ? value : SMALLEST; 90 } 91 92 /* 93 * (non-Javadoc) 94 * @see com.cosylab.gui.components.range2.Range#getDefaultTickCalculator() 95 */ 96 public TickCalculator getDefaultTickCalculator() { 97 if (defaultTickCalculator == null) { 98 defaultTickCalculator = new LogarithmicTickCalculator(3, false); 99 } 100 return defaultTickCalculator; 101 } 102 } 103 104 /* __oOo__ */