Path: blob/master/thirdparty/jolt_physics/Jolt/Math/Trigonometry.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56JPH_NAMESPACE_BEGIN78// Note that this file exists because std::sin etc. are not platform independent and will lead to non-deterministic simulation910/// Sine of x (input in radians)11JPH_INLINE float Sin(float inX)12{13Vec4 s, c;14Vec4::sReplicate(inX).SinCos(s, c);15return s.GetX();16}1718/// Cosine of x (input in radians)19JPH_INLINE float Cos(float inX)20{21Vec4 s, c;22Vec4::sReplicate(inX).SinCos(s, c);23return c.GetX();24}2526/// Tangent of x (input in radians)27JPH_INLINE float Tan(float inX)28{29return Vec4::sReplicate(inX).Tan().GetX();30}3132/// Arc sine of x (returns value in the range [-PI / 2, PI / 2])33/// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::asin34JPH_INLINE float ASin(float inX)35{36return Vec4::sReplicate(inX).ASin().GetX();37}3839/// Arc cosine of x (returns value in the range [0, PI])40/// Note that all input values will be clamped to the range [-1, 1] and this function will not return NaNs like std::acos41JPH_INLINE float ACos(float inX)42{43return Vec4::sReplicate(inX).ACos().GetX();44}4546/// An approximation of ACos, max error is 4.2e-3 over the entire range [-1, 1], is approximately 2.5x faster than ACos47JPH_INLINE float ACosApproximate(float inX)48{49// See: https://www.johndcook.com/blog/2022/09/06/inverse-cosine-near-1/50// See also: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/51// Taylor of cos(x) = 1 - x^2 / 2 + ...52// Substitute x = sqrt(2 y) we get: cos(sqrt(2 y)) = 1 - y53// Substitute z = 1 - y we get: cos(sqrt(2 (1 - z))) = z <=> acos(z) = sqrt(2 (1 - z))54// 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 + ...55// Since the approximation was made at 1, it has quite a large error at 0 meaning that if we want to extend to the56// range [-1, 1] by mirroring the range [0, 1], the value at 0+ is not the same as 0-.57// 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 / 258// 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].59float abs_x = min(abs(inX), 1.0f); // Ensure that we don't get a value larger than 160float val = sqrt(1.0f - abs_x) * (JPH_PI / 2 - 0.175394f * abs_x);6162// Our approximation is valid in the range [0, 1], extend it to the range [-1, 1]63return inX < 0? JPH_PI - val : val;64}6566/// Arc tangent of x (returns value in the range [-PI / 2, PI / 2])67JPH_INLINE float ATan(float inX)68{69return Vec4::sReplicate(inX).ATan().GetX();70}7172/// Arc tangent of y / x using the signs of the arguments to determine the correct quadrant (returns value in the range [-PI, PI])73JPH_INLINE float ATan2(float inY, float inX)74{75return Vec4::sATan2(Vec4::sReplicate(inY), Vec4::sReplicate(inX)).GetX();76}7778JPH_NAMESPACE_END798081