src/layers/roughness.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/roughness.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/layer_fields.h" | ||
| 6 | #include "terraingen/noise/hash.h" | ||
| 7 | #include "terraingen/noise/perlin.h" | ||
| 8 | #include "terraingen/noise/structure_function.h" | ||
| 9 | #include "terraingen/random/stream.h" | ||
| 10 | #include "terraingen/random/streams.h" | ||
| 11 | #include "terraingen/raster/cell_loops.h" | ||
| 12 | #include "terraingen/raster/grid.h" | ||
| 13 | #include "terraingen/util/bits.h" | ||
| 14 | #include "terraingen/util/log.h" | ||
| 15 | |||
| 16 | #include <algorithm> | ||
| 17 | #include <array> | ||
| 18 | #include <cmath> | ||
| 19 | #include <memory> | ||
| 20 | #include <stdexcept> | ||
| 21 | |||
| 22 | namespace terraingen { | ||
| 23 | |||
| 24 | namespace { | ||
| 25 | |||
| 26 | constexpr int kMaxOctaves = 40; | ||
| 27 | |||
| 28 | constexpr std::uint32_t kOffsetZSalt = 7u; | ||
| 29 | |||
| 30 | /// The golden angle, in radians, by which each octave is turned from the last. | ||
| 31 | constexpr double kGoldenAngle = 2.39996322972865332; | ||
| 32 | |||
| 33 | /// A uniform in [0, 1024) from the top 16 bits of a hash, for lattice offsets. | ||
| 34 | 1430 | double LatticeOffset(std::uint32_t hash) { | |
| 35 | 1430 | return static_cast<double>(hash >> 16) / 65536.0 * 1024.0; | |
| 36 | } | ||
| 37 | |||
| 38 | /// The rotation matrix, row-major, of a uniform random rotation from three uniform hashes, by | ||
| 39 | /// Shoemake's (1992) unit quaternion. | ||
| 40 | 286 | std::array<double, 9> UniformRotation(const UVec3& turn) { | |
| 41 | 286 | const double u1 = static_cast<double>(HashToUnitFloat(turn.x)); | |
| 42 | 286 | const double u2 = 2.0 * kPi * static_cast<double>(HashToUnitFloat(turn.y)); | |
| 43 | 286 | const double u3 = 2.0 * kPi * static_cast<double>(HashToUnitFloat(turn.z)); | |
| 44 | 286 | const double a = std::sqrt(1.0 - u1); | |
| 45 | 286 | const double b = std::sqrt(u1); | |
| 46 | 286 | const double qx = a * std::sin(u2); | |
| 47 | 286 | const double qy = a * std::cos(u2); | |
| 48 | 286 | const double qz = b * std::sin(u3); | |
| 49 | 286 | const double qw = b * std::cos(u3); | |
| 50 | 286 | return {1.0 - 2.0 * (qy * qy + qz * qz), 2.0 * (qx * qy - qz * qw), | |
| 51 | 286 | 2.0 * (qx * qz + qy * qw), 2.0 * (qx * qy + qz * qw), | |
| 52 | 286 | 1.0 - 2.0 * (qx * qx + qz * qz), 2.0 * (qy * qz - qx * qw), | |
| 53 | 286 | 2.0 * (qx * qz - qy * qw), 2.0 * (qy * qz + qx * qw), | |
| 54 | 286 | 1.0 - 2.0 * (qx * qx + qy * qy)}; | |
| 55 | } | ||
| 56 | |||
| 57 | } // namespace | ||
| 58 | |||
| 59 | 52 | void ValidateRoughnessOptions(const RoughnessOptions& options) { | |
| 60 | 52 | CheckRanges(options); | |
| 61 | 40 | if (!(options.minWavelength <= options.maxWavelength)) { | |
| 62 | 2 | throw std::invalid_argument("Roughness wavelengths must satisfy minWavelength <= " | |
| 63 | 2 | "maxWavelength"); | |
| 64 | } | ||
| 65 | 38 | if (std::log2(options.maxWavelength / options.minWavelength) >= kMaxOctaves) { | |
| 66 | 4 | throw std::invalid_argument("Roughness wavelengths may span at most " + | |
| 67 | 8 | std::to_string(kMaxOctaves) + " octaves"); | |
| 68 | } | ||
| 69 | 36 | ValidateResolutionFade(options.fade); | |
| 70 | 34 | } | |
| 71 | |||
| 72 | 42 | RoughnessLayer::RoughnessLayer(RoughnessOptions options) : m_options(options) { | |
| 73 | 42 | ValidateRoughnessOptions(m_options); | |
| 74 | 33 | m_planeOctaves = MakeOctaves(false); | |
| 75 | 33 | m_bodyOctaves = MakeOctaves(true); | |
| 76 | 51 | } | |
| 77 | |||
| 78 | 4 | LayerPtr RoughnessLayer::Reseeded(std::uint64_t seed) const { | |
| 79 | 4 | RoughnessOptions options = m_options; | |
| 80 | 4 | options.seed = ReseedWord(seed, 0); | |
| 81 | 4 | return std::make_shared<RoughnessLayer>(options); | |
| 82 | } | ||
| 83 | |||
| 84 | 66 | std::vector<RoughnessLayer::Octave> RoughnessLayer::MakeOctaves(bool body) const { | |
| 85 | 66 | std::vector<Octave> octaves; | |
| 86 | 66 | const double shortest = m_options.minWavelength * (1.0 - 1e-12); | |
| 87 | 66 | double wavelength = m_options.maxWavelength; | |
| 88 | 638 | for (std::uint32_t i = 0; wavelength >= shortest; ++i, wavelength *= 0.5) { | |
| 89 | 572 | octaves.push_back(MakeOctave(i, wavelength, body)); | |
| 90 | } | ||
| 91 | 66 | const double scale = m_options.rmsSlope / OctavesRmsSlope(octaves, m_options.baseline, body); | |
| 92 | 638 | for (Octave& octave : octaves) { | |
| 93 | 572 | octave.amplitude *= scale; | |
| 94 | } | ||
| 95 | 66 | return octaves; | |
| 96 | } | ||
| 97 | |||
| 98 | 572 | RoughnessLayer::Octave RoughnessLayer::MakeOctave(std::uint32_t i, double wavelength, | |
| 99 | bool body) const { | ||
| 100 | 572 | const UVec3 hash = Pcg3dHash({m_options.seed, i, kRoughnessOctaveStream}); | |
| 101 | 572 | const double angle = kGoldenAngle * static_cast<double>(i); | |
| 102 | 572 | Octave octave{wavelength, | |
| 103 | 572 | std::pow(wavelength, m_options.hurst), | |
| 104 | 572 | std::cos(angle), | |
| 105 | 572 | std::sin(angle), | |
| 106 | 572 | LatticeOffset(hash.x), | |
| 107 | 572 | LatticeOffset(hash.y), | |
| 108 | 572 | hash.z}; | |
| 109 | 572 | if (body) { | |
| 110 | 286 | octave.rotation = UniformRotation(Pcg3dHash({m_options.seed, i, kRoughnessRotationStream})); | |
| 111 | 286 | octave.offsetZ = LatticeOffset(Pcg3dHash({hash.z, i, kOffsetZSalt}).x); | |
| 112 | } | ||
| 113 | 572 | return octave; | |
| 114 | } | ||
| 115 | |||
| 116 | 21039080 | double RoughnessLayer::OctaveNoise(const Octave& octave, const Surface& surface, double x, | |
| 117 | double y) { | ||
| 118 | 21039080 | if (surface.IsBody()) { | |
| 119 | 94229 | const Vec3d p = surface.Position({x, y}); | |
| 120 | 94231 | const std::array<double, 9>& m = octave.rotation; | |
| 121 | 94231 | const double scale = 1.0 / octave.wavelength; | |
| 122 | 94231 | return static_cast<double>(PerlinNoise3Precise( | |
| 123 | 94231 | scale * (m[0] * p.x + m[1] * p.y + m[2] * p.z) + octave.offsetX, | |
| 124 | 94231 | scale * (m[3] * p.x + m[4] * p.y + m[5] * p.z) + octave.offsetY, | |
| 125 | 94231 | scale * (m[6] * p.x + m[7] * p.y + m[8] * p.z) + octave.offsetZ, octave.seed)); | |
| 126 | } | ||
| 127 | 20944851 | const double u = (octave.cos * x + octave.sin * y) / octave.wavelength + octave.offsetX; | |
| 128 | 20944851 | const double v = (octave.cos * y - octave.sin * x) / octave.wavelength + octave.offsetY; | |
| 129 | 20944851 | return static_cast<double>(PerlinNoise2Precise(u, v, octave.seed)); | |
| 130 | } | ||
| 131 | |||
| 132 | 107 | double RoughnessLayer::OctavesRmsSlope(const std::vector<Octave>& octaves, double baseline, | |
| 133 | bool body) { | ||
| 134 | 107 | double structure = 0.0; | |
| 135 | 1006 | for (const Octave& octave : octaves) { | |
| 136 | 899 | const double lag = baseline / octave.wavelength; | |
| 137 | 899 | structure += octave.amplitude * octave.amplitude * | |
| 138 | 899 | (body ? Perlin3StructureFunction(lag) : PerlinStructureFunction(lag)); | |
| 139 | } | ||
| 140 | 107 | return std::sqrt(structure) / baseline; | |
| 141 | } | ||
| 142 | |||
| 143 | 41 | double RoughnessLayer::RmsSlope(double baseline, const Surface& surface) const { | |
| 144 | 41 | const bool body = surface.IsBody(); | |
| 145 | 41 | return OctavesRmsSlope(body ? m_bodyOctaves : m_planeOctaves, baseline, body); | |
| 146 | } | ||
| 147 | |||
| 148 | 4 | std::vector<double> RoughnessLayer::Wavelengths() const { | |
| 149 | 4 | std::vector<double> wavelengths; | |
| 150 | 32 | for (const Octave& octave : m_planeOctaves) { | |
| 151 | 28 | wavelengths.push_back(octave.wavelength); | |
| 152 | } | ||
| 153 | 4 | return wavelengths; | |
| 154 | } | ||
| 155 | |||
| 156 | 4335235 | float RoughnessLayer::HeightAt(float height, double x, double y, | |
| 157 | const LayerContext& context) const { | ||
| 158 | 4335235 | const Surface& surface = context.SurfaceOrPlane(); | |
| 159 | 4336514 | double sum = 0.0; | |
| 160 | 25357241 | for (const Octave& octave : surface.IsBody() ? m_bodyOctaves : m_planeOctaves) { | |
| 161 | 21110515 | const double weight = ResolutionWeight(octave.wavelength, context.spacing, m_options.fade); | |
| 162 | 21102722 | if (weight <= 0.0) { | |
| 163 | break; | ||
| 164 | } | ||
| 165 | 21035662 | sum += weight * octave.amplitude * OctaveNoise(octave, surface, x, y); | |
| 166 | } | ||
| 167 | 4313786 | return height + static_cast<float>(sum); | |
| 168 | } | ||
| 169 | |||
| 170 | } // namespace terraingen | ||
| 171 |