wren
Vulkan-based game engine
Toggle main menu visibility
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
7
namespace
wren::math
{
8
9
/*
10
@brief Column major matrix
11
*/
12
template
<
typename
T, std::
size_t
Rows, std::
size_t
Cols>
13
class
Mat
{
14
public
:
15
using
mat_t
=
Mat<T, Rows, Cols>
;
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)
const
{
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
86
struct
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
99
using
Mat3f
=
Mat<float, 3, 3>
;
100
101
}
// namespace wren::math
wren::math::Mat
Definition
matrix.hpp:13
wren::math::Mat::get_index
static constexpr auto get_index(std::size_t col, std::size_t row)
Definition
matrix.hpp:79
wren::math::Mat< float, 3, 3 >::data
auto data() const
Definition
matrix.hpp:76
wren::math::Mat::operator*
auto operator*(const Vec< T, Rows > &rhs)
Definition
matrix.hpp:55
wren::math::Mat::identity
static constexpr auto identity()
Definition
matrix.hpp:18
wren::math::Mat::operator*
auto operator*(const mat_t &rhs) const
Definition
matrix.hpp:39
wren::math::Mat::mat_t
Mat< T, Rows, Cols > mat_t
Definition
matrix.hpp:15
wren::math::Mat::operator!=
auto operator!=(const mat_t &rhs) const
Definition
matrix.hpp:38
wren::math::Mat::operator==
auto operator==(const mat_t &rhs) const
Definition
matrix.hpp:37
wren::math::Mat::Mat
Mat(const data_t &data)
Definition
matrix.hpp:27
wren::math::Mat::Mat
Mat()=default
wren::math::Mat::data_t
std::array< T, Rows *Cols > data_t
Definition
matrix.hpp:16
wren::math::Mat::at
auto at(std::size_t col, std::size_t row) const -> const T &
Definition
matrix.hpp:72
wren::math::Mat< float, 3, 3 >::data_
std::array< float, Rows *Cols > data_
Definition
matrix.hpp:83
wren::math::Mat::at
auto at(std::size_t col, std::size_t row) -> T &
Definition
matrix.hpp:69
wren::math::Mat::Mat
Mat(const std::array< std::array< T, Cols >, Rows > &rows)
Definition
matrix.hpp:28
wren::math
Definition
geometry.hpp:8
wren::math::Mat3f
Mat< float, 3, 3 > Mat3f
Definition
matrix.hpp:99
wren::math::Mat4f::Mat4f
Mat4f()=default
wren::math::Mat4f::Mat4f
Mat4f(const Mat< float, 4, 4 > &other)
Definition
matrix.hpp:88
wren::math::Mat4f::Mat4f
Mat4f(const Mat< float, 3, 3 > &other)
Definition
matrix.hpp:90
wren::math::Mat4f::Mat4f
Mat4f(const data_t &data)
Definition
matrix.hpp:89
wren::math::Vec
Definition
vector.hpp:10
wren::math::Vec::at
auto at(std::size_t i) -> T &
Definition
vector.hpp:47
vector.hpp
wren_math
include
wren
math
matrix.hpp
Generated by
1.17.0