EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
EBGeometry::PerlinSDF< T > Class Template Reference

Ken Perlin's gradient-noise implicit function. More...

#include <EBGeometry_AnalyticDistanceFunctions.hpp>

Inheritance diagram for EBGeometry::PerlinSDF< T >:
Inheritance graph
[legend]
Collaboration diagram for EBGeometry::PerlinSDF< T >:
Collaboration graph
[legend]

Public Member Functions

 PerlinSDF ()=default
 Default constructor. Constructs a single-octave Perlin noise field with unit amplitude, unit frequency along all axes, and persistence 0.5.
 
 PerlinSDF (const T a_noiseAmplitude, const Vec3T< T > a_noiseFrequency, const T a_noisePersistence, const unsigned int a_noiseOctaves) noexcept
 Full constructor.
 
 PerlinSDF (const PerlinSDF &)=default
 Copy constructor.
 
 PerlinSDF (PerlinSDF &&)=default
 Move constructor.
 
PerlinSDFoperator= (const PerlinSDF &)=default
 Copy assignment.
 
PerlinSDFoperator= (PerlinSDF &&)=default
 Move assignment.
 
 ~PerlinSDF () override=default
 Destructor.
 
signedDistance (const Vec3T< T > &a_point) const noexcept override
 Signed distance function. Generates a noise value on [0, m_noiseAmplitude].
 
template<class URNG >
void shuffle (URNG &g) noexcept
 Shuffle the permutation table with the provided uniform random number generator.
 
std::array< int, 512 > & getPermutationTable () noexcept
 Get the internal permutation table.
 
- Public Member Functions inherited from EBGeometry::SignedDistanceFunction< T >
 SignedDistanceFunction ()=default
 Default constructor.
 
 ~SignedDistanceFunction () override=default
 Destructor.
 
value (const Vec3T< T > &a_point) const noexcept final
 Implementation of ImplicitFunction::value; delegates to signedDistance().
 
virtual Vec3T< T > normal (const Vec3T< T > &a_point, const T &a_delta) const noexcept
 Compute the outward normal using central finite differences.
 
- Public Member Functions inherited from EBGeometry::ImplicitFunction< T >
 ImplicitFunction ()=default
 Default constructor.
 
virtual ~ImplicitFunction ()=default
 Default destructor.
 
operator() (const Vec3T< T > &a_point) const noexcept
 Call operator — alternative signature for the value function.
 
template<class BV >
BV approximateBoundingVolumeOctree (const Vec3T< T > &a_initialLowCorner, const Vec3T< T > &a_initialHighCorner, const unsigned int a_maxTreeDepth, const T &a_safety=0.0) const
 Compute an approximation to the bounding volume for the implicit surface using octree subdivision.
 

Protected Member Functions

virtuallerp (const T t, const T a, const T b) const noexcept
 Ken Perlin's linear interpolation helper.
 
virtualfade (const T t) const noexcept
 Ken Perlin's fade (smoothstep) function: 6t^5 - 15t^4 + 10t^3.
 
grad (const int hash, const T x, const T y, const T z) const noexcept
 Ken Perlin's gradient hash function.
 
noise (const Vec3T< T > &a_point) const noexcept
 Single-octave Perlin noise evaluation.
 

Protected Attributes

Vec3T< T > m_noiseFrequency = Vec3T<T>(T(1), T(1), T(1))
 Spatial noise frequency along each Cartesian axis.
 
m_noiseAmplitude = T(1)
 Noise amplitude (output scale).
 
m_noisePersistence = T(0.5)
 Per-octave amplitude decay factor.
 
std::array< int, 512 > m_permutationTable = {}
 Integer hash permutation table (512 entries: [0..255] mirrored).
 
unsigned int m_noiseOctaves = 1U
 Number of noise octaves.
 

Static Protected Attributes

static constexpr std::array< int, 256 > s_perlinPermutationTable
 Ken Perlin's original 256-entry permutation table.
 

Detailed Description

template<class T>
class EBGeometry::PerlinSDF< T >

Ken Perlin's gradient-noise implicit function.

This class evaluates multi-octave Perlin noise at a 3-D point and returns the result as a scalar. It is not a true signed distance function (the gradient magnitude is not guaranteed to equal 1), but it can serve as a procedural displacement field when added to or subtracted from a conventional SDF.

The output of signedDistance is on the range [0, m_noiseAmplitude] for a single octave. Multiple octaves sum scaled contributions: each successive octave doubles the spatial frequency (controlled by m_noisePersistence dividing the frequency) and attenuates the amplitude by m_noisePersistence.

Note
The default constructor leaves the permutation table all-zeros, producing constant-zero noise. Use the full constructor (which initialises the table from Ken Perlin's reference permutation and then shuffles it) or call shuffle() explicitly before use.
Template Parameters
TFloating-point precision.

Constructor & Destructor Documentation

◆ PerlinSDF()

template<class T >
EBGeometry::PerlinSDF< T >::PerlinSDF ( const a_noiseAmplitude,
const Vec3T< T >  a_noiseFrequency,
const a_noisePersistence,
const unsigned int  a_noiseOctaves 
)
inlinenoexcept

Full constructor.

Parameters
[in]a_noiseAmplitudeNoise amplitude (output scale).
[in]a_noiseFrequencySpatial frequency along each Cartesian axis.
[in]a_noisePersistencePer-octave amplitude decay factor. Clamped to [0, 1].
[in]a_noiseOctavesNumber of noise octaves. Clamped to >= 1.

Member Function Documentation

◆ fade()

template<class T >
virtual T EBGeometry::PerlinSDF< T >::fade ( const t) const
inlineprotectedvirtualnoexcept

Ken Perlin's fade (smoothstep) function: 6t^5 - 15t^4 + 10t^3.

Parameters
[in]tInput in [0,1].
Returns
Smoothed value in [0,1].

◆ getPermutationTable()

template<class T >
std::array< int, 512 > & EBGeometry::PerlinSDF< T >::getPermutationTable ( )
inlinenoexcept

Get the internal permutation table.

Returns
m_permutationTable.

◆ grad()

template<class T >
T EBGeometry::PerlinSDF< T >::grad ( const int  hash,
const x,
const y,
const z 
) const
inlineprotectednoexcept

Ken Perlin's gradient hash function.

Parameters
[in]hashHash value selecting one of 16 gradient directions.
[in]xx-component of the relative position.
[in]yy-component of the relative position.
[in]zz-component of the relative position.
Returns
Dot product of the selected pseudo-random gradient with (x, y, z).

◆ lerp()

template<class T >
virtual T EBGeometry::PerlinSDF< T >::lerp ( const t,
const a,
const b 
) const
inlineprotectedvirtualnoexcept

Ken Perlin's linear interpolation helper.

Parameters
[in]tInterpolation weight in [0,1].
[in]aStart value.
[in]bEnd value.
Returns
a + t*(b - a).

◆ noise()

template<class T >
T EBGeometry::PerlinSDF< T >::noise ( const Vec3T< T > &  a_point) const
inlineprotectednoexcept

Single-octave Perlin noise evaluation.

Parameters
[in]a_pointInput point in noise space.
Returns
Noise value in [-1, 1].

◆ operator=() [1/2]

template<class T >
PerlinSDF & EBGeometry::PerlinSDF< T >::operator= ( const PerlinSDF< T > &  )
default

Copy assignment.

Returns
Reference to (*this).

◆ operator=() [2/2]

template<class T >
PerlinSDF & EBGeometry::PerlinSDF< T >::operator= ( PerlinSDF< T > &&  )
default

Move assignment.

Returns
Reference to (*this).

◆ shuffle()

template<class T >
template<class URNG >
void EBGeometry::PerlinSDF< T >::shuffle ( URNG g)
inlinenoexcept

Shuffle the permutation table with the provided uniform random number generator.

Reseeds the 256-entry permutation table using std::shuffle and then mirrors it into the upper 256 entries. The URNG type must satisfy the C++ UniformRandomBitGenerator concept (e.g., std::mt19937).

Template Parameters
URNGUniform random number generator type (e.g., std::mt19937).
Parameters
[in]gUniform random number generator instance to drive the shuffle.
Note
When using parallel calculations it is exceptionally important that the input RNG produces the same sequence across all threads/ranks. Otherwise, each thread/rank will generate a different permutation table and therefore a different noise field, resulting in no single coherent geometry.

◆ signedDistance()

template<class T >
T EBGeometry::PerlinSDF< T >::signedDistance ( const Vec3T< T > &  a_point) const
inlineoverridevirtualnoexcept

Signed distance function. Generates a noise value on [0, m_noiseAmplitude].

Parameters
[in]a_pointInput point.
Returns
Octave-summed Perlin noise value scaled by m_noiseAmplitude.

Implements EBGeometry::SignedDistanceFunction< T >.

Member Data Documentation

◆ m_permutationTable

template<class T >
std::array<int, 512> EBGeometry::PerlinSDF< T >::m_permutationTable = {}
protected

Integer hash permutation table (512 entries: [0..255] mirrored).

Default-constructed with all zeros (degenerate, constant-zero noise). Use the full constructor or shuffle() to populate with a real permutation.

◆ s_perlinPermutationTable

template<class T >
constexpr std::array<int, 256> EBGeometry::PerlinSDF< T >::s_perlinPermutationTable
staticconstexprprotected
Initial value:
= {
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142,
8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203,
117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165,
71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41,
55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89,
18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250,
124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189,
28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34,
242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31,
181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114,
67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180}

Ken Perlin's original 256-entry permutation table.

Used by the full constructor and shuffle() to seed m_permutationTable. Stored as a static constexpr member so it is available to subclasses without polluting the enclosing namespace.


The documentation for this class was generated from the following file: