-
Notifications
You must be signed in to change notification settings - Fork 0
/
maths.h
183 lines (144 loc) · 3.26 KB
/
maths.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef MATHS_H
#define MATHS_H
#include <vector>
#include <iostream>
#define PI 3.14159265
// Vector of length 3
struct vec3 {
double x;
double y;
double z;
};
// Vector of length 4
struct vec4 {
double x;
double y;
double z;
double w;
};
//Ver simple polygon structure used by Object class to store raw polygon data
struct poly {
std::vector<int> verts;
int vert_count;
};
// Axis enum for rotation matrix
enum axis {
kAxisX,
kAxisY,
kAxisZ
};
/*
* Matricies are stored in a row major order
* m[row][column]
*/
class Matrix
{
public:
Matrix();
// Sets matrix to identitiy
void SetIdentity();
// Allows you to manually set the data of a matrix
void SetData(std::vector< std::vector<double> >);
// Functions for creating transform matricies
void SetPerspective(double alpha, double zn, double zf);
void SetTranslate(double x, double y, double z);
void SetScale(double x, double y, double z);
void SetRotation(double theta, axis j);
void PrintMatrix() const;
// Acces the matricies raw data
std::vector< std::vector<double> > data();
/*
* Mupltiply this matrix by T
* m = TC
*/
void Multiply(Matrix T);
static double deg_to_rad(double x) {
double i = 180.0;
return (x * PI) / i;
}
private:
std::vector< std::vector<double> > data_;
};
// static functions
// Multiply a vec4 by a 4x4 matrix
static vec4 TransformVector(vec4 v, Matrix* C) {
struct vec4 output = {0, 0, 0, 1};
output.x = v.x * C->data()[0][0] + v.y * C->data()[1][0] + v.z * C->data()[2][0] + v.w * C->data()[3][0];
output.y = v.x * C->data()[0][1] + v.y * C->data()[1][1] + v.z * C->data()[2][1] + v.w * C->data()[3][1];
output.z = v.x * C->data()[0][2] + v.y * C->data()[1][2] + v.z * C->data()[2][2] + v.w * C->data()[3][2];
output.w = v.x * C->data()[0][3] + v.y * C->data()[1][3] + v.z * C->data()[2][3] + v.w * C->data()[3][3];
return output;
}
// Multiply a vec3 by a 4x4 matrix (setw w to 1.0)
static vec4 TransformVector(vec3 v, Matrix* C)
{
vec4 v4;
v4.x = v.x;
v4.y = v.y;
v4.z = v.z;
v4.w = 1.0;
return TransformVector(v4, C);
}
// Divide a vec4 by w. REturns a vec3
static vec3 DivideByW(vec4 v)
{
vec3 output;
if (v.w != 0) {
output.x = v.x / v.w;
output.y = v.y / v.w;
output.z = v.z / v.w;
} else {
std::cout << "ERROR: DivideByW is divind by 0";
output.x = v.x;
output.y = v.y;
output.z = v.z;
}
return output;
}
static double magnitude(vec4 v)
{
double sum;
sum = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
return sqrt(sum);
}
static double magnitude(vec3 v)
{
vec4 temp = { v.x, v.y, v.z, 0.0 };
return magnitude(temp);;
}
static vec3 Vec3Subtract(vec3 a, vec3 b)
{
vec3 result;
result.x = a.x - b.x;
result.y = a.y - b.y;
result.z = a.z - b.z;
return result;
}
static vec4 Vec4Subtract(vec4 a, vec4 b)
{
vec4 result;
result.x = a.x - b.x;
result.y = a.y - b.y;
result.z = a.z - b.z;
result.w = a.w - b.w;
return result;
}
static void Vec3Print(vec3 v)
{
std::cout << v.x << " " << v.y << " " << v.z << std::endl;
}
static void Vec3Print(vec4 v)
{
vec3 temp = { v.x, v.y, v.z };
Vec3Print(temp);
}
static vec3 Vec3Cross(vec3 a, vec3 b)
{
vec3 result = { 0.0, 0.0, 0.0};
result.x = a.y * b.z - a.z * b.y;
result.y = a.z * b.x - a.x * b.z;
result.z = a.x * b.y - a.y * b.x;
//Vec3Print(result);
return result;
}
#endif