-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.cpp
160 lines (130 loc) · 3.39 KB
/
math.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include "math.hpp"
using namespace std;
Vec3f::Vec3f(float f) {
x_ = f;
y_ = f;
z_ = f;
}
Vec3f::Vec3f(float x, float y, float z) {
x_ = x;
y_ = y;
z_ = z;
}
void Vec3f::print() {
cout << x_ << " " << y_ << " " << z_ << endl;
}
Vec3f Vec3f::add(const Vec3f vec) {
return Vec3f(vec.x_ + x_, vec.y_ + y_, vec.z_ + z_);
}
Vec3f Vec3f::operator *(const float val) {
return Vec3f(x_ * val, y_ * val, z_ * val);
}
Vec4f::Vec4f(float f) {
x_ = f;
y_ = f;
z_ = f;
w_ = f;
}
Vec4f::Vec4f(float x, float y, float z, float w) {
x_ = x;
y_ = y;
z_ = z;
w_ = w;
}
void Vec4f::print() {
cout << x_ << " " << y_ << " " << z_ << " " << w_ << endl;
}
Vec4f Vec4f::operator +(const Vec4f vec) {
return Vec4f(vec.x_ + x_, vec.y_ + y_, vec.z_ + z_, vec.w_ + w_);
}
Vec4f Vec4f::operator *(const float f) {
return Vec4f(x_ * f, y_ * f, z_ * f, w_ * f);
}
void Vec4f::operator =(const Vec4f vec) {
x_ = vec.x_;
y_ = vec.y_;
z_ = vec.z_;
w_ = vec.w_;
}
float Vec4f::dot(const Vec4f vec) {
return x_ * vec.x_ + y_ * vec.y_ + z_ * vec.z_ + w_ * vec.w_;
}
float Vec4f::get(const unsigned i) {
switch(i) {
case 0: return x_;
case 1: return y_;
case 2: return z_;
case 3: return w_;
default:
return -1;
}
}
Mat4f::Mat4f() {
for (unsigned i = 0; i < 4; i++) {
vector <float> row(4, 0);
row[i] = 1.0;
mat_.push_back(row);
}
}
Mat4f::Mat4f(float x) {
for (unsigned i = 0; i < 4; i++)
mat_.push_back(vector<float> (4, x));
}
void Mat4f::print() {
for (unsigned i = 0; i < 4; i++) {
for (unsigned j = 0; j < 4; j++)
cout << mat_[i][j] << "\t";
cout << endl;
}
}
Mat4f Mat4f::operator +(Mat4f mat) {
Mat4f newMat;
for (unsigned i = 0; i < 4; i++)
for (unsigned j = 0; j < 4; j++)
newMat.set(i, j, get(i, j) + mat.get(i, j));
return newMat;
}
Mat4f Mat4f:: operator *(const float val) {
Mat4f newMat;
for (unsigned i = 0; i < 4; i++)
for (unsigned j = 0; j < 4; j++)
newMat.set(i, j, get(i, j) * val);
return newMat;
}
Vec4f Mat4f::operator *(const Vec4f vec) {
return Vec4f(getRow(0).dot(vec), getRow(1).dot(vec), getRow(2).dot(vec), getRow(3).dot(vec));
}
Mat4f Mat4f::operator *(Mat4f mat) {
Mat4f newMat = Mat4f() * 0.0;
for (unsigned i = 0; i < 4; i++)
for (unsigned j = 0; j < 4; j++)
for (unsigned k = 0; k < 4; k++)
newMat.set(i, j, newMat.get(i, j) + get(i, k) * mat.get(k, j));
return newMat;
}
void Mat4f::operator =(Mat4f mat) {
for (unsigned i = 0; i < 4; i++)
for (unsigned j = 0; j < 4; j++)
mat_[i][j] = mat.get(i, j);
}
Vec4f Mat4f::getRow(const unsigned i) {
return Vec4f(mat_[i][0], mat_[i][1], mat_[i][2], mat_[i][3]);
}
Vec4f Mat4f::getCol(const unsigned j) {
return Vec4f(mat_[0][j], mat_[1][j], mat_[2][j], mat_[3][j]);
}
float Mat4f::get(const unsigned i, const unsigned j) {
return mat_[i][j];
}
void Mat4f::setRow(const unsigned i, Vec4f vec) {
for (unsigned c = 0; c < 4; c++)
set(i, c, vec.get(c));
}
void Mat4f::setCol(const unsigned j, Vec4f vec) {
for (unsigned c = 0; c < 4; c++)
set(c, j, vec.get(c));
}
void Mat4f::set(const unsigned i, const unsigned j, const float val) {
mat_[i][j] = val;
}