PxMath.h
Go to the documentation of this file.
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-2018 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 
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>
50 #include "foundation/PxAssert.h"
51 
52 #if !PX_DOXYGEN
53 namespace physx
54 {
55 #endif
56 
57 // constants
58 static const float PxPi = float(3.141592653589793);
59 static const float PxHalfPi = float(1.57079632679489661923);
60 static const float PxTwoPi = float(6.28318530717958647692);
61 static const float PxInvPi = float(0.31830988618379067154);
62 static const float PxInvTwoPi = float(0.15915494309189533577);
63 static const float PxPiDivTwo = float(1.57079632679489661923);
64 static const float PxPiDivFour = float(0.78539816339744830962);
65 
69 template <class T>
71 {
72  return a < b ? b : a;
73 }
74 
76 template <>
77 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxMax(float a, float b)
78 {
79  return intrinsics::selectMax(a, b);
80 }
81 
85 template <class T>
87 {
88  return a < b ? a : b;
89 }
90 
91 template <>
93 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxMin(float a, float b)
94 {
95  return intrinsics::selectMin(a, b);
96 }
97 
98 /*
99 Many of these are just implemented as PX_CUDA_CALLABLE PX_FORCE_INLINE calls to the C lib right now,
100 but later we could replace some of them with some approximations or more
101 clever stuff.
102 */
103 
108 {
109  return intrinsics::abs(a);
110 }
111 
112 PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxEquals(float a, float b, float eps)
113 {
114  return (PxAbs(a - b) < eps);
115 }
116 
121 {
122  return ::fabs(a);
123 }
124 
129 {
130  return ::abs(a);
131 }
132 
136 template <class T>
138 {
139  PX_ASSERT(lo <= hi);
140  return PxMin(hi, PxMax(lo, v));
141 }
142 
145 {
146  return intrinsics::sqrt(a);
147 }
148 
151 {
152  return ::sqrt(a);
153 }
154 
157 {
158  return intrinsics::recipSqrt(a);
159 }
160 
163 {
164  return 1 / ::sqrt(a);
165 }
166 
168 
171 {
172  return intrinsics::sin(a);
173 }
174 
177 {
178  return ::sin(a);
179 }
180 
183 {
184  return intrinsics::cos(a);
185 }
186 
189 {
190  return ::cos(a);
191 }
192 
198 {
199  return ::tanf(a);
200 }
201 
207 {
208  return ::tan(a);
209 }
210 
217 {
218  return ::asinf(PxClamp(f, -1.0f, 1.0f));
219 }
220 
227 {
228  return ::asin(PxClamp(f, -1.0, 1.0));
229 }
230 
237 {
238  return ::acosf(PxClamp(f, -1.0f, 1.0f));
239 }
240 
247 {
248  return ::acos(PxClamp(f, -1.0, 1.0));
249 }
250 
257 {
258  return ::atanf(a);
259 }
260 
267 {
268  return ::atan(a);
269 }
270 
276 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan2(float x, float y)
277 {
278  return ::atan2f(x, y);
279 }
280 
286 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAtan2(double x, double y)
287 {
288  return ::atan2(x, y);
289 }
290 
293 {
294  return intrinsics::isFinite(f);
295 }
296 
299 {
300  return intrinsics::isFinite(f);
301 }
302 
304 {
305  return ::floorf(a);
306 }
307 
309 {
310  return ::expf(a);
311 }
312 
314 {
315  return ::ceilf(a);
316 }
317 
319 {
320  return physx::intrinsics::sign(a);
321 }
322 
323 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxPow(float x, float y)
324 {
325  return ::powf(x, y);
326 }
327 
329 {
330  return ::logf(x);
331 }
332 
333 #if !PX_DOXYGEN
334 } // namespace physx
335 #endif
336 
338 #endif // #ifndef PXFOUNDATION_PXMATH_H
Definition: GuContactBuffer.h:37
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxPow(float x, float y)
Definition: PxMath.h:323
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:276
PX_CUDA_CALLABLE PX_FORCE_INLINE float sqrt(float a)
platform-specific square root
Definition: PxUnixIntrinsics.h:86
PX_CUDA_CALLABLE PX_FORCE_INLINE float sqrt(float a)
platform-specific square root
Definition: PxWindowsIntrinsics.h:81
static const float PxPi
Definition: PxMath.h:58
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxExp(float a)
Definition: PxMath.h:308
PX_CUDA_CALLABLE PX_FORCE_INLINE float recipSqrt(float a)
platform-specific reciprocal square root
Definition: PxUnixIntrinsics.h:92
#define PX_FORCE_INLINE
Definition: PxPreprocessor.h:364
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSin(float a)
trigonometry – all angles are in radians.
Definition: PxMath.h:170
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxLog(float x)
Definition: PxMath.h:328
static const float PxPiDivTwo
Definition: PxMath.h:63
static const float PxHalfPi
Definition: PxMath.h:59
PX_CUDA_CALLABLE PX_FORCE_INLINE float selectMin(float a, float b)
platform-specific minimum
Definition: PxUnixIntrinsics.h:115
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxClamp(T v, T lo, T hi)
Clamps v to the range [hi,lo].
Definition: PxMath.h:137
PX_CUDA_CALLABLE PX_FORCE_INLINE float selectMax(float a, float b)
platform-specific maximum
Definition: PxUnixIntrinsics.h:121
PX_CUDA_CALLABLE PX_FORCE_INLINE float sign(float a)
platform-specific sign
Definition: PxUnixIntrinsics.h:68
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isFinite(float a)
platform-specific finiteness check (not INF or NAN)
Definition: PxUnixIntrinsics.h:127
PX_CUDA_CALLABLE PX_FORCE_INLINE float cos(float a)
platform-specific cosine
Definition: PxUnixIntrinsics.h:109
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCeil(float a)
Definition: PxMath.h:313
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition: PxMath.h:144
PX_CUDA_CALLABLE PX_FORCE_INLINE float sin(float a)
platform-specific sine
Definition: PxWindowsIntrinsics.h:99
static const float PxInvPi
Definition: PxMath.h:61
static const float PxInvTwoPi
Definition: PxMath.h:62
PX_CUDA_CALLABLE PX_FORCE_INLINE float abs(float a)
platform-specific absolute value
Definition: PxUnixIntrinsics.h:56
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:86
static const float PxTwoPi
Definition: PxMath.h:60
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
reciprocal square root.
Definition: PxMath.h:156
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCos(float a)
Cosine of an angle (Unit: Radians)
Definition: PxMath.h:182
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition: PxMath.h:107
PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxEquals(float a, float b, float eps)
Definition: PxMath.h:112
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:70
static const float PxPiDivFour
Definition: PxMath.h:64
PX_CUDA_CALLABLE PX_FORCE_INLINE float cos(float a)
platform-specific cosine
Definition: PxWindowsIntrinsics.h:105
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:216
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSign(float a)
Definition: PxMath.h:318
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:292
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxFloor(float a)
Definition: PxMath.h:303
PX_CUDA_CALLABLE PX_FORCE_INLINE float abs(float a)
platform-specific absolute value
Definition: PxWindowsIntrinsics.h:51
PX_CUDA_CALLABLE PX_FORCE_INLINE float sin(float a)
platform-specific sine
Definition: PxUnixIntrinsics.h:103
#define PX_ASSERT(exp)
Definition: PxAssert.h:61
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAcos(float f)
Arccosine. Returns angle between 0 and PI in radians Unit: Radians.
Definition: PxMath.h:236
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:256
#define PX_CUDA_CALLABLE
Definition: PxPreprocessor.h:473
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxTan(float a)
Tangent of an angle. Unit: Radians.
Definition: PxMath.h:197