Repository navigation
Expand file tree
/
Copy pathCommonUtils.cpp
More file actions
295 lines (254 loc) · 8 KB
/
Copy pathCommonUtils.cpp
File metadata and controls
295 lines (254 loc) · 8 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
#include "CommonUtils.h"
#include <string>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <glm/gtx/quaternion.hpp>
#include "ErrorHandling.h"
glm::vec4 gSolidColours[16] =
{
{0.0f, 0.0f, 0.0f, 1.0f}, // BLACK
{0.0f, 0.0f, 0.67f, 1.0f}, // BLUE
{0.0f, 0.67f, 0.0f, 1.0f}, // GREEN
{0.0f, 0.67f, 0.67f, 1.0f}, // CYAN
{0.67f, 0.0f, 0.0f, 1.0f}, // RED
{0.67f, 0.0f, 0.67f, 1.0f}, // MAGENTA
{0.67f, 0.67f, 0.0f, 1.0f}, // YELLOW
{0.67f, 0.67f, 0.67f, 1.0f}, // WHITE
{0.33f, 0.33f, 0.33f, 1.0f}, // BRIGHT BLACK
{0.33f, 0.33f, 1.0f, 1.0f}, // BRIGHT BLUE
{0.33f, 1.0f, 0.33f, 1.0f}, // BRIGHT GREEN
{0.33f, 1.0f, 1.0f, 1.0f}, // BRIGHT CYAN
{1.0f, 0.33f, 0.33f, 1.0f}, // BRIGHT RED
{1.0f, 0.33f, 1.0f, 1.0f}, // BRIGHT MAGENTA
{1.0f, 1.0f, 0.33f, 1.0f}, // BRIGHT YELLOW
{1.0f, 1.0f, 1.0f, 1.0f} // BRIGHT WHITE
};
OWUtils::SolidColours OWUtils::colour(const std::string& solidColourName)
{
std::string upper = solidColourName;
std::transform(upper.begin(), upper.end(), upper.begin(),
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
if (upper == "BLACK") return OWUtils::SolidColours::BLACK;
if (upper == "BLUE") return OWUtils::SolidColours::BLUE;
if (upper == "BRIGHT_BLACK") return OWUtils::SolidColours::BRIGHT_BLACK;
if (upper == "BRIGHT_BLUE") return OWUtils::SolidColours::BRIGHT_BLUE;
if (upper == "BRIGHT_CYAN") return OWUtils::SolidColours::BRIGHT_CYAN;
if (upper == "BRIGHT_GREEN") return OWUtils::SolidColours::BRIGHT_GREEN;
if (upper == "BRIGHT_RED") return OWUtils::SolidColours::BRIGHT_RED;
if (upper == "BRIGHT_MAGENTA") return OWUtils::SolidColours::BRIGHT_MAGENTA;
if (upper == "BRIGHT_WHITE") return OWUtils::SolidColours::BRIGHT_WHITE;
if (upper == "BRIGHT_YELLOW") return OWUtils::SolidColours::BRIGHT_YELLOW;
if (upper == "CYAN") return OWUtils::SolidColours::CYAN;
if (upper == "GREEN") return OWUtils::SolidColours::GREEN;
if (upper == "MAGENTA") return OWUtils::SolidColours::MAGENTA;
if (upper == "RED") return OWUtils::SolidColours::RED;
if (upper == "WHITE") return OWUtils::SolidColours::WHITE;
if (upper == "YELLOW") return OWUtils::SolidColours::YELLOW;
return OWUtils::SolidColours::UNKNOWN;
}
#include<cstdlib>
#include<ctime>
static std::vector<glm::vec4> gAllSolidColours =
{
OWUtils::colour(OWUtils::SolidColours::BLACK),
OWUtils::colour(OWUtils::SolidColours::BLUE),
OWUtils::colour(OWUtils::SolidColours::BRIGHT_BLACK),
OWUtils::colour(OWUtils::SolidColours::BRIGHT_BLUE),
OWUtils::colour(OWUtils::SolidColours::BRIGHT_CYAN),
OWUtils::colour(OWUtils::SolidColours::BRIGHT_GREEN),
OWUtils::colour(OWUtils::SolidColours::BRIGHT_RED),
OWUtils::colour(OWUtils::SolidColours::BRIGHT_MAGENTA),
OWUtils::colour(OWUtils::SolidColours::BRIGHT_WHITE),
OWUtils::colour(OWUtils::SolidColours::BRIGHT_YELLOW),
OWUtils::colour(OWUtils::SolidColours::CYAN),
OWUtils::colour(OWUtils::SolidColours::GREEN),
OWUtils::colour(OWUtils::SolidColours::MAGENTA),
OWUtils::colour(OWUtils::SolidColours::RED),
OWUtils::colour(OWUtils::SolidColours::WHITE),
OWUtils::colour(OWUtils::SolidColours::YELLOW)
};
std::vector<glm::vec4> OWUtils::allSolidColours()
{
return gAllSolidColours;
}
glm::vec4 OWUtils::randomSolidColour()
{
static bool gOnceOnly = true;
if (gOnceOnly)
{
gOnceOnly = false;
std::srand(static_cast<unsigned int>(std::time(nullptr)));
}
int r = std::rand() % gAllSolidColours.size();
return gAllSolidColours[r];
}
glm::vec4 OWUtils::colour(OWUtils::SolidColours colour)
{
return gSolidColours[static_cast<unsigned int>(colour)];
}
std::string OWUtils::nowAsString()
{
std::time_t now_c
= std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm t;
localtime_s(&t, &now_c);
char buffer[100];
std::strftime(buffer, sizeof(buffer), "config_%H-%M-%S.json", &t);
return std::string(buffer);
}
template <typename Out>
void splitX(const std::string &s, char delim, Out result)
{
std::istringstream iss(s);
std::string item;
while (std::getline(iss, item, delim))
{
*result++ = item;
}
}
std::vector<std::string> OWUtils::split1(const std::string &s, char delim)
{
// https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string?rq=1
std::vector<std::string> elems;
splitX(s, delim, std::back_inserter(elems));
return elems;
}
std::vector<std::string> OWUtils::split2(const std::string &text, char sep)
{
// https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string?rq=1
std::vector<std::string> tokens;
std::size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != std::string::npos) {
if (end != start) {
tokens.push_back(text.substr(start, end - start));
}
start = end + 1;
}
if (end != start) {
tokens.push_back(text.substr(start));
}
return tokens;
}
void OWUtils::ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch)
{
return !std::isspace(ch);
}));
}
// trim from end (in place)
void OWUtils::rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
return !std::isspace(ch);
}).base(), s.end());
}
void OWUtils::trim(std::string &s)
{
// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
ltrim(s);
rtrim(s);
}
glm::quat rotationBetweenVectors(const glm::vec3& start_, const glm::vec3& dest_)
{
// https://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions
// https://github.com/opengl-tutorials/ogl/blob/master/common/quaternion_utils.cpp
glm::vec3 start = normalize(start);
glm::vec3 dest = normalize(dest);
float cosTheta = dot(start, dest);
glm::vec3 rotationAxis;
if (cosTheta < -1 + 0.001f)
{
// special case when vectors in opposite directions:
// there is no "ideal" rotation axis
// So guess one; any will do as long as it's perpendicular to start
rotationAxis = glm::cross(glm::vec3(0.0f, 0.0f, 1.0f), start);
if (glm::length(rotationAxis) < 0.01) // bad luck, they were parallel, try again!
rotationAxis = glm::cross(glm::vec3(1.0f, 0.0f, 0.0f), start);
rotationAxis = normalize(rotationAxis);
return glm::angleAxis(glm::radians(180.0f), rotationAxis);
}
rotationAxis = cross(start, dest);
float s = sqrt((1 + cosTheta) * 2);
float invs = 1 / s;
return glm::quat(
s * 0.5f,
rotationAxis.x * invs,
rotationAxis.y * invs,
rotationAxis.z * invs
);
}
template <typename OutputIterator>
void extractWords(std::string const& s, char delim, OutputIterator out)
{
auto const isDelim = [delim](char letter) { return letter == delim; };
auto beginWord = std::find_if_not(begin(s), end(s), isDelim);
while (beginWord != end(s))
{
auto const endWord = std::find_if(beginWord, end(s), isDelim);
*out = std::string(beginWord, endWord);
++out;
beginWord = std::find_if_not(endWord, end(s), isDelim);
}
}
std::ostream& operator<<(std::ostream& os, const glm::vec4& v)
{
os << v.x << " " << v.y << " " << v.z << " " << v.w;
return os;
}
std::ostream& operator<<(std::ostream& os, const glm::vec3& v)
{
os << v.x << " " << v.y << " " << v.z;
return os;
}
std::ostream& operator<<(std::ostream& os, const glm::vec2& v)
{
os << v.x << " " << v.y;
return os;
}
std::istream& operator>>(std::istream& is, glm::vec4& v)
{
is >> v.x >> v.y >> v.z >> v.w;
return is;
}
std::istream& operator>>(std::istream& is, glm::vec3& v)
{
is >> v.x >> v.y >> v.z;
return is;
}
std::istream& operator>>(std::istream& is, glm::vec2& v)
{
is >> v.x >> v.y;
return is;
}
std::vector<std::string> OWUtils::split(const std::string& s, char delim)
{
std::vector<std::string> results;
extractWords(s, delim, back_inserter(results));
return results;
}
std::string OWUtils::to_string(const glm::vec4& v)
{
std::stringstream ss;
ss << v;
return ss.str();
}
std::string OWUtils::to_string(const glm::vec3& v)
{
std::stringstream ss;
ss << v;
return ss.str();
}
std::string OWUtils::to_string(const glm::vec2& v)
{
std::stringstream ss;
ss << v;
return ss.str();
}
std::string OWUtils::to_string(float v)
{
std::stringstream ss;
ss << v;
return ss.str();
}