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

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

#include <EBGeometry_AnalyticDistanceFunctions.hpp>

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

Public Member Functions

 PerlinSDF () noexcept
 Default constructor. Constructs a single-octave Perlin noise field with unit amplitude, unit frequency along all axes, and persistence 0.5: the full constructor with those values.
 
 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.
 
PerlinSDF & operator= (const PerlinSDF &)=default
 Copy assignment.
 
PerlinSDF & operator= (PerlinSDF &&)=default
 Move assignment.
 
T signedDistance (const Vec3T< T > &a_point) const noexcept
 Signed distance function. Generates a noise value on [0, m_noiseAmplitude] for one octave.
 
template<class URNG >
void shuffle (URNG &g) noexcept
 Shuffle the permutation table with the provided uniform random number generator.
 
Array< int, 512 > & getPermutationTable () noexcept
 Get the internal permutation table.
 
BoundingVolumes::AABBT< T > computeBoundingVolume () const noexcept
 The shape's axis-aligned bounding box.
 

Static Public Attributes

static constexpr DistanceQuality distanceQuality = DistanceQuality::NotADistance
 How far signedDistance() can be trusted as a distance: not at all. The value is noise, so only its sign means anything.
 

Protected Member Functions

T lerp (const T t, const T a, const T b) const noexcept
 Ken Perlin's linear interpolation helper.
 
T fade (const T t) const noexcept
 Ken Perlin's fade (smoothstep) function: 6t^5 - 15t^4 + 10t^3.
 
T grad (const int hash, const T x, const T y, const T z) const noexcept
 Ken Perlin's gradient hash function.
 
T 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.
 
T m_noiseAmplitude = T(1)
 Noise amplitude (output scale).
 
T m_noisePersistence = T(0.5)
 Per-octave amplitude decay factor.
 
Array< int, 512 > m_permutationTable = {}
 Integer hash permutation table (512 entries: [0..255] mirrored).
 
unsigned int m_noiseOctaves = 1U
 Number of noise octaves.
 

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 divides the spatial frequency by m_noisePersistence (doubling it at the default 0.5) and multiplies the amplitude by m_noisePersistence.

The full constructor shuffles the permutation table with a std::mt19937 seeded with 0, so every PerlinSDF built with the same parameters is the same noise field; call shuffle() for another.

Template Parameters
TFloating-point precision.

Constructor & Destructor Documentation

◆ PerlinSDF()

template<class T >
EBGeometry::PerlinSDF< T >::PerlinSDF ( const T  a_noiseAmplitude,
const Vec3T< T >  a_noiseFrequency,
const T  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, at most 1. A value of zero or less keeps only the first octave, since every later one would have zero amplitude.
[in]a_noiseOctavesNumber of noise octaves. Clamped to >= 1.

Member Function Documentation

◆ computeBoundingVolume()

template<class T >
BoundingVolumes::AABBT< T > EBGeometry::PerlinSDF< T >::computeBoundingVolume ( ) const
inlinenoexcept

The shape's axis-aligned bounding box.

Noise fills all of space, so the box is the whole space.

Returns
The bounding box.

◆ fade()

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

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 >
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 T  x,
const T  y,
const T  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 >
T EBGeometry::PerlinSDF< T >::lerp ( const T  t,
const T  a,
const T  b 
) const
inlineprotectednoexcept

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.

Resets the first 256 entries of the permutation table to 0, ..., 255, permutes them with std::shuffle and then mirrors them 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
inlinenoexcept

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

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

Member Data Documentation

◆ m_permutationTable

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

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

Filled by every constructor (the default constructor delegates to the full one) and refilled by shuffle().


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