questionable patch: merge of a lot of stuff from the svg branch
[org.ibex.core.git] / src / org / ibex / graphics / Affine.java
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.
4
5 // FIXME
6 package org.ibex.graphics;
7 import java.util.*;
8 import org.ibex.util.*;
9
10 /** an affine transform; all operations are destructive */
11 public final class Affine {
12
13     //  [ a c e ]
14     //  [ b d f ]
15     //  [ 0 0 1 ]
16     public float a, b, c, d, e, f;
17
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)));
34     }
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; }
43
44     public long rotateBox(long box) { return rotateBox(Encode.longToFloat1(box), Encode.longToFloat2(box)); }
45     public long rotateBox(float x, float y) {
46         float width = 
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)));
49         float height = 
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);
53     }
54
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; }
57
58     public boolean equals(Object o) {
59         if (!(o instanceof Affine)) return false;
60         Affine x = (Affine)o;
61         return a == x.a && b == x.b && c == x.c && d == x.d && e == x.e && f == x.f;
62     }
63
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);
68     }
69
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;
79         return this;
80     }
81
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;
91         return this;
92     }
93
94     public Affine inverse() { return copy().invert(); }
95     public Affine invert() {
96         float det = (a * d - b * c);
97         float _a = d / det;
98         float _b = -1 * b / det;
99         float _c = -1 * c / det;
100         float _d = a / 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;
104         return this;
105     }
106
107     public static Affine parse(String t) {
108         if (t == null) return null;
109         t = t.trim();
110         Affine ret = Affine.identity();
111         while (t.length() > 0) {
112             if (t.startsWith("skewX(")) {
113                 // FIXME
114                 
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(')')))));
118                 
119             } else if (t.startsWith("skewY(")) {
120                 // FIXME
121                 
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));
133                 } else {
134                     ret.multiply(Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
135                 }
136                 
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(')')))));
142                 } else {
143                     ret.multiply(Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
144                 }
145                 
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")));
149                 
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(')')))));
155                 } else {
156                     ret.multiply(Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
157                                                              Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
158                 }
159                 
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]));
167             }
168             t = t.substring(t.indexOf(')') + 1).trim();
169         }
170         return ret;
171     }
172     
173 }