Back to index

wims  3.65+svn20090927
Matrix3D.java
Go to the documentation of this file.
00001 /*
00002  * @(#)Matrix3D.java 1.2 96/12/06
00003  *
00004  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
00005  *
00006  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
00007  * modify and redistribute this software in source and binary code form,
00008  * provided that i) this copyright notice and license appear on all copies of
00009  * the software; and ii) Licensee does not utilize the software in a manner
00010  * which is disparaging to Sun.
00011  *
00012  * This software is provided "AS IS," without a warranty of any kind. ALL
00013  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
00014  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
00015  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
00016  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
00017  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
00018  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
00019  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
00020  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
00021  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
00022  * POSSIBILITY OF SUCH DAMAGES.
00023  *
00024  * This software is not designed or intended for use in on-line control of
00025  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
00026  * the design, construction, operation or maintenance of any nuclear
00027  * facility. Licensee represents and warrants that it will not use or
00028  * redistribute the Software for such purposes.
00029  */
00030 
00033 class Matrix3D {
00034     float xx, xy, xz, xo;
00035     float yx, yy, yz, yo;
00036     float zx, zy, zz, zo;
00037     static final double pi = 3.14159265;
00039     Matrix3D () {
00040        xx = 1.0f;
00041        yy = 1.0f;
00042        zz = 1.0f;
00043     }
00045     void scale(float f) {
00046        xx *= f;
00047        xy *= f;
00048        xz *= f;
00049        xo *= f;
00050        yx *= f;
00051        yy *= f;
00052        yz *= f;
00053        yo *= f;
00054        zx *= f;
00055        zy *= f;
00056        zz *= f;
00057        zo *= f;
00058     }
00060     void scale(float xf, float yf, float zf) {
00061        xx *= xf;
00062        xy *= xf;
00063        xz *= xf;
00064        xo *= xf;
00065        yx *= yf;
00066        yy *= yf;
00067        yz *= yf;
00068        yo *= yf;
00069        zx *= zf;
00070        zy *= zf;
00071        zz *= zf;
00072        zo *= zf;
00073     }
00075     void translate(float x, float y, float z) {
00076        xo += x;
00077        yo += y;
00078        zo += z;
00079     }
00081     void yrot(double theta) {
00082        theta *= (pi / 180);
00083        double ct = Math.cos(theta);
00084        double st = Math.sin(theta);
00085 
00086        float Nxx = (float) (xx * ct + zx * st);
00087        float Nxy = (float) (xy * ct + zy * st);
00088        float Nxz = (float) (xz * ct + zz * st);
00089        float Nxo = (float) (xo * ct + zo * st);
00090 
00091        float Nzx = (float) (zx * ct - xx * st);
00092        float Nzy = (float) (zy * ct - xy * st);
00093        float Nzz = (float) (zz * ct - xz * st);
00094        float Nzo = (float) (zo * ct - xo * st);
00095 
00096        xo = Nxo;
00097        xx = Nxx;
00098        xy = Nxy;
00099        xz = Nxz;
00100        zo = Nzo;
00101        zx = Nzx;
00102        zy = Nzy;
00103        zz = Nzz;
00104     }
00106     void xrot(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 + zx * st);
00112        float Nyy = (float) (yy * ct + zy * st);
00113        float Nyz = (float) (yz * ct + zz * st);
00114        float Nyo = (float) (yo * ct + zo * st);
00115 
00116        float Nzx = (float) (zx * ct - yx * st);
00117        float Nzy = (float) (zy * ct - yy * st);
00118        float Nzz = (float) (zz * ct - yz * st);
00119        float Nzo = (float) (zo * ct - yo * st);
00120 
00121        yo = Nyo;
00122        yx = Nyx;
00123        yy = Nyy;
00124        yz = Nyz;
00125        zo = Nzo;
00126        zx = Nzx;
00127        zy = Nzy;
00128        zz = Nzz;
00129     }
00131     void zrot(double theta) {
00132        theta *= (pi / 180);
00133        double ct = Math.cos(theta);
00134        double st = Math.sin(theta);
00135 
00136        float Nyx = (float) (yx * ct + xx * st);
00137        float Nyy = (float) (yy * ct + xy * st);
00138        float Nyz = (float) (yz * ct + xz * st);
00139        float Nyo = (float) (yo * ct + xo * st);
00140 
00141        float Nxx = (float) (xx * ct - yx * st);
00142        float Nxy = (float) (xy * ct - yy * st);
00143        float Nxz = (float) (xz * ct - yz * st);
00144        float Nxo = (float) (xo * ct - yo * st);
00145 
00146        yo = Nyo;
00147        yx = Nyx;
00148        yy = Nyy;
00149        yz = Nyz;
00150        xo = Nxo;
00151        xx = Nxx;
00152        xy = Nxy;
00153        xz = Nxz;
00154     }
00156     void mult(Matrix3D rhs) {
00157        float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz;
00158        float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz;
00159        float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz;
00160        float lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + rhs.xo;
00161 
00162        float lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz;
00163        float lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz;
00164        float lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz;
00165        float lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + rhs.yo;
00166 
00167        float lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz;
00168        float lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz;
00169        float lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz;
00170        float lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + rhs.zo;
00171 
00172        xx = lxx;
00173        xy = lxy;
00174        xz = lxz;
00175        xo = lxo;
00176 
00177        yx = lyx;
00178        yy = lyy;
00179        yz = lyz;
00180        yo = lyo;
00181 
00182        zx = lzx;
00183        zy = lzy;
00184        zz = lzz;
00185        zo = lzo;
00186     }
00187 
00189     void unit() {
00190        xo = 0;
00191        xx = 1;
00192        xy = 0;
00193        xz = 0;
00194        yo = 0;
00195        yx = 0;
00196        yy = 1;
00197        yz = 0;
00198        zo = 0;
00199        zx = 0;
00200        zy = 0;
00201        zz = 1;
00202     }
00207     void transform(float v[], int tv[], int nvert) {
00208        float lxx = xx, lxy = xy, lxz = xz, lxo = xo;
00209        float lyx = yx, lyy = yy, lyz = yz, lyo = yo;
00210        float lzx = zx, lzy = zy, lzz = zz, lzo = zo;
00211        for (int i = nvert * 3; (i -= 3) >= 0;) {
00212            float x = v[i];
00213            float y = v[i + 1];
00214            float z = v[i + 2];
00215            tv[i    ] = (int) (x * lxx + y * lxy + z * lxz + lxo);
00216            tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo);
00217            tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo);
00218        }
00219     }
00220     public String toString() {
00221        return ("[" + xo + "," + xx + "," + xy + "," + xz + ";"
00222               + yo + "," + yx + "," + yy + "," + yz + ";"
00223               + zo + "," + zx + "," + zy + "," + zz + "]");
00224     }
00225 }