6 #ifndef UTILS_EXT_GLM_H 7 #define UTILS_EXT_GLM_H 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> 22 constexpr
float PI = glm::pi<float>();
27 constexpr
float TWO_PI = glm::two_pi<float>();
32 constexpr
float HALF_PI = glm::half_pi<float>();
37 constexpr
float QUAT_PI = glm::quarter_pi<float>();
41 constexpr
float FLT_GLM_CALC_EPSILON = 0.000001f;
51 using MyMat = glm::mat<N, N, float, glm::highp>;
61 void Set(
const MyMat &new_mat)
64 inverse_ = glm::inverse(new_mat);
69 matrix_ = glm::inverse(inv_mat);
73 const MyMat &
Get()
const {
return matrix_; };
77 MyMat matrix_ = MyMat(1.0f);
78 MyMat inverse_ = MyMat(1.0f);
86 template <
int N,
typename genType>
87 inline std::string
to_string(
const glm::vec<N, genType, glm::highp> &vec,
const std::string &delim =
", ")
90 for (
int i = 0; i < N; i++)
92 result += std::to_string(vec[i]);
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")
106 for (
int i = 0; i < N; i++)
108 const auto &v = mat[i];
119 inline std::string
to_string(
const glm::quat &q,
const std::string &delim =
", ")
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);
125 template <
int N,
typename genType>
127 inline bool parse_string(
const std::string &str, glm::vec<N, genType, glm::highp> &out_vec,
const std::string &delim =
", ")
130 if (vals.size() != N)
134 for (glm::length_t i = 0; i < N; ++i)
136 std_ext::StringTo(vals[i], out_vec[i]);
142 template <
int N,
int M,
typename genType>
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")
147 std::vector<std::string> vals;
149 for (
const auto &row : rows)
154 if (vals.size() != N * M)
159 for (glm::length_t r = 0; r < N; ++r)
161 for (glm::length_t c = 0; c < M; ++c)
163 out_mat[r][c] = std::stof(vals[r * M + c]);
172 inline bool parse_string(
const std::string &str, glm::quat &out_quat,
const std::string &delim =
", ")
176 if (vals.size() != N)
181 for (glm::length_t i = 0; i < static_cast<glm::length_t>(N); ++i)
183 out_quat[i] = std::stof(vals[i]);
189 template <
typename genType>
190 genType
sqr(genType value)
192 return static_cast<genType
>(glm::pow(value, 2));
196 template <
typename Type, glm::length_t N>
197 Type
min(
const glm::vec<N, Type> &vec)
200 for (
auto i = 1; i < N; i++)
211 template <
typename Type, glm::length_t N>
212 Type
max(
const glm::vec<N, Type> &vec)
215 for (
auto i = 1; i < N; i++)
226 template <
typename genType>
227 genType
min(genType p1, genType p2, genType p3)
229 return (glm::min)(p1, (glm::min)(p2, p3));
233 template <
typename genType>
234 genType
max(genType p1, genType p2, genType p3)
236 return (glm::max)(p1, (glm::max)(p2, p3));
240 template <
typename genType>
243 return glm::clamp(x, genType(0.0f), genType(1.0f));
250 return glm::abs(glm::length2(vector) - 1.f) < 0.000001f;
254 template <
typename T>
255 inline bool AreEqual(
const T val1,
const T val2,
const T epsilon = std::numeric_limits<T>::epsilon())
257 return glm::epsilonEqual(val1, val2, epsilon);
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())
264 const auto not_equal = glm::epsilonNotEqual(vec1, vec2, epsilon);
265 return !glm::any(not_equal);
269 inline bool AreEqual(
const glm::quat &q1,
const glm::quat &q2,
const float epsilon = FLT_EPSILON)
271 const auto not_equal = glm::epsilonNotEqual(q1, q2, epsilon);
272 return !glm::any(not_equal);
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())
279 for (
auto i = 0; i < M; ++i)
281 const auto not_equal = glm::epsilonNotEqual(mat1[i], mat2[i], epsilon);
282 if (glm::any(not_equal))
291 inline bool IsZero(
const float val,
const float epsilon = FLT_EPSILON)
293 return AreEqual(val, 0.0f, epsilon);
301 template <
typename T,
typename... TS>
302 inline bool AllEqual(
const float precision,
const T a,
const T b,
const TS ... elements)
308 template <
typename T,
typename... TS>
309 inline bool AllEqual(
const float precision,
const T a)
315 template <
int N,
int M,
typename genType>
317 inline bool IsAnyNan(
const glm::mat<N, M, genType, glm::highp> &mat)
319 for (glm::length_t r = 0; r < N; ++r)
321 if (glm::any(glm::isnan(mat[r])))
329 template <
typename Type, glm::length_t N>
330 inline bool IsZero(
const glm::vec<N, Type> &vec,
const float epsilon = FLT_EPSILON)
332 for (glm::length_t i = 0; i < N; ++i)
334 if (!
AreEqual(vec[i], 0.0f, epsilon))
344 inline bool IsLess(
const glm::vec<N, float> &vec1,
const glm::vec<N, float> &vec2,
const float epsilon = FLT_EPSILON)
346 for (
int i = 0; i < N; i++)
348 if ((vec1[i] - vec2[i]) > -(epsilon * glm::max(1.0f, (glm::abs)(vec1[i]), (glm::abs)(vec2[i]))))
357 inline bool IsLess(
const float val1,
const float val2,
const float epsilon = FLT_EPSILON)
359 return (val1 - val2) < -(epsilon *
glm_ext::max(1.0f, (glm::abs)(val1), (glm::abs)(val2)));
364 inline bool IsLequal(
const glm::vec<N, float> &vec1,
const glm::vec<N, float> &vec2,
const float epsilon = FLT_EPSILON)
366 for (
int i = 0; i < N; i++)
368 if ((vec1[i] - vec2[i]) > epsilon *
glm_ext::max(1.0f, (glm::abs)(vec1[i]), (glm::abs)(vec2[i])))
377 inline bool IsLequal(
const float val1,
const float val2,
const float epsilon = FLT_EPSILON)
379 return (val1 - val2) < epsilon *
glm_ext::max(1.0f, (glm::abs)(val1), (glm::abs)(val2));
384 inline bool IsGreater(
const glm::vec<N, float> &vec1,
const glm::vec<N, float> &vec2,
const float epsilon = FLT_EPSILON)
386 for (
int i = 0; i < N; i++)
388 if ((vec1[i] - vec2[i]) < epsilon *
glm_ext::max(1.0f, (glm::abs)(vec1[i]), (glm::abs)(vec2[i])))
397 inline bool IsGreater(
const float val1,
const float val2,
const float epsilon = FLT_EPSILON)
399 return (val1 - val2) > epsilon *
glm_ext::max(1.0f, (glm::abs)(val1), (glm::abs)(val2));
404 inline bool IsGequal(
const glm::vec<N, float> &vec1,
const glm::vec<N, float> &vec2,
const float epsilon = FLT_EPSILON)
406 for (
int i = 0; i < N; i++)
408 if ((vec1[i] - vec2[i]) < -(epsilon *
glm_ext::max(1.0f, (glm::abs)(vec1[i]), (glm::abs)(vec2[i]))))
417 inline bool IsGequal(
const float val1,
const float val2,
const float epsilon = FLT_EPSILON)
419 return (val1 - val2) > -(epsilon *
glm_ext::max(1.0f, (glm::abs)(val1), (glm::abs)(val2)));
423 inline int doti(
const glm::ivec2 &x,
const glm::ivec2 &y)
425 return x[0] * y[0] + x[1] * y[1];
429 inline float map(
float val,
float in_min,
float in_max,
float out_min,
float out_max)
431 float perc = (val - in_min) / (in_max - in_min);
432 return out_min + perc * (out_max - out_min);
445 inline float Gaussian(
float x,
float mean,
float sigma)
447 return (glm::exp)(-0.5f * (
glm_ext::sqr)((x - mean) / sigma));
451 inline float Rand01(
int precision = 1000)
453 return float(rand() % (precision + 1)) / precision;
459 return min +
Rand01(precision) * (max -
min);
462 inline glm::vec3 RandomVec3()
464 const auto polar_angle = glm::vec2(
468 return glm::euclidean(polar_angle);
471 inline glm::quat RandomQuat()
473 return glm::rotate(glm::quat(),
RandomInRange(0, TWO_PI), RandomVec3());
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)
478 glm::mat4 projection;
480 const auto width = float(resolution.x);
481 const auto height = float(resolution.y);
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;
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;
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;
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;
506 template <
typename T>
507 bool IsInRange(T value, T
min, T
max)
509 return value >= min && value <=
max;
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