Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1#pragma once
2
3// Small matrix operations for spatial audio and graphics.
4// Supports 2x2, 3x3, and 4x4 float matrices with common operations.
5
6#include <array>
7#include <cmath>
8#include <cstring>
9
10namespace pulp::signal {
11
16template<int N, typename SampleType = float>
17struct Matrix {
18 std::array<SampleType, N * N> data{};
19
20 SampleType& at(int row, int col) { return data[row * N + col]; }
21 SampleType at(int row, int col) const { return data[row * N + col]; }
22
23 SampleType& operator()(int row, int col) { return at(row, col); }
24 SampleType operator()(int row, int col) const { return at(row, col); }
25
27 static Matrix identity() {
28 Matrix m{};
29 for (int i = 0; i < N; ++i)
30 m.at(i, i) = SampleType{1.0f};
31 return m;
32 }
33
35 static Matrix zero() { return Matrix{}; }
36
38 Matrix operator*(const Matrix& other) const {
39 Matrix result{};
40 for (int i = 0; i < N; ++i)
41 for (int j = 0; j < N; ++j)
42 for (int k = 0; k < N; ++k)
43 result.at(i, j) += at(i, k) * other.at(k, j);
44 return result;
45 }
46
48 Matrix operator*(SampleType scalar) const {
49 Matrix result{};
50 for (int i = 0; i < N * N; ++i)
51 result.data[i] = data[i] * scalar;
52 return result;
53 }
54
56 Matrix operator+(const Matrix& other) const {
57 Matrix result{};
58 for (int i = 0; i < N * N; ++i)
59 result.data[i] = data[i] + other.data[i];
60 return result;
61 }
62
65 Matrix result{};
66 for (int i = 0; i < N; ++i)
67 for (int j = 0; j < N; ++j)
68 result.at(j, i) = at(i, j);
69 return result;
70 }
71
72 bool operator==(const Matrix& other) const { return data == other.data; }
73};
74
81
83template <typename SampleType>
84inline SampleType determinant(const Matrix<2, SampleType>& m) {
85 return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0);
86}
87
89template <typename SampleType>
90inline SampleType determinant(const Matrix<3, SampleType>& m) {
91 return m(0, 0) * (m(1, 1) * m(2, 2) - m(1, 2) * m(2, 1))
92 - m(0, 1) * (m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0))
93 + m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0));
94}
95
97inline Matrix4 translation_matrix(float x, float y, float z) {
98 auto m = Matrix4::identity();
99 m(0, 3) = x;
100 m(1, 3) = y;
101 m(2, 3) = z;
102 return m;
103}
104
106inline Matrix4 scale_matrix(float sx, float sy, float sz) {
107 auto m = Matrix4::identity();
108 m(0, 0) = sx;
109 m(1, 1) = sy;
110 m(2, 2) = sz;
111 return m;
112}
113
115inline Matrix4 rotation_x(float radians) {
116 auto m = Matrix4::identity();
117 float c = std::cos(radians), s = std::sin(radians);
118 m(1, 1) = c; m(1, 2) = -s;
119 m(2, 1) = s; m(2, 2) = c;
120 return m;
121}
122
124inline Matrix4 rotation_y(float radians) {
125 auto m = Matrix4::identity();
126 float c = std::cos(radians), s = std::sin(radians);
127 m(0, 0) = c; m(0, 2) = s;
128 m(2, 0) = -s; m(2, 2) = c;
129 return m;
130}
131
133inline Matrix4 rotation_z(float radians) {
134 auto m = Matrix4::identity();
135 float c = std::cos(radians), s = std::sin(radians);
136 m(0, 0) = c; m(0, 1) = -s;
137 m(1, 0) = s; m(1, 1) = c;
138 return m;
139}
140
142struct Vec3 {
143 float x = 0, y = 0, z = 0;
144};
145
146inline Vec3 transform(const Matrix4& m, const Vec3& v) {
147 return {
148 m(0, 0) * v.x + m(0, 1) * v.y + m(0, 2) * v.z + m(0, 3),
149 m(1, 0) * v.x + m(1, 1) * v.y + m(1, 2) * v.z + m(1, 3),
150 m(2, 0) * v.x + m(2, 1) * v.y + m(2, 2) * v.z + m(2, 3)
151 };
152}
153
154} // namespace pulp::signal
Definition additive_bank.hpp:218
Matrix< 3, double > Matrix3d
Definition matrix.hpp:79
Matrix< 2, double > Matrix2d
Definition matrix.hpp:78
Matrix< 4 > Matrix4
Definition matrix.hpp:77
Matrix< 2 > Matrix2
Definition matrix.hpp:75
@ c
Definition spectrum_trace.hpp:35
Matrix4 rotation_z(float radians)
4x4 rotation around Z axis
Definition matrix.hpp:133
Matrix4 rotation_y(float radians)
4x4 rotation around Y axis
Definition matrix.hpp:124
Matrix4 scale_matrix(float sx, float sy, float sz)
4x4 scale matrix
Definition matrix.hpp:106
Matrix4 translation_matrix(float x, float y, float z)
4x4 translation matrix
Definition matrix.hpp:97
Matrix< 3 > Matrix3
Definition matrix.hpp:76
Matrix< 4, double > Matrix4d
Definition matrix.hpp:80
SampleType determinant(const Matrix< 2, SampleType > &m)
Determinant of a 2x2 matrix.
Definition matrix.hpp:84
@ i
Definition formant_filter_bank.hpp:20
Vec3 transform(const Matrix4 &m, const Vec3 &v)
Definition matrix.hpp:146
Matrix4 rotation_x(float radians)
4x4 rotation around X axis
Definition matrix.hpp:115
NxN matrix stored in row-major order.
Definition matrix.hpp:17
Matrix operator*(const Matrix &other) const
Matrix multiply.
Definition matrix.hpp:38
static Matrix zero()
Zero matrix.
Definition matrix.hpp:35
std::array< float, N *N > data
Definition matrix.hpp:18
Matrix operator*(SampleType scalar) const
Scalar multiply.
Definition matrix.hpp:48
SampleType at(int row, int col) const
Definition matrix.hpp:21
Matrix operator+(const Matrix &other) const
Matrix add.
Definition matrix.hpp:56
SampleType operator()(int row, int col) const
Definition matrix.hpp:24
static Matrix identity()
Identity matrix.
Definition matrix.hpp:27
Matrix transposed() const
Transpose.
Definition matrix.hpp:64
SampleType & operator()(int row, int col)
Definition matrix.hpp:23
bool operator==(const Matrix &other) const
Definition matrix.hpp:72
SampleType & at(int row, int col)
Definition matrix.hpp:20
Transform a 3D vector by a 4x4 matrix (assumes w=1).
Definition matrix.hpp:142
float x
Definition matrix.hpp:143
float y
Definition matrix.hpp:143
float z
Definition matrix.hpp:143