diff options
author | Jacques Lucke <jacques@blender.org> | 2020-07-23 23:30:05 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-07-23 23:30:22 +0300 |
commit | f359672c3589c5c474abe1de78fcf39dd59e3532 (patch) | |
tree | bafb3806a8eab57f2def3b28c64e71450d9bec9b /source/blender/blenlib/BLI_rand.hh | |
parent | c049fe7979de5a3a904a6528878df95ade8e358a (diff) |
Particles: spawn particles on mesh surface
Diffstat (limited to 'source/blender/blenlib/BLI_rand.hh')
-rw-r--r-- | source/blender/blenlib/BLI_rand.hh | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_rand.hh b/source/blender/blenlib/BLI_rand.hh index 612ac0bbe19..7a98ee0f2bb 100644 --- a/source/blender/blenlib/BLI_rand.hh +++ b/source/blender/blenlib/BLI_rand.hh @@ -63,6 +63,15 @@ class RandomNumberGenerator { } /** + * \return Random value (0..N), but never N. + */ + int32_t get_int32(int32_t max_exclusive) + { + BLI_assert(max_exclusive > 0); + return this->get_int32() % max_exclusive; + } + + /** * \return Random value (0..1), but never 1.0. */ double get_double() @@ -78,6 +87,35 @@ class RandomNumberGenerator { return (float)this->get_int32() / 0x80000000; } + template<typename T> void shuffle(MutableSpan<T> values) + { + /* Cannot shuffle arrays of this size yet. */ + BLI_assert(values.size() <= INT32_MAX); + + for (int i = values.size() - 1; i >= 2; i--) { + int j = this->get_int32(i); + if (i != j) { + std::swap(values[i], values[j]); + } + } + } + + /** + * Compute uniformly distributed barycentric coordinates. + */ + float3 get_barycentric_coordinates() + { + float rand1 = this->get_float(); + float rand2 = this->get_float(); + + if (rand1 + rand2 > 1.0f) { + rand1 = 1.0f - rand1; + rand2 = 1.0f - rand2; + } + + return float3(rand1, rand2, 1.0f - rand1 - rand2); + } + float2 get_unit_float2(); float3 get_unit_float3(); float2 get_triangle_sample(float2 v1, float2 v2, float2 v3); |