Smooth 2D Noise Viewer
Perlin and Value Noise
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CPerlinNoise2D Class Reference

2D Perlin and Value noise generator. More...

#include <perlin.h>

Public Member Functions

 CPerlinNoise2D ()
 Constructor. More...
 
 ~CPerlinNoise2D ()
 Destructor. More...
 
const float generate (float, float, eNoise, size_t, float=0.5f, float=2.0f) const
 Generate noise at a point. More...
 
void SetSeed ()
 Set seed for PRNG. More...
 
void RandomizeTable (eDistribution)
 Randomize table from distribution. More...
 
bool DoubleTableSize ()
 Double table size. More...
 
bool HalveTableSize ()
 Halve table size. More...
 
bool DefaultTableSize ()
 Set table size to default. More...
 
void SetSpline (eSpline)
 Set spline function. More...
 
void SetHash (eHash)
 Set hash function. More...
 
const size_t GetTableSize () const
 Get table size. More...
 
const size_t GetMinTableSize () const
 Get minimum table size. More...
 
const size_t GetMaxTableSize () const
 Get maximum table size. More...
 
const size_t GetDefTableSize () const
 Get default table size. More...
 
const eHash GetHash () const
 Get hash function type. More...
 
const eSpline GetSpline () const
 Get spline function type. More...
 
const eDistribution GetDistribution () const
 Get distribution type. More...
 

Private Member Functions

const size_t pair (size_t, size_t) const
 Perlin pairing function. More...
 
const size_t pairstd (size_t, size_t) const
 Std pairing function. More...
 
const size_t hash (size_t) const
 Perlin hash function. More...
 
const size_t hashstd (size_t) const
 std::hash function. More...
 
const size_t hash2 (size_t, size_t) const
 Hash function. More...
 
void HashCorners (size_t, size_t, size_t[4]) const
 Hash grid corners. More...
 
void RandomizeTableUniform ()
 Randomize table using uniform distribution. More...
 
void RandomizeTableCos ()
 Randomize table using cosine. More...
 
void RandomizeTableNormal ()
 Randomize table using normal distribution. More...
 
void RandomizeTableExp ()
 Randomize table using exponential distribution. More...
 
void RandomizeTableMaximal ()
 Randomize table using large magnitude values. More...
 
void RandomizeTableMidpoint (size_t, size_t, float)
 Midpoint displacement. More...
 
void RandomizeTableMidpoint ()
 Randomize table using midpoint displacement. More...
 
const float spline (float) const
 Spline curve. More...
 
const float z (size_t, float, float, eNoise) const
 Apply gradients. More...
 
const float Lerp (float, float, float, size_t *, eNoise) const
 Linear interpolation. More...
 
const float noise (float, float, eNoise) const
 Perlin noise. More...
 
void RandomizePermutation ()
 Randomize permutation. More...
 
void Initialize ()
 Initialize. More...
 

Private Attributes

eHash m_eHash = eHash::Permutation
 Hash function type. More...
 
eSpline m_eSpline = eSpline::Cubic
 Spline function type. More...
 
eDistribution m_eDistribution = eDistribution::Uniform
 Uniform distribution.. More...
 
size_t * m_nPerm = nullptr
 Random permutation, used for hash function. More...
 
float * m_fTable = nullptr
 Table of gradients or values. More...
 
std::default_random_engine m_stdRandom
 PRNG. More...
 
UINT m_nSeed = 0
 PRNG seed. More...
 
const size_t m_nDefTableSize = 256
 Default table size. More...
 
const size_t m_nMinTableSize = 16
 Min table size. More...
 
const size_t m_nMaxTableSize = 1024
 Max table size. More...
 
size_t m_nSize = m_nDefTableSize
 Table size, must be a power of 2. More...
 
size_t m_nMask = m_nDefTableSize - 1
 Mask for values less than m_nSize. More...
 

Detailed Description

This implementation of a Perlin noise generator can generate either Perlin or Value noise using gradients or values (respectively) from a table that can be filled with pseudo-random numbers from various probability distributions. There is a choice of hash functions including Perlin's original pseudo-random permutation method and various non-repeating hash functions. There is a choice of spline functions including cubic and quintic splines. The table size can be doubled or halved within hard-coded limits. The source of pseudo-randomness is std::default_random_engine

Definition at line 48 of file perlin.h.

Constructor & Destructor Documentation

◆ CPerlinNoise2D()

CPerlinNoise2D::CPerlinNoise2D ( )

Set the PRNG seed and initialize.

Definition at line 42 of file perlin.cpp.

◆ ~CPerlinNoise2D()

CPerlinNoise2D::~CPerlinNoise2D ( )

Deletes the permutation and gradient/value table.

Definition at line 49 of file perlin.cpp.

Member Function Documentation

◆ DefaultTableSize()

bool CPerlinNoise2D::DefaultTableSize ( )

Set table size to the default and call Initialize() to re-initialize.

Returns
true if the table size changed.

Definition at line 278 of file perlin.cpp.

◆ DoubleTableSize()

bool CPerlinNoise2D::DoubleTableSize ( )

Double the size of the permutation and gradient/value tables up to a maximum of m_nMaxTableSize and call Initialize() to re-initialize.

Returns
true if the table size changed.

Definition at line 245 of file perlin.cpp.

◆ generate()

const float CPerlinNoise2D::generate ( float  x,
float  y,
eNoise  t,
size_t  n,
float  alpha = 0.5f,
float  beta = 2.0f 
) const

Add multiple octaves of Perlin or Value noise to compute an effect similar to turbulence at a single point. Each successive octave has its amplitude multiplied by a value called the lacunarity and its frequency multiplied by a value called the persistence. These are usually set to 0.5 and 2.0, respectively.

Parameters
xX-coordinate of a 2D point.
yY-coordinate of a 2D point.
tNoise type.
nNumber of octaves.
alphaLacunarity. Defaults to 0.5f.
betaPersistence. Defaults to 2.0f.
Returns
Smooth noise in \([-1, 1]\) at point \((\mathsf{x}, \mathsf{y})\).

Definition at line 548 of file perlin.cpp.

◆ GetDefTableSize()

const size_t CPerlinNoise2D::GetDefTableSize ( ) const

Reader function for the default table size.

Returns
The default table size.

Definition at line 602 of file perlin.cpp.

◆ GetDistribution()

const eDistribution CPerlinNoise2D::GetDistribution ( ) const

Reader function for the distribution type.

Returns
The distribution type.

Definition at line 623 of file perlin.cpp.

◆ GetHash()

const eHash CPerlinNoise2D::GetHash ( ) const

Reader function for the hash function type.

Returns
The hash function type.

Definition at line 609 of file perlin.cpp.

◆ GetMaxTableSize()

const size_t CPerlinNoise2D::GetMaxTableSize ( ) const

Reader function for the maximum table size.

Returns
The maximum table size.

Definition at line 595 of file perlin.cpp.

◆ GetMinTableSize()

const size_t CPerlinNoise2D::GetMinTableSize ( ) const

Reader function for the minimum table size.

Returns
The minimum table size.

Definition at line 588 of file perlin.cpp.

◆ GetSpline()

const eSpline CPerlinNoise2D::GetSpline ( ) const

Reader function for the spline function type.

Returns
The spline function type.

Definition at line 616 of file perlin.cpp.

◆ GetTableSize()

const size_t CPerlinNoise2D::GetTableSize ( ) const

Reader function for the table size.

Returns
The table size.

Definition at line 581 of file perlin.cpp.

◆ HalveTableSize()

bool CPerlinNoise2D::HalveTableSize ( )

Halve the size of the permutation and gradient/value tables down to a minimum of m_nMinTableSize and call Initialize() to re-initialize.

Returns
true if the table size changed.

Definition at line 262 of file perlin.cpp.

◆ hash()

const size_t CPerlinNoise2D::hash ( size_t  x) const
inlineprivate

Perlin's hash function, which uses a random permutation. Note that this means that it repeats with a period of m_nSize.

Parameters
xA number.
Returns
Hashed number in the range [0, m_nSize - 1].

Definition at line 367 of file perlin.cpp.

◆ hash2()

const size_t CPerlinNoise2D::hash2 ( size_t  x,
size_t  y 
) const
inlineprivate

A 2D linear congruential hash function. For fixed primes \(p_0, p_1, p_2\) and parameters \(x, y\), this function returns \((p_0x + p_1y) \bmod p_2 \) right-shifted by 8 bits and masked with m_nMask.

Parameters
xA number.
yA number.
Returns
Hashed number in the range [0, m_nSize - 1].

Definition at line 389 of file perlin.cpp.

◆ HashCorners()

void CPerlinNoise2D::HashCorners ( size_t  x,
size_t  y,
size_t  c[4] 
) const
private

Get hash values at grid corners (at whole number coordinates).

Parameters
xX-coordinate.
yY-coordinate.
c[OUT] Array of four hash values for corners in row-major order.

Definition at line 404 of file perlin.cpp.

◆ hashstd()

const size_t CPerlinNoise2D::hashstd ( size_t  x) const
inlineprivate

A hash function using std::hash. Note: The C++ Standard does not require that std::hash be a good hash function, but we'll assume that it is. Most implementations are.

Parameters
xA number.
Returns
Hashed number in the range [0, m_nSize - 1].

Definition at line 377 of file perlin.cpp.

◆ Initialize()

void CPerlinNoise2D::Initialize ( )
private

Initialize the generator. Assumes that m_nSize has set initialized to the table size, which must be a power of two. Create and initialize the permutation and gradient/value tables. Initialize the bit mask.

Definition at line 58 of file perlin.cpp.

◆ Lerp()

const float CPerlinNoise2D::Lerp ( float  sX,
float  fX,
float  fY,
size_t *  c,
eNoise  t 
) const
private

Linear interpolation of gradients or heights (depending on whether we're generating Perlin or Value noise) along the X-axis.

Parameters
sXSmoothed fractional part of X-coordinate.
fXFractional part of X-coordinate (ignored in Value noise).
fYFractional part of Y-coordinate (ignored in Value noise).
cArray of two gradients at grid points along X-axis.
tNoise type.
Returns
Linearly interpolated gradient or value.

Definition at line 466 of file perlin.cpp.

◆ noise()

const float CPerlinNoise2D::noise ( float  x,
float  y,
eNoise  t 
) const
private

Compute a single octave of Perlin or Value noise at a 2D point.

Parameters
xX-coordinate of point.
yY-coordinate of point.
tNoise type.
Returns
A smoothed noise value in [-1, 1] at the given point.

Definition at line 506 of file perlin.cpp.

◆ pair()

const size_t CPerlinNoise2D::pair ( size_t  x,
size_t  y 
) const
inlineprivate

Perlin's pairing function, which combines two unsigned integers into one.

Parameters
xFirst unsigned integer.
ySecond unsigned integer.
Returns
An unsigned integer that depends upon x and y.

Definition at line 346 of file perlin.cpp.

◆ pairstd()

const size_t CPerlinNoise2D::pairstd ( size_t  x,
size_t  y 
) const
inlineprivate

Alternate version of Perlin's pairing function. Note: This function depends on the implementation of std::hash. The C++ Standard does not require that it be a good hash function, but we'll assume that it is. Most implementations are.

Parameters
xFirst number.
ySecond number.
Returns
An unsigned integer that combines the parameters into a single number.

Definition at line 358 of file perlin.cpp.

◆ RandomizePermutation()

void CPerlinNoise2D::RandomizePermutation ( )
private

Use the standard algorithm to set the permutation in m_nPerm to a pseudo-random permutation with each permutation equally likely . The source of randomness is std::default_random_engine with std::uniform_int_distribution. This function should be called during initialization and when the table size changes. The pseudo-random number generator is re-seeded from m_nSeed before the permutation is generated. This means that the permutation used for each table size remains the same until m_nSeed is changed.

Definition at line 86 of file perlin.cpp.

◆ RandomizeTable()

void CPerlinNoise2D::RandomizeTable ( eDistribution  d)

Set m_fTable to pseudo-random values in \([-1, 1]\) according to some probability distribution. The pseudo-random number generator is re-seeded from m_nSeed before the table is generated. This means that the table contents for each table size remains the same until m_nSeed is changed.

Parameters
dProbability distribution enumerated type.

Definition at line 227 of file perlin.cpp.

◆ RandomizeTableCos()

void CPerlinNoise2D::RandomizeTableCos ( )
private

Fill the gradient/value table m_fTable using a cosine distribution. The source of randomness is std::default_random_engine with std::uniform_real_distribution<float>(0.0f, 1.0f). It simply multiplies the each pseudo-random number by \(pi\) and enters the cosine of the result into the table.

Definition at line 194 of file perlin.cpp.

◆ RandomizeTableExp()

void CPerlinNoise2D::RandomizeTableExp ( )
private

Fill the gradient/value table m_fTable using an exponential distribution. The source of randomness is std::default_random_engine with std::exponential_distribution<float>(8.0f). It fills half of the table with negative gradients and half with positive gradients.

Definition at line 208 of file perlin.cpp.

◆ RandomizeTableMaximal()

void CPerlinNoise2D::RandomizeTableMaximal ( )
private

Fill the gradient/value table m_fTable with large magnitude entries, that is, either -1 ot +1, using a uniform distribution. The source of randomness is std::default_random_engine with std::uniform_real_distribution<float>(-1.0f, 1.0f).

Definition at line 166 of file perlin.cpp.

◆ RandomizeTableMidpoint() [1/2]

void CPerlinNoise2D::RandomizeTableMidpoint ( )
private

Fill the gradient/value table m_fTable using midpoint displacement. This function fills in the first and last entries then calls the recursive RandomizeTableMidpoint(size_t, size_t, float) to fill in the rest.

Definition at line 141 of file perlin.cpp.

◆ RandomizeTableMidpoint() [2/2]

void CPerlinNoise2D::RandomizeTableMidpoint ( size_t  i,
size_t  j,
float  alpha 
)
private

Initialize a chunk of the gradient/value table m_fTable using midpoint displacement. Given \(\mathsf{i}\) and \(\mathsf{j}\) such that \(\mathsf{j} > \mathsf{i}+1\) and \(0 \leq \mathsf{i}, \mathsf{j} \leq\) m_nSize, where \(\mathsf{j} - \mathsf{i}\) is a power of 2, this function assumes that m_fTable \([\mathsf{i}]\) and m_fTable \([\mathsf{j}]\) have been set, and fills in the entries m_fTable \([\mathsf{i} + 1]\) through m_fTable \([\mathsf{j}-1]\) recursively using midpoint displacement. That is, it sets the middle entry to the average of the first and last entry offset by a pseudo-random value in \([-1,1]\) times a value called the lacunarity. The function then halves the lacunarity and calls itself recursively on the top and bottom halves of the table chunk. The source of randomness is std::default_random_engine with std::uniform_real_distribution<float>(-1.0f, 1.0f).

Parameters
iLower index.
jUpper index.
alphaLacunarity.

Definition at line 116 of file perlin.cpp.

◆ RandomizeTableNormal()

void CPerlinNoise2D::RandomizeTableNormal ( )
private

Fill the gradient/value table m_fTable using a normal distribution. The source of randomness is std::default_random_engine with std::normal_distribution<float>(500.0f, 200.0f).

Definition at line 179 of file perlin.cpp.

◆ RandomizeTableUniform()

void CPerlinNoise2D::RandomizeTableUniform ( )
private

Fill the gradient/value table m_fTable using a uniform distribution. The source of randomness is std::default_random_engine with std::uniform_real_distribution<float>(-1.0f, 1.0f).

Definition at line 152 of file perlin.cpp.

◆ SetHash()

void CPerlinNoise2D::SetHash ( eHash  d)

Set the hash function type.

Parameters
dHash function enumerated type.

Definition at line 309 of file perlin.cpp.

◆ SetSeed()

void CPerlinNoise2D::SetSeed ( )

Set the pseudo-random number generator seed to timeGetTime(), the number of milliseconds since Windows last rebooted. This should be sufficiently unpredictable to make a good seed.

Definition at line 295 of file perlin.cpp.

◆ SetSpline()

void CPerlinNoise2D::SetSpline ( eSpline  d)

Set the spline function type.

Parameters
dSpline function enumerated type.

Definition at line 302 of file perlin.cpp.

◆ spline()

const float CPerlinNoise2D::spline ( float  x) const
inlineprivate

Compute a spline function. Depending on the value of m_eSpline this will be either identity function, a cubic spline, or a quintic spline.

Parameters
xA float in the range \([-1, 1]\).
Returns
The spline of \(\mathsf{x}\) in the range \([-1, 1]\).

Definition at line 325 of file perlin.cpp.

◆ z()

const float CPerlinNoise2D::z ( size_t  h,
float  x,
float  y,
eNoise  t 
) const
inlineprivate

For Perlin noise, multiply hashed gradient from m_fTable by coordinates. Use the hash value parameter to index into the gradient table for the X gradient and rehash it for the index of the Y gradient. Add these gradients multiplied by the fractional values of the position (that is, return \(z = x \frac{dz}{dx} + y\frac{dz}{dy}\)). For Value noise, just read the \(z\) value directly from the table.

Parameters
hHash value for gradient table index.
xX-coordinate of point in the range \([-1, 1]\).
yY-coordinate of point in the range \([-1, 1]\).
tNoise type.
Returns
Corresponding Z-value.

Definition at line 435 of file perlin.cpp.

Member Data Documentation

◆ m_eDistribution

eDistribution CPerlinNoise2D::m_eDistribution = eDistribution::Uniform
private

Definition at line 52 of file perlin.h.

◆ m_eHash

eHash CPerlinNoise2D::m_eHash = eHash::Permutation
private

Definition at line 50 of file perlin.h.

◆ m_eSpline

eSpline CPerlinNoise2D::m_eSpline = eSpline::Cubic
private

Definition at line 51 of file perlin.h.

◆ m_fTable

float* CPerlinNoise2D::m_fTable = nullptr
private

Definition at line 55 of file perlin.h.

◆ m_nDefTableSize

const size_t CPerlinNoise2D::m_nDefTableSize = 256
private

Definition at line 60 of file perlin.h.

◆ m_nMask

size_t CPerlinNoise2D::m_nMask = m_nDefTableSize - 1
private

Definition at line 65 of file perlin.h.

◆ m_nMaxTableSize

const size_t CPerlinNoise2D::m_nMaxTableSize = 1024
private

Definition at line 62 of file perlin.h.

◆ m_nMinTableSize

const size_t CPerlinNoise2D::m_nMinTableSize = 16
private

Definition at line 61 of file perlin.h.

◆ m_nPerm

size_t* CPerlinNoise2D::m_nPerm = nullptr
private

Definition at line 54 of file perlin.h.

◆ m_nSeed

UINT CPerlinNoise2D::m_nSeed = 0
private

Definition at line 58 of file perlin.h.

◆ m_nSize

size_t CPerlinNoise2D::m_nSize = m_nDefTableSize
private

Definition at line 64 of file perlin.h.

◆ m_stdRandom

std::default_random_engine CPerlinNoise2D::m_stdRandom
private

Definition at line 57 of file perlin.h.