Skip to content

Commit

Permalink
fix: Merge git butler branch into main. Was a nice tool experiment.
Browse files Browse the repository at this point in the history
  • Loading branch information
duncaneddy committed Feb 7, 2024
1 parent 1055aef commit 57c3c7d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 46 deletions.
30 changes: 22 additions & 8 deletions src/attitude/attitude_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@

use crate::attitude::attitude_types::{EulerAngle, EulerAxis, Quaternion, RotationMatrix, EulerAngleOrder};

/// `AttitudeRepresentation` trait defines the interface for converting between different attitude representations.
/// `ToAttitude` trait defines the interface for converting to different attitude representations. Any struct that
/// implements `ToAttitude` can convert its attitude representation into the main attitude representation methods of
/// `Quaternion`, `EulerAxis`, `EulerAngle`, and `RotationMatrix`.
///
/// This trait is implemented by the `Quaternion`, `EulerAxis`, `EulerAngle`, and `RotationMatrix` structs. The
/// trait provides methods for converting between the different representations. Since all attitude representations
/// are ultimately equivalent, any representation can be converted to any other representation.
/// This trait is implemented by the `Quaternion`, `EulerAxis`, `EulerAngle`, and `RotationMatrix` structs.
///
/// See [_Representing Attitude: Euler Angles, Unit Quaternions, and Rotation Vectors_ by James Diebel](https://www.astro.rug.nl/software/kapteyn-beta/_downloads/attitude.pdf) for more information
/// on the different attitude representations and their conversions.
pub trait ToAttitude {
fn from_quaternion(q: Quaternion) -> Self;
fn from_euler_axis(e: EulerAxis) -> Self;
fn from_euler_angle(e: EulerAngle) -> Self;
fn from_rotation_matrix(r: RotationMatrix) -> Self;

fn to_quaternion(&self) -> Quaternion;
fn to_euler_axis(&self) -> EulerAxis;
fn to_euler_angle(&self, order: EulerAngleOrder) -> EulerAngle;
fn to_rotation_matrix(&self) -> RotationMatrix;
}

/// `FromAttitude` trait defines the interface for initializing an attitude representation from an alternative
/// representation. Any struct that implements `FromAttitude` can be initialized from the main attitude representation
/// methods of `Quaternion`, `EulerAxis`, and `RotationMatrix`.
///
/// This trait is implemented by the `Quaternion`, `EulerAxis`, and `RotationMatrix` structs. It is _**NOT**_ implemented
/// by the `EulerAngle` struct, as when converting to an Euler Angle representation from a different attitude
/// representation and angle order must be supplied as well.
///
/// See [_Representing Attitude: Euler Angles, Unit Quaternions, and Rotation Vectors_ by James Diebel](https://www.astro.rug.nl/software/kapteyn-beta/_downloads/attitude.pdf) for more information
/// on the different attitude representations and their conversions.
pub trait FromAttitude {
fn from_quaternion(q: Quaternion) -> Self;
fn from_euler_axis(e: EulerAxis) -> Self;
fn from_euler_angle(e: EulerAngle) -> Self;
fn from_rotation_matrix(r: RotationMatrix) -> Self;
}
13 changes: 9 additions & 4 deletions src/attitude/attitude_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use nalgebra::{Matrix3, Vector3, Vector4};
///
/// `q = [s, v] = [s, v1, v2, v3]`
///
/// `Quaternion` structure implements the AttitudeRepresentation trait, which allows for conversion to and from
/// `Quaternion` structure implements the `ToAttitude` and `FromAttitude` traits, which allows for conversion to and from
/// other attitude representations. Specifically, `EulerAxis`, `EulerAngle`, and `RotationMatrix`.
#[derive(Clone, Copy)]
pub struct Quaternion {
Expand All @@ -23,7 +23,7 @@ pub struct Quaternion {
///
/// `e = [angle, v] = [angle, v1, v2, v3]`
///
/// `EulerAxis` structure implements the AttitudeRepresentation trait, which allows for conversion to and from
/// `EulerAxis` structure implements the `ToAttitude` and `FromAttitude` trait`, which allows for conversion to and from
/// other attitude representations. Specifically, `Quaternion`, `EulerAngle`, and `RotationMatrix`.
#[derive(Clone, Copy)]
pub struct EulerAxis {
Expand Down Expand Up @@ -107,8 +107,13 @@ impl fmt::Debug for EulerAngleOrder {
/// Where `phi` is first rotation, `theta` is the second rotation, and `psi` is the third rotation. The axis of each
/// rotation is determined by the order, which is specified by the `EulerAngleOrder` enum field, `order`.
///
/// The EulerAngle structure implements the AttitudeRepresentation trait, which allows for conversion to and from
/// The EulerAngle structure implements the `ToAttitude` trait, which allows for conversion to
/// other attitude representations. Specifically, `Quaternion`, `EulerAxis`, and `RotationMatrix`.
///
/// It does _**NOT**_ implement the `FromAttitude` trait, as when converting to an Euler Angle representation from a
/// different attitude representation and angle order must be supplied. Since this information is not part of the
/// `FromAttitude` trait method signatures, `EulerAngle` implements its own initialization function for initialization
/// from `Quaternion`, `EulerAxis`, and `RotationMatrix`.
#[derive(Clone, Copy)]
pub struct EulerAngle {
pub order: EulerAngleOrder,
Expand All @@ -124,7 +129,7 @@ pub struct EulerAngle {
/// ` | r21, r22, r23 |`
/// ` | r31, r32, r33 |`
///
/// The RotationMatrix structure implements the AttitudeRepresentation trait, which allows for conversion to and from
/// The RotationMatrix structure implements the `ToAttitude` and `FromAttitude` traits, which allows for conversion to and from
/// other attitude representations. Specifically, `Quaternion`, `EulerAxis`, and `EulerAngle`.
#[derive(Clone, Copy)]
pub struct RotationMatrix {
Expand Down
59 changes: 29 additions & 30 deletions src/attitude/euler_angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,37 @@ impl EulerAngle {
///
/// let e = EulerAngle::from_vector(EulerAngleOrder::XYZ, v, true);
/// ```
pub fn from_vector(order: EulerAngleOrder, vector: Vector3<f64>, as_degrees: bool) -> Self {
pub fn from_vector(vector: Vector3<f64>, order: EulerAngleOrder, as_degrees: bool) -> Self {
Self::new(order, vector.x, vector.y, vector.z, as_degrees)
}

/// Convert a `Quaternion` to an `EulerAngle`.
///
/// # Arguments
///
/// - `q` - A `Quaternion` struct.
///
/// # Returns
///
/// - A new `EulerAngle` struct.
///
/// # Example
///
fn from_quaternion(q: Quaternion, order: EulerAngleOrder) -> Self {
q.to_euler_angle(order)
}

fn from_euler_axis(e: EulerAxis, order: EulerAngleOrder) -> Self {
todo!()
}

fn from_euler_angle(e: EulerAngle, order: EulerAngleOrder) -> Self {
todo!()
}

fn from_rotation_matrix(r: RotationMatrix, order: EulerAngleOrder) -> Self {
todo!()
}
}

impl fmt::Display for EulerAngle {
Expand All @@ -89,35 +117,6 @@ impl fmt::Debug for EulerAngle {
}

impl ToAttitude for EulerAngle {

/// Convert a `Quaternion` to an `EulerAngle`.
///
/// # Arguments
///
/// - `q` - A `Quaternion` struct.
///
/// # Returns
///
/// - A new `EulerAngle` struct.
///
/// # Example
///
fn from_quaternion(q: Quaternion, order: EulerAngleOrder) -> Self {
q.to_euler_angle()
}

fn from_euler_axis(e: EulerAxis) -> Self {
todo!()
}

fn from_euler_angle(e: EulerAngle) -> Self {
todo!()
}

fn from_rotation_matrix(r: RotationMatrix) -> Self {
todo!()
}

fn to_quaternion(&self) -> Quaternion {
todo!()
}
Expand Down
5 changes: 4 additions & 1 deletion src/attitude/euler_axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use nalgebra::{Vector3, Vector4};
use crate::constants::{DEG2RAD, RAD2DEG};
use crate::attitude::ToAttitude;
use crate::attitude::attitude_types::{Quaternion, EulerAngle, RotationMatrix, EulerAngleOrder, EulerAxis};
use crate::FromAttitude;

impl EulerAxis {
pub fn new(axis: Vector3<f64>, angle: f64) -> Self {
Expand Down Expand Up @@ -77,7 +78,7 @@ impl PartialEq for EulerAxis {
}
}

impl ToAttitude for EulerAxis {
impl FromAttitude for EulerAxis {
fn from_quaternion(q: Quaternion) -> Self {
todo!()
}
Expand All @@ -93,7 +94,9 @@ impl ToAttitude for EulerAxis {
fn from_rotation_matrix(r: RotationMatrix) -> Self {
todo!()
}
}

impl ToAttitude for EulerAxis {
fn to_quaternion(&self) -> Quaternion {
todo!()
}
Expand Down
6 changes: 4 additions & 2 deletions src/attitude/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{fmt, ops};

use crate::attitude::attitude_representation::ToAttitude;
use crate::attitude::attitude_types::{EulerAngle, EulerAxis, EulerAngleOrder, RotationMatrix};
use crate::Quaternion;
use crate::{FromAttitude, Quaternion};

impl Quaternion {
/// Create a new `Quaternion` from the scalar and vector components.
Expand Down Expand Up @@ -350,7 +350,7 @@ impl PartialEq for Quaternion {
}
}

impl ToAttitude for Quaternion {
impl FromAttitude for Quaternion {
/// Create a new `Quaternion` from a `Quaternion`.
///
/// # Arguments
Expand Down Expand Up @@ -555,7 +555,9 @@ impl ToAttitude for Quaternion {
};
}
}
}

impl ToAttitude for Quaternion {
/// Return a new `Quaternion` from the current one. Since the `Quaternion` is already a `Quaternion`, this
/// method simply returns a copy of the quaternion.
///
Expand Down
5 changes: 4 additions & 1 deletion src/attitude/rotation_maxtrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use nalgebra::{Matrix3, Vector3};
use crate::constants::{DEG2RAD, RAD2DEG};
use crate::attitude::ToAttitude;
use crate::attitude::attitude_types::{Quaternion, EulerAngle, EulerAxis, EulerAngleOrder, RotationMatrix};
use crate::FromAttitude;

impl RotationMatrix {
pub fn new(matrix: Matrix3<f64>) -> Self {
Expand Down Expand Up @@ -111,7 +112,7 @@ impl std::ops::Index<(usize, usize)> for RotationMatrix {
}
}

impl ToAttitude for RotationMatrix {
impl FromAttitude for RotationMatrix {
fn from_quaternion(q: Quaternion) -> Self {
todo!()
}
Expand All @@ -127,7 +128,9 @@ impl ToAttitude for RotationMatrix {
fn from_rotation_matrix(r: RotationMatrix) -> Self {
todo!()
}
}

impl ToAttitude for RotationMatrix {
fn to_quaternion(&self) -> Quaternion {
todo!()
}
Expand Down

0 comments on commit 57c3c7d

Please sign in to comment.