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

Side by Side 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, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <cmath>
6 #include <limits>
7
8 #include "base/basictypes.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/matrix3_f.h"
11
12 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.
13
14 TEST(Matrix3fTest, Constructors) {
15 Matrix3F zeros = Matrix3F::Zeros();
16 Matrix3F ones = Matrix3F::Ones();
17 Matrix3F identity = Matrix3F::Identity();
18
19 Matrix3F product_ones = Matrix3F::FromOuterProduct(
20 Matrix3F::VectorType(1.0f, 1.0f, 1.0f),
21 Matrix3F::VectorType(1.0f, 1.0f, 1.0f));
22 Matrix3F product_zeros = Matrix3F::FromOuterProduct(
23 Matrix3F::VectorType(1.0f, 1.0f, 1.0f),
24 Matrix3F::VectorType(0.0f, 0.0f, 0.0f));
25 EXPECT_EQ(ones, product_ones);
26 EXPECT_EQ(zeros, product_zeros);
27
28 for (int i = 0; i < 3; ++i) {
29 for (int j = 0; j < 3; ++j)
30 EXPECT_EQ(i == j ? 1.0 : 0.0, identity.get(i, j));
31 }
32 }
33
34 TEST(Matrix3fTest, DataAccess) {
35 Matrix3F matrix = Matrix3F::Ones();
36 Matrix3F identity = Matrix3F::Identity();
37
38 EXPECT_EQ(Matrix3F::VectorType(0.0, 1.0, 0.0), identity.GetColumn(1));
39 matrix.set(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
40 EXPECT_EQ(Matrix3F::VectorType(2.0, 5.0, 8.0), matrix.GetColumn(2));
41 matrix.SetColumn(0, Matrix3F::VectorType(0.1, 0.2, 0.3));
42 EXPECT_EQ(Matrix3F::VectorType(0.1, 0.2, 0.3), matrix.GetColumn(0));
43
44 EXPECT_EQ(0.1f, matrix.get(0, 0));
45 EXPECT_EQ(5.0f, matrix.get(1, 2));
46 }
47
48 TEST(Matrix3fTest, Determinant) {
49 EXPECT_EQ(1.0, Matrix3F::Identity().Determinant());
50 EXPECT_EQ(0.0, Matrix3F::Zeros().Determinant());
51 EXPECT_EQ(0.0, Matrix3F::Ones().Determinant());
52
53 // Now for something non-trivial...
54 Matrix3F matrix = Matrix3F::Zeros();
55 matrix.set(0, 5, 6, 8, 7, 0, 1, 9, 0);
56 EXPECT_EQ(390.0, matrix.Determinant());
57 matrix.set(2, 0, 3 * matrix.get(0, 0));
58 matrix.set(2, 1, 3 * matrix.get(0, 1));
59 matrix.set(2, 2, 3 * matrix.get(0, 2));
60 EXPECT_EQ(0, matrix.Determinant());
61
62 matrix.set(0.57 , 0.205, 0.942,
63 0.314, 0.845, 0.826,
64 0.131, 0.025, 0.962);
65 EXPECT_NEAR(0.3149, matrix.Determinant(), 0.0001);
66 }
67
68 TEST(Matrix3fTest, Inverse) {
69 Matrix3F identity = Matrix3F::Identity();
70 Matrix3F inv_identity = identity.Inverse();
71 EXPECT_EQ(identity, inv_identity);
72
73 Matrix3F singular = Matrix3F::Zeros();
74 singular.set(1.0, 3.0, 4.0,
75 2.0, 11.0, 5.0,
76 0.5, 1.5, 2.0);
77 EXPECT_EQ(0, singular.Determinant());
78 EXPECT_EQ(Matrix3F::Zeros(), singular.Inverse());
79
80 Matrix3F regular = Matrix3F::Zeros();
81 regular.set(0.57 , 0.205, 0.942,
82 0.314, 0.845, 0.826,
83 0.131, 0.025, 0.962);
84 Matrix3F inv_regular = regular.Inverse();
85 regular.set(2.51540616, -0.55138018, -1.98968043,
86 -0.61552266, 1.34920184, -0.55573636,
87 -0.32653861, 0.04002158, 1.32488726);
88 EXPECT_TRUE(regular.IsNear(inv_regular, 0.0001));
89 }
90
91 TEST(Matrix3fTest, EigenvectorsIdentity) {
92 // This block tests the trivial case of eigenvalues of the identity matrix.
93 Matrix3F identity = Matrix3F::Identity();
94 Matrix3F::VectorType eigenvals = identity.SolveEigenproblem(NULL);
95 EXPECT_EQ(Matrix3F::VectorType(1.0, 1.0, 1.0), eigenvals);
96 }
97
98 TEST(Matrix3fTest, EigenvectorsDiagonal) {
99 // This block tests the another trivial case of eigenvalues of a diagonal
100 // matrix. Here we expect values to be sorted.
101 Matrix3F matrix = Matrix3F::Zeros();
102 matrix.set(0, 0, 1.0);
103 matrix.set(1, 1, -2.5);
104 matrix.set(2, 2, 3.14);
105 Matrix3F eigenvectors = Matrix3F::Zeros();
106 Matrix3F::VectorType eigenvals = matrix.SolveEigenproblem(&eigenvectors);
107 EXPECT_EQ(Matrix3F::VectorType(3.14, 1.0, -2.5), eigenvals);
108
109 EXPECT_EQ(Matrix3F::VectorType(0.0, 0.0, 1.0), eigenvectors.GetColumn(0));
110 EXPECT_EQ(Matrix3F::VectorType(1.0, 0.0, 0.0), eigenvectors.GetColumn(1));
111 EXPECT_EQ(Matrix3F::VectorType(0.0, 1.0, 0.0), eigenvectors.GetColumn(2));
112 }
113
114 TEST(Matrix3fTest, EigenvectorsNiceNotPositive) {
115 // This block tests computation of eigenvectors of a matrix where nice
116 // round values are expected.
117 Matrix3F matrix = Matrix3F::Zeros();
118 // This is not a positive-definite matrix but eigenvalues and the first
119 // eigenvector should nonetheless be computed correctly.
120 matrix.set(3, 2, 4, 2, 0, 2, 4, 2, 3);
121 Matrix3F eigenvectors = Matrix3F::Zeros();
122 Matrix3F::VectorType eigenvals = matrix.SolveEigenproblem(&eigenvectors);
123 EXPECT_EQ(Matrix3F::VectorType(8.0, -1.0, -1.0), eigenvals);
124
125 Matrix3F::VectorType expected_principal(0.66666667, 0.33333333, 0.66666667);
126 EXPECT_NEAR(0.0,
127 (expected_principal - eigenvectors.GetColumn(0)).Length(),
128 0.000001);
129 }
130
131 TEST(Matrix3fTest, EigenvectorsPositiveDefinite) {
132 // This block tests computation of eigenvectors of a matrix where output
133 // is not as nice as above, but it actually meets the definition.
134 Matrix3F matrix = Matrix3F::Zeros();
135 Matrix3F eigenvectors = Matrix3F::Zeros();
136 Matrix3F expected_eigenvectors = Matrix3F::Zeros();
137 matrix.set(1, -1, 2, -1, 4, 5, 2, 5, 0);
138 Matrix3F::VectorType eigenvals = matrix.SolveEigenproblem(&eigenvectors);
139 Matrix3F::VectorType expected_eigv(7.3996266, 1.91197255, -4.31159915);
140 expected_eigv -= eigenvals;
141 EXPECT_NEAR(0.0, expected_eigv.LengthSquared(), 0.00001);
142 expected_eigenvectors.set(0.04926317, -0.92135662, -0.38558414,
143 0.82134249, 0.25703273, -0.50924521,
144 0.56830419, -0.2916096, 0.76941158);
145 EXPECT_TRUE(expected_eigenvectors.IsNear(eigenvectors, 0.00001));
146 }
OLDNEW
« 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