diff options
author | shawnsingh@google.com <shawnsingh@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-07 18:46:17 +0000 |
---|---|---|
committer | shawnsingh@google.com <shawnsingh@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-07 18:46:17 +0000 |
commit | bda419621f3a609ac9381f03344574b297d2a3fc (patch) | |
tree | ec426b89d2fd0db9841add54580ff4bfcc50802c /ui/gfx | |
parent | 38eb497c29f6ba7ddf8b067f7b892bd7ed9e1423 (diff) | |
download | chromium_src-bda419621f3a609ac9381f03344574b297d2a3fc.zip chromium_src-bda419621f3a609ac9381f03344574b297d2a3fc.tar.gz chromium_src-bda419621f3a609ac9381f03344574b297d2a3fc.tar.bz2 |
Migrate from MathUtil::inverse() to gfx::Transform::GetInverse()
BUG=159972, 163769
Review URL: https://codereview.chromium.org/11644008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175363 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r-- | ui/gfx/transform.cc | 9 | ||||
-rw-r--r-- | ui/gfx/transform.h | 10 | ||||
-rw-r--r-- | ui/gfx/transform_unittest.cc | 57 |
3 files changed, 75 insertions, 1 deletions
diff --git a/ui/gfx/transform.cc b/ui/gfx/transform.cc index a4a4e64..c2b6ba2 100644 --- a/ui/gfx/transform.cc +++ b/ui/gfx/transform.cc @@ -245,7 +245,14 @@ bool Transform::IsBackFaceVisible() const { } bool Transform::GetInverse(Transform* transform) const { - return matrix_.invert(&transform->matrix_); + if (!matrix_.invert(&transform->matrix_)) { + // Initialize the return value to identity if this matrix turned + // out to be un-invertible. + transform->MakeIdentity(); + return false; + } + + return true; } void Transform::Transpose() { diff --git a/ui/gfx/transform.h b/ui/gfx/transform.h index 6cd10b1..95de30f 100644 --- a/ui/gfx/transform.h +++ b/ui/gfx/transform.h @@ -22,7 +22,17 @@ class Vector3dF; // copy/assign. class UI_EXPORT Transform { public: + + enum SkipInitialization { + kSkipInitialization + }; + Transform() : matrix_(SkMatrix44::kIdentity_Constructor) {} + + // Skips initializing this matrix to avoid overhead, when we know it will be + // initialized before use. + Transform(SkipInitialization) + : matrix_(SkMatrix44::kUninitialized_Constructor) {} Transform(const Transform& rhs) : matrix_(rhs.matrix_) {} // Initialize with the concatenation of lhs * rhs. Transform(const Transform& lhs, const Transform& rhs) diff --git a/ui/gfx/transform_unittest.cc b/ui/gfx/transform_unittest.cc index 31892c3..1984fea 100644 --- a/ui/gfx/transform_unittest.cc +++ b/ui/gfx/transform_unittest.cc @@ -1182,6 +1182,63 @@ TEST(XFormTest, IntegerTranslation) { EXPECT_FALSE(transform.IsIdentityOrIntegerTranslation()); } +TEST(XFormTest, verifyMatrixInversion) +{ + { + // Invert a translation + gfx::Transform translation; + translation.Translate3d(2, 3, 4); + EXPECT_TRUE(translation.IsInvertible()); + + gfx::Transform inverse_translation; + bool is_invertible = translation.GetInverse(&inverse_translation); + EXPECT_TRUE(is_invertible); + EXPECT_ROW1_EQ(1, 0, 0, -2, inverse_translation); + EXPECT_ROW2_EQ(0, 1, 0, -3, inverse_translation); + EXPECT_ROW3_EQ(0, 0, 1, -4, inverse_translation); + EXPECT_ROW4_EQ(0, 0, 0, 1, inverse_translation); + } + + { + // Invert a non-uniform scale + gfx::Transform scale; + scale.Scale3d(4, 10, 100); + EXPECT_TRUE(scale.IsInvertible()); + + gfx::Transform inverse_scale; + bool is_invertible = scale.GetInverse(&inverse_scale); + EXPECT_TRUE(is_invertible); + EXPECT_ROW1_EQ(0.25, 0, 0, 0, inverse_scale); + EXPECT_ROW2_EQ(0, .1f, 0, 0, inverse_scale); + EXPECT_ROW3_EQ(0, 0, .01f, 0, inverse_scale); + EXPECT_ROW4_EQ(0, 0, 0, 1, inverse_scale); + } + + { + // Try to invert a matrix that is not invertible. + // The inverse() function should reset the output matrix to identity. + gfx::Transform uninvertible; + uninvertible.matrix().setDouble(0, 0, 0); + uninvertible.matrix().setDouble(1, 1, 0); + uninvertible.matrix().setDouble(2, 2, 0); + uninvertible.matrix().setDouble(3, 3, 0); + EXPECT_FALSE(uninvertible.IsInvertible()); + + gfx::Transform inverse_of_uninvertible; + + // Add a scale just to more easily ensure that inverse_of_uninvertible is + // reset to identity. + inverse_of_uninvertible.Scale3d(4, 10, 100); + + bool is_invertible = uninvertible.GetInverse(&inverse_of_uninvertible); + EXPECT_FALSE(is_invertible); + EXPECT_TRUE(inverse_of_uninvertible.IsIdentity()); + EXPECT_ROW1_EQ(1, 0, 0, 0, inverse_of_uninvertible); + EXPECT_ROW2_EQ(0, 1, 0, 0, inverse_of_uninvertible); + EXPECT_ROW3_EQ(0, 0, 1, 0, inverse_of_uninvertible); + EXPECT_ROW4_EQ(0, 0, 0, 1, inverse_of_uninvertible); + } +} } // namespace |