diff --git a/PerlinNoise.hpp b/PerlinNoise.hpp index af85725..9dc3106 100644 --- a/PerlinNoise.hpp +++ b/PerlinNoise.hpp @@ -3,7 +3,7 @@ // siv::PerlinNoise // Perlin noise library for modern C++ // -// Copyright (C) 2013-2020 Ryo Suzuki +// Copyright (C) 2013-2021 Ryo Suzuki // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files(the "Software"), to deal @@ -29,379 +29,631 @@ # include # include # include -# ifdef __cpp_concepts -# if __has_include() -# include -# endif -# endif # include # include # include # include +# if __has_include() && defined(__cpp_concepts) +# include +# endif + + +// Library major version +# define SIVPERLIN_VERSION_MAJOR 3 + +// Library minor version +# define SIVPERLIN_VERSION_MINOR 0 + +// Library revision version +# define SIVPERLIN_VERSION_REVISION 0 + +// Library version +# define SIVPERLIN_VERSION ((SIVPERLIN_VERSION_MAJOR * 100 * 100) + (SIVPERLIN_VERSION_MINOR * 100) + (SIVPERLIN_VERSION_REVISION)) + + +// [[nodiscard]] for constructors +# if (201907L <= __has_cpp_attribute(nodiscard)) +# define SIVPERLIN_NODISCARD_CXX20 [[nodiscard]] +# else +# define SIVPERLIN_NODISCARD_CXX20 +# endif + + +// std::uniform_random_bit_generator concept +# if __cpp_lib_concepts +# define SIVPERLIN_CONCEPT_URBG template +# define SIVPERLIN_CONCEPT_URBG_ template +# else +# define SIVPERLIN_CONCEPT_URBG template , std::is_unsigned>>>* = nullptr> +# define SIVPERLIN_CONCEPT_URBG_ template , std::is_unsigned>>>*> +# endif + + +// arbitrary value for increasing entropy +# ifndef SIVPERLIN_DEFAULT_Y +# define SIVPERLIN_DEFAULT_Y (0.12345) +# endif + +// arbitrary value for increasing entropy +# ifndef SIVPERLIN_DEFAULT_Z +# define SIVPERLIN_DEFAULT_Z (0.34567) +# endif + + namespace siv { -# ifdef __cpp_lib_concepts - template -# else template -# endif class BasicPerlinNoise { public: + static_assert(std::is_floating_point_v); + + /////////////////////////////////////// + // + // Typedefs + // + + using state_type = std::array; + using value_type = Float; - private: + using default_random_engine = std::mt19937; + + using seed_type = typename default_random_engine::result_type; + + /////////////////////////////////////// + // + // Constructors + // + + SIVPERLIN_NODISCARD_CXX20 + constexpr BasicPerlinNoise() noexcept; + + SIVPERLIN_NODISCARD_CXX20 + explicit BasicPerlinNoise(seed_type seed); - std::uint8_t p[512]; + SIVPERLIN_CONCEPT_URBG + SIVPERLIN_NODISCARD_CXX20 + explicit BasicPerlinNoise(URBG&& urbg); + + /////////////////////////////////////// + // + // Reseed + // + + void reseed(seed_type seed); + + SIVPERLIN_CONCEPT_URBG + void reseed(URBG&& urbg); + + /////////////////////////////////////// + // + // Serialization + // [[nodiscard]] - static constexpr value_type Fade(value_type t) noexcept - { - return t * t * t * (t * (t * 6 - 15) + 10); - } + constexpr const state_type& serialize() const noexcept; + + constexpr void deserialize(const state_type& state) noexcept; + + /////////////////////////////////////// + // + // Noise (The result is in the range [-1, 1]) + // [[nodiscard]] - static constexpr value_type Lerp(value_type t, value_type a, value_type b) noexcept - { - return a + t * (b - a); - } + value_type noise1D(value_type x) const noexcept; [[nodiscard]] - static constexpr value_type Grad(std::uint8_t hash, value_type x, value_type y, value_type z) noexcept - { - const std::uint8_t h = hash & 15; - const value_type u = h < 8 ? x : y; - const value_type v = h < 4 ? y : h == 12 || h == 14 ? x : z; - return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); - } + value_type noise2D(value_type x, value_type y) const noexcept; [[nodiscard]] - static constexpr value_type Weight(std::int32_t octaves) noexcept - { - value_type amp = 1; - value_type value = 0; + value_type noise3D(value_type x, value_type y, value_type z) const noexcept; - for (std::int32_t i = 0; i < octaves; ++i) - { - value += amp; - amp /= 2; - } + /////////////////////////////////////// + // + // Noise (The result is remapped to the range [0, 1]) + // - return value; - } + [[nodiscard]] + value_type noise1D_01(value_type x) const noexcept; - public: + [[nodiscard]] + value_type noise2D_01(value_type x, value_type y) const noexcept; - # if __has_cpp_attribute(nodiscard) >= 201907L [[nodiscard]] - # endif - explicit BasicPerlinNoise(std::uint32_t seed = std::default_random_engine::default_seed) - { - reseed(seed); - } + value_type noise3D_01(value_type x, value_type y, value_type z) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result can be out of the range [-1, 1]) + // - # ifdef __cpp_lib_concepts - template - # else - template >* = nullptr> - # endif - # if __has_cpp_attribute(nodiscard) >= 201907L [[nodiscard]] - # endif - explicit BasicPerlinNoise(URNG&& urng) - { - reseed(std::forward(urng)); - } + value_type octave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - void reseed(std::uint32_t seed) - { - for (size_t i = 0; i < 256; ++i) - { - p[i] = static_cast(i); - } + [[nodiscard]] + value_type octave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - std::shuffle(std::begin(p), std::begin(p) + 256, std::default_random_engine(seed)); + [[nodiscard]] + value_type octave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - for (size_t i = 0; i < 256; ++i) - { - p[256 + i] = p[i]; - } + /////////////////////////////////////// + // + // Octave noise (The result is clamped to the range [-1, 1]) + // + + [[nodiscard]] + value_type octave1D_11(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave2D_11(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave3D_11(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is clamped and remapped to the range [0, 1]) + // + + [[nodiscard]] + value_type octave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is normalized to the range [-1, 1]) + // + + [[nodiscard]] + value_type normalizedOctave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is normalized and remapped to the range [0, 1]) + // + + [[nodiscard]] + value_type normalizedOctave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + private: + + state_type m_permutation; + }; + + using PerlinNoise = BasicPerlinNoise; + + namespace perlin_detail + { + //////////////////////////////////////////////// + // + // These functions are provided for consistency. + // You may get different results from std::shuffle() with different standard library implementations. + // + SIVPERLIN_CONCEPT_URBG + [[nodiscard]] + inline std::uint64_t Random(const std::uint64_t max, URBG&& urbg) + { + return (urbg() % (max + 1)); } - # ifdef __cpp_lib_concepts - template - # else - template >* = nullptr> - # endif - void reseed(URNG&& urng) + template + inline void Shuffle(RandomIt first, RandomIt last, URBG&& urbg) { - for (size_t i = 0; i < 256; ++i) + if (first == last) { - p[i] = static_cast(i); + return; } - std::shuffle(std::begin(p), std::begin(p) + 256, std::forward(urng)); + using difference_type = typename std::iterator_traits::difference_type; - for (size_t i = 0; i < 256; ++i) + for (RandomIt it = first + 1; it < last; ++it) { - p[256 + i] = p[i]; + const std::uint64_t n = static_cast(it - first); + std::iter_swap(it, first + static_cast(Random(n, std::forward(urbg)))); } } - - /////////////////////////////////////// - // - // Noise [-1, 1] // + //////////////////////////////////////////////// + + template [[nodiscard]] - value_type noise1D(value_type x) const noexcept + inline constexpr Float Fade(const Float t) noexcept { - return noise3D(x, 0, 0); + return t * t * t * (t * (t * 6 - 15) + 10); } + template [[nodiscard]] - value_type noise2D(value_type x, value_type y) const noexcept + inline constexpr Float Lerp(const Float a, const Float b, const Float t) noexcept { - return noise3D(x, y, 0); + return (a + (b - a) * t); } + template [[nodiscard]] - value_type noise3D(value_type x, value_type y, value_type z) const noexcept + inline constexpr Float Grad(const std::uint8_t hash, const Float x, const Float y, const Float z) noexcept { - const std::int32_t X = static_cast(std::floor(x)) & 255; - const std::int32_t Y = static_cast(std::floor(y)) & 255; - const std::int32_t Z = static_cast(std::floor(z)) & 255; - - x -= std::floor(x); - y -= std::floor(y); - z -= std::floor(z); - - const value_type u = Fade(x); - const value_type v = Fade(y); - const value_type w = Fade(z); - - const std::int32_t A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z; - const std::int32_t B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z; - - return Lerp(w, Lerp(v, Lerp(u, Grad(p[AA], x, y, z), - Grad(p[BA], x - 1, y, z)), - Lerp(u, Grad(p[AB], x, y - 1, z), - Grad(p[BB], x - 1, y - 1, z))), - Lerp(v, Lerp(u, Grad(p[AA + 1], x, y, z - 1), - Grad(p[BA + 1], x - 1, y, z - 1)), - Lerp(u, Grad(p[AB + 1], x, y - 1, z - 1), - Grad(p[BB + 1], x - 1, y - 1, z - 1)))); + const std::uint8_t h = hash & 15; + const Float u = h < 8 ? x : y; + const Float v = h < 4 ? y : h == 12 || h == 14 ? x : z; + return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); } - /////////////////////////////////////// - // - // Noise [0, 1] - // + template [[nodiscard]] - value_type noise1D_0_1(value_type x) const noexcept + inline constexpr Float Remap_01(const Float x) noexcept { - return noise1D(x) - * value_type(0.5) + value_type(0.5); + return (x * Float(0.5) + Float(0.5)); } + template [[nodiscard]] - value_type noise2D_0_1(value_type x, value_type y) const noexcept + inline constexpr Float Clamp_11(const Float x) noexcept { - return noise2D(x, y) - * value_type(0.5) + value_type(0.5); + return std::clamp(x, Float(-1.0), Float(1.0)); } + template [[nodiscard]] - value_type noise3D_0_1(value_type x, value_type y, value_type z) const noexcept + inline constexpr Float RemapClamp_01(const Float x) noexcept { - return noise3D(x, y, z) - * value_type(0.5) + value_type(0.5); + if (x <= Float(-1.0)) + { + return Float(0.0); + } + else if (Float(1.0) <= x) + { + return Float(1.0); + } + + return (x * Float(0.5) + Float(0.5)); } - /////////////////////////////////////// - // - // Accumulated octave noise - // * Return value can be outside the range [-1, 1] - // + template [[nodiscard]] - value_type accumulatedOctaveNoise1D(value_type x, std::int32_t octaves) const noexcept + inline auto Octave1D(const Noise& noise, Float x, const std::int32_t octaves, const Float persistence) noexcept { + using value_type = Float; value_type result = 0; - value_type amp = 1; + value_type amplitude = 1; for (std::int32_t i = 0; i < octaves; ++i) { - result += noise1D(x) * amp; + result += (noise.noise1D(x) * amplitude); x *= 2; - amp /= 2; + amplitude *= persistence; } - return result; // unnormalized + return result; } + template [[nodiscard]] - value_type accumulatedOctaveNoise2D(value_type x, value_type y, std::int32_t octaves) const noexcept + inline auto Octave2D(const Noise& noise, Float x, Float y, const std::int32_t octaves, const Float persistence) noexcept { + using value_type = Float; value_type result = 0; - value_type amp = 1; + value_type amplitude = 1; for (std::int32_t i = 0; i < octaves; ++i) { - result += noise2D(x, y) * amp; + result += (noise.noise2D(x, y) * amplitude); x *= 2; y *= 2; - amp /= 2; + amplitude *= persistence; } - return result; // unnormalized + return result; } + template [[nodiscard]] - value_type accumulatedOctaveNoise3D(value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept + inline auto Octave3D(const Noise& noise, Float x, Float y, Float z, const std::int32_t octaves, const Float persistence) noexcept { + using value_type = Float; value_type result = 0; - value_type amp = 1; + value_type amplitude = 1; for (std::int32_t i = 0; i < octaves; ++i) { - result += noise3D(x, y, z) * amp; + result += (noise.noise3D(x, y, z) * amplitude); x *= 2; y *= 2; z *= 2; - amp /= 2; + amplitude *= persistence; } - return result; // unnormalized + return result; } - /////////////////////////////////////// - // - // Normalized octave noise [-1, 1] - // + template [[nodiscard]] - value_type normalizedOctaveNoise1D(value_type x, std::int32_t octaves) const noexcept + inline constexpr Float MaxAmplitude(const std::int32_t octaves, const Float persistence) noexcept { - return accumulatedOctaveNoise1D(x, octaves) - / Weight(octaves); - } + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; - [[nodiscard]] - value_type normalizedOctaveNoise2D(value_type x, value_type y, std::int32_t octaves) const noexcept - { - return accumulatedOctaveNoise2D(x, y, octaves) - / Weight(octaves); - } + for (std::int32_t i = 0; i < octaves; ++i) + { + result += amplitude; + amplitude *= persistence; + } - [[nodiscard]] - value_type normalizedOctaveNoise3D(value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept - { - return accumulatedOctaveNoise3D(x, y, z, octaves) - / Weight(octaves); + return result; } + } - /////////////////////////////////////// - // - // Accumulated octave noise clamped within the range [0, 1] - // - [[nodiscard]] - value_type accumulatedOctaveNoise1D_0_1(value_type x, std::int32_t octaves) const noexcept - { - return std::clamp(accumulatedOctaveNoise1D(x, octaves) - * value_type(0.5) + value_type(0.5), 0, 1); - } + /////////////////////////////////////// - [[nodiscard]] - value_type accumulatedOctaveNoise2D_0_1(value_type x, value_type y, std::int32_t octaves) const noexcept - { - return std::clamp(accumulatedOctaveNoise2D(x, y, octaves) - * value_type(0.5) + value_type(0.5), 0, 1); - } + template + inline constexpr BasicPerlinNoise::BasicPerlinNoise() noexcept + : m_permutation{ 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 } {} - [[nodiscard]] - value_type accumulatedOctaveNoise3D_0_1(value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept - { - return std::clamp(accumulatedOctaveNoise3D(x, y, z, octaves) - * value_type(0.5) + value_type(0.5), 0, 1); - } + template + inline BasicPerlinNoise::BasicPerlinNoise(const seed_type seed) + { + reseed(seed); + } - /////////////////////////////////////// - // - // Normalized octave noise [0, 1] - // - [[nodiscard]] - value_type normalizedOctaveNoise1D_0_1(value_type x, std::int32_t octaves) const noexcept - { - return normalizedOctaveNoise1D(x, octaves) - * value_type(0.5) + value_type(0.5); - } + template + SIVPERLIN_CONCEPT_URBG_ + inline BasicPerlinNoise::BasicPerlinNoise(URBG&& urbg) + { + reseed(std::forward(urbg)); + } - [[nodiscard]] - value_type normalizedOctaveNoise2D_0_1(value_type x, value_type y, std::int32_t octaves) const noexcept - { - return normalizedOctaveNoise2D(x, y, octaves) - * value_type(0.5) + value_type(0.5); - } + /////////////////////////////////////// - [[nodiscard]] - value_type normalizedOctaveNoise3D_0_1(value_type x, value_type y, value_type z, std::int32_t octaves) const noexcept - { - return normalizedOctaveNoise3D(x, y, z, octaves) - * value_type(0.5) + value_type(0.5); - } + template + inline void BasicPerlinNoise::reseed(const seed_type seed) + { + reseed(default_random_engine{ seed }); + } - /////////////////////////////////////// - // - // Serialization - // - void serialize(std::array& s) const noexcept - { - for (std::size_t i = 0; i < 256; ++i) - { - s[i] = p[i]; - } - } + template + SIVPERLIN_CONCEPT_URBG_ + inline void BasicPerlinNoise::reseed(URBG&& urbg) + { + std::iota(m_permutation.begin(), m_permutation.end(), uint8_t{ 0 }); - void deserialize(const std::array& s) noexcept - { - for (std::size_t i = 0; i < 256; ++i) - { - p[256 + i] = p[i] = s[i]; - } - } + perlin_detail::Shuffle(m_permutation.begin(), m_permutation.end(), std::forward(urbg)); + } - /////////////////////////////////////// - // - // Legacy interface - // - [[deprecated("use noise1D() instead")]] - double noise(double x) const; - [[deprecated("use noise2D() instead")]] - double noise(double x, double y) const; - [[deprecated("use noise3D() instead")]] - double noise(double x, double y, double z) const; - - [[deprecated("use noise1D_0_1() instead")]] - double noise0_1(double x) const; - [[deprecated("use noise2D_0_1() instead")]] - double noise0_1(double x, double y) const; - [[deprecated("use noise3D_0_1() instead")]] - double noise0_1(double x, double y, double z) const; - - [[deprecated("use accumulatedOctaveNoise1D() instead")]] - double octaveNoise(double x, std::int32_t octaves) const; - [[deprecated("use accumulatedOctaveNoise2D() instead")]] - double octaveNoise(double x, double y, std::int32_t octaves) const; - [[deprecated("use accumulatedOctaveNoise3D() instead")]] - double octaveNoise(double x, double y, double z, std::int32_t octaves) const; - - [[deprecated("use accumulatedOctaveNoise1D_0_1() instead")]] - double octaveNoise0_1(double x, std::int32_t octaves) const; - [[deprecated("use accumulatedOctaveNoise2D_0_1() instead")]] - double octaveNoise0_1(double x, double y, std::int32_t octaves) const; - [[deprecated("use accumulatedOctaveNoise3D_0_1() instead")]] - double octaveNoise0_1(double x, double y, double z, std::int32_t octaves) const; - }; + /////////////////////////////////////// - using PerlinNoise = BasicPerlinNoise; + template + inline constexpr const typename BasicPerlinNoise::state_type& BasicPerlinNoise::serialize() const noexcept + { + return m_permutation; + } + + template + inline constexpr void BasicPerlinNoise::deserialize(const state_type& state) noexcept + { + m_permutation = state; + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise1D(const value_type x) const noexcept + { + return noise3D(x, + static_cast(SIVPERLIN_DEFAULT_Y), + static_cast(SIVPERLIN_DEFAULT_Z)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise2D(const value_type x, const value_type y) const noexcept + { + return noise3D(x, + y, + static_cast(SIVPERLIN_DEFAULT_Z)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise3D(const value_type x, const value_type y, const value_type z) const noexcept + { + const value_type _x = std::floor(x); + const value_type _y = std::floor(y); + const value_type _z = std::floor(z); + + const std::int32_t ix = static_cast(_x) & 255; + const std::int32_t iy = static_cast(_y) & 255; + const std::int32_t iz = static_cast(_z) & 255; + + const value_type fx = (x - _x); + const value_type fy = (y - _y); + const value_type fz = (z - _z); + + const value_type u = perlin_detail::Fade(fx); + const value_type v = perlin_detail::Fade(fy); + const value_type w = perlin_detail::Fade(fz); + + const std::uint8_t A = (m_permutation[ix & 255] + iy) & 255; + const std::uint8_t B = (m_permutation[(ix + 1) & 255] + iy) & 255; + + const std::uint8_t AA = (m_permutation[A] + iz) & 255; + const std::uint8_t AB = (m_permutation[(A + 1) & 255] + iz) & 255; + + const std::uint8_t BA = (m_permutation[B] + iz) & 255; + const std::uint8_t BB = (m_permutation[(B + 1) & 255] + iz) & 255; + + const value_type p0 = perlin_detail::Grad(m_permutation[AA], fx, fy, fz); + const value_type p1 = perlin_detail::Grad(m_permutation[BA], fx - 1, fy, fz); + const value_type p2 = perlin_detail::Grad(m_permutation[AB], fx, fy - 1, fz); + const value_type p3 = perlin_detail::Grad(m_permutation[BB], fx - 1, fy - 1, fz); + const value_type p4 = perlin_detail::Grad(m_permutation[(AA + 1) & 255], fx, fy, fz - 1); + const value_type p5 = perlin_detail::Grad(m_permutation[(BA + 1) & 255], fx - 1, fy, fz - 1); + const value_type p6 = perlin_detail::Grad(m_permutation[(AB + 1) & 255], fx, fy - 1, fz - 1); + const value_type p7 = perlin_detail::Grad(m_permutation[(BB + 1) & 255], fx - 1, fy - 1, fz - 1); + + const value_type q0 = perlin_detail::Lerp(p0, p1, u); + const value_type q1 = perlin_detail::Lerp(p2, p3, u); + const value_type q2 = perlin_detail::Lerp(p4, p5, u); + const value_type q3 = perlin_detail::Lerp(p6, p7, u); + + const value_type r0 = perlin_detail::Lerp(q0, q1, v); + const value_type r1 = perlin_detail::Lerp(q2, q3, v); + + return perlin_detail::Lerp(r0, r1, w); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise1D_01(const value_type x) const noexcept + { + return perlin_detail::Remap_01(noise1D(x)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise2D_01(const value_type x, const value_type y) const noexcept + { + return perlin_detail::Remap_01(noise2D(x, y)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise3D_01(const value_type x, const value_type y, const value_type z) const noexcept + { + return perlin_detail::Remap_01(noise3D(x, y, z)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave1D(*this, x, octaves, persistence); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave2D(*this, x, y, octaves, persistence); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave3D(*this, x, y, z, octaves, persistence); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D_11(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D_11(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D_11(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave3D(x, y, z, octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D_01(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D_01(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D_01(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave3D(x, y, z, octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave1D(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave1D(x, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave2D(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave2D(x, y, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave3D(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave3D(x, y, z, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave1D_01(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave2D_01(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave3D_01(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave3D(x, y, z, octaves, persistence)); + } } + +# undef SIVPERLIN_NODISCARD_CXX20 +# undef SIVPERLIN_CONCEPT_URBG +# undef SIVPERLIN_CONCEPT_URBG_ diff --git a/README.md b/README.md index 11d03bf..e0022dc 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,120 @@ -# siv::PerlinNoise -**siv::PerlinNoise** is a header-only Perlin noise library for modern C++. -Based on Ken Perlin's [Improved Noise](). +# siv::PerlinNoise +![noise](images/top.png) + +**siv::PerlinNoise** is a header-only Perlin noise library for modern C++ (C++17/20). +The implementation is based on Ken Perlin's [Improved Noise](https://cs.nyu.edu/~perlin/noise/). ## Features -* random seed * 1D / 2D / 3D noise -* octave noise (accumulated / normalized) -* [0.0, 1.0] noise +* octave noise +* initial seed +* *(✨ new in v3.0)* produce the same output on any platform (except for floating point errors) ## License -siv::PerlinNoise is distributed under the MIT license. +siv::PerlinNoise is distributed under the **MIT license**. + +## Usage + +```cpp +# include +# include "PerlinNoise.hpp" + +int main() +{ + const siv::PerlinNoise::seed_type seed = 123456u; + + const siv::PerlinNoise perlin{ seed }; + + for (int y = 0; y < 5; ++y) + { + for (int x = 0; x < 5; ++x) + { + const double noise = perlin.octave2D_01((x * 0.01), (y * 0.01), 4); + + std::cout << noise << '\t'; + } + + std::cout << '\n'; + } +} +``` + +## API + +### `template class BasicPerlinNoise` + +- Typedefs + - `using PerlinNoise = BasicPerlinNoise;` + - `using state_type = std::array;` + - `using value_type = Float;` + - `using default_random_engine = std::mt19937;` + - `using seed_type = typename default_random_engine::result_type;` +- Constructors + - `constexpr BasicPerlinNoise();` + - `BasicPerlinNoise(seed_type seed);` + - `BasicPerlinNoise(URBG&& urbg);` +- Reseed + - `void reseed(seed_type seed);` + - `void reseed(URBG&& urbg);` +- Serialization + - `constexpr const state_type& serialize() const noexcept;` + - `constexpr void deserialize(const state_type& state) noexcept;` +- Noise (The result is **in the range [-1, 1]**) + - `value_type noise1D(value_type x) const noexcept;` + - `value_type noise2D(value_type x, value_type y) const noexcept;` + - `value_type noise3D(value_type x, value_type y, value_type z) const noexcept;` +- Noise (The result is **remapped to the range [0, 1]**) + - `value_type noise1D_01(value_type x) const noexcept;` + - `value_type noise2D_01(value_type x, value_type y) const noexcept;` + - `value_type noise3D_01(value_type x, value_type y, value_type z) const noexcept;` +- Octave noise (The result **can be out of the range [-1, 1]**) + - `value_type octave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type octave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type octave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` +- Octave noise (The result is **clamped to the range [-1, 1]**) + - `value_type octave1D_11(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type octave2D_11(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type octave3D_11(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` +- Octave noise (The result is **clamped and remapped to the range [0, 1]**) + - `value_type octave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type octave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type octave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` +- Octave noise (The result is **normalized to the range [-1, 1]**) + - `value_type normalizedOctave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type normalizedOctave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type normalizedOctave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` +- Octave noise (The result is **normalized and remapped to the range [0, 1]**) + - `value_type normalizedOctave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type normalizedOctave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` + - `value_type normalizedOctave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;` ## Example Run example.cpp with the following parameters. + ``` frequency = 8.0 octaves = 8 seed = 12345 ``` -![noise](f8o8_12345.bmp) +![noise](images/f8o8_12345.png) + +--- + +``` +frequency = 8.0 +octaves = 8 +seed = 23456 +``` + +![noise](images/f8o8_23456.png) + +--- + +``` +frequency = 8.0 +octaves = 3 +seed = 23456 +``` + +![noise](images/f8o3_23456.png) diff --git a/example.cpp b/example.cpp index 11725c5..d8f3bbf 100644 --- a/example.cpp +++ b/example.cpp @@ -1,4 +1,3 @@ - # include # include # include @@ -25,7 +24,6 @@ struct BMPHeader std::uint32_t biClrUsed; std::uint32_t biClrImportant; }; - static_assert(sizeof(BMPHeader) == 54); # pragma pack (pop) @@ -36,41 +34,25 @@ struct RGB double b = 0.0; constexpr RGB() = default; explicit constexpr RGB(double _rgb) noexcept - : r(_rgb), g(_rgb), b(_rgb) {} + : r{ _rgb }, g{ _rgb }, b{ _rgb } {} constexpr RGB(double _r, double _g, double _b) noexcept - : r(_r), g(_g), b(_b) {} + : r{ _r }, g{ _g }, b{ _b } {} }; class Image { -private: - - std::vector m_data; - - std::int32_t m_width = 0, m_height = 0; - - bool inBounds(std::int32_t y, std::int32_t x) const noexcept - { - return (0 <= y) && (y < m_height) && (0 <= x) && (x < m_width); - } - - static constexpr std::uint8_t ToUint8(double x) noexcept - { - return x >= 1.0 ? 255 : x <= 0.0 ? 0 : static_cast(x * 255.0 + 0.5); - } - public: Image() = default; Image(std::size_t width, std::size_t height) - : m_data(width * height) - , m_width(static_cast(width)) - , m_height(static_cast(height)) {} + : m_data(width* height) + , m_width{ static_cast(width) } + , m_height{ static_cast(height) } {} void set(std::int32_t x, std::int32_t y, const RGB& color) { - if (!inBounds(y, x)) + if (not inBounds(y, x)) { return; } @@ -78,15 +60,9 @@ class Image m_data[static_cast(y) * m_width + x] = color; } - std::int32_t width() const - { - return m_width; - } + std::int32_t width() const noexcept { return m_width; } - std::int32_t height() const - { - return m_height; - } + std::int32_t height() const noexcept { return m_height; } bool saveBMP(const std::string& path) { @@ -96,20 +72,9 @@ class Image { 0x4d42, static_cast(bmpsize + sizeof(BMPHeader)), - 0, - 0, - sizeof(BMPHeader), - 40, - m_width, - m_height, - 1, - 24, - 0, - bmpsize, - 0, - 0, - 0, - 0 + 0, 0, sizeof(BMPHeader), 40, + m_width, m_height, 1, 24, + 0, bmpsize, 0, 0, 0, 0 }; if (std::ofstream ofs{ path, std::ios_base::binary }) @@ -140,38 +105,62 @@ class Image return false; } } + +private: + + std::vector m_data; + + std::int32_t m_width = 0, m_height = 0; + + bool inBounds(std::int32_t y, std::int32_t x) const noexcept + { + return (0 <= y) && (y < m_height) && (0 <= x) && (x < m_width); + } + + static constexpr std::uint8_t ToUint8(double x) noexcept + { + return (x <= 0.0) ? 0 : (1.0 <= x) ? 255 : static_cast(x * 255.0 + 0.5); + } }; void Test() { - siv::PerlinNoise perlinA(std::random_device{}); + siv::PerlinNoise perlinA{ std::random_device{} }; siv::PerlinNoise perlinB; - std::array state; - perlinA.serialize(state); - perlinB.deserialize(state); + perlinB.deserialize(perlinA.serialize()); + + assert(perlinA.octave3D(0.1, 0.2, 0.3, 4) + == perlinB.octave3D(0.1, 0.2, 0.3, 4)); - assert(perlinA.accumulatedOctaveNoise3D(0.1, 0.2, 0.3, 4) - == perlinB.accumulatedOctaveNoise3D(0.1, 0.2, 0.3, 4)); + perlinA.reseed(12345u); + perlinB.reseed(12345u); - perlinA.reseed(1234); - perlinB.reseed(1234); + assert(perlinA.octave3D(0.1, 0.2, 0.3, 4) + == perlinB.octave3D(0.1, 0.2, 0.3, 4)); - assert(perlinA.accumulatedOctaveNoise3D(0.1, 0.2, 0.3, 4) - == perlinB.accumulatedOctaveNoise3D(0.1, 0.2, 0.3, 4)); + perlinA.reseed(std::mt19937{ 67890u }); + perlinB.reseed(std::mt19937{ 67890u }); - perlinA.reseed(std::mt19937{ 1234 }); - perlinB.reseed(std::mt19937{ 1234 }); + assert(perlinA.octave3D(0.1, 0.2, 0.3, 4) + == perlinB.octave3D(0.1, 0.2, 0.3, 4)); - assert(perlinA.accumulatedOctaveNoise3D(0.1, 0.2, 0.3, 4) - == perlinB.accumulatedOctaveNoise3D(0.1, 0.2, 0.3, 4)); + for (std::int32_t y = 0; y < 20; ++y) + { + for (std::int32_t x = 0; x < 20; ++x) + { + const double noise = perlinA.octave2D_01(x * 0.1, y * 0.1, 6); + std::cout << static_cast(std::floor(noise * 10) - 0.5); + } + std::cout << '\n'; + } } int main() { Test(); - Image image(512, 512); + Image image{ 512, 512 }; std::cout << "---------------------------------\n"; std::cout << "* frequency [0.1 .. 8.0 .. 64.0] \n"; @@ -195,15 +184,15 @@ int main() std::cout << "uint32 seed = "; std::cin >> seed; - const siv::PerlinNoise perlin(seed); - const double fx = image.width() / frequency; - const double fy = image.height() / frequency; + const siv::PerlinNoise perlin{ seed }; + const double fx = (frequency / image.width()); + const double fy = (frequency / image.height()); for (std::int32_t y = 0; y < image.height(); ++y) { for (std::int32_t x = 0; x < image.width(); ++x) { - const RGB color(perlin.accumulatedOctaveNoise2D_0_1(x / fx, y / fy, octaves)); + const RGB color(perlin.octave2D_01((x * fx), (y * fy), octaves)); image.set(x, y, color); } } diff --git a/f8o8_12345.bmp b/f8o8_12345.bmp deleted file mode 100644 index e11c711..0000000 Binary files a/f8o8_12345.bmp and /dev/null differ diff --git a/images/f8o3_23456.png b/images/f8o3_23456.png new file mode 100644 index 0000000..a8a0373 Binary files /dev/null and b/images/f8o3_23456.png differ diff --git a/images/f8o8_12345.png b/images/f8o8_12345.png new file mode 100644 index 0000000..a569cb8 Binary files /dev/null and b/images/f8o8_12345.png differ diff --git a/images/f8o8_23456.png b/images/f8o8_23456.png new file mode 100644 index 0000000..136a911 Binary files /dev/null and b/images/f8o8_23456.png differ diff --git a/images/top.png b/images/top.png new file mode 100644 index 0000000..9d3531b Binary files /dev/null and b/images/top.png differ