nalgebra/src/structs/spec/mat.rs

266 lines
7.5 KiB
Rust
Raw Normal View History

use std::num::{Zero, One};
use structs::vec::{Vec2, Vec3, Vec2MulRhs, Vec3MulRhs};
use structs::mat::{Mat1, Mat2, Mat3, Mat3MulRhs, Mat2MulRhs};
use traits::operations::Inv;
use traits::structure::{Row, Col};
2013-06-29 08:34:45 +08:00
// some specializations:
impl<N: Num + Clone>
2013-08-05 16:13:44 +08:00
Inv for Mat1<N> {
2013-08-05 16:15:11 +08:00
#[inline]
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
fn inv_cpy(m: &Mat1<N>) -> Option<Mat1<N>> {
let mut res = m.clone();
2013-08-05 16:15:11 +08:00
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
if res.inv() {
2013-08-05 16:15:11 +08:00
Some(res)
}
else {
None
}
2013-08-05 16:13:44 +08:00
}
2013-06-29 08:34:45 +08:00
2013-08-05 16:15:11 +08:00
#[inline]
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
fn inv(&mut self) -> bool {
2013-08-05 16:15:11 +08:00
if self.m11.is_zero() {
false
}
else {
let _1: N = One::one();
self.m11 = _1 / self.m11;
2013-08-05 16:15:11 +08:00
true
}
}
2013-06-29 08:34:45 +08:00
}
impl<N: Num + Clone>
2013-08-05 16:13:44 +08:00
Inv for Mat2<N> {
2013-08-05 16:15:11 +08:00
#[inline]
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
fn inv_cpy(m: &Mat2<N>) -> Option<Mat2<N>> {
let mut res = m.clone();
2013-08-05 16:15:11 +08:00
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
if res.inv() {
2013-08-05 16:15:11 +08:00
Some(res)
}
else {
None
}
2013-08-05 16:13:44 +08:00
}
2013-06-29 08:34:45 +08:00
2013-08-05 16:15:11 +08:00
#[inline]
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
fn inv(&mut self) -> bool {
2013-08-05 16:15:11 +08:00
let det = self.m11 * self.m22 - self.m21 * self.m12;
2013-06-29 08:34:45 +08:00
2013-08-05 16:15:11 +08:00
if det.is_zero() {
false
}
else {
2013-08-17 23:50:01 +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-08-05 16:15:11 +08:00
true
}
}
2013-06-29 08:34:45 +08:00
}
impl<N: Num + Clone>
2013-08-05 16:13:44 +08:00
Inv for Mat3<N> {
2013-08-05 16:15:11 +08:00
#[inline]
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
fn inv_cpy(m: &Mat3<N>) -> Option<Mat3<N>> {
let mut res = m.clone();
2013-08-05 16:15:11 +08:00
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
if res.inv() {
2013-08-05 16:15:11 +08:00
Some(res)
}
else {
None
}
2013-08-05 16:13:44 +08:00
}
2013-06-29 08:34:45 +08:00
2013-08-05 16:15:11 +08:00
#[inline]
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
fn inv(&mut self) -> bool {
2013-08-05 16:15:11 +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;
let det = self.m11 * minor_m12_m23
- self.m12 * minor_m11_m23
+ self.m13 * minor_m11_m22;
if det.is_zero() {
false
}
else {
*self = Mat3::new(
2013-08-08 02:53:51 +08:00
(minor_m12_m23 / det),
2013-08-05 16:15:11 +08:00
((self.m13 * self.m32 - self.m33 * self.m12) / det),
((self.m12 * self.m23 - self.m22 * self.m13) / det),
(-minor_m11_m23 / det),
((self.m11 * self.m33 - self.m31 * self.m13) / det),
((self.m13 * self.m21 - self.m23 * self.m11) / det),
(minor_m11_m22 / det),
((self.m12 * self.m31 - self.m32 * self.m11) / det),
((self.m11 * self.m22 - self.m21 * self.m12) / det)
);
true
}
}
2013-06-29 08:34:45 +08:00
}
impl<N: Clone> Row<Vec3<N>> for Mat3<N> {
#[inline]
fn nrows(&self) -> uint {
3
}
#[inline]
fn row(&self, i: uint) -> Vec3<N> {
match i {
0 => Vec3::new(self.m11.clone(), self.m12.clone(), self.m13.clone()),
1 => Vec3::new(self.m21.clone(), self.m22.clone(), self.m23.clone()),
2 => Vec3::new(self.m31.clone(), self.m32.clone(), self.m33.clone()),
2014-05-31 05:14:16 +08:00
_ => fail!("Index out of range: 3d matrices do not have {} rows.", i.to_str())
}
}
#[inline]
fn set_row(&mut self, i: uint, r: Vec3<N>) {
match i {
0 => {
self.m11 = r.x.clone();
self.m12 = r.y.clone();
self.m13 = r.z;
},
1 => {
self.m21 = r.x.clone();
self.m22 = r.y.clone();
self.m23 = r.z;
},
2 => {
self.m31 = r.x.clone();
self.m32 = r.y.clone();
self.m33 = r.z;
},
2014-05-31 05:14:16 +08:00
_ => fail!("Index out of range: 3d matrices do not have {} rows.", i.to_str())
}
}
}
impl<N: Clone> Col<Vec3<N>> for Mat3<N> {
#[inline]
fn ncols(&self) -> uint {
3
}
#[inline]
fn col(&self, i: uint) -> Vec3<N> {
match i {
0 => Vec3::new(self.m11.clone(), self.m21.clone(), self.m31.clone()),
1 => Vec3::new(self.m12.clone(), self.m22.clone(), self.m32.clone()),
2 => Vec3::new(self.m13.clone(), self.m23.clone(), self.m33.clone()),
2014-05-31 05:14:16 +08:00
_ => fail!("Index out of range: 3d matrices do not have {} cols.", i.to_str() )
}
}
#[inline]
fn set_col(&mut self, i: uint, r: Vec3<N>) {
match i {
0 => {
self.m11 = r.x.clone();
self.m21 = r.y.clone();
self.m31 = r.z;
},
1 => {
self.m12 = r.x.clone();
self.m22 = r.y.clone();
self.m32 = r.z;
},
2 => {
self.m13 = r.x.clone();
self.m23 = r.y.clone();
self.m33 = r.z;
},
2014-05-31 05:14:16 +08:00
_ => fail!("Index out of range: 3d matrices do not have {} cols.", i.to_str() )
}
}
}
impl<N: Mul<N, N> + Add<N, N>> Mat3MulRhs<N, Mat3<N>> for Mat3<N> {
#[inline]
fn binop(left: &Mat3<N>, right: &Mat3<N>) -> Mat3<N> {
Mat3::new(
left.m11 * right.m11 + left.m12 * right.m21 + left.m13 * right.m31,
left.m11 * right.m12 + left.m12 * right.m22 + left.m13 * right.m32,
left.m11 * right.m13 + left.m12 * right.m23 + left.m13 * right.m33,
left.m21 * right.m11 + left.m22 * right.m21 + left.m23 * right.m31,
left.m21 * right.m12 + left.m22 * right.m22 + left.m23 * right.m32,
left.m21 * right.m13 + left.m22 * right.m23 + left.m23 * right.m33,
left.m31 * right.m11 + left.m32 * right.m21 + left.m33 * right.m31,
left.m31 * right.m12 + left.m32 * right.m22 + left.m33 * right.m32,
left.m31 * right.m13 + left.m32 * right.m23 + left.m33 * right.m33
)
}
}
impl<N: Mul<N, N> + Add<N, N>> Mat2MulRhs<N, Mat2<N>> for Mat2<N> {
#[inline(always)]
fn binop(left: &Mat2<N>, right: &Mat2<N>) -> Mat2<N> {
Mat2::new(
left.m11 * right.m11 + left.m12 * right.m21,
left.m11 * right.m12 + left.m12 * right.m22,
left.m21 * right.m11 + left.m22 * right.m21,
left.m21 * right.m12 + left.m22 * right.m22
)
}
}
impl<N: Mul<N, N> + Add<N, N>> Mat3MulRhs<N, Vec3<N>> for Vec3<N> {
#[inline(always)]
fn binop(left: &Mat3<N>, right: &Vec3<N>) -> Vec3<N> {
Vec3::new(
left.m11 * right.x + left.m12 * right.y + left.m13 * right.z,
left.m21 * right.x + left.m22 * right.y + left.m23 * right.z,
left.m31 * right.x + left.m32 * right.y + left.m33 * right.z
)
}
}
impl<N: Mul<N, N> + Add<N, N>> Vec3MulRhs<N, Vec3<N>> for Mat3<N> {
#[inline(always)]
fn binop(left: &Vec3<N>, right: &Mat3<N>) -> Vec3<N> {
Vec3::new(
left.x * right.m11 + left.y * right.m21 + left.z * right.m31,
left.x * right.m12 + left.y * right.m22 + left.z * right.m32,
left.x * right.m13 + left.y * right.m23 + left.z * right.m33
)
}
}
impl<N: Mul<N, N> + Add<N, N>> Vec2MulRhs<N, Vec2<N>> for Mat2<N> {
#[inline(always)]
fn binop(left: &Vec2<N>, right: &Mat2<N>) -> Vec2<N> {
Vec2::new(
left.x * right.m11 + left.y * right.m21,
left.x * right.m12 + left.y * right.m22
)
}
}
impl<N: Mul<N, N> + Add<N, N>> Mat2MulRhs<N, Vec2<N>> for Vec2<N> {
#[inline(always)]
fn binop(left: &Mat2<N>, right: &Vec2<N>) -> Vec2<N> {
Vec2::new(
2013-09-15 17:51:42 +08:00
left.m11 * right.x + left.m12 * right.y,
left.m21 * right.x + left.m22 * right.y
)
}
}