2013-06-29 08:34:45 +08:00
|
|
|
use std::num::{Zero, One};
|
|
|
|
use mat::{Mat1, Mat2, Mat3};
|
|
|
|
use traits::division_ring::DivisionRing;
|
|
|
|
use traits::inv::Inv;
|
|
|
|
|
|
|
|
// some specializations:
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: DivisionRing + Clone>
|
2013-06-29 08:34:45 +08:00
|
|
|
Inv for Mat1<N>
|
|
|
|
{
|
|
|
|
#[inline]
|
2013-07-04 22:23:08 +08:00
|
|
|
fn inverse(&self) -> Option<Mat1<N>>
|
2013-06-29 08:34:45 +08:00
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
let mut res : Mat1<N> = self.clone();
|
2013-06-29 08:34:45 +08:00
|
|
|
|
2013-07-13 21:34:41 +08:00
|
|
|
if res.inplace_inverse()
|
2013-07-04 22:23:08 +08:00
|
|
|
{ Some(res) }
|
|
|
|
else
|
|
|
|
{ None }
|
2013-06-29 08:34:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-07-13 21:34:41 +08:00
|
|
|
fn inplace_inverse(&mut self) -> bool
|
2013-06-29 08:34:45 +08:00
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
if self.m11.is_zero()
|
2013-07-04 22:23:08 +08:00
|
|
|
{ false }
|
|
|
|
else
|
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
self.m11 = One::one::<N>() / self.m11;
|
2013-07-04 22:23:08 +08:00
|
|
|
true
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: DivisionRing + Clone>
|
2013-06-29 08:34:45 +08:00
|
|
|
Inv for Mat2<N>
|
|
|
|
{
|
|
|
|
#[inline]
|
2013-07-04 22:23:08 +08:00
|
|
|
fn inverse(&self) -> Option<Mat2<N>>
|
2013-06-29 08:34:45 +08:00
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
let mut res : Mat2<N> = self.clone();
|
2013-06-29 08:34:45 +08:00
|
|
|
|
2013-07-13 21:34:41 +08:00
|
|
|
if res.inplace_inverse()
|
2013-07-04 22:23:08 +08:00
|
|
|
{ Some(res) }
|
|
|
|
else
|
|
|
|
{ None }
|
2013-06-29 08:34:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-07-13 21:34:41 +08:00
|
|
|
fn inplace_inverse(&mut self) -> bool
|
2013-06-29 08:34:45 +08:00
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
let det = self.m11 * self.m22 - self.m21 * self.m12;
|
2013-06-29 08:34:45 +08:00
|
|
|
|
2013-07-04 22:23:08 +08:00
|
|
|
if det.is_zero()
|
|
|
|
{ false }
|
|
|
|
else
|
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
*self = Mat2::new(self.m22 / det , -self.m12 / det,
|
|
|
|
-self.m21 / det, self.m11 / det);
|
2013-06-29 08:34:45 +08:00
|
|
|
|
2013-07-04 22:23:08 +08:00
|
|
|
true
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: DivisionRing + Clone>
|
2013-06-29 08:34:45 +08:00
|
|
|
Inv for Mat3<N>
|
|
|
|
{
|
|
|
|
#[inline]
|
2013-07-04 22:23:08 +08:00
|
|
|
fn inverse(&self) -> Option<Mat3<N>>
|
2013-06-29 08:34:45 +08:00
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
let mut res = self.clone();
|
2013-06-29 08:34:45 +08:00
|
|
|
|
2013-07-13 21:34:41 +08:00
|
|
|
if res.inplace_inverse()
|
2013-07-04 22:23:08 +08:00
|
|
|
{ Some(res) }
|
|
|
|
else
|
|
|
|
{ None }
|
2013-06-29 08:34:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-07-13 21:34:41 +08:00
|
|
|
fn inplace_inverse(&mut self) -> bool
|
2013-06-29 08:34:45 +08:00
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
let minor_m12_m23 = self.m22 * self.m33 - self.m32 * self.m23;
|
|
|
|
let minor_m11_m23 = self.m21 * self.m33 - self.m31 * self.m23;
|
|
|
|
let minor_m11_m22 = self.m21 * self.m32 - self.m31 * self.m22;
|
2013-06-29 08:34:45 +08:00
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
let det = self.m11 * minor_m12_m23
|
|
|
|
- self.m12 * minor_m11_m23
|
|
|
|
+ self.m13 * minor_m11_m22;
|
2013-06-29 08:34:45 +08:00
|
|
|
|
2013-07-04 22:23:08 +08:00
|
|
|
if det.is_zero()
|
|
|
|
{ false }
|
|
|
|
else
|
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
*self = Mat3::new(
|
2013-07-04 22:23:08 +08:00
|
|
|
(minor_m12_m23 / det),
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
((self.m13 * self.m32 - self.m33 * self.m12) / det),
|
|
|
|
((self.m12 * self.m23 - self.m22 * self.m13) / det),
|
2013-07-04 22:23:08 +08:00
|
|
|
|
|
|
|
(-minor_m11_m23 / det),
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
((self.m11 * self.m33 - self.m31 * self.m13) / det),
|
|
|
|
((self.m13 * self.m21 - self.m23 * self.m11) / det),
|
2013-07-04 22:23:08 +08:00
|
|
|
|
|
|
|
(minor_m11_m22 / det),
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
((self.m12 * self.m31 - self.m32 * self.m11) / det),
|
|
|
|
((self.m11 * self.m22 - self.m21 * self.m12) / det)
|
|
|
|
);
|
2013-07-04 22:23:08 +08:00
|
|
|
|
|
|
|
true
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
}
|
|
|
|
}
|