./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
60 / 60
100.0%
Branches:
52 / 52
100.0%
Functions:
4 / 4, 1 excl
100.0%

include/terraingen/noise/worley.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Worley cellular noise in 2D and 3D: feature-point distances and Voronoi cell values.
5 #pragma once
6
7 #include "terraingen/noise/hash.h"
8 #include "terraingen/noise/lattice.h"
9
10 namespace terraingen {
11
12 /// Distances from a point to its nearest and second-nearest feature points, and the random value
13 /// of the nearest point's Voronoi cell.
14 struct WorleyDistances {
15 float f1 = 0.0f; ///< Distance to the nearest feature point (F1), in lattice units.
16 float f2 = 0.0f; ///< Distance to the second-nearest feature point (F2), in lattice units.
17 float value = 0.0f; ///< Value of the nearest point's cell, uniform in [-1, 1).
18 };
19
20 namespace detail {
21
22 /// The squared gap along one axis between a fraction `f` of the center cell and the cell `d`
23 /// cells away.
24 5773572 inline float CellGapSquared(int32_t d, float f) {
25 5773572 float gap = 0.0f;
26 5773572 if (d > 0) {
27 2415474 gap = float(d) - f;
28
6/6
✓ Branch 0 taken 805158 times.
✓ Branch 1 taken 314208 times.
✓ Branch 2 taken 805158 times.
✓ Branch 3 taken 314208 times.
✓ Branch 4 taken 805158 times.
✓ Branch 5 taken 314208 times.
3358098 } else if (d < 0) {
29 2415474 gap = f - float(d + 1);
30 }
31 5773572 return gap * gap;
32 }
33
34 } // namespace detail
35
36 /// Worley cellular distances at a lattice cell plus fraction, searching the 5 x 5 cells around it;
37 /// F2 is exact but for rare configurations.
38 /// @param fx,fy Fractions in [0, 1).
39 /// @return F1 in [0, sqrt(2)] and F2 >= F1.
40 2093435 inline WorleyDistances Worley2(int32_t ix, int32_t iy, float fx, float fy, uint32_t seed) {
41 2093435 float d1 = 1e30f;
42 2093435 float d2 = 1e30f;
43 2093435 uint32_t nearest = 0u;
44
2/2
✓ Branch 0 taken 10467175 times.
✓ Branch 1 taken 2093435 times.
12560610 for (int32_t dy = -2; dy <= 2; ++dy) {
45
2/2
✓ Branch 0 taken 52335875 times.
✓ Branch 1 taken 10467175 times.
62803050 for (int32_t dx = -2; dx <= 2; ++dx) {
46 52335875 const UVec3 h =
47
2/2
✓ Branch 0 taken 13231993 times.
✓ Branch 1 taken 39103882 times.
52335875 Pcg3dHash(detail::SeededLatticeKey2(uint32_t(ix + dx), uint32_t(iy + dy), seed));
48
2/2
✓ Branch 0 taken 13231993 times.
✓ Branch 1 taken 39103882 times.
52335875 const float px = float(dx) + HashToUnitFloat(h.y) - fx;
49 52335875 const float py = float(dy) + HashToUnitFloat(h.z) - fy;
50 52335875 const float d = px * px + py * py;
51
2/2
✓ Branch 0 taken 13231993 times.
✓ Branch 1 taken 39103882 times.
52335875 if (d < d1) {
52 13231993 d2 = d1;
53 13231993 d1 = d;
54 13231993 nearest = h.x;
55
2/2
✓ Branch 0 taken 5476962 times.
✓ Branch 1 taken 33626920 times.
39103882 } else if (d < d2) {
56 5476962 d2 = d;
57 }
58 }
59 }
60 2093435 const WorleyDistances distances = {sqrt(d1), sqrt(d2), 2.0f * HashToUnitFloat(nearest) - 1.0f};
61 return distances;
62 }
63
64 /// Worley cellular distances at a point in lattice units.
65 /// @return F1 in [0, sqrt(2)] and F2 >= F1.
66 2093435 inline WorleyDistances Worley2(float x, float y, uint32_t seed) {
67 2093435 const LatticePoint2 point = SplitLattice2(x, y);
68 2093435 return Worley2(point.ix, point.iy, point.fx, point.fy, seed);
69 }
70
71 /// Worley cellular distances in 3D, one feature point per cell: the 3 x 3 x 3 cells around the
72 /// point, then the outer shell of the 5 x 5 x 5 cells where a cell could still be nearer than F2.
73 /// F1 and the cell value are exact, F2 exact but for rare configurations.
74 /// @return F1 in [0, sqrt(3)] and F2 >= F1.
75 19638 inline WorleyDistances Worley3(LatticePoint3 point, uint32_t seed) {
76 19638 const uint32_t seedKey = PcgHash(seed);
77 19638 float d1 = 1e30f;
78 19638 float d2 = 1e30f;
79 19638 uint32_t nearest = 0u;
80
2/2
✓ Branch 0 taken 39276 times.
✓ Branch 1 taken 19638 times.
58914 for (int32_t shell = 0; shell < 2; ++shell) {
81
2/2
✓ Branch 0 taken 196380 times.
✓ Branch 1 taken 39276 times.
235656 for (int32_t dz = -2; dz <= 2; ++dz) {
82
2/2
✓ Branch 0 taken 981900 times.
✓ Branch 1 taken 196380 times.
1178280 for (int32_t dy = -2; dy <= 2; ++dy) {
83
2/2
✓ Branch 0 taken 4909500 times.
✓ Branch 1 taken 981900 times.
5891400 for (int32_t dx = -2; dx <= 2; ++dx) {
84
6/6
✓ Branch 0 taken 2945700 times.
✓ Branch 1 taken 1963800 times.
✓ Branch 2 taken 1767420 times.
✓ Branch 3 taken 1178280 times.
✓ Branch 4 taken 706968 times.
✓ Branch 5 taken 1060452 times.
4909500 const bool inner = abs(dx) <= 1 && abs(dy) <= 1 && abs(dz) <= 1;
85
2/2
✓ Branch 0 taken 2454750 times.
✓ Branch 1 taken 2454750 times.
4909500 if (inner != (shell == 0)) {
86 4379086 continue;
87 }
88
4/4
✓ Branch 0 taken 1924524 times.
✓ Branch 1 taken 530226 times.
✓ Branch 2 taken 805158 times.
✓ Branch 3 taken 1119366 times.
2454750 if (shell == 1 && detail::CellGapSquared(dx, point.fx) +
89
2/2
✓ Branch 0 taken 805158 times.
✓ Branch 1 taken 1119366 times.
1924524 detail::CellGapSquared(dy, point.fy) +
90
4/4
✓ Branch 0 taken 805158 times.
✓ Branch 1 taken 1119366 times.
✓ Branch 2 taken 1924336 times.
✓ Branch 3 taken 188 times.
3849048 detail::CellGapSquared(dz, point.fz) >=
91 d2) {
92 1924336 continue;
93 }
94 530414 const UVec3 h = Pcg3dHash(detail::CellularLatticeKey3(
95
2/2
✓ Branch 0 taken 95123 times.
✓ Branch 1 taken 435291 times.
530414 uint32_t(point.ix + dx), uint32_t(point.iy + dy), uint32_t(point.iz + dz),
96 seedKey));
97
2/2
✓ Branch 0 taken 95123 times.
✓ Branch 1 taken 435291 times.
530414 const float px = float(dx) + HashToUnitFloat(h.x) - point.fx;
98 530414 const float py = float(dy) + HashToUnitFloat(h.y) - point.fy;
99 530414 const float pz = float(dz) + HashToUnitFloat(h.z) - point.fz;
100 530414 const float d = px * px + py * py + pz * pz;
101
2/2
✓ Branch 0 taken 95123 times.
✓ Branch 1 taken 435291 times.
530414 if (d < d1) {
102 95123 d2 = d1;
103 95123 d1 = d;
104 95123 nearest = PcgHash(h.x ^ h.y ^ h.z);
105
2/2
✓ Branch 0 taken 56368 times.
✓ Branch 1 taken 378923 times.
435291 } else if (d < d2) {
106 56368 d2 = d;
107 }
108 }
109 }
110 }
111 }
112 19638 const WorleyDistances distances = {sqrt(d1), sqrt(d2), 2.0f * HashToUnitFloat(nearest) - 1.0f};
113 19638 return distances;
114 }
115
116 /// Worley cellular distances in 3D at a point in lattice units.
117 /// @return F1 in [0, sqrt(3)] and F2 >= F1.
118 7400 inline WorleyDistances Worley3(float x, float y, float z, uint32_t seed) {
119 7400 return Worley3(SplitLattice3(x, y, z), seed);
120 }
121
122 } // namespace terraingen
123