-
Notifications
You must be signed in to change notification settings - Fork 0
/
maths.cpp
140 lines (116 loc) · 4.31 KB
/
maths.cpp
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
#include <iostream>
#include "maths.h"
Matrix::Matrix()
{
data_ = std::vector<std::vector<double>>(4, std::vector<double>(4, 0));
}
void Matrix::SetIdentity()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
data_[i][j] = 0.0;
}
}
data_[0][0] = 1.0;
data_[1][1] = 1.0;
data_[2][2] = 1.0;
data_[3][3] = 1.0;
}
void Matrix::SetData(std::vector< std::vector<double> >data)
{
data_ = data;
}
std::vector< std::vector<double> > Matrix::data()
{
return data_;
}
void Matrix::SetPerspective(double alpha, double zn, double zf)
{
SetIdentity();
double half_alpha = alpha * 0.5;
double s = sin(deg_to_rad(half_alpha));
double c = cos(deg_to_rad(half_alpha));
double Q = s / (1.0 - (zn / zf));
data_[0][0] = c;
data_[1][1] = c;
data_[2][2] = Q;
data_[2][3] = s;
data_[3][2] = -Q * zn;
}
void Matrix::SetTranslate(double x, double y, double z)
{
SetIdentity();
data_[3][0] = x;
data_[3][1] = y;
data_[3][2] = z;
}
void Matrix::SetScale(double x, double y, double z)
{
SetIdentity();
data_[0][0] = x;
data_[1][1] = y;
data_[2][2] = z;
}
void Matrix::SetRotation(double theta, axis j)
{
SetIdentity();
double theta_rad = deg_to_rad(theta);
double s = sin(theta_rad);
double c = cos(theta_rad);
switch (j) {
case axis::kAxisX:
data_[1][1] = c;
data_[1][2] = -s;
data_[2][1] = s;
data_[2][2] = c;
break;
case axis::kAxisY:
data_[0][0] = c;
data_[0][2] = s;
data_[2][0] = -s;
data_[2][2] = c;
break;
case axis::kAxisZ:
data_[0][0] = c;
data_[0][1] = -s;
data_[1][0] = s;
data_[1][1] = c;
break;
default:
std::cout << "ERROR: Invalid axis input" << std::endl;
break;
}
}
void Matrix::Multiply(Matrix T)
{
std::vector<std::vector<double>> temp_data = std::vector<std::vector<double>>(4, std::vector<double>(4, 0));
temp_data[0][0] = data_[0][0] * T.data()[0][0] + data_[1][0] * T.data()[0][1] + data_[2][0] * T.data()[0][2] + data_[3][0] * T.data()[0][3];
temp_data[0][1] = data_[0][1] * T.data()[0][0] + data_[1][1] * T.data()[0][1] + data_[2][1] * T.data()[0][2] + data_[3][1] * T.data()[0][3];
temp_data[0][2] = data_[0][2] * T.data()[0][0] + data_[1][2] * T.data()[0][1] + data_[2][2] * T.data()[0][2] + data_[3][2] * T.data()[0][3];
temp_data[0][3] = data_[0][3] * T.data()[0][0] + data_[1][3] * T.data()[0][1] + data_[2][3] * T.data()[0][2] + data_[3][3] * T.data()[0][3];
temp_data[1][0] = data_[0][0] * T.data()[1][0] + data_[1][0] * T.data()[1][1] + data_[2][0] * T.data()[1][2] + data_[3][0] * T.data()[1][3];
temp_data[1][1] = data_[0][1] * T.data()[1][0] + data_[1][1] * T.data()[1][1] + data_[2][1] * T.data()[1][2] + data_[3][1] * T.data()[1][3];
temp_data[1][2] = data_[0][2] * T.data()[1][0] + data_[1][2] * T.data()[1][1] + data_[2][2] * T.data()[1][2] + data_[3][2] * T.data()[1][3];
temp_data[1][3] = data_[0][3] * T.data()[1][0] + data_[1][3] * T.data()[1][1] + data_[2][3] * T.data()[1][2] + data_[3][3] * T.data()[1][3];
temp_data[2][0] = data_[0][0] * T.data()[2][0] + data_[1][0] * T.data()[2][1] + data_[2][0] * T.data()[2][2] + data_[3][0] * T.data()[2][3];
temp_data[2][1] = data_[0][1] * T.data()[2][0] + data_[1][1] * T.data()[2][1] + data_[2][1] * T.data()[2][2] + data_[3][1] * T.data()[2][3];
temp_data[2][2] = data_[0][2] * T.data()[2][0] + data_[1][2] * T.data()[2][1] + data_[2][2] * T.data()[2][2] + data_[3][2] * T.data()[2][3];
temp_data[2][3] = data_[0][3] * T.data()[2][0] + data_[1][3] * T.data()[2][1] + data_[2][3] * T.data()[2][2] + data_[3][3] * T.data()[2][3];
temp_data[3][0] = data_[0][0] * T.data()[3][0] + data_[1][0] * T.data()[3][1] + data_[2][0] * T.data()[3][2] + data_[3][0] * T.data()[3][3];
temp_data[3][1] = data_[0][1] * T.data()[3][0] + data_[1][1] * T.data()[3][1] + data_[2][1] * T.data()[3][2] + data_[3][1] * T.data()[3][3];
temp_data[3][2] = data_[0][2] * T.data()[3][0] + data_[1][2] * T.data()[3][1] + data_[2][2] * T.data()[3][2] + data_[3][2] * T.data()[3][3];
temp_data[3][3] = data_[0][3] * T.data()[3][0] + data_[1][3] * T.data()[3][1] + data_[2][3] * T.data()[3][2] + data_[3][3] * T.data()[3][3];
data_ = temp_data;
}
void Matrix::PrintMatrix() const
{
for (int i = 0; i < 4; i++) {
std::cout << "|";
std::cout << data_[i][0] << ", ";
std::cout << data_[i][1] << ", ";
std::cout << data_[i][2] << ", ";
std::cout << data_[i][3];
std::cout << "|" << std::endl;
}
std::cout << std::endl;
}