Blender
V4.3
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
9
#pragma once
10
11
#include "
BLI_math_vector_types.hh
"
12
#include "
BLI_span.hh
"
13
#include "
BLI_utildefines.h
"
14
15
namespace
blender
{
16
17
class
RandomNumberGenerator
{
18
private
:
19
uint64_t
x_;
20
21
public
:
22
RandomNumberGenerator
(
uint32_t
seed
= 0)
23
{
24
this->
seed
(
seed
);
25
}
26
31
static
RandomNumberGenerator
from_random_seed
();
32
36
void
seed
(
uint32_t
seed
)
37
{
38
constexpr
uint64_t
lowseed = 0x330E;
39
x_
= (
uint64_t
(
seed
) << 16) | lowseed;
40
}
41
45
void
seed_random
(
uint32_t
seed
);
46
47
uint32_t
get_uint32
()
48
{
49
this->step();
50
return
uint32_t
(
x_
>> 17);
51
}
52
53
int32_t
get_int32
()
54
{
55
this->step();
56
return
int32_t
(
x_
>> 17);
57
}
58
59
uint64_t
get_uint64
()
60
{
61
return
(
uint64_t
(this->
get_uint32
()) << 32) | this->
get_uint32
();
62
}
63
67
int32_t
get_int32
(
int32_t
max_exclusive)
68
{
69
BLI_assert
(max_exclusive > 0);
70
return
this->
get_int32
() % max_exclusive;
71
}
72
76
double
get_double
()
77
{
78
return
double
(this->
get_int32
()) / 0x80000000;
79
}
80
84
float
get_float
()
85
{
86
return
(
float
)this->
get_int32
() / 0x80000000;
87
}
88
89
template
<
typename
T>
void
shuffle
(
MutableSpan<T>
values)
90
{
91
/* Cannot shuffle arrays of this size yet. */
92
BLI_assert
(values.size() <=
INT32_MAX
);
93
94
for
(
int
i = values.size() - 1; i >= 2; i--) {
95
int
j = this->
get_int32
(i);
96
if
(i != j) {
97
std::swap(values[i], values[j]);
98
}
99
}
100
}
101
105
float3
get_barycentric_coordinates
()
106
{
107
float
rand1 = this->
get_float
();
108
float
rand2 = this->
get_float
();
109
110
if
(rand1 + rand2 > 1.0f) {
111
rand1 = 1.0f - rand1;
112
rand2 = 1.0f - rand2;
113
}
114
115
return
float3
(rand1, rand2, 1.0f - rand1 - rand2);
116
}
117
122
int
round_probabilistic
(
float
x);
123
124
float2
get_unit_float2
();
125
float3
get_unit_float3
();
129
float2
get_triangle_sample
(
float2
v1,
float2
v2
,
float2
v3);
130
float3
get_triangle_sample_3d
(
float3
v1,
float3
v2
,
float3
v3);
131
void
get_bytes
(
MutableSpan<char>
r_bytes);
132
136
void
skip
(
int64_t
n)
137
{
138
while
(n--) {
139
this->step();
140
}
141
}
142
143
private
:
144
void
step()
145
{
146
constexpr
uint64_t
multiplier = 0x5DEECE66Dll;
147
constexpr
uint64_t
addend = 0xB;
148
constexpr
uint64_t
mask = 0x0000FFFFFFFFFFFFll;
149
150
x_
= (multiplier *
x_
+ addend) & mask;
151
}
152
};
153
154
}
// namespace blender
BLI_assert
#define BLI_assert(a)
Definition
BLI_assert.h:50
BLI_math_vector_types.hh
BLI_span.hh
BLI_utildefines.h
double
typedef double(DMatrix)[4][4]
v2
ATTR_WARN_UNUSED_RESULT const BMVert * v2
Definition
bmesh_query_inline.hh:35
seed
static unsigned long seed
Definition
btSoftBody.h:39
blender::MutableSpan
Definition
BLI_span.hh:444
blender::RandomNumberGenerator
Definition
BLI_rand.hh:17
blender::RandomNumberGenerator::get_bytes
void get_bytes(MutableSpan< char > r_bytes)
Definition
rand.cc:450
blender::RandomNumberGenerator::round_probabilistic
int round_probabilistic(float x)
Definition
rand.cc:383
blender::RandomNumberGenerator::get_uint64
uint64_t get_uint64()
Definition
BLI_rand.hh:59
blender::RandomNumberGenerator::RandomNumberGenerator
RandomNumberGenerator(uint32_t seed=0)
Definition
BLI_rand.hh:22
blender::RandomNumberGenerator::get_barycentric_coordinates
float3 get_barycentric_coordinates()
Definition
BLI_rand.hh:105
blender::RandomNumberGenerator::seed_random
void seed_random(uint32_t seed)
Definition
rand.cc:374
blender::RandomNumberGenerator::get_triangle_sample_3d
float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3)
Definition
rand.cc:431
blender::RandomNumberGenerator::get_double
double get_double()
Definition
BLI_rand.hh:76
blender::RandomNumberGenerator::get_unit_float3
float3 get_unit_float3()
Definition
rand.cc:398
blender::RandomNumberGenerator::get_unit_float2
float2 get_unit_float2()
Definition
rand.cc:392
blender::RandomNumberGenerator::shuffle
void shuffle(MutableSpan< T > values)
Definition
BLI_rand.hh:89
blender::RandomNumberGenerator::get_uint32
uint32_t get_uint32()
Definition
BLI_rand.hh:47
blender::RandomNumberGenerator::get_triangle_sample
float2 get_triangle_sample(float2 v1, float2 v2, float2 v3)
Definition
rand.cc:412
blender::RandomNumberGenerator::skip
void skip(int64_t n)
Definition
BLI_rand.hh:136
blender::RandomNumberGenerator::seed
void seed(uint32_t seed)
Definition
BLI_rand.hh:36
blender::RandomNumberGenerator::get_int32
int32_t get_int32()
Definition
BLI_rand.hh:53
blender::RandomNumberGenerator::get_int32
int32_t get_int32(int32_t max_exclusive)
Definition
BLI_rand.hh:67
blender::RandomNumberGenerator::from_random_seed
static RandomNumberGenerator from_random_seed()
Definition
rand.cc:365
blender::RandomNumberGenerator::get_float
float get_float()
Definition
BLI_rand.hh:84
x_
const Mat x_
Definition
fundamental.cc:446
blender
Definition
ANIM_action.hh:36
blender::float3
VecBase< float, 3 > float3
Definition
BLI_math_vector_types.hh:612
INT32_MAX
#define INT32_MAX
Definition
stdint.h:137
uint32_t
unsigned int uint32_t
Definition
stdint.h:80
int64_t
__int64 int64_t
Definition
stdint.h:89
int32_t
signed int int32_t
Definition
stdint.h:77
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:90
blender::VecBase< float, 3 >
Generated on Thu Feb 6 2025 07:36:39 for Blender by
doxygen
1.11.0