-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.hpp
More file actions
380 lines (356 loc) · 8.94 KB
/
Copy pathcommon.hpp
File metadata and controls
380 lines (356 loc) · 8.94 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#pragma once
#include <string>
#include <algorithm>
//using namespace std;
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062f
#define PI2 PI*2.f;
struct vec3;
typedef vec3 point3;
struct vec2;
typedef vec2 point2;
struct line;
struct plane;
struct intersection;
struct colRGB;
//Vectors
struct vec3
{
public:
vec3();
vec3(float x, float y, float z);
float x;
float y;
float z;
inline friend vec3 operator*(float factor, const vec3& rightRef){
return vec3(rightRef.x * factor, rightRef.y * factor, rightRef.z*factor);
}
//methods
inline float Dot(const vec3& otherRef) const{
return x * otherRef.x + y * otherRef.y + z*otherRef.z;
}
inline float Length() const{
return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
}
inline float Angle(const vec3& otherRef) const{
return acos((vec3(x, y, z).Dot(otherRef)) / (vec3(x, y, z).Length() / otherRef.Length()));
}
inline vec3 Norm(float epsilon = 0.001) const{
if (Length() < epsilon)return vec3(0, 0, 0);
else return vec3(x, y, z) / Length();
}
inline vec3 Cross(const vec3& otherRef) const{
return vec3(y * otherRef.z - z * otherRef.y, z * otherRef.x - x * otherRef.z, x * otherRef.y - y * otherRef.x);
}
inline std::string ToString() const{
using namespace std;
return string("x: ") + to_string(x) + string(", y: ") + to_string(y) + string(", z: ") + to_string(z);
}
//Operators
inline vec3& operator= (const vec3& otherRef){
if (&otherRef != this){
x = otherRef.x;
y = otherRef.y;
z = otherRef.z;
}
return *this;
}
inline vec3 operator+ (const vec3& otherRef) const{
return vec3(x + otherRef.x, y + otherRef.y, z + otherRef.z);
}
inline vec3 operator- (const vec3& otherRef) const{
return vec3(x - otherRef.x, y - otherRef.y, z - otherRef.z);
}
inline vec3 operator- () const{
return vec3(-x, -y, -z);
}
inline vec3 operator+ () const{
return *this;
}
inline vec3 operator* (float factor) const{
return vec3(x * factor, y * factor, z*factor);
}
inline vec3 operator/ (float divisor) const{
return vec3(x / divisor, y / divisor, z / divisor);
}
inline vec3& operator+=(const vec3& otherRef){
x += otherRef.x;
y += otherRef.y;
z += otherRef.z;
return *this;
}
inline vec3& operator-=(const vec3& otherRef){
x -= otherRef.x;
y -= otherRef.y;
z -= otherRef.z;
return *this;
}
inline vec3& operator*=(float factor){
x *= factor;
y *= factor;
z *= factor;
return *this;
}
inline vec3& operator/=(float divisor){
x /= divisor;
y /= divisor;
z /= divisor;
return *this;
}
inline bool operator==(const vec3& otherRef) const{
return x == otherRef.x && y == otherRef.y && z == otherRef.z;
}
inline bool operator!=(const vec3& otherRef) const{
return !(*this == otherRef);
}
};
struct vec2
{
public:
vec2();
vec2(float x, float y);
float x;
float y;
inline friend vec2 operator*(float factor, const vec2& rightRef){
return vec2(rightRef.x * factor, rightRef.y * factor);
}
//methods
inline float Dot(const vec2& otherRef) const{
return x * otherRef.x + y * otherRef.y;
}
inline float Length() const{
return sqrt(pow(x, 2) + pow(y, 2));
}
inline float Angle(const vec2& otherRef) const{
return acos((vec2(x, y).Dot(otherRef)) / (vec2(x, y).Length() / otherRef.Length()));
}
inline vec2 Norm(float epsilon = 0.001) const{
if (Length() < epsilon)return vec2(0, 0);
else return vec2(x, y) / Length();
}
inline vec2 Orthagonal() const{
return vec2(-y, x);
}
inline std::string ToString() const{
using namespace std;
return string("x: ") + to_string(x) + string(", y: ") + to_string(y);
}
inline vec2 correctUV() const{
vec2 ret = vec2(x, y);
if (x > 1){
int intX = (int)x;
ret.x = x - intX;
}
if (x < 0){
int intX = (int)x;
ret.x = -(x - intX);
}
if (y > 1){
int intY = (int)y;
ret.y = y - intY;
}
if (y < 0){
int intY = (int)y;
ret.y = -(y - intY);
}
return ret;
}
//Operators
inline vec2& operator= (const vec2& otherRef){
if (&otherRef != this){
x = otherRef.x;
y = otherRef.y;
}
return *this;
}
inline vec2 operator+ (const vec2& otherRef) const{
return vec2(x + otherRef.x, y + otherRef.y);
}
inline vec2 operator- (const vec2& otherRef) const{
return vec2(x - otherRef.x, y - otherRef.y);
}
inline vec2 operator- () const{
return vec2(-x, -y);
}
inline vec2 operator+ () const{
return *this;
}
inline vec2 operator* (float factor) const{
return vec2(x * factor, y * factor);
}
inline vec2 operator/ (float divisor) const{
return vec2(x / divisor, y / divisor);
}
inline vec2& operator+=(const vec2& otherRef){
x += otherRef.x;
y += otherRef.y;
return *this;
}
inline vec2& operator-=(const vec2& otherRef){
x -= otherRef.x;
y -= otherRef.y;
return *this;
}
inline vec2& operator*=(float factor){
x *= factor;
y *= factor;
return *this;
}
inline vec2& operator/=(float divisor){
x /= divisor;
y /= divisor;
return *this;
}
inline bool operator==(const vec2& otherRef) const{
return x == otherRef.x && y == otherRef.y;
}
inline bool operator!=(const vec2& otherRef) const{
return !(*this == otherRef);
}
};
struct line{
line();
line(point3, vec3);
point3 orig;
vec3 dir;
};
struct intersection{
intersection();
intersection(bool, point3, float);
bool hit;
point3 p;
float t;
bool backfacing = false;
};
struct plane{
plane();
plane(vec3, float);
plane(point3, point3, point3);
vec3 n;
float d;
inline intersection lineIts(line ln){
intersection ret(false, point3(0, 0, 0), 0);
float nd = n.Dot(ln.dir);
if (!(nd == 0)){
ret.t = (d - n.Dot(ln.orig)) / nd;
ret.hit = true;
}
return ret;
}
inline intersection rayIts(line ln, bool bfc){
intersection ret(false, point3(0, 0, 0), 0);
float nd = n.Dot(ln.dir);
if (nd<0){
ret.t = (d - n.Dot(ln.orig)) / nd;
ret.hit = true;
ret.backfacing = false;
}
else if (nd>0 && bfc == false){
ret.t = (d - n.Dot(ln.orig)) / nd;
ret.hit = true;
ret.backfacing = true;
}
return ret;
}
};
struct colRGB
{
public:
colRGB();
colRGB(float red, float green, float blue);
float red;
float green;
float blue;
inline std::string ToString() const{
using namespace std;
return string("red: ") + to_string(red) + string(", green: ") + to_string(green) + string(", blue: ") + to_string(blue);
}
inline friend colRGB operator*(float factor, const colRGB& rightRef){
return colRGB(rightRef.red * factor, rightRef.green * factor, rightRef.blue*factor);
}
inline colRGB& operator= (const colRGB& otherRef){
if (&otherRef != this){
red = otherRef.red;
green = otherRef.green;
blue = otherRef.blue;
}
return *this;
}
inline colRGB operator+ (const colRGB& otherRef) const{
return colRGB(red + otherRef.red, green + otherRef.green, blue + otherRef.blue);
}
inline colRGB operator- (const colRGB& otherRef) const{
return colRGB(red - otherRef.red, green - otherRef.green, blue - otherRef.blue);
}
inline colRGB operator- () const{
return colRGB(-red, -green, -blue);
}
inline colRGB operator+ () const{
return *this;
}
inline colRGB operator* (float factor) const{
return colRGB(red * factor, green * factor, blue*factor);
}
inline colRGB operator* (const colRGB& otherRef) const{
return colRGB(((red)*(otherRef.red)),
((green)*(otherRef.green)),
((blue)*(otherRef.blue)));
}
inline colRGB operator/ (float divisor) const{
return colRGB(red / divisor, green / divisor, blue / divisor);
}
inline colRGB& operator+=(const colRGB& otherRef){
red += otherRef.red;
green += otherRef.green;
blue += otherRef.blue;
return *this;
}
inline colRGB& operator-=(const colRGB& otherRef){
red -= otherRef.red;
green -= otherRef.green;
blue -= otherRef.blue;
return *this;
}
inline colRGB& operator*=(float factor){
red *= factor;
green *= factor;
blue *= factor;
return *this;
}
inline colRGB& operator*=(const colRGB& otherRef){
red = ((red)*(otherRef.red));
green = ((green)*(otherRef.green));
blue = ((blue)*(otherRef.blue));
return *this;
}
inline colRGB& operator/=(float divisor){
red /= divisor;
green /= divisor;
blue /= divisor;
return *this;
}
inline bool operator==(const colRGB& otherRef) const{
return red == otherRef.red && green == otherRef.green && blue == otherRef.blue;
}
inline bool operator!=(const colRGB& otherRef) const{
return !(*this == otherRef);
}
};
namespace LW_Util{
using namespace std;
inline std::string getTimeFromFloat(float time)
{
int seconds = (int)time;
int minutes = (int)(time / 60.f);
seconds -= minutes * 60;
int hours = (int)(time / 3600.f);
minutes -= hours * 60;
string hoursString = hours > 9 ? to_string(hours) : '0' + to_string(hours);
string minutesString = minutes > 9 ? to_string(minutes) : '0' + to_string(minutes);
string secondsString = seconds > 9 ? to_string(seconds) : '0' + to_string(seconds);
if (hours > 0)
{
return hoursString + ":" + minutesString + ":" + secondsString;
}
else return minutesString + ":" + secondsString;
}
}