Skip to content
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
merged 1 commit into from Feb 10, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -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());

This comment has been minimized.

@kvark

kvark Feb 10, 2017

Member

I wonder how this is passing? considering the tests are in a separate module, they shouldn't see is_identity()

This comment has been minimized.

@staktrace

staktrace Feb 10, 2017

Author Contributor

I think the test submodule is allowed to access private functions? If I move the exact same test into another module it fails as expected.

This comment has been minimized.

@kvark

kvark Feb 10, 2017

Member

that's good news! :)

let m2 = m1.post_translated(0.1, 0.0);
assert!(!m2.is_identity());
}
}
@@ -85,6 +85,30 @@ impl<T, Src, Dst> TypedMatrix4D<T, Src, Dst> {
}
}

impl <T, Src, Dst> TypedMatrix4D<T, Src, Dst>
where T: Copy + Clone +
PartialEq +
One + Zero {
#[inline]
pub fn identity() -> TypedMatrix4D<T, Src, Dst> {
let (_0, _1): (T, T) = (Zero::zero(), One::one());
TypedMatrix4D::row_major(
_1, _0, _0, _0,
_0, _1, _0, _0,
_0, _0, _1, _0,
_0, _0, _0, _1
)
}

// 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.
#[inline]
fn is_identity(&self) -> bool {
*self == TypedMatrix4D::identity()
}
}

impl <T, Src, Dst> TypedMatrix4D<T, Src, Dst>
where T: Copy + Clone +
Add<T, Output=T> +
@@ -128,17 +152,6 @@ where T: Copy + Clone +
)
}

#[inline]
pub fn identity() -> TypedMatrix4D<T, Src, Dst> {
let (_0, _1): (T, T) = (Zero::zero(), One::one());
TypedMatrix4D::row_major(
_1, _0, _0, _0,
_0, _1, _0, _0,
_0, _0, _1, _0,
_0, _0, _0, _1
)
}

/// Returns true if this matrix can be represented with a TypedMatrix2D.
///
/// See https://drafts.csswg.org/css-transforms/#2d-matrix
@@ -579,9 +592,16 @@ impl<T: Copy, Src, Dst> TypedMatrix4D<T, Src, Dst> {
}
}

impl<T: Copy + fmt::Debug, Src, Dst> fmt::Debug for TypedMatrix4D<T, Src, Dst> {
impl<T, Src, Dst> fmt::Debug for TypedMatrix4D<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)
}
}
}

@@ -794,4 +814,12 @@ mod tests {
let p2 = m2.transform_point4d(&m1.transform_point4d(&p));
assert!(p1.approx_eq(&p2));
}

#[test]
pub fn test_is_identity() {
let m1 = Matrix4D::identity();
assert!(m1.is_identity());
let m2 = m1.post_translated(0.1, 0.0, 0.0);
assert!(!m2.is_identity());
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.