checkpoint
[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     //  [ m n o p ]   [ 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 plus(Matrix x) {
40         return new Matrix(a+x.a, b+x.b, c+x.c, d+x.d, e+x.e, f+x.f, g+x.g, h+x.h, i+x.i, j+x.j, k+x.k, l+x.l, m+x.m, n+x.n, o+x.o, p+x.p);
41     }
42     public Matrix minus(Matrix x) {
43         return new Matrix(a-x.a, b-x.b, c-x.c, d-x.d, e-x.e, f-x.f, g-x.g, h-x.h, i-x.i, j-x.j, k-x.k, l-x.l, m-x.m, n-x.n, o-x.o, p-x.p);
44     }
45     public Matrix times(float x) {
46         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);
47     }
48
49     /** computes (v^T)(this)(v) */
50     public float preAndPostMultiply(Point point) {
51         float ret =
52             ((a*point.x + b*point.y + c*point.z + d) * point.x) +
53             ((e*point.x + f*point.y + g*point.z + h) * point.y) +
54             ((i*point.x + j*point.y + k*point.z + l) * point.z) + 
55             ((m*point.x + n*point.y + o*point.z + p) * 1);
56         return ret;
57     }
58
59     public Matrix(Vec axis, float angle) {
60         double q = Math.cos(angle);
61         double s = Math.sin(angle);
62         double t = 1.0 - q;
63         a = (float)(q + axis.x*axis.x*t);
64         f = (float)(q + axis.y*axis.y*t);
65         k = (float)(q + axis.z*axis.z*t);
66         double tmp1 = axis.x*axis.y*t;
67         double tmp2 = axis.z*s;
68         e = (float)(tmp1 + tmp2);
69         b = (float)(tmp1 - tmp2);
70         tmp1 = axis.x*axis.z*t;
71         tmp2 = axis.y*s;
72         i = (float)(tmp1 - tmp2);
73         c = (float)(tmp1 + tmp2);
74         tmp1 = axis.y*axis.z*t;
75         tmp2 = axis.x*s;
76         j = (float)(tmp1 + tmp2);
77         g = (float)(tmp1 - tmp2);
78         d = h = l = 0;
79         m = n = o = 0;
80         p = 1;
81     }
82     public Point times(Point p) {
83         // discards bottom row
84         return new Point(a*p.x + b*p.y + c*p.z + d,
85                          e*p.x + f*p.y + g*p.z + h,
86                          i*p.x + j*p.y + k*p.z + l);
87     }
88     public Vec times(Vec p) {
89         // discards bottom row
90         return new Vec(a*p.x + b*p.y + c*p.z + d,
91                        e*p.x + f*p.y + g*p.z + h,
92                        i*p.x + j*p.y + k*p.z + l);
93     }
94     public Point apply(Point p) { return p; }
95     public Vec apply(Vec v) { return v; }
96     public Matrix invert() { return this; }
97     public Matrix times(Matrix m) { return this; }
98 }