Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Unified Diff: ui/gfx/matrix3_unittest.cc

Issue 12096069: New Matrix3F class added in gfx. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: A typo fix. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« ui/gfx/matrix3_f.cc ('K') | « ui/gfx/matrix3_f.cc ('k') | ui/ui.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/matrix3_unittest.cc
diff --git a/ui/gfx/matrix3_unittest.cc b/ui/gfx/matrix3_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..98d3804c4245ccffa3499fb098209f8da83236b6
--- /dev/null
+++ b/ui/gfx/matrix3_unittest.cc
@@ -0,0 +1,146 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <cmath>
+#include <limits>
+
+#include "base/basictypes.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/gfx/matrix3_f.h"
+
+using gfx::Matrix3F;
danakj 2013/01/31 22:40:55 please put this file in namespace gfx { namespace
Alexei Svitkine (slow) 2013/01/31 22:45:05 You don't need the inner namespace. Just "namespac
danakj 2013/01/31 22:46:41 It's true, but this is the general practice used e
motek. 2013/01/31 23:29:56 Done.
+
+TEST(Matrix3fTest, Constructors) {
+ Matrix3F zeros = Matrix3F::Zeros();
+ Matrix3F ones = Matrix3F::Ones();
+ Matrix3F identity = Matrix3F::Identity();
+
+ Matrix3F product_ones = Matrix3F::FromOuterProduct(
+ Matrix3F::VectorType(1.0f, 1.0f, 1.0f),
+ Matrix3F::VectorType(1.0f, 1.0f, 1.0f));
+ Matrix3F product_zeros = Matrix3F::FromOuterProduct(
+ Matrix3F::VectorType(1.0f, 1.0f, 1.0f),
+ Matrix3F::VectorType(0.0f, 0.0f, 0.0f));
+ EXPECT_EQ(ones, product_ones);
+ EXPECT_EQ(zeros, product_zeros);
+
+ for (int i = 0; i < 3; ++i) {
+ for (int j = 0; j < 3; ++j)
+ EXPECT_EQ(i == j ? 1.0 : 0.0, identity.get(i, j));
+ }
+}
+
+TEST(Matrix3fTest, DataAccess) {
+ Matrix3F matrix = Matrix3F::Ones();
+ Matrix3F identity = Matrix3F::Identity();
+
+ EXPECT_EQ(Matrix3F::VectorType(0.0, 1.0, 0.0), identity.GetColumn(1));
+ matrix.set(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
+ EXPECT_EQ(Matrix3F::VectorType(2.0, 5.0, 8.0), matrix.GetColumn(2));
+ matrix.SetColumn(0, Matrix3F::VectorType(0.1, 0.2, 0.3));
+ EXPECT_EQ(Matrix3F::VectorType(0.1, 0.2, 0.3), matrix.GetColumn(0));
+
+ EXPECT_EQ(0.1f, matrix.get(0, 0));
+ EXPECT_EQ(5.0f, matrix.get(1, 2));
+}
+
+TEST(Matrix3fTest, Determinant) {
+ EXPECT_EQ(1.0, Matrix3F::Identity().Determinant());
+ EXPECT_EQ(0.0, Matrix3F::Zeros().Determinant());
+ EXPECT_EQ(0.0, Matrix3F::Ones().Determinant());
+
+ // Now for something non-trivial...
+ Matrix3F matrix = Matrix3F::Zeros();
+ matrix.set(0, 5, 6, 8, 7, 0, 1, 9, 0);
+ EXPECT_EQ(390.0, matrix.Determinant());
+ matrix.set(2, 0, 3 * matrix.get(0, 0));
+ matrix.set(2, 1, 3 * matrix.get(0, 1));
+ matrix.set(2, 2, 3 * matrix.get(0, 2));
+ EXPECT_EQ(0, matrix.Determinant());
+
+ matrix.set(0.57 , 0.205, 0.942,
+ 0.314, 0.845, 0.826,
+ 0.131, 0.025, 0.962);
+ EXPECT_NEAR(0.3149, matrix.Determinant(), 0.0001);
+}
+
+TEST(Matrix3fTest, Inverse) {
+ Matrix3F identity = Matrix3F::Identity();
+ Matrix3F inv_identity = identity.Inverse();
+ EXPECT_EQ(identity, inv_identity);
+
+ Matrix3F singular = Matrix3F::Zeros();
+ singular.set(1.0, 3.0, 4.0,
+ 2.0, 11.0, 5.0,
+ 0.5, 1.5, 2.0);
+ EXPECT_EQ(0, singular.Determinant());
+ EXPECT_EQ(Matrix3F::Zeros(), singular.Inverse());
+
+ Matrix3F regular = Matrix3F::Zeros();
+ regular.set(0.57 , 0.205, 0.942,
+ 0.314, 0.845, 0.826,
+ 0.131, 0.025, 0.962);
+ Matrix3F inv_regular = regular.Inverse();
+ regular.set(2.51540616, -0.55138018, -1.98968043,
+ -0.61552266, 1.34920184, -0.55573636,
+ -0.32653861, 0.04002158, 1.32488726);
+ EXPECT_TRUE(regular.IsNear(inv_regular, 0.0001));
+}
+
+TEST(Matrix3fTest, EigenvectorsIdentity) {
+ // This block tests the trivial case of eigenvalues of the identity matrix.
+ Matrix3F identity = Matrix3F::Identity();
+ Matrix3F::VectorType eigenvals = identity.SolveEigenproblem(NULL);
+ EXPECT_EQ(Matrix3F::VectorType(1.0, 1.0, 1.0), eigenvals);
+}
+
+TEST(Matrix3fTest, EigenvectorsDiagonal) {
+ // This block tests the another trivial case of eigenvalues of a diagonal
+ // matrix. Here we expect values to be sorted.
+ Matrix3F matrix = Matrix3F::Zeros();
+ matrix.set(0, 0, 1.0);
+ matrix.set(1, 1, -2.5);
+ matrix.set(2, 2, 3.14);
+ Matrix3F eigenvectors = Matrix3F::Zeros();
+ Matrix3F::VectorType eigenvals = matrix.SolveEigenproblem(&eigenvectors);
+ EXPECT_EQ(Matrix3F::VectorType(3.14, 1.0, -2.5), eigenvals);
+
+ EXPECT_EQ(Matrix3F::VectorType(0.0, 0.0, 1.0), eigenvectors.GetColumn(0));
+ EXPECT_EQ(Matrix3F::VectorType(1.0, 0.0, 0.0), eigenvectors.GetColumn(1));
+ EXPECT_EQ(Matrix3F::VectorType(0.0, 1.0, 0.0), eigenvectors.GetColumn(2));
+}
+
+TEST(Matrix3fTest, EigenvectorsNiceNotPositive) {
+ // This block tests computation of eigenvectors of a matrix where nice
+ // round values are expected.
+ Matrix3F matrix = Matrix3F::Zeros();
+ // This is not a positive-definite matrix but eigenvalues and the first
+ // eigenvector should nonetheless be computed correctly.
+ matrix.set(3, 2, 4, 2, 0, 2, 4, 2, 3);
+ Matrix3F eigenvectors = Matrix3F::Zeros();
+ Matrix3F::VectorType eigenvals = matrix.SolveEigenproblem(&eigenvectors);
+ EXPECT_EQ(Matrix3F::VectorType(8.0, -1.0, -1.0), eigenvals);
+
+ Matrix3F::VectorType expected_principal(0.66666667, 0.33333333, 0.66666667);
+ EXPECT_NEAR(0.0,
+ (expected_principal - eigenvectors.GetColumn(0)).Length(),
+ 0.000001);
+}
+
+TEST(Matrix3fTest, EigenvectorsPositiveDefinite) {
+ // This block tests computation of eigenvectors of a matrix where output
+ // is not as nice as above, but it actually meets the definition.
+ Matrix3F matrix = Matrix3F::Zeros();
+ Matrix3F eigenvectors = Matrix3F::Zeros();
+ Matrix3F expected_eigenvectors = Matrix3F::Zeros();
+ matrix.set(1, -1, 2, -1, 4, 5, 2, 5, 0);
+ Matrix3F::VectorType eigenvals = matrix.SolveEigenproblem(&eigenvectors);
+ Matrix3F::VectorType expected_eigv(7.3996266, 1.91197255, -4.31159915);
+ expected_eigv -= eigenvals;
+ EXPECT_NEAR(0.0, expected_eigv.LengthSquared(), 0.00001);
+ expected_eigenvectors.set(0.04926317, -0.92135662, -0.38558414,
+ 0.82134249, 0.25703273, -0.50924521,
+ 0.56830419, -0.2916096, 0.76941158);
+ EXPECT_TRUE(expected_eigenvectors.IsNear(eigenvectors, 0.00001));
+}
« ui/gfx/matrix3_f.cc ('K') | « ui/gfx/matrix3_f.cc ('k') | ui/ui.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698