added bounding box translation functions to Affine.java
[org.ibex.core.git] / src / org / ibex / graphics / Affine.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 // FIXME
6 package org.ibex.graphics;
7 import java.util.*;
8
9 /** an affine transform; all operations are destructive */
10 public final class Affine {
11
12     //  [ a b e ]
13     //  [ c d f ]
14     //  [ 0 0 1 ]
15     public float a, b, c, d, e, f;
16
17     Affine(float _a, float _b, float _c, float _d, float _e, float _f) { a = _a; b = _b; c = _c; d = _d; e = _e; f = _f; }
18     public String toString() { return "[ " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + " ]"; }
19     public Affine copy() { return new Affine(a, b, c, d, e, f); }
20     public static Affine identity() { return new Affine(1, 0, 0, 1, 0, 0); }
21     public static Affine scale(float sx, float sy) { return new Affine(sx, 0, 0, sy, 0, 0); }
22     public static Affine shear(float degrees) {
23         return new Affine(1, 0, (float)Math.tan(degrees * (float)(Math.PI / 180.0)), 1, 0, 0); }
24     public static Affine translate(float tx, float ty) { return new Affine(1, 0, 0, 1, tx, ty); }
25     public static Affine flip(boolean horiz, boolean vert) { return new Affine(horiz ? -1 : 1, 0, 0, vert ? -1 : 1, 0, 0); }
26     public float multiply_px(float x, float y) { return x * a + y * c + e; }
27     public float multiply_py(float x, float y) { return x * b + y * d + f; }
28     public float sign(float x) { return x >= 0 ? 1 : -1; }
29     public float divide_boundingbox_x(float bx, float by, float aspect) {
30         return (float)Math.min(Math.abs(bx * (sign(a) * sign(c)) / (aspect * a + c)),
31                                Math.abs(by * (sign(b) * sign(d)) / (aspect * b + d)));
32     }
33     public float multiply_boundingbox_x(float x, float y) {
34         return (float)Math.max((int)Math.abs(multiply_px(x, y) - multiply_px(0, 0)),
35                                (int)Math.abs(multiply_px(x, 0) - multiply_px(0, y))); }
36     public float multiply_boundingbox_y(float x, float y) {
37         return (float)Math.max((int)Math.abs(multiply_py(x, y) - multiply_py(0, 0)),
38                                (int)Math.abs(multiply_py(x, 0) - multiply_py(0, y))); }
39     public boolean equalsIgnoringTranslation(Affine x) { return a == x.a && b == x.b && c == x.c && d == x.d; }
40     public Affine clearTranslation() { e = (float)0.0; f = (float)0.0; return this; }
41
42     public boolean equals(Object o) {
43         if (!(o instanceof Affine)) return false;
44         Affine x = (Affine)o;
45         return a == x.a && b == x.b && c == x.c && d == x.d && e == x.e && f == x.f;
46     }
47
48     public static Affine rotate(float degrees) {
49         float s = (float)Math.sin(degrees * (float)(Math.PI / 180.0));
50         float c = (float)Math.cos(degrees * (float)(Math.PI / 180.0));
51         return new Affine(c, s, -s, c, 0, 0);
52     }
53
54     /** this = this * a */
55     public Affine multiply(Affine A) {
56         float _a = this.a * A.a + this.b * A.c;
57         float _b = this.a * A.b + this.b * A.d;
58         float _c = this.c * A.a + this.d * A.c;
59         float _d = this.c * A.b + this.d * A.d;
60         float _e = this.e * A.a + this.f * A.c + A.e;
61         float _f = this.e * A.b + this.f * A.d + A.f;
62         a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
63         return this;
64     }
65
66     /** this = a * this */
67     public Affine premultiply(Affine A) {
68         float _a = A.a * this.a + A.b * this.c;
69         float _b = A.a * this.b + A.b * this.d;
70         float _c = A.c * this.a + A.d * this.c;
71         float _d = A.c * this.b + A.d * this.d;
72         float _e = A.e * this.a + A.f * this.c + this.e;
73         float _f = A.e * this.b + A.f * this.d + this.f;
74         a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
75         return this;
76     }
77
78     public Affine invert() {
79         float det = 1 / (a * d - b * c);
80         float _a = d * det;
81         float _b = -1 * b * det;
82         float _c = -1 * c * det;
83         float _d = a * det;
84         float _e = -1 * e * a - f * c;
85         float _f = -1 * e * b - f * d;
86         a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
87         return this;
88     }
89
90     public static Affine parse(String t) {
91         if (t == null) return null;
92         t = t.trim();
93         Affine ret = Affine.identity();
94         while (t.length() > 0) {
95             if (t.startsWith("skewX(")) {
96                 // FIXME
97                 
98             } else if (t.startsWith("shear(")) {
99                 // FIXME: nonstandard; remove this
100                 ret.multiply(Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
101                 
102             } else if (t.startsWith("skewY(")) {
103                 // FIXME
104                 
105             } else if (t.startsWith("rotate(")) {
106                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
107                 if (sub.indexOf(',') != -1) {
108                     float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
109                     sub = sub.substring(sub.indexOf(',') + 1);
110                     float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
111                     sub = sub.substring(sub.indexOf(',') + 1);
112                     float cy = Float.parseFloat(sub);
113                     ret.multiply(Affine.translate(cx, cy));
114                     ret.multiply(Affine.rotate(angle));
115                     ret.multiply(Affine.translate(-1 * cx, -1 * cy));
116                 } else {
117                     ret.multiply(Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
118                 }
119                 
120             } else if (t.startsWith("translate(")) {
121                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
122                 if (sub.indexOf(',') > -1) {
123                     ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
124                                                                  Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
125                 } else {
126                     ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
127                 }
128                 
129             } else if (t.startsWith("flip(")) {
130                 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
131                 ret.multiply(Affine.flip(which.equals("horizontal"), which.equals("vertical")));
132                 
133             } else if (t.startsWith("scale(")) {
134                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
135                 if (sub.indexOf(',') > -1) {
136                     ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
137                                                              Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
138                 } else {
139                     ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
140                                                              Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
141                 }
142                 
143             } else if (t.startsWith("matrix(")) {
144                 // FIXME: is this mapped right?
145                 float d[] = new float[6];
146                 StringTokenizer st = new StringTokenizer(t, ",", false);
147                 for(int i=0; i<6; i++)
148                     d[i] = Float.parseFloat(st.nextToken());
149                 ret.multiply(new Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
150             }
151             t = t.substring(t.indexOf(')') + 1).trim();
152         }
153         return ret;
154     }
155     
156 }