Skip to content

Commit

Permalink
editorial: Move the rotation matrix creation algorithm to a separate …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
rakuco committed Dec 5, 2023
1 parent 1324459 commit 3c8cd70
Showing 1 changed file with 71 additions and 54 deletions.
125 changes: 71 additions & 54 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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:

<img src="images/quaternion_to_rotation_matrix.png" style="display: block;margin: auto; width: 50%; height: 50%;" alt="Converting quaternion to rotation matrix.">

where:

- W = cos(θ/2)
- X = V<sub>x</sub> * sin(θ/2)
- Y = V<sub>y</sub> * sin(θ/2)
- Z = V<sub>z</sub> * sin(θ/2)

The rotation matrix is flattened in |targetMatrix| object according to the column-major order.

<div algorithm="populate rotation matrix">
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 <emu-val>this</emu-val>
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]
</div>


Expand Down Expand Up @@ -489,6 +470,42 @@ Abstract Operations {#abstract-operations}
1. Return |orientation|.
</div>

<h3 id="convert-quaternion-to-rotation-matrix">Convert quaternion to rotation matrix</h3>

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:

<img src="images/quaternion_to_rotation_matrix.png" style="display: block;margin: auto; width: 50%; height: 50%;" alt="Converting quaternion to rotation matrix.">

where:

- W = cos(θ/2)
- X = V<sub>x</sub> * sin(θ/2)
- Y = V<sub>y</sub> * sin(θ/2)
- Z = V<sub>z</sub> * sin(θ/2)

<div algorithm>
To <dfn lt="converting a quaternion to rotation matrix">convert a quaternion to rotation matrix</dfn> 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| ».

</div>

Automation {#automation}
==========

Expand Down

0 comments on commit 3c8cd70

Please sign in to comment.