diff --git a/src/base/norm.rs b/src/base/norm.rs index 93319ddc..629fd297 100644 --- a/src/base/norm.rs +++ b/src/base/norm.rs @@ -185,6 +185,21 @@ impl> Matrix { self.norm_squared() } + + /// Sets the magnitude of this vector unless it is smaller than `min_magnitude`. + /// + /// If `self.magnitude()` is smaller than `min_magnitude`, it will be left unchanged. + /// Otherwise this is equivalent to: `*self = self.normalize() * magnitude. + #[inline] + pub fn try_set_magnitude(&mut self, magnitude: N::RealField, min_magnitude: N::RealField) + where S: StorageMut { + let n = self.norm(); + + if n >= min_magnitude { + self.scale_mut(magnitude / n) + } + } + /// Returns a normalized version of this matrix. #[inline] pub fn normalize(&self) -> MatrixMN @@ -225,7 +240,7 @@ impl> Matrix /// Normalizes this matrix in-place or does nothing if its norm is smaller or equal to `eps`. /// - /// If the normalization succeeded, returns the old normal of this matrix. + /// If the normalization succeeded, returns the old norm of this matrix. #[inline] pub fn try_normalize_mut(&mut self, min_norm: N::RealField) -> Option { let n = self.norm();