home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / applets / WireFrame / Matrix3D.java < prev    next >
Encoding:
Text File  |  2002-09-06  |  6.4 KB  |  235 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Matrix3D.java    1.7 02/06/13
  38.  */
  39.  
  40. /** A fairly conventional 3D matrix object that can transform sets of
  41.     3D points and perform a variety of manipulations on the transform */
  42. class Matrix3D {
  43.     float xx, xy, xz, xo;
  44.     float yx, yy, yz, yo;
  45.     float zx, zy, zz, zo;
  46.     static final double pi = 3.14159265;
  47.     /** Create a new unit matrix */
  48.     Matrix3D () {
  49.     xx = 1.0f;
  50.     yy = 1.0f;
  51.     zz = 1.0f;
  52.     }
  53.     /** Scale by f in all dimensions */
  54.     void scale(float f) {
  55.     xx *= f;
  56.     xy *= f;
  57.     xz *= f;
  58.     xo *= f;
  59.     yx *= f;
  60.     yy *= f;
  61.     yz *= f;
  62.     yo *= f;
  63.     zx *= f;
  64.     zy *= f;
  65.     zz *= f;
  66.     zo *= f;
  67.     }
  68.     /** Scale along each axis independently */
  69.     void scale(float xf, float yf, float zf) {
  70.     xx *= xf;
  71.     xy *= xf;
  72.     xz *= xf;
  73.     xo *= xf;
  74.     yx *= yf;
  75.     yy *= yf;
  76.     yz *= yf;
  77.     yo *= yf;
  78.     zx *= zf;
  79.     zy *= zf;
  80.     zz *= zf;
  81.     zo *= zf;
  82.     }
  83.     /** Translate the origin */
  84.     void translate(float x, float y, float z) {
  85.     xo += x;
  86.     yo += y;
  87.     zo += z;
  88.     }
  89.     /** rotate theta degrees about the y axis */
  90.     void yrot(double theta) {
  91.     theta *= (pi / 180);
  92.     double ct = Math.cos(theta);
  93.     double st = Math.sin(theta);
  94.  
  95.     float Nxx = (float) (xx * ct + zx * st);
  96.     float Nxy = (float) (xy * ct + zy * st);
  97.     float Nxz = (float) (xz * ct + zz * st);
  98.     float Nxo = (float) (xo * ct + zo * st);
  99.  
  100.     float Nzx = (float) (zx * ct - xx * st);
  101.     float Nzy = (float) (zy * ct - xy * st);
  102.     float Nzz = (float) (zz * ct - xz * st);
  103.     float Nzo = (float) (zo * ct - xo * st);
  104.  
  105.     xo = Nxo;
  106.     xx = Nxx;
  107.     xy = Nxy;
  108.     xz = Nxz;
  109.     zo = Nzo;
  110.     zx = Nzx;
  111.     zy = Nzy;
  112.     zz = Nzz;
  113.     }
  114.     /** rotate theta degrees about the x axis */
  115.     void xrot(double theta) {
  116.     theta *= (pi / 180);
  117.     double ct = Math.cos(theta);
  118.     double st = Math.sin(theta);
  119.  
  120.     float Nyx = (float) (yx * ct + zx * st);
  121.     float Nyy = (float) (yy * ct + zy * st);
  122.     float Nyz = (float) (yz * ct + zz * st);
  123.     float Nyo = (float) (yo * ct + zo * st);
  124.  
  125.     float Nzx = (float) (zx * ct - yx * st);
  126.     float Nzy = (float) (zy * ct - yy * st);
  127.     float Nzz = (float) (zz * ct - yz * st);
  128.     float Nzo = (float) (zo * ct - yo * st);
  129.  
  130.     yo = Nyo;
  131.     yx = Nyx;
  132.     yy = Nyy;
  133.     yz = Nyz;
  134.     zo = Nzo;
  135.     zx = Nzx;
  136.     zy = Nzy;
  137.     zz = Nzz;
  138.     }
  139.     /** rotate theta degrees about the z axis */
  140.     void zrot(double theta) {
  141.     theta *= (pi / 180);
  142.     double ct = Math.cos(theta);
  143.     double st = Math.sin(theta);
  144.  
  145.     float Nyx = (float) (yx * ct + xx * st);
  146.     float Nyy = (float) (yy * ct + xy * st);
  147.     float Nyz = (float) (yz * ct + xz * st);
  148.     float Nyo = (float) (yo * ct + xo * st);
  149.  
  150.     float Nxx = (float) (xx * ct - yx * st);
  151.     float Nxy = (float) (xy * ct - yy * st);
  152.     float Nxz = (float) (xz * ct - yz * st);
  153.     float Nxo = (float) (xo * ct - yo * st);
  154.  
  155.     yo = Nyo;
  156.     yx = Nyx;
  157.     yy = Nyy;
  158.     yz = Nyz;
  159.     xo = Nxo;
  160.     xx = Nxx;
  161.     xy = Nxy;
  162.     xz = Nxz;
  163.     }
  164.     /** Multiply this matrix by a second: M = M*R */
  165.     void mult(Matrix3D rhs) {
  166.     float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz;
  167.     float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz;
  168.     float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz;
  169.     float lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + rhs.xo;
  170.  
  171.     float lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz;
  172.     float lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz;
  173.     float lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz;
  174.     float lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + rhs.yo;
  175.  
  176.     float lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz;
  177.     float lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz;
  178.     float lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz;
  179.     float lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + rhs.zo;
  180.  
  181.     xx = lxx;
  182.     xy = lxy;
  183.     xz = lxz;
  184.     xo = lxo;
  185.  
  186.     yx = lyx;
  187.     yy = lyy;
  188.     yz = lyz;
  189.     yo = lyo;
  190.  
  191.     zx = lzx;
  192.     zy = lzy;
  193.     zz = lzz;
  194.     zo = lzo;
  195.     }
  196.  
  197.     /** Reinitialize to the unit matrix */
  198.     void unit() {
  199.     xo = 0;
  200.     xx = 1;
  201.     xy = 0;
  202.     xz = 0;
  203.     yo = 0;
  204.     yx = 0;
  205.     yy = 1;
  206.     yz = 0;
  207.     zo = 0;
  208.     zx = 0;
  209.     zy = 0;
  210.     zz = 1;
  211.     }
  212.     /** Transform nvert points from v into tv.  v contains the input
  213.         coordinates in floating point.  Three successive entries in
  214.     the array constitute a point.  tv ends up holding the transformed
  215.     points as integers; three successive entries per point */
  216.     void transform(float v[], int tv[], int nvert) {
  217.     float lxx = xx, lxy = xy, lxz = xz, lxo = xo;
  218.     float lyx = yx, lyy = yy, lyz = yz, lyo = yo;
  219.     float lzx = zx, lzy = zy, lzz = zz, lzo = zo;
  220.     for (int i = nvert * 3; (i -= 3) >= 0;) {
  221.         float x = v[i];
  222.         float y = v[i + 1];
  223.         float z = v[i + 2];
  224.         tv[i    ] = (int) (x * lxx + y * lxy + z * lxz + lxo);
  225.         tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo);
  226.         tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo);
  227.     }
  228.     }
  229.     public String toString() {
  230.     return ("[" + xo + "," + xx + "," + xy + "," + xz + ";"
  231.         + yo + "," + yx + "," + yy + "," + yz + ";"
  232.         + zo + "," + zx + "," + zy + "," + zz + "]");
  233.     }
  234. }
  235.