extern crate alga;
extern crate nalgebra as na;
use alga::linear::FiniteDimInnerSpace;
use na::{Real, DefaultAllocator, Unit, VectorN, Vector2, Vector3};
use na::allocator::Allocator;
use na::dimension::Dim;
fn reflect_wrt_hyperplane_with_algebraic_genericity<V>(plane_normal: &Unit<V>, vector: &V) -> V
where V: FiniteDimInnerSpace + Copy {
let n = plane_normal.as_ref(); *vector - *n * (n.dot(vector) * na::convert(2.0))
}
fn reflect_wrt_hyperplane_with_dimensional_genericity<N: Real, D: Dim>(plane_normal: &Unit<VectorN<N, D>>,
vector: &VectorN<N, D>)
-> VectorN<N, D>
where N: Real,
D: Dim,
DefaultAllocator: Allocator<N, D> {
let n = plane_normal.as_ref(); vector - n * (n.dot(vector) * na::convert(2.0))
}
fn reflect_wrt_hyperplane2<N>(plane_normal: &Unit<Vector2<N>>,
vector: &Vector2<N>)
-> Vector2<N>
where N: Real {
let n = plane_normal.as_ref(); vector - n * (n.dot(vector) * na::convert(2.0))
}
fn reflect_wrt_hyperplane3<N>(plane_normal: &Unit<Vector3<N>>,
vector: &Vector3<N>)
-> Vector3<N>
where N: Real {
let n = plane_normal.as_ref(); vector - n * (n.dot(vector) * na::convert(2.0))
}
fn main() {
let plane2 = Vector2::y_axis(); let plane3 = Vector3::y_axis();
let v2 = Vector2::new(1.0, 2.0); let v3 = Vector3::new(1.0, 2.0, 3.0);
assert_eq!(reflect_wrt_hyperplane_with_algebraic_genericity(&plane2, &v2).y, -2.0);
assert_eq!(reflect_wrt_hyperplane_with_algebraic_genericity(&plane3, &v3).y, -2.0);
assert_eq!(reflect_wrt_hyperplane_with_dimensional_genericity(&plane2, &v2).y, -2.0);
assert_eq!(reflect_wrt_hyperplane_with_dimensional_genericity(&plane3, &v3).y, -2.0);
assert_eq!(reflect_wrt_hyperplane2(&plane2, &v2).y, -2.0);
assert_eq!(reflect_wrt_hyperplane3(&plane3, &v3).y, -2.0);
}