Back to index

wims  3.65+svn20090927
Matrix3D.java
Go to the documentation of this file.
00001 /*
00002  * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
00003  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
00004  */
00005 
00008 class Matrix3D {
00009     float xx, xy, xz, xo;
00010     float yx, yy, yz, yo;
00011     float zx, zy, zz, zo;
00012     static final double pi = 3.14159265;
00014     Matrix3D () {
00015        xx = 1.0f;
00016        yy = 1.0f;
00017        zz = 1.0f;
00018     }
00020     void scale(float f) {
00021        xx *= f;
00022        xy *= f;
00023        xz *= f;
00024        xo *= f;
00025        yx *= f;
00026        yy *= f;
00027        yz *= f;
00028        yo *= f;
00029        zx *= f;
00030        zy *= f;
00031        zz *= f;
00032        zo *= f;
00033     }
00035     void scale(float xf, float yf, float zf) {
00036        xx *= xf;
00037        xy *= xf;
00038        xz *= xf;
00039        xo *= xf;
00040        yx *= yf;
00041        yy *= yf;
00042        yz *= yf;
00043        yo *= yf;
00044        zx *= zf;
00045        zy *= zf;
00046        zz *= zf;
00047        zo *= zf;
00048     }
00050     void translate(float x, float y, float z) {
00051        xo += x;
00052        yo += y;
00053        zo += z;
00054     }
00056     void yrot(double theta) {
00057        theta *= (pi / 180);
00058        double ct = Math.cos(theta);
00059        double st = Math.sin(theta);
00060 
00061        float Nxx = (float) (xx * ct + zx * st);
00062        float Nxy = (float) (xy * ct + zy * st);
00063        float Nxz = (float) (xz * ct + zz * st);
00064        float Nxo = (float) (xo * ct + zo * st);
00065 
00066        float Nzx = (float) (zx * ct - xx * st);
00067        float Nzy = (float) (zy * ct - xy * st);
00068        float Nzz = (float) (zz * ct - xz * st);
00069        float Nzo = (float) (zo * ct - xo * st);
00070 
00071        xo = Nxo;
00072        xx = Nxx;
00073        xy = Nxy;
00074        xz = Nxz;
00075        zo = Nzo;
00076        zx = Nzx;
00077        zy = Nzy;
00078        zz = Nzz;
00079     }
00081     void xrot(double theta) {
00082        theta *= (pi / 180);
00083        double ct = Math.cos(theta);
00084        double st = Math.sin(theta);
00085 
00086        float Nyx = (float) (yx * ct + zx * st);
00087        float Nyy = (float) (yy * ct + zy * st);
00088        float Nyz = (float) (yz * ct + zz * st);
00089        float Nyo = (float) (yo * ct + zo * st);
00090 
00091        float Nzx = (float) (zx * ct - yx * st);
00092        float Nzy = (float) (zy * ct - yy * st);
00093        float Nzz = (float) (zz * ct - yz * st);
00094        float Nzo = (float) (zo * ct - yo * st);
00095 
00096        yo = Nyo;
00097        yx = Nyx;
00098        yy = Nyy;
00099        yz = Nyz;
00100        zo = Nzo;
00101        zx = Nzx;
00102        zy = Nzy;
00103        zz = Nzz;
00104     }
00106     void zrot(double theta) {
00107        theta *= (pi / 180);
00108        double ct = Math.cos(theta);
00109        double st = Math.sin(theta);
00110 
00111        float Nyx = (float) (yx * ct + xx * st);
00112        float Nyy = (float) (yy * ct + xy * st);
00113        float Nyz = (float) (yz * ct + xz * st);
00114        float Nyo = (float) (yo * ct + xo * st);
00115 
00116        float Nxx = (float) (xx * ct - yx * st);
00117        float Nxy = (float) (xy * ct - yy * st);
00118        float Nxz = (float) (xz * ct - yz * st);
00119        float Nxo = (float) (xo * ct - yo * st);
00120 
00121        yo = Nyo;
00122        yx = Nyx;
00123        yy = Nyy;
00124        yz = Nyz;
00125        xo = Nxo;
00126        xx = Nxx;
00127        xy = Nxy;
00128        xz = Nxz;
00129     }
00131     void mult(Matrix3D rhs) {
00132        float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz;
00133        float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz;
00134        float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz;
00135        float lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + rhs.xo;
00136 
00137        float lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz;
00138        float lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz;
00139        float lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz;
00140        float lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + rhs.yo;
00141 
00142        float lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz;
00143        float lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz;
00144        float lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz;
00145        float lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + rhs.zo;
00146 
00147        xx = lxx;
00148        xy = lxy;
00149        xz = lxz;
00150        xo = lxo;
00151 
00152        yx = lyx;
00153        yy = lyy;
00154        yz = lyz;
00155        yo = lyo;
00156 
00157        zx = lzx;
00158        zy = lzy;
00159        zz = lzz;
00160        zo = lzo;
00161     }
00162 
00164     void unit() {
00165        xo = 0;
00166        xx = 1;
00167        xy = 0;
00168        xz = 0;
00169        yo = 0;
00170        yx = 0;
00171        yy = 1;
00172        yz = 0;
00173        zo = 0;
00174        zx = 0;
00175        zy = 0;
00176        zz = 1;
00177     }
00182     void transform(float v[], int tv[], int nvert) {
00183        float lxx = xx, lxy = xy, lxz = xz, lxo = xo;
00184        float lyx = yx, lyy = yy, lyz = yz, lyo = yo;
00185        float lzx = zx, lzy = zy, lzz = zz, lzo = zo;
00186        for (int i = nvert * 3; (i -= 3) >= 0;) {
00187            float x = v[i];
00188            float y = v[i + 1];
00189            float z = v[i + 2];
00190            tv[i    ] = (int) (x * lxx + y * lxy + z * lxz + lxo);
00191            tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo);
00192            tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo);
00193        }
00194     }
00195     public String toString() {
00196        return ("[" + xo + "," + xx + "," + xy + "," + xz + ";"
00197               + yo + "," + yx + "," + yy + "," + yz + ";"
00198               + zo + "," + zx + "," + zy + "," + zz + "]");
00199     }
00200 }