samer@0
|
1 /*
|
samer@0
|
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
3 * All rights reserved.
|
samer@0
|
4 *
|
samer@0
|
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
6 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
7 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
8 */
|
samer@0
|
9
|
samer@0
|
10 package samer.core.util;
|
samer@0
|
11
|
samer@0
|
12 /**
|
samer@0
|
13 Integer map:
|
samer@0
|
14 This class maps a given interval of real numbers
|
samer@0
|
15 to the interval [0,1], returning either the real
|
samer@0
|
16 number, or converting it to an integer between 0
|
samer@0
|
17 and a given value (defaults to 256).
|
samer@0
|
18 */
|
samer@0
|
19
|
samer@0
|
20 public class IMap
|
samer@0
|
21 {
|
samer@0
|
22 double a, b;
|
samer@0
|
23 double ia, ib;
|
samer@0
|
24 int maxi;
|
samer@0
|
25 double c;
|
samer@0
|
26 boolean clipped;
|
samer@0
|
27
|
samer@0
|
28 public IMap copy() { try { return (IMap)this.clone(); } catch (Exception ex) {} return null; }
|
samer@0
|
29
|
samer@0
|
30 public String toString() { return "["+getDomainMin()+","+getDomainMax()+")->{0.."+(maxi-1)+"}"; }
|
samer@0
|
31 public double getDomainMin() { return b/a; }
|
samer@0
|
32 public double getDomainMax() { return (1+b)/a; }
|
samer@0
|
33 public void setDomain( double t1, double t2)
|
samer@0
|
34 {
|
samer@0
|
35 a = 1/(t2-t1);
|
samer@0
|
36 b = a*t1;
|
samer@0
|
37 ia = c*a;
|
samer@0
|
38 ib = c*b;
|
samer@0
|
39 }
|
samer@0
|
40
|
samer@0
|
41 public int getIntRange() { return maxi+1; }
|
samer@0
|
42 public void setIntRange( int m)
|
samer@0
|
43 {
|
samer@0
|
44 maxi= m-1;
|
samer@0
|
45 c = m;
|
samer@0
|
46 ia = c*a;
|
samer@0
|
47 ib = c*b;
|
samer@0
|
48 }
|
samer@0
|
49
|
samer@0
|
50 public double map( double t) { return a*t-b; }
|
samer@0
|
51 public int clipInt( double t)
|
samer@0
|
52 {
|
samer@0
|
53 double x=ia*t-ib;
|
samer@0
|
54 if (x<0) return 0;
|
samer@0
|
55 if (x>=c) return maxi;
|
samer@0
|
56 return (int)x;
|
samer@0
|
57 }
|
samer@0
|
58
|
samer@0
|
59 // sets a flag if clipped
|
samer@0
|
60 public int toInt( double t)
|
samer@0
|
61 {
|
samer@0
|
62 double x=ia*t-ib;
|
samer@0
|
63 clipped = (x<0 || x>=c);
|
samer@0
|
64 return (int)x;
|
samer@0
|
65 }
|
samer@0
|
66
|
samer@0
|
67 // inverse mapping
|
samer@0
|
68 public double inverse(double y) { return (y+b)/a; }
|
samer@0
|
69 public double inverseFromInt(int y) { return (y+ib)/ia; }
|
samer@0
|
70
|
samer@0
|
71 public boolean wasClipped() { return clipped; }
|
samer@0
|
72 protected IMap() {}
|
samer@0
|
73 }
|
samer@0
|
74
|
samer@0
|
75
|
samer@0
|
76
|
samer@0
|
77
|
samer@0
|
78
|
samer@0
|
79
|
samer@0
|
80
|
samer@0
|
81
|