Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to scale Rotor3 #127

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- next-header -->
## Unreleased

- Add methods to scale `Rotor3`

## 0.8.1

Expand Down
33 changes: 33 additions & 0 deletions src/rotor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,21 @@ macro_rules! rotor3s {

}

/// Multiply the angle of the rotation represented by self by `scale`.
#[inline]
pub fn scale_by(&mut self, scale: $t) {
*self = self.scaled_by(scale)
}

/// Return a rotor representing the same rotatation as `self` but with an angle
/// multiplied by `scale`
#[inline]
#[must_use]
pub fn scaled_by(self, scale: $t) -> Self {
let (angle, plane) = self.into_angle_plane();
Self::from_angle_plane(angle * scale, plane)
}

/// Create new Rotor from a rotation in the xy plane (also known as
/// "around the z axis").
#[inline]
Expand Down Expand Up @@ -883,6 +898,24 @@ mod test {
);
}

#[test]
pub fn rotor_scaling() {
use std::f32::consts::PI;

let axis = Vec3::new(0.42, 0.123, 0.789).normalized(); //aribitrary rotation axis
let plane = Bivec3::from_normalized_axis(axis).normalized();
let angle = PI / 10.;

// rotation of angle pi/10 on the axis;
let rotation_1 = Rotor3::from_angle_plane(angle, plane);

let fraction = 1.234;

let scaled_rotor_1 = Rotor3::from_angle_plane(fraction * angle, plane);
let scaled_rotor_2 = rotation_1.scaled_by(fraction);
assert!(scaled_rotor_1.eq_eps(scaled_rotor_2));
}

// This test exists because Rotor3 used to implement PartialEq without DRotor3 getting the same
// impl. Use `cargo test --all-features` to run
#[cfg(feature = "f64")]
Expand Down