Add a method to cap the magnitude of a vector.
This commit is contained in:
parent
6139372c38
commit
9d930eb21a
@ -8,7 +8,7 @@ use crate::allocator::Allocator;
|
|||||||
use crate::base::{DefaultAllocator, Dim, DimName, Matrix, MatrixMN, Normed, VectorN};
|
use crate::base::{DefaultAllocator, Dim, DimName, Matrix, MatrixMN, Normed, VectorN};
|
||||||
use crate::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
|
use crate::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
|
||||||
use crate::storage::{Storage, StorageMut};
|
use crate::storage::{Storage, StorageMut};
|
||||||
use crate::{ComplexField, Scalar, SimdComplexField, Unit};
|
use crate::{ComplexField, RealField, Scalar, SimdComplexField, Unit};
|
||||||
use simba::scalar::ClosedNeg;
|
use simba::scalar::ClosedNeg;
|
||||||
use simba::simd::{SimdOption, SimdPartialOrd};
|
use simba::simd::{SimdOption, SimdPartialOrd};
|
||||||
|
|
||||||
@ -334,11 +334,27 @@ impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> Matrix<N, R, C, S> {
|
|||||||
{
|
{
|
||||||
let n = self.norm();
|
let n = self.norm();
|
||||||
|
|
||||||
if n >= min_magnitude {
|
if n > min_magnitude {
|
||||||
self.scale_mut(magnitude / n)
|
self.scale_mut(magnitude / n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a new vector with the same magnitude as `self` clamped between `0.0` and `max`.
|
||||||
|
#[inline]
|
||||||
|
pub fn cap_magnitude(&self, max: N::RealField) -> MatrixMN<N, R, C>
|
||||||
|
where
|
||||||
|
N: RealField,
|
||||||
|
DefaultAllocator: Allocator<N, R, C>,
|
||||||
|
{
|
||||||
|
let n = self.norm();
|
||||||
|
|
||||||
|
if n > max {
|
||||||
|
self.scale(max / n)
|
||||||
|
} else {
|
||||||
|
self.clone_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a normalized version of this matrix unless its norm as smaller or equal to `eps`.
|
/// Returns a normalized version of this matrix unless its norm as smaller or equal to `eps`.
|
||||||
///
|
///
|
||||||
/// The components of this matrix cannot be SIMD types (see `simd_try_normalize`) instead.
|
/// The components of this matrix cannot be SIMD types (see `simd_try_normalize`) instead.
|
||||||
|
Loading…
Reference in New Issue
Block a user