2013-10-06 22:54:09 +08:00
|
|
|
#[macro_escape];
|
|
|
|
|
|
|
|
macro_rules! iso_impl(
|
2013-10-14 17:22:38 +08:00
|
|
|
($t: ident, $submat: ident, $subvec: ident, $subrotvec: ident) => (
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Clone + Real + Real + Num> $t<N> {
|
2013-10-06 22:54:09 +08:00
|
|
|
/// Creates a new isometry from a rotation matrix and a vector.
|
|
|
|
#[inline]
|
2013-10-14 17:22:38 +08:00
|
|
|
pub fn new(translation: $subvec<N>, rotation: $subrotvec<N>) -> $t<N> {
|
|
|
|
$t {
|
|
|
|
rotation: $submat::new(rotation),
|
|
|
|
translation: translation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new isometry from a rotation matrix and a vector.
|
|
|
|
#[inline]
|
|
|
|
pub fn new_with_rotmat(translation: $subvec<N>, rotation: $submat<N>) -> $t<N> {
|
2013-10-06 22:54:09 +08:00
|
|
|
$t {
|
|
|
|
rotation: rotation,
|
|
|
|
translation: translation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! rotation_matrix_impl(
|
|
|
|
($t: ident, $trot: ident, $tlv: ident, $tav: ident) => (
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Cast<f32> + Real + Real + Num + Clone>
|
2013-10-06 22:54:09 +08:00
|
|
|
RotationMatrix<$tlv<N>, $tav<N>, $trot<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn to_rot_mat(&self) -> $trot<N> {
|
|
|
|
self.rotation.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! dim_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
|
|
|
impl<N> Dim for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn dim(_: Option<$t<N>>) -> uint {
|
|
|
|
$dim
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! one_impl(
|
|
|
|
($t: ident) => (
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Real + Real + Num + Clone> One for $t<N> {
|
2013-10-06 22:54:09 +08:00
|
|
|
#[inline]
|
|
|
|
fn one() -> $t<N> {
|
2013-10-14 17:22:38 +08:00
|
|
|
$t::new_with_rotmat(Zero::zero(), One::one())
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! iso_mul_iso_impl(
|
|
|
|
($t: ident, $tmul: ident) => (
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Num + Real + Real + Clone> $tmul<N, $t<N>> for $t<N> {
|
2013-10-06 22:54:09 +08:00
|
|
|
#[inline]
|
|
|
|
fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> {
|
2013-10-14 17:22:38 +08:00
|
|
|
$t::new_with_rotmat(
|
|
|
|
left.translation + left.rotation * right.translation,
|
|
|
|
left.rotation * right.rotation)
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! iso_mul_vec_impl(
|
|
|
|
($t: ident, $tv: ident, $tmul: ident) => (
|
|
|
|
impl<N: Num + Clone> $tmul<N, $tv<N>> for $tv<N> {
|
|
|
|
#[inline]
|
|
|
|
fn binop(left: &$t<N>, right: &$tv<N>) -> $tv<N> {
|
|
|
|
left.translation + left.rotation * *right
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! vec_mul_iso_impl(
|
|
|
|
($t: ident, $tv: ident, $tmul: ident) => (
|
|
|
|
impl<N: Clone + Num> $tmul<N, $tv<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn binop(left: &$tv<N>, right: &$t<N>) -> $tv<N> {
|
|
|
|
(left + right.translation) * right.rotation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! translation_impl(
|
|
|
|
($t: ident, $tv: ident) => (
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Real + Num + Real + Clone> Translation<$tv<N>> for $t<N> {
|
2013-10-06 22:54:09 +08:00
|
|
|
#[inline]
|
|
|
|
fn translation(&self) -> $tv<N> {
|
|
|
|
self.translation.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inv_translation(&self) -> $tv<N> {
|
|
|
|
-self.translation
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-10-14 16:22:32 +08:00
|
|
|
fn append_translation(&mut self, t: &$tv<N>) {
|
|
|
|
self.translation = *t + self.translation
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-10-14 16:22:32 +08:00
|
|
|
fn append_translation_cpy(iso: &$t<N>, t: &$tv<N>) -> $t<N> {
|
2013-10-14 17:22:38 +08:00
|
|
|
$t::new_with_rotmat(*t + iso.translation, iso.rotation.clone())
|
2013-10-14 16:22:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn prepend_translation(&mut self, t: &$tv<N>) {
|
|
|
|
self.translation = self.translation + self.rotation * *t
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn prepend_translation_cpy(iso: &$t<N>, t: &$tv<N>) -> $t<N> {
|
2013-10-14 17:22:38 +08:00
|
|
|
$t::new_with_rotmat(iso.translation + iso.rotation * *t, iso.rotation.clone())
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_translation(&mut self, t: $tv<N>) {
|
|
|
|
self.translation = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! translate_impl(
|
|
|
|
($t: ident, $tv: ident) => (
|
|
|
|
impl<N: Clone + Add<N, N> + Sub<N, N>> Translate<$tv<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn translate(&self, v: &$tv<N>) -> $tv<N> {
|
|
|
|
v + self.translation
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inv_translate(&self, v: &$tv<N>) -> $tv<N> {
|
|
|
|
v - self.translation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! rotation_impl(
|
|
|
|
($t: ident, $trot: ident, $tav: ident) => (
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Cast<f32> + Num + Real + Real + Clone> Rotation<$tav<N>> for $t<N> {
|
2013-10-06 22:54:09 +08:00
|
|
|
#[inline]
|
|
|
|
fn rotation(&self) -> $tav<N> {
|
|
|
|
self.rotation.rotation()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inv_rotation(&self) -> $tav<N> {
|
|
|
|
self.rotation.inv_rotation()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-10-14 16:22:32 +08:00
|
|
|
fn append_rotation(&mut self, rot: &$tav<N>) {
|
|
|
|
let delta = $trot::new(rot.clone());
|
|
|
|
|
|
|
|
self.rotation = delta * self.rotation;
|
2013-10-06 22:54:09 +08:00
|
|
|
self.translation = delta * self.translation;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-10-14 16:22:32 +08:00
|
|
|
fn append_rotation_cpy(t: &$t<N>, rot: &$tav<N>) -> $t<N> {
|
|
|
|
let delta = $trot::new(rot.clone());
|
|
|
|
|
2013-10-14 17:22:38 +08:00
|
|
|
$t::new_with_rotmat(delta * t.translation, delta * t.rotation)
|
2013-10-14 16:22:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn prepend_rotation(&mut self, rot: &$tav<N>) {
|
|
|
|
let delta = $trot::new(rot.clone());
|
|
|
|
|
|
|
|
self.rotation = self.rotation * delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn prepend_rotation_cpy(t: &$t<N>, rot: &$tav<N>) -> $t<N> {
|
|
|
|
let delta = $trot::new(rot.clone());
|
2013-10-06 22:54:09 +08:00
|
|
|
|
2013-10-14 17:22:38 +08:00
|
|
|
$t::new_with_rotmat(t.translation.clone(), t.rotation * delta)
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_rotation(&mut self, rot: $tav<N>) {
|
|
|
|
// FIXME: should the translation be changed too?
|
|
|
|
self.rotation.set_rotation(rot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! rotate_impl(
|
|
|
|
($t: ident, $tv: ident) => (
|
|
|
|
impl<N: Num + Clone> Rotate<$tv<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn rotate(&self, v: &$tv<N>) -> $tv<N> {
|
|
|
|
self.rotation.rotate(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inv_rotate(&self, v: &$tv<N>) -> $tv<N> {
|
|
|
|
self.rotation.inv_rotate(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! transformation_impl(
|
|
|
|
($t: ident) => (
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Num + Real + Real + Clone> Transformation<$t<N>> for $t<N> {
|
2013-10-06 22:54:09 +08:00
|
|
|
fn transformation(&self) -> $t<N> {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inv_transformation(&self) -> $t<N> {
|
|
|
|
// inversion will never fails
|
2013-10-14 16:22:32 +08:00
|
|
|
Inv::inv_cpy(self).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_transformation(&mut self, t: &$t<N>) {
|
|
|
|
*self = *t * *self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_transformation_cpy(iso: &$t<N>, t: &$t<N>) -> $t<N> {
|
|
|
|
t * *iso
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
|
2013-10-14 16:22:32 +08:00
|
|
|
fn prepend_transformation(&mut self, t: &$t<N>) {
|
|
|
|
*self = *self * *t
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
|
2013-10-14 16:22:32 +08:00
|
|
|
fn prepend_transformation_cpy(iso: &$t<N>, t: &$t<N>) -> $t<N> {
|
|
|
|
*iso * *t
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_transformation(&mut self, t: $t<N>) {
|
|
|
|
*self = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! transform_impl(
|
|
|
|
($t: ident, $tv: ident) => (
|
|
|
|
impl<N: Num + Clone> Transform<$tv<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn transform(&self, v: &$tv<N>) -> $tv<N> {
|
|
|
|
self.rotation.transform(v) + self.translation
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inv_transform(&self, v: &$tv<N>) -> $tv<N> {
|
|
|
|
self.rotation.inv_transform(&(v - self.translation))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! inv_impl(
|
|
|
|
($t: ident) => (
|
|
|
|
impl<N: Clone + Num> Inv for $t<N> {
|
|
|
|
#[inline]
|
2013-10-14 16:22:32 +08:00
|
|
|
fn inv(&mut self) -> bool {
|
|
|
|
self.rotation.inv();
|
|
|
|
self.translation = self.rotation * -self.translation;
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
|
|
// always succeed
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-10-14 16:22:32 +08:00
|
|
|
fn inv_cpy(m: &$t<N>) -> Option<$t<N>> {
|
|
|
|
let mut res = m.clone();
|
2013-10-06 22:54:09 +08:00
|
|
|
|
2013-10-14 16:22:32 +08:00
|
|
|
res.inv();
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
|
|
// always succeed
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! to_homogeneous_impl(
|
|
|
|
($t: ident, $th: ident) => (
|
2014-01-21 06:40:32 +08:00
|
|
|
impl<N: Num + Clone> ToHomogeneous<$th<N>> for $t<N> {
|
2013-10-17 03:44:33 +08:00
|
|
|
fn to_homogeneous(m: &$t<N>) -> $th<N> {
|
|
|
|
let mut res = ToHomogeneous::to_homogeneous(&m.rotation);
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
|
|
// copy the translation
|
|
|
|
let dim = Dim::dim(None::<$th<N>>);
|
|
|
|
|
2013-10-17 03:44:33 +08:00
|
|
|
res.set_col(dim - 1, ToHomogeneous::to_homogeneous(&m.translation));
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! approx_eq_impl(
|
|
|
|
($t: ident) => (
|
|
|
|
impl<N: ApproxEq<N>> ApproxEq<N> for $t<N> {
|
|
|
|
#[inline]
|
2014-01-10 03:48:30 +08:00
|
|
|
fn approx_epsilon(_: Option<$t<N>>) -> N {
|
|
|
|
ApproxEq::approx_epsilon(None::<N>)
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-01-10 03:48:30 +08:00
|
|
|
fn approx_eq(a: &$t<N>, b: &$t<N>) -> bool {
|
|
|
|
ApproxEq::approx_eq(&a.rotation, &b.rotation) &&
|
|
|
|
ApproxEq::approx_eq(&a.translation, &b.translation)
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-01-10 03:48:30 +08:00
|
|
|
fn approx_eq_eps(a: &$t<N>, b: &$t<N>, epsilon: &N) -> bool {
|
|
|
|
ApproxEq::approx_eq_eps(&a.rotation, &b.rotation, epsilon) &&
|
|
|
|
ApproxEq::approx_eq_eps(&a.translation, &b.translation, epsilon)
|
2013-10-06 22:54:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! rand_impl(
|
|
|
|
($t: ident) => (
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Rand + Clone + Real + Real + Num> Rand for $t<N> {
|
2013-10-06 22:54:09 +08:00
|
|
|
#[inline]
|
|
|
|
fn rand<R: Rng>(rng: &mut R) -> $t<N> {
|
|
|
|
$t::new(rng.gen(), rng.gen())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! absolute_rotate_impl(
|
|
|
|
($t: ident, $tv: ident) => (
|
|
|
|
impl<N: Signed> AbsoluteRotate<$tv<N>> for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn absolute_rotate(&self, v: &$tv<N>) -> $tv<N> {
|
|
|
|
self.rotation.absolute_rotate(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|