Blender V4.3
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
17{
18 if (isfinite(x)) {
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
28namespace Freestyle {
29
31
33{
35 for (uint i = 0; i < NB_VALUE_NOISE; i++) {
36 _values[i] = -1.0 + 2.0 * RandGen::drand48();
37 }
38}
39
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
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
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
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
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 */
#define BLI_assert(a)
Definition BLI_assert.h:50
#define M_PI
unsigned int uint
#define UNLIKELY(x)
static int modf_to_index(Freestyle::real x, uint range)
Class to define a pseudo Perlin noise.
Pseudo-random number generator.
static unsigned long seed
Definition btSoftBody.h:39
real turbulenceLinear(real x, uint nbOctave=8)
static real _values[NB_VALUE_NOISE]
Definition PseudoNoise.h:34
static const uint NB_VALUE_NOISE
Definition PseudoNoise.h:33
static void init(long seed)
real turbulenceSmooth(real x, uint nbOctave=8)
static void srand48(long seedval)
Definition RandGen.cpp:101
static real drand48()
Definition RandGen.cpp:94
ccl_device_inline float2 fabs(const float2 a)
inherits from class Rep
Definition AppCanvas.cpp:20
static const real M_EPSILON
Definition Precision.h:17
static real LanczosWindowed(real t)
double real
Definition Precision.h:14
ccl_device_inline int abs(int x)
Definition util/math.h:120