2016-12-05 05:44:42 +08:00
|
|
|
|
use std::ops::{Neg, Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Index, IndexMut};
|
2017-08-03 01:37:44 +08:00
|
|
|
|
use num::{Zero, One};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
use alga::general::{ClosedNeg, ClosedAdd, ClosedSub, ClosedMul, ClosedDiv};
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
use core::{DefaultAllocator, Scalar, Vector, Matrix, VectorSum};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
use core::dimension::{Dim, DimName, U1};
|
|
|
|
|
use core::constraint::{ShapeConstraint, SameNumberOfRows, SameNumberOfColumns, AreMultipliable};
|
2017-08-03 01:37:44 +08:00
|
|
|
|
use core::storage::Storage;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
use core::allocator::{SameShapeAllocator, Allocator};
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
use geometry::Point;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* Indexing.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar, D: DimName> Index<usize> for Point<N, D>
|
|
|
|
|
where DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
type Output = N;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index(&self, i: usize) -> &Self::Output {
|
|
|
|
|
&self.coords[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar, D: DimName> IndexMut<usize> for Point<N, D>
|
|
|
|
|
where DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
|
|
|
|
|
&mut self.coords[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2017-08-03 01:37:44 +08:00
|
|
|
|
*
|
2016-12-05 05:44:42 +08:00
|
|
|
|
* Neg.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar + ClosedNeg, D: DimName> Neg for Point<N, D>
|
|
|
|
|
where DefaultAllocator: Allocator<N, D> {
|
|
|
|
|
type Output = Point<N, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn neg(self) -> Self::Output {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
Point::from_coordinates(-self.coords)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<'a, N: Scalar + ClosedNeg, D: DimName> Neg for &'a Point<N, D>
|
|
|
|
|
where DefaultAllocator: Allocator<N, D> {
|
|
|
|
|
type Output = Point<N, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn neg(self) -> Self::Output {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
Point::from_coordinates(-&self.coords)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* Subtraction & Addition.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
// Point - Point
|
2016-12-05 05:44:42 +08:00
|
|
|
|
add_sub_impl!(Sub, sub, ClosedSub;
|
|
|
|
|
(D, U1), (D, U1) for D: DimName;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self: &'a Point<N, D>, right: &'b Point<N, D>, Output = VectorSum<N, D, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
&self.coords - &right.coords; 'a, 'b);
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Sub, sub, ClosedSub;
|
|
|
|
|
(D, U1), (D, U1) for D: DimName;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self: &'a Point<N, D>, right: Point<N, D>, Output = VectorSum<N, D, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
&self.coords - right.coords; 'a);
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Sub, sub, ClosedSub;
|
|
|
|
|
(D, U1), (D, U1) for D: DimName;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self: Point<N, D>, right: &'b Point<N, D>, Output = VectorSum<N, D, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
self.coords - &right.coords; 'b);
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Sub, sub, ClosedSub;
|
|
|
|
|
(D, U1), (D, U1) for D: DimName;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self: Point<N, D>, right: Point<N, D>, Output = VectorSum<N, D, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
self.coords - right.coords; );
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
// Point - Vector
|
2016-12-05 05:44:42 +08:00
|
|
|
|
add_sub_impl!(Sub, sub, ClosedSub;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
|
|
|
|
|
self: &'a Point<N, D1>, right: &'b Vector<N, D2, SB>, Output = Point<N, D1>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::Output::from_coordinates(&self.coords - right); 'a, 'b);
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Sub, sub, ClosedSub;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
|
|
|
|
|
self: &'a Point<N, D1>, right: Vector<N, D2, SB>, Output = Point<N, D1>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::Output::from_coordinates(&self.coords - &right); 'a); // FIXME: should not be a ref to `right`.
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Sub, sub, ClosedSub;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
|
|
|
|
|
self: Point<N, D1>, right: &'b Vector<N, D2, SB>, Output = Point<N, D1>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::Output::from_coordinates(self.coords - right); 'b);
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Sub, sub, ClosedSub;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
|
|
|
|
|
self: Point<N, D1>, right: Vector<N, D2, SB>, Output = Point<N, D1>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::Output::from_coordinates(self.coords - right); );
|
|
|
|
|
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
// Point + Vector
|
2016-12-05 05:44:42 +08:00
|
|
|
|
add_sub_impl!(Add, add, ClosedAdd;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
|
|
|
|
|
self: &'a Point<N, D1>, right: &'b Vector<N, D2, SB>, Output = Point<N, D1>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::Output::from_coordinates(&self.coords + right); 'a, 'b);
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Add, add, ClosedAdd;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
|
|
|
|
|
self: &'a Point<N, D1>, right: Vector<N, D2, SB>, Output = Point<N, D1>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::Output::from_coordinates(&self.coords + &right); 'a); // FIXME: should not be a ref to `right`.
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Add, add, ClosedAdd;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
|
|
|
|
|
self: Point<N, D1>, right: &'b Vector<N, D2, SB>, Output = Point<N, D1>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::Output::from_coordinates(self.coords + right); 'b);
|
|
|
|
|
|
|
|
|
|
add_sub_impl!(Add, add, ClosedAdd;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
|
|
|
|
|
self: Point<N, D1>, right: Vector<N, D2, SB>, Output = Point<N, D1>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
Self::Output::from_coordinates(self.coords + right); );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// XXX: replace by the shared macro: add_sub_assign_impl
|
|
|
|
|
macro_rules! op_assign_impl(
|
|
|
|
|
($($TraitAssign: ident, $method_assign: ident, $bound: ident);* $(;)*) => {$(
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<'b, N, D1: DimName, D2: Dim, SB> $TraitAssign<&'b Vector<N, D2, SB>> for Point<N, D1>
|
|
|
|
|
where N: Scalar + $bound,
|
|
|
|
|
SB: Storage<N, D2>,
|
|
|
|
|
DefaultAllocator: Allocator<N, D1>,
|
2016-12-05 05:44:42 +08:00
|
|
|
|
ShapeConstraint: SameNumberOfRows<D1, D2> {
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
fn $method_assign(&mut self, right: &'b Vector<N, D2, SB>) {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
self.coords.$method_assign(right)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N, D1: DimName, D2: Dim, SB> $TraitAssign<Vector<N, D2, SB>> for Point<N, D1>
|
|
|
|
|
where N: Scalar + $bound,
|
|
|
|
|
SB: Storage<N, D2>,
|
|
|
|
|
DefaultAllocator: Allocator<N, D1>,
|
2016-12-05 05:44:42 +08:00
|
|
|
|
ShapeConstraint: SameNumberOfRows<D1, D2> {
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
fn $method_assign(&mut self, right: Vector<N, D2, SB>) {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
self.coords.$method_assign(right)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)*}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
op_assign_impl!(
|
|
|
|
|
AddAssign, add_assign, ClosedAdd;
|
|
|
|
|
SubAssign, sub_assign, ClosedSub;
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
2017-08-03 01:37:44 +08:00
|
|
|
|
* Matrix × Point
|
2016-12-05 05:44:42 +08:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
md_impl_all!(
|
|
|
|
|
Mul, mul;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
(R1, C1), (D2, U1) for R1: DimName, C1: Dim, D2: DimName, SA: Storage<N, R1, C1>
|
2016-12-05 05:44:42 +08:00
|
|
|
|
where ShapeConstraint: AreMultipliable<R1, C1, D2, U1>;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self: Matrix<N, R1, C1, SA>, right: Point<N, D2>, Output = Point<N, R1>;
|
|
|
|
|
[val val] => Point::from_coordinates(self * right.coords);
|
|
|
|
|
[ref val] => Point::from_coordinates(self * right.coords);
|
|
|
|
|
[val ref] => Point::from_coordinates(self * &right.coords);
|
|
|
|
|
[ref ref] => Point::from_coordinates(self * &right.coords);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
2017-08-03 01:37:44 +08:00
|
|
|
|
* Point ×/÷ Scalar
|
2016-12-05 05:44:42 +08:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
macro_rules! componentwise_scalarop_impl(
|
|
|
|
|
($Trait: ident, $method: ident, $bound: ident;
|
|
|
|
|
$TraitAssign: ident, $method_assign: ident) => {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar + $bound, D: DimName> $Trait<N> for Point<N, D>
|
|
|
|
|
where DefaultAllocator: Allocator<N, D> {
|
|
|
|
|
type Output = Point<N, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn $method(self, right: N) -> Self::Output {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
Point::from_coordinates(self.coords.$method(right))
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<'a, N: Scalar + $bound, D: DimName> $Trait<N> for &'a Point<N, D>
|
|
|
|
|
where DefaultAllocator: Allocator<N, D> {
|
|
|
|
|
type Output = Point<N, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn $method(self, right: N) -> Self::Output {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
Point::from_coordinates((&self.coords).$method(right))
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<N: Scalar + $bound, D: DimName> $TraitAssign<N> for Point<N, D>
|
|
|
|
|
where DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn $method_assign(&mut self, right: N) {
|
|
|
|
|
self.coords.$method_assign(right)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
componentwise_scalarop_impl!(Mul, mul, ClosedMul; MulAssign, mul_assign);
|
|
|
|
|
componentwise_scalarop_impl!(Div, div, ClosedDiv; DivAssign, div_assign);
|
|
|
|
|
|
|
|
|
|
macro_rules! left_scalar_mul_impl(
|
|
|
|
|
($($T: ty),* $(,)*) => {$(
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<D: DimName> Mul<Point<$T, D>> for $T
|
|
|
|
|
where DefaultAllocator: Allocator<$T, D> {
|
|
|
|
|
type Output = Point<$T, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
fn mul(self, right: Point<$T, D>) -> Self::Output {
|
|
|
|
|
Point::from_coordinates(self * right.coords)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
impl<'b, D: DimName> Mul<&'b Point<$T, D>> for $T
|
|
|
|
|
where DefaultAllocator: Allocator<$T, D> {
|
|
|
|
|
type Output = Point<$T, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
fn mul(self, right: &'b Point<$T, D>) -> Self::Output {
|
|
|
|
|
Point::from_coordinates(self * &right.coords)
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)*}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
left_scalar_mul_impl!(
|
|
|
|
|
u8, u16, u32, u64, usize,
|
|
|
|
|
i8, i16, i32, i64, isize,
|
|
|
|
|
f32, f64
|
|
|
|
|
);
|