Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

83 lignes
2.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #pragma once
  13. //
  14. // The Matrix classes
  15. // ------------------
  16. //
  17. #include <lol/math> // lol::lerp, lol::clamp
  18. #include <algorithm> // std::swap
  19. #include <cstdlib>
  20. #include <stdint.h>
  21. namespace lol
  22. {
  23. /* Next power of two. */
  24. template <typename T> static inline T PotUp(T val)
  25. {
  26. val = val - 1;
  27. if (sizeof(val) > 4) val = val | ((uint64_t)val >> 32);
  28. if (sizeof(val) > 2) val = val | ((uint64_t)val >> 16);
  29. if (sizeof(val) > 1) val = val | ((uint64_t)val >> 8);
  30. val = val | ((uint64_t)val >> 4);
  31. val = val | ((uint64_t)val >> 2);
  32. val = val | ((uint64_t)val >> 1);
  33. return val + 1;
  34. }
  35. //Damp for float
  36. template <typename T1, typename T2, typename Tf> static inline T1 damp(const T1 &a, const T2 &b, const Tf &x, const Tf &dt)
  37. {
  38. if (dt <= .0f)
  39. return a;
  40. return lol::lerp(a, b, dt / (dt + x));
  41. }
  42. //SmoothClamp clamps x in [a - sd, b + sd] and returns a value in [a, b] that is smoothed at the borders.
  43. static inline float SmoothClamp(float &x, float a, float b, float sd)
  44. {
  45. if (b < a)
  46. std::swap(a, b);
  47. float tx = x;
  48. float ta = a - sd;
  49. float tb = b + sd;
  50. if (sd > 0.f)
  51. {
  52. if ((b - a) < 2.f * sd)
  53. sd = .5f * (b - a);
  54. if (tx < a + sd && tx > a - sd)
  55. {
  56. float t = (tx - a) / sd;
  57. t = 0.25f * (t + 1.0f) * (t + 1.0f);
  58. tx = a + t * sd;
  59. }
  60. else if (tx < b + sd && tx > b - sd)
  61. {
  62. float t = -(tx - b) / sd;
  63. t = 0.25f * (t + 1.0f) * (t + 1.0f);
  64. tx = b - t * sd;
  65. }
  66. }
  67. x = lol::clamp(x, ta, tb);
  68. return lol::clamp(tx, a, b);
  69. }
  70. } /* namespace lol */