Trigonometry.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. JPH_NAMESPACE_BEGIN
  6. // Note that this file exists because std::sin etc. are not platform independent and will lead to non-deterministic simulation
  7. /// Sine of x (input in radians)
  8. JPH_INLINE float Sin(float inX)
  9. {
  10. Vec4 s, c;
  11. Vec4::sReplicate(inX).SinCos(s, c);
  12. return s.GetX();
  13. }
  14. /// Cosine of x (input in radians)
  15. JPH_INLINE float Cos(float inX)
  16. {
  17. Vec4 s, c;
  18. Vec4::sReplicate(inX).SinCos(s, c);
  19. return c.GetX();
  20. }
  21. /// Tangent of x (input in radians)
  22. JPH_INLINE float Tan(float inX)
  23. {
  24. return Vec4::sReplicate(inX).Tan().GetX();
  25. }
  26. /// Arc sine of x (returns value in the range [-PI / 2, PI / 2])
  27. /// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::asin
  28. JPH_INLINE float ASin(float inX)
  29. {
  30. return Vec4::sReplicate(inX).ASin().GetX();
  31. }
  32. /// Arc cosine of x (returns value in the range [0, PI])
  33. /// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::acos
  34. JPH_INLINE float ACos(float inX)
  35. {
  36. return Vec4::sReplicate(inX).ACos().GetX();
  37. }
  38. /// An approximation of ACos, max error is 4.2e-3 over the entire range [-1, 1], is approximately 2.5x faster than ACos
  39. JPH_INLINE float ACosApproximate(float inX)
  40. {
  41. // See: https://www.johndcook.com/blog/2022/09/06/inverse-cosine-near-1/
  42. // See also: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
  43. // Taylor of cos(x) = 1 - x^2 / 2 + ...
  44. // Substitute x = sqrt(2 y) we get: cos(sqrt(2 y)) = 1 - y
  45. // Substitute z = 1 - y we get: cos(sqrt(2 (1 - z))) = z <=> acos(z) = sqrt(2 (1 - z))
  46. // To avoid the discontinuity at 1, instead of using the Taylor expansion of acos(x) we use acos(x) / sqrt(2 (1 - x)) = 1 + (1 - x) / 12 + ...
  47. // Since the approximation was made at 1, it has quite a large error at 0 meaning that if we want to extend to the
  48. // range [-1, 1] by mirroring the range [0, 1], the value at 0+ is not the same as 0-.
  49. // So we observe that the form of the Taylor expansion is f(x) = sqrt(1 - x) * (a + b x) and we fit the function so that f(0) = pi / 2
  50. // this gives us a = pi / 2. f(1) = 0 regardless of b. We search for a constant b that minimizes the error in the range [0, 1].
  51. float abs_x = min(abs(inX), 1.0f); // Ensure that we don't get a value larger than 1
  52. float val = sqrt(1.0f - abs_x) * (JPH_PI / 2 - 0.175394f * abs_x);
  53. // Our approximation is valid in the range [0, 1], extend it to the range [-1, 1]
  54. return inX < 0? JPH_PI - val : val;
  55. }
  56. /// Arc tangent of x (returns value in the range [-PI / 2, PI / 2])
  57. JPH_INLINE float ATan(float inX)
  58. {
  59. return Vec4::sReplicate(inX).ATan().GetX();
  60. }
  61. /// Arc tangent of y / x using the signs of the arguments to determine the correct quadrant (returns value in the range [-PI, PI])
  62. JPH_INLINE float ATan2(float inY, float inX)
  63. {
  64. return Vec4::sATan2(Vec4::sReplicate(inY), Vec4::sReplicate(inX)).GetX();
  65. }
  66. JPH_NAMESPACE_END