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
impl Rotation
Sourcepub fn from_quat(quat: &ArrayView1<'_, f64>) -> SpatialResult<Self>
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();Sourcepub fn from_matrix(matrix: &ArrayView2<'_, f64>) -> SpatialResult<Self>
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();Sourcepub fn from_euler(
angles: &ArrayView1<'_, f64>,
convention: &str,
) -> SpatialResult<Self>
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();Sourcepub fn from_rotvec(rotvec: &ArrayView1<'_, f64>) -> SpatialResult<Self>
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();Sourcepub fn as_matrix(&self) -> Array2<f64>
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 ZSourcepub fn as_euler(&self, convention: &str) -> SpatialResult<Array1<f64>>
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]Sourcepub fn as_rotvec(&self) -> Array1<f64>
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]Sourcepub fn as_quat(&self) -> Array1<f64>
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)Sourcepub fn inv(&self) -> Rotation
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();Sourcepub fn apply(&self, vec: &ArrayView1<'_, f64>) -> SpatialResult<Array1<f64>>
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]Sourcepub fn apply_multiple(
&self,
vecs: &ArrayView2<'_, f64>,
) -> SpatialResult<Array2<f64>>
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]Sourcepub fn compose(&self, other: &Rotation) -> Rotation
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);Sourcepub fn identity() -> Rotation
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]Sourcepub fn slerp(&self, other: &Rotation, t: f64) -> Rotation
pub fn slerp(&self, other: &Rotation, t: f64) -> Rotation
Spherical linear interpolation (SLERP) between two rotations
§Arguments
other- The target rotation to interpolate towardst- 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);Sourcepub fn angular_distance(&self, other: &Rotation) -> f64
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§
Auto Trait Implementations§
impl Freeze for Rotation
impl RefUnwindSafe for Rotation
impl Send for Rotation
impl Sync for Rotation
impl Unpin for Rotation
impl UnwindSafe for Rotation
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.