RavEngine
Loading...
Searching...
No Matches
CmRandom.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 CM_RANDOM_H
30#define CM_RANDOM_H
31
32#include "common/PxPhysXCommonConfig.h"
33#define TEST_MAX_RAND 0xffff
34
35namespace physx
36{
37namespace Cm
38{
40 {
41 public:
42 BasicRandom(PxU32 seed = 0) : mRnd(seed) {}
43 ~BasicRandom() {}
44
45 PX_FORCE_INLINE void setSeed(PxU32 seed) { mRnd = seed; }
46 PX_FORCE_INLINE PxU32 getCurrentValue() const { return mRnd; }
47 PxU32 randomize() { mRnd = mRnd * 2147001325 + 715136305; return mRnd; }
48
49 PX_FORCE_INLINE PxU32 rand() { return randomize() & 0xffff; }
50 PX_FORCE_INLINE PxU32 rand32() { return randomize() & 0xffffffff; }
51
52 PxF32 rand(PxF32 a, PxF32 b)
53 {
54 const PxF32 r = rand32() / (static_cast<PxF32>(0xffffffff));
55 return r * (b - a) + a;
56 }
57
58 PxI32 rand(PxI32 a, PxI32 b)
59 {
60 return a + static_cast<PxI32>(rand32() % (b - a));
61 }
62
63 PxF32 randomFloat()
64 {
65 return rand() / (static_cast<PxF32>(0xffff)) - 0.5f;
66 }
67 PxF32 randomFloat32()
68 {
69 return rand32() / (static_cast<PxF32>(0xffffffff)) - 0.5f;
70 }
71
72 PxF32 randomFloat32(PxReal a, PxReal b) { return rand32() / PxF32(0xffffffff)*(b - a) + a; }
73 void unitRandomPt(physx::PxVec3& v)
74 {
75 v = unitRandomPt();
76 }
77
78 void unitRandomQuat(physx::PxQuat& v)
79 {
80 v = unitRandomQuat();
81 }
82
83 PxVec3 unitRandomPt()
84 {
85 PxVec3 v;
86 do
87 {
88 v.x = randomFloat();
89 v.y = randomFloat();
90 v.z = randomFloat();
91 } while (v.normalize() < 1e-6f);
92 return v;
93 }
94
95 PxQuat unitRandomQuat()
96 {
97 PxQuat v;
98 do
99 {
100 v.x = randomFloat();
101 v.y = randomFloat();
102 v.z = randomFloat();
103 v.w = randomFloat();
104 } while (v.normalize() < 1e-6f);
105
106 return v;
107 }
108
109 private:
110 PxU32 mRnd;
111 };
112
113 //--------------------------------------
114 // Fast, very good random numbers
115 //
116 // Period = 2^249
117 //
118 // Kirkpatrick, S., and E. Stoll, 1981; A Very Fast Shift-Register
119 // Sequence Random Number Generator, Journal of Computational Physics,
120 // V. 40.
121 //
122 // Maier, W.L., 1991; A Fast Pseudo Random Number Generator,
123 // Dr. Dobb's Journal, May, pp. 152 - 157
124
126 {
127 public:
128 RandomR250(PxI32 s)
129 {
130 setSeed(s);
131 }
132
133 void setSeed(PxI32 s)
134 {
135 BasicRandom lcg(s);
136 mIndex = 0;
137
138 PxI32 j;
139 for (j = 0; j < 250; j++) // fill r250 buffer with bit values
140 mBuffer[j] = lcg.randomize();
141
142 for (j = 0; j < 250; j++) // set some MSBs to 1
143 if (lcg.randomize() > 0x40000000L)
144 mBuffer[j] |= 0x80000000L;
145
146 PxU32 msb = 0x80000000; // turn on diagonal bit
147 PxU32 mask = 0xffffffff; // turn off the leftmost bits
148
149 for (j = 0; j < 32; j++)
150 {
151 const PxI32 k = 7 * j + 3; // select a word to operate on
152 mBuffer[k] &= mask; // turn off bits left of the diagonal
153 mBuffer[k] |= msb; // turn on the diagonal bit
154 mask >>= 1;
155 msb >>= 1;
156 }
157 }
158
159 PxU32 randI()
160 {
161 PxI32 j;
162
163 // wrap pointer around
164 if (mIndex >= 147) j = mIndex - 147;
165 else j = mIndex + 103;
166
167 const PxU32 new_rand = mBuffer[mIndex] ^ mBuffer[j];
168 mBuffer[mIndex] = new_rand;
169
170 // increment pointer for next time
171 if (mIndex >= 249) mIndex = 0;
172 else mIndex++;
173
174 return new_rand >> 1;
175 }
176
177 PxReal randUnit()
178 {
179 PxU32 mask = (1 << 23) - 1;
180 return PxF32(randI()&(mask)) / PxF32(mask);
181 }
182
183 PxReal rand(PxReal lower, PxReal upper)
184 {
185 return lower + randUnit() * (upper - lower);
186 }
187
188 private:
189 PxU32 mBuffer[250];
190 PxI32 mIndex;
191 };
192
193 static RandomR250 gRandomR250(0x95d6739b);
194
195 PX_FORCE_INLINE PxU32 Rand()
196 {
197 return gRandomR250.randI() & TEST_MAX_RAND;
198 }
199
200 PX_FORCE_INLINE PxF32 Rand(PxF32 a, PxF32 b)
201 {
202 const PxF32 r = static_cast<PxF32>(Rand()) / (static_cast<PxF32>(TEST_MAX_RAND));
203 return r * (b - a) + a;
204 }
205 PX_FORCE_INLINE PxF32 RandLegacy(PxF32 a, PxF32 b)
206 {
207 const PxF32 r = static_cast<PxF32>(Rand()) / (static_cast<PxF32>(0x7fff) + 1.0f);
208 return r * (b - a) + a;
209 }
210 //returns numbers from [a, b-1]
211 PX_FORCE_INLINE PxI32 Rand(PxI32 a, PxI32 b)
212 {
213 return a + static_cast<PxI32>(Rand() % (b - a));
214 }
215
216 PX_FORCE_INLINE void SetSeed(PxU32 seed)
217 {
218 gRandomR250.setSeed(seed);
219 }
220
221}
222}
223#endif
Definition CmRandom.h:40
Definition CmRandom.h:126
This is a quaternion class. For more information on quaternion mathematics consult a mathematics sour...
Definition PxQuat.h:50
float x
Definition PxQuat.h:395
PX_CUDA_CALLABLE PX_INLINE float normalize()
maps to the closest unit quaternion.
Definition PxQuat.h:227
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize()
normalizes the vector in place
Definition PxVec3.h:300
#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