Blender
V5.0
source
blender
blenlib
BLI_rand.hh
Go to the documentation of this file.
1
/* SPDX-FileCopyrightText: 2023 Blender Authors
2
*
3
* SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9
#pragma once
10
11
#include "
BLI_math_vector_types.hh
"
12
#include "
BLI_span.hh
"
13
14
namespace
blender
{
15
16
class
RandomNumberGenerator
{
17
private
:
18
uint64_t
x_;
19
20
public
:
21
RandomNumberGenerator
(uint32_t
seed
= 0)
22
{
23
this->
seed
(
seed
);
24
}
25
30
static
RandomNumberGenerator
from_random_seed
();
31
35
void
seed
(uint32_t
seed
)
36
{
37
constexpr
uint64_t
lowseed = 0x330E;
38
x_ = (
uint64_t
(
seed
) << 16) | lowseed;
39
}
40
44
void
seed_random
(uint32_t
seed
);
45
46
uint32_t
get_uint32
()
47
{
48
this->
step
();
49
return
uint32_t(x_ >> 17);
50
}
51
52
int32_t
get_int32
()
53
{
54
this->
step
();
55
return
int32_t
(x_ >> 17);
56
}
57
58
uint64_t
get_uint64
()
59
{
60
return
(
uint64_t
(this->
get_uint32
()) << 32) | this->
get_uint32
();
61
}
62
66
int32_t
get_int32
(
int32_t
max_exclusive)
67
{
68
BLI_assert
(max_exclusive > 0);
69
return
this->
get_int32
() % max_exclusive;
70
}
71
75
double
get_double
()
76
{
77
return
double(this->
get_int32
()) / 0x80000000;
78
}
79
83
float
get_float
()
84
{
85
return
(
float
)this->
get_int32
() / 0x80000000;
86
}
87
88
template
<
typename
T>
void
shuffle
(
MutableSpan<T>
values)
89
{
90
/* Cannot shuffle arrays of this size yet. */
91
BLI_assert
(values.
size
() <=
INT32_MAX
);
92
93
for
(
int
i
= values.
size
() - 1;
i
>= 2;
i
--) {
94
int
j = this->
get_int32
(
i
);
95
if
(
i
!= j) {
96
std::swap(values[
i
], values[j]);
97
}
98
}
99
}
100
104
float3
get_barycentric_coordinates
()
105
{
106
float
rand1 = this->
get_float
();
107
float
rand2 = this->
get_float
();
108
109
if
(rand1 + rand2 > 1.0f) {
110
rand1 = 1.0f - rand1;
111
rand2 = 1.0f - rand2;
112
}
113
114
return
float3
(rand1, rand2, 1.0f - rand1 - rand2);
115
}
116
121
int
round_probabilistic
(
float
x
);
122
123
float2
get_unit_float2
();
124
float3
get_unit_float3
();
128
float2
get_triangle_sample
(
float2
v1,
float2
v2
,
float2
v3);
129
float3
get_triangle_sample_3d
(
float3
v1,
float3
v2
,
float3
v3);
130
void
get_bytes
(
MutableSpan<char>
r_bytes);
131
135
void
skip
(
int64_t
n)
136
{
137
while
(n--) {
138
this->
step
();
139
}
140
}
141
142
private
:
143
void
step
()
144
{
145
constexpr
uint64_t
multiplier = 0x5DEECE66Dll;
146
constexpr
uint64_t
addend = 0xB;
147
constexpr
uint64_t
mask
= 0x0000FFFFFFFFFFFFll;
148
149
x_ = (multiplier * x_ + addend) &
mask
;
150
}
151
};
152
153
}
// namespace blender
BLI_assert
#define BLI_assert(a)
Definition
BLI_assert.h:46
x
x
Definition
BLI_expr_pylike_eval_test.cc:345
BLI_math_vector_types.hh
BLI_span.hh
v2
ATTR_WARN_UNUSED_RESULT const BMVert * v2
Definition
bmesh_query_inline.hh:41
int64_t
long long int int64_t
Definition
btConvexHullComputer.cpp:31
int32_t
int int32_t
Definition
btConvexHullComputer.cpp:30
uint64_t
unsigned long long int uint64_t
Definition
btConvexHullComputer.cpp:33
seed
static unsigned long seed
Definition
btSoftBody.h:39
blender::MutableSpan
Definition
BLI_span.hh:443
blender::MutableSpan::size
constexpr int64_t size() const
Definition
BLI_span.hh:493
blender::RandomNumberGenerator
Definition
BLI_rand.hh:16
blender::RandomNumberGenerator::get_bytes
void get_bytes(MutableSpan< char > r_bytes)
Definition
rand.cc:373
blender::RandomNumberGenerator::round_probabilistic
int round_probabilistic(float x)
Definition
rand.cc:306
blender::RandomNumberGenerator::get_uint64
uint64_t get_uint64()
Definition
BLI_rand.hh:58
blender::RandomNumberGenerator::RandomNumberGenerator
RandomNumberGenerator(uint32_t seed=0)
Definition
BLI_rand.hh:21
blender::RandomNumberGenerator::get_barycentric_coordinates
float3 get_barycentric_coordinates()
Definition
BLI_rand.hh:104
blender::RandomNumberGenerator::seed_random
void seed_random(uint32_t seed)
Definition
rand.cc:297
blender::RandomNumberGenerator::get_triangle_sample_3d
float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3)
Definition
rand.cc:354
blender::RandomNumberGenerator::get_double
double get_double()
Definition
BLI_rand.hh:75
blender::RandomNumberGenerator::get_unit_float3
float3 get_unit_float3()
Definition
rand.cc:321
blender::RandomNumberGenerator::get_unit_float2
float2 get_unit_float2()
Definition
rand.cc:315
blender::RandomNumberGenerator::shuffle
void shuffle(MutableSpan< T > values)
Definition
BLI_rand.hh:88
blender::RandomNumberGenerator::get_uint32
uint32_t get_uint32()
Definition
BLI_rand.hh:46
blender::RandomNumberGenerator::get_triangle_sample
float2 get_triangle_sample(float2 v1, float2 v2, float2 v3)
Definition
rand.cc:335
blender::RandomNumberGenerator::skip
void skip(int64_t n)
Definition
BLI_rand.hh:135
blender::RandomNumberGenerator::seed
void seed(uint32_t seed)
Definition
BLI_rand.hh:35
blender::RandomNumberGenerator::get_int32
int32_t get_int32()
Definition
BLI_rand.hh:52
blender::RandomNumberGenerator::get_int32
int32_t get_int32(int32_t max_exclusive)
Definition
BLI_rand.hh:66
blender::RandomNumberGenerator::from_random_seed
static RandomNumberGenerator from_random_seed()
Definition
rand.cc:288
blender::RandomNumberGenerator::get_float
float get_float()
Definition
BLI_rand.hh:83
INT32_MAX
#define INT32_MAX
step
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
mask
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
Definition
math_float2.h:157
blender
Definition
ANIM_action.hh:36
blender::float2
VecBase< float, 2 > float2
Definition
BLI_math_vector_types.hh:618
blender::float3
VecBase< float, 3 > float3
Definition
BLI_math_vector_types.hh:619
i
i
Definition
text_draw.cc:230
Generated on
for Blender by
doxygen
1.16.1