src/layers/lattice.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Jittered-lattice cell loops (`ForEachLatticeCell`) and their random streams. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/cells.h" | ||
| 8 | #include "terraingen/geometry/geometry.h" | ||
| 9 | #include "terraingen/geometry/surface.h" | ||
| 10 | #include "terraingen/random/stream.h" | ||
| 11 | |||
| 12 | #include <cmath> | ||
| 13 | #include <cstdint> | ||
| 14 | #include <memory> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | /// The cells features are generated in on `surface` (`MakeCells`), made once per thread for the | ||
| 19 | /// surface it last asked for, so layers reading the surface from their context at each point | ||
| 20 | /// keep their cells and the areas they recall. | ||
| 21 | 150286 | inline std::shared_ptr<const SurfaceCells> ThreadCells(const Surface& surface) { | |
| 22 | 150286 | thread_local Surface last{}; | |
| 23 | 150286 | thread_local std::shared_ptr<const SurfaceCells> cells = MakeCells(last); | |
| 24 | 150286 | if (!(surface == last)) { | |
| 25 | 24008 | cells = MakeCells(surface); | |
| 26 | 12004 | last = surface; | |
| 27 | } | ||
| 28 | 150286 | return cells; | |
| 29 | } | ||
| 30 | |||
| 31 | /// The random stream of lattice cell (`i`, `j`) under `seed` and `stream`, keyed by all 64 bits of | ||
| 32 | /// each index. | ||
| 33 | 13708701 | inline RandomStream LatticeStream(std::uint32_t seed, std::uint32_t stream, std::int64_t i, | |
| 34 | std::int64_t j) { | ||
| 35 | 13708701 | return RandomStream(seed, stream, static_cast<std::uint32_t>(i), static_cast<std::uint32_t>(j), | |
| 36 | 13708701 | static_cast<std::uint32_t>((i >> 32) ^ ((j >> 32) << 16))); | |
| 37 | } | ||
| 38 | |||
| 39 | /// Calls `visit(i, j)` for each cell within `rings` cells of the one holding `p`, on a lattice of | ||
| 40 | /// cells `cell.x` by `cell.y` across, row `j` by row with `i` ascending in each. | ||
| 41 | template <typename Visit> | ||
| 42 | 710674 | void ForEachLatticeCell(const Vec2d& p, const Vec2d& cell, std::int64_t rings, const Visit& visit) { | |
| 43 | 710674 | const auto ci = static_cast<std::int64_t>(std::floor(p.x / cell.x)); | |
| 44 | 710674 | const auto cj = static_cast<std::int64_t>(std::floor(p.y / cell.y)); | |
| 45 | 3020159 | for (std::int64_t j = cj - rings; j <= cj + rings; ++j) { | |
| 46 | 10171242 | for (std::int64_t i = ci - rings; i <= ci + rings; ++i) { | |
| 47 | 7861757 | visit(i, j); | |
| 48 | } | ||
| 49 | } | ||
| 50 | 710707 | } | |
| 51 | |||
| 52 | } // namespace terraingen | ||
| 53 |