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:
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_
bool is_Zero() const
Definition: mat.cpp:150
float matrix_[MAX_MATRIX_ROWS][MAX_MATRIX_COLS]
Definition: mat.h:145
void fill(const float(&mat)[M][N])
Definition: mat.h:94
Mat()
Definition: mat.h:21
void print() const
Definition: mat.h:135
Definition: mat.h:18
Mat transpose()
Definition: mat.cpp:129
Mat & operator=(Mat &&)=default
Mat & operator *=(const Mat &rhs)
Definition: mat.cpp:79
Mat mult(const Mat &m)
Definition: mat.cpp:96
friend Mat operator-(Mat lhs, const Mat &rhs)
Definition: mat.h:63
uint8_t rows_
Definition: mat.h:146
void swap_Rows(uint8_t a, uint8_t b)
Definition: mat.cpp:162
Mat & operator-=(const Mat &rhs)
Definition: mat.cpp:64
Mat mult_EW(float num)
Definition: mat.cpp:117
void _Error_Handler(const char *file, size_t line)
Definition: main.cpp:250
Mat(const float(&mat)[M][N])
Definition: mat.h:24
~Mat()
Definition: mat.h:31
void fill(float num)
Definition: mat.h:85
float & at(uint8_t i, uint8_t j)
Definition: mat.h:36
uint8_t cols_
Definition: mat.h:146
#define MAX_MATRIX_COLS
Definition: mat.h:16
#define MAX_MATRIX_ROWS
Definition: mat.h:15
friend void swap(Mat &first, Mat &second)
Definition: mat.cpp:38
Mat & operator=(const Mat &m)
Definition: mat.h:46
static Mat eye(uint8_t n)
Definition: mat.cpp:140
Mat trans()
Definition: mat.h:83
friend Mat operator+(Mat lhs, const Mat &rhs)
Definition: mat.h:58
Mat solve(Mat A, Mat B)
Definition: mat.cpp:312
void add_Cols(uint8_t c)
Definition: mat.h:111
friend Mat operator *(Mat lhs, const Mat &rhs)
Definition: mat.h:68
Mat & operator+=(const Mat &rhs)
Definition: mat.cpp:49
uint8_t rows() const
Definition: mat.h:33
uint8_t cols() const
Definition: mat.h:34
void add_Rows(uint8_t r)
Definition: mat.h:105
void swap_Cols(size_t a, size_t b)
Definition: mat.cpp:178
Mat inv() const
Definition: mat.h:124