./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
93 / 93
100.0%
Branches:
73 / 74
98.6%
Functions:
4 / 4
100.0%

include/terraingen/noise/simplex.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Simplex noise in 2D and 3D.
5 #pragma once
6
7 #include "terraingen/noise/perlin.h"
8
9 namespace terraingen {
10
11 namespace detail {
12
13 /// The contribution (0.5 - r^2)^4 dot(g, d) of one simplex corner; 0 for r^2 >= 0.5.
14 /// @param ci Corner x on the skewed lattice, wrapped to 32 bits.
15 /// @param cj Corner y on the skewed lattice, wrapped to 32 bits.
16 /// @param dx Offset x from the corner, unskewed.
17 /// @param dy Offset y from the corner, unskewed.
18 2522406 inline float SimplexCorner(uint32_t ci, uint32_t cj, float dx, float dy, uint32_t seed) {
19 2522406 const float t = 0.5f - dx * dx - dy * dy;
20 2522406 const float t2 = t * t;
21
2/2
✓ Branch 0 taken 2281500 times.
✓ Branch 1 taken 240906 times.
2522406 const float corner = t2 * t2 * PerlinGradient2(PerlinCornerHash2(ci, cj, seed), dx, dy);
22
2/2
✓ Branch 0 taken 2281500 times.
✓ Branch 1 taken 240906 times.
2522406 return !(t <= 0.0f) ? corner : 0.0f;
23 }
24
25 /// The contribution (0.5 - r^2)^4 dot(g, d) of one corner of a 3D simplex, with `PerlinNoise3`'s
26 /// gradients and corner hash; 0 for r^2 >= 0.5.
27 7125713 inline float SimplexCorner3(uint32_t ci, uint32_t cj, uint32_t ck, float dx, float dy, float dz,
28 uint32_t seed) {
29 7125713 const float t = 0.5f - dx * dx - dy * dy - dz * dz;
30
2/2
✓ Branch 0 taken 5280848 times.
✓ Branch 1 taken 1844865 times.
7125713 if (t <= 0.0f) {
31 return 0.0f;
32 }
33 5280848 const float t2 = t * t;
34 5280848 return t2 * t2 * PerlinGradient3(PerlinCornerHash3(ci, cj, ck, seed), dx, dy, dz);
35 }
36
37 } // namespace detail
38
39 /// 2D simplex noise at a point in lattice units, with the gradients and corner hash of
40 /// `PerlinNoise2`.
41 /// @return Noise in [-1, 1].
42 840802 TERRAINGEN_KERNEL_INLINE float SimplexNoise2(float x, float y, uint32_t seed) {
43 840802 constexpr float kSkew = 0.366025403784438647f; // (sqrt(3) - 1) / 2
44 840802 constexpr float kUnskew = 0.211324865405187118f; // (3 - sqrt(3)) / 6
45 840802 constexpr float kScale = 99.0f;
46
47 840802 const float skew = (x + y) * kSkew;
48
16/16
terraingen::detail::SimplexCorner(unsigned int, unsigned int, float, float, unsigned int):
✓ Branch 0 taken 221762 times.
✓ Branch 1 taken 227567 times.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 200 times.
✓ Branch 4 taken 201 times.
✓ Branch 5 taken 199 times.
✓ Branch 6 taken 201 times.
✓ Branch 7 taken 199 times.
✓ Branch 8 taken 125105 times.
✓ Branch 9 taken 124895 times.
terraingen::detail::SimplexCorner3(unsigned int, unsigned int, unsigned int, float, float, float, unsigned int):
✓ Branch 0 taken 2412 times.
✓ Branch 1 taken 2304 times.
✓ Branch 2 taken 41260 times.
✓ Branch 2 taken 16601 times.
✓ Branch 3 taken 40660 times.
✓ Branch 3 taken 16632 times.
820198 const float cellX = floor(x + skew);
49 840802 const float cellY = floor(y + skew);
50 840802 const float unskew = (cellX + cellY) * kUnskew;
51 840802 const float x0 = x - (cellX - unskew);
52
4/4
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 19718 times.
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 199 times.
570001 const float y0 = y - (cellY - unskew);
53
54
14/14
terraingen::detail::SimplexCorner(unsigned int, unsigned int, float, float, unsigned int):
✓ Branch 0 taken 221762 times.
✓ Branch 1 taken 227567 times.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 200 times.
✓ Branch 4 taken 201 times.
✓ Branch 5 taken 199 times.
✓ Branch 6 taken 125105 times.
✓ Branch 7 taken 124895 times.
terraingen::detail::SimplexCorner3(unsigned int, unsigned int, unsigned int, float, float, float, unsigned int):
✓ Branch 0 taken 2412 times.
✓ Branch 1 taken 2304 times.
✓ Branch 2 taken 41260 times.
✓ Branch 2 taken 16601 times.
✓ Branch 3 taken 40660 times.
✓ Branch 3 taken 16632 times.
819998 const uint32_t stepX = x0 > y0 ? 1u : 0u;
55 820402 const uint32_t stepY = 1u - stepX;
56 820402 const float x1 = x0 - float(stepX) + kUnskew;
57 820402 const float y1 = y0 - float(stepY) + kUnskew;
58 820402 const float x2 = x0 - 1.0f + 2.0f * kUnskew;
59 820402 const float y2 = y0 - 1.0f + 2.0f * kUnskew;
60
61 820402 const uint32_t i = uint32_t(int32_t(cellX));
62 820402 const uint32_t j = uint32_t(int32_t(cellY));
63 820402 return kScale * (detail::SimplexCorner(i, j, x0, y0, seed) +
64 820402 detail::SimplexCorner(i + stepX, j + stepY, x1, y1, seed) +
65
4/4
✓ Branch 1 taken 282 times.
✓ Branch 2 taken 19718 times.
✓ Branch 3 taken 201 times.
✓ Branch 4 taken 199 times.
820095 detail::SimplexCorner(i + 1u, j + 1u, x2, y2, seed));
66 }
67
68 /// 3D simplex noise (Perlin 2001, Gustavson 2005) at a lattice cell and fraction, on the
69 /// tetrahedral lattice skewed by (x + y + z) / 3. The skew is split into integer and fractional
70 /// parts, so points far from the origin keep the precision of their fraction.
71 /// @return Noise in [-1, 1].
72 1781857 inline float SimplexNoise3(LatticePoint3 point, uint32_t seed) {
73 1781857 constexpr float kScale = 76.0f;
74 1781857 const int32_t cellSum = int32_t(uint32_t(point.ix) + uint32_t(point.iy) + uint32_t(point.iz));
75 1781857 int32_t third = cellSum / 3;
76
2/2
✓ Branch 0 taken 581204 times.
✓ Branch 1 taken 1200653 times.
1781857 if (cellSum - 3 * third < 0) {
77 581204 third -= 1;
78 }
79 1781857 const float remainder = float(cellSum - 3 * third);
80 1781857 const float skew = (remainder + point.fx + point.fy + point.fz) / 3.0f;
81
2/2
✓ Branch 0 taken 886130 times.
✓ Branch 1 taken 895727 times.
1781857 const float stepX = floor(point.fx + skew);
82 1781857 const float stepY = floor(point.fy + skew);
83 1781857 const float stepZ = floor(point.fz + skew);
84 1781857 const float unskew = (remainder + stepX + stepY + stepZ) / 6.0f;
85 1781857 const float x0 = point.fx - stepX + unskew;
86 1781857 const float y0 = point.fy - stepY + unskew;
87 1781857 const float z0 = point.fz - stepZ + unskew;
88
89
4/4
✓ Branch 0 taken 886130 times.
✓ Branch 1 taken 895727 times.
✓ Branch 2 taken 297140 times.
✓ Branch 3 taken 588990 times.
1781857 const uint32_t i1 = x0 >= y0 && x0 >= z0 ? 1u : 0u;
90
4/4
✓ Branch 0 taken 895741 times.
✓ Branch 1 taken 886116 times.
✓ Branch 2 taken 298614 times.
✓ Branch 3 taken 597127 times.
1781857 const uint32_t j1 = y0 > x0 && y0 >= z0 ? 1u : 0u;
91
4/4
✓ Branch 0 taken 892534 times.
✓ Branch 1 taken 889323 times.
✓ Branch 2 taken 296798 times.
✓ Branch 3 taken 595736 times.
1781857 const uint32_t k1 = z0 > x0 && z0 > y0 ? 1u : 0u;
92
4/4
✓ Branch 0 taken 895736 times.
✓ Branch 1 taken 886121 times.
✓ Branch 2 taken 595404 times.
✓ Branch 3 taken 300332 times.
1781857 const uint32_t i2 = x0 >= y0 || x0 >= z0 ? 1u : 0u;
93
4/4
✓ Branch 0 taken 886131 times.
✓ Branch 1 taken 895726 times.
✓ Branch 2 taken 593007 times.
✓ Branch 3 taken 293124 times.
1781857 const uint32_t j2 = y0 > x0 || y0 >= z0 ? 1u : 0u;
94
4/4
✓ Branch 0 taken 889337 times.
✓ Branch 1 taken 892520 times.
✓ Branch 2 taken 593471 times.
✓ Branch 3 taken 295866 times.
1781857 const uint32_t k2 = z0 > x0 || z0 > y0 ? 1u : 0u;
95 1781857 constexpr float kUnskew = 1.0f / 6.0f;
96 1781857 const float x1 = x0 - float(i1) + kUnskew;
97 1781857 const float y1 = y0 - float(j1) + kUnskew;
98 1781857 const float z1 = z0 - float(k1) + kUnskew;
99 1781857 const float x2 = x0 - float(i2) + 2.0f * kUnskew;
100 1781857 const float y2 = y0 - float(j2) + 2.0f * kUnskew;
101 1781857 const float z2 = z0 - float(k2) + 2.0f * kUnskew;
102 1781857 const float x3 = x0 - 1.0f + 3.0f * kUnskew;
103 1781857 const float y3 = y0 - 1.0f + 3.0f * kUnskew;
104 1781857 const float z3 = z0 - 1.0f + 3.0f * kUnskew;
105
106 1781857 const uint32_t i = uint32_t(point.ix) + uint32_t(third) + uint32_t(int32_t(stepX));
107 1781857 const uint32_t j = uint32_t(point.iy) + uint32_t(third) + uint32_t(int32_t(stepY));
108 1781857 const uint32_t k = uint32_t(point.iz) + uint32_t(third) + uint32_t(int32_t(stepZ));
109 1781857 const float sum = detail::SimplexCorner3(i, j, k, x0, y0, z0, seed) +
110 1781857 detail::SimplexCorner3(i + i1, j + j1, k + k1, x1, y1, z1, seed) +
111 1781857 detail::SimplexCorner3(i + i2, j + j2, k + k2, x2, y2, z2, seed) +
112 1781857 detail::SimplexCorner3(i + 1u, j + 1u, k + 1u, x3, y3, z3, seed);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1781857 times.
1781857 return clamp(kScale * sum, -1.0f, 1.0f);
114 }
115
116 /// 3D simplex noise at a point in lattice units.
117 /// @return Noise in [-1, 1].
118 28101 inline float SimplexNoise3(float x, float y, float z, uint32_t seed) {
119 28101 return SimplexNoise3(SplitLattice3(x, y, z), seed);
120 }
121
122 } // namespace terraingen
123