From 9d07a204bacbb7fc1a56b13bd6398e9e2f98e16c Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Tue, 3 Jan 2023 12:54:51 -0800 Subject: [PATCH] remove default `Angle` wrapping behavior In CAD applications, it's often useful to work with negative angles (where the direction is significant) or angles with a magnitude greater than 1 revolution (such as for helical sweeps or mechanical simulations). `Angle` is currently only used to specify rotations, where wrapping doesn't matter. This commit removes the default wrapping behavior, which can still be accessed manually as needed using `Angle::normalized`. It may still be worth exploring automatic wrapping solutions in the future to reduce floating point inaccuracy across repeated calculations, but it's unlikely to cause problems any time soon. --- crates/fj/src/angle.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/crates/fj/src/angle.rs b/crates/fj/src/angle.rs index b4f1cfb123..807a9cccfd 100644 --- a/crates/fj/src/angle.rs +++ b/crates/fj/src/angle.rs @@ -7,16 +7,14 @@ const GON_RAD: f64 = PI / 200.; #[derive(Copy, Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Angle { - // The value of the angle in radians + /// The value of the angle in radians rad: f64, } impl Angle { /// Create a new angle specified in radians pub fn from_rad(rad: f64) -> Self { - Self { - rad: Self::wrap(rad), - } + Self { rad } } /// Create a new angle specified in degrees pub fn from_deg(deg: f64) -> Self { @@ -47,6 +45,13 @@ impl Angle { self.rad / GON_RAD } + /// Returns this angle normalized to the range [0, 2pi) radians + pub fn normalized(&self) -> Self { + Self { + rad: Self::wrap(self.rad), + } + } + // ensures that the angle is always 0 <= a < 2*pi fn wrap(rad: f64) -> f64 { let modulo = rad % TAU; @@ -56,11 +61,6 @@ impl Angle { modulo } } - - // ensures that the angle is always 0 <= a < 2*pi - fn wrap_assign(&mut self) { - self.rad = Self::wrap(self.rad); - } } impl std::ops::Add for Angle { @@ -73,7 +73,6 @@ impl std::ops::Add for Angle { impl std::ops::AddAssign for Angle { fn add_assign(&mut self, rhs: Self) { self.rad += rhs.rad; - self.wrap_assign(); } } @@ -87,7 +86,6 @@ impl std::ops::Sub for Angle { impl std::ops::SubAssign for Angle { fn sub_assign(&mut self, rhs: Self) { self.rad -= rhs.rad; - self.wrap_assign(); } } @@ -108,7 +106,6 @@ impl std::ops::Mul for f64 { impl std::ops::MulAssign for Angle { fn mul_assign(&mut self, rhs: f64) { self.rad *= rhs; - self.wrap_assign(); } } @@ -122,7 +119,6 @@ impl std::ops::Div for Angle { impl std::ops::DivAssign for Angle { fn div_assign(&mut self, rhs: f64) { self.rad /= rhs; - self.wrap_assign(); } }