From 3c8cd7031736494da054cf9c4d1cf488f85ae682 Mon Sep 17 00:00:00 2001 From: Raphael Kubo da Costa Date: Tue, 5 Dec 2023 18:21:52 +0100 Subject: [PATCH] editorial: Move the rotation matrix creation algorithm to a separate operation This will make it easier to invoke from the Automation section in an upcoming commit. The "convert a quaternion to rotation matrix" algorithm performs the same steps that were part of `OrientationSensor.populateMatrix()` and returns the matrix as a 9-element list that is used by the latter. There are no changes to the algorithm itself. Drive-by improvements to populateMatrix(): - Stop checking `targetMatrix`'s type, this is already done by Web IDL. - Refer to `this` with the right notation. --- index.bs | 125 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 54 deletions(-) diff --git a/index.bs b/index.bs index fd12772..b17ac3e 100644 --- a/index.bs +++ b/index.bs @@ -364,67 +364,48 @@ and "quaternion" as arguments. ### OrientationSensor.populateMatrix() ### {#orientationsensor-populatematrix} -The {{OrientationSensor/populateMatrix()}} method populates the given object with rotation matrix -which is converted from the value of [=latest reading=]["quaternion"] [[QUATCONV]], as shown below: - -Converting quaternion to rotation matrix. - -where: - - - W = cos(θ/2) - - X = Vx * sin(θ/2) - - Y = Vy * sin(θ/2) - - Z = Vz * sin(θ/2) - -The rotation matrix is flattened in |targetMatrix| object according to the column-major order. -
The {{OrientationSensor/populateMatrix(targetMatrix)}} method steps are: - 1. If |targetMatrix| is not of type defined by {{RotationMatrixType}} union, [=throw=] a - "{{TypeError!!exception}}" exception and abort these steps. 1. If |targetMatrix| is of type {{Float32Array}} or {{Float64Array}} with a size less than sixteen, [=throw=] a "{{TypeError!!exception}}" exception and abort these steps. - 1. Let |quaternion| be the result of invoking [=get value from latest reading=] with this + 1. Let |quaternion| be the result of invoking [=get value from latest reading=] with [=this=] and "quaternion" as arguments. 1. If |quaternion| is `null`, [=throw=] a "{{NotReadableError!!exception}}" {{DOMException}} and abort these steps. - 1. Let |x| be the value of |quaternion|[0] - 1. Let |y| be the value of |quaternion|[1] - 1. Let |z| be the value of |quaternion|[2] - 1. Let |w| be the value of |quaternion|[3] - 1. If |targetMatrix| is of {{Float32Array}} or {{Float64Array}} type, run these sub-steps: - 1. Set |targetMatrix|[0] = 1 - 2 * y * y - 2 * z * z - 1. Set |targetMatrix|[1] = 2 * x * y - 2 * z * w - 1. Set |targetMatrix|[2] = 2 * x * z + 2 * y * w - 1. Set |targetMatrix|[3] = 0 - 1. Set |targetMatrix|[4] = 2 * x * y + 2 * z * w - 1. Set |targetMatrix|[5] = 1 - 2 * x * x - 2 * z * z - 1. Set |targetMatrix|[6] = 2 * y * z - 2 * x * w - 1. Set |targetMatrix|[7] = 0 - 1. Set |targetMatrix|[8] = 2 * x * z - 2 * y * w - 1. Set |targetMatrix|[9] = 2 * y * z + 2 * x * w - 1. Set |targetMatrix|[10] = 1 - 2 * x * x - 2 * y * y - 1. Set |targetMatrix|[11] = 0 - 1. Set |targetMatrix|[12] = 0 - 1. Set |targetMatrix|[13] = 0 - 1. Set |targetMatrix|[14] = 0 - 1. Set |targetMatrix|[15] = 1 + 1. Let |rotationMatrix| be the result of [=converting a quaternion to rotation matrix=] with |quaternion|[0], |quaternion|[1], |quaternion|[2], and |quaternion|[3]. + 1. If |targetMatrix| is of {{Float32Array}} or {{Float64Array}} type, run these sub-steps: + 1. Set |targetMatrix|[0] = |rotationMatrix|[0] + 1. Set |targetMatrix|[1] = |rotationMatrix|[1] + 1. Set |targetMatrix|[2] = |rotationMatrix|[2] + 1. Set |targetMatrix|[3] = |rotationMatrix|[3] + 1. Set |targetMatrix|[4] = |rotationMatrix|[4] + 1. Set |targetMatrix|[5] = |rotationMatrix|[5] + 1. Set |targetMatrix|[6] = |rotationMatrix|[6] + 1. Set |targetMatrix|[7] = |rotationMatrix|[7] + 1. Set |targetMatrix|[8] = |rotationMatrix|[8] + 1. Set |targetMatrix|[9] = |rotationMatrix|[9] + 1. Set |targetMatrix|[10] = |rotationMatrix|[10] + 1. Set |targetMatrix|[11] = |rotationMatrix|[11] + 1. Set |targetMatrix|[12] = |rotationMatrix|[12] + 1. Set |targetMatrix|[13] = |rotationMatrix|[13] + 1. Set |targetMatrix|[14] = |rotationMatrix|[14] + 1. Set |targetMatrix|[15] = |rotationMatrix|[15] 1. If |targetMatrix| is of {{DOMMatrix}} type, run these sub-steps: - 1. Set |targetMatrix|.m11 = 1 - 2 * y * y - 2 * z * z - 1. Set |targetMatrix|.m12 = 2 * x * y - 2 * z * w - 1. Set |targetMatrix|.m13 = 2 * x * z + 2 * y * w - 1. Set |targetMatrix|.m14 = 0 - 1. Set |targetMatrix|.m21 = 2 * x * y + 2 * z * w - 1. Set |targetMatrix|.m22 = 1 - 2 * x * x - 2 * z * z - 1. Set |targetMatrix|.m23 = 2 * y * z - 2 * x * w - 1. Set |targetMatrix|.m24 = 0 - 1. Set |targetMatrix|.m31 = 2 * x * z - 2 * y * w - 1. Set |targetMatrix|.m32 = 2 * y * z + 2 * x * w - 1. Set |targetMatrix|.m33 = 1 - 2 * x * x - 2 * y * y - 1. Set |targetMatrix|.m34 = 0 - 1. Set |targetMatrix|.m41 = 0 - 1. Set |targetMatrix|.m42 = 0 - 1. Set |targetMatrix|.m43 = 0 - 1. Set |targetMatrix|.m44 = 1 + 1. Set |targetMatrix|.m11 = |rotationMatrix|[0] + 1. Set |targetMatrix|.m12 = |rotationMatrix|[1] + 1. Set |targetMatrix|.m13 = |rotationMatrix|[2] + 1. Set |targetMatrix|.m14 = |rotationMatrix|[3] + 1. Set |targetMatrix|.m21 = |rotationMatrix|[4] + 1. Set |targetMatrix|.m22 = |rotationMatrix|[5] + 1. Set |targetMatrix|.m23 = |rotationMatrix|[6] + 1. Set |targetMatrix|.m24 = |rotationMatrix|[7] + 1. Set |targetMatrix|.m31 = |rotationMatrix|[8] + 1. Set |targetMatrix|.m32 = |rotationMatrix|[9] + 1. Set |targetMatrix|.m33 = |rotationMatrix|[10] + 1. Set |targetMatrix|.m34 = |rotationMatrix|[11] + 1. Set |targetMatrix|.m41 = |rotationMatrix|[12] + 1. Set |targetMatrix|.m42 = |rotationMatrix|[13] + 1. Set |targetMatrix|.m43 = |rotationMatrix|[14] + 1. Set |targetMatrix|.m44 = |rotationMatrix|[15]
@@ -489,6 +470,42 @@ Abstract Operations {#abstract-operations} 1. Return |orientation|. +

Convert quaternion to rotation matrix

+ +The [=convert a quaternion to rotation matrix=] algorithm creates a [=list=] representation of a rotation matrix in column-major order converted from a quaternion [[QUATCONV]], as shown below: + +Converting quaternion to rotation matrix. + +where: + + - W = cos(θ/2) + - X = Vx * sin(θ/2) + - Y = Vy * sin(θ/2) + - Z = Vz * sin(θ/2) + +
+To convert a quaternion to rotation matrix given a number |x|, a number |y|, a number |z|, and a number |w|: + + 1. Let |m11| be 1 - 2 * y * y - 2 * z * z + 1. Let |m12| be 2 * x * y - 2 * z * w + 1. Let |m13| be 2 * x * z + 2 * y * w + 1. Let |m14| be 0 + 1. Let |m21| be 2 * x * y + 2 * z * w + 1. Let |m22| be 1 - 2 * x * x - 2 * z * z + 1. Let |m23| be 2 * y * z - 2 * x * w + 1. Let |m24| be 0 + 1. Let |m31| be 2 * x * z - 2 * y * w + 1. Let |m32| be 2 * y * z + 2 * x * w + 1. Let |m33| be 1 - 2 * x * x - 2 * y * y + 1. Let |m34| be 0 + 1. Let |m41| be 0 + 1. Let |m42| be 0 + 1. Let |m43| be 0 + 1. Let |m44| be 1 + 1. Return « |m11|, |m12|, |m13|, |m14|, |m21|, |m22|, |m23|, |m24|, |m31|, |m32|, |m33|, |m34|, |m41|, |m42|, |m43|, |m44| ». + +
+ Automation {#automation} ==========