-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat.hpp
113 lines (88 loc) · 2.41 KB
/
mat.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef KMUVCL_GRAPHICS_MAT_HPP
#define KMUVCL_GRAPHICS_MAT_HPP
#include <iostream>
#include <cstring>
#include <cstdarg>
namespace kmuvcl {
namespace math {
template <unsigned int M, unsigned int N, typename T>
class mat
{
public:
mat()
{
set_to_zero();
}
mat(const T elem)
{
std::fill(val, val + M*N, elem);
}
T& operator()(unsigned int r, unsigned int c)
{
return val[r + c*M]; // column major
}
const T& operator()(unsigned int r, unsigned int c) const
{
return val[r + c*M]; // column major
}
// type casting operators
operator const T* () const
{
return val;
}
operator T* ()
{
return val;
}
void set_to_zero()
{
std::fill(val, val + M*N, static_cast<T>(0));
}
void get_ith_column(unsigned int i, vec<M, T>& col) const
{
T* dest = (T*) col;
const T* src = val + i*M;
std::memcpy(dest, src, sizeof(T)*M);
}
void set_ith_column(unsigned int i, const vec<M, T>& col)
{
T* dest = val + i*M;
const T* src = (const T*)col;
std::memcpy(dest, src, sizeof(T)*M);
}
void get_ith_row(unsigned int i, vec<N, T>& row) const
{
for (unsigned int c = 0; c < N; ++c)
row(c) = (*this)(i, c);
}
void set_ith_row(unsigned int i, const vec<N, T>& row)
{
for (unsigned int c = 0; c < N; ++c)
(*this)(i, c) = row(c);
}
mat<N, M, T> transpose() const
{
mat<N, M, T> trans;
vec<M, T> col;
for (unsigned int i = 0; i < N; ++i)
{
get_ith_column(i, col);
trans.set_ith_row(i, col);
}
return trans;
}
protected:
T val[M*N]; // column major
};
typedef mat<3, 3, float> mat3x3f;
typedef mat<3, 3, double> mat3x3d;
typedef mat<3, 3, float> mat3f;
typedef mat<3, 3, double> mat3d;
typedef mat<4, 4, float> mat4x4f;
typedef mat<4, 4, double> mat4x4d;
typedef mat<4, 4, float> mat4f;
typedef mat<4, 4, double> mat4d;
} // math
} // kmuvcl
#include "operator.hpp"
#endif // #ifndef KMUVCL_GRAPHICS_MAT_HPP