* Creates a quaternion from the given 3x3 rotation matrix. * * NOTE: The resultant quaternion is not normalized, so you should be sure * to renormalize the quaternion yourself where necessary. * * @param {quat} out the receiving quaternion * @param {ReadonlyMat3} m rotation matrix * @r
(out, m)
| 4456 | */ |
| 4457 | |
| 4458 | function fromMat3(out, m) { |
| 4459 | // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes |
| 4460 | // article "Quaternion Calculus and Fast Animation". |
| 4461 | var fTrace = m[0] + m[4] + m[8]; |
| 4462 | var fRoot; |
| 4463 | |
| 4464 | if (fTrace > 0.0) { |
| 4465 | // |w| > 1/2, may as well choose w > 1/2 |
| 4466 | fRoot = Math.sqrt(fTrace + 1.0); // 2w |
| 4467 | |
| 4468 | out[3] = 0.5 * fRoot; |
| 4469 | fRoot = 0.5 / fRoot; // 1/(4w) |
| 4470 | |
| 4471 | out[0] = (m[5] - m[7]) * fRoot; |
| 4472 | out[1] = (m[6] - m[2]) * fRoot; |
| 4473 | out[2] = (m[1] - m[3]) * fRoot; |
| 4474 | } else { |
| 4475 | // |w| <= 1/2 |
| 4476 | var i = 0; |
| 4477 | if (m[4] > m[0]) i = 1; |
| 4478 | if (m[8] > m[i * 3 + i]) i = 2; |
| 4479 | var j = (i + 1) % 3; |
| 4480 | var k = (i + 2) % 3; |
| 4481 | fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0); |
| 4482 | out[i] = 0.5 * fRoot; |
| 4483 | fRoot = 0.5 / fRoot; |
| 4484 | out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot; |
| 4485 | out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot; |
| 4486 | out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot; |
| 4487 | } |
| 4488 | |
| 4489 | return out; |
| 4490 | } |
| 4491 | /** |
| 4492 | * Creates a quaternion from the given euler angle x, y, z. |
| 4493 | * |