reorganized file layout (part 2: edits)
[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     public void invert() {
52         float det = 1 / (a * d - b * c);
53         float _a = d * det;
54         float _b = -1 * b * det;
55         float _c = -1 * c * det;
56         float _d = a * det;
57         float _e = -1 * e * a - f * c;
58         float _f = -1 * e * b - f * d;
59         a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
60     }
61
62     public static Affine parse(String t) {
63         if (t == null) return null;
64         t = t.trim();
65         Affine ret = Affine.identity();
66         while (t.length() > 0) {
67             if (t.startsWith("skewX(")) {
68                 // FIXME
69                 
70             } else if (t.startsWith("shear(")) {
71                 // FIXME: nonstandard; remove this
72                 ret.multiply(Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
73                 
74             } else if (t.startsWith("skewY(")) {
75                 // FIXME
76                 
77             } else if (t.startsWith("rotate(")) {
78                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
79                 if (sub.indexOf(',') != -1) {
80                     float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
81                     sub = sub.substring(sub.indexOf(',') + 1);
82                     float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
83                     sub = sub.substring(sub.indexOf(',') + 1);
84                     float cy = Float.parseFloat(sub);
85                     ret.multiply(Affine.translate(cx, cy));
86                     ret.multiply(Affine.rotate(angle));
87                     ret.multiply(Affine.translate(-1 * cx, -1 * cy));
88                 } else {
89                     ret.multiply(Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
90                 }
91                 
92             } else if (t.startsWith("translate(")) {
93                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
94                 if (sub.indexOf(',') > -1) {
95                     ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
96                                                                  Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
97                 } else {
98                     ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
99                 }
100                 
101             } else if (t.startsWith("flip(")) {
102                 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
103                 ret.multiply(Affine.flip(which.equals("horizontal"), which.equals("vertical")));
104                 
105             } else if (t.startsWith("scale(")) {
106                 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
107                 if (sub.indexOf(',') > -1) {
108                     ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
109                                                              Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
110                 } else {
111                     ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
112                                                              Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
113                 }
114                 
115             } else if (t.startsWith("matrix(")) {
116                 // FIXME: is this mapped right?
117                 float d[] = new float[6];
118                 StringTokenizer st = new StringTokenizer(t, ",", false);
119                 for(int i=0; i<6; i++)
120                     d[i] = Float.parseFloat(st.nextToken());
121                 ret.multiply(new Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
122             }
123             t = t.substring(t.indexOf(')') + 1).trim();
124         }
125         return ret;
126     }
127     
128 }