RavEngine
Loading...
Searching...
No Matches
PxMath.h
1//
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions
4// are met:
5// * Redistributions of source code must retain the above copyright
6// notice, this list of conditions and the following disclaimer.
7// * Redistributions in binary form must reproduce the above copyright
8// notice, this list of conditions and the following disclaimer in the
9// documentation and/or other materials provided with the distribution.
10// * Neither the name of NVIDIA CORPORATION nor the names of its
11// contributors may be used to endorse or promote products derived
12// from this software without specific prior written permission.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
28// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
29
30#ifndef PXFOUNDATION_PXMATH_H
31#define PXFOUNDATION_PXMATH_H
32
37#include "foundation/PxPreprocessor.h"
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/PxIntrinsics.h"
50#include "foundation/PxSharedAssert.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);
65
69template <class T>
70PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
71{
72 return a < b ? b : a;
73}
74
76template <>
77PX_CUDA_CALLABLE PX_FORCE_INLINE float PxMax(float a, float b)
78{
79 return intrinsics::selectMax(a, b);
80}
81
85template <class T>
86PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
87{
88 return a < b ? a : b;
89}
90
91template <>
93PX_CUDA_CALLABLE PX_FORCE_INLINE float PxMin(float a, float b)
94{
95 return intrinsics::selectMin(a, b);
96}
97
98/*
99Many of these are just implemented as PX_CUDA_CALLABLE PX_FORCE_INLINE calls to the C lib right now,
100but later we could replace some of them with some approximations or more
101clever stuff.
102*/
103
107PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
108{
109 return intrinsics::abs(a);
110}
111
112PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxEquals(float a, float b, float eps)
113{
114 return (PxAbs(a - b) < eps);
115}
116
120PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAbs(double a)
121{
122 return ::fabs(a);
123}
124
128PX_CUDA_CALLABLE PX_FORCE_INLINE int32_t PxAbs(int32_t a)
129{
130 return ::abs(a);
131}
132
136template <class T>
137PX_CUDA_CALLABLE PX_FORCE_INLINE T PxClamp(T v, T lo, T hi)
138{
139 PX_SHARED_ASSERT(lo <= hi);
140 return PxMin(hi, PxMax(lo, v));
141}
142
144PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
145{
146 return intrinsics::sqrt(a);
147}
148
150PX_CUDA_CALLABLE PX_FORCE_INLINE double PxSqrt(double a)
151{
152 return ::sqrt(a);
153}
154
156PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
157{
158 return intrinsics::recipSqrt(a);
159}
160
162PX_CUDA_CALLABLE PX_FORCE_INLINE double PxRecipSqrt(double a)
163{
164 return 1 / ::sqrt(a);
165}
166
168
170PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSin(float a)
171{
172 return intrinsics::sin(a);
173}
174
176PX_CUDA_CALLABLE PX_FORCE_INLINE double PxSin(double a)
177{
178 return ::sin(a);
179}
180
182PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCos(float a)
183{
184 return intrinsics::cos(a);
185}
186
188PX_CUDA_CALLABLE PX_FORCE_INLINE double PxCos(double a)
189{
190 return ::cos(a);
191}
192
197PX_CUDA_CALLABLE PX_FORCE_INLINE float PxTan(float a)
198{
199 return ::tanf(a);
200}
201
206PX_CUDA_CALLABLE PX_FORCE_INLINE double PxTan(double a)
207{
208 return ::tan(a);
209}
210
216PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAsin(float f)
217{
218 return ::asinf(PxClamp(f, -1.0f, 1.0f));
219}
220
226PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAsin(double f)
227{
228 return ::asin(PxClamp(f, -1.0, 1.0));
229}
230
236PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAcos(float f)
237{
238 return ::acosf(PxClamp(f, -1.0f, 1.0f));
239}
240
246PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAcos(double f)
247{
248 return ::acos(PxClamp(f, -1.0, 1.0));
249}
250
256PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan(float a)
257{
258 return ::atanf(a);
259}
260
266PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAtan(double a)
267{
268 return ::atan(a);
269}
270
276PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan2(float x, float y)
277{
278 return ::atan2f(x, y);
279}
280
286PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAtan2(double x, double y)
287{
288 return ::atan2(x, y);
289}
290
292PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(float f)
293{
294 return intrinsics::isFinite(f);
295}
296
298PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(double f)
299{
300 return intrinsics::isFinite(f);
301}
302
303PX_CUDA_CALLABLE PX_FORCE_INLINE float PxFloor(float a)
304{
305 return ::floorf(a);
306}
307
308PX_CUDA_CALLABLE PX_FORCE_INLINE float PxExp(float a)
309{
310 return ::expf(a);
311}
312
313PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCeil(float a)
314{
315 return ::ceilf(a);
316}
317
318PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSign(float a)
319{
320 return physx::intrinsics::sign(a);
321}
322
323PX_CUDA_CALLABLE PX_FORCE_INLINE float PxPow(float x, float y)
324{
325 return ::powf(x, y);
326}
327
328PX_CUDA_CALLABLE PX_FORCE_INLINE float PxLog(float x)
329{
330 return ::logf(x);
331}
332
333#if !PX_DOXYGEN
334} // namespace physx
335#endif
336
338#endif // #ifndef PXFOUNDATION_PXMATH_H
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
int32 int32_t
Definition fwd.hpp:71
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 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 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 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