📄 matrix3d.java
字号:
/*
* Generic 3D Model for Java Virtual Cube
* --------------------------------------
*
* Copyright 1996, Song Li
* URL: http://www.cs.umbc.edu/~sli2
*
* Portion of Program by David W. Liu
* URL: http://reality.sgi.com/employees/davidliu_mti
*
*
* You can use the code for any nonprofittable use. But remember to mention
* the authors' names in your revised program. You are also encouraged to
* improve the program or give your comments and bug reports. If there are
* further questions, please contact me and I'll be glad to help. My E-Mail
* address is: sli2@gl.umbc.edu.
*
*/
class Matrix3D {
float xx, xy, xz, xd ;
float yx, yy, yz, yd ;
float zx, zy, zz, zd ;
float dx, dy, dz, dd ;
static final double pi = 3.14159265;
Matrix3D () {
xx = 1.0f ;
yy = 1.0f ;
zz = 1.0f ;
dd = 1.0f ;
}
Matrix3D (float mat [][]) {
xx = mat [0] [0] ;
xy = mat [0] [1] ;
xz = mat [0] [2] ;
xd = mat [0] [3] ;
yx = mat [1] [0] ;
yy = mat [1] [1] ;
yz = mat [1] [2] ;
yd = mat [1] [3] ;
zx = mat [2] [0] ;
zy = mat [2] [1] ;
zz = mat [2] [2] ;
zd = mat [2] [3] ;
dx = mat [3] [0] ;
dy = mat [3] [1] ;
dz = mat [3] [2] ;
dd = mat [3] [3] ;
}
void SetValue (float xx, float xy, float xz,
float yx, float yy, float yz,
float zx, float zy, float zz) {
this.xx = xx ;
this.xy = xy ;
this.xz = xz ;
this.yx = yx ;
this.yy = yy ;
this.yz = yz ;
this.zx = zx ;
this.zy = zy ;
this.zz = zz ;
this.xd = 0 ;
this.yd = 0 ;
this.zd = 0 ;
this.dx = 0 ;
this.dy = 0 ;
this.dz = 0 ;
this.dd = 1 ;
}
void PreScale (float factorx, float factory, float factorz) {
xx *= factorx ;
yx *= factorx ;
zx *= factorx ;
dx *= factorx ;
xy *= factory ;
yy *= factory ;
zy *= factory ;
dy *= factory ;
xz *= factorz ;
yz *= factorz ;
zz *= factorz ;
dz *= factorz ;
}
void ProScale (float factorx, float factory, float factorz) {
xx *= factorx ;
xy *= factorx ;
xz *= factorx ;
xd *= factorx ;
yx *= factory ;
yy *= factory ;
yz *= factory ;
yd *= factory ;
zx *= factorz ;
zy *= factorz ;
zz *= factorz ;
zd *= factorz ;
}
void PreTranslate (float x, float y, float z) {
xd += x * xx ;
yd += y * yy ;
zd += z * zz ;
}
void ProTranslate (float x, float y, float z) {
xx += x * dx ;
xy += x * dy ;
xz += x * dz ;
xd += x * dd ;
yx += y * dx ;
yy += y * dy ;
yz += y * dz ;
yd += y * dd ;
zx += z * dx ;
zy += z * dy ;
zz += z * dz ;
zd += z * dd ;
}
void PreXRotate (double theta) {
double cost = Math.cos (theta);
double sint = Math.sin (theta);
float nxy = (float) (xy * cost + xz * sint) ;
float nyy = (float) (yy * cost + yz * sint) ;
float nzy = (float) (zy * cost + zz * sint) ;
float ndy = (float) (dy * cost + dz * sint) ;
float nxz = (float) (xz * cost - xy * sint) ;
float nyz = (float) (yz * cost - yy * sint) ;
float nzz = (float) (zz * cost - zy * sint) ;
float ndz = (float) (dz * cost - dy * sint) ;
xy = nxy ;
yy = nyy ;
zy = nzy ;
dy = ndy ;
xz = nxz ;
yz = nyz ;
zz = nzz ;
dz = ndz ;
}
void PreYRotate (double theta) {
double cost = Math.cos (theta);
double sint = Math.sin (theta);
float nxz = (float) (xz * cost + xx * sint) ;
float nyz = (float) (yz * cost + yx * sint) ;
float nzz = (float) (zz * cost + zx * sint) ;
float ndz = (float) (dz * cost + dx * sint) ;
float nxx = (float) (xx * cost - xz * sint) ;
float nyx = (float) (yx * cost - yz * sint) ;
float nzx = (float) (zx * cost - zz * sint) ;
float ndx = (float) (dx * cost - dz * sint) ;
xz = nxz ;
yz = nyz ;
zz = nzz ;
dz = ndz ;
xx = nxx ;
yx = nyx ;
zx = nzx ;
dx = ndx ;
}
void PreZRotate (double theta) {
double cost = Math.cos (theta);
double sint = Math.sin (theta);
float nxx = (float) (xx * cost + xy * sint) ;
float nyx = (float) (yx * cost + yy * sint) ;
float nzx = (float) (zx * cost + zy * sint) ;
float ndx = (float) (dx * cost + dy * sint) ;
float nxy = (float) (xy * cost - xx * sint) ;
float nyy = (float) (yy * cost - yx * sint) ;
float nzy = (float) (zy * cost - zx * sint) ;
float ndy = (float) (dy * cost - dx * sint) ;
xx = nxx ;
yx = nyx ;
zx = nzx ;
dx = ndx ;
xy = nxy ;
yy = nyy ;
zy = nzy ;
dy = ndy ;
}
void ProXRotate (double theta) {
double cost = Math.cos (theta);
double sint = Math.sin (theta);
float nyx = (float) (yx * cost - zx * sint) ;
float nyy = (float) (yy * cost - zy * sint) ;
float nyz = (float) (yz * cost - zz * sint) ;
float nyd = (float) (yd * cost - zd * sint) ;
float nzx = (float) (zx * cost + yx * sint) ;
float nzy = (float) (zy * cost + yy * sint) ;
float nzz = (float) (zz * cost + yz * sint) ;
float nzd = (float) (zd * cost + yd * sint) ;
yx = nyx ;
yy = nyy ;
yz = nyz ;
yd = nyd ;
zx = nzx ;
zy = nzy ;
zz = nzz ;
zd = nzd ;
}
void ProYRotate (double theta) {
double cost = Math.cos (theta);
double sint = Math.sin (theta);
float nzx = (float) (zx * cost - xx * sint) ;
float nzy = (float) (zy * cost - xy * sint) ;
float nzz = (float) (zz * cost - xz * sint) ;
float nzd = (float) (zd * cost - xd * sint) ;
float nxx = (float) (xx * cost + zx * sint) ;
float nxy = (float) (xy * cost + zy * sint) ;
float nxz = (float) (xz * cost + zz * sint) ;
float nxd = (float) (xd * cost + zd * sint) ;
zx = nzx ;
zy = nzy ;
zz = nzz ;
zd = nzd ;
xx = nxx ;
xy = nxy ;
xz = nxz ;
xd = nxd ;
}
void ProZRotate (double theta) {
double cost = Math.cos (theta);
double sint = Math.sin (theta);
float nxx = (float) (xx * cost - yx * sint) ;
float nxy = (float) (xy * cost - yy * sint) ;
float nxz = (float) (xz * cost - yz * sint) ;
float nxd = (float) (xd * cost - yd * sint) ;
float nyx = (float) (yx * cost + xx * sint) ;
float nyy = (float) (yy * cost + xy * sint) ;
float nyz = (float) (yz * cost + xz * sint) ;
float nyd = (float) (yd * cost + xd * sint) ;
xx = nxx ;
xy = nxy ;
xz = nxz ;
xd = nxd ;
yx = nyx ;
yy = nyy ;
yz = nyz ;
yd = nyd ;
}
void Unify () {
xx = 1; xy = 0; xz = 0; xd = 0;
yx = 0; yy = 1; yz = 0; yd = 0;
zx = 0; zy = 0; zz = 1; zd = 0;
dx = 0; dy = 0; dz = 0; dd = 1;
}
/*
* Transform nvert points from v into tv. v contains the input
* coordinates in floating point. tv ends up holding the transformed
* points.
*
*/
void Transform (Vertex v [], Vertex tv [], int vernum) {
for (int i=0; i<vernum; i++) {
float x = v[i].x;
float y = v[i].y;
float z = v[i].z;
float d = x*dx + y*dy + z*dz + dd ;
tv[i].x = (x*xx + y*xy + z*xz + xd) / d ;
tv[i].y = (x*yx + y*yy + z*yz + yd) / d ;
tv[i].z = (x*zx + y*zy + z*zz + zd) / d ;
}
}
void Transform (Vertex v, Vertex tv) {
float x = v.x;
float y = v.y;
float z = v.z;
float d = x*dx + y*dy + z*dz + dd ;
tv.x = (x*xx + y*xy + z*xz + xd) / d ;
tv.y = (x*yx + y*yy + z*yz + yd) / d ;
tv.z = (x*zx + y*zy + z*zz + zd) / d ;
}
Vertex Origin () {
float A41, A42, A43, A44 ;
float det ;
A41 = - (xy*yz*zd + yy*zz*xd + zy*xz*yd - xd*yz*zy - yd*zz*xy - zd*xz*yy) ;
A42 = xx*yz*zd + yx*zz*xd + zx*xz*yd - xd*yz*zx - yd*zz*xx - zd*xz*yx ;
A43 = - (xx*yy*zd + yx*zy*xd + zx*xy*yd - xd*yy*zx - yd*zy*xx - zd*xy*yx) ;
A44 = xx*yy*zz + yx*zy*xz + zx*xy*yz - xz*yy*zx - yz*zy*xx - zz*xy*yx ;
det = dx*A41 + dy*A42 + dz*A43 + dd*A44 ;
float x = A41 / det ;
float y = A42 / det ;
float z = A43 / det ;
float d = A44 / det ;
return new Vertex (x/d, y/d, z/d) ;
}
Matrix3D Inverse () {
float A11, A12, A13, A14 ;
float A21, A22, A23, A24 ;
float A31, A32, A33, A34 ;
float A41, A42, A43, A44 ;
A11 = yy*zz*dd + zy*dz*yd + dy*yz*zd - yd*zz*dy - zd*dz*yy - dd*yz*zy ;
A12 = -(yx*zz*dd + zx*dz*yd + dx*yz*zd - yd*zz*dx - zd*dz*yx - dd*yz*zx) ;
A13 = yx*zy*dd + zx*dy*yd + dx*yy*zd - yd*zy*dx - zd*dy*yx - dd*yy*zx ;
A14 = -(yx*zy*dz + zx*dy*yz + dx*yy*zz - yz*zy*dx - zz*dy*yx - dz*yy*zx) ;
A21 = -(xy*zz*dd + zy*dz*xd + dy*xz*zd - xd*zz*dy - zd*dz*xy - dd*xz*zy) ;
A22 = xx*zz*dd + zx*dz*xd + dx*xz*zd - xd*zz*dx - zd*dz*xx - dd*xz*zx ;
A23 = -(xx*zy*dd + zx*dy*xd + dx*xy*zd - xd*zy*dx - zd*dy*xx - dd*xy*zx) ;
A24 = xx*zy*dz + zx*dy*xz + dx*xy*zz - xz*zy*dx - zz*dy*xx - dz*xy*zx ;
A31 = xy*yz*dd + yy*dz*xd + dy*xz*yd - xd*yz*dy - yd*dz*xy - dd*xz*yy ;
A32 = -(xx*yz*dd + yx*dz*xd + dx*xz*yd - xd*yz*dx - yd*dz*xx - dd*xz*yx) ;
A33 = xx*yy*dd + yx*dy*xd + dx*xy*yd - xd*yy*dx - yd*dy*xx - dd*xy*yx ;
A34 = -(xx*yy*dz + yx*dy*xz + dx*xy*yz - xz*yy*dx - yz*dy*xx - dz*xy*yx) ;
A41 = -(xy*yz*zd + yy*zz*xd + zy*xz*yd - xd*yz*zy - yd*zz*xy - zd*xz*yy) ;
A42 = xx*yz*zd + yx*zz*xd + zx*xz*yd - xd*yz*zx - yd*zz*xx - zd*xz*yx ;
A43 = -(xx*yy*zd + yx*zy*xd + zx*xy*yd - xd*yy*zx - yd*zy*xx - zd*xy*yx) ;
A44 = xx*yy*zz + yx*zy*xz + zx*xy*yz - xz*yy*zx - yz*zy*xx - zz*xy*yx ;
float det = xx*A11 + xy*A12 + xz*A13 + xd*A14 ;
float mat [] [] = {
{A11/det, A21/det, A31/det, A41/det},
{A12/det, A22/det, A32/det, A42/det},
{A13/det, A23/det, A33/det, A43/det},
{A14/det, A24/det, A34/det, A44/det}} ;
return new Matrix3D (mat) ;
}
public void Print () {
System.out.println ("--------------------") ;
System.out.println ("|" + xx + " " + xy + " " + xz + " " + xd + "| ") ;
System.out.println ("|" + yx + " " + yy + " " + yz + " " + yd + "| ") ;
System.out.println ("|" + zx + " " + zy + " " + zz + " " + zd + "| ") ;
System.out.println ("|" + dx + " " + dy + " " + dz + " " + dd + "| ") ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -