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  float 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 
195  float NAPAPI smoothDamp(float currentValue, float targetValue, float& currentVelocity, float deltaTime, float smoothTime, float maxSpeed = math::max<float>());
196 
207  double NAPAPI smoothDamp(double currentValue, double targetValue, double& currentVelocity, float deltaTime, float smoothTime, float maxSpeed = math::max<float>());
208 
220  template<typename T>
221  void smooth(T& currentValue, const T& targetValue, T& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
222 
230  glm::mat4 NAPAPI composeMatrix(const glm::vec3& translate, const glm::quat& rotate, const glm::vec3& scale);
231 
238  glm::quat NAPAPI eulerToQuat(const glm::vec3& eulerAngle);
239 
248  glm::quat NAPAPI eulerToQuat(float roll, float pitch, float yaw);
249 
255  glm::vec3 NAPAPI radians(const glm::vec3& eulerDegrees);
256 
265  glm::vec3 NAPAPI radians(float roll, float pitch, float yaw);
266 
272  float NAPAPI radians(float degrees);
273 
279  float NAPAPI degrees(float radians);
280 
287  glm::vec3 NAPAPI extractPosition(const glm::mat4x4& matrix);
288 
295  glm::vec3 NAPAPI objectToWorld(const glm::vec3& point, const glm::mat4x4& objectToWorldMatrix);
296 
304  glm::vec3 NAPAPI worldToObject(const glm::vec3& point, const glm::mat4x4& objectToWorldMatrix);
305 
309  std::string NAPAPI generateUUID();
310 
312  // Template definitions
314 
315  template<typename T>
316  float fit(T value, T min, T max, T outMin, T outMax)
317  {
318  T v = glm::clamp<T>(value, min, max);
319  T m = max - min;
320  m = (m == 0.0f) ? std::numeric_limits<T>::epsilon() : m;
321  return (v - min) / (m) * (outMax - outMin) + outMin;
322  }
323 
324  template<typename T>
325  T clamp(T value, T min, T max)
326  {
327  return glm::clamp<T>(value, min, max);
328  }
329 
330  template<typename T>
331  T min(T left, T right)
332  {
333  return std::min<T>(left, right);
334  }
335 
336  template<typename T>
337  T max(T left, T right)
338  {
339  return std::max<T>(left, right);
340  }
341 
342  template<typename T>
343  T floor(T value)
344  {
345  return glm::floor(value);
346  }
347 
348  template<typename T>
349  T ceil(T value)
350  {
351  return glm::ceil(value);
352  }
353 
354  template<typename T>
355  T constexpr epsilon()
356  {
357  return std::numeric_limits<T>::epsilon();
358  }
359 
360  template<typename T>
361  T constexpr max()
362  {
363  return std::numeric_limits<T>::max();
364  }
365 
366  template<typename T>
367  T constexpr min()
368  {
369  return std::numeric_limits<T>::lowest();
370  }
371 
372  template<typename T>
373  T sign(T value)
374  {
375  return static_cast<T>(value == 0 ? 0 : value > 0 ? 1 : -1);
376  }
377 
378  template<typename T>
379  T bell(T t, T strength)
380  {
381  return power<T>(4.0f, strength) * power<T>(t *(1.0f - t), strength);
382  }
383 
384  template<typename T>
385  T smoothStep(T value, T edge0, T edge1)
386  {
387  T x = math::clamp<T>((value - edge0) / (edge1 - edge0), 0, 1);
388  return x * x * (3 - 2 * x);
389  }
390 
392  // Forward declarations of template functions
394 
395  template<>
396  NAPAPI float lerp<float>(const float& start, const float& end, float percent);
397 
398  template<>
399  NAPAPI double lerp<double>(const double& start, const double& end, float percent);
400 
401  template<>
402  NAPAPI glm::vec2 lerp<glm::vec2>(const glm::vec2& start, const glm::vec2& end, float percent);
403 
404  template<>
405  NAPAPI glm::vec3 lerp<glm::vec3>(const glm::vec3& start, const glm::vec3& end, float percent);
406 
407  template<>
408  NAPAPI glm::vec4 lerp<glm::vec4>(const glm::vec4& start, const glm::vec4& end, float percent);
409 
410  template<>
411  NAPAPI float power<float>(float value, float exp);
412 
413  template<>
414  NAPAPI double power<double>(double value, double exp);
415 
416  template<>
417  NAPAPI int power<int>(int value, int exp);
418 
419  template<>
420  NAPAPI void smooth(float& currentValue, const float& targetValue, float& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
421 
422  template<>
423  NAPAPI void smooth(double& currentValue, const double& targetValue, double& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
424 
425  template<>
426  NAPAPI void smooth(glm::vec2& currentValue, const glm::vec2& targetValue, glm::vec2& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
427 
428  template<>
429  NAPAPI void smooth(glm::vec3& currentValue, const glm::vec3& targetValue, glm::vec3& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
430 
431  template<>
432  NAPAPI void smooth(glm::vec4& currentValue, const glm::vec4& targetValue, glm::vec4& currentVelocity, float deltaTime, float smoothTime, float maxSpeed);
433 
434  template<>
435  NAPAPI uint random(uint min, uint max);
436 
437  template<>
438  NAPAPI int random(int min, int max);
439 
440  template<>
441  NAPAPI float random(float min, float max);
442 
443  template<>
444  NAPAPI glm::vec3 random(glm::vec3 min, glm::vec3 max);
445 
446  template<>
447  NAPAPI glm::vec4 random(glm::vec4 min, glm::vec4 max);
448 
449  template<>
450  NAPAPI glm::vec2 random(glm::vec2 min, glm::vec2 max);
451 
452  template<>
453  NAPAPI glm::ivec2 random(glm::ivec2 min, glm::ivec2 max);
454 
455  template<>
456  NAPAPI glm::ivec3 random(glm::ivec3 min, glm::ivec3 max);
457 
458  template<>
459  NAPAPI glm::ivec4 random(glm::ivec4 min, glm::ivec4 max);
460 
461  template<>
462  NAPAPI glm::mat4 random(glm::mat4 min, glm::mat4 max);
463 
464  template<>
465  NAPAPI float abs(float value);
466 
467  template<>
468  NAPAPI int abs(int value);
469  }
470 }
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::lerp< float >
NAPAPI float lerp< float >(const float &start, const float &end, float percent)
nap::math::sign
T sign(T value)
Definition: mathutils.h:373
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::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:337
nap::math::smoothDamp
float NAPAPI smoothDamp(float currentValue, float targetValue, float &currentVelocity, float deltaTime, float smoothTime, float maxSpeed=math::max< float >())
nap::math::eulerToQuat
glm::quat NAPAPI eulerToQuat(const glm::vec3 &eulerAngle)
nap::math::bell
T bell(T time, T strength)
Definition: mathutils.h:379
nap::math::fit
float fit(T value, T min, T max, T outMin, T outMax)
Definition: mathutils.h:316
nap::math::min
T min(T left, T right)
Definition: mathutils.h:331
nap::math::epsilon
constexpr T epsilon()
Definition: mathutils.h:355
nap::math::ceil
T ceil(T value)
Definition: mathutils.h:349
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:385
nap::math::floor
T floor(T value)
Definition: mathutils.h:343
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:325
nap::math::smooth
void smooth(T &currentValue, const T &targetValue, T &currentVelocity, float deltaTime, float smoothTime, float maxSpeed)
nap::math::power< int >
NAPAPI int power< int >(int value, int exp)
nap::math::setRandomSeed
void NAPAPI setRandomSeed(int value)