RoboidControl
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1#ifndef MATRIX_H
2#define MATRIX_H
3
4#include "Vector3.h"
5
6namespace Passer {
7namespace LinearAlgebra {
8
10template <typename T> class MatrixOf {
11public:
12 MatrixOf(unsigned int rows, unsigned int cols);
13 MatrixOf(unsigned int rows, unsigned int cols, const T *source)
14 : MatrixOf(rows, cols) {
15 Set(source);
16 }
17 MatrixOf(Vector3 v); // creates a 3,1 matrix
18
20 if (this->data == nullptr)
21 return;
22
23 delete[] this->data;
24 }
25
28 void Transpose(MatrixOf<T> *r) const {
29 // Check dimensions first
30 // We dont care about the rows and cols (we overwrite them)
31 // but the data size should be equal to avoid problems
32 // We cannot check the data size directly, but the row*col should be equal
33 unsigned int matrixSize = this->cols * this->rows;
34 unsigned int resultSize = r->rows * r->cols;
35 if (matrixSize != resultSize) {
36 // Return a null matrix;
37 // We dont set data to nullptr because it is allocated memory
38 // Instead we write all zeros
39 for (unsigned int dataIx = 0; dataIx < resultSize; dataIx++)
40 r->data[dataIx] = 0.0f;
41 r->rows = 0;
42 r->cols = 0;
43 return;
44 }
45
46 r->cols = this->rows;
47 r->rows = this->cols;
48
49 for (unsigned int rDataIx = 0; rDataIx < matrixSize; rDataIx++) {
50 unsigned int rowIx = rDataIx / this->rows;
51 unsigned int colIx = rDataIx % this->rows;
52 unsigned int mDataIx = this->cols * colIx + rowIx;
53 r->data[rDataIx] = this->data[mDataIx];
54 }
55 }
56
57 static void Multiply(const MatrixOf<T> *m1, const MatrixOf<T> *m2,
58 MatrixOf<T> *r);
59 void Multiply(const MatrixOf<T> *m, MatrixOf<T> *r) const {
60 Multiply(this, m, r);
61 }
62
63 static Vector3 Multiply(const MatrixOf<T> *m, Vector3 v);
64 Vector3 operator*(const Vector3 v) const;
65
66 T Get(unsigned int rowIx, unsigned int colIx) const {
67 unsigned int dataIx = rowIx * this->cols + colIx;
68 return this->data[dataIx];
69 }
70
71 void Set(unsigned int rowIx, unsigned int colIx, T value) {
72 unsigned int dataIx = rowIx * this->cols + colIx;
73 this->data[dataIx] = value;
74 }
75
76 // This function does not check on source size!
77 void Set(const T *source) {
78 unsigned int matrixSize = this->cols * this->rows;
79 for (unsigned int dataIx = 0; dataIx < matrixSize; dataIx++)
80 this->data[dataIx] = source[dataIx];
81 }
82
83 // This function does not check on source size!
84 void SetRow(unsigned int rowIx, const T *source) {
85 unsigned int dataIx = rowIx * this->cols;
86 for (unsigned int sourceIx = 0; sourceIx < this->cols; dataIx++, sourceIx++)
87 this->data[dataIx] = source[sourceIx];
88 }
89
90 // This function does not check on source size!
91 void SetCol(unsigned int colIx, const T *source) {
92 unsigned int dataIx = colIx;
93 for (unsigned int sourceIx = 0; sourceIx < this->cols;
94 dataIx += this->cols, sourceIx++)
95 this->data[dataIx] = source[sourceIx];
96 }
97
98 void CopyFrom(const MatrixOf<T> *m) {
99 unsigned int thisMatrixSize = this->cols * this->rows;
100 unsigned int mMatrixSize = m->cols * m->rows;
101 if (mMatrixSize != thisMatrixSize)
102 return;
103
104 for (unsigned int dataIx = 0; dataIx < thisMatrixSize; dataIx++)
105 this->data[dataIx] = m->data[dataIx];
106 }
107
108 unsigned int RowCount() const { return rows; }
109 unsigned int ColCount() const { return cols; }
110
111private:
112 unsigned int rows;
113 unsigned int cols;
114 T *data;
115};
116
117} // namespace LinearAlgebra
118} // namespace Passer
119using namespace Passer::LinearAlgebra;
120
121#endif
Single precision float matrix.
Definition Matrix.h:10
void Set(const T *source)
Definition Matrix.h:77
MatrixOf(unsigned int rows, unsigned int cols)
void CopyFrom(const MatrixOf< T > *m)
Definition Matrix.h:98
MatrixOf(unsigned int rows, unsigned int cols, const T *source)
Definition Matrix.h:13
~MatrixOf()
Definition Matrix.h:19
void Set(unsigned int rowIx, unsigned int colIx, T value)
Definition Matrix.h:71
Vector3 operator*(const Vector3 v) const
Definition Matrix.cpp:50
void Transpose(MatrixOf< T > *r) const
Transpose with result in matrix m.
Definition Matrix.h:28
void Multiply(const MatrixOf< T > *m, MatrixOf< T > *r) const
Definition Matrix.h:59
unsigned int ColCount() const
Definition Matrix.h:109
T Get(unsigned int rowIx, unsigned int colIx) const
Definition Matrix.h:66
static void Multiply(const MatrixOf< T > *m1, const MatrixOf< T > *m2, MatrixOf< T > *r)
unsigned int RowCount() const
Definition Matrix.h:108
static Vector3 Multiply(const MatrixOf< T > *m, Vector3 v)
void SetRow(unsigned int rowIx, const T *source)
Definition Matrix.h:84
void SetCol(unsigned int colIx, const T *source)
Definition Matrix.h:91
Definition Angle.h:9
Definition AbsoluteEncoder.h:5
A 3-dimensional vector.
Definition Vector3.h:42