MCPcopy
hub / github.com/spritejs/spritejs / fromMat3

Function fromMat3

docs/demo/spritejs.worker.esm.js:4310–4342  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

4308 */
4309
4310function fromMat3(out, m) {
4311 // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
4312 // article "Quaternion Calculus and Fast Animation".
4313 var fTrace = m[0] + m[4] + m[8];
4314 var fRoot;
4315
4316 if (fTrace > 0.0) {
4317 // |w| > 1/2, may as well choose w > 1/2
4318 fRoot = Math.sqrt(fTrace + 1.0); // 2w
4319
4320 out[3] = 0.5 * fRoot;
4321 fRoot = 0.5 / fRoot; // 1/(4w)
4322
4323 out[0] = (m[5] - m[7]) * fRoot;
4324 out[1] = (m[6] - m[2]) * fRoot;
4325 out[2] = (m[1] - m[3]) * fRoot;
4326 } else {
4327 // |w| <= 1/2
4328 var i = 0;
4329 if (m[4] > m[0]) i = 1;
4330 if (m[8] > m[i * 3 + i]) i = 2;
4331 var j = (i + 1) % 3;
4332 var k = (i + 2) % 3;
4333 fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
4334 out[i] = 0.5 * fRoot;
4335 fRoot = 0.5 / fRoot;
4336 out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
4337 out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
4338 out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
4339 }
4340
4341 return out;
4342}
4343/**
4344 * Creates a quaternion from the given euler angle x, y, z.
4345 *

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected