PxMath.h

Go to the documentation of this file.
00001 //
00002 // Redistribution and use in source and binary forms, with or without
00003 // modification, are permitted provided that the following conditions
00004 // are met:
00005 //  * Redistributions of source code must retain the above copyright
00006 //    notice, this list of conditions and the following disclaimer.
00007 //  * Redistributions in binary form must reproduce the above copyright
00008 //    notice, this list of conditions and the following disclaimer in the
00009 //    documentation and/or other materials provided with the distribution.
00010 //  * Neither the name of NVIDIA CORPORATION nor the names of its
00011 //    contributors may be used to endorse or promote products derived
00012 //    from this software without specific prior written permission.
00013 //
00014 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
00015 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017 // PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00018 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00019 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00020 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00021 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00022 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00024 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 //
00026 // Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
00027 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
00028 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
00029 
00030 #ifndef PXFOUNDATION_PXMATH_H
00031 #define PXFOUNDATION_PXMATH_H
00032 
00037 #include "foundation/PxPreprocessor.h"
00038 
00039 #if PX_VC
00040 #pragma warning(push)
00041 #pragma warning(disable : 4985) // 'symbol name': attributes not present on previous declaration
00042 #endif
00043 #include <math.h>
00044 #if PX_VC
00045 #pragma warning(pop)
00046 #endif
00047 
00048 #include <float.h>
00049 #include "foundation/PxIntrinsics.h"
00050 #include "foundation/PxAssert.h"
00051 
00052 #if !PX_DOXYGEN
00053 namespace physx
00054 {
00055 #endif
00056 
00057 // constants
00058 static const float PxPi = float(3.141592653589793);
00059 static const float PxHalfPi = float(1.57079632679489661923);
00060 static const float PxTwoPi = float(6.28318530717958647692);
00061 static const float PxInvPi = float(0.31830988618379067154);
00062 static const float PxInvTwoPi = float(0.15915494309189533577);
00063 static const float PxPiDivTwo = float(1.57079632679489661923);
00064 static const float PxPiDivFour = float(0.78539816339744830962);
00065 
00069 template <class T>
00070 PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
00071 {
00072     return a < b ? b : a;
00073 }
00074 
00076 template <>
00077 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxMax(float a, float b)
00078 {
00079     return intrinsics::selectMax(a, b);
00080 }
00081 
00085 template <class T>
00086 PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
00087 {
00088     return a < b ? a : b;
00089 }
00090 
00091 template <>
00093 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxMin(float a, float b)
00094 {
00095     return intrinsics::selectMin(a, b);
00096 }
00097 
00098 /*
00099 Many of these are just implemented as PX_CUDA_CALLABLE PX_FORCE_INLINE calls to the C lib right now,
00100 but later we could replace some of them with some approximations or more
00101 clever stuff.
00102 */
00103 
00107 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
00108 {
00109     return intrinsics::abs(a);
00110 }
00111 
00112 PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxEquals(float a, float b, float eps)
00113 {
00114     return (PxAbs(a - b) < eps);
00115 }
00116 
00120 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAbs(double a)
00121 {
00122     return ::fabs(a);
00123 }
00124 
00128 PX_CUDA_CALLABLE PX_FORCE_INLINE int32_t PxAbs(int32_t a)
00129 {
00130     return ::abs(a);
00131 }
00132 
00136 template <class T>
00137 PX_CUDA_CALLABLE PX_FORCE_INLINE T PxClamp(T v, T lo, T hi)
00138 {
00139     PX_ASSERT(lo <= hi);
00140     return PxMin(hi, PxMax(lo, v));
00141 }
00142 
00144 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
00145 {
00146     return intrinsics::sqrt(a);
00147 }
00148 
00150 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxSqrt(double a)
00151 {
00152     return ::sqrt(a);
00153 }
00154 
00156 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
00157 {
00158     return intrinsics::recipSqrt(a);
00159 }
00160 
00162 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxRecipSqrt(double a)
00163 {
00164     return 1 / ::sqrt(a);
00165 }
00166 
00168 
00170 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSin(float a)
00171 {
00172     return intrinsics::sin(a);
00173 }
00174 
00176 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxSin(double a)
00177 {
00178     return ::sin(a);
00179 }
00180 
00182 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCos(float a)
00183 {
00184     return intrinsics::cos(a);
00185 }
00186 
00188 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxCos(double a)
00189 {
00190     return ::cos(a);
00191 }
00192 
00197 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxTan(float a)
00198 {
00199     return ::tanf(a);
00200 }
00201 
00206 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxTan(double a)
00207 {
00208     return ::tan(a);
00209 }
00210 
00216 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAsin(float f)
00217 {
00218     return ::asinf(PxClamp(f, -1.0f, 1.0f));
00219 }
00220 
00226 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAsin(double f)
00227 {
00228     return ::asin(PxClamp(f, -1.0, 1.0));
00229 }
00230 
00236 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAcos(float f)
00237 {
00238     return ::acosf(PxClamp(f, -1.0f, 1.0f));
00239 }
00240 
00246 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAcos(double f)
00247 {
00248     return ::acos(PxClamp(f, -1.0, 1.0));
00249 }
00250 
00256 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan(float a)
00257 {
00258     return ::atanf(a);
00259 }
00260 
00266 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAtan(double a)
00267 {
00268     return ::atan(a);
00269 }
00270 
00276 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan2(float x, float y)
00277 {
00278     return ::atan2f(x, y);
00279 }
00280 
00286 PX_CUDA_CALLABLE PX_FORCE_INLINE double PxAtan2(double x, double y)
00287 {
00288     return ::atan2(x, y);
00289 }
00290 
00292 PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(float f)
00293 {
00294     return intrinsics::isFinite(f);
00295 }
00296 
00298 PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(double f)
00299 {
00300     return intrinsics::isFinite(f);
00301 }
00302 
00303 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxFloor(float a)
00304 {
00305     return ::floorf(a);
00306 }
00307 
00308 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxExp(float a)
00309 {
00310     return ::expf(a);
00311 }
00312 
00313 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCeil(float a)
00314 {
00315     return ::ceilf(a);
00316 }
00317 
00318 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSign(float a)
00319 {
00320     return physx::intrinsics::sign(a);
00321 }
00322 
00323 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxPow(float x, float y)
00324 {
00325     return ::powf(x, y);
00326 }
00327 
00328 PX_CUDA_CALLABLE PX_FORCE_INLINE float PxLog(float x)
00329 {
00330     return ::logf(x);
00331 }
00332 
00333 #if !PX_DOXYGEN
00334 } // namespace physx
00335 #endif
00336 
00338 #endif // #ifndef PXFOUNDATION_PXMATH_H


Copyright © 2008-2018 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com