Blender
V4.3
source
blender
freestyle
intern
system
PseudoNoise.cpp
Go to the documentation of this file.
1
/* SPDX-FileCopyrightText: 2008-2022 Blender Authors
2
*
3
* SPDX-License-Identifier: GPL-2.0-or-later */
4
10
#include "
BLI_math_base.h
"
11
#include "
BLI_utildefines.h
"
12
13
#include "
PseudoNoise.h
"
14
#include "
RandGen.h
"
15
16
static
int
modf_to_index
(
Freestyle::real
x,
uint
range)
17
{
18
if
(isfinite(x)) {
19
Freestyle::real
tmp;
20
int
i =
abs
(
int
(modf(x, &tmp) * range));
21
BLI_assert
(i >= 0 && i < range);
22
return
i;
23
}
24
25
return
0;
26
}
27
28
namespace
Freestyle
{
29
30
real
PseudoNoise::_values
[];
31
32
void
PseudoNoise::init
(
long
seed
)
33
{
34
RandGen::srand48
(
seed
);
35
for
(
uint
i = 0; i <
NB_VALUE_NOISE
; i++) {
36
_values
[i] = -1.0 + 2.0 *
RandGen::drand48
();
37
}
38
}
39
40
real
PseudoNoise::linearNoise
(
real
x)
41
{
42
real
tmp;
43
int
i =
modf_to_index
(x,
NB_VALUE_NOISE
);
44
real
x1 =
_values
[i], x2 =
_values
[(i + 1) %
NB_VALUE_NOISE
];
45
real
t = modf(x *
NB_VALUE_NOISE
, &tmp);
46
return
x1 * (1 - t) + x2 * t;
47
}
48
49
static
real
LanczosWindowed
(
real
t)
50
{
51
if
(
fabs
(t) > 2) {
52
return
0;
53
}
54
if
(
fabs
(t) <
M_EPSILON
) {
55
return
1.0;
56
}
57
return
sin(
M_PI
* t) / (
M_PI
* t) * sin(
M_PI
* t / 2.0) / (
M_PI
* t / 2.0);
58
}
59
60
real
PseudoNoise::smoothNoise
(
real
x)
61
{
62
real
tmp;
63
int
i =
modf_to_index
(x,
NB_VALUE_NOISE
);
64
int
h = i - 1;
65
if
(
UNLIKELY
(h < 0)) {
66
h =
NB_VALUE_NOISE
+ h;
67
}
68
69
real
x1 =
_values
[i], x2 =
_values
[(i + 1) %
NB_VALUE_NOISE
];
70
real
x0 =
_values
[h], x3 =
_values
[(i + 2) %
NB_VALUE_NOISE
];
71
72
real
t = modf(x *
NB_VALUE_NOISE
, &tmp);
73
real
y0 =
LanczosWindowed
(-1 - t);
74
real
y1 =
LanczosWindowed
(-t);
75
real
y2 =
LanczosWindowed
(1 - t);
76
real
y3 =
LanczosWindowed
(2 - t);
77
#if 0
78
cerr <<
"x0="
<< x0 <<
" x1="
<< x1 <<
" x2="
<< x2 <<
" x3="
<< x3 << endl;
79
cerr <<
"y0="
<< y0 <<
" y1="
<< y1 <<
" y2="
<< y2 <<
" y3="
<< y3 <<
" :"
<< endl;
80
#endif
81
return
(x0 * y0 + x1 * y1 + x2 * y2 + x3 * y3) / (y0 + y1 + y2 + y3);
82
}
83
84
real
PseudoNoise::turbulenceSmooth
(
real
x,
uint
nbOctave)
85
{
86
real
y = 0;
87
real
k = 1.0;
88
for
(
uint
i = 0; i < nbOctave; i++) {
89
y = y + k *
smoothNoise
(x * k);
90
k = k / 2.0;
91
}
92
return
y
;
93
}
94
95
real
PseudoNoise::turbulenceLinear
(
real
x,
uint
nbOctave)
96
{
97
real
y = 0;
98
real
k = 1.0;
99
for
(
uint
i = 0; i < nbOctave; i++) {
100
y = y + k *
linearNoise
(x * k);
101
k = k / 2.0;
102
}
103
return
y
;
104
}
105
106
}
/* namespace Freestyle */
BLI_assert
#define BLI_assert(a)
Definition
BLI_assert.h:50
BLI_math_base.h
M_PI
#define M_PI
Definition
BLI_math_base.h:58
uint
unsigned int uint
Definition
BLI_sys_types.h:68
BLI_utildefines.h
UNLIKELY
#define UNLIKELY(x)
Definition
BLI_utildefines.h:554
modf_to_index
static int modf_to_index(Freestyle::real x, uint range)
Definition
PseudoNoise.cpp:16
PseudoNoise.h
Class to define a pseudo Perlin noise.
RandGen.h
Pseudo-random number generator.
seed
static unsigned long seed
Definition
btSoftBody.h:39
Freestyle::PseudoNoise::turbulenceLinear
real turbulenceLinear(real x, uint nbOctave=8)
Definition
PseudoNoise.cpp:95
Freestyle::PseudoNoise::smoothNoise
real smoothNoise(real x)
Definition
PseudoNoise.cpp:60
Freestyle::PseudoNoise::linearNoise
real linearNoise(real x)
Definition
PseudoNoise.cpp:40
Freestyle::PseudoNoise::_values
static real _values[NB_VALUE_NOISE]
Definition
PseudoNoise.h:34
Freestyle::PseudoNoise::NB_VALUE_NOISE
static const uint NB_VALUE_NOISE
Definition
PseudoNoise.h:33
Freestyle::PseudoNoise::init
static void init(long seed)
Definition
PseudoNoise.cpp:32
Freestyle::PseudoNoise::turbulenceSmooth
real turbulenceSmooth(real x, uint nbOctave=8)
Definition
PseudoNoise.cpp:84
Freestyle::RandGen::srand48
static void srand48(long seedval)
Definition
RandGen.cpp:101
Freestyle::RandGen::drand48
static real drand48()
Definition
RandGen.cpp:94
y
y
Definition
compositor_morphological_blur_info.hh:15
fabs
ccl_device_inline float2 fabs(const float2 a)
Definition
math_float2.h:215
Freestyle
inherits from class Rep
Definition
AppCanvas.cpp:20
Freestyle::M_EPSILON
static const real M_EPSILON
Definition
Precision.h:17
Freestyle::LanczosWindowed
static real LanczosWindowed(real t)
Definition
PseudoNoise.cpp:49
Freestyle::real
double real
Definition
Precision.h:14
abs
ccl_device_inline int abs(int x)
Definition
util/math.h:120
Generated on Thu Feb 6 2025 07:36:39 for Blender by
doxygen
1.11.0