File: Matrix3.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (165 lines) | stat: -rw-r--r-- 4,338 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "gtest/gtest.h"

#include "math/Matrix3.h"

namespace test
{

TEST(Matrix3Test, CreateIdentityMatrix)
{
    const auto identity = Matrix3::getIdentity();
    EXPECT_EQ(identity, Matrix3::byRows(1, 0, 0,
        0, 1, 0,
        0, 0, 1));
}

TEST(Matrix3Test, AssignMatrixComponents)
{
    Matrix3 identity;

    identity.xx() = 1;
    identity.xy() = 0;
    identity.xz() = 0;

    identity.yx() = 0;
    identity.yy() = 1;
    identity.yz() = 0;

    identity.zx() = 0;
    identity.zy() = 0;
    identity.zz() = 1;

    EXPECT_EQ(identity, Matrix3::getIdentity());
}

TEST(Matrix3Test, ConstructMatrixByRows)
{
    auto m = Matrix3::byRows(1, 2.5, 3,
        0.34, 51, -6,
        7, 9, 17);

    // Check individual values
    EXPECT_EQ(m.xx(), 1);
    EXPECT_EQ(m.xy(), 0.34);
    EXPECT_EQ(m.xz(), 7);

    EXPECT_EQ(m.yx(), 2.5);
    EXPECT_EQ(m.yy(), 51);
    EXPECT_EQ(m.yz(), 9);

    EXPECT_EQ(m.zx(), 3);
    EXPECT_EQ(m.zy(), -6);
    EXPECT_EQ(m.zz(), 17);
}

TEST(Matrix3Test, MatrixEquality)
{
    Matrix3 m1 = Matrix3::byRows(1, 2, 3.5,
        5, -6, 17,
        9.01, 10, 11);
    Matrix3 m2 = m1;

    EXPECT_TRUE(m1 == m2);
    EXPECT_EQ(m1, m2);
    EXPECT_NE(m1, Matrix3::getIdentity());
    EXPECT_NE(m1, Matrix3::getIdentity());
    EXPECT_TRUE(m1 != Matrix3::getIdentity());
    EXPECT_TRUE(m2 != Matrix3::getIdentity());
}

TEST(Matrix3Test, MatrixMultiplication)
{
    auto a = Matrix3::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29);
    auto b = Matrix3::byColumns(61, 67, 71, 73, 79, 83, 89, 97, 101);

    // Check multiplied result
    auto c = a.getMultipliedBy(b);
    EXPECT_EQ(c, Matrix3::byColumns(2269, 2809, 3625,
        2665, 3301, 4261,
        3253, 4029, 5201));

    // Multiplication has not changed original
    EXPECT_NE(a, c);

    // Check operator multiplication as well
    EXPECT_EQ(a * b, c);

    // Test Pre-Multiplication
    EXPECT_EQ(b.getMultipliedBy(a), a.getPremultipliedBy(b)) << "Matrix pre-multiplication mismatch";
}

TEST(Matrix3Test, MatrixFullInverse)
{
    auto a = Matrix3::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29);

    auto inv = a.getFullInverse();

    EXPECT_DOUBLE_EQ(inv.xx(), -7.0 / 10) << "Matrix inversion failed on xx";
    EXPECT_DOUBLE_EQ(inv.xy(), 4.0 / 5) << "Matrix inversion failed on xy";
    EXPECT_DOUBLE_EQ(inv.xz(), -3.0 / 10) << "Matrix inversion failed on xz";

    EXPECT_DOUBLE_EQ(inv.yx(), 1.0 / 5) << "Matrix inversion failed on yx";
    EXPECT_DOUBLE_EQ(inv.yy(), -23.0 / 10) << "Matrix inversion failed on yy";
    EXPECT_DOUBLE_EQ(inv.yz(), 13.0 / 10) << "Matrix inversion failed on yz";

    EXPECT_DOUBLE_EQ(inv.zx(), 3.0 / 10) << "Matrix inversion failed on zx";
    EXPECT_DOUBLE_EQ(inv.zy(), 13.0 / 10) << "Matrix inversion failed on zy";
    EXPECT_DOUBLE_EQ(inv.zz(), -4.0 / 5) << "Matrix inversion failed on zz";
}

TEST(Matrix3Test, MatrixTransformation)
{
    auto a = Matrix3::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29);

    {
        Vector2 v(61, 67);

        Vector2 transformed = a.transformPoint(v);

        EXPECT_EQ(transformed.x(), 939) << "Vector2 transformation failed";
        EXPECT_EQ(transformed.y(), 1199) << "Vector2 transformation failed";

        EXPECT_EQ(a * v, a.transformPoint(v));
    }

    {
        Vector3 vector(83, 89, 97);
        Vector3 transformed = a.transform(vector);

        EXPECT_EQ(transformed.x(), 3071) << "Vector3 transformation failed";
        EXPECT_EQ(transformed.y(), 3803) << "Vector3 transformation failed";
        EXPECT_EQ(transformed.z(), 4907) << "Vector3 transformation failed";

        EXPECT_EQ(a * vector, a.transform(vector));
    }
}

TEST(Matrix3Test, ConstructTranslationMatrix)
{
    const Vector2 translation(1.5, -2939);
    auto tm = Matrix3::getTranslation(translation);

    EXPECT_EQ(tm, Matrix3::byRows(1, 0, translation.x(), 0, 1, translation.y(), 0, 0, 1));
}

TEST(Matrix3Test, ConstructRotationMatrix)
{
    const double angle = degrees_to_radians(15);
    auto tm = Matrix3::getRotation(angle);

    EXPECT_EQ(tm, Matrix3::byRows(cos(angle), -sin(angle), 0,
        sin(angle), cos(angle), 0,
        0, 0, 1));
}

TEST(Matrix3Test, ConstructScaleMatrix)
{
    const Vector2 scale(1.5, -3.2);
    auto tm = Matrix3::getScale(scale);

    EXPECT_EQ(tm, Matrix3::byRows(scale.x(), 0, 0,
        0, scale.y(), 0,
        0, 0, 1));
}

}