New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an is_identity function to the Matrix classes, and special-case t… #169
Merged
+80
−25
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
| @@ -99,15 +99,9 @@ impl<T: Copy, Src, Dst> TypedMatrix2D<T, Src, Dst> { | ||
| } | ||
|
|
||
| impl<T, Src, Dst> TypedMatrix2D<T, Src, Dst> | ||
| where T: Copy + Clone + | ||
| Add<T, Output=T> + | ||
| Mul<T, Output=T> + | ||
| Div<T, Output=T> + | ||
| Sub<T, Output=T> + | ||
| Trig + | ||
| PartialOrd + | ||
| One + Zero { | ||
|
|
||
| where T: Copy + | ||
| PartialEq + | ||
| One + Zero { | ||
| pub fn identity() -> TypedMatrix2D<T, Src, Dst> { | ||
| let (_0, _1) = (Zero::zero(), One::one()); | ||
| TypedMatrix2D::row_major( | ||
| @@ -117,6 +111,24 @@ where T: Copy + Clone + | ||
| ) | ||
| } | ||
|
|
||
| // Intentional not public, because it checks for exact equivalence | ||
| // while most consumers will probably want some sort of approximate | ||
| // equivalence to deal with floating-point errors. | ||
| fn is_identity(&self) -> bool { | ||
| *self == TypedMatrix2D::identity() | ||
| } | ||
| } | ||
|
|
||
| impl<T, Src, Dst> TypedMatrix2D<T, Src, Dst> | ||
| where T: Copy + Clone + | ||
| Add<T, Output=T> + | ||
| Mul<T, Output=T> + | ||
| Div<T, Output=T> + | ||
| Sub<T, Output=T> + | ||
| Trig + | ||
| PartialOrd + | ||
| One + Zero { | ||
|
|
||
| /// Returns the multiplication of the two matrices such that mat's transformation | ||
| /// applies after self's transformation. | ||
| pub fn post_mul<NewDst>(&self, mat: &TypedMatrix2D<T, Dst, NewDst>) -> TypedMatrix2D<T, Src, NewDst> { | ||
| @@ -277,9 +289,16 @@ impl<T: ApproxEq<T>, Src, Dst> TypedMatrix2D<T, Src, Dst> { | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy + fmt::Debug, Src, Dst> fmt::Debug for TypedMatrix2D<T, Src, Dst> { | ||
| impl<T: Copy + fmt::Debug, Src, Dst> fmt::Debug for TypedMatrix2D<T, Src, Dst> | ||
| where T: Copy + fmt::Debug + | ||
| PartialEq + | ||
| One + Zero { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| self.to_row_major_array().fmt(f) | ||
| if self.is_identity() { | ||
| write!(f, "[I]") | ||
| } else { | ||
| self.to_row_major_array().fmt(f) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @@ -401,4 +420,12 @@ mod test { | ||
| assert_eq!(size_of::<Matrix2D<f32>>(), 6*size_of::<f32>()); | ||
| assert_eq!(size_of::<Matrix2D<f64>>(), 6*size_of::<f64>()); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| pub fn test_is_identity() { | ||
| let m1 = Matrix2D::identity(); | ||
| assert!(m1.is_identity()); | ||
staktrace
Author
Contributor
|
||
| let m2 = m1.post_translated(0.1, 0.0); | ||
| assert!(!m2.is_identity()); | ||
| } | ||
| } | ||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
I wonder how this is passing? considering the tests are in a separate module, they shouldn't see
is_identity()