MathUtil.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef Spine_MathUtil_h
  30. #define Spine_MathUtil_h
  31. #include <spine/SpineObject.h>
  32. #include <string.h>
  33. namespace spine {
  34. class SP_API MathUtil : public SpineObject {
  35. private:
  36. MathUtil();
  37. public:
  38. static const float Pi;
  39. static const float Pi_2;
  40. static const float Deg_Rad;
  41. static const float Rad_Deg;
  42. template<typename T>
  43. static inline T min(T a, T b) { return a < b ? a : b; }
  44. template<typename T>
  45. static inline T max(T a, T b) { return a > b ? a : b; }
  46. static float sign(float val);
  47. static float clamp(float x, float lower, float upper);
  48. static float abs(float v);
  49. /// Returns the sine in radians from a lookup table.
  50. static float sin(float radians);
  51. /// Returns the cosine in radians from a lookup table.
  52. static float cos(float radians);
  53. /// Returns the sine in radians from a lookup table.
  54. static float sinDeg(float degrees);
  55. /// Returns the cosine in radians from a lookup table.
  56. static float cosDeg(float degrees);
  57. /// Returns atan2 in radians, faster but less accurate than Math.Atan2. Average error of 0.00231 radians (0.1323
  58. /// degrees), largest error of 0.00488 radians (0.2796 degrees).
  59. static float atan2(float y, float x);
  60. static float acos(float v);
  61. static float sqrt(float v);
  62. static float fmod(float a, float b);
  63. static bool isNan(float v);
  64. static float random();
  65. static float randomTriangular(float min, float max);
  66. static float randomTriangular(float min, float max, float mode);
  67. static float pow(float a, float b);
  68. };
  69. struct SP_API Interpolation {
  70. virtual float apply(float a) = 0;
  71. virtual float interpolate(float start, float end, float a) {
  72. return start + (end - start) * apply(a);
  73. }
  74. virtual ~Interpolation() {};
  75. };
  76. struct SP_API PowInterpolation: public Interpolation {
  77. PowInterpolation(int power): power(power) {
  78. }
  79. float apply(float a) {
  80. if (a <= 0.5f) return MathUtil::pow(a * 2.0f, (float)power) / 2.0f;
  81. return MathUtil::pow((a - 1.0f) * 2.0f, (float)power) / (power % 2 == 0 ? -2.0f : 2.0f) + 1.0f;
  82. }
  83. int power;
  84. };
  85. struct SP_API PowOutInterpolation: public Interpolation {
  86. PowOutInterpolation(int power): power(power) {
  87. }
  88. float apply(float a) {
  89. return MathUtil::pow(a - 1, (float)power) * (power % 2 == 0 ? -1.0f : 1.0f) + 1.0f;
  90. }
  91. int power;
  92. };
  93. }
  94. #endif /* Spine_MathUtil_h */