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