-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.hpp
154 lines (121 loc) · 3.43 KB
/
operator.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef KMUVCL_GRAPHICS_OPERATOR_HPP
#define KMUVCL_GRAPHICS_OPERATOR_HPP
#include "vec.hpp"
#include "mat.hpp"
namespace kmuvcl {
namespace math {
/// w_n = u_n + v_n
template <unsigned int N, typename T>
vec<N, T> operator+ (const vec<N, T>& u, const vec<N, T>& v)
{
vec<N, T> w;
for (unsigned int i = 0; i < N; ++i)
w(i) = u(i) + v(i);
return w;
}
/// w_n = u_n - v_n
template <unsigned int N, typename T>
vec<N, T> operator- (const vec<N, T>& u, const vec<N, T>& v)
{
vec<N, T> w;
for (unsigned int i = 0; i < N; ++i)
w(i) = u(i) - v(i);
return w;
}
/// y_n = s * x_n
template <unsigned int N, typename T>
vec<N, T> operator* (const T s, const vec<N, T>& x)
{
vec<N, T> y;
for (unsigned int i = 0; i < N; ++i)
y(i) = s*x(i);
return y;
}
/// s = u_n * v_n (dot product)
template <unsigned int N, typename T>
T dot(const vec<N, T>& u, const vec<N, T>& v)
{
T val = 0;
for (unsigned int i = 0; i < N; ++i)
val += u(i) * v(i);
return val;
}
/// w_3 = u_3 x v_3 (cross product, only for vec3)
template <typename T>
vec<3,T> cross(const vec<3, T>& u, const vec<3, T>& v)
{
vec<3, T> w;
w(0) = u(1)*v(2) - u(2)*v(1);
w(1) = u(2)*v(0) - u(0)*v(2);
w(2) = u(0)*v(1) - u(1)*v(0);
return w;
}
/// y_m = A_{mxn} * x_n
template <unsigned int M, unsigned int N, typename T>
vec<M, T> operator* (const mat<M, N, T>& A, const vec<N, T>& x)
{
vec<M, T> y, col;
for (unsigned int i = 0; i < N; ++i)
{
A.get_ith_column(i, col);
y += x(i)*col;
}
return y;
}
/// y_n = x_m * A_{mxn}
template <unsigned int M, unsigned int N, typename T>
vec<N, T> operator* (const vec<M, T>& x, const mat<M, N, T>& A)
{
vec<N, T> y;
vec<M, T> col;
for (unsigned int i = 0; i < N; ++i)
{
A.get_ith_column(i, col);
y(i) = dot(x, col);
}
return y;
}
/// C_{mxl} = A_{mxn} * B_{nxl}
template <unsigned int M, unsigned int N, unsigned int L, typename T>
mat<M, L, T> operator* (const mat<M, N, T>& A, const mat<N, L, T>& B)
{
mat<M, L, T> C;
vec<N, T> row;
vec<N, T> col;
for (unsigned int i = 0; i < M; ++i)
{
A.get_ith_row(i, row);
for (unsigned int j = 0; j < L; ++j)
{
B.get_ith_column(j, col);
C(i,j) = dot(row, col);
}
}
return C;
}
/// ostream for vec class
template <unsigned int N, typename T>
std::ostream& operator << (std::ostream& os, const vec<N, T>& v)
{
os << "[";
for (unsigned int i = 0; i < N - 1; ++i)
os << v(i) << ", ";
os << v(N - 1);
os << "]";
return os;
}
/// ostream for mat class
template <unsigned int M, unsigned int N, typename T>
std::ostream& operator << (std::ostream& os, const mat<M, N, T>& A)
{
vec<N, T> row;
for (unsigned int i = 0; i < M; ++i)
{
A.get_ith_row(i, row);
os << row << std::endl;
}
return os;
}
} // math
} // kmuvcl
#endif // KMUVCL_GRAPHICS_OPERATOR_HPP