2015-01-08 04:11:09 +08:00
|
|
|
#![macro_use]
|
2014-10-10 17:23:52 +08:00
|
|
|
|
2016-07-24 02:57:28 +08:00
|
|
|
macro_rules! point_impl(
|
|
|
|
($t: ident, $tv: ident | $($compN: ident),+) => (
|
|
|
|
|
2016-08-21 05:13:53 +08:00
|
|
|
euclidean_space_impl!($t, $tv);
|
|
|
|
|
2016-07-24 02:57:28 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Origin.
|
|
|
|
*
|
|
|
|
*/
|
2016-04-17 23:26:58 +08:00
|
|
|
impl<N: Zero> Origin for $t<N> {
|
2014-10-10 17:23:52 +08:00
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn origin() -> $t<N> {
|
2014-10-10 17:23:52 +08:00
|
|
|
$t {
|
2015-01-10 04:55:15 +08:00
|
|
|
$($compN: ::zero() ),+
|
2014-10-10 17:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn is_origin(&self) -> bool {
|
2015-01-10 04:55:15 +08:00
|
|
|
$(self.$compN.is_zero() )&&+
|
2014-10-10 17:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 02:57:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Point - Point
|
|
|
|
*
|
|
|
|
*/
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Copy + Sub<N, Output = N>> Sub<$t<N>> for $t<N> {
|
|
|
|
type Output = $tv<N>;
|
|
|
|
|
2014-10-10 17:23:52 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn sub(self, right: $t<N>) -> $tv<N> {
|
2016-04-17 23:26:58 +08:00
|
|
|
*self.as_vector() - *right.as_vector()
|
2014-10-10 17:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 02:57:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Point + Vector
|
|
|
|
*
|
|
|
|
*/
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Copy + Add<N, Output = N>> Add<$tv<N>> for $t<N> {
|
|
|
|
type Output = $t<N>;
|
|
|
|
|
2014-10-10 17:23:52 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn add(self, right: $tv<N>) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(self.$compN + right.$compN),+)
|
2014-10-10 17:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 18:57:54 +08:00
|
|
|
|
|
|
|
impl<N: Copy + AddAssign<N>> AddAssign<$tv<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn add_assign(&mut self, right: $tv<N>) {
|
|
|
|
$( self.$compN += right.$compN; )+
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 17:23:52 +08:00
|
|
|
|
2016-07-24 02:57:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Point - Vector
|
|
|
|
*
|
|
|
|
*/
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Copy + Sub<N, Output = N>> Sub<$tv<N>> for $t<N> {
|
|
|
|
type Output = $t<N>;
|
|
|
|
|
2014-10-10 17:23:52 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn sub(self, right: $tv<N>) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(self.$compN - right.$compN),+)
|
2014-10-10 17:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 18:57:54 +08:00
|
|
|
|
|
|
|
impl<N: Copy + SubAssign<N>> SubAssign<$tv<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn sub_assign(&mut self, right: $tv<N>) {
|
|
|
|
$( self.$compN -= right.$compN; )+
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 17:23:52 +08:00
|
|
|
|
2016-07-24 02:57:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Point as vector.
|
|
|
|
*
|
|
|
|
*/
|
2014-10-10 17:23:52 +08:00
|
|
|
impl<N> $t<N> {
|
2014-10-12 16:35:56 +08:00
|
|
|
/// Converts this point to its associated vector.
|
2014-10-10 18:19:37 +08:00
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
pub fn to_vector(self) -> $tv<N> {
|
2014-10-10 18:19:37 +08:00
|
|
|
$tv::new(
|
2015-01-10 04:55:15 +08:00
|
|
|
$(self.$compN),+
|
2014-10-10 18:19:37 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-10-12 16:35:56 +08:00
|
|
|
/// Converts a reference to this point to a reference to its associated vector.
|
2014-10-10 17:23:52 +08:00
|
|
|
#[inline]
|
2016-05-06 21:08:06 +08:00
|
|
|
pub fn as_vector(&self) -> &$tv<N> {
|
2014-10-10 17:23:52 +08:00
|
|
|
unsafe {
|
|
|
|
mem::transmute(self)
|
|
|
|
}
|
|
|
|
}
|
2014-10-11 02:56:40 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_coords(&mut self, v: $tv<N>) {
|
2015-01-10 04:55:15 +08:00
|
|
|
$(self.$compN = v.$compN;)+
|
2014-10-11 02:56:40 +08:00
|
|
|
}
|
2014-10-10 17:23:52 +08:00
|
|
|
}
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
impl<N> PointAsVector for $t<N> {
|
|
|
|
type Vector = $tv<N>;
|
2016-04-17 15:23:37 +08:00
|
|
|
|
2014-10-10 18:19:37 +08:00
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn to_vector(self) -> $tv<N> {
|
|
|
|
self.to_vector()
|
2014-10-10 18:19:37 +08:00
|
|
|
}
|
|
|
|
|
2014-10-10 17:23:52 +08:00
|
|
|
#[inline]
|
2016-05-06 21:08:06 +08:00
|
|
|
fn as_vector(&self) -> &$tv<N> {
|
2016-04-17 23:26:58 +08:00
|
|
|
self.as_vector()
|
2014-10-10 17:23:52 +08:00
|
|
|
}
|
2014-10-11 02:56:40 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_coords(&mut self, v: $tv<N>) {
|
|
|
|
self.set_coords(v)
|
|
|
|
}
|
2014-10-10 17:23:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 02:57:28 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* NumPoint / FloatPoint
|
|
|
|
*
|
|
|
|
*/
|
2016-04-17 23:26:58 +08:00
|
|
|
impl<N> NumPoint<N> for $t<N>
|
2014-11-26 21:17:34 +08:00
|
|
|
where N: BaseNum {
|
2014-10-26 22:04:47 +08:00
|
|
|
}
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
impl<N> FloatPoint<N> for $t<N>
|
2014-11-26 21:17:34 +08:00
|
|
|
where N: BaseFloat + ApproxEq<N> {
|
2014-10-26 22:04:47 +08:00
|
|
|
}
|
2015-01-10 08:36:13 +08:00
|
|
|
|
2016-03-28 20:56:25 +08:00
|
|
|
|
2016-07-24 02:57:28 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Display
|
|
|
|
*
|
|
|
|
*/
|
2016-03-28 20:56:25 +08:00
|
|
|
impl<N: fmt::Display> fmt::Display for $t<N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// FIXME: differenciate them from vectors ?
|
|
|
|
try!(write!(f, "("));
|
|
|
|
|
|
|
|
let mut it = self.iter();
|
|
|
|
|
|
|
|
try!(write!(f, "{}", *it.next().unwrap()));
|
|
|
|
|
|
|
|
for comp in it {
|
|
|
|
try!(write!(f, ", {}", *comp));
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
|
|
|
}
|
2016-07-24 02:57:28 +08:00
|
|
|
);
|
|
|
|
($t: ident, $tv: ident, $th: ident, $comp_extra: ident | $($compN: ident),+) => (
|
|
|
|
point_impl!($t, $tv | $($compN),+);
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* ToHomogeneous / FromHomogeneous
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
impl<N: Copy + One + Zero> ToHomogeneous<$th<N>> for $t<N> {
|
|
|
|
fn to_homogeneous(&self) -> $th<N> {
|
|
|
|
let mut res: $th<N> = Origin::origin();
|
|
|
|
|
|
|
|
$( res.$compN = self.$compN; )+
|
|
|
|
res.$comp_extra = ::one();
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: Copy + Div<N, Output = N> + One + Zero> FromHomogeneous<$th<N>> for $t<N> {
|
|
|
|
fn from(v: &$th<N>) -> $t<N> {
|
|
|
|
let mut res: $t<N> = Origin::origin();
|
|
|
|
|
|
|
|
$( res.$compN = v.$compN / v.$comp_extra; )+
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
2016-03-28 20:56:25 +08:00
|
|
|
)
|
|
|
|
);
|