Mercurial > hg > jslab
diff src/samer/core_/util/IMap.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/samer/core_/util/IMap.java Tue Jan 17 17:50:20 2012 +0000 @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2000, Samer Abdallah, King's College London. + * All rights reserved. + * + * This software is provided AS iS and WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + */ + +package samer.core.util; + +/** + Integer map: + This class maps a given interval of real numbers + to the interval [0,1], returning either the real + number, or converting it to an integer between 0 + and a given value (defaults to 256). + */ + +public class IMap +{ + double a, b; + double ia, ib; + int maxi; + double c; + boolean clipped; + + public IMap copy() { try { return (IMap)this.clone(); } catch (Exception ex) {} return null; } + + public String toString() { return "["+getDomainMin()+","+getDomainMax()+")->{0.."+(maxi-1)+"}"; } + public double getDomainMin() { return b/a; } + public double getDomainMax() { return (1+b)/a; } + public void setDomain( double t1, double t2) + { + a = 1/(t2-t1); + b = a*t1; + ia = c*a; + ib = c*b; + } + + public int getIntRange() { return maxi+1; } + public void setIntRange( int m) + { + maxi= m-1; + c = m; + ia = c*a; + ib = c*b; + } + + public double map( double t) { return a*t-b; } + public int clipInt( double t) + { + double x=ia*t-ib; + if (x<0) return 0; + if (x>=c) return maxi; + return (int)x; + } + + // sets a flag if clipped + public int toInt( double t) + { + double x=ia*t-ib; + clipped = (x<0 || x>=c); + return (int)x; + } + + // inverse mapping + public double inverse(double y) { return (y+b)/a; } + public double inverseFromInt(int y) { return (y+ib)/ia; } + + public boolean wasClipped() { return clipped; } + protected IMap() {} +} + + + + + + + +