TerrainGen coverage


terraingen/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
42 / 42
100.0%
Branches:
-
-%
Functions:
9 / 9
100.0%

noise/lattice.h
Line Branch Exec Source
1 /// @file
2 /// @brief Points split into a lattice cell and a fraction, and the frozen lattice hash keys.
3 #pragma once
4
5 #include "terraingen/noise/hash.h"
6
7 namespace terraingen {
8
9 /// A 2D point as its integer lattice cell and its fraction within the cell.
10 struct LatticePoint2 {
11 int32_t ix = 0; ///< Cell x.
12 int32_t iy = 0; ///< Cell y.
13 float fx = 0.0f; ///< Fraction in x, in [0, 1).
14 float fy = 0.0f; ///< Fraction in y, in [0, 1).
15 };
16
17 /// A 3D point as its integer lattice cell and its fraction within the cell.
18 struct LatticePoint3 {
19 int32_t ix = 0; ///< Cell x.
20 int32_t iy = 0; ///< Cell y.
21 int32_t iz = 0; ///< Cell z.
22 float fx = 0.0f; ///< Fraction in x, in [0, 1).
23 float fy = 0.0f; ///< Fraction in y, in [0, 1).
24 float fz = 0.0f; ///< Fraction in z, in [0, 1).
25 };
26
27 /// The lattice cell and fraction of (`x`, `y`) in lattice units, split at floor(x) and floor(y).
28 600547366 inline LatticePoint2 SplitLattice2(float x, float y) {
29 600547366 const float cellX = floor(x);
30 600547366 const float cellY = floor(y);
31 600547366 const LatticePoint2 point = {int32_t(cellX), int32_t(cellY), x - cellX, y - cellY};
32 600547366 return point;
33 }
34
35 /// The lattice cell and fraction of (`x`, `y`, `z`) in lattice units.
36 20716254 inline LatticePoint3 SplitLattice3(float x, float y, float z) {
37 20716254 const float cellX = floor(x);
38 20716254 const float cellY = floor(y);
39 20716254 const float cellZ = floor(z);
40 20716254 const LatticePoint3 point = {int32_t(cellX), int32_t(cellY), int32_t(cellZ),
41 20716254 x - cellX, y - cellY, z - cellZ};
42 20716254 return point;
43 }
44
45 #ifndef __SLANG__
46 /// `SplitLattice2` in double precision: the fraction is found before narrowing, so it
47 /// stays exact far from the origin.
48 97974634 inline LatticePoint2 SplitLatticePrecise2(double x, double y) {
49 97974634 const double cellX = floor(x);
50 97974634 const double cellY = floor(y);
51 97974634 return {int32_t(static_cast<std::int64_t>(cellX)), int32_t(static_cast<std::int64_t>(cellY)),
52 97974634 float(x - cellX), float(y - cellY)};
53 }
54
55 /// `SplitLattice3` in double precision, as `SplitLatticePrecise2`.
56 34508490 inline LatticePoint3 SplitLatticePrecise3(double x, double y, double z) {
57 34508490 const double cellX = floor(x);
58 34508490 const double cellY = floor(y);
59 34508490 const double cellZ = floor(z);
60 34508490 return {int32_t(static_cast<std::int64_t>(cellX)),
61 34508490 int32_t(static_cast<std::int64_t>(cellY)),
62 34508490 int32_t(static_cast<std::int64_t>(cellZ)),
63 34508490 float(x - cellX),
64 34508490 float(y - cellY),
65 34508490 float(z - cellZ)};
66 }
67 #endif
68
69 namespace detail {
70
71 /// The `pcg3d` key of 2D lattice point (`x`, `y`): the seed is the third word. Frozen for
72 /// reproducibility: Perlin, value and Worley noise in 2D and simplex noise hash this key.
73 /// @param x Lattice x, wrapped to 32 bits.
74 /// @param y Lattice y, wrapped to 32 bits.
75 2886904976 constexpr UVec3 SeededLatticeKey2(uint32_t x, uint32_t y, uint32_t seed) {
76 2886904976 const UVec3 key = {x, y, seed};
77 2886904976 return key;
78 }
79
80 /// The `pcg3d` key of a corner of 3D Perlin noise: the hashed seed is added to z. Frozen
81 /// for reproducibility.
82 /// @param x Lattice x, wrapped to 32 bits.
83 /// @param y Lattice y, wrapped to 32 bits.
84 /// @param z Lattice z, wrapped to 32 bits.
85 440142942 constexpr UVec3 PerlinLatticeKey3(uint32_t x, uint32_t y, uint32_t z, uint32_t seed) {
86 440142942 const UVec3 key = {x, y, z + PcgHash(seed)};
87 440142942 return key;
88 }
89
90 /// The `pcg3d` key of a corner of 3D value noise: `seedKey`, the hashed seed
91 /// (`PcgHash(seed)`), is XORed into z. Frozen for reproducibility.
92 /// @param x Lattice x, wrapped to 32 bits.
93 /// @param y Lattice y, wrapped to 32 bits.
94 /// @param z Lattice z, wrapped to 32 bits.
95 /// @param seedKey `PcgHash` of the noise seed.
96 80969 constexpr UVec3 ValueLatticeKey3(uint32_t x, uint32_t y, uint32_t z, uint32_t seedKey) {
97 80969 const UVec3 key = {x, y, z ^ seedKey};
98 80969 return key;
99 }
100
101 /// The `pcg3d` key of a cell of 3D Worley noise: `seedKey`, the hashed seed, is XORed into x so
102 /// the feature points do not follow the value noise lattice. Frozen for reproducibility.
103 /// @param seedKey `PcgHash` of the noise seed.
104 702103 constexpr UVec3 CellularLatticeKey3(uint32_t x, uint32_t y, uint32_t z, uint32_t seedKey) {
105 702103 const UVec3 key = {x ^ seedKey, y, z};
106 702103 return key;
107 }
108
109 /// The `pcg3d` key of a fractal octave's seed and lattice offset: (seed, octave, `word`).
110 /// Frozen for reproducibility.
111 /// @param seed The fractal seed.
112 /// @param octave The octave, from 0.
113 /// @param word 0 for the seed and x and y offsets, 1 for the z offset of 3D fractals.
114 642650711 constexpr UVec3 OctaveKey(uint32_t seed, uint32_t octave, uint32_t word) {
115 642650711 const UVec3 key = {seed, octave, word};
116 642650711 return key;
117 }
118
119 } // namespace detail
120
121 } // namespace terraingen
122