-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.h
More file actions
236 lines (201 loc) · 5.08 KB
/
Utilities.h
File metadata and controls
236 lines (201 loc) · 5.08 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
#pragma once
#include <chrono>
#include <string>
#include <random>
#include <vector>
#include <boost/circular_buffer.hpp>
#include <numeric>
#include <D:\Programming\CPP\Sources\fmt-master11.1.1\include\fmt\core.h>
#include <D:\Programming\CPP\Sources\fmt-master11.1.1\include\fmt\ranges.h>
#include <D:\Programming\CPP\Sources\fmt-master11.1.1\include\fmt\format.h>
#ifdef BUILD_CONFIG_DEBUG
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\fmt11.1.1\\Debug\\fmtd.lib")
#elif BUILD_CONFIG_PROFILE
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\fmt11.1.1\\RelWithDebInfo\\fmt.lib")
#elif BUILD_CONFIG_RELEASE
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\fmt11.1.1\\Release\\fmt.lib")
#endif
#define NEWL std::string("\n")
using std::string;
using std::vector;
using std::chrono::high_resolution_clock;
using std::chrono::nanoseconds;
using std::chrono::microseconds;
using std::chrono::duration_cast;
using fmt::print;
using fmt::format;
using fmt::runtime;
class Benchmark_Timer
{
private:
high_resolution_clock clock;
std::chrono::time_point<high_resolution_clock> benchmark_start_time;
public:
void benchmark_start()
{
benchmark_start_time = clock.now();
}
void benchmark_end(string label) const
{
auto elapsed = duration_cast<microseconds>(clock.now() - benchmark_start_time);
print(runtime(label + " - time (us): {}" + NEWL), elapsed.count());
}
};
class Stopwatch
{
private:
high_resolution_clock clock;
std::chrono::time_point<high_resolution_clock> start_time;
public:
Stopwatch() : start_time(clock.now())
{}
nanoseconds get_elapsed_time() const
{
return (clock.now() - start_time);
}
nanoseconds restart()
{
std::chrono::time_point<high_resolution_clock> now = clock.now();
nanoseconds elapsed = now - start_time;
start_time = now;
return elapsed;
}
};
class Moving_average_timer
{
public:
Stopwatch stopwatch;
boost::circular_buffer<nanoseconds> buffer{100};
void start()
{
stopwatch.restart();
}
void end()
{
buffer.push_back(stopwatch.restart());
}
nanoseconds get_moving_average()
{
nanoseconds sum = std::accumulate(buffer.begin(), buffer.end(), nanoseconds());
return sum / buffer.size();
}
};
string bool_to_string(bool bool_arg)
{
if (bool_arg)
{
return "true";
}
else
{
return "false";
}
}
//Math
constexpr double PI = 3.1415926535897932384;
constexpr double TAU = 6.2831853071795864769;
std::minstd_rand minstd_generator;
//including min and max
template <typename T>
T get_random_int_in_range(const T min, const T max)
{
std::uniform_int_distribution<T> distribution(min, max);
return distribution(minstd_generator);
}
//including min, excluding max
template <typename T>
T get_random_real_in_range(const T min, const T max)
{
std::uniform_real_distribution<T> distribution(min, max);
return distribution(minstd_generator);
}
//angles in degrees, from 0 to 360
template <typename T>
T angle_difference_deg(const T ang1, const T ang2)
{
T diff = ang1 - ang2;
if (diff > 180)
{
diff -= 360;
}
if (diff < -180)
{
diff += 360;
}
return diff;
}
//restricts an angle in radians to the interval from 0 to TAU
double normalize_angle_rad(double angle)
{
double int_part;
double fract_part = modf(angle / TAU, &int_part);
double effective_rotation = fract_part * TAU;
if (effective_rotation >= 0)
{
return effective_rotation;
}
else //below 0
{
return TAU + effective_rotation;
}
}
//restricts an angle in degrees to the interval from 0 to 360
double normalize_angle_deg(double angle)
{
double int_part;
double fract_part = modf(angle / 360.0, &int_part);
double effective_rotation = fract_part * 360.0;
if (effective_rotation >= 0)
{
return effective_rotation;
}
else //below 0
{
return 360.0 + effective_rotation;
}
}
//angles in radians, from 0 to TAU
template <typename T>
T angle_difference_rad(const T ang1, const T ang2)
{
T diff = ang1 - ang2;
if (diff > PI)
{
diff -= TAU;
}
if (diff < -PI)
{
diff += TAU;
}
return diff;
}
constexpr double rad_to_deg(const double rad)
{
return rad * (180.0 / PI);
}
constexpr double deg_to_rad(const double deg)
{
return deg / (180.0 / PI);
}
// Custom assert providing more information than is typically given in C's assert function:
#ifndef NDEBUG
#define custom_assert(condition, message) \
{ \
if (not(condition)) \
{ \
std::cout << std::endl << "Assertion `" #condition "` failed in " << __FILE__ << ", line " << __LINE__ << ". " << std::endl << message << std::endl; \
std::cout.flush(); \
} \
}
#else
#define custom_assert(condition, message) { }
#endif
// Like a reverse-assert, but doesn't get turned off in non-debug compile modes:
#define custom_fail_if(condition, message) \
{ \
if (condition) \
{ \
std::cout << std::endl << "Aborting because `" #condition "` in " << __FILE__ << ", line " << __LINE__ << ". " << std::endl << message << std::endl; \
std::cout.flush(); \
} \
}