f617376b09f7a683770f23e1513bc2b64f555ce5
[anneal.git] / src / edu / berkeley / qfat / geom / Matrix.java
1 package edu.berkeley.qfat.geom;
2
3 /** affine matrix; immutable */
4 public class Matrix {
5     //
6     //  [ a b c d ]   [ x ]
7     //  [ e f g h ]   [ y ]
8     //  [ i j k l ]   [ z ]
9     //  [ 0 0 0 1 ]   [ 1 ]
10     //
11     public final float a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
12     public Matrix() { this(1); }
13     public Matrix(float scale) {
14         a = f = k = scale;
15         l = h = d = e = b = i = c = j = g = 0;            
16         m = n = o = 0;
17         p = 1;
18     }
19     public Matrix(float scalex, float scaley, float scalez) {
20         a = scalex;
21         f = scaley;
22         k = scalez;
23         l = h = d = e = b = i = c = j = g = 0;            
24         m = n = o = 0;
25         p = 1;
26     }
27     public Matrix(Vec translate) {
28         d = translate.x; h = translate.y; l = translate.z;
29         a = f = k = 1;
30         b = c = e = g = i = j = 0;
31         m = n = o = 0;
32         p = 1;
33     }
34     public Matrix(float a, float b, float c, float d, float e, float f, float g,
35                   float h, float i, float j, float k, float l, float m, float n, float o, float p) {
36         this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; this.g = g; this.h = h; this.i = i;
37         this.j = j; this.k = k; this.l = l; this.m = m; this.n = n; this.o = o; this.p = p;
38     }
39     public Matrix times(float x) {
40         return new Matrix(a*x, b*x, c*x, d*x, e*x, f*x, g*x, h*x, i*x, j*x, k*x, l*x, m*x, n*x, o*x, p*x);
41     }
42     public Matrix(Vec axis, float angle) {
43         double q = Math.cos(angle);
44         double s = Math.sin(angle);
45         double t = 1.0 - q;
46         a = (float)(q + axis.x*axis.x*t);
47         f = (float)(q + axis.y*axis.y*t);
48         k = (float)(q + axis.z*axis.z*t);
49         double tmp1 = axis.x*axis.y*t;
50         double tmp2 = axis.z*s;
51         e = (float)(tmp1 + tmp2);
52         b = (float)(tmp1 - tmp2);
53         tmp1 = axis.x*axis.z*t;
54         tmp2 = axis.y*s;
55         i = (float)(tmp1 - tmp2);
56         c = (float)(tmp1 + tmp2);
57         tmp1 = axis.y*axis.z*t;
58         tmp2 = axis.x*s;
59         j = (float)(tmp1 + tmp2);
60         g = (float)(tmp1 - tmp2);
61         d = h = l = 0;
62         m = n = o = 0;
63         p = 1;
64     }
65     public Point times(Point p) {
66         // discards bottom row
67         return new Point(a*p.x + b*p.y + c*p.z + d,
68                          e*p.x + f*p.y + g*p.z + h,
69                          i*p.x + j*p.y + k*p.z + l);
70     }
71     public Point apply(Point p) { return p; }
72     public Vec apply(Vec v) { return v; }
73     public Matrix invert() { return this; }
74     public Matrix times(Matrix m) { return this; }
75 }