1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use alga::general::Real;
use core::{DefaultAllocator, Matrix, Scalar, Unit, Vector};
use core::constraint::{AreMultipliable, DimEq, SameNumberOfRows, ShapeConstraint};
use core::allocator::Allocator;
use dimension::{Dim, DimName, U1};
use storage::{Storage, StorageMut};
use geometry::Point;
pub struct Reflection<N: Scalar, D: Dim, S: Storage<N, D>> {
axis: Vector<N, D, S>,
bias: N,
}
impl<N: Real, D: Dim, S: Storage<N, D>> Reflection<N, D, S> {
pub fn new(axis: Unit<Vector<N, D, S>>, bias: N) -> Reflection<N, D, S> {
Reflection {
axis: axis.unwrap(),
bias: bias,
}
}
pub fn new_containing_point(
axis: Unit<Vector<N, D, S>>,
pt: &Point<N, D>,
) -> Reflection<N, D, S>
where
D: DimName,
DefaultAllocator: Allocator<N, D>,
{
let bias = pt.coords.dot(axis.as_ref());
Self::new(axis, bias)
}
pub fn axis(&self) -> &Vector<N, D, S> {
&self.axis
}
pub fn reflect<R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<N, R2, C2, S2>)
where
S2: StorageMut<N, R2, C2>,
ShapeConstraint: SameNumberOfRows<R2, D>,
{
for i in 0..rhs.ncols() {
let m_two: N = ::convert(-2.0f64);
let factor = (rhs.column(i).dot(&self.axis) - self.bias) * m_two;
rhs.column_mut(i).axpy(factor, &self.axis, N::one());
}
}
pub fn reflect_rows<R2: Dim, C2: Dim, S2, S3>(
&self,
rhs: &mut Matrix<N, R2, C2, S2>,
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>,
{
rhs.mul_to(&self.axis, work);
if !self.bias.is_zero() {
work.add_scalar_mut(-self.bias);
}
let m_two: N = ::convert(-2.0f64);
rhs.ger(m_two, &work, &self.axis, N::one());
}
}