view src/samer/core_/util/IMap.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	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() {}
}