Developer Documentation
Loading...
Searching...
No Matches
Matrix3x3.cc
1#include <gtest/gtest.h>
2
3#include <ACG/Math/Matrix3x3T.hh>
4#include "MatrixTestHelper.hh"
5
6namespace {
7
8template<class Scalar>
9class Matrix3x3Test: public testing::Test {
10 public:
11 Matrix3x3Test() {}
12 virtual ~Matrix3x3Test() {}
13
14 typedef typename ACG::Matrix3x3T<Scalar> Matrix3x3;
15};
16
17typedef testing::Types<double, float> Implementations;
18
19TYPED_TEST_SUITE(Matrix3x3Test, Implementations);
20
21TYPED_TEST(Matrix3x3Test, access) {
22 using Matrix3x3 = typename Matrix3x3Test<TypeParam>::Matrix3x3;
23
24 Matrix3x3 m {{
25 1, 2, 3,
26 4, 5, 6,
27 7, 8, 9 }};
28
29 EXPECT_EQ(1, m[0]);
30 EXPECT_EQ(1, m(0, 0));
31 EXPECT_EQ(2, m[1]);
32 EXPECT_EQ(2, m(0, 1));
33 EXPECT_EQ(3, m[2]);
34 EXPECT_EQ(3, m(0, 2));
35
36 EXPECT_EQ(4, m[3]);
37 EXPECT_EQ(4, m(1, 0));
38 EXPECT_EQ(5, m[4]);
39 EXPECT_EQ(5, m(1, 1));
40 EXPECT_EQ(6, m[5]);
41 EXPECT_EQ(6, m(1, 2));
42
43 EXPECT_EQ(7, m[6]);
44 EXPECT_EQ(7, m(2, 0));
45 EXPECT_EQ(8, m[7]);
46 EXPECT_EQ(8, m(2, 1));
47 EXPECT_EQ(9, m[8]);
48 EXPECT_EQ(9, m(2, 2));
49}
50
51TYPED_TEST(Matrix3x3Test, construction) {
52
53 using Matrix3x3 = typename Matrix3x3Test<TypeParam>::Matrix3x3;
54 using Vec3 = typename Matrix3x3::Vec3;
55
56 Matrix3x3 m {{
57 1, 2, 3,
58 4, 5, 6,
59 7, 8, 9 }};
60
61 EXPECT_EQ(m, Matrix3x3::fromColumns(
62 Vec3(1, 4, 7),
63 Vec3(2, 5, 8),
64 Vec3(3, 6, 9)));
65
66 EXPECT_EQ(m, Matrix3x3::fromRows(
67 Vec3(1, 2, 3),
68 Vec3(4, 5, 6),
69 Vec3(7, 8, 9)));
70}
71
72TYPED_TEST(Matrix3x3Test, transpose) {
73
74 using Matrix3x3 = typename Matrix3x3Test<TypeParam>::Matrix3x3;
75
76 Matrix3x3 m {{
77 1, 2, 3,
78 4, 5, 6,
79 7, 8, 9 }};
80
81 EXPECT_EQ(Matrix3x3({
82 1, 4, 7,
83 2, 5, 8,
84 3, 6, 9}),
85 m.transposed());
86
87 m.transpose();
88
89 EXPECT_EQ(Matrix3x3({
90 1, 4, 7,
91 2, 5, 8,
92 3, 6, 9}),
93 m);
94}
95
96TYPED_TEST(Matrix3x3Test, det) {
97
98 using Matrix3x3 = typename Matrix3x3Test<TypeParam>::Matrix3x3;
99
100 ASSERT_NEAR(1.0, Matrix3x3::identity().det(), 1e-8);
101 ASSERT_NEAR(0.0, Matrix3x3::zero().det(), 1e-8);
102
103 {
104 Matrix3x3 m {{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }};
105 ASSERT_NEAR(0.0, m.det(), 1e-8);
106 }
107 {
108 Matrix3x3 m {{
109 0.540963817892, 0.63815313458, 0.232901321176,
110 0.427852547119, 0.290360892496, 0.790216925959,
111 0.521293721788, 0.126183253064, 0.878791594258 }};
112 ASSERT_NEAR(0.0843528547941, m.det(), 1e-8);
113 }
114 {
115 Matrix3x3 m {{
116 0.55770108832, 0.698229653416, 0.213133500513,
117 0.554922251751, 0.110301921695, 0.961776097631,
118 0.324829998436, 0.473104184394, 0.475331624799 }};
119 ASSERT_NEAR(-0.142243235281, m.det(), 1e-8);
120 }
121}
122
123TYPED_TEST(Matrix3x3Test, invert_inverted) {
124
125 using Matrix3x3 = typename Matrix3x3Test<TypeParam>::Matrix3x3;
126
127 EXPECT_EQ(Matrix3x3::identity(), Matrix3x3::identity().inverse());
128 {
129 Matrix3x3 m {{
130 0.828277802518, 0.265835425799, 0.764172058766,
131 0.460819591568, 0.0582725933838, 0.682623689065,
132 0.00010136137219, 0.549667442228, 0.71426378955 }};
133 EXPECT_TRUE(areClose({{
134 1.95965938707, -1.35207106222, -0.804410387772,
135 1.93312797956, -3.47488258913, 1.25275115042,
136 -1.48793227563, 2.67431570876, 0.436092407449 }}, m.inverse()));
137 Matrix3x3 minv = m; minv.invert();
138 EXPECT_EQ(m.inverse(), minv);
139 EXPECT_TRUE(areClose(minv.inverse(), m));
140 }
141 {
142 Matrix3x3 m {{
143 0.177907401463, 0.801552028587, 0.537416435716,
144 0.998303071804, 0.3525500305, 0.779702329831,
145 0.00697248858758, 0.927937880557, 0.917278319035 }};
146 EXPECT_TRUE(areClose({{
147 1.3148934724, 0.777368523682, -1.43114841493,
148 2.9913570641, -0.523959200394, -1.30720656662,
149 -3.03611407354, 0.524139060932, 2.42345764719 }}, m.inverse()));
150 Matrix3x3 minv = m; minv.invert();
151 EXPECT_EQ(m.inverse(), minv);
152 EXPECT_TRUE(areClose(minv.inverse(), m));
153 }
154}
155
156TYPED_TEST(Matrix3x3Test, matrix_multiplication) {
157 using Matrix3x3 = typename Matrix3x3Test<TypeParam>::Matrix3x3;
158
159 EXPECT_EQ(Matrix3x3::identity(), Matrix3x3::identity() * Matrix3x3::identity());
160 EXPECT_EQ(Matrix3x3::zero(), Matrix3x3::identity() * Matrix3x3::zero());
161 {
162 Matrix3x3 a {{
163 0.0198677492323, 0.335847288905, 0.903519116051,
164 0.00663887675374, 0.24203377376, 0.860915878917,
165 0.159079677011, 0.51176099262, 0.754881431552 }};
166 Matrix3x3 b {{
167 0.685605402853, 0.550061681341, 0.963595467664,
168 0.31801733629, 0.219196960585, 0.881308916676,
169 0.593421354113, 0.562284336992, 0.0822415517585 }};
170 EXPECT_EQ(a, a * Matrix3x3::identity());
171 EXPECT_EQ(Matrix3x3::zero(), a * Matrix3x3::zero());
172 EXPECT_EQ(Matrix3x3::zero(), Matrix3x3::zero() * a);
173 EXPECT_TRUE(areClose({{
174 0.656594233747, 0.592579839624, 0.389436497614,
175 0.592408452439, 0.540784373459, 0.290506772318,
176 0.719777515039, 0.62413809398, 0.666390602093 }}, a * b));
177 auto ab = a; ab *= b; EXPECT_EQ(ab, a*b);
178 }
179 {
180 Matrix3x3 a {{
181 -0.69449806209, 0.782467649048, -0.665216245975,
182 -0.285290453681, 0.395547604054, 0.635321636096,
183 -0.463882724822, 0.197102669096, -0.972470374827 }};
184 Matrix3x3 b {{
185 0.510414656025, 0.738189497916, 0.9901980894,
186 -0.06427439106, 0.706676712526, 0.550975704042,
187 -0.468791241495, -0.70849622459, 0.313163366829 }};
188 EXPECT_TRUE(areClose({{
189 -0.0929270713252, 0.511583688938, -0.464891349608,
190 -0.468873228703, -0.381197116857, 0.134402520046,
191 0.206444398874, 0.485846099589, -0.655279102673 }}, a * b));
192 auto ab = a; ab *= b; EXPECT_EQ(ab, a*b);
193 }
194 {
195 Matrix3x3 a {{
196 -4.33305043354, -2.68416159097, 6.12099479706,
197 -2.56865371689, 2.45552630644, -3.71903598774,
198 -1.46747079278, 4.91597319165, 1.04193027614 }};
199 Matrix3x3 b {{
200 9.52441107801, -0.222280392529, 8.43656371755,
201 9.3234433143, -1.272702334, -2.20088570156,
202 -2.76499303176, -4.57362467379, 3.2829486774 }};
203 EXPECT_TRUE(areClose({{
204 -83.2198899519, -23.6158419591, -10.5536114343,
205 8.71215499908, 14.4552820513, -39.2843477657,
206 28.9760723585, -10.6957785904, -19.7793023317 }}, a * b));
207 auto ab = a; ab *= b; EXPECT_EQ(ab, a*b);
208 }
209}
210
211TYPED_TEST(Matrix3x3Test, vector_multiplication) {
212 using Matrix3x3 = typename Matrix3x3Test<TypeParam>::Matrix3x3;
213 using Vec3 = typename Matrix3x3::Vec3;
214
215 {
216 const Matrix3x3 m {{
217 0.637256867597, 0.496729074271, 0.875025689854,
218 -0.0790360016434, -0.372010734739, -0.900793564733,
219 -0.150797220981, -0.679495918006, -0.995588120053 }};
220 const auto v = Vec3(0.0128269540114,-0.406921319051,0.42309802041);
221 EXPECT_TRUE(areClose(Vec3(0.176266051606,-0.230758666314,-0.146664256512), m * v));
222 EXPECT_EQ(m * v, v * m);
223
224 EXPECT_EQ(v, Matrix3x3::identity() * v);
225 EXPECT_EQ(v, v * Matrix3x3::identity());
226 EXPECT_EQ(Vec3(0), Matrix3x3::zero() * v);
227 EXPECT_EQ(Vec3(0), v * Matrix3x3::zero());
228 }
229}
230
231} /* anonymous namespace */