-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathlib.h
More file actions
479 lines (379 loc) · 7.25 KB
/
Mathlib.h
File metadata and controls
479 lines (379 loc) · 7.25 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#pragma once
#include <math.h>
//#include <cmath>
#include <assert.h>
//#include <directxmath.h>
//using namespace DirectX;
#include "external/glm/glm.hpp"
#include "external/glm/gtc/quaternion.hpp"
#include <algorithm> // std::swap
#include <stdlib.h> // rand()
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef char int8;
typedef short int16;
typedef int int32;
#define PI 3.14159265f
#define ARRLEN(a) (sizeof(a) / sizeof(a[0]))
inline float sqr(float x)
{
return x * x;
}
inline float ConvertToRadians(float degrees)
{
return degrees / 180 * PI;
}
#define MAX_RESOURCE_VIEW_CACHE_SIZE 8
template<class T>
void SafeRelease(T*& p)
{
if (p)
{
p->Release();
p = 0;
}
}
inline float frac(float x)
{
return x - floorf(x);
}
inline double frac(double x)
{
return x - floor(x);
}
inline uint32 DivideAndRoundUp(uint32 Value, uint32 Div)
{
return (Value + Div - 1) / Div;
}
struct TNoSafeRelease
{
template <class T>
static void DoRelease(T* Ptr)
{
assert(Ptr);
}
template <class T>
static void DoAddRef(T* Ptr)
{
assert(Ptr);
}
};
struct TSafeRelease
{
template <class T>
static void DoRelease(T* Ptr)
{
assert(Ptr);
Ptr->Release();
}
template <class T>
static void DoAddRef(T* Ptr)
{
assert(Ptr);
Ptr->AddRef();
}
};
// simple pointer wrapper
// zero init on construction
// @param TTrait = TSafeRelease / TNoSafeRelease
template <class T, class TTrait = TNoSafeRelease>
class Pointer
{
public:
// default constructor
Pointer() :Ptr(0)
{
}
// 0 constructor
// Pointer(int Null) :Ptr(0)
// {
// assert(!Null); // todo: compile time error
// }
// constructor
// Pointer(T *ref) :Ptr(ref)
// {
// }
// copy constructor
Pointer(const Pointer<T, TTrait>& ref) :Ptr(ref.Get())
{
if (Ptr)
{
TTrait::DoAddRef(Ptr);
}
}
// copy constructor
template<class Derived>
Pointer(const Pointer<Derived, TTrait> ref) :Ptr(ref.Get())
{
}
// destructor
~Pointer()
{
Release();
}
// assignment operator
template<class Derived>
Pointer<T, TTrait> &operator=(const Pointer<Derived, TTrait> ref)
{
if (this != &ref) // protect against invalid self-assignment
{
Ptr = ref.Get();
}
return *this;
}
// assignment operator
Pointer<T, TTrait> &operator=(T* ptr)
{
Release();
Ptr = ptr;
return *this;
}
// assignment operator
Pointer<T, TTrait> &operator=(int Null)
{
assert(Null == 0);
Release();
return *this;
}
T& operator*()
{
return *Ptr;
}
// doesn't work with void type
const T& operator*() const
{
return *Ptr;
}
T* operator->()
{
return Ptr;
}
const T* operator->() const
{
return Ptr;
}
// dangerous? Maybe better we go though Get()
T** operator&()
{
return &Ptr;
}
// dangerous?
operator T*()
{
return Ptr;
}
T* Get() const
{
return Ptr;
}
T* &Get()
{
return Ptr;
}
operator bool() const
{
return Ptr != 0;
}
/* bool operator ==(const Pointer<T> &ref) const
{
return Ptr == ref.Ptr;
}
bool operator ==(const T* ref) const
{
return Ptr == ref;
}
bool operator !=(const Pointer<T> &ref) const
{
return !(*this == ref);
}
bool operator !=(const T* ref) const
{
return !(*this == ref);
}*/
bool operator!() const
{
return Ptr == 0;
}
void Release()
{
if (Ptr)
{
TTrait::DoRelease(Ptr);
Ptr = 0;
}
}
private:
T * Ptr;
};
template <class T>
class TInitZero
{
public:
TInitZero()
: Value((T)0)
{
}
TInitZero(const T& InValue)
: Value(InValue)
{
}
/*
// assignment operator
TInitZero<T> &operator=(const T& in)
{
Value = in;
return *this;
}
*/
operator const T() const
{
return Value;
}
/*
const T& T::operator++()
{
++itsVal;
return *this;
}
const T T::operator++(int)
{
T temp(*this);
++itsVal;
return temp;
}
*/
T operator ->() const
{
return Value;
}
// dangerous to expose? needed for ++Value
operator T& ()
{
return Value;
}
T& GetRef()
{
return Value;
}
// dangerous? Maybe better we go though Get()
T* operator&()
{
return &Value;
}
private:
T Value;
};
template<class T, class TTrait>
void SafeRelease(Pointer<T, TTrait>& p)
{
if (p)
{
p->Release();
p = 0;
}
};
inline float frand()
{
return rand() / (float)RAND_MAX;
}
// @return 0 .. Count-1
inline uint32 randInt(uint32 Count)
{
uint32 Ret = (uint32)(uint64(rand() * Count) / RAND_MAX);
assert(Ret < Count);
return Ret;
}
inline float frand2()
{
return frand() * 2 - 1.0f;
}
#include <stdio.h> // FILE
#include <fcntl.h> // filesize
#include <io.h> // filesize
#include <string.h> // strlen
#pragma warning( disable : 4996 )
// ---------------------------------------------------------------------------------------------------------
inline float saturate(float x)
{
if (x < 0)
return 0;
if (x > 1)
return 1;
return x;
}
/*
// from: http://stackoverflow.com/questions/19301538/converting-tchar-to-char
// multi byte to wide char:
inline std::wstring s2ws(const std::string& str)
{
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
// wide char to multi byte:
inline std::string ws2s(const std::wstring& wstr)
{
int size_needed = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), int(wstr.length() + 1), 0, 0, 0, 0);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), int(wstr.length() + 1), &strTo[0], size_needed, 0, 0);
return strTo;
}
*/
// ported from HLSL, should be moved in MathUtils
// public domain: https://github.com/elfrank/raytracer-gpupro4/blob/master/RayTracerCS/Shaders/Core/Intersection.hlsl
// Ray-Box Intersection Test
// @return hit is if Ret.y >= 0
inline glm::vec2 IntersectBox(glm::vec3 RayStart, glm::vec3 RayInvDir, glm::vec3 BBoxMin, glm::vec3 BBoxMax)
{
glm::vec2 Ret(0, 0);
glm::vec3 DiffMax = BBoxMax - RayStart;
glm::vec3 DiffMin = BBoxMin - RayStart;
DiffMax *= RayInvDir;
DiffMin *= RayInvDir;
Ret[0] = glm::min(DiffMin.x, DiffMax.x);
Ret[1] = glm::max(DiffMin.x, DiffMax.x);
Ret[0] = glm::max(Ret[0], glm::min(DiffMin.y, DiffMax.y));
Ret[1] = glm::min(Ret[1], glm::max(DiffMin.y, DiffMax.y));
Ret[0] = glm::max(Ret[0], glm::min(DiffMin.z, DiffMax.z));
Ret[1] = glm::min(Ret[1], glm::max(DiffMin.z, DiffMax.z));
//empty interval
if (Ret[0] > Ret[1])
{
// T[1] = -1.0f; // not really faster but would work with the test we do later (I guess compiler optimizes it out anyway)
Ret[0] = Ret[1] = -1.0f;
}
return Ret;
}
// input vector don't have to be normalized
// can be optimized
inline void GetOtherBaseVec(glm::vec3& outU, glm::vec3& outV, const glm::vec3& n)
{
// normalized input allows stable asserts
glm::vec3 v = glm::normalize(n);
if (v.z < -0.5f || v.z > 0.5f)
{
outU.x = v.z;
outU.y = v.y;
outU.z = -v.x;
}
else
{
outU.x = v.y;
outU.y = -v.x;
outU.z = v.z;
}
outV = glm::normalize(cross(v, outU));
outU = glm::normalize(cross(outV, v));
// assert(fabs(a*b)<0.01f);
// assert(fabs((*this)*a)<0.01f);
// assert(fabs((*this)*b)<0.01f);
}
// 0: no key
// 38: up
// 40: down
typedef int32 CKey;
// like HLSL
typedef unsigned int uint;