BB.Message.Quaternion (bb v0.4.0)

View Source

Helper functions for quaternion tagged tuples.

Quaternions are represented as {:quaternion, x, y, z, w} where components are floats in XYZW order. Used for representing 3D orientations.

The quaternion should be normalised (magnitude = 1) for rotation purposes, but this module does not enforce normalisation.

Examples

iex> alias BB.Message.Quaternion
iex> q = Quaternion.identity()
{:quaternion, 0.0, 0.0, 0.0, 1.0}
iex> Quaternion.w(q)
1.0

Summary

Functions

Create from a list [x, y, z, w].

Returns the identity quaternion (no rotation).

Create a new quaternion from x, y, z, w components.

Convert to a list [x, y, z, w].

Get the W (scalar) component.

Get the X component.

Get the Y component.

Get the Z component.

Types

t()

@type t() :: {:quaternion, float(), float(), float(), float()}

Functions

from_list(list)

@spec from_list([number()]) :: t()

Create from a list [x, y, z, w].

Examples

iex> BB.Message.Quaternion.from_list([0.0, 0.0, 0.0, 1.0])
{:quaternion, 0.0, 0.0, 0.0, 1.0}

identity()

@spec identity() :: t()

Returns the identity quaternion (no rotation).

Examples

iex> BB.Message.Quaternion.identity()
{:quaternion, 0.0, 0.0, 0.0, 1.0}

new(x, y, z, w)

@spec new(number(), number(), number(), number()) :: t()

Create a new quaternion from x, y, z, w components.

Accepts any numeric type and converts to float.

Examples

iex> BB.Message.Quaternion.new(0, 0, 0, 1)
{:quaternion, 0.0, 0.0, 0.0, 1.0}

iex> BB.Message.Quaternion.new(0.0, 0.707, 0.0, 0.707)
{:quaternion, 0.0, 0.707, 0.0, 0.707}

to_list(arg)

@spec to_list(t()) :: [float()]

Convert to a list [x, y, z, w].

Examples

iex> BB.Message.Quaternion.to_list({:quaternion, 0.0, 0.0, 0.0, 1.0})
[0.0, 0.0, 0.0, 1.0]

w(arg)

@spec w(t()) :: float()

Get the W (scalar) component.

Examples

iex> BB.Message.Quaternion.w({:quaternion, 0.1, 0.2, 0.3, 0.9})
0.9

x(arg)

@spec x(t()) :: float()

Get the X component.

Examples

iex> BB.Message.Quaternion.x({:quaternion, 0.1, 0.2, 0.3, 0.9})
0.1

y(arg)

@spec y(t()) :: float()

Get the Y component.

Examples

iex> BB.Message.Quaternion.y({:quaternion, 0.1, 0.2, 0.3, 0.9})
0.2

z(arg)

@spec z(t()) :: float()

Get the Z component.

Examples

iex> BB.Message.Quaternion.z({:quaternion, 0.1, 0.2, 0.3, 0.9})
0.3