KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Matrix3D


1 /*
2  * @(#)Matrix3D.java 1.12 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Matrix3D.java 1.12 06/02/22
39  */

40
41 /** A fairly conventional 3D matrix object that can transform sets of
42     3D points and perform a variety of manipulations on the transform */

43 class Matrix3D {
44     float xx, xy, xz, xo;
45     float yx, yy, yz, yo;
46     float zx, zy, zz, zo;
47     static final double pi = 3.14159265;
48     /** Create a new unit matrix */
49     Matrix3D () {
50     xx = 1.0f;
51     yy = 1.0f;
52     zz = 1.0f;
53     }
54     /** Scale by f in all dimensions */
55     void scale(float f) {
56     xx *= f;
57     xy *= f;
58     xz *= f;
59     xo *= f;
60     yx *= f;
61     yy *= f;
62     yz *= f;
63     yo *= f;
64     zx *= f;
65     zy *= f;
66     zz *= f;
67     zo *= f;
68     }
69     /** Scale along each axis independently */
70     void scale(float xf, float yf, float zf) {
71     xx *= xf;
72     xy *= xf;
73     xz *= xf;
74     xo *= xf;
75     yx *= yf;
76     yy *= yf;
77     yz *= yf;
78     yo *= yf;
79     zx *= zf;
80     zy *= zf;
81     zz *= zf;
82     zo *= zf;
83     }
84     /** Translate the origin */
85     void translate(float x, float y, float z) {
86     xo += x;
87     yo += y;
88     zo += z;
89     }
90     /** rotate theta degrees about the y axis */
91     void yrot(double theta) {
92     theta *= (pi / 180);
93     double ct = Math.cos(theta);
94     double st = Math.sin(theta);
95
96     float Nxx = (float) (xx * ct + zx * st);
97     float Nxy = (float) (xy * ct + zy * st);
98     float Nxz = (float) (xz * ct + zz * st);
99     float Nxo = (float) (xo * ct + zo * st);
100
101     float Nzx = (float) (zx * ct - xx * st);
102     float Nzy = (float) (zy * ct - xy * st);
103     float Nzz = (float) (zz * ct - xz * st);
104     float Nzo = (float) (zo * ct - xo * st);
105
106     xo = Nxo;
107     xx = Nxx;
108     xy = Nxy;
109     xz = Nxz;
110     zo = Nzo;
111     zx = Nzx;
112     zy = Nzy;
113     zz = Nzz;
114     }
115     /** rotate theta degrees about the x axis */
116     void xrot(double theta) {
117     theta *= (pi / 180);
118     double ct = Math.cos(theta);
119     double st = Math.sin(theta);
120
121     float Nyx = (float) (yx * ct + zx * st);
122     float Nyy = (float) (yy * ct + zy * st);
123     float Nyz = (float) (yz * ct + zz * st);
124     float Nyo = (float) (yo * ct + zo * st);
125
126     float Nzx = (float) (zx * ct - yx * st);
127     float Nzy = (float) (zy * ct - yy * st);
128     float Nzz = (float) (zz * ct - yz * st);
129     float Nzo = (float) (zo * ct - yo * st);
130
131     yo = Nyo;
132     yx = Nyx;
133     yy = Nyy;
134     yz = Nyz;
135     zo = Nzo;
136     zx = Nzx;
137     zy = Nzy;
138     zz = Nzz;
139     }
140     /** rotate theta degrees about the z axis */
141     void zrot(double theta) {
142     theta *= (pi / 180);
143     double ct = Math.cos(theta);
144     double st = Math.sin(theta);
145
146     float Nyx = (float) (yx * ct + xx * st);
147     float Nyy = (float) (yy * ct + xy * st);
148     float Nyz = (float) (yz * ct + xz * st);
149     float Nyo = (float) (yo * ct + xo * st);
150
151     float Nxx = (float) (xx * ct - yx * st);
152     float Nxy = (float) (xy * ct - yy * st);
153     float Nxz = (float) (xz * ct - yz * st);
154     float Nxo = (float) (xo * ct - yo * st);
155
156     yo = Nyo;
157     yx = Nyx;
158     yy = Nyy;
159     yz = Nyz;
160     xo = Nxo;
161     xx = Nxx;
162     xy = Nxy;
163     xz = Nxz;
164     }
165     /** Multiply this matrix by a second: M = M*R */
166     void mult(Matrix3D rhs) {
167     float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz;
168     float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz;
169     float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz;
170     float lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + rhs.xo;
171
172     float lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz;
173     float lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz;
174     float lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz;
175     float lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + rhs.yo;
176
177     float lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz;
178     float lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz;
179     float lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz;
180     float lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + rhs.zo;
181
182     xx = lxx;
183     xy = lxy;
184     xz = lxz;
185     xo = lxo;
186
187     yx = lyx;
188     yy = lyy;
189     yz = lyz;
190     yo = lyo;
191
192     zx = lzx;
193     zy = lzy;
194     zz = lzz;
195     zo = lzo;
196     }
197
198     /** Reinitialize to the unit matrix */
199     void unit() {
200     xo = 0;
201     xx = 1;
202     xy = 0;
203     xz = 0;
204     yo = 0;
205     yx = 0;
206     yy = 1;
207     yz = 0;
208     zo = 0;
209     zx = 0;
210     zy = 0;
211     zz = 1;
212     }
213     /** Transform nvert points from v into tv. v contains the input
214         coordinates in floating point. Three successive entries in
215     the array constitute a point. tv ends up holding the transformed
216     points as integers; three successive entries per point */

217     void transform(float v[], int tv[], int nvert) {
218     float lxx = xx, lxy = xy, lxz = xz, lxo = xo;
219     float lyx = yx, lyy = yy, lyz = yz, lyo = yo;
220     float lzx = zx, lzy = zy, lzz = zz, lzo = zo;
221     for (int i = nvert * 3; (i -= 3) >= 0;) {
222         float x = v[i];
223         float y = v[i + 1];
224         float z = v[i + 2];
225         tv[i ] = (int) (x * lxx + y * lxy + z * lxz + lxo);
226         tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo);
227         tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo);
228     }
229     }
230     public String JavaDoc toString() {
231     return ("[" + xo + "," + xx + "," + xy + "," + xz + ";"
232         + yo + "," + yx + "," + yy + "," + yz + ";"
233         + zo + "," + zx + "," + zy + "," + zz + "]");
234     }
235 }
236
Popular Tags