Utilities
Utilities header only library
ExtGLM.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_EXT_GLM_H
7 #define UTILS_EXT_GLM_H
8 
9 #include <string>
10 #include <vector>
11 #include <glm/glm.hpp>
12 #include <glm/gtc/constants.hpp>
13 #include <glm/gtc/quaternion.hpp>
14 #include <glm/gtc/epsilon.hpp>
15 #include <glm/gtx/norm.hpp>
16 #include <glm/gtx/polar_coordinates.hpp>
17 #include <Utils/ExtSTD.h>
18 
19 
20 
21 #ifndef PI
22 constexpr float PI = glm::pi<float>();
24 #endif
25 
26 #ifndef TWO_PI
27 constexpr float TWO_PI = glm::two_pi<float>();
29 #endif
30 
31 #ifndef HALF_PI
32 constexpr float HALF_PI = glm::half_pi<float>();
34 #endif
35 
36 #ifndef QUAT_PI
37 constexpr float QUAT_PI = glm::quarter_pi<float>();
39 #endif
40 
41 constexpr float FLT_GLM_CALC_EPSILON = 0.000001f;
42 
44 namespace glm_ext
45 {
46 
48  template<int N>
49  class TransMat
50  {
51  using MyMat = glm::mat<N, N, float, glm::highp>;
52  public:
54  TransMat() = default;
56  TransMat(const MyMat &new_mat)
57  {
58  Set(new_mat);
59  }
61  void Set(const MyMat &new_mat)
62  {
63  matrix_ = new_mat;
64  inverse_ = glm::inverse(new_mat);
65  }
67  void SetInverse(const MyMat &inv_mat)
68  {
69  matrix_ = glm::inverse(inv_mat);
70  inverse_ = inv_mat;
71  }
73  const MyMat &Get() const { return matrix_; };
75  const MyMat &GetInverse() const { return inverse_; };
76  private:
77  MyMat matrix_ = MyMat(1.0f);
78  MyMat inverse_ = MyMat(1.0f);
79  };
84 
86  template <int N, typename genType>
87  inline std::string to_string(const glm::vec<N, genType, glm::highp> &vec, const std::string &delim = ", ")
88  {
89  std::string result;
90  for (int i = 0; i < N; i++)
91  {
92  result += std::to_string(vec[i]);
93  if (i < N - 1)
94  {
95  result += delim;
96  }
97  }
98  return result;
99  }
100 
102  template <int N, int M, typename genType>
103  inline std::string to_string(const glm::mat<N, M, genType, glm::highp> &mat, const std::string &delim_col = ", ", const std::string &delim_row = "\n")
104  {
105  std::string result;
106  for (int i = 0; i < N; i++)
107  {
108  const auto &v = mat[i];
109  result += to_string(v, delim_col);
110  if (i < N - 1)
111  {
112  result += delim_row;
113  }
114  }
115  return result;
116  }
117 
119  inline std::string to_string(const glm::quat &q, const std::string &delim = ", ")
120  {
121  return std::to_string(q.x) + delim + std::to_string(q.y) + delim + std::to_string(q.z) + delim + std::to_string(q.w);
122  }
123 
125  template <int N, typename genType>
126  [[nodiscard]]
127  inline bool parse_string(const std::string &str, glm::vec<N, genType, glm::highp> &out_vec, const std::string &delim = ", ")
128  {
129  const auto vals = std_ext::Parse(str, delim);
130  if (vals.size() != N)
131  {
132  return false;
133  }
134  for (glm::length_t i = 0; i < N; ++i)
135  {
136  std_ext::StringTo(vals[i], out_vec[i]);
137  }
138  return true;
139  }
140 
142  template <int N, int M, typename genType>
143  [[nodiscard]]
144  inline bool parse_string(const std::string &str, glm::mat<N, M, genType, glm::highp> &out_mat, const std::string &delim_col = ", ", const std::string &delim_row = "\n")
145  {
146  const auto rows = std_ext::Parse(str, delim_row);
147  std::vector<std::string> vals;
148  vals.reserve(N * M);
149  for (const auto &row : rows)
150  {
151  std_ext::InsertBack(vals, std_ext::Parse(row, delim_col));
152  }
153 
154  if (vals.size() != N * M)
155  {
156  return false;
157  }
158 
159  for (glm::length_t r = 0; r < N; ++r)
160  {
161  for (glm::length_t c = 0; c < M; ++c)
162  {
163  out_mat[r][c] = std::stof(vals[r * M + c]);
164  }
165  }
166 
167  return true;
168  }
169 
171  [[nodiscard]]
172  inline bool parse_string(const std::string &str, glm::quat &out_quat, const std::string &delim = ", ")
173  {
174  const size_t N = 4;
175  const auto vals = std_ext::Parse(str, delim);
176  if (vals.size() != N)
177  {
178  return false;
179  }
180 
181  for (glm::length_t i = 0; i < static_cast<glm::length_t>(N); ++i)
182  {
183  out_quat[i] = std::stof(vals[i]);
184  }
185  return true;
186  }
187 
189  template <typename genType>
190  genType sqr(genType value)
191  {
192  return static_cast<genType>(glm::pow(value, 2));
193  }
194 
196  template <typename Type, glm::length_t N>
197  Type min(const glm::vec<N, Type> &vec)
198  {
199  Type val = vec[0];
200  for (auto i = 1; i < N; i++)
201  {
202  if (vec[i] < val)
203  {
204  val = vec[i];
205  }
206  }
207  return val;
208  }
209 
211  template <typename Type, glm::length_t N>
212  Type max(const glm::vec<N, Type> &vec)
213  {
214  Type val = vec[0];
215  for (auto i = 1; i < N; i++)
216  {
217  if (vec[i] > val)
218  {
219  val = vec[i];
220  }
221  }
222  return val;
223  }
224 
226  template <typename genType>
227  genType min(genType p1, genType p2, genType p3)
228  {
229  return (glm::min)(p1, (glm::min)(p2, p3));
230  }
231 
233  template <typename genType>
234  genType max(genType p1, genType p2, genType p3)
235  {
236  return (glm::max)(p1, (glm::max)(p2, p3));
237  }
238 
240  template <typename genType>
241  genType Clamp01(genType x)
242  {
243  return glm::clamp(x, genType(0.0f), genType(1.0f));
244  }
245 
247  template <int N>
248  inline bool IsNormalized(const glm::vec<N, float> &vector)
249  {
250  return glm::abs(glm::length2(vector) - 1.f) < 0.000001f;
251  }
252 
254  template <typename T>
255  inline bool AreEqual(const T val1, const T val2, const T epsilon = std::numeric_limits<T>::epsilon())
256  {
257  return glm::epsilonEqual(val1, val2, epsilon);
258  }
259 
261  template <typename T, int N>
262  inline bool AreEqual(const glm::vec<N, T> &vec1, const glm::vec<N, T> &vec2, const T epsilon = std::numeric_limits<T>::epsilon())
263  {
264  const auto not_equal = glm::epsilonNotEqual(vec1, vec2, epsilon);
265  return !glm::any(not_equal);
266  }
267 
269  inline bool AreEqual(const glm::quat &q1, const glm::quat &q2, const float epsilon = FLT_EPSILON)
270  {
271  const auto not_equal = glm::epsilonNotEqual(q1, q2, epsilon);
272  return !glm::any(not_equal);
273  }
274 
276  template <typename T, int N, int M>
277  inline bool AreEqual(const glm::mat<N, M, T> &mat1, const glm::mat<N, M, T> &mat2, const T epsilon = std::numeric_limits<T>::epsilon())
278  {
279  for (auto i = 0; i < M; ++i)
280  {
281  const auto not_equal = glm::epsilonNotEqual(mat1[i], mat2[i], epsilon);
282  if (glm::any(not_equal))
283  {
284  return false;
285  }
286  }
287  return true;
288  }
289 
291  inline bool IsZero(const float val, const float epsilon = FLT_EPSILON)
292  {
293  return AreEqual(val, 0.0f, epsilon);
294  }
295 
301  template <typename T, typename... TS>
302  inline bool AllEqual(const float precision, const T a, const T b, const TS ... elements)
303  {
304  return AreEqual(a, b, precision) && AllEqual(precision, b, elements ...);
305  }
306 
308  template <typename T, typename... TS>
309  inline bool AllEqual(const float precision, const T a)
310  {
311  return true;
312  }
313 
315  template <int N, int M, typename genType>
316  [[nodiscard]]
317  inline bool IsAnyNan(const glm::mat<N, M, genType, glm::highp> &mat)
318  {
319  for (glm::length_t r = 0; r < N; ++r)
320  {
321  if (glm::any(glm::isnan(mat[r])))
322  {
323  return true;
324  }
325  }
326  }
327 
329  template <typename Type, glm::length_t N>
330  inline bool IsZero(const glm::vec<N, Type> &vec, const float epsilon = FLT_EPSILON)
331  {
332  for (glm::length_t i = 0; i < N; ++i)
333  {
334  if (!AreEqual(vec[i], 0.0f, epsilon))
335  {
336  return false;
337  }
338  }
339  return true;
340  }
341 
343  template <int N>
344  inline bool IsLess(const glm::vec<N, float> &vec1, const glm::vec<N, float> &vec2, const float epsilon = FLT_EPSILON)
345  {
346  for (int i = 0; i < N; i++)
347  {
348  if ((vec1[i] - vec2[i]) > -(epsilon * glm::max(1.0f, (glm::abs)(vec1[i]), (glm::abs)(vec2[i]))))
349  {
350  return false;
351  }
352  }
353  return true;
354  }
355 
357  inline bool IsLess(const float val1, const float val2, const float epsilon = FLT_EPSILON)
358  {
359  return (val1 - val2) < -(epsilon * glm_ext::max(1.0f, (glm::abs)(val1), (glm::abs)(val2)));
360  }
361 
363  template <int N>
364  inline bool IsLequal(const glm::vec<N, float> &vec1, const glm::vec<N, float> &vec2, const float epsilon = FLT_EPSILON)
365  {
366  for (int i = 0; i < N; i++)
367  {
368  if ((vec1[i] - vec2[i]) > epsilon * glm_ext::max(1.0f, (glm::abs)(vec1[i]), (glm::abs)(vec2[i])))
369  {
370  return false;
371  }
372  }
373  return true;
374  }
375 
377  inline bool IsLequal(const float val1, const float val2, const float epsilon = FLT_EPSILON)
378  {
379  return (val1 - val2) < epsilon * glm_ext::max(1.0f, (glm::abs)(val1), (glm::abs)(val2));
380  }
381 
383  template <int N>
384  inline bool IsGreater(const glm::vec<N, float> &vec1, const glm::vec<N, float> &vec2, const float epsilon = FLT_EPSILON)
385  {
386  for (int i = 0; i < N; i++)
387  {
388  if ((vec1[i] - vec2[i]) < epsilon * glm_ext::max(1.0f, (glm::abs)(vec1[i]), (glm::abs)(vec2[i])))
389  {
390  return false;
391  }
392  }
393  return true;
394  }
395 
397  inline bool IsGreater(const float val1, const float val2, const float epsilon = FLT_EPSILON)
398  {
399  return (val1 - val2) > epsilon * glm_ext::max(1.0f, (glm::abs)(val1), (glm::abs)(val2));
400  }
401 
403  template <int N>
404  inline bool IsGequal(const glm::vec<N, float> &vec1, const glm::vec<N, float> &vec2, const float epsilon = FLT_EPSILON)
405  {
406  for (int i = 0; i < N; i++)
407  {
408  if ((vec1[i] - vec2[i]) < -(epsilon * glm_ext::max(1.0f, (glm::abs)(vec1[i]), (glm::abs)(vec2[i]))))
409  {
410  return false;
411  }
412  }
413  return true;
414  }
415 
417  inline bool IsGequal(const float val1, const float val2, const float epsilon = FLT_EPSILON)
418  {
419  return (val1 - val2) > -(epsilon * glm_ext::max(1.0f, (glm::abs)(val1), (glm::abs)(val2)));
420  }
421 
423  inline int doti(const glm::ivec2 &x, const glm::ivec2 &y)
424  {
425  return x[0] * y[0] + x[1] * y[1];
426  }
427 
429  inline float map(float val, float in_min, float in_max, float out_min, float out_max)
430  {
431  float perc = (val - in_min) / (in_max - in_min);
432  return out_min + perc * (out_max - out_min);
433  }
434 
445  inline float Gaussian(float x, float mean, float sigma)
446  {
447  return (glm::exp)(-0.5f * (glm_ext::sqr)((x - mean) / sigma));
448  }
449 
451  inline float Rand01(int precision = 1000)
452  {
453  return float(rand() % (precision + 1)) / precision;
454  }
455 
457  inline float RandomInRange(float min, float max, int precision = 1000)
458  {
459  return min + Rand01(precision) * (max - min);
460  }
461 
462  inline glm::vec3 RandomVec3()
463  {
464  const auto polar_angle = glm::vec2(
465  glm_ext::RandomInRange(-HALF_PI, HALF_PI),
466  glm_ext::RandomInRange(-PI, PI)
467  );
468  return glm::euclidean(polar_angle);
469  }
470 
471  inline glm::quat RandomQuat()
472  {
473  return glm::rotate(glm::quat(), RandomInRange(0, TWO_PI), RandomVec3());
474  }
475 
476  inline glm::mat4 CreateProjectionMatrix(const glm::uvec2 &resolution, const float fx, const float fy, const float cx, const float cy, const float near_plane = 0.01f, const float far_plane = 1000.0f)
477  {
478  glm::mat4 projection;
479 
480  const auto width = float(resolution.x);
481  const auto height = float(resolution.y);
482 
483  projection[0][0] = 2.0f * fx / width;
484  projection[0][1] = 0.0f;
485  projection[0][2] = 0.0f;
486  projection[0][3] = 0.0f;
487 
488  projection[1][0] = 0.0f;
489  projection[1][1] = -2.0f * fy / height;
490  projection[1][2] = 0.0f;
491  projection[1][3] = 0.0f;
492 
493  projection[2][0] = 1.0f - 2.0f * cx / width;
494  projection[2][1] = 1.0f - 2.0f * cy / height;
495  projection[2][2] = (far_plane + near_plane) / (near_plane - far_plane);
496  projection[2][3] = -1.0f;
497 
498  projection[3][0] = 0.0f;
499  projection[3][1] = 0.0f;
500  projection[3][2] = 2.0f * far_plane * near_plane / (near_plane - far_plane);
501  projection[3][3] = 0.0f;
502 
503  return projection;
504  }
505 
506  template <typename T>
507  bool IsInRange(T value, T min, T max)
508  {
509  return value >= min && value <= max;
510  }
511 }
512 
513 #endif /* !UTILS_EXT_GLM_H */
void InsertBack(T &target, const S &source)
Insert the source vector to the back of target vector.
Definition: ExtSTD.h:673
void Set(const MyMat &new_mat)
Sets matrix to a defined value.
Definition: ExtGLM.h:61
TransMat()=default
Creates identity.
float Rand01(int precision=1000)
Returns random number in interval <0,1>, with specified precision (defaults to 3 digits).
Definition: ExtGLM.h:451
std::string to_string(const glm::vec< N, genType, glm::highp > &vec, const std::string &delim=", ")
Converts the input vector into a string with predefined delimiter.
Definition: ExtGLM.h:87
Type max(const glm::vec< N, Type > &vec)
Returns maximal value component of a vector.
Definition: ExtGLM.h:212
void SetInverse(const MyMat &inv_mat)
Sets inverse of the matrix.
Definition: ExtGLM.h:67
const MyMat & GetInverse() const
Returns inverse of matrix.
Definition: ExtGLM.h:75
Type min(const glm::vec< N, Type > &vec)
Returns minimal value component of a vector.
Definition: ExtGLM.h:197
bool IsLequal(const glm::vec< N, float > &vec1, const glm::vec< N, float > &vec2, const float epsilon=FLT_EPSILON)
Returns true if all components of vec1 are less or equal then vec2 within defined epsilon threshold...
Definition: ExtGLM.h:364
float Gaussian(float x, float mean, float sigma)
Calculates function value f(x) of normal distribution function.
Definition: ExtGLM.h:445
bool IsGreater(const glm::vec< N, float > &vec1, const glm::vec< N, float > &vec2, const float epsilon=FLT_EPSILON)
Returns true if all components of vec1 are grater then vec2 within defined epsilon threshold...
Definition: ExtGLM.h:384
bool AllEqual(const float precision, const T a, const T b, const TS ... elements)
Checks a variable number of elements for equality with a given precision.
Definition: ExtGLM.h:302
bool AreEqual(const T val1, const T val2, const T epsilon=std::numeric_limits< T >::epsilon())
Returns true if two values are equal within defined epsilon threshold.
Definition: ExtGLM.h:255
std::vector< std::string > Parse(const std::string &text, const std::string &separators=WHITESPACES)
Parses the input text to sub strings by a given list of separators.
Definition: ExtSTD.h:735
bool IsAnyNan(const glm::mat< N, M, genType, glm::highp > &mat)
Return whether any element in the matrix is NaN.
Definition: ExtGLM.h:317
Template wrapper for glm matrix holding matrix and its inverse.
Definition: ExtGLM.h:49
bool IsLess(const glm::vec< N, float > &vec1, const glm::vec< N, float > &vec2, const float epsilon=FLT_EPSILON)
Returns true if all components of vec1 are less then vec2 within defined epsilon threshold.
Definition: ExtGLM.h:344
genType Clamp01(genType x)
Clamps a value or individual vector components to the range 0-1.
Definition: ExtGLM.h:241
TransMat(const MyMat &new_mat)
Creates matrix with a defined value.
Definition: ExtGLM.h:56
bool IsZero(const float val, const float epsilon=FLT_EPSILON)
Returns true if a value is equal to zero within defined epsilon threshold.
Definition: ExtGLM.h:291
bool IsGequal(const glm::vec< N, float > &vec1, const glm::vec< N, float > &vec2, const float epsilon=FLT_EPSILON)
Returns true if all components of vec1 are grater or equal then vec2 within defined epsilon threshold...
Definition: ExtGLM.h:404
int doti(const glm::ivec2 &x, const glm::ivec2 &y)
Dot product of two integer vectors.
Definition: ExtGLM.h:423
Extensions to the glm library.
Definition: ExtGLM.h:44
bool IsNormalized(const glm::vec< N, float > &vector)
Returns true if vector is normalized in <0,1> interval.
Definition: ExtGLM.h:248
const MyMat & Get() const
Returns matrix.
Definition: ExtGLM.h:73
float RandomInRange(float min, float max, int precision=1000)
Returns random number in specified interval, with specified precision (defaults to 3 digits)...
Definition: ExtGLM.h:457
float map(float val, float in_min, float in_max, float out_min, float out_max)
Function that maps the input value in the input interval into an output value inside the output inter...
Definition: ExtGLM.h:429
bool parse_string(const std::string &str, glm::vec< N, genType, glm::highp > &out_vec, const std::string &delim=", ")
Parses a string to a glm vector using defined delimiters or fails.
Definition: ExtGLM.h:127
genType sqr(genType value)
Returns square of a value or vector.
Definition: ExtGLM.h:190