QuaternionSpace

Struct QuaternionSpace 

Source
pub struct QuaternionSpace { /* private fields */ }
Expand description

A spatial context representing 3D orientation using unit quaternions.

QuaternionSpace models the rotational state of an object in 3D space using a quaternion [w, x, y, z], which encodes a rotation around an axis without the risk of gimbal lock. This is especially useful in applications that require smooth, continuous rotation or orientation tracking.

The quaternion should be normalized (unit length) to represent a valid rotation.

§Fields

  • id: A unique identifier for the orientation context (e.g., sensor ID)
  • quat: A 4-element array representing the quaternion in [w, x, y, z] order:
    • w: scalar component (cos(θ/2))
    • x, y, z: vector part representing the axis of rotation (unit vector scaled by sin(θ/2))

§Coordinate Index Mapping

When used with the Coordinate trait, the following index mapping applies:

  • 0 => w
  • 1 => x
  • 2 => y
  • 3 => z

§Background

Quaternions are an extension of complex numbers used to represent rotations in three-dimensional space. Unlike Euler angles or axis-angle representations, quaternions avoid singularities (gimbal lock), allow smooth interpolation (slerp), and are computationally stable for tracking cumulative orientation over time.

Quaternions are commonly used in:

  • Aerospace and inertial navigation (IMUs, magnetometers, gyroscopes)
  • Robotics and drone control
  • Virtual and augmented reality (VR/AR head tracking)
  • 3D game engines and computer graphics

§Example

use deep_causality::*;

// Represents a 90-degree rotation around the Z-axis
let q = QuaternionSpace::new(1, std::f64::consts::FRAC_1_SQRT_2, 0.0, 0.0, std::f64::consts::FRAC_1_SQRT_2);

println!("{}", q);
assert_eq!(q.dimension(), 4);
assert!((q.distance(&q) - 0.0).abs() < 1e-9);

§Notes

  • All components are assumed to be in floating-point units (unitless)
  • Input quaternions should be normalized before use (||q|| = 1.0)
  • For multi-sensor fusion, make sure quaternions follow the same handedness convention (e.g., right-handed)

Implementations§

Source§

impl QuaternionSpace

Source

pub fn w(&self) -> f64

Source

pub fn x(&self) -> f64

Source

pub fn y(&self) -> f64

Source

pub fn z(&self) -> f64

Source§

impl QuaternionSpace

Source

pub fn new(id: u64, w: f64, x: f64, y: f64, z: f64) -> Self

Trait Implementations§

Source§

impl Adjustable<f64> for QuaternionSpace

Source§

fn update<const W: usize, const H: usize, const D: usize, const C: usize>( &mut self, array_grid: &ArrayGrid<f64, W, H, D, C>, ) -> Result<(), UpdateError>

The default implementation does nothing to keep update optional. Override this method to implement a node update when needed. For a sample implementation, see src/types/context_types/node_types_adjustable
Source§

fn adjust<const W: usize, const H: usize, const D: usize, const C: usize>( &mut self, array_grid: &ArrayGrid<f64, W, H, D, C>, ) -> Result<(), AdjustmentError>

The default implementation does nothing to keep adjustment optional. Override this method to implement a node adjustment when needed. Depending on the type of node adjustment, select a 1, 2,3, or 4 dimensional array grid that contains the transformation data to apply to the node. For a sample implementation, see src/types/context_types/node_types_adjustable
Source§

impl Clone for QuaternionSpace

Source§

fn clone(&self) -> QuaternionSpace

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Coordinate<f64> for QuaternionSpace

Source§

fn dimension(&self) -> usize

Returns the number of dimensions in the coordinate system (always 4).

Source§

fn coordinate(&self, index: usize) -> Result<&f64, IndexError>

Returns a reference to the coordinate value at the specified index.

§Index Mapping
  • 0 => w
  • 1 => x
  • 2 => y
  • 3 => z
§Errors

Returns IndexError if the index is out of bounds.

Source§

impl Debug for QuaternionSpace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for QuaternionSpace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Identifiable for QuaternionSpace

Source§

fn id(&self) -> u64

Source§

impl Metric<f64> for QuaternionSpace

Source§

fn distance(&self, other: &Self) -> f64

Computes the Euclidean distance between two quaternions in 4D space.

The quaternion is treated as a 4D vector in ℝ⁴ with components [w, x, y, z]. The standard Euclidean norm is applied:

d(q₁, q₂) = √[(w₁ - w₂)² + (x₁ - x₂)² + (y₁ - y₂)² + (z₁ - z₂)²]

This implementation is appropriate when:

  • Quaternions are treated as general 4D points (not necessarily normalized).
  • You need straight-line (chordal) distance in Euclidean space.
§Note

If the quaternions represent unit quaternions (i.e., orientations), and you wish to compute the angular distance between them, consider overriding this method with:

let dot = q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z;
let theta = 2.0 * dot.abs().acos(); // Angular distance in radians

This version reflects the minimal rotation angle between two orientations on the unit hypersphere.

Source§

impl PartialEq for QuaternionSpace

Source§

fn eq(&self, other: &QuaternionSpace) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Spatial<f64> for QuaternionSpace

Source§

impl StructuralPartialEq for QuaternionSpace

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Satisfies<NoConstraint> for T