nalgebra/src/geometry/reflection.rs

124 lines
4.2 KiB
Rust
Raw Normal View History

use alga::general::Complex;
use base::allocator::Allocator;
use base::constraint::{AreMultipliable, DimEq, SameNumberOfRows, ShapeConstraint};
use base::{DefaultAllocator, Matrix, Scalar, Unit, Vector};
use dimension::{Dim, DimName, U1};
use storage::{Storage, StorageMut};
use geometry::Point;
/// A reflection wrt. a plane.
pub struct Reflection<N: Scalar, D: Dim, S: Storage<N, D>> {
2018-02-02 19:26:35 +08:00
axis: Vector<N, D, S>,
bias: N,
}
impl<N: Complex, D: Dim, S: Storage<N, D>> Reflection<N, D, S> {
/// 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.
pub fn new(axis: Unit<Vector<N, D, S>>, bias: N) -> Self {
Self {
axis: axis.into_inner(),
bias,
2018-02-02 19:26:35 +08:00
}
}
/// Creates a new reflection wrt. the plane orthogonal to the given axis and that contains the
/// point `pt`.
2018-02-02 19:26:35 +08:00
pub fn new_containing_point(
axis: Unit<Vector<N, D, S>>,
pt: &Point<N, D>,
) -> Self
2018-02-02 19:26:35 +08:00
where
D: DimName,
DefaultAllocator: Allocator<N, D>,
{
let bias = axis.dotc(&pt.coords);
Self::new(axis, bias)
}
/// The reflexion axis.
pub fn axis(&self) -> &Vector<N, D, S> {
&self.axis
}
2018-09-24 12:48:42 +08:00
// FIXME: naming convention: reflect_to, reflect_assign ?
/// Applies the reflection to the columns of `rhs`.
pub fn reflect<R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<N, R2, C2, S2>)
2018-02-02 19:26:35 +08:00
where
S2: StorageMut<N, R2, C2>,
ShapeConstraint: SameNumberOfRows<R2, D>,
{
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.
let m_two: N = ::convert(-2.0f64);
let factor = (self.axis.dotc(&rhs.column(i)) - self.bias) * m_two;
rhs.column_mut(i).axpy(factor, &self.axis, N::one());
}
}
2019-03-19 19:00:10 +08:00
// FIXME: naming convention: reflect_to, reflect_assign ?
/// Applies the reflection to the columns of `rhs`.
pub fn reflect_with_sign<R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<N, R2, C2, S2>, sign: N)
where
S2: StorageMut<N, R2, C2>,
ShapeConstraint: SameNumberOfRows<R2, D>,
{
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.
let m_two = sign.scale(::convert(-2.0f64));
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,
2019-03-18 18:23:19 +08:00
lhs: &mut Matrix<N, R2, C2, S2>,
2018-02-02 19:26:35 +08:00
work: &mut Vector<N, R2, S3>,
) where
S2: StorageMut<N, R2, C2>,
S3: StorageMut<N, R2>,
ShapeConstraint: DimEq<C2, D> + AreMultipliable<R2, C2, D, U1>,
DefaultAllocator: Allocator<N, D>
2018-02-02 19:26:35 +08:00
{
2019-03-18 18:23:19 +08:00
lhs.mul_to(&self.axis, work);
if !self.bias.is_zero() {
work.add_scalar_mut(-self.bias);
}
let m_two: N = ::convert(-2.0f64);
2019-03-18 18:23:19 +08:00
lhs.ger(m_two, &work, &self.axis.conjugate(), N::one());
}
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,
lhs: &mut Matrix<N, R2, C2, S2>,
work: &mut Vector<N, R2, S3>,
sign: N,
) where
S2: StorageMut<N, R2, C2>,
S3: StorageMut<N, R2>,
ShapeConstraint: DimEq<C2, D> + AreMultipliable<R2, C2, D, U1>,
DefaultAllocator: Allocator<N, D>
{
lhs.mul_to(&self.axis, work);
if !self.bias.is_zero() {
work.add_scalar_mut(-self.bias);
}
let m_two = sign.scale(::convert(-2.0f64));
lhs.ger(m_two, &work, &self.axis.conjugate(), sign);
}
}