Skip to content

Commit

Permalink
Add methods to scale Rotor3 (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
thenlevy authored Jul 15, 2021
1 parent 5573bcc commit 9ba47d0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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

0 comments on commit 9ba47d0

Please sign in to comment.