Visual Computing Library
All Classes Functions Variables Typedefs Enumerations Friends Modules Pages Concepts
perlin_noise.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MATH_PERLIN_NOISE_H
24#define VCL_MATH_PERLIN_NOISE_H
25
26#include <cmath>
27
28// TODO: Improve the documentation of these functions.
29
30namespace vcl {
31
32namespace detail {
33
34inline int perlinNoiseP(int i)
35{
36 static int p[512] = {
37 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233,
38 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10,
39 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252,
40 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87,
41 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48,
42 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230,
43 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25,
44 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169,
45 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186,
46 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126,
47 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189,
48 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70,
49 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98,
50 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
51 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51,
52 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157,
53 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236,
54 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66,
55 215, 61, 156, 180, 151, 160, 137, 91, 90, 15, 131, 13, 201, 95,
56 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99,
57 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26,
58 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88,
59 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165,
60 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122,
61 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102,
62 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187,
63 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100,
64 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202,
65 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16,
66 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2,
67 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22,
68 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104,
69 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179,
70 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31,
71 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4,
72 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141,
73 128, 195, 78, 66, 215, 61, 156, 180};
74 return p[i];
75}
76
77inline double perlinNoiseGrad(int hash, double x, double y, double z)
78{
79 int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
80 double u = h < 8 ? x : y, // INTO 12 GRADIENT DIRECTIONS.
81 v = h < 4 ? y :
82 h == 12 || h == 14 ? x :
83 z;
84 return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
85}
86
87inline double perlinNoiseLerp(double t, double a, double b)
88{
89 return a + t * (b - a);
90}
91
92inline double perlinNoiseFade(double t)
93{
94 return t * t * t * (t * (t * 6 - 15) + 10);
95}
96
97} // namespace detail
98
112inline double perlinNoise(double x, double y, double z)
113{
114 int X = (int) floor(x) & 255, // FIND UNIT CUBE THAT
115 Y = (int) floor(y) & 255, // CONTAINS POINT.
116 Z = (int) floor(z) & 255;
117 x -= floor(x); // FIND RELATIVE X,Y,Z
118 y -= floor(y); // OF POINT IN CUBE.
119 z -= floor(z);
120 double u = detail::perlinNoiseFade(x), // COMPUTE FADE CURVES
121 v = detail::perlinNoiseFade(y), // FOR EACH OF X,Y,Z.
122 w = detail::perlinNoiseFade(z);
123 int A = detail::perlinNoiseP(X) + Y, AA = detail::perlinNoiseP(A) + Z,
124 AB = detail::perlinNoiseP(A + 1) + Z, // HASH COORDINATES OF
125 B = detail::perlinNoiseP(X + 1) + Y, BA = detail::perlinNoiseP(B) + Z,
126 BB = detail::perlinNoiseP(B + 1) + Z; // THE 8 CUBE CORNERS,
127
128 return detail::perlinNoiseLerp(
129 w,
130 detail::perlinNoiseLerp(
131 v,
132 detail::perlinNoiseLerp(
133 u,
134 detail::perlinNoiseGrad(
135 detail::perlinNoiseP(AA), x, y, z), // AND ADD
136 detail::perlinNoiseGrad(
137 detail::perlinNoiseP(BA), x - 1, y, z)), // BLENDED
138 detail::perlinNoiseLerp(
139 u,
140 detail::perlinNoiseGrad(
141 detail::perlinNoiseP(AB), x, y - 1, z), // RESULTS
142 detail::perlinNoiseGrad(
143 detail::perlinNoiseP(BB), x - 1, y - 1, z))), // FROM 8
144 detail::perlinNoiseLerp(
145 v,
146 detail::perlinNoiseLerp(
147 u,
148 detail::perlinNoiseGrad(
149 detail::perlinNoiseP(AA + 1), x, y, z - 1), // CORNERS
150 detail::perlinNoiseGrad(
151 detail::perlinNoiseP(BA + 1), x - 1, y, z - 1)), // OF
152 // CUBE
153 detail::perlinNoiseLerp(
154 u,
155 detail::perlinNoiseGrad(
156 detail::perlinNoiseP(AB + 1), x, y - 1, z - 1),
157 detail::perlinNoiseGrad(
158 detail::perlinNoiseP(BB + 1), x - 1, y - 1, z - 1))));
159}
160
161} // namespace vcl
162
163#endif // VCL_MATH_PERLIN_NOISE_H