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     //  [ 0 0 0 1 ]   [ 1 ]
10     //
11     public final float a, b, c, d, e, f, g, h, i, j, k, l;
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     }
17     public Matrix(float scalex, float scaley, float scalez) {
18         a = scalex;
19         f = scaley;
20         k = scalez;
21         l = h = d = e = b = i = c = j = g = 0;            
22     }
23     public Matrix(Vec translate) {
24         d = translate.x; h = translate.y; l = translate.z;
25         a = f = k = 1;
26         b = c = e = g = i = j = 0;
27     }
28     public Matrix(float a, float b, float c, float d, float e, float f, float g, float h, float i, float j, float k, float l) {
29         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;
30         this.j = j; this.k = k; this.l = l;
31     }
32     public Matrix times(float x) {
33         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);
34     }
35     public Matrix(Vec axis, float angle) {
36         double q = Math.cos(angle);
37         double s = Math.sin(angle);
38         double t = 1.0 - q;
39         a = (float)(q + axis.x*axis.x*t);
40         f = (float)(q + axis.y*axis.y*t);
41         k = (float)(q + axis.z*axis.z*t);
42         double tmp1 = axis.x*axis.y*t;
43         double tmp2 = axis.z*s;
44         e = (float)(tmp1 + tmp2);
45         b = (float)(tmp1 - tmp2);
46         tmp1 = axis.x*axis.z*t;
47         tmp2 = axis.y*s;
48         i = (float)(tmp1 - tmp2);
49         c = (float)(tmp1 + tmp2);
50         tmp1 = axis.y*axis.z*t;
51         tmp2 = axis.x*s;
52         j = (float)(tmp1 + tmp2);
53         g = (float)(tmp1 - tmp2);
54         d = h = l = 0;
55     }
56     public Point times(Point p) {
57         return new Point(a*p.x + b*p.y + c*p.z + d,
58                          e*p.x + f*p.y + g*p.z + h,
59                          i*p.x + j*p.y + k*p.z + l);
60     }
61     public Point apply(Point p) { return p; }
62     public Vec apply(Vec v) { return v; }
63     public Matrix invert() { return this; }
64     public Matrix times(Matrix m) { return this; }
65 }