1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
package javajs.util;
/**
* A base class for both M3 and M4 to conserve code size.
*
* @author Kenji hiranabe
*
* additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique
* constructor and method names for the optimization of compiled
* JavaScript using Java2Script and for subclassing to M3 and M4
*
*/
public abstract class M34 {
/**
* The first element of the first row
*/
public float m00;
/**
* The second element of the first row.
*/
public float m01;
/**
* third element of the first row.
*/
public float m02;
/**
* The first element of the second row.
*/
public float m10;
/**
* The second element of the second row.
*/
public float m11;
/**
* The third element of the second row.
*/
public float m12;
/**
* The first element of the third row.
*/
public float m20;
/**
* The second element of the third row.
*/
public float m21;
/**
* The third element of the third row.
*/
public float m22;
protected void setAA33(A4 a) {
double x = a.x;
double y = a.y;
double z = a.z;
double angle = a.angle;
// Taken from Rick's which is taken from Wertz. pg. 412
// Bug Fixed and changed into right-handed by hiranabe
double n = Math.sqrt(x * x + y * y + z * z);
// zero-div may occur
n = 1 / n;
x *= n;
y *= n;
z *= n;
double c = Math.cos(angle);
double s = Math.sin(angle);
double omc = 1.0 - c;
m00 = (float) (c + x * x * omc);
m11 = (float) (c + y * y * omc);
m22 = (float) (c + z * z * omc);
double tmp1 = x * y * omc;
double tmp2 = z * s;
m01 = (float) (tmp1 - tmp2);
m10 = (float) (tmp1 + tmp2);
tmp1 = x * z * omc;
tmp2 = y * s;
m02 = (float) (tmp1 + tmp2);
m20 = (float) (tmp1 - tmp2);
tmp1 = y * z * omc;
tmp2 = x * s;
m12 = (float) (tmp1 - tmp2);
m21 = (float) (tmp1 + tmp2);
}
public void rotate(T3 t) {
// alias-safe
rotate2(t, t);
}
/**
* Transform the vector vec using this Matrix3f and place the result into
* vecOut.
*
* @param t
* the single precision vector to be transformed
* @param result
* the vector into which the transformed values are placed
*/
public void rotate2(T3 t, T3 result) {
// alias-safe
result.set(m00 * t.x + m01 * t.y + m02 * t.z, m10 * t.x + m11 * t.y + m12
* t.z, m20 * t.x + m21 * t.y + m22 * t.z);
}
/**
* Sets the value of this matrix to the double value of the Matrix3f argument.
*
* @param m1
* the matrix3f
*/
protected void setM33(M34 m1) {
m00 = m1.m00;
m01 = m1.m01;
m02 = m1.m02;
m10 = m1.m10;
m11 = m1.m11;
m12 = m1.m12;
m20 = m1.m20;
m21 = m1.m21;
m22 = m1.m22;
}
protected void clear33() {
m00 = m01 = m02 = m10 = m11 = m12 = m20 = m21 = m22 = 0.0f;
}
protected void set33(int row, int col, float v) {
switch (row) {
case 0:
switch (col) {
case 0:
m00 = v;
return;
case 1:
m01 = v;
return;
case 2:
m02 = v;
return;
}
break;
case 1:
switch (col) {
case 0:
m10 = v;
return;
case 1:
m11 = v;
return;
case 2:
m12 = v;
return;
}
break;
case 2:
switch (col) {
case 0:
m20 = v;
return;
case 1:
m21 = v;
return;
case 2:
m22 = v;
return;
}
break;
}
err();
}
protected float get33(int row, int col) {
switch (row) {
case 0:
switch (col) {
case 0:
return m00;
case 1:
return m01;
case 2:
return m02;
}
break;
case 1:
switch (col) {
case 0:
return m10;
case 1:
return m11;
case 2:
return m12;
}
break;
case 2:
switch (col) {
case 0:
return m20;
case 1:
return m21;
case 2:
return m22;
}
break;
}
err();
return 0;
}
protected void setRow33(int row, float v[]) {
switch (row) {
case 0:
m00 = v[0];
m01 = v[1];
m02 = v[2];
return;
case 1:
m10 = v[0];
m11 = v[1];
m12 = v[2];
return;
case 2:
m20 = v[0];
m21 = v[1];
m22 = v[2];
return;
default:
err();
}
}
public abstract void getRow(int row, float v[]);
protected void getRow33(int row, float v[]) {
switch (row) {
case 0:
v[0] = m00;
v[1] = m01;
v[2] = m02;
return;
case 1:
v[0] = m10;
v[1] = m11;
v[2] = m12;
return;
case 2:
v[0] = m20;
v[1] = m21;
v[2] = m22;
return;
}
err();
}
protected void setColumn33(int column, float v[]) {
switch(column) {
case 0:
m00 = v[0];
m10 = v[1];
m20 = v[2];
break;
case 1:
m01 = v[0];
m11 = v[1];
m21 = v[2];
break;
case 2:
m02 = v[0];
m12 = v[1];
m22 = v[2];
break;
default:
err();
}
}
protected void getColumn33(int column, float v[]) {
switch(column) {
case 0:
v[0] = m00;
v[1] = m10;
v[2] = m20;
break;
case 1:
v[0] = m01;
v[1] = m11;
v[2] = m21;
break;
case 2:
v[0] = m02;
v[1] = m12;
v[2] = m22;
break;
default:
err();
}
}
protected void add33(M34 m1) {
m00 += m1.m00;
m01 += m1.m01;
m02 += m1.m02;
m10 += m1.m10;
m11 += m1.m11;
m12 += m1.m12;
m20 += m1.m20;
m21 += m1.m21;
m22 += m1.m22;
}
protected void sub33(M34 m1) {
m00 -= m1.m00;
m01 -= m1.m01;
m02 -= m1.m02;
m10 -= m1.m10;
m11 -= m1.m11;
m12 -= m1.m12;
m20 -= m1.m20;
m21 -= m1.m21;
m22 -= m1.m22;
}
protected void mul33(float x) {
m00 *= x;
m01 *= x;
m02 *= x;
m10 *= x;
m11 *= x;
m12 *= x;
m20 *= x;
m21 *= x;
m22 *= x;
}
protected void transpose33() {
float tmp = m01;
m01 = m10;
m10 = tmp;
tmp = m02;
m02 = m20;
m20 = tmp;
tmp = m12;
m12 = m21;
m21 = tmp;
}
protected void setXRot(float angle) {
double c = Math.cos(angle);
double s = Math.sin(angle);
m00 = 1.0f;
m01 = 0.0f;
m02 = 0.0f;
m10 = 0.0f;
m11 = (float) c;
m12 = (float) -s;
m20 = 0.0f;
m21 = (float) s;
m22 = (float) c;
}
protected void setYRot(float angle) {
double c = Math.cos(angle);
double s = Math.sin(angle);
m00 = (float) c;
m01 = 0.0f;
m02 = (float) s;
m10 = 0.0f;
m11 = 1.0f;
m12 = 0.0f;
m20 = (float) -s;
m21 = 0.0f;
m22 = (float) c;
}
protected void setZRot(float angle) {
double c = Math.cos(angle);
double s = Math.sin(angle);
m00 = (float) c;
m01 = (float) -s;
m02 = 0.0f;
m10 = (float) s;
m11 = (float) c;
m12 = 0.0f;
m20 = 0.0f;
m21 = 0.0f;
m22 = 1.0f;
}
/**
* @return 3x3 determinant
*/
public float determinant3() {
return m00 * (m11 * m22 - m21 * m12) - m01 * (m10 * m22 - m20 * m12) + m02
* (m10 * m21 - m20 * m11);
}
protected void err() {
throw new ArrayIndexOutOfBoundsException(
"matrix column/row out of bounds");
}
}
|