一、前言
关于j3d源码包中org.j3d.util.MatrixUtils数据矩阵工具类,基于javax.vecmath的图像3d/4d渲染进行通用处理,详情参见源码说明部分。
二、源码说明
package org.j3d.util;@b@@b@import javax.vecmath.AxisAngle4d;@b@import javax.vecmath.AxisAngle4f;@b@import javax.vecmath.Matrix4d;@b@import javax.vecmath.Matrix4f;@b@import javax.vecmath.Point3d;@b@import javax.vecmath.Point3f;@b@import javax.vecmath.Vector3d;@b@import javax.vecmath.Vector3f;@b@@b@public class MatrixUtils@b@{@b@ private AxisAngle4f orient;@b@ private AxisAngle4d orientd;@b@ private float[] tempMat3;@b@ private float[] tempMat4;@b@ private float[] resMat4;@b@@b@ public MatrixUtils()@b@ {@b@ this.tempMat3 = new float[9];@b@ this.tempMat4 = new float[16];@b@ this.resMat4 = new float[16];@b@ }@b@@b@ public void lookAt(Point3f paramPoint3f1, Point3f paramPoint3f2, Vector3f paramVector3f, Matrix4f paramMatrix4f)@b@ {@b@ double d1 = paramPoint3f1.x - paramPoint3f2.x;@b@ double d2 = paramPoint3f1.y - paramPoint3f2.y;@b@ double d3 = paramPoint3f1.z - paramPoint3f2.z;@b@@b@ double d4 = d1 * d1 + d2 * d2 + d3 * d3;@b@ if (d4 != 1.0D)@b@ {@b@ if (d4 == 0.0D)@b@ {@b@ paramMatrix4f.setIdentity();@b@ paramMatrix4f.m03 = paramPoint3f1.x;@b@ paramMatrix4f.m13 = paramPoint3f1.y;@b@ paramMatrix4f.m23 = paramPoint3f1.z;@b@ return;@b@ }@b@@b@ d4 = 1.0D / Math.sqrt(d4);@b@ d1 *= d4;@b@ d2 *= d4;@b@ d3 *= d4;@b@ }@b@@b@ double d5 = paramVector3f.x;@b@ double d6 = paramVector3f.y;@b@ double d7 = paramVector3f.z;@b@@b@ d4 = paramVector3f.x * paramVector3f.x + paramVector3f.y * paramVector3f.y + paramVector3f.z * paramVector3f.z;@b@ if (d4 != 1.0D)@b@ {@b@ if (d4 == 0.0D)@b@ throw new IllegalArgumentException("Up vector is all zeroes");@b@@b@ d4 = 1.0D / Math.sqrt(d4);@b@ d5 *= d4;@b@ d6 *= d4;@b@ d7 *= d4;@b@ }@b@@b@ double d8 = d6 * d3 - d2 * d7;@b@ double d9 = d7 * d1 - d5 * d3;@b@ double d10 = d5 * d2 - d6 * d1;@b@@b@ d4 = d8 * d8 + d9 * d9 + d10 * d10;@b@@b@ if (d4 != 1.0D)@b@ {@b@ if (d4 == 0.0D)@b@ {@b@ lookAtFallback(paramPoint3f1, (float)d1, (float)d2, (float)d3, paramMatrix4f);@b@ return;@b@ }@b@@b@ d4 = 1.0D / Math.sqrt(d4);@b@ d8 *= d4;@b@ d9 *= d4;@b@ d10 *= d4;@b@ }@b@@b@ d5 = d2 * d10 - d9 * d3;@b@ d6 = d3 * d8 - d1 * d10;@b@ d7 = d1 * d9 - d2 * d8;@b@@b@ paramMatrix4f.m00 = (float)d8;@b@ paramMatrix4f.m01 = (float)d9;@b@ paramMatrix4f.m02 = (float)d10;@b@ paramMatrix4f.m03 = (-paramPoint3f1.x * paramMatrix4f.m00 - paramPoint3f1.y * paramMatrix4f.m01 - paramPoint3f1.z * paramMatrix4f.m02);@b@@b@ paramMatrix4f.m10 = (float)d5;@b@ paramMatrix4f.m11 = (float)d6;@b@ paramMatrix4f.m12 = (float)d7;@b@ paramMatrix4f.m13 = (-paramPoint3f1.x * paramMatrix4f.m10 - paramPoint3f1.y * paramMatrix4f.m11 - paramPoint3f1.z * paramMatrix4f.m12);@b@@b@ paramMatrix4f.m20 = (float)d1;@b@ paramMatrix4f.m21 = (float)d2;@b@ paramMatrix4f.m22 = (float)d3;@b@ paramMatrix4f.m23 = (-paramPoint3f1.x * paramMatrix4f.m20 - paramPoint3f1.y * paramMatrix4f.m21 - paramPoint3f1.z * paramMatrix4f.m22);@b@@b@ paramMatrix4f.m30 = 0.0F;@b@ paramMatrix4f.m31 = 0.0F;@b@ paramMatrix4f.m32 = 0.0F;@b@ paramMatrix4f.m33 = 1.0F;@b@ }@b@@b@ public void lookAt(Point3d paramPoint3d1, Point3d paramPoint3d2, Vector3d paramVector3d, Matrix4d paramMatrix4d)@b@ {@b@ double d1 = paramPoint3d1.x - paramPoint3d2.x;@b@ double d2 = paramPoint3d1.y - paramPoint3d2.y;@b@ double d3 = paramPoint3d1.z - paramPoint3d2.z;@b@@b@ double d4 = d1 * d1 + d2 * d2 + d3 * d3;@b@ if (d4 != 1.0D)@b@ {@b@ if (d4 == 0.0D)@b@ {@b@ paramMatrix4d.setIdentity();@b@ paramMatrix4d.m03 = paramPoint3d1.x;@b@ paramMatrix4d.m13 = paramPoint3d1.y;@b@ paramMatrix4d.m23 = paramPoint3d1.z;@b@ return;@b@ }@b@@b@ d4 = 1.0D / Math.sqrt(d4);@b@ d1 *= d4;@b@ d2 *= d4;@b@ d3 *= d4;@b@ }@b@@b@ double d5 = paramVector3d.x;@b@ double d6 = paramVector3d.y;@b@ double d7 = paramVector3d.z;@b@@b@ d4 = paramVector3d.x * paramVector3d.x + paramVector3d.y * paramVector3d.y + paramVector3d.z * paramVector3d.z;@b@ if (d4 != 1.0D)@b@ {@b@ if (d4 == 0.0D)@b@ throw new IllegalArgumentException("Up vector is all zeroes");@b@@b@ d4 = 1.0D / Math.sqrt(d4);@b@ d5 *= d4;@b@ d6 *= d4;@b@ d7 *= d4;@b@ }@b@@b@ double d8 = d6 * d3 - d2 * d7;@b@ double d9 = d7 * d1 - d5 * d3;@b@ double d10 = d5 * d2 - d6 * d1;@b@@b@ d4 = d8 * d8 + d9 * d9 + d10 * d10;@b@@b@ if (d4 != 1.0D)@b@ {@b@ if (d4 == 0.0D)@b@ {@b@ lookAtFallback(paramPoint3d1, d1, d2, d3, paramMatrix4d);@b@ return;@b@ }@b@@b@ d4 = 1.0D / Math.sqrt(d4);@b@ d8 *= d4;@b@ d9 *= d4;@b@ d10 *= d4;@b@ }@b@@b@ d5 = d2 * d10 - d9 * d3;@b@ d6 = d3 * d8 - d1 * d10;@b@ d7 = d1 * d9 - d2 * d8;@b@@b@ paramMatrix4d.m00 = d8;@b@ paramMatrix4d.m01 = d9;@b@ paramMatrix4d.m02 = d10;@b@ paramMatrix4d.m03 = (-paramPoint3d1.x * paramMatrix4d.m00 - paramPoint3d1.y * paramMatrix4d.m01 - paramPoint3d1.z * paramMatrix4d.m02);@b@@b@ paramMatrix4d.m10 = d5;@b@ paramMatrix4d.m11 = d6;@b@ paramMatrix4d.m12 = d7;@b@ paramMatrix4d.m13 = (-paramPoint3d1.x * paramMatrix4d.m10 - paramPoint3d1.y * paramMatrix4d.m11 - paramPoint3d1.z * paramMatrix4d.m12);@b@@b@ paramMatrix4d.m20 = d1;@b@ paramMatrix4d.m21 = d2;@b@ paramMatrix4d.m22 = d3;@b@ paramMatrix4d.m23 = (-paramPoint3d1.x * paramMatrix4d.m20 - paramPoint3d1.y * paramMatrix4d.m21 - paramPoint3d1.z * paramMatrix4d.m22);@b@@b@ paramMatrix4d.m30 = 0.0D;@b@ paramMatrix4d.m31 = 0.0D;@b@ paramMatrix4d.m32 = 0.0D;@b@ paramMatrix4d.m33 = 1.0D;@b@ }@b@@b@ public void setEuler(Vector3f paramVector3f, Matrix4f paramMatrix4f)@b@ {@b@ setEuler(paramVector3f.x, paramVector3f.y, paramVector3f.z, paramMatrix4f);@b@ }@b@@b@ public void setEuler(float paramFloat1, float paramFloat2, float paramFloat3, Matrix4f paramMatrix4f)@b@ {@b@ float f1 = (float)Math.cos(paramFloat1);@b@ float f2 = (float)Math.sin(paramFloat1);@b@ float f3 = (float)Math.cos(paramFloat2);@b@ float f4 = (float)Math.sin(paramFloat2);@b@ float f5 = (float)Math.cos(paramFloat3);@b@ float f6 = (float)Math.sin(paramFloat3);@b@ float f7 = f1 * f4;@b@ float f8 = f2 * f4;@b@@b@ paramMatrix4f.m00 = (f3 * f5);@b@ paramMatrix4f.m01 = (-f3 * f6);@b@ paramMatrix4f.m02 = f4;@b@ paramMatrix4f.m03 = 0.0F;@b@@b@ paramMatrix4f.m10 = (f8 * f5 + f1 * f6);@b@ paramMatrix4f.m11 = (-f8 * f6 + f1 * f5);@b@ paramMatrix4f.m12 = (-f2 * f3);@b@ paramMatrix4f.m13 = 0.0F;@b@@b@ paramMatrix4f.m20 = (-f7 * f5 + f2 * f6);@b@ paramMatrix4f.m21 = (f7 * f6 + f2 * f5);@b@ paramMatrix4f.m22 = (f1 * f3);@b@ paramMatrix4f.m23 = 0.0F;@b@@b@ paramMatrix4f.m30 = 0.0F;@b@ paramMatrix4f.m31 = 0.0F;@b@ paramMatrix4f.m33 = 1.0F;@b@ paramMatrix4f.m32 = 0.0F;@b@ }@b@@b@ public void rotateX(float paramFloat, Matrix4f paramMatrix4f)@b@ {@b@ float f1 = (float)Math.cos(paramFloat);@b@ float f2 = (float)Math.sin(paramFloat);@b@@b@ paramMatrix4f.m00 = 1.0F;@b@ paramMatrix4f.m01 = 0.0F;@b@ paramMatrix4f.m02 = 0.0F;@b@ paramMatrix4f.m03 = 0.0F;@b@@b@ paramMatrix4f.m10 = 0.0F;@b@ paramMatrix4f.m11 = f1;@b@ paramMatrix4f.m12 = (-f2);@b@ paramMatrix4f.m13 = 0.0F;@b@@b@ paramMatrix4f.m20 = 0.0F;@b@ paramMatrix4f.m21 = f2;@b@ paramMatrix4f.m22 = f1;@b@ paramMatrix4f.m23 = 0.0F;@b@@b@ paramMatrix4f.m30 = 0.0F;@b@ paramMatrix4f.m31 = 0.0F;@b@ paramMatrix4f.m33 = 1.0F;@b@ paramMatrix4f.m32 = 0.0F;@b@ }@b@@b@ public void rotateY(float paramFloat, Matrix4f paramMatrix4f)@b@ {@b@ float f1 = (float)Math.cos(paramFloat);@b@ float f2 = (float)Math.sin(paramFloat);@b@@b@ paramMatrix4f.m00 = f1;@b@ paramMatrix4f.m01 = 0.0F;@b@ paramMatrix4f.m02 = f2;@b@ paramMatrix4f.m03 = 0.0F;@b@@b@ paramMatrix4f.m10 = 0.0F;@b@ paramMatrix4f.m11 = 1.0F;@b@ paramMatrix4f.m12 = 0.0F;@b@ paramMatrix4f.m13 = 0.0F;@b@@b@ paramMatrix4f.m20 = (-f2);@b@ paramMatrix4f.m21 = 0.0F;@b@ paramMatrix4f.m22 = f1;@b@ paramMatrix4f.m23 = 0.0F;@b@@b@ paramMatrix4f.m30 = 0.0F;@b@ paramMatrix4f.m31 = 0.0F;@b@ paramMatrix4f.m33 = 1.0F;@b@ paramMatrix4f.m32 = 0.0F;@b@ }@b@@b@ public boolean inverse(Matrix4f paramMatrix4f1, Matrix4f paramMatrix4f2)@b@ {@b@ float f = paramMatrix4f1.determinant();@b@@b@ if (Math.abs(f) < 0.0005D)@b@ return false;@b@@b@ f = 1.0F / f;@b@@b@ this.tempMat4[0] = paramMatrix4f1.m00;@b@ this.tempMat4[1] = paramMatrix4f1.m01;@b@ this.tempMat4[2] = paramMatrix4f1.m02;@b@ this.tempMat4[3] = paramMatrix4f1.m03;@b@@b@ this.tempMat4[4] = paramMatrix4f1.m10;@b@ this.tempMat4[5] = paramMatrix4f1.m11;@b@ this.tempMat4[6] = paramMatrix4f1.m12;@b@ this.tempMat4[7] = paramMatrix4f1.m13;@b@@b@ this.tempMat4[8] = paramMatrix4f1.m20;@b@ this.tempMat4[9] = paramMatrix4f1.m21;@b@ this.tempMat4[10] = paramMatrix4f1.m22;@b@ this.tempMat4[11] = paramMatrix4f1.m23;@b@@b@ this.tempMat4[12] = paramMatrix4f1.m30;@b@ this.tempMat4[13] = paramMatrix4f1.m31;@b@ this.tempMat4[14] = paramMatrix4f1.m32;@b@ this.tempMat4[15] = paramMatrix4f1.m33;@b@@b@ for (int i = 0; i < 4; ++i)@b@ {@b@ for (int j = 0; j < 4; ++j)@b@ {@b@ int k = 1 - (i + j) % 2 * 2;@b@ submatrix(i, j);@b@ this.resMat4[(i + j * 4)] = (determinant3x3() * k * f);@b@ }@b@@b@ }@b@@b@ paramMatrix4f2.m00 = this.resMat4[0];@b@ paramMatrix4f2.m01 = this.resMat4[1];@b@ paramMatrix4f2.m02 = this.resMat4[2];@b@ paramMatrix4f2.m03 = this.resMat4[3];@b@@b@ paramMatrix4f2.m10 = this.resMat4[4];@b@ paramMatrix4f2.m11 = this.resMat4[5];@b@ paramMatrix4f2.m12 = this.resMat4[6];@b@ paramMatrix4f2.m13 = this.resMat4[7];@b@@b@ paramMatrix4f2.m20 = this.resMat4[8];@b@ paramMatrix4f2.m21 = this.resMat4[9];@b@ paramMatrix4f2.m22 = this.resMat4[10];@b@ paramMatrix4f2.m23 = this.resMat4[11];@b@@b@ paramMatrix4f2.m30 = this.resMat4[12];@b@ paramMatrix4f2.m31 = this.resMat4[13];@b@ paramMatrix4f2.m32 = this.resMat4[14];@b@ paramMatrix4f2.m33 = this.resMat4[15];@b@@b@ return true;@b@ }@b@@b@ private void submatrix(int paramInt1, int paramInt2)@b@ {@b@ for (int i = 0; i < 3; ++i)@b@ {@b@ for (int j = 0; j < 3; ++j)@b@ {@b@ int k = i + ((i >= paramInt1) ? 1 : 0);@b@ int l = j + ((j >= paramInt2) ? 1 : 0);@b@@b@ this.tempMat3[(i * 3 + j)] = this.tempMat4[(k * 4 + l)];@b@ }@b@ }@b@ }@b@@b@ private float determinant3x3()@b@ {@b@ return (this.tempMat3[0] * (this.tempMat3[4] * this.tempMat3[8] - this.tempMat3[7] * this.tempMat3[5]) - this.tempMat3[1] * (this.tempMat3[3] * this.tempMat3[8] - this.tempMat3[6] * this.tempMat3[5]) + this.tempMat3[2] * (this.tempMat3[3] * this.tempMat3[7] - this.tempMat3[6] * this.tempMat3[4]));@b@ }@b@@b@ private void lookAtFallback(Point3f paramPoint3f, float paramFloat1, float paramFloat2, float paramFloat3, Matrix4f paramMatrix4f)@b@ {@b@ float f1 = paramFloat2;@b@ float f2 = -paramFloat1;@b@ float f3 = 0.0F;@b@@b@ float f4 = paramFloat1 * paramFloat1 + paramFloat2 * paramFloat2 + paramFloat3 * paramFloat3;@b@ if ((f4 != 0.0F) || (f4 != 1.0F))@b@ {@b@ f4 = 1.0F / (float)Math.sqrt(f4);@b@ paramFloat1 *= f4;@b@ paramFloat2 *= f4;@b@ paramFloat3 *= f4;@b@ }@b@@b@ float f5 = -paramFloat3;@b@ float f6 = (float)Math.acos(f5);@b@@b@ if (this.orient == null)@b@ this.orient = new AxisAngle4f(f1, f2, f3, f6);@b@ else@b@ this.orient.set(f1, f2, f3, f6);@b@@b@ paramMatrix4f.set(this.orient);@b@ paramMatrix4f.m03 = paramPoint3f.x;@b@ paramMatrix4f.m13 = paramPoint3f.y;@b@ paramMatrix4f.m23 = paramPoint3f.z;@b@ }@b@@b@ private void lookAtFallback(Point3d paramPoint3d, double paramDouble1, double paramDouble2, double paramDouble3, Matrix4d paramMatrix4d)@b@ {@b@ double d1 = paramDouble2;@b@ double d2 = -paramDouble1;@b@ double d3 = 0.0D;@b@@b@ double d4 = paramDouble1 * paramDouble1 + paramDouble2 * paramDouble2 + paramDouble3 * paramDouble3;@b@ if ((d4 != 0.0D) || (d4 != 1.0D))@b@ {@b@ d4 = 1.0D / Math.sqrt(d4);@b@ paramDouble1 *= d4;@b@ paramDouble2 *= d4;@b@ paramDouble3 *= d4;@b@ }@b@@b@ double d5 = -paramDouble3;@b@ double d6 = Math.acos(d5);@b@@b@ if (this.orientd == null)@b@ this.orientd = new AxisAngle4d(d1, d2, d3, d6);@b@ else@b@ this.orientd.set(d1, d2, d3, d6);@b@@b@ paramMatrix4d.set(this.orientd);@b@ paramMatrix4d.m03 = paramPoint3d.x;@b@ paramMatrix4d.m13 = paramPoint3d.y;@b@ paramMatrix4d.m23 = paramPoint3d.z;@b@ }@b@}