home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / Matrix3D.java < prev    next >
Encoding:
Text File  |  1997-07-30  |  6.5 KB  |  227 lines

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