Rotation

Struct Rotation 

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

Rotation representation for 3D rotations

This class provides a convenient way to represent and manipulate 3D rotations. It supports multiple representations including quaternions, rotation matrices, Euler angles (in various conventions), and axis-angle representation.

Rotations can be composed, inverted, and applied to vectors.

§Examples

use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;

// Create a rotation from a quaternion [w, x, y, z]
let quat = array![std::f64::consts::FRAC_1_SQRT_2, std::f64::consts::FRAC_1_SQRT_2, 0.0, 0.0]; // 90 degree rotation around X
let rot = Rotation::from_quat(&quat.view()).unwrap();

// Apply the rotation to a vector
let vec = array![0.0, 1.0, 0.0];
let rotated = rot.apply(&vec.view()).unwrap();

// Verify the result (should be approximately [0, 0, 1])
assert!((rotated[0]).abs() < 1e-10);
assert!((rotated[1]).abs() < 1e-10);
assert!((rotated[2] - 1.0).abs() < 1e-10);

// Convert to other representations
let matrix = rot.as_matrix();
let euler = rot.as_euler("xyz").unwrap();
let axis_angle = rot.as_rotvec();

Implementations§

Source§

impl Rotation

Source

pub fn from_quat(quat: &ArrayView1<'_, f64>) -> SpatialResult<Self>

Create a new rotation from a quaternion [w, x, y, z]

§Arguments
  • quat - A quaternion in the format [w, x, y, z] where w is the scalar part
§Returns

A SpatialResult containing the rotation if valid, or an error if the quaternion is invalid

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;

// Create a quaternion for a 90-degree rotation around the x-axis
let quat = array![std::f64::consts::FRAC_1_SQRT_2, std::f64::consts::FRAC_1_SQRT_2, 0.0, 0.0];
let rot = Rotation::from_quat(&quat.view()).unwrap();
Source

pub fn from_matrix(matrix: &ArrayView2<'_, f64>) -> SpatialResult<Self>

Create a rotation from a rotation matrix

§Arguments
  • matrix - A 3x3 orthogonal rotation matrix
§Returns

A SpatialResult containing the rotation if valid, or an error if the matrix is invalid

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;

// Create a rotation matrix for a 90-degree rotation around the z-axis
let matrix = array![
    [0.0, -1.0, 0.0],
    [1.0, 0.0, 0.0],
    [0.0, 0.0, 1.0]
];
let rot = Rotation::from_matrix(&matrix.view()).unwrap();
Source

pub fn from_euler( angles: &ArrayView1<'_, f64>, convention: &str, ) -> SpatialResult<Self>

Create a rotation from Euler angles

§Arguments
  • angles - A 3-element array of Euler angles (in radians)
  • convention - The Euler angle convention (e.g., “xyz”, “zyx”)
§Returns

A SpatialResult containing the rotation if valid, or an error if the angles are invalid

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

// Create a rotation using Euler angles in the XYZ convention
let angles = array![PI/2.0, 0.0, 0.0]; // 90 degrees around X
let rot = Rotation::from_euler(&angles.view(), "xyz").unwrap();
Source

pub fn from_rotvec(rotvec: &ArrayView1<'_, f64>) -> SpatialResult<Self>

Create a rotation from an axis-angle representation (rotation vector)

§Arguments
  • rotvec - A 3-element array representing the rotation axis and angle (axis is the unit vector in the direction of the array, and the angle is the magnitude of the array in radians)
§Returns

A SpatialResult containing the rotation if valid, or an error if the rotation vector is invalid

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

// Create a rotation for a 90-degree rotation around the x-axis
let rotvec = array![PI/2.0, 0.0, 0.0];
let rot = Rotation::from_rotvec(&rotvec.view()).unwrap();
Source

pub fn as_matrix(&self) -> Array2<f64>

Convert the rotation to a rotation matrix

§Returns

A 3x3 rotation matrix

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

let angles = array![0.0, 0.0, PI/2.0];
let rot = Rotation::from_euler(&angles.view(), "xyz").unwrap();
let matrix = rot.as_matrix();
// Should approximately be a 90 degree rotation around Z
Source

pub fn as_euler(&self, convention: &str) -> SpatialResult<Array1<f64>>

Convert the rotation to Euler angles

§Arguments
  • convention - The Euler angle convention to use (e.g., “xyz”, “zyx”)
§Returns

A SpatialResult containing a 3-element array of Euler angles (in radians), or an error if the convention is invalid

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

let rot = Rotation::from_rotvec(&array![PI/2.0, 0.0, 0.0].view()).unwrap();
let angles = rot.as_euler("xyz").unwrap();
// Should be approximately [PI/2, 0, 0]
Source

pub fn as_rotvec(&self) -> Array1<f64>

Convert the rotation to an axis-angle representation (rotation vector)

§Returns

A 3-element array representing the rotation axis and angle (axis is the unit vector in the direction of the array, and the angle is the magnitude of the array in radians)

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

let angles = array![PI/2.0, 0.0, 0.0];
let rot = Rotation::from_euler(&angles.view(), "xyz").unwrap();
let rotvec = rot.as_rotvec();
// Should be approximately [PI/2, 0, 0]
Source

pub fn as_quat(&self) -> Array1<f64>

Get the quaternion representation [w, x, y, z]

§Returns

A 4-element array representing the quaternion (w, x, y, z)

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;

let angles = array![0.0, 0.0, 0.0];
let rot = Rotation::from_euler(&angles.view(), "xyz").unwrap();
let quat = rot.as_quat();
// Should be [1, 0, 0, 0] (identity rotation)
Source

pub fn inv(&self) -> Rotation

Get the inverse of the rotation

§Returns

A new Rotation that is the inverse of this one

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

let angles = array![0.0, 0.0, PI/4.0];
let rot = Rotation::from_euler(&angles.view(), "xyz").unwrap();
let rot_inv = rot.inv();
Source

pub fn apply(&self, vec: &ArrayView1<'_, f64>) -> SpatialResult<Array1<f64>>

Apply the rotation to a vector or array of vectors

§Arguments
  • vec - A 3-element vector or a 2D array of 3-element vectors to rotate
§Returns

The rotated vector or array of vectors

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

let angles = array![0.0, 0.0, PI/2.0];
let rot = Rotation::from_euler(&angles.view(), "xyz").unwrap();
let vec = array![1.0, 0.0, 0.0];
let rotated = rot.apply(&vec.view());
// Should be approximately [0, 1, 0]
Source

pub fn apply_multiple( &self, vecs: &ArrayView2<'_, f64>, ) -> SpatialResult<Array2<f64>>

Apply the rotation to multiple vectors

§Arguments
  • vecs - A 2D array of vectors (each row is a 3-element vector)
§Returns

A 2D array of rotated vectors

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

let angles = array![0.0, 0.0, PI/2.0];
let rot = Rotation::from_euler(&angles.view(), "xyz").unwrap();
let vecs = array![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
let rotated = rot.apply_multiple(&vecs.view());
// First row should be approximately [0, 1, 0]
// Second row should be approximately [-1, 0, 0]
Source

pub fn compose(&self, other: &Rotation) -> Rotation

Combine this rotation with another (apply the other rotation after this one)

§Arguments
  • other - The other rotation to combine with
§Returns

A new rotation that represents the composition of the two rotations

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

// Rotate 90 degrees around X, then 90 degrees around Y
let angles1 = array![PI/2.0, 0.0, 0.0];
let rot1 = Rotation::from_euler(&angles1.view(), "xyz").unwrap();
let angles2 = array![0.0, PI/2.0, 0.0];
let rot2 = Rotation::from_euler(&angles2.view(), "xyz").unwrap();
let combined = rot1.compose(&rot2);
Source

pub fn identity() -> Rotation

Create an identity rotation (no rotation)

§Returns

A new Rotation that represents no rotation

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;

let identity = Rotation::identity();
let vec = array![1.0, 2.0, 3.0];
let rotated = identity.apply(&vec.view());
// Should still be [1.0, 2.0, 3.0]
Source

pub fn random() -> Rotation

Create a random uniform rotation

§Returns

A new random Rotation uniformly distributed over the rotation space

§Examples
use scirs2_spatial::transform::Rotation;

let random_rot = Rotation::random();
Source

pub fn slerp(&self, other: &Rotation, t: f64) -> Rotation

Spherical linear interpolation (SLERP) between two rotations

§Arguments
  • other - The target rotation to interpolate towards
  • t - Interpolation parameter (0.0 = this rotation, 1.0 = other rotation)
§Returns

A new rotation interpolated between this and the other rotation

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

let rot1 = Rotation::identity();
let rot2 = Rotation::from_euler(&array![0.0, 0.0, PI/2.0].view(), "xyz").unwrap();
let interpolated = rot1.slerp(&rot2, 0.5);
Source

pub fn angular_distance(&self, other: &Rotation) -> f64

Calculate the angular distance between two rotations

§Arguments
  • other - The other rotation to calculate distance to
§Returns

The angular distance in radians (always positive, in range [0, π])

§Examples
use scirs2_spatial::transform::Rotation;
use scirs2_core::ndarray::array;
use std::f64::consts::PI;

let rot1 = Rotation::identity();
let rot2 = Rotation::from_euler(&array![0.0, 0.0, PI/2.0].view(), "xyz")?;
let distance = rot1.angular_distance(&rot2);
assert!((distance - PI/2.0).abs() < 1e-10);

Trait Implementations§

Source§

impl Clone for Rotation

Source§

fn clone(&self) -> Rotation

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 Debug for Rotation

Source§

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

Formats the value using the given formatter. Read more

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V