NAP
mathutils.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include <glm/glm.hpp>
9 #include <nap/numeric.h>
10 #include <limits>
11 #include <utility/dllexport.h>
12 #include <algorithm>
13 #include <string>
14 
15 namespace nap
16 {
17  namespace math
18  {
19  inline constexpr double E = 2.71828182845904523536; // e
20  inline constexpr double LOG2E = 1.44269504088896340736; // log2(e)
21  inline constexpr double LOG10E = 0.434294481903251827651; // log10(e)
22  inline constexpr double LN2 = 0.693147180559945309417; // ln(2)
23  inline constexpr double LN10 = 2.30258509299404568402; // ln(10)
24  inline constexpr double PI = 3.14159265358979323846; // pi
25  inline constexpr double PIX2 = 6.28318530717958647692; // pi*2
26  inline constexpr double PI_2 = 1.57079632679489661923; // pi/2
27  inline constexpr double PI_4 = 0.785398163397448309616; // pi/4
28  inline constexpr double M1_PI = 0.318309886183790671538; // 1/pi
29  inline constexpr double M2_PI = 0.636619772367581343076; // 2/pi
30  inline constexpr double M2_SQRTPI = 1.12837916709551257390; // 2/sqrt(pi)
31  inline constexpr double SQRT2 = 1.41421356237309504880; // sqrt(2)
32  inline constexpr double SQRT1_2 = 0.707106781186547524401; // 1/sqrt(2)
33 
34  inline constexpr glm::vec3 X_AXIS = { 1.0f, 0.0f, 0.0f };
35  inline constexpr glm::vec3 Y_AXIS = { 0.0f, 1.0f, 0.0f };
36  inline constexpr glm::vec3 Z_AXIS = { 0.0f, 0.0f, 1.0f };
37  inline constexpr glm::vec3 FORWARD = { 0.0f, 0.0f, -1.0f };
38 
50  template<typename T>
51  T fit(T value, T min, T max, T outMin, T outMax);
52 
60  template<typename T>
61  T lerp(const T& start, const T& end, float percent);
62 
71  template<typename T>
72  T clamp(T value, T min, T max);
73 
80  template<typename T>
81  T min(T left, T right);
82 
89  template<typename T>
90  T max(T left, T right);
91 
98  template<typename T>
99  T floor(T value);
100 
107  template<typename T>
108  T ceil(T value);
109 
115  template<typename T>
116  T power(T value, T exp);
117 
124  template<typename T>
125  T smoothStep(T value, T edge0, T edge1);
126 
130  template<typename T>
131  T abs(T value);
132 
138  template<typename T>
139  T constexpr epsilon();
140 
144  template<typename T>
145  T constexpr max();
146 
150  template<typename T>
151  T constexpr min();
152 
157  template<typename T>
158  T sign(T value);
159 
165  template<typename T>
166  T bell(T time, T strength);
167 
176  template<typename T>
177  T random(T min, T max);
178 
183  void NAPAPI setRandomSeed(int value);
184 
198  template<typename T>
199  T NAPAPI smoothDamp(T currentValue, T targetValue, T& currentVelocity, float deltaTime, float smoothTime, float maxSpeed = math::max<float>());
200 
213  template<typename T>
214  void NAPAPI smooth(T& currentValue, const T& targetValue, T& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
215 
223  glm::mat4 NAPAPI composeMatrix(const glm::vec3& translate, const glm::quat& rotate, const glm::vec3& scale);
224 
231  glm::quat NAPAPI eulerToQuat(const glm::vec3& eulerAngle);
232 
241  glm::quat NAPAPI eulerToQuat(float roll, float pitch, float yaw);
242 
248  glm::vec3 NAPAPI radians(const glm::vec3& eulerDegrees);
249 
258  glm::vec3 NAPAPI radians(float roll, float pitch, float yaw);
259 
265  float NAPAPI radians(float degrees);
266 
272  float NAPAPI degrees(float radians);
273 
280  glm::vec3 NAPAPI extractPosition(const glm::mat4x4& matrix);
281 
288  glm::vec3 NAPAPI objectToWorld(const glm::vec3& point, const glm::mat4x4& objectToWorldMatrix);
289 
297  glm::vec3 NAPAPI worldToObject(const glm::vec3& point, const glm::mat4x4& objectToWorldMatrix);
298 
302  std::string NAPAPI generateUUID();
303 
305  // Template definitions
307 
308  template<typename T>
309  T fit(T value, T min, T max, T outMin, T outMax)
310  {
311  T v = glm::clamp<T>(value, min, max);
312  T m = max - min;
313  m = (m == 0.0f) ? std::numeric_limits<T>::epsilon() : m;
314  return (v - min) / (m) * (outMax - outMin) + outMin;
315  }
316 
317  template<typename T>
318  T clamp(T value, T min, T max)
319  {
320  return glm::clamp<T>(value, min, max);
321  }
322 
323  template<typename T>
324  T min(T left, T right)
325  {
326  return std::min<T>(left, right);
327  }
328 
329  template<typename T>
330  T max(T left, T right)
331  {
332  return std::max<T>(left, right);
333  }
334 
335  template<typename T>
336  T floor(T value)
337  {
338  return glm::floor(value);
339  }
340 
341  template<typename T>
342  T ceil(T value)
343  {
344  return glm::ceil(value);
345  }
346 
347  template<typename T>
348  T constexpr epsilon()
349  {
350  return std::numeric_limits<T>::epsilon();
351  }
352 
353  template<typename T>
354  T constexpr max()
355  {
356  return std::numeric_limits<T>::max();
357  }
358 
359  template<typename T>
360  T constexpr min()
361  {
362  return std::numeric_limits<T>::lowest();
363  }
364 
365  template<typename T>
366  T sign(T value)
367  {
368  return static_cast<T>(value == 0 ? 0 : value > 0 ? 1 : -1);
369  }
370 
371  template<typename T>
372  T bell(T t, T strength)
373  {
374  return power<T>(4.0f, strength) * power<T>(t *(1.0f - t), strength);
375  }
376 
377  template<typename T>
378  T smoothStep(T value, T edge0, T edge1)
379  {
380  T x = math::clamp<T>((value - edge0) / (edge1 - edge0), 0, 1);
381  return x * x * (3 - 2 * x);
382  }
383 
384 
386  // Forward declarations of template functions
388 
389  template<>
390  NAPAPI float lerp<float>(const float& start, const float& end, float percent);
391 
392  template<>
393  NAPAPI double lerp<double>(const double& start, const double& end, float percent);
394 
395  template<>
396  NAPAPI glm::vec2 lerp<glm::vec2>(const glm::vec2& start, const glm::vec2& end, float percent);
397 
398  template<>
399  NAPAPI glm::vec3 lerp<glm::vec3>(const glm::vec3& start, const glm::vec3& end, float percent);
400 
401  template<>
402  NAPAPI glm::vec4 lerp<glm::vec4>(const glm::vec4& start, const glm::vec4& end, float percent);
403 
404  template<>
405  NAPAPI float power<float>(float value, float exp);
406 
407  template<>
408  NAPAPI double power<double>(double value, double exp);
409 
410  template<>
411  NAPAPI int power<int>(int value, int exp);
412 
413  template<>
414  NAPAPI void smooth(float& currentValue, const float& targetValue, float& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
415 
416  template<>
417  NAPAPI void smooth(double& currentValue, const double& targetValue, double& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
418 
419  template<>
420  NAPAPI void smooth(glm::vec2& currentValue, const glm::vec2& targetValue, glm::vec2& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
421 
422  template<>
423  NAPAPI void smooth(glm::vec3& currentValue, const glm::vec3& targetValue, glm::vec3& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
424 
425  template<>
426  NAPAPI void smooth(glm::vec4& currentValue, const glm::vec4& targetValue, glm::vec4& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
427 
428  template<>
429  NAPAPI uint random(uint min, uint max);
430 
431  template<>
432  NAPAPI int random(int min, int max);
433 
434  template<>
435  NAPAPI float random(float min, float max);
436 
437  template<>
438  NAPAPI glm::vec3 random(glm::vec3 min, glm::vec3 max);
439 
440  template<>
441  NAPAPI glm::vec4 random(glm::vec4 min, glm::vec4 max);
442 
443  template<>
444  NAPAPI glm::vec2 random(glm::vec2 min, glm::vec2 max);
445 
446  template<>
447  NAPAPI glm::ivec2 random(glm::ivec2 min, glm::ivec2 max);
448 
449  template<>
450  NAPAPI glm::ivec3 random(glm::ivec3 min, glm::ivec3 max);
451 
452  template<>
453  NAPAPI glm::ivec4 random(glm::ivec4 min, glm::ivec4 max);
454 
455  template<>
456  NAPAPI glm::mat4 random(glm::mat4 min, glm::mat4 max);
457 
458  template<>
459  NAPAPI float abs(float value);
460 
461  template<>
462  NAPAPI int abs(int value);
463  }
464 }
nap::math::smooth
void NAPAPI smooth(T &currentValue, const T &targetValue, T &currentVelocity, float deltaTime, float smoothTime, float maxSpeed)
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::math::worldToObject
glm::vec3 NAPAPI worldToObject(const glm::vec3 &point, const glm::mat4x4 &objectToWorldMatrix)
nap::math::abs
T abs(T value)
nap::math::LOG2E
constexpr double LOG2E
Definition: mathutils.h:20
nap::math::M1_PI
constexpr double M1_PI
Definition: mathutils.h:28
nap::math::fit
T fit(T value, T min, T max, T outMin, T outMax)
Definition: mathutils.h:309
nap::math::lerp< float >
NAPAPI float lerp< float >(const float &start, const float &end, float percent)
nap::math::sign
T sign(T value)
Definition: mathutils.h:366
nap::math::LN10
constexpr double LN10
Definition: mathutils.h:23
nap::math::lerp< double >
NAPAPI double lerp< double >(const double &start, const double &end, float percent)
nap::math::generateUUID
std::string NAPAPI generateUUID()
nap::math::SQRT1_2
constexpr double SQRT1_2
Definition: mathutils.h:32
nap::math::smoothDamp
T NAPAPI smoothDamp(T currentValue, T targetValue, T &currentVelocity, float deltaTime, float smoothTime, float maxSpeed=math::max< float >())
nap::math::FORWARD
constexpr glm::vec3 FORWARD
Definition: mathutils.h:37
nap::math::composeMatrix
glm::mat4 NAPAPI composeMatrix(const glm::vec3 &translate, const glm::quat &rotate, const glm::vec3 &scale)
nap::math::power< double >
NAPAPI double power< double >(double value, double exp)
nap::math::max
T max(T left, T right)
Definition: mathutils.h:330
nap::math::eulerToQuat
glm::quat NAPAPI eulerToQuat(const glm::vec3 &eulerAngle)
nap::math::bell
T bell(T time, T strength)
Definition: mathutils.h:372
nap::math::min
T min(T left, T right)
Definition: mathutils.h:324
nap::math::epsilon
constexpr T epsilon()
Definition: mathutils.h:348
nap::math::ceil
T ceil(T value)
Definition: mathutils.h:342
nap::math::Y_AXIS
constexpr glm::vec3 Y_AXIS
Definition: mathutils.h:35
nap::math::smoothStep
T smoothStep(T value, T edge0, T edge1)
Definition: mathutils.h:378
nap::math::floor
T floor(T value)
Definition: mathutils.h:336
nap::math::PIX2
constexpr double PIX2
Definition: mathutils.h:25
nap::math::power
T power(T value, T exp)
nap::math::objectToWorld
glm::vec3 NAPAPI objectToWorld(const glm::vec3 &point, const glm::mat4x4 &objectToWorldMatrix)
nap::math::power< float >
NAPAPI float power< float >(float value, float exp)
nap::math::degrees
float NAPAPI degrees(float radians)
nap::math::LOG10E
constexpr double LOG10E
Definition: mathutils.h:21
nap::math::PI_2
constexpr double PI_2
Definition: mathutils.h:26
nap::math::E
constexpr double E
Definition: mathutils.h:19
nap::math::PI
constexpr double PI
Definition: mathutils.h:24
nap::math::M2_SQRTPI
constexpr double M2_SQRTPI
Definition: mathutils.h:30
nap::math::PI_4
constexpr double PI_4
Definition: mathutils.h:27
nap::math::X_AXIS
constexpr glm::vec3 X_AXIS
Definition: mathutils.h:34
nap::math::extractPosition
glm::vec3 NAPAPI extractPosition(const glm::mat4x4 &matrix)
nap::math::LN2
constexpr double LN2
Definition: mathutils.h:22
nap::math::lerp
T lerp(const T &start, const T &end, float percent)
nap
Definition: templateapp.h:17
nap::math::radians
glm::vec3 NAPAPI radians(const glm::vec3 &eulerDegrees)
nap::math::SQRT2
constexpr double SQRT2
Definition: mathutils.h:31
nap::math::M2_PI
constexpr double M2_PI
Definition: mathutils.h:29
nap::math::Z_AXIS
constexpr glm::vec3 Z_AXIS
Definition: mathutils.h:36
nap::math::random
T random(T min, T max)
nap::math::clamp
T clamp(T value, T min, T max)
Definition: mathutils.h:318
nap::math::power< int >
NAPAPI int power< int >(int value, int exp)
nap::math::setRandomSeed
void NAPAPI setRandomSeed(int value)