Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/applets/MoleculeViewer/Matrix3D.java
38827 views
/*1* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041/** A fairly conventional 3D matrix object that can transform sets of423D points and perform a variety of manipulations on the transform */43class Matrix3D {4445float xx, xy, xz, xo;46float yx, yy, yz, yo;47float zx, zy, zz, zo;48static final double pi = 3.14159265;4950/** Create a new unit matrix */51Matrix3D() {52xx = 1.0f;53yy = 1.0f;54zz = 1.0f;55}5657/** Scale by f in all dimensions */58void scale(float f) {59xx *= f;60xy *= f;61xz *= f;62xo *= f;63yx *= f;64yy *= f;65yz *= f;66yo *= f;67zx *= f;68zy *= f;69zz *= f;70zo *= f;71}7273/** Scale along each axis independently */74void scale(float xf, float yf, float zf) {75xx *= xf;76xy *= xf;77xz *= xf;78xo *= xf;79yx *= yf;80yy *= yf;81yz *= yf;82yo *= yf;83zx *= zf;84zy *= zf;85zz *= zf;86zo *= zf;87}8889/** Translate the origin */90void translate(float x, float y, float z) {91xo += x;92yo += y;93zo += z;94}9596/** rotate theta degrees about the y axis */97void yrot(double theta) {98theta *= (pi / 180);99double ct = Math.cos(theta);100double st = Math.sin(theta);101102float Nxx = (float) (xx * ct + zx * st);103float Nxy = (float) (xy * ct + zy * st);104float Nxz = (float) (xz * ct + zz * st);105float Nxo = (float) (xo * ct + zo * st);106107float Nzx = (float) (zx * ct - xx * st);108float Nzy = (float) (zy * ct - xy * st);109float Nzz = (float) (zz * ct - xz * st);110float Nzo = (float) (zo * ct - xo * st);111112xo = Nxo;113xx = Nxx;114xy = Nxy;115xz = Nxz;116zo = Nzo;117zx = Nzx;118zy = Nzy;119zz = Nzz;120}121122/** rotate theta degrees about the x axis */123void xrot(double theta) {124theta *= (pi / 180);125double ct = Math.cos(theta);126double st = Math.sin(theta);127128float Nyx = (float) (yx * ct + zx * st);129float Nyy = (float) (yy * ct + zy * st);130float Nyz = (float) (yz * ct + zz * st);131float Nyo = (float) (yo * ct + zo * st);132133float Nzx = (float) (zx * ct - yx * st);134float Nzy = (float) (zy * ct - yy * st);135float Nzz = (float) (zz * ct - yz * st);136float Nzo = (float) (zo * ct - yo * st);137138yo = Nyo;139yx = Nyx;140yy = Nyy;141yz = Nyz;142zo = Nzo;143zx = Nzx;144zy = Nzy;145zz = Nzz;146}147148/** rotate theta degrees about the z axis */149void zrot(double theta) {150theta *= (pi / 180);151double ct = Math.cos(theta);152double st = Math.sin(theta);153154float Nyx = (float) (yx * ct + xx * st);155float Nyy = (float) (yy * ct + xy * st);156float Nyz = (float) (yz * ct + xz * st);157float Nyo = (float) (yo * ct + xo * st);158159float Nxx = (float) (xx * ct - yx * st);160float Nxy = (float) (xy * ct - yy * st);161float Nxz = (float) (xz * ct - yz * st);162float Nxo = (float) (xo * ct - yo * st);163164yo = Nyo;165yx = Nyx;166yy = Nyy;167yz = Nyz;168xo = Nxo;169xx = Nxx;170xy = Nxy;171xz = Nxz;172}173174/** Multiply this matrix by a second: M = M*R */175void mult(Matrix3D rhs) {176float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz;177float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz;178float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz;179float lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + rhs.xo;180181float lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz;182float lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz;183float lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz;184float lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + rhs.yo;185186float lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz;187float lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz;188float lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz;189float lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + rhs.zo;190191xx = lxx;192xy = lxy;193xz = lxz;194xo = lxo;195196yx = lyx;197yy = lyy;198yz = lyz;199yo = lyo;200201zx = lzx;202zy = lzy;203zz = lzz;204zo = lzo;205}206207/** Reinitialize to the unit matrix */208void unit() {209xo = 0;210xx = 1;211xy = 0;212xz = 0;213yo = 0;214yx = 0;215yy = 1;216yz = 0;217zo = 0;218zx = 0;219zy = 0;220zz = 1;221}222223/** Transform nvert points from v into tv. v contains the input224coordinates in floating point. Three successive entries in225the array constitute a point. tv ends up holding the transformed226points as integers; three successive entries per point */227void transform(float v[], int tv[], int nvert) {228float lxx = xx, lxy = xy, lxz = xz, lxo = xo;229float lyx = yx, lyy = yy, lyz = yz, lyo = yo;230float lzx = zx, lzy = zy, lzz = zz, lzo = zo;231for (int i = nvert * 3; (i -= 3) >= 0;) {232float x = v[i];233float y = v[i + 1];234float z = v[i + 2];235tv[i] = (int) (x * lxx + y * lxy + z * lxz + lxo);236tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo);237tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo);238}239}240241@Override242public String toString() {243return ("[" + xo + "," + xx + "," + xy + "," + xz + ";"244+ yo + "," + yx + "," + yy + "," + yz + ";"245+ zo + "," + zx + "," + zy + "," + zz + "]");246}247}248249250