Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Functions | Variables
SimplexNoise.cpp File Reference

A Perlin Simplex Noise C++ Implementation (1D, 2D, 3D, 4D). More...

#include "SimplexNoise.h"
#include <cstdint>

Functions

static int32_t fastfloor (float fp)
 
static uint8_t hash (int32_t i)
 
static float grad (int32_t hash, float x)
 
static float grad (int32_t hash, float x, float y)
 

Variables

static const uint8_t perm [256]
 

Detailed Description

A Perlin Simplex Noise C++ Implementation (1D, 2D, 3D, 4D).

Copyright (c) 2014-2015 Sebastien Rombauts (sebas.nosp@m.tien.nosp@m..romb.nosp@m.auts.nosp@m.@gmai.nosp@m.l.co.nosp@m.m)

This C++ implementation is based on the speed-improved Java version 2012-03-09 by Stefan Gustavson (original Java source code in the public domain). http://webstaff.itn.liu.se/~stegu/simplexnoise/SimplexNoise.java:

This implementation is "Simplex Noise" as presented by Ken Perlin at a relatively obscure and not often cited course session "Real-Time Shading" at Siggraph 2001 (before real time shading actually took on), under the title "hardware noise". The 3D function is numerically equivalent to his Java reference code available in the PDF course notes, although I re-implemented it from scratch to get more readable code. The 1D, 2D and 4D cases were implemented from scratch by me from Ken Perlin's text.

Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt or copy at http://opensource.org/licenses/MIT)

Function Documentation

static int32_t fastfloor ( float  fp)
inlinestatic

Computes the largest integer value not greater than the float one

This method is faster than using (int32_t)std::floor(fp).

I measured it to be approximately twice as fast: float: ~18.4ns instead of ~39.6ns on an AMD APU), double: ~20.6ns instead of ~36.6ns on an AMD APU), Reference: http://www.codeproject.com/Tips/700780/Fast-floor-ceiling-functions

Parameters
[in]fpfloat input value
Returns
largest integer value not greater than fp
static uint8_t hash ( int32_t  i)
inlinestatic

Helper function to hash an integer using the above permutation table

This inline function costs around 1ns, and is called N+1 times for a noise of N dimension.

Using a real hash function would be better to improve the "repeatability of 256" of the above permutation table, but fast integer Hash functions uses more time and have bad random properties.

Parameters
[in]iInteger value to hash
Returns
8-bits hashed value
static float grad ( int32_t  hash,
float  x 
)
static

Helper function to compute gradients-dot-residual vectors (1D)

Note
that these generate gradients of more than unit length. To make a close match with the value range of classic Perlin noise, the final noise values need to be rescaled to fit nicely within [-1,1]. (The simplex noise functions as such also have different scaling.) Note also that these noise functions are the most practical and useful signed version of Perlin noise.
Parameters
[in]hashhash value
[in]xdistance to the corner
Returns
gradient value
static float grad ( int32_t  hash,
float  x,
float  y 
)
static

Helper functions to compute gradients-dot-residual vectors (2D)

Parameters
[in]hashhash value
[in]xx coord of the distance to the corner
[in]yy coord of the distance to the corner
Returns
gradient value

Variable Documentation

const uint8_t perm[256]
static
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
}

Permutation table. This is just a random jumble of all numbers 0-255.

This produce a repeatable pattern of 256, but Ken Perlin stated that it is not a problem for graphic texture as the noise features disappear at a distance far enough to be able to see a repeatable pattern of 256.

This needs to be exactly the same for all instances on all platforms, so it's easiest to just keep it as static explicit data. This also removes the need for any initialisation of this class.

Note that making this an uint32_t[] instead of a uint8_t[] might make the code run faster on platforms with a high penalty for unaligned single byte addressing. Intel x86 is generally single-byte-friendly, but some other CPUs are faster with 4-aligned reads. However, a char[] is smaller, which avoids cache trashing, and that is probably the most important aspect on most architectures. This array is accessed a lot by the noise functions. A vector-valued noise over 3D accesses it 96 times, and a float-valued 4D noise 64 times. We want this to fit in the cache!