OpenVDB 13.0.1
Loading...
Searching...
No Matches
OpenSimplexNoise.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4/// @file math/OpenSimplexNoise.h
5///
6/// @authors Francisco Gochez
7///
8/// @brief Methods for generating OpenSimplexNoise (n-dimensional gradient noise)
9///
10/// @details This code is based on https://gist.github.com/tombsar/716134ec71d1b8c1b530
11/// (accessed on 22/05/2019). We have simplified that code in a number of ways,
12/// most notably by removing the template on dimension (this only generates 3
13/// dimensional noise) and removing the base class as it's unnecessary for our
14/// uses. We also assume C++ 2011 or above and have thus removed a number of
15/// ifdef blocks.
16///
17/// The OSN namespace contains the original copyright.
18///
19
20#ifndef OPENVDB_AX_MATH_OPEN_SIMPLEX_NOISE_HAS_BEEN_INCLUDED
21#define OPENVDB_AX_MATH_OPEN_SIMPLEX_NOISE_HAS_BEEN_INCLUDED
22
23#include <openvdb/version.h>
24#include <cstdint>
25
26namespace openvdb {
28namespace OPENVDB_VERSION_NAME {
29
30namespace ax {
31namespace math {
32
33template <typename NoiseT>
34void curlnoise(double (*out)[3], const double (*in)[3])
35{
36 float delta = 0.0001f;
37 float a, b;
38
39 // noise coordinates for vector potential components.
40 float p[3][3] = {
41 { static_cast<float>((*in)[0]) + 000.0f, static_cast<float>((*in)[1]) + 000.0f, static_cast<float>((*in)[2]) + 000.0f }, // x
42 { static_cast<float>((*in)[0]) + 256.0f, static_cast<float>((*in)[1]) - 256.0f, static_cast<float>((*in)[2]) + 256.0f }, // y
43 { static_cast<float>((*in)[0]) - 512.0f, static_cast<float>((*in)[1]) + 512.0f, static_cast<float>((*in)[2]) - 512.0f }, // z
44 };
45
47 // Compute curl.x
48 a = (NoiseT::noise(p[2][0], p[2][1] + delta, p[2][2]) - NoiseT::noise(p[2][0], p[2][1] - delta, p[2][2])) / (2.0f * delta);
49 b = (NoiseT::noise(p[1][0], p[1][1], p[1][2] + delta) - NoiseT::noise(p[1][0], p[1][1], p[1][2] - delta)) / (2.0f * delta);
50 (*out)[0] = a - b;
51
52 // Compute curl.y
53 a = (NoiseT::noise(p[0][0], p[0][1], p[0][2] + delta) - NoiseT::noise(p[0][0], p[0][1], p[0][2] - delta)) / (2.0f * delta);
54 b = (NoiseT::noise(p[2][0] + delta, p[2][1], p[2][2]) - NoiseT::noise(p[2][0] - delta, p[2][1], p[2][2])) / (2.0f * delta);
55 (*out)[1] = a - b;
56
57 // Compute curl.z
58 a = (NoiseT::noise(p[1][0] + delta, p[1][1], p[1][2]) - NoiseT::noise(p[1][0] - delta, p[1][1], p[1][2])) / (2.0f * delta);
59 b = (NoiseT::noise(p[0][0], p[0][1] + delta, p[0][2]) - NoiseT::noise(p[0][0], p[0][1] - delta, p[0][2])) / (2.0f * delta);
60 (*out)[2] = a - b;
62}
63
64template <typename NoiseT>
65void curlnoise(double (*out)[3], double x, double y, double z)
66{
67 const double in[3] = {x, y, z};
68 curlnoise<NoiseT>(out, &in);
69}
70
71}
72}
73}
74}
75
76/// @cond OPENVDB_DOCS_INTERNAL
77
78namespace OSN
79{
80
81// The following is the original copyright notice:
82/*
83 *
84 *
85 * OpenSimplex (Simplectic) Noise in C++
86 * by Arthur Tombs
87 *
88 * Modified 2015-01-08
89 *
90 * This is a derivative work based on OpenSimplex by Kurt Spencer:
91 * https://gist.github.com/KdotJPG/b1270127455a94ac5d19
92 *
93 * Anyone is free to make use of this software in whatever way they want.
94 * Attribution is appreciated, but not required.
95 *
96 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
97 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
98 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
99 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
100 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
101 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
102 * OTHER DEALINGS IN THE SOFTWARE.
103 */
104
105// 3D Implementation of the OpenSimplexNoise generator.
106class OPENVDB_AX_API OSNoise
107{
108public:
109 using inttype = int64_t;
110
111 // Initializes the class using a permutation array generated from a 64-bit seed.
112 // Generates a proper permutation (i.e. doesn't merely perform N successive
113 // pair swaps on a base array).
114 // Uses a simple 64-bit LCG.
115 OSNoise(inttype seed = 0LL);
116
117 OSNoise(const int * p);
118
119 template <typename T>
120 T eval(const T x, const T y, const T z) const;
121
122private:
123
124 template <typename T>
125 inline T extrapolate(const inttype xsb,
126 const inttype ysb,
127 const inttype zsb,
128 const T dx,
129 const T dy,
130 const T dz) const;
131
132 template <typename T>
133 inline T extrapolate(const inttype xsb,
134 const inttype ysb,
135 const inttype zsb,
136 const T dx,
137 const T dy,
138 const T dz,
139 T (&de) [3]) const;
140
141 int mPerm [256];
142 // Array of gradient values for 3D. Values are defined below the class definition.
143 static const int sGradients [72];
144
145 // Because 72 is not a power of two, extrapolate cannot use a bitmask to index
146 // into the perm array. Pre-calculate and store the indices instead.
147 int mPermGradIndex [256];
148};
149
150/// @endcond
151
152}
153
154#endif // OPENVDB_AX_MATH_OPEN_SIMPLEX_NOISE_HAS_BEEN_INCLUDED
#define OPENVDB_AX_API
Definition Platform.h:312
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
Bracket code with OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN/_END, to inhibit warnings about type conve...
Definition Platform.h:244
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_END
Definition Platform.h:245
Definition OpenSimplexNoise.h:31
void curlnoise(double(*out)[3], const double(*in)[3])
Definition OpenSimplexNoise.h:34
Definition Exceptions.h:13
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218