--- /dev/null
+// FIXME
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.ibex.graphics;
+import java.util.*;
+
+/** an affine transform; all operations are destructive */
+public final class Affine {
+
+ // [ a b e ]
+ // [ c d f ]
+ // [ 0 0 1 ]
+ public float a, b, c, d, e, f;
+
+ 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; }
+ public String toString() { return "[ " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + " ]"; }
+ public Affine copy() { return new Affine(a, b, c, d, e, f); }
+ public static Affine identity() { return new Affine(1, 0, 0, 1, 0, 0); }
+ public static Affine scale(float sx, float sy) { return new Affine(sx, 0, 0, sy, 0, 0); }
+ public static Affine shear(float degrees) {
+ return new Affine(1, 0, (float)Math.tan(degrees * (float)(Math.PI / 180.0)), 1, 0, 0); }
+ public static Affine translate(float tx, float ty) { return new Affine(1, 0, 0, 1, tx, ty); }
+ public static Affine flip(boolean horiz, boolean vert) { return new Affine(horiz ? -1 : 1, 0, 0, vert ? -1 : 1, 0, 0); }
+ public float multiply_px(float x, float y) { return x * a + y * c + e; }
+ public float multiply_py(float x, float y) { return x * b + y * d + f; }
+ public boolean equalsIgnoringTranslation(Affine x) { return a == x.a && b == x.b && c == x.c && d == x.d; }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof Affine)) return false;
+ Affine x = (Affine)o;
+ return a == x.a && b == x.b && c == x.c && d == x.d && e == x.e && f == x.f;
+ }
+
+ public static Affine rotate(float degrees) {
+ float s = (float)Math.sin(degrees * (float)(Math.PI / 180.0));
+ float c = (float)Math.cos(degrees * (float)(Math.PI / 180.0));
+ return new Affine(c, s, -s, c, 0, 0);
+ }
+
+ /** this = this * a */
+ public Affine multiply(Affine A) {
+ float _a = this.a * A.a + this.b * A.c;
+ float _b = this.a * A.b + this.b * A.d;
+ float _c = this.c * A.a + this.d * A.c;
+ float _d = this.c * A.b + this.d * A.d;
+ float _e = this.e * A.a + this.f * A.c + A.e;
+ float _f = this.e * A.b + this.f * A.d + A.f;
+ a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
+ return this;
+ }
+
+ public void invert() {
+ float det = 1 / (a * d - b * c);
+ float _a = d * det;
+ float _b = -1 * b * det;
+ float _c = -1 * c * det;
+ float _d = a * det;
+ float _e = -1 * e * a - f * c;
+ float _f = -1 * e * b - f * d;
+ a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
+ }
+
+ public static Affine parse(String t) {
+ if (t == null) return null;
+ t = t.trim();
+ Affine ret = Affine.identity();
+ while (t.length() > 0) {
+ if (t.startsWith("skewX(")) {
+ // FIXME
+
+ } else if (t.startsWith("shear(")) {
+ // FIXME: nonstandard; remove this
+ ret.multiply(Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
+
+ } else if (t.startsWith("skewY(")) {
+ // FIXME
+
+ } else if (t.startsWith("rotate(")) {
+ String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
+ if (sub.indexOf(',') != -1) {
+ float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
+ sub = sub.substring(sub.indexOf(',') + 1);
+ float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
+ sub = sub.substring(sub.indexOf(',') + 1);
+ float cy = Float.parseFloat(sub);
+ ret.multiply(Affine.translate(cx, cy));
+ ret.multiply(Affine.rotate(angle));
+ ret.multiply(Affine.translate(-1 * cx, -1 * cy));
+ } else {
+ ret.multiply(Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
+ }
+
+ } else if (t.startsWith("translate(")) {
+ String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
+ if (sub.indexOf(',') > -1) {
+ ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
+ Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
+ } else {
+ ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
+ }
+
+ } else if (t.startsWith("flip(")) {
+ String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
+ ret.multiply(Affine.flip(which.equals("horizontal"), which.equals("vertical")));
+
+ } else if (t.startsWith("scale(")) {
+ String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
+ if (sub.indexOf(',') > -1) {
+ ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
+ Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
+ } else {
+ ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
+ Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
+ }
+
+ } else if (t.startsWith("matrix(")) {
+ // FIXME: is this mapped right?
+ float d[] = new float[6];
+ StringTokenizer st = new StringTokenizer(t, ",", false);
+ for(int i=0; i<6; i++)
+ d[i] = Float.parseFloat(st.nextToken());
+ ret.multiply(new Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
+ }
+ t = t.substring(t.indexOf(')') + 1).trim();
+ }
+ return ret;
+ }
+
+}