1 package edu.berkeley.qfat.geom;
3 /** affine matrix; immutable */
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) {
15 l = h = d = e = b = i = c = j = g = 0;
19 public Matrix(float scalex, float scaley, float scalez) {
23 l = h = d = e = b = i = c = j = g = 0;
27 public Matrix(Vec translate) {
28 d = translate.x; h = translate.y; l = translate.z;
30 b = c = e = g = i = j = 0;
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;
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);
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);
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);
49 /** computes (v^T)(this)(v) */
50 public float preAndPostMultiply(Point point) {
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);
59 public Matrix(Vec axis, float angle) {
60 double q = Math.cos(angle);
61 double s = Math.sin(angle);
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;
72 i = (float)(tmp1 - tmp2);
73 c = (float)(tmp1 + tmp2);
74 tmp1 = axis.y*axis.z*t;
76 j = (float)(tmp1 + tmp2);
77 g = (float)(tmp1 - tmp2);
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);
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);
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; }