2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::constraint::{AreMultipliable, DimEq, SameNumberOfRows, ShapeConstraint};
|
2021-04-07 20:29:20 +08:00
|
|
|
use crate::base::{Const, Matrix, Scalar, Unit, Vector};
|
|
|
|
use crate::dimension::{Dim, U1};
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::storage::{Storage, StorageMut};
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::ComplexField;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::geometry::Point;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
/// A reflection wrt. a plane.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub struct Reflection<T: Scalar, D: Dim, S: Storage<T, D>> {
|
|
|
|
axis: Vector<T, D, S>,
|
|
|
|
bias: T,
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: ComplexField, S: Storage<T, Const<D>>, const D: usize> Reflection<T, Const<D>, S> {
|
2021-04-07 20:29:20 +08:00
|
|
|
/// Creates a new reflection wrt. the plane orthogonal to the given axis and that contains the
|
|
|
|
/// point `pt`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new_containing_point(axis: Unit<Vector<T, Const<D>, S>>, pt: &Point<T, D>) -> Self {
|
2021-04-07 20:29:20 +08:00
|
|
|
let bias = axis.dotc(&pt.coords);
|
|
|
|
Self::new(axis, bias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: ComplexField, D: Dim, S: Storage<T, D>> Reflection<T, D, S> {
|
2017-08-03 01:37:44 +08:00
|
|
|
/// Creates a new reflection wrt the plane orthogonal to the given axis and bias.
|
|
|
|
///
|
|
|
|
/// The bias is the position of the plane on the axis. In particular, a bias equal to zero
|
|
|
|
/// represents a plane that passes through the origin.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new(axis: Unit<Vector<T, D, S>>, bias: T) -> Self {
|
2019-02-17 05:29:41 +08:00
|
|
|
Self {
|
2018-12-10 04:08:14 +08:00
|
|
|
axis: axis.into_inner(),
|
2019-03-03 02:33:49 +08:00
|
|
|
bias,
|
2018-02-02 19:26:35 +08:00
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The reflexion axis.
|
2021-06-06 20:46:36 +08:00
|
|
|
#[must_use = "This function does not mutate self. You should use the return value."]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn axis(&self) -> &Vector<T, D, S> {
|
2017-08-03 01:37:44 +08:00
|
|
|
&self.axis
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: naming convention: reflect_to, reflect_assign ?
|
2017-08-03 01:37:44 +08:00
|
|
|
/// Applies the reflection to the columns of `rhs`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn reflect<R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<T, R2, C2, S2>)
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S2: StorageMut<T, R2, C2>,
|
2018-02-02 19:26:35 +08:00
|
|
|
ShapeConstraint: SameNumberOfRows<R2, D>,
|
|
|
|
{
|
|
|
|
for i in 0..rhs.ncols() {
|
2017-08-03 01:37:44 +08:00
|
|
|
// NOTE: we borrow the column twice here. First it is borrowed immutably for the
|
|
|
|
// dot product, and then mutably. Somehow, this allows significantly
|
|
|
|
// better optimizations of the dot product from the compiler.
|
2021-04-11 17:00:38 +08:00
|
|
|
let m_two: T = crate::convert(-2.0f64);
|
2019-03-23 18:48:12 +08:00
|
|
|
let factor = (self.axis.dotc(&rhs.column(i)) - self.bias) * m_two;
|
2021-04-11 17:00:38 +08:00
|
|
|
rhs.column_mut(i).axpy(factor, &self.axis, T::one());
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: naming convention: reflect_to, reflect_assign ?
|
2019-03-19 19:00:10 +08:00
|
|
|
/// Applies the reflection to the columns of `rhs`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn reflect_with_sign<R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<T, R2, C2, S2>, sign: T)
|
2020-03-21 19:16:46 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S2: StorageMut<T, R2, C2>,
|
2020-03-21 19:16:46 +08:00
|
|
|
ShapeConstraint: SameNumberOfRows<R2, D>,
|
2019-03-19 19:00:10 +08:00
|
|
|
{
|
|
|
|
for i in 0..rhs.ncols() {
|
|
|
|
// NOTE: we borrow the column twice here. First it is borrowed immutably for the
|
|
|
|
// dot product, and then mutably. Somehow, this allows significantly
|
|
|
|
// better optimizations of the dot product from the compiler.
|
2019-03-23 21:29:07 +08:00
|
|
|
let m_two = sign.scale(crate::convert(-2.0f64));
|
2019-03-23 18:48:12 +08:00
|
|
|
let factor = (self.axis.dotc(&rhs.column(i)) - self.bias) * m_two;
|
2019-03-19 19:00:10 +08:00
|
|
|
rhs.column_mut(i).axpy(factor, &self.axis, sign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 18:23:19 +08:00
|
|
|
/// Applies the reflection to the rows of `lhs`.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub fn reflect_rows<R2: Dim, C2: Dim, S2, S3>(
|
|
|
|
&self,
|
2021-04-11 17:00:38 +08:00
|
|
|
lhs: &mut Matrix<T, R2, C2, S2>,
|
|
|
|
work: &mut Vector<T, R2, S3>,
|
2018-02-02 19:26:35 +08:00
|
|
|
) where
|
2021-04-11 17:00:38 +08:00
|
|
|
S2: StorageMut<T, R2, C2>,
|
|
|
|
S3: StorageMut<T, R2>,
|
2018-02-02 19:26:35 +08:00
|
|
|
ShapeConstraint: DimEq<C2, D> + AreMultipliable<R2, C2, D, U1>,
|
|
|
|
{
|
2019-03-18 18:23:19 +08:00
|
|
|
lhs.mul_to(&self.axis, work);
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
if !self.bias.is_zero() {
|
|
|
|
work.add_scalar_mut(-self.bias);
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let m_two: T = crate::convert(-2.0f64);
|
|
|
|
lhs.gerc(m_two, &work, &self.axis, T::one());
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
2019-03-19 19:00:10 +08:00
|
|
|
|
|
|
|
/// Applies the reflection to the rows of `lhs`.
|
|
|
|
pub fn reflect_rows_with_sign<R2: Dim, C2: Dim, S2, S3>(
|
|
|
|
&self,
|
2021-04-11 17:00:38 +08:00
|
|
|
lhs: &mut Matrix<T, R2, C2, S2>,
|
|
|
|
work: &mut Vector<T, R2, S3>,
|
|
|
|
sign: T,
|
2019-03-19 19:00:10 +08:00
|
|
|
) where
|
2021-04-11 17:00:38 +08:00
|
|
|
S2: StorageMut<T, R2, C2>,
|
|
|
|
S3: StorageMut<T, R2>,
|
2019-03-19 19:00:10 +08:00
|
|
|
ShapeConstraint: DimEq<C2, D> + AreMultipliable<R2, C2, D, U1>,
|
|
|
|
{
|
|
|
|
lhs.mul_to(&self.axis, work);
|
|
|
|
|
|
|
|
if !self.bias.is_zero() {
|
|
|
|
work.add_scalar_mut(-self.bias);
|
|
|
|
}
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
let m_two = sign.scale(crate::convert(-2.0f64));
|
2019-03-23 21:13:00 +08:00
|
|
|
lhs.gerc(m_two, &work, &self.axis, sign);
|
2019-03-19 19:00:10 +08:00
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|