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;
8 import org.ibex.util.*;
10 /** an affine transform; all operations are destructive */
11 public final class Affine {
16 public float a, b, c, d, e, f;
18 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; }
19 public String toString() { return "[ " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + " ]"; }
20 public Affine copy() { return new Affine(a, b, c, d, e, f); }
21 public boolean doesNotRotate() { return a==0 && b==0 && c==0 && d==0; }
22 public static Affine identity() { return new Affine(1, 0, 0, 1, 0, 0); }
23 public static Affine scale(float sx, float sy) { return new Affine(sx, 0, 0, sy, 0, 0); }
24 public static Affine shear(float degrees) {
25 return new Affine(1, 0, (float)Math.tan(degrees * (float)(Math.PI / 180.0)), 1, 0, 0); }
26 public static Affine translate(float tx, float ty) { return new Affine(1, 0, 0, 1, tx, ty); }
27 public static Affine flip(boolean horiz, boolean vert) { return new Affine(horiz ? -1 : 1, 0, 0, vert ? -1 : 1, 0, 0); }
28 public float multiply_px(float x, float y) { return x * a + y * c + e; }
29 public float multiply_py(float x, float y) { return x * b + y * d + f; }
30 public float sign(float x) { return x >= 0 ? 1 : -1; }
31 public float divide_boundingbox_x(float bx, float by, float aspect) {
32 return (float)Math.min(Math.abs(bx * (sign(a) * sign(c)) / (aspect * a + c)),
33 Math.abs(by * (sign(b) * sign(d)) / (aspect * b + d)));
35 public float multiply_boundingbox_x(float x, float y) {
36 return (float)Math.max((int)Math.abs(multiply_px(x, y) - multiply_px(0, 0)),
37 (int)Math.abs(multiply_px(x, 0) - multiply_px(0, y))); }
38 public float multiply_boundingbox_y(float x, float y) {
39 return (float)Math.max((int)Math.abs(multiply_py(x, y) - multiply_py(0, 0)),
40 (int)Math.abs(multiply_py(x, 0) - multiply_py(0, y))); }
41 public boolean equalsIgnoringTranslation(Affine x) { return a == x.a && b == x.b && c == x.c && d == x.d; }
42 public Affine clearTranslation() { e = (float)0.0; f = (float)0.0; return this; }
44 public long rotateBox(long box) { return rotateBox(Encode.longToFloat1(box), Encode.longToFloat2(box)); }
45 public long rotateBox(float x, float y) {
47 max(max(multiply_px(0, 0), multiply_px(x, y)), max(multiply_px(x, 0), multiply_px(0, y))) -
48 min(min(multiply_px(0, 0), multiply_px(x, y)), min(multiply_px(x, 0), multiply_px(0, y)));
50 max(max(multiply_py(0, 0), multiply_py(x, y)), max(multiply_py(x, 0), multiply_py(0, y))) -
51 min(min(multiply_py(0, 0), multiply_py(x, y)), min(multiply_py(x, 0), multiply_py(0, y)));
52 return Encode.twoFloatsToLong(width, height);
55 static float min(float a, float b) { if (a<b) return a; else return b; }
56 static float max(float a, float b) { if (a>b) return a; else return b; }
58 public boolean equals(Object o) {
59 if (!(o instanceof Affine)) return false;
61 return a == x.a && b == x.b && c == x.c && d == x.d && e == x.e && f == x.f;
64 public static Affine rotate(float degrees) {
65 float s = (float)Math.sin(degrees * (float)(Math.PI / 180.0));
66 float c = (float)Math.cos(degrees * (float)(Math.PI / 180.0));
67 return new Affine(c, s, -s, c, 0, 0);
70 /** this = a * this */
71 public Affine premultiply(Affine A) {
72 float _a = this.a * A.a + this.b * A.c;
73 float _b = this.a * A.b + this.b * A.d;
74 float _c = this.c * A.a + this.d * A.c;
75 float _d = this.c * A.b + this.d * A.d;
76 float _e = this.e * A.a + this.f * A.c + A.e;
77 float _f = this.e * A.b + this.f * A.d + A.f;
78 a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
82 /** this = this * a */
83 public Affine multiply(Affine A) {
84 float _a = A.a * this.a + A.b * this.c;
85 float _b = A.a * this.b + A.b * this.d;
86 float _c = A.c * this.a + A.d * this.c;
87 float _d = A.c * this.b + A.d * this.d;
88 float _e = A.e * this.a + A.f * this.c + this.e;
89 float _f = A.e * this.b + A.f * this.d + this.f;
90 a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
94 public Affine inverse() { return copy().invert(); }
95 public Affine invert() {
96 float det = (a * d - b * c);
98 float _b = -1 * b / det;
99 float _c = -1 * c / det;
101 float _e = (f*c-e*d)/det;
102 float _f = (b*e-a*f)/det;
103 a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
107 public static Affine parse(String t) {
108 if (t == null) return null;
110 Affine ret = Affine.identity();
111 while (t.length() > 0) {
112 if (t.startsWith("skewX(")) {
115 } else if (t.startsWith("shear(")) {
116 // FIXME: nonstandard; remove this
117 ret.multiply(Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
119 } else if (t.startsWith("skewY(")) {
122 } else if (t.startsWith("rotate(")) {
123 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
124 if (sub.indexOf(',') != -1) {
125 float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
126 sub = sub.substring(sub.indexOf(',') + 1);
127 float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
128 sub = sub.substring(sub.indexOf(',') + 1);
129 float cy = Float.parseFloat(sub);
130 ret.multiply(Affine.translate(cx, cy));
131 ret.multiply(Affine.rotate(angle));
132 ret.multiply(Affine.translate(-1 * cx, -1 * cy));
134 ret.multiply(Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
137 } else if (t.startsWith("translate(")) {
138 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
139 if (sub.indexOf(',') > -1) {
140 ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
141 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
143 ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
146 } else if (t.startsWith("flip(")) {
147 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
148 ret.multiply(Affine.flip(which.equals("horizontal"), which.equals("vertical")));
150 } else if (t.startsWith("scale(")) {
151 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
152 if (sub.indexOf(',') > -1) {
153 ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
154 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
156 ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
157 Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
160 } else if (t.startsWith("matrix(")) {
161 // FIXME: is this mapped right?
162 float d[] = new float[6];
163 StringTokenizer st = new StringTokenizer(t, ",", false);
164 for(int i=0; i<6; i++)
165 d[i] = Float.parseFloat(st.nextToken());
166 ret.multiply(new Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
168 t = t.substring(t.indexOf(')') + 1).trim();