diff options
| author | git perforce import user <a@b> | 2016-10-25 12:29:14 -0600 |
|---|---|---|
| committer | Sheikh Dawood Abdul Ajees <Sheikh Dawood Abdul Ajees> | 2016-10-25 18:56:37 -0500 |
| commit | 3dfe2108cfab31ba3ee5527e217d0d8e99a51162 (patch) | |
| tree | fa6485c169e50d7415a651bf838f5bcd0fd3bfbd /APEX_1.4/common/include/ApexRand.h | |
| download | physx-3.4-3dfe2108cfab31ba3ee5527e217d0d8e99a51162.tar.xz physx-3.4-3dfe2108cfab31ba3ee5527e217d0d8e99a51162.zip | |
Initial commit:
PhysX 3.4.0 Update @ 21294896
APEX 1.4.0 Update @ 21275617
[CL 21300167]
Diffstat (limited to 'APEX_1.4/common/include/ApexRand.h')
| -rw-r--r-- | APEX_1.4/common/include/ApexRand.h | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/APEX_1.4/common/include/ApexRand.h b/APEX_1.4/common/include/ApexRand.h new file mode 100644 index 00000000..1c075f7e --- /dev/null +++ b/APEX_1.4/common/include/ApexRand.h @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved. + * + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + */ + + +#ifndef APEX_RAND_H +#define APEX_RAND_H + +#include "PxMath.h" +#include "PxVec3.h" +#include "ApexUsingNamespace.h" + +namespace nvidia +{ +namespace apex +{ + +// "Quick and Dirty Symmetric Random number generator" - returns a uniform deviate in [-1.0,1.0) +class QDSRand +{ + uint32_t mSeed; + +public: + + PX_CUDA_CALLABLE PX_INLINE QDSRand(uint32_t seed = 0) : mSeed(seed) {} + + PX_CUDA_CALLABLE PX_INLINE void setSeed(uint32_t seed = 0) + { + mSeed = seed; + } + + PX_CUDA_CALLABLE PX_INLINE uint32_t seed() const + { + return mSeed; + } + + PX_CUDA_CALLABLE PX_INLINE uint32_t nextSeed() + { + mSeed = mSeed * 1664525L + 1013904223L; + return mSeed; + } + + PX_CUDA_CALLABLE PX_INLINE float getNext() + { + union U32F32 + { + uint32_t u; + float f; + } r; + r.u = 0x40000000 | (nextSeed() >> 9); + return r.f - 3.0f; + } + + PX_CUDA_CALLABLE PX_INLINE float getScaled(const float min, const float max) + { + const float scale = (max - min) / 2.0f; + return ((getNext() + 1.0f) * scale) + min; + } + + PX_CUDA_CALLABLE PX_INLINE PxVec3 getScaled(const PxVec3& min, const PxVec3& max) + { + return PxVec3(getScaled(min.x, max.x), getScaled(min.y, max.y), getScaled(min.z, max.z)); + } + + PX_CUDA_CALLABLE PX_INLINE float getUnit() + { + union U32F32 + { + uint32_t u; + float f; + } r; + r.u = 0x3F800000 | (nextSeed() >> 9); + return r.f - 1.0f; + } + +}; + +// "Quick and Dirty Normal Random number generator" - returns normally-distributed values +class QDNormRand +{ + QDSRand mBase; + +public: + + PX_CUDA_CALLABLE PX_INLINE QDNormRand(uint32_t seed = 0) : mBase(seed) {} + + PX_CUDA_CALLABLE PX_INLINE void setSeed(uint32_t seed = 0) + { + mBase.setSeed(seed); + } + + PX_CUDA_CALLABLE PX_INLINE uint32_t setSeed() const + { + return mBase.seed(); + } + + PX_CUDA_CALLABLE PX_INLINE uint32_t nextSeed() + { + return mBase.nextSeed(); + } + + PX_CUDA_CALLABLE PX_INLINE float getNext() + { + //Using Box-Muller transform (see http://en.wikipedia.org/wiki/Box_Muller_transform) + + float u, v, s; + do + { + u = mBase.getNext(); + v = mBase.getNext(); + s = u * u + v * v; + } + while (s >= 1.0); + + return u * PxSqrt(-2.0f * PxLog(s) / s); + } + + PX_CUDA_CALLABLE PX_INLINE float getScaled(const float m, const float s) + { + return m + s * getNext(); + } +}; + +} +} // end namespace nvidia::apex + +#endif |