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.
6 package org.ibex.graphics;
9 /** an affine transform; all operations are destructive */
10 public final class Affine {
15 public float a, b, c, d, e, f;
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)));
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; }
43 public boolean equals(Object o) {
44 if (!(o instanceof Affine)) return false;
46 return a == x.a && b == x.b && c == x.c && d == x.d && e == x.e && f == x.f;
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);
55 /** this = a * this */
56 public Affine premultiply(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;
67 /** this = this * a */
68 public Affine multiply(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;
79 public Affine inverse() { return copy().invert(); }
80 public Affine invert() {
81 float det = (a * d - b * c);
83 float _b = -1 * b / det;
84 float _c = -1 * c / det;
86 float _e = (f*c-e*d)/det;
87 float _f = (b*e-a*f)/det;
88 a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
92 public static Affine parse(String t) {
93 if (t == null) return null;
95 Affine ret = Affine.identity();
96 while (t.length() > 0) {
97 if (t.startsWith("skewX(")) {
100 } else if (t.startsWith("shear(")) {
101 // FIXME: nonstandard; remove this
102 ret.multiply(Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
104 } else if (t.startsWith("skewY(")) {
107 } else if (t.startsWith("rotate(")) {
108 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
109 if (sub.indexOf(',') != -1) {
110 float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
111 sub = sub.substring(sub.indexOf(',') + 1);
112 float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
113 sub = sub.substring(sub.indexOf(',') + 1);
114 float cy = Float.parseFloat(sub);
115 ret.multiply(Affine.translate(cx, cy));
116 ret.multiply(Affine.rotate(angle));
117 ret.multiply(Affine.translate(-1 * cx, -1 * cy));
119 ret.multiply(Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
122 } else if (t.startsWith("translate(")) {
123 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
124 if (sub.indexOf(',') > -1) {
125 ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
126 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
128 ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
131 } else if (t.startsWith("flip(")) {
132 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
133 ret.multiply(Affine.flip(which.equals("horizontal"), which.equals("vertical")));
135 } else if (t.startsWith("scale(")) {
136 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
137 if (sub.indexOf(',') > -1) {
138 ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
139 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
141 ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
142 Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
145 } else if (t.startsWith("matrix(")) {
146 // FIXME: is this mapped right?
147 float d[] = new float[6];
148 StringTokenizer st = new StringTokenizer(t, ",", false);
149 for(int i=0; i<6; i++)
150 d[i] = Float.parseFloat(st.nextToken());
151 ret.multiply(new Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
153 t = t.substring(t.indexOf(')') + 1).trim();