-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.h
78 lines (66 loc) · 1.33 KB
/
Utils.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
#ifndef __UTILS_H__
#define __UTILS_H__
#define _USE_MATH_DEFINES
#include <cmath>
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef _WIN32
#define RANDOM_GEN() (float)rand()/RAND_MAX
#define INIT_RANDOM_GEN() srand((unsigned int)time(NULL));
#else
#define RANDOM_GEN() (float)drand48()
#define INIT_RANDOM_GEN() srand48(time(NULL));
#endif
#ifdef _OPENMP
#include <omp.h>
#endif
#ifdef EXECUTION_TIME_COMPUTATION
#include <chrono>
using namespace std::chrono;
#endif
#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
#define PBWIDTH 60
inline
bool
power_of_two(size_t x)
{
return !(x == 0) && !(x & (x - 1));
}
inline
void
printProgress (double percentage)
{
int val = (int) (percentage * 100);
int lpad = (int) (percentage * PBWIDTH);
int rpad = PBWIDTH - lpad;
printf ("\r%3d%% [%.*s%*s]", val, lpad, PBSTR, rpad, "");
fflush (stdout);
}
inline
float
degrees_to_radians(float a)
{
return a*(static_cast<float>(M_PI)/180.0f);
}
template <typename T>
T
lerp(const T& A, const T& B, float t)
{
return (1.0f-t) * A + t * B;
}
template<typename T>
void
safe_delete(T*& ptr)
{
if(ptr) { delete ptr; ptr = nullptr; }
}
template <typename T>
void
safe_array_delete(T*& ptr)
{
if(ptr) { delete[] ptr; ptr = nullptr; }
}
#endif