0956adca97632d911c34d5d4e18ce8441da3cd25
[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 boolean doesNotRotate() { return a==0 && b==0 && c==0 && d==0; }
21     public static Affine identity() { return new Affine(1, 0, 0, 1, 0, 0); }
22     public static Affine scale(float sx, float sy) { return new Affine(sx, 0, 0, sy, 0, 0); }
23     public static Affine shear(float degrees) {
24         return new Affine(1, 0, (float)Math.tan(degrees * (float)(Math.PI / 180.0)), 1, 0, 0); }
25     public static Affine translate(float tx, float ty) { return new Affine(1, 0, 0, 1, tx, ty); }
26     public static Affine flip(boolean horiz, boolean vert) { return new Affine(horiz ? -1 : 1, 0, 0, vert ? -1 : 1, 0, 0); }
27     public float multiply_px(float x, float y) { return x * a + y * c + e; }
28     public float multiply_py(float x, float y) { return x * b + y * d + f; }
29     public float sign(float x) { return x >= 0 ? 1 : -1; }
30     public float divide_boundingbox_x(float bx, float by, float aspect) {
31         return (float)Math.min(Math.abs(bx * (sign(a) * sign(c)) / (aspect * a + c)),
32                                Math.abs(by * (sign(b) * sign(d)) / (aspect * b + d)));
33     }
34     public float multiply_boundingbox_x(float x, float y) {
35         return (float)Math.max((int)Math.abs(multiply_px(x, y) - multiply_px(0, 0)),
36                                (int)Math.abs(multiply_px(x, 0) - multiply_px(0, y))); }
37     public float multiply_boundingbox_y(float x, float y) {
38         return (float)Math.max((int)Math.abs(multiply_py(x, y) - multiply_py(0, 0)),
39                                (int)Math.abs(multiply_py(x, 0) - multiply_py(0, y))); }
40     public boolean equalsIgnoringTranslation(Affine x) { return a == x.a && b == x.b && c == x.c && d == x.d; }
41     public Affine clearTranslation() { e = (float)0.0; f = (float)0.0; return this; }
42
43     public boolean equals(Object o) {
44         if (!(o instanceof Affine)) return false;
45         Affine x = (Affine)o;
46         return a == x.a && b == x.b && c == x.c && d == x.d && e == x.e && f == x.f;
47     }
48
49     public static Affine rotate(float degrees) {
50         float s = (float)Math.sin(degrees * (float)(Math.PI / 180.0));
51         float c = (float)Math.cos(degrees * (float)(Math.PI / 180.0));
52         return new Affine(c, s, -s, c, 0, 0);
53     }
54
55     /** this = this * a */
56     public Affine multiply(Affine A) {
57         float _a = this.a * A.a + this.b * A.c;
58         float _b = this.a * A.b + this.b * A.d;
59         float _c = this.c * A.a + this.d * A.c;
60         float _d = this.c * A.b + this.d * A.d;
61         float _e = this.e * A.a + this.f * A.c + A.e;
62         float _f = this.e * A.b + this.f * A.d + A.f;
63         a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
64         return this;
65     }
66
67     /** this = a * this */
68     public Affine premultiply(Affine A) {
69         float _a = A.a * this.a + A.b * this.c;
70         float _b = A.a * this.b + A.b * this.d;
71         float _c = A.c * this.a + A.d * this.c;
72         float _d = A.c * this.b + A.d * this.d;
73         float _e = A.e * this.a + A.f * this.c + this.e;
74         float _f = A.e * this.b + A.f * this.d + this.f;
75         a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
76         return this;
77     }
78
79     public Affine invert() {
80         float det = 1 / (a * d - b * c);
81         float _a = d * det;
82         float _b = -1 * b * det;
83         float _c = -1 * c * det;
84         float _d = a * det;
85         float _e = -1 * e * a - f * c;
86         float _f = -1 * e * b - f * d;
87         a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
88         return this;
89     }
90
91     public static Affine parse(String t) {
92         if (t == null) return null;
93         t = t.trim();
94         Affine ret = Affine.identity();
95         while (t.length() > 0) {
96             if (t.startsWith("skewX(")) {
97                 // FIXME
98                 
99             } else if (t.startsWith("shear(")) {
100                 // FIXME: nonstandard; remove this
101                 ret.multiply(Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
102                 
103             } else if (t.startsWith("skewY(")) {
104                 // FIXME
105                 
106             } else if (t.startsWith("rotate(")) {
107                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
108                 if (sub.indexOf(',') != -1) {
109                     float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
110                     sub = sub.substring(sub.indexOf(',') + 1);
111                     float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
112                     sub = sub.substring(sub.indexOf(',') + 1);
113                     float cy = Float.parseFloat(sub);
114                     ret.multiply(Affine.translate(cx, cy));
115                     ret.multiply(Affine.rotate(angle));
116                     ret.multiply(Affine.translate(-1 * cx, -1 * cy));
117                 } else {
118                     ret.multiply(Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
119                 }
120                 
121             } else if (t.startsWith("translate(")) {
122                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
123                 if (sub.indexOf(',') > -1) {
124                     ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
125                                                                  Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
126                 } else {
127                     ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
128                 }
129                 
130             } else if (t.startsWith("flip(")) {
131                 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
132                 ret.multiply(Affine.flip(which.equals("horizontal"), which.equals("vertical")));
133                 
134             } else if (t.startsWith("scale(")) {
135                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
136                 if (sub.indexOf(',') > -1) {
137                     ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
138                                                              Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
139                 } else {
140                     ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
141                                                              Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
142                 }
143                 
144             } else if (t.startsWith("matrix(")) {
145                 // FIXME: is this mapped right?
146                 float d[] = new float[6];
147                 StringTokenizer st = new StringTokenizer(t, ",", false);
148                 for(int i=0; i<6; i++)
149                     d[i] = Float.parseFloat(st.nextToken());
150                 ret.multiply(new Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
151             }
152             t = t.substring(t.indexOf(')') + 1).trim();
153         }
154         return ret;
155     }
156     
157 }