Skip to content

Commit

Permalink
Moving random
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy committed Jan 30, 2024
1 parent 93093fb commit caea8ac
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 111 deletions.
124 changes: 122 additions & 2 deletions libs/yocto/yocto_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// # Yocto/Math: Math types
//
// Yocto/Math defines the basic math primitives used in graphics, including
// small-sized vectors, matrices, frames, quaternions, rays, bounding boxes
// and their transforms. Yocto/Math is implemented in `yocto_math.h`.
// small-sized vectors, matrices, frames and their transforms.
// Yocto/Math is implemented in `yocto_math.h`.
// This library includes a stand-alone implementation of the PCG32 random number
// generator by M.E. O'Neill.
//

//
Expand All @@ -29,6 +31,15 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
// LICENSE FOR RANDOM NUMBER GENERATION
//
// This code also includes a small exerpt from http://www.pcg-random.org/
// licensed as follows
// *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org
// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website)
//
//

#ifndef _YOCTO_MATH_H_
#define _YOCTO_MATH_H_
Expand All @@ -43,6 +54,7 @@
#include <limits>
#include <tuple>
#include <utility>
#include <vector>

// -----------------------------------------------------------------------------
// USING DIRECTIVES
Expand All @@ -53,6 +65,7 @@ namespace yocto {
using std::array;
using std::pair;
using std::tuple;
using std::vector;

} // namespace yocto

Expand Down Expand Up @@ -1087,6 +1100,36 @@ inline frame3f camera_fpscam(const frame3f& frame, vec3f transl, vec2f rotate);

} // namespace yocto

// -----------------------------------------------------------------------------
// RANDOM NUMBER GENERATION
// -----------------------------------------------------------------------------
namespace yocto {

// PCG random numbers from http://www.pcg-random.org/
struct rng_state {
uint64_t state = 0x853c49e6748fea9bULL;
uint64_t inc = 0xda3e39cb94b95bdbULL;
};

// Init a random number generator with a state state from the sequence seq.
inline rng_state make_rng(uint64_t seed, uint64_t seq = 1);

// Init a sequence of random number generators.
inline vector<rng_state> make_rngs(
int num, uint64_t seed1 = 961748941ull, uint64_t seed2 = 1301081ull);

// Next random numbers: floats in [0,1), ints in [0,n).
inline int rand1i(rng_state& rng, int n);
inline float rand1f(rng_state& rng);
inline vec2f rand2f(rng_state& rng);
inline vec3f rand3f(rng_state& rng);

// Shuffles a sequence of elements
template <typename T>
inline void shuffle(vector<T>& vals, rng_state& rng);

} // namespace yocto

// -----------------------------------------------------------------------------
// PYTHON-LIKE ITERATORS
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -2708,6 +2751,83 @@ inline void update_fpscam(frame3f& frame, vec3f transl, vec2f rotate) {

} // namespace yocto

// -----------------------------------------------------------------------------
// IMPLEMENTATION FOR RANDOM NUMBER GENERATION
// -----------------------------------------------------------------------------
namespace yocto {

// PCG random numbers from http://www.pcg-random.org/

// Next random number, used internally only.
inline uint32_t _advance_rng(rng_state& rng) {
uint64_t oldstate = rng.state;
rng.state = oldstate * 6364136223846793005ULL + rng.inc;
auto xorshifted = (uint32_t)(((oldstate >> 18u) ^ oldstate) >> 27u);
auto rot = (uint32_t)(oldstate >> 59u);
// return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
return (xorshifted >> rot) | (xorshifted << ((~rot + 1u) & 31));
}

// Init a random number generator with a state state from the sequence seq.
inline rng_state make_rng(uint64_t seed, uint64_t seq) {
auto rng = rng_state();
rng.state = 0U;
rng.inc = (seq << 1u) | 1u;
_advance_rng(rng);
rng.state += seed;
_advance_rng(rng);
return rng;
}

// Init a sequence of random number generators.
inline vector<rng_state> make_rngs(int num, uint64_t seed1, uint64_t seed2) {
auto rng_ = make_rng(seed2);
auto rngs = vector<rng_state>(num);
for (auto& rng : rngs) {
rng = make_rng(seed1, rand1i(rng_, 1 << 31) / 2 + 1);
}
return rngs;
}

// Next random numbers: floats in [0,1), ints in [0,n).
inline int rand1i(rng_state& rng, int n) { return _advance_rng(rng) % n; }
inline float rand1f(rng_state& rng) {
union {
uint32_t u;
float f;
} x;
x.u = (_advance_rng(rng) >> 9) | 0x3f800000u;
return x.f - 1.0f;
// alternate implementation
// const static auto scale = (float)(1.0 / numeric_limits<uint32_t>::max());
// return advance_rng(rng) * scale;
}
inline vec2f rand2f(rng_state& rng) {
// force order of evaluation by using separate assignments.
auto x = rand1f(rng);
auto y = rand1f(rng);
return {x, y};
}
inline vec3f rand3f(rng_state& rng) {
// force order of evaluation by using separate assignments.
auto x = rand1f(rng);
auto y = rand1f(rng);
auto z = rand1f(rng);
return {x, y, z};
}

// Shuffles a sequence of elements
template <typename T>
inline void shuffle(vector<T>& vals, rng_state& rng) {
// https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
for (auto i = (int)vals.size() - 1; i > 0; i--) {
auto j = rand1i(rng, i + 1);
std::swap(vals[j], vals[i]);
}
}

} // namespace yocto

// -----------------------------------------------------------------------------
// PYTHON-LIKE ITERATORS
// -----------------------------------------------------------------------------
Expand Down
109 changes: 0 additions & 109 deletions libs/yocto/yocto_sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// Yocto/Sampling provides many functions to generate points and directions
// useful in path tracing and procedural generation. We also include a random
// number generator suitable for ray tracing.
// This library includes a stand-alone implementaton of the PCG32 random number
// generator by M.E. O'Neill.
//

//
Expand Down Expand Up @@ -73,36 +71,6 @@ using std::vector;
#define inline inline __device__ __forceinline__
#endif

// -----------------------------------------------------------------------------
// RANDOM NUMBER GENERATION
// -----------------------------------------------------------------------------
namespace yocto {

// PCG random numbers from http://www.pcg-random.org/
struct rng_state {
uint64_t state = 0x853c49e6748fea9bULL;
uint64_t inc = 0xda3e39cb94b95bdbULL;
};

// Init a random number generator with a state state from the sequence seq.
inline rng_state make_rng(uint64_t seed, uint64_t seq = 1);

// Init a sequence of random number generators.
inline vector<rng_state> make_rngs(
int num, uint64_t seed1 = 961748941ull, uint64_t seed2 = 1301081ull);

// Next random numbers: floats in [0,1), ints in [0,n).
inline int rand1i(rng_state& rng, int n);
inline float rand1f(rng_state& rng);
inline vec2f rand2f(rng_state& rng);
inline vec3f rand3f(rng_state& rng);

// Shuffles a sequence of elements
template <typename T>
inline void shuffle(vector<T>& vals, rng_state& rng);

} // namespace yocto

// -----------------------------------------------------------------------------
// MONETACARLO SAMPLING FUNCTIONS
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -214,83 +182,6 @@ vector<pair<int, vec2f>> sample_shape(
//
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// IMPLEMENTATION FOR RANDOM NUMBER GENERATION
// -----------------------------------------------------------------------------
namespace yocto {

// PCG random numbers from http://www.pcg-random.org/

// Next random number, used internally only.
inline uint32_t _advance_rng(rng_state& rng) {
uint64_t oldstate = rng.state;
rng.state = oldstate * 6364136223846793005ULL + rng.inc;
auto xorshifted = (uint32_t)(((oldstate >> 18u) ^ oldstate) >> 27u);
auto rot = (uint32_t)(oldstate >> 59u);
// return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
return (xorshifted >> rot) | (xorshifted << ((~rot + 1u) & 31));
}

// Init a random number generator with a state state from the sequence seq.
inline rng_state make_rng(uint64_t seed, uint64_t seq) {
auto rng = rng_state();
rng.state = 0U;
rng.inc = (seq << 1u) | 1u;
_advance_rng(rng);
rng.state += seed;
_advance_rng(rng);
return rng;
}

// Init a sequence of random number generators.
inline vector<rng_state> make_rngs(int num, uint64_t seed1, uint64_t seed2) {
auto rng_ = make_rng(seed2);
auto rngs = vector<rng_state>(num);
for (auto& rng : rngs) {
rng = make_rng(seed1, rand1i(rng_, 1 << 31) / 2 + 1);
}
return rngs;
}

// Next random numbers: floats in [0,1), ints in [0,n).
inline int rand1i(rng_state& rng, int n) { return _advance_rng(rng) % n; }
inline float rand1f(rng_state& rng) {
union {
uint32_t u;
float f;
} x;
x.u = (_advance_rng(rng) >> 9) | 0x3f800000u;
return x.f - 1.0f;
// alternate implementation
// const static auto scale = (float)(1.0 / numeric_limits<uint32_t>::max());
// return advance_rng(rng) * scale;
}
inline vec2f rand2f(rng_state& rng) {
// force order of evaluation by using separate assignments.
auto x = rand1f(rng);
auto y = rand1f(rng);
return {x, y};
}
inline vec3f rand3f(rng_state& rng) {
// force order of evaluation by using separate assignments.
auto x = rand1f(rng);
auto y = rand1f(rng);
auto z = rand1f(rng);
return {x, y, z};
}

// Shuffles a sequence of elements
template <typename T>
inline void shuffle(vector<T>& vals, rng_state& rng) {
// https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
for (auto i = (int)vals.size() - 1; i > 0; i--) {
auto j = rand1i(rng, i + 1);
std::swap(vals[j], vals[i]);
}
}

} // namespace yocto

// -----------------------------------------------------------------------------
// IMPLEMENTATION OF MONTECARLO SAMPLING FUNCTIONS
// -----------------------------------------------------------------------------
Expand Down

0 comments on commit caea8ac

Please sign in to comment.