RavEngine
Loading...
Searching...
No Matches
PxMath.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_MATH_H
30#define PX_MATH_H
31
36#include "foundation/PxPreprocessor.h"
37
38
39#if PX_VC
40#pragma warning(push)
41#pragma warning(disable : 4985) // 'symbol name': attributes not present on previous declaration
42#endif
43#include <math.h>
44#if PX_VC
45#pragma warning(pop)
46#endif
47
48#include <float.h>
49#include "foundation/PxMathIntrinsics.h"
50#include "foundation/PxAssert.h"
51
52#if !PX_DOXYGEN
53namespace physx
54{
55#endif
56
57// constants
58static const float PxPi = float(3.141592653589793);
59static const float PxHalfPi = float(1.57079632679489661923);
60static const float PxTwoPi = float(6.28318530717958647692);
61static const float PxInvPi = float(0.31830988618379067154);
62static const float PxInvTwoPi = float(0.15915494309189533577);
63static const float PxPiDivTwo = float(1.57079632679489661923);
64static const float PxPiDivFour = float(0.78539816339744830962);
65static const float PxSqrt2 = float(1.4142135623730951);
66static const float PxInvSqrt2 = float(0.7071067811865476);
67
71template <class T>
72PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
73{
74 return a < b ? b : a;
75}
76
78template <>
79PX_CUDA_CALLABLE PX_FORCE_INLINE float PxMax(float a, float b)
80{
81 return intrinsics::selectMax(a, b);
82}
83
87template <class T>
88PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
89{
90 return a < b ? a : b;
91}
92
93template <>
95PX_CUDA_CALLABLE PX_FORCE_INLINE float PxMin(float a, float b)
96{
97 return intrinsics::selectMin(a, b);
98}
99
100/*
101Many of these are just implemented as PX_CUDA_CALLABLE PX_FORCE_INLINE calls to the C lib right now,
102but later we could replace some of them with some approximations or more
103clever stuff.
104*/
105
109PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
110{
111 return intrinsics::abs(a);
112}
113
114PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxEquals(float a, float b, float eps)
115{
116 return (PxAbs(a - b) < eps);
117}
118
122PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAbs(double a)
123{
124 return ::fabs(a);
125}
126
130PX_CUDA_CALLABLE PX_FORCE_INLINE int32_t PxAbs(int32_t a)
131{
132 return ::abs(a);
133}
134
138template <class T>
139PX_CUDA_CALLABLE PX_FORCE_INLINE T PxClamp(T v, T lo, T hi)
140{
141 PX_ASSERT(lo <= hi);
142 return PxMin(hi, PxMax(lo, v));
143}
144
146PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
147{
148 return intrinsics::sqrt(a);
149}
150
152PX_CUDA_CALLABLE PX_FORCE_INLINE double PxSqrt(double a)
153{
154 return ::sqrt(a);
155}
156
158PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
159{
160 return intrinsics::recipSqrt(a);
161}
162
164PX_CUDA_CALLABLE PX_FORCE_INLINE double PxRecipSqrt(double a)
165{
166 return 1 / ::sqrt(a);
167}
168
170PX_CUDA_CALLABLE PX_FORCE_INLINE PxF32 PxSqr(const PxF32 a)
171{
172 return a * a;
173}
174
176
178PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSin(float a)
179{
180 return intrinsics::sin(a);
181}
182
184PX_CUDA_CALLABLE PX_FORCE_INLINE double PxSin(double a)
185{
186 return ::sin(a);
187}
188
190PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCos(float a)
191{
192 return intrinsics::cos(a);
193}
194
196PX_CUDA_CALLABLE PX_FORCE_INLINE double PxCos(double a)
197{
198 return ::cos(a);
199}
200
202PX_CUDA_CALLABLE PX_FORCE_INLINE void PxSinCos(const PxF32 a, PxF32& sin, PxF32& cos)
203{
204#if defined(__CUDACC__) && __CUDA_ARCH__ >= 350
205 __sincosf(a, &sin, &cos);
206#else
207 sin = PxSin(a);
208 cos = PxCos(a);
209#endif
210}
211
213PX_CUDA_CALLABLE PX_FORCE_INLINE void PxSinCos(const double a, double& sin, double& cos)
214{
215 sin = PxSin(a);
216 cos = PxCos(a);
217}
218
223PX_CUDA_CALLABLE PX_FORCE_INLINE float PxTan(float a)
224{
225 return ::tanf(a);
226}
227
232PX_CUDA_CALLABLE PX_FORCE_INLINE double PxTan(double a)
233{
234 return ::tan(a);
235}
236
242PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAsin(float f)
243{
244 return ::asinf(PxClamp(f, -1.0f, 1.0f));
245}
246
252PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAsin(double f)
253{
254 return ::asin(PxClamp(f, -1.0, 1.0));
255}
256
262PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAcos(float f)
263{
264 return ::acosf(PxClamp(f, -1.0f, 1.0f));
265}
266
272PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAcos(double f)
273{
274 return ::acos(PxClamp(f, -1.0, 1.0));
275}
276
282PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan(float a)
283{
284 return ::atanf(a);
285}
286
292PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAtan(double a)
293{
294 return ::atan(a);
295}
296
302PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan2(float x, float y)
303{
304 return ::atan2f(x, y);
305}
306
312PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAtan2(double x, double y)
313{
314 return ::atan2(x, y);
315}
316
320PX_CUDA_CALLABLE PX_FORCE_INLINE PxF32 PxDegToRad(const PxF32 a)
321{
322 return 0.01745329251994329547f * a;
323}
324
326PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(float f)
327{
328 return intrinsics::isFinite(f);
329}
330
332PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(double f)
333{
334 return intrinsics::isFinite(f);
335}
336
337PX_CUDA_CALLABLE PX_FORCE_INLINE float PxFloor(float a)
338{
339 return ::floorf(a);
340}
341
342PX_CUDA_CALLABLE PX_FORCE_INLINE float PxExp(float a)
343{
344 return ::expf(a);
345}
346
347PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCeil(float a)
348{
349 return ::ceilf(a);
350}
351
352PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSign(float a)
353{
354 return physx::intrinsics::sign(a);
355}
356
357PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSign2(float a, float eps = FLT_EPSILON)
358{
359 return (a < -eps) ? -1.0f : (a > eps) ? 1.0f : 0.0f;
360}
361
362PX_CUDA_CALLABLE PX_FORCE_INLINE float PxPow(float x, float y)
363{
364 return ::powf(x, y);
365}
366
367PX_CUDA_CALLABLE PX_FORCE_INLINE float PxLog(float x)
368{
369 return ::logf(x);
370}
371
372#if !PX_DOXYGEN
373} // namespace physx
374#endif
375
377#endif
378
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCos(float a)
Cosine of an angle (Unit: Radians)
Definition PxMath.h:190
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan2(float x, float y)
Arctangent of (x/y) with correct sign. Returns angle between -PI and PI in radians Unit: Radians.
Definition PxMath.h:302
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition PxMath.h:109
PX_CUDA_CALLABLE PX_FORCE_INLINE PxF32 PxSqr(const PxF32 a)
square of the argument
Definition PxMath.h:170
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
reciprocal square root.
Definition PxMath.h:158
PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(float f)
returns true if the passed number is a finite floating point number as opposed to INF,...
Definition PxMath.h:326
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition PxMath.h:146
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAsin(float f)
Arcsine. Returns angle between -PI/2 and PI/2 in radians Unit: Radians.
Definition PxMath.h:242
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
The return value is the greater of the two specified values.
Definition PxMath.h:72
PX_CUDA_CALLABLE PX_FORCE_INLINE void PxSinCos(const PxF32 a, PxF32 &sin, PxF32 &cos)
compute sine and cosine at the same time
Definition PxMath.h:202
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan(float a)
ArcTangent. Returns angle between -PI/2 and PI/2 in radians Unit: Radians.
Definition PxMath.h:282
PX_CUDA_CALLABLE PX_FORCE_INLINE PxF32 PxDegToRad(const PxF32 a)
Converts degrees to radians.
Definition PxMath.h:320
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAcos(float f)
Arccosine. Returns angle between 0 and PI in radians Unit: Radians.
Definition PxMath.h:262
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxTan(float a)
Tangent of an angle. Unit: Radians.
Definition PxMath.h:223
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
The return value is the lesser of the two specified values.
Definition PxMath.h:88
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxClamp(T v, T lo, T hi)
Clamps v to the range [hi,lo].
Definition PxMath.h:139
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSin(float a)
trigonometry – all angles are in radians.
Definition PxMath.h:178