Skip to content

Commit

Permalink
Automation: Integrate with Device Orientation API's Automation section
Browse files Browse the repository at this point in the history
Related to w3c/deviceorientation#124.

Both the Device Orientation API and the Orientation Sensor specifications
provide orientation data. Among other differences, the former provides the
data as a set of intrinsic Tait-Bryant Euler angles, whereas the latter uses
quaternions.

From an automation perspective, though, it makes sense to use the same
virtual sensor types for both APIs and take care of the conversions
internally.

The Device Orientation spec's Automation section defines steps for parsing a
user-provided reading as alpha, beta, gamma numbers that adhere to the
requirements laid out by the same spec. This commit augments the same "parse
orientation data reading" algorithm by also setting a "quaternion" entry in
the reading map that is derived from the user-provider Euler angles.

The Euler angles to quaternion algorithm has been copied from Chromium.
  • Loading branch information
rakuco committed Jan 9, 2024
1 parent c8614cf commit 59e1a7f
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -506,26 +506,62 @@ To <dfn lt="converting a quaternion to rotation matrix">convert a quaternion to

</div>

<h3 id="helper-create-quaternion-from-euler-angles">Create a quaternion from Euler angles</h3>

<div algorithm>
<!--
This algorithm was copied from
https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/sensor/sensor_inspector_agent.cc;l=27;drc=047c7dc4ee1ce908d7fea38ca063fa2f80f92c77
-->

To <dfn>create a quaternion from Euler angles</dfn> given a number |alpha|, a number |beta| and a number |gamma|:

1. Let |alphaInRadians| be |alpha| converted from degrees to radians.
1. Let |betaInRadians| be |beta| converted from degrees to radians.
1. Let |gammaInRadians| be |gamma| converted from degrees to radians.
1. Let |cosZ| be the cosine of (0.5 * |alphaInRadians|).
1. Let |sinZ| be the sine of (0.5 * |alphaInRadians|).
1. Let |cosX| be the cosine of (0.5 * |betaInRadians|).
1. Let |sinX| be the sine of (0.5 * |betaInRadians|).
1. Let |cosY| be the cosine of (0.5 * |gammaInRadians|).
1. Let |sinY| be the sine of (0.5 * |gammaInRadians|).
1. Let |quaternionX| be (|sinX| * |cosY| * |cosZ| - |cosX| * |sinY| * |sinZ|).
1. Let |quaternionY| be (|cosX| * |sinY| * |cosZ| + |sinX| * |cosY| * |sinZ|).
1. Let |quaternionZ| be (|cosX| * |cosY| * |sinZ| + |sinX| * |sinY| * |cosZ|).
1. Let |quaternionW| be (|cosX| * |cosY| * |cosZ| - |sinX| * |sinY| * |sinZ|).
1. Return « |quaternionX|, |quaternionY|, |quaternionZ|, |quaternionW| ».

</div>

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

This section extends [[GENERIC-SENSOR#automation]] by providing [=Orientation Sensor=]-specific virtual sensor metadata.

<h3 id="absolute-orientation-sensor-automation">Absolute Orientation Sensor automation</h3>
Modifications to other specifications {#modifications-to-other-specifications}
-------------------------------------

This specification integrates with [[DEVICE-ORIENTATION#automation]] as follows.

<div algorithm="parse-orientation-data-reading-modifications">
The [=parse orientation data reading=] algorithm is modified as follows:

* Add the following steps after setting |reading|'s "`alpha`", "`beta`", and "`gamma`" keys and before returning |reading|:
1. [=map/Set=] |reading|["`quaternion`"] to the result of invoking [=create a quaternion from Euler angles=] with |reading|["`alpha`"], |reading|["`beta`"], and |reading|["`gamma`"].

</div>

Note: This specification does not currently provide a way for specifying quaternions in WebDriver (and consequently deriving Euler angles from the quaternion) directly. This decision was made for simplicity and under the assumption that automation users are much more likely to work with Euler angles as inputs (or pick specific quaternion values and provide the corresponding Euler angle values on their own). Feedback from users with different use cases who are interested in being able to provide quaternion values directly is welcome via <a href="https://github.com/w3c/orientation-sensor/issues">this specification's issue tracker</a>.

Absolute Orientation Sensor automation {#absolute-orientation-sensor-automation}
--------------------------------------

The [=per-type virtual sensor metadata=] [=map=] must have the following [=map/entry=]:
: [=map/key=]
:: "<code>[=absolute-orientation virtual sensor type|absolute-orientation=]</code>"
: [=map/value=]
:: A [=virtual sensor metadata=] whose [=virtual sensor metadata/reading parsing algorithm=] is [=parse quaternion reading=].
The [=absolute-orientation virtual sensor type=] and its corresponding entry in the [=per-type virtual sensor metadata=] [=map=] are defined in [[DEVICE-ORIENTATION#automation]].

<h3 id="relative-orientation-sensor-automation">Relative Orientation Sensor automation</h3>
Relative Orientation Sensor automation {#relative-orientation-sensor-automation}
--------------------------------------

The [=per-type virtual sensor metadata=] [=map=] must have the following [=map/entry=]:
: [=map/key=]
:: "<code>[=relative-orientation virtual sensor type|relative-orientation=]</code>"
: [=map/value=]
:: A [=virtual sensor metadata=] whose [=virtual sensor metadata/reading parsing algorithm=] is [=parse quaternion reading=].
The [=relative-orientation virtual sensor type=] and its corresponding entry in the [=per-type virtual sensor metadata=] [=map=] are defined in [[DEVICE-ORIENTATION#automation]].

Acknowledgements {#acknowledgements}
================
Expand Down

0 comments on commit 59e1a7f

Please sign in to comment.