wren
Vulkan-based game engine
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4
5#include "vector.hpp"
6
7namespace wren::math {
8
9/*
10 @brief Column major matrix
11*/
12template <typename T, std::size_t Rows, std::size_t Cols>
13class Mat {
14 public:
16 using data_t = std::array<T, Rows * Cols>;
17
18 constexpr static auto identity() {
19 mat_t m{};
20 for (size_t i = 0; i < Rows; ++i) {
21 m.at(i, i) = 1.0;
22 }
23 return m;
24 }
25
26 Mat() = default;
27 Mat(const data_t& data) : data_(data) {}
28 Mat(const std::array<std::array<T, Cols>, Rows>& rows) {
29 for (auto row = 0; row < Rows; ++row) {
30 for (auto col = 0; col < Cols; ++col) {
31 at(col, row) = rows.at(row).at(col);
32 }
33 }
34 }
35 // Mat(float d) { data.fill(std::array<T, N>{d}); }
36
37 auto operator==(const mat_t& rhs) const { return data_ == rhs.data_; }
38 auto operator!=(const mat_t& rhs) const { return !(*this == rhs); }
39 auto operator*(const mat_t& rhs) {
40 mat_t result{};
41
42 // Multiply the rows of this matrix by the cols of rhs
43
44 for (size_t row = 0; row < Cols; ++row) {
45 for (size_t col = 0; col < Rows; ++col) {
46 result.at(col, row) = 0;
47 for (size_t k = 0; k < Cols; ++k) {
48 result.at(col, row) += at(k, row) * rhs.at(col, k);
49 }
50 }
51 }
52 return result;
53 }
54
55 auto operator*(const Vec<T, Rows>& rhs) {
56 Vec<T, Rows> res{};
57
58 // Multiply the vector by the columns
59
60 for (auto r = 0; r < Rows; ++r) {
61 for (auto c = 0; c < Cols; ++c) {
62 res.at(r) += rhs.at(c) * at(c, r);
63 }
64 }
65
66 return res;
67 }
68
69 auto at(std::size_t col, std::size_t row) -> T& {
70 return data_.at(get_index(col, row));
71 }
72 [[nodiscard]] auto at(std::size_t col, std::size_t row) const -> const T& {
73 return data_.at(get_index(col, row));
74 }
75
76 [[nodiscard]] auto data() const { return data_; }
77
78 private:
79 constexpr static auto get_index(std::size_t col, std::size_t row) {
80 return row + Rows * col;
81 }
82
83 std::array<T, Rows * Cols> data_{};
84};
85
86struct Mat4f : public Mat<float, 4, 4> {
87 Mat4f() = default;
88 Mat4f(const Mat<float, 4, 4>& other) : Mat<float, 4, 4>(other) {}
89 Mat4f(const data_t& data) : Mat<float, 4, 4>(data) {}
90 Mat4f(const Mat<float, 3, 3>& other) : Mat(identity()) {
91 for (auto row = 0; row < 3; ++row) {
92 for (auto col = 0; col < 3; ++col) {
93 at(col, row) = other.at(col, row);
94 }
95 }
96 }
97};
98
100
101} // namespace wren::math
Definition matrix.hpp:13
static constexpr auto get_index(std::size_t col, std::size_t row)
Definition matrix.hpp:79
auto data() const
Definition matrix.hpp:76
auto operator*(const Vec< T, Rows > &rhs)
Definition matrix.hpp:55
static constexpr auto identity()
Definition matrix.hpp:18
auto operator!=(const mat_t &rhs) const
Definition matrix.hpp:38
auto operator==(const mat_t &rhs) const
Definition matrix.hpp:37
Mat(const data_t &data)
Definition matrix.hpp:27
auto operator*(const mat_t &rhs)
Definition matrix.hpp:39
std::array< T, Rows *Cols > data_t
Definition matrix.hpp:16
auto at(std::size_t col, std::size_t row) const -> const T &
Definition matrix.hpp:72
std::array< T, Rows *Cols > data_
Definition matrix.hpp:83
auto at(std::size_t col, std::size_t row) -> T &
Definition matrix.hpp:69
Mat(const std::array< std::array< T, Cols >, Rows > &rows)
Definition matrix.hpp:28
Definition geometry.hpp:8
Definition matrix.hpp:86
Mat4f(const Mat< float, 4, 4 > &other)
Definition matrix.hpp:88
Mat4f(const Mat< float, 3, 3 > &other)
Definition matrix.hpp:90
Mat4f(const data_t &data)
Definition matrix.hpp:89
Definition vector.hpp:9
auto at(std::size_t i) -> T &
Definition vector.hpp:28