summary refs log tree commit diff
path: root/src/Math/Matrix.hpp
blob: c4532c395d8dcbb7ab6743f8f58b2141d700785a (plain)
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
#pragma once

#include <sstream>
#include "../Common/Sizes.hpp"
#include "Rotation.hpp"

template <uint R, uint C, typename T = Real>
struct Matrix {
    Matrix() : elements{} {}

    explicit Matrix(const T scalar) {
        std::fill(elements, elements + R * C, scalar);
    }

    template<typename ...Args, std::enable_if_t<sizeof...(Args) == R * C, Int> = 0>
    Matrix(Args... args): elements{ static_cast<T>(args)... } {}

    explicit Matrix(const T values[R * C]) {
        std::copy(values, values + R * C, elements);
    }

    static Matrix<R, R, T> identity() {
        Matrix<R, R, T> result{};
        for (Int i = 0; i < R; i++) {
            result(i, i) = 1;
        }
        return result;
    }

    static Matrix<4, 4, T> transformation(Vector<3> position) {
        return {
            1.0, 0.0, 0.0, position.x(),
            0.0, 1.0, 0.0, position.y(),
            0.0, 0.0, 1.0, position.z(),
            0.0, 0.0, 0.0, 1.0
        };
    }

    static Matrix<4, 4, T> scale(Vector<3> factor) {
        return {
            factor.x(), 0.0, 0.0, 0.0,
            0.0, factor.y(), 0.0, 0.0,
            0.0, 0.0, factor.z(), 0.0,
            0.0, 0.0, 0.0, 1.0
        };
    }

    static Matrix<4, 4, T> rotation(Rotation angles) {
        auto radians = angles.radians();

        auto c = radians.map([](auto a) { return cos(a); });
        auto s = radians.map([](auto a) { return sin(a); });

        Matrix<4, 4, T> rotation_x{
            1.0f, 0.0f,  0.0f,   0.0f,
            0.0f, c.x(), -s.x(), 0.0f,
            0.0f, s.x(), c.x(),  0.0f,
            0.0f, 0.0f,  0.0f,   1.0f,
        };

        Matrix<4, 4, T> rotation_y{
            c.y(),  0.0f, s.y(),  0.0f,
            0.0f,   1.0f, 0.0f,   0.0f,
            -s.y(), 0.0f, c.y(),  0.0f,
            0.0f,   0.0f, 0.0f,   1.0f,
        };

        Matrix<4, 4, T> rotation_z{
            c.z(), -s.z(), 0.0f,  0.0f,
            s.z(), c.z(),  0.0f,  0.0f,
            0.0f,  0.0f,   1.0f,  0.0f,
            0.0f,  0.0f,   0.0f,  1.0f,
        };

        return rotation_x * rotation_y * rotation_z;
    }

    Vector<C, T> row(USize index) const {
        return Vector<C, T>{ &elements[index * C] };
    }

    Vector<R, T> col(USize index) const {
        Vector<R, T> result{};
        for (Int i = 0; i < R; i++) {
            result[i] = this->operator()(index, i);
        }
        return result;
    }

    Matrix transpose() const {
        Matrix result{};
        for (Int y = 0; y < R; y++) {
            for (Int x = 0; x < C; x++) {
                result(x, y) = this->operator()(y, x);
            }
        }
        return result;
    }

    Matrix operator+(const Matrix other) const {
        Matrix result{};
        for (Int i = 0; i < R * C; i++) {
            result.elements[i] = elements[i] + other.elements[i];
        }
        return result;
    }

    Matrix operator*(Real scalar) {
        Matrix result{};
        for (Int i = 0; i < R * C; i++) {
            result.elements[i] = elements[i] * scalar;
        }
        return result;
    }

    template<uint N>
    Matrix<R, N, T> operator*(const Matrix<C, N, T> other) const {
        Matrix<R, N, T> result{};
        for (Int y = 0; y < R; y++) {
            for (Int x = 0; x < N; x++) {
                auto r = row(y);
                auto c = other.col(x);

                auto dot = r * c;

                result(x, y) = dot;
            }
        }
        return result;
    }

    Vector<R, T> operator*(const Vector<R, T> vector) const {
        Matrix<R, 1, T> matrix(vector.elements);
        matrix = this->operator*(matrix);
        return Vector<R, T>{ matrix.elements };
    }

    const T& operator()(const USize x, const USize y) const {
        return elements[y * C + x];
    }
    T& operator()(const USize x, const USize y) {
        return elements[y * C + x];
    }

    std::string string() {
        std::stringstream str{};
        for (Int x = 0; x < R; x++) {
            for (Int y = 0; y < C; y++) {
                str << this->operator()(x, y) << " ";
            }

            str << "\n";
        }

        return str.str();
    }

    T elements[R * C];
};