Khangai Robot Play - Naive
01
THe naive play of the Khangai Robot
mat.h
Go to the documentation of this file.
1
/*
2
* mat.h
3
*
4
* Created : 12/31/2018
5
* Author : n-is
6
* email : 073bex422.nischal@pcampus.edu.np
7
*/
8
9
#ifndef _MAT_H_
10
#define _MAT_H_
11
12
#include <math.h>
13
#include "
vec3.h
"
14
15
#define MAX_MATRIX_ROWS (6)
16
#define MAX_MATRIX_COLS (6)
17
18
class
Mat
19
{
20
public
:
21
Mat
() :
Mat
(0,0) { }
22
23
template
<
size_t
M,
size_t
N>
24
Mat
(
const
float
(&mat)[M][N]) :
Mat
(M,N){
25
fill
(mat);
26
}
27
28
Mat
(uint8_t
rows
, uint8_t columns);
29
Mat
(
Mat
&&) =
default
;
30
Mat
&
operator=
(
Mat
&&) =
default
;
31
~Mat
() { }
32
33
uint8_t
rows
()
const
{
return
rows_
; }
34
uint8_t
cols
()
const
{
return
cols_
; }
35
36
inline
float
&
at
(uint8_t i, uint8_t j) {
37
if
(!(i <
rows_
&& j <
cols_
)) {
38
_Error_Handler
(__FILE__, __LINE__);
39
}
40
return
matrix_
[i][j];
41
}
42
43
44
Mat
(
const
Mat
&m);
45
46
Mat
&
operator=
(
const
Mat
&m) {
47
Mat
temp(m);
48
swap
(*
this
, temp);
49
50
return
*
this
;
51
}
52
53
Mat
&
operator+=
(
const
Mat
&rhs);
54
Mat
&
operator-=
(
const
Mat
&rhs);
55
Mat
&
operator*=
(
const
Mat
&rhs);
56
Mat
&
operator*=
(
const
Vec3<float>
&rhs);
57
58
friend
Mat
operator+
(
Mat
lhs,
const
Mat
&rhs) {
59
lhs += rhs;
60
return
lhs;
61
}
62
63
friend
Mat
operator-
(
Mat
lhs,
const
Mat
&rhs) {
64
lhs -= rhs;
65
return
lhs;
66
}
67
68
friend
Mat
operator*
(
Mat
lhs,
const
Mat
&rhs) {
69
lhs *= rhs;
70
return
lhs;
71
}
72
73
friend
Mat
operator*
(
Mat
lhs,
const
Vec3<float>
&rhs) {
74
lhs *= rhs;
75
return
lhs;
76
}
77
78
Mat
mult
(
const
Mat
&m);
79
Mat
mult_EW
(
float
num);
80
Mat
transpose
();
81
82
// Just an alias for getting transpose of a matrix
83
inline
Mat
trans
() {
return
transpose
(); }
84
85
void
fill
(
float
num) {
86
for
(uint8_t i = 0; i <
rows_
; ++i) {
87
for
(uint8_t j = 0; j <
cols_
; ++j) {
88
matrix_
[i][j] = num;
89
}
90
}
91
}
92
93
template
<
size_t
M,
size_t
N>
94
void
fill
(
const
float
(&mat)[M][N]) {
95
if
(M !=
rows_
&& N !=
cols_
) {
96
_Error_Handler
(__FILE__, __LINE__);
97
}
98
for
(uint8_t i = 0; i < M; ++i) {
99
for
(uint8_t j = 0; j < N; ++j) {
100
matrix_
[i][j] = mat[i][j];
101
}
102
}
103
}
104
105
void
add_Rows
(uint8_t r) {
106
if
(r +
rows_
<=
MAX_MATRIX_ROWS
) {
107
rows_
+= r;
108
}
109
}
110
111
void
add_Cols
(uint8_t c) {
112
if
(c +
cols_
<=
MAX_MATRIX_COLS
) {
113
cols_
+= c;
114
}
115
}
116
117
static
Mat
eye
(uint8_t n);
118
119
bool
is_Zero
()
const
;
120
void
swap_Rows
(uint8_t a, uint8_t b);
121
void
swap_Cols
(
size_t
a,
size_t
b);
122
bool
inv
(
Mat
&
inv
)
const
;
123
124
Mat
inv
()
const
125
{
126
Mat
i(
rows_
,
cols_
);
127
if
(
inv
(i)) {
128
return
i;
129
}
130
131
i.
fill
(0);
132
return
i;
133
}
134
135
void
print
()
const
{
136
for
(uint8_t i = 0; i <
rows_
; ++i) {
137
for
(uint8_t j = 0; j <
cols_
; ++j) {
138
printf(
"%ld\t"
, (int32_t)
matrix_
[i][j]);
139
}
140
printf(
"\n"
);
141
}
142
}
143
144
private
:
145
float
matrix_
[
MAX_MATRIX_ROWS
][
MAX_MATRIX_COLS
];
146
uint8_t
rows_
,
cols_
;
147
148
friend
void
swap
(
Mat
&first,
Mat
&second);
149
};
150
151
Mat
solve
(
Mat
A,
Mat
B);
152
153
154
#endif // !_MAT_H_
Mat::is_Zero
bool is_Zero() const
Definition:
mat.cpp:150
Mat::matrix_
float matrix_[MAX_MATRIX_ROWS][MAX_MATRIX_COLS]
Definition:
mat.h:145
Mat::fill
void fill(const float(&mat)[M][N])
Definition:
mat.h:94
Mat::Mat
Mat()
Definition:
mat.h:21
Vec3< float >
Mat::print
void print() const
Definition:
mat.h:135
Mat
Definition:
mat.h:18
Mat::transpose
Mat transpose()
Definition:
mat.cpp:129
Mat::operator=
Mat & operator=(Mat &&)=default
Mat::operator *=
Mat & operator *=(const Mat &rhs)
Definition:
mat.cpp:79
Mat::mult
Mat mult(const Mat &m)
Definition:
mat.cpp:96
Mat::operator-
friend Mat operator-(Mat lhs, const Mat &rhs)
Definition:
mat.h:63
Mat::rows_
uint8_t rows_
Definition:
mat.h:146
Mat::swap_Rows
void swap_Rows(uint8_t a, uint8_t b)
Definition:
mat.cpp:162
Mat::operator-=
Mat & operator-=(const Mat &rhs)
Definition:
mat.cpp:64
Mat::mult_EW
Mat mult_EW(float num)
Definition:
mat.cpp:117
_Error_Handler
void _Error_Handler(const char *file, size_t line)
Definition:
main.cpp:250
Mat::Mat
Mat(const float(&mat)[M][N])
Definition:
mat.h:24
Mat::~Mat
~Mat()
Definition:
mat.h:31
Mat::fill
void fill(float num)
Definition:
mat.h:85
Mat::at
float & at(uint8_t i, uint8_t j)
Definition:
mat.h:36
Mat::cols_
uint8_t cols_
Definition:
mat.h:146
MAX_MATRIX_COLS
#define MAX_MATRIX_COLS
Definition:
mat.h:16
MAX_MATRIX_ROWS
#define MAX_MATRIX_ROWS
Definition:
mat.h:15
Mat::swap
friend void swap(Mat &first, Mat &second)
Definition:
mat.cpp:38
Mat::operator=
Mat & operator=(const Mat &m)
Definition:
mat.h:46
Mat::eye
static Mat eye(uint8_t n)
Definition:
mat.cpp:140
Mat::trans
Mat trans()
Definition:
mat.h:83
Mat::operator+
friend Mat operator+(Mat lhs, const Mat &rhs)
Definition:
mat.h:58
solve
Mat solve(Mat A, Mat B)
Definition:
mat.cpp:312
Mat::add_Cols
void add_Cols(uint8_t c)
Definition:
mat.h:111
Mat::operator *
friend Mat operator *(Mat lhs, const Mat &rhs)
Definition:
mat.h:68
Mat::operator+=
Mat & operator+=(const Mat &rhs)
Definition:
mat.cpp:49
Mat::rows
uint8_t rows() const
Definition:
mat.h:33
Mat::cols
uint8_t cols() const
Definition:
mat.h:34
Mat::add_Rows
void add_Rows(uint8_t r)
Definition:
mat.h:105
Mat::swap_Cols
void swap_Cols(size_t a, size_t b)
Definition:
mat.cpp:178
vec3.h
Mat::inv
Mat inv() const
Definition:
mat.h:124
Core
Inc
utils
math
mat.h
Generated by
1.8.15