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