1 /* |
1 /* |
2 * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. |
3 * |
3 * |
4 * Redistribution and use in source and binary forms, with or without |
4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions |
5 * modification, are permitted provided that the following conditions |
6 * are met: |
6 * are met: |
7 * |
7 * |
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ |
30 */ |
31 |
31 |
32 /* |
|
33 */ |
|
34 |
32 |
35 /** A fairly conventional 3D matrix object that can transform sets of |
33 /** A fairly conventional 3D matrix object that can transform sets of |
36 3D points and perform a variety of manipulations on the transform */ |
34 3D points and perform a variety of manipulations on the transform */ |
37 class Matrix3D { |
35 class Matrix3D { |
|
36 |
38 float xx, xy, xz, xo; |
37 float xx, xy, xz, xo; |
39 float yx, yy, yz, yo; |
38 float yx, yy, yz, yo; |
40 float zx, zy, zz, zo; |
39 float zx, zy, zz, zo; |
41 static final double pi = 3.14159265; |
40 static final double pi = 3.14159265; |
|
41 |
42 /** Create a new unit matrix */ |
42 /** Create a new unit matrix */ |
43 Matrix3D () { |
43 Matrix3D() { |
44 xx = 1.0f; |
44 xx = 1.0f; |
45 yy = 1.0f; |
45 yy = 1.0f; |
46 zz = 1.0f; |
46 zz = 1.0f; |
47 } |
47 } |
|
48 |
48 /** Scale by f in all dimensions */ |
49 /** Scale by f in all dimensions */ |
49 void scale(float f) { |
50 void scale(float f) { |
50 xx *= f; |
51 xx *= f; |
51 xy *= f; |
52 xy *= f; |
52 xz *= f; |
53 xz *= f; |
73 zx *= zf; |
75 zx *= zf; |
74 zy *= zf; |
76 zy *= zf; |
75 zz *= zf; |
77 zz *= zf; |
76 zo *= zf; |
78 zo *= zf; |
77 } |
79 } |
|
80 |
78 /** Translate the origin */ |
81 /** Translate the origin */ |
79 void translate(float x, float y, float z) { |
82 void translate(float x, float y, float z) { |
80 xo += x; |
83 xo += x; |
81 yo += y; |
84 yo += y; |
82 zo += z; |
85 zo += z; |
83 } |
86 } |
|
87 |
84 /** rotate theta degrees about the y axis */ |
88 /** rotate theta degrees about the y axis */ |
85 void yrot(double theta) { |
89 void yrot(double theta) { |
86 theta *= (pi / 180); |
90 theta *= (pi / 180); |
87 double ct = Math.cos(theta); |
91 double ct = Math.cos(theta); |
88 double st = Math.sin(theta); |
92 double st = Math.sin(theta); |
154 xo = Nxo; |
160 xo = Nxo; |
155 xx = Nxx; |
161 xx = Nxx; |
156 xy = Nxy; |
162 xy = Nxy; |
157 xz = Nxz; |
163 xz = Nxz; |
158 } |
164 } |
|
165 |
159 /** Multiply this matrix by a second: M = M*R */ |
166 /** Multiply this matrix by a second: M = M*R */ |
160 void mult(Matrix3D rhs) { |
167 void mult(Matrix3D rhs) { |
161 float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz; |
168 float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz; |
162 float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz; |
169 float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz; |
163 float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz; |
170 float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz; |
202 zo = 0; |
209 zo = 0; |
203 zx = 0; |
210 zx = 0; |
204 zy = 0; |
211 zy = 0; |
205 zz = 1; |
212 zz = 1; |
206 } |
213 } |
|
214 |
207 /** Transform nvert points from v into tv. v contains the input |
215 /** Transform nvert points from v into tv. v contains the input |
208 coordinates in floating point. Three successive entries in |
216 coordinates in floating point. Three successive entries in |
209 the array constitute a point. tv ends up holding the transformed |
217 the array constitute a point. tv ends up holding the transformed |
210 points as integers; three successive entries per point */ |
218 points as integers; three successive entries per point */ |
211 void transform(float v[], int tv[], int nvert) { |
219 void transform(float v[], int tv[], int nvert) { |
212 float lxx = xx, lxy = xy, lxz = xz, lxo = xo; |
220 float lxx = xx, lxy = xy, lxz = xz, lxo = xo; |
213 float lyx = yx, lyy = yy, lyz = yz, lyo = yo; |
221 float lyx = yx, lyy = yy, lyz = yz, lyo = yo; |
214 float lzx = zx, lzy = zy, lzz = zz, lzo = zo; |
222 float lzx = zx, lzy = zy, lzz = zz, lzo = zo; |
215 for (int i = nvert * 3; (i -= 3) >= 0;) { |
223 for (int i = nvert * 3; (i -= 3) >= 0;) { |
216 float x = v[i]; |
224 float x = v[i]; |
217 float y = v[i + 1]; |
225 float y = v[i + 1]; |
218 float z = v[i + 2]; |
226 float z = v[i + 2]; |
219 tv[i ] = (int) (x * lxx + y * lxy + z * lxz + lxo); |
227 tv[i] = (int) (x * lxx + y * lxy + z * lxz + lxo); |
220 tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo); |
228 tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo); |
221 tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo); |
229 tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo); |
222 } |
230 } |
223 } |
231 } |
|
232 |
|
233 @Override |
224 public String toString() { |
234 public String toString() { |
225 return ("[" + xo + "," + xx + "," + xy + "," + xz + ";" |
235 return ("[" + xo + "," + xx + "," + xy + "," + xz + ";" |
226 + yo + "," + yx + "," + yy + "," + yz + ";" |
236 + yo + "," + yx + "," + yy + "," + yz + ";" |
227 + zo + "," + zx + "," + zy + "," + zz + "]"); |
237 + zo + "," + zx + "," + zy + "," + zz + "]"); |
228 } |
238 } |