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