MathUtil.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/MathUtil.h>
  33. #include <math.h>
  34. #include <stdlib.h>
  35. // Required for division by 0 in _isNaN on MSVC
  36. #ifdef _MSC_VER
  37. #pragma warning(disable:4723)
  38. #endif
  39. using namespace spine;
  40. const float MathUtil::Pi = 3.1415926535897932385f;
  41. const float MathUtil::Pi_2 = 3.1415926535897932385f * 2;
  42. const float MathUtil::Deg_Rad = (3.1415926535897932385f / 180.0f);
  43. const float MathUtil::Rad_Deg = (180.0f / 3.1415926535897932385f);
  44. float MathUtil::abs(float v) {
  45. return ((v) < 0 ? -(v) : (v));
  46. }
  47. float MathUtil::sign(float v) {
  48. return ((v) < 0 ? -1.0f : (v) > 0 ? 1.0f : 0.0f);
  49. }
  50. float MathUtil::clamp(float x, float min, float max) {
  51. return ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)));
  52. }
  53. float MathUtil::fmod(float a, float b) {
  54. return (float)::fmod(a, b);
  55. }
  56. /// Returns atan2 in radians, faster but less accurate than Math.Atan2. Average error of 0.00231 radians (0.1323
  57. /// degrees), largest error of 0.00488 radians (0.2796 degrees).
  58. float MathUtil::atan2(float y, float x) {
  59. return (float)::atan2(y, x);
  60. }
  61. /// Returns the cosine in radians from a lookup table.
  62. float MathUtil::cos(float radians) {
  63. return (float)::cos(radians);
  64. }
  65. /// Returns the sine in radians from a lookup table.
  66. float MathUtil::sin(float radians) {
  67. return (float)::sin(radians);
  68. }
  69. float MathUtil::sqrt(float v) {
  70. return (float)::sqrt(v);
  71. }
  72. float MathUtil::acos(float v) {
  73. return (float)::acos(v);
  74. }
  75. /// Returns the sine in radians from a lookup table.
  76. float MathUtil::sinDeg(float degrees) {
  77. return (float)::sin(degrees * MathUtil::Deg_Rad);
  78. }
  79. /// Returns the cosine in radians from a lookup table.
  80. float MathUtil::cosDeg(float degrees) {
  81. return (float)::cos(degrees * MathUtil::Deg_Rad);
  82. }
  83. /* Need to pass 0 as an argument, so VC++ doesn't error with C2124 */
  84. static bool _isNan(float value, float zero) {
  85. float _nan = (float) 0.0 / zero;
  86. return 0 == memcmp((void *) &value, (void *) &_nan, sizeof(value));
  87. }
  88. bool MathUtil::isNan(float v) {
  89. return _isNan(v, 0);
  90. }
  91. float MathUtil::random() {
  92. return ::rand() / (float)RAND_MAX;
  93. }
  94. float MathUtil::randomTriangular(float min, float max) {
  95. return randomTriangular(min, max, (min + max) * 0.5f);
  96. }
  97. float MathUtil::randomTriangular(float min, float max, float mode) {
  98. float u = random();
  99. float d = max - min;
  100. if (u <= (mode - min) / d) return min + sqrt(u * d * (mode - min));
  101. return max - sqrt((1 - u) * d * (max - mode));
  102. }
  103. float MathUtil::pow(float a, float b) {
  104. return (float)::pow(a, b);
  105. }