You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

81 lines
1.9 KiB

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