25#ifdef __SANITIZE_ADDRESS__
27# if defined(__GNUC__) || defined(__clang__)
28# define ATTR_NO_SIGNED_INT_OVERFLOW [[gnu::no_sanitize("signed-integer-overflow")]]
31# define ATTR_NO_SIGNED_INT_OVERFLOW
34# define ATTR_NO_SIGNED_INT_OVERFLOW
47 return (
x << k) | (
x >> (32 - k));
93 a =
b = c = 0xdeadbeef + (1 << 2) + 13;
101uint32_t
hash(uint32_t kx, uint32_t ky)
104 a =
b = c = 0xdeadbeef + (2 << 2) + 13;
113uint32_t
hash(uint32_t kx, uint32_t ky, uint32_t kz)
116 a =
b = c = 0xdeadbeef + (3 << 2) + 13;
126uint32_t
hash(uint32_t kx, uint32_t ky, uint32_t kz, uint32_t kw)
129 a =
b = c = 0xdeadbeef + (4 << 2) + 13;
163 v.x +=
v.y * 1664525;
164 v.y +=
v.x * 1664525;
166 v.x +=
v.y * 1664525;
167 v.y +=
v.x * 1664525;
332 return float(k & 0x7fffffff) * (1.0f /
float(0x7fffffff));
377template<
typename T>
T static mix(
T v0,
T v1,
float x)
379 return (1 -
x) * v0 +
x * v1;
395 return (1.0 -
y) * (v0 * x1 + v1 *
x) +
y * (
v2 * x1 + v3 *
x);
431 return z1 * (y1 * (v0 * x1 + v1 *
x) +
y * (
v2 * x1 + v3 *
x)) +
432 z * (y1 * (v4 * x1 + v5 *
x) +
y * (v6 * x1 + v7 *
x));
457 return mix(
mix(v0, v1,
v2, v3, v4, v5, v6, v7,
x,
y,
z),
458 mix(v8, v9, v10, v11, v12, v13, v14, v15,
x,
y,
z),
464 return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
469 return (condition != 0u) ? -value : value;
474 uint32_t h =
hash & 15u;
475 float g = 1u + (h & 7u);
481 uint32_t h =
hash & 7u;
482 float u = h < 4u ?
x :
y;
483 float v = 2.0 * (h < 4u ?
y :
x);
489 uint32_t h =
hash & 15u;
490 float u = h < 8u ?
x :
y;
491 float vt =
ELEM(h, 12u, 14u) ?
x :
z;
492 float v = h < 4u ?
y : vt;
498 uint32_t h =
hash & 31u;
499 float u = h < 24u ?
x :
y;
500 float v = h < 16u ?
y :
z;
501 float s = h < 8u ?
z :
w;
616 float precision_correction = 0.5f *
float(
math::abs(position) >= 1000000.0f);
619 position =
math::mod(position, 100000.0f) + precision_correction;
627 float(
math::abs(position.y) >= 1000000.0f));
631 position =
math::mod(position, 100000.0f) + precision_correction;
639 float(
math::abs(position.y) >= 1000000.0f),
640 float(
math::abs(position.z) >= 1000000.0f));
644 position =
math::mod(position, 100000.0f) + precision_correction;
652 float(
math::abs(position.y) >= 1000000.0f),
653 float(
math::abs(position.z) >= 1000000.0f),
654 float(
math::abs(position.w) >= 1000000.0f));
658 position =
math::mod(position, 100000.0f) + precision_correction;
689#if defined(_MSC_VER) && _MSC_VER >= 1930
698 const float roughness,
699 const float lacunarity,
712 fscale *= lacunarity;
717 float sum2 =
sum + t * amp;
718 return normalize ?
mix(0.5f *
sum / maxamp + 0.5f, 0.5f * sum2 / (maxamp + amp) + 0.5f, rmd) :
727 const float roughness,
728 const float lacunarity,
733 const float roughness,
734 const float lacunarity,
759 T p,
const float detail,
const float roughness,
const float lacunarity,
const float offset)
761 float pwr = roughness;
777 value += rmd * increment;
786 const float roughness,
787 const float lacunarity,
795 for (
int i = 0; (weight > 0.001f) && (
i <= int(
detail));
i++) {
796 weight = std::min(weight, 1.0f);
800 value += weight * signal;
801 weight *= gain * signal;
806 if ((rmd != 0.0f) && (weight > 0.001f)) {
807 weight = std::min(weight, 1.0f);
809 value += rmd * weight * signal;
818 const float roughness,
819 const float lacunarity,
823 float pwr = roughness;
827 float value = signal;
832 weight = std::clamp(signal * gain, 0.0f, 1.0f);
836 value += signal * pwr;
1044 position,
detail, roughness, lacunarity, offset, gain, type,
normalize),
1075 position,
detail, roughness, lacunarity, offset, gain, type,
normalize),
1106 position,
detail, roughness, lacunarity, offset, gain, type,
normalize),
1168 return std::abs(
b - a);
1177 return std::abs(a.x -
b.x) + std::abs(a.y -
b.y);
1179 return std::max(std::abs(a.x -
b.x), std::abs(a.y -
b.y));
1181 return std::pow(std::pow(std::abs(a.x -
b.x),
params.exponent) +
1182 std::pow(std::abs(a.y -
b.y),
params.exponent),
1197 return std::abs(a.x -
b.x) + std::abs(a.y -
b.y) + std::abs(a.z -
b.z);
1199 return std::max({std::abs(a.x -
b.x), std::abs(a.y -
b.y), std::abs(a.z -
b.z)});
1201 return std::pow(std::pow(std::abs(a.x -
b.x),
params.exponent) +
1202 std::pow(std::abs(a.y -
b.y),
params.exponent) +
1203 std::pow(std::abs(a.z -
b.z),
params.exponent),
1218 return std::abs(a.x -
b.x) + std::abs(a.y -
b.y) + std::abs(a.z -
b.z) + std::abs(a.w -
b.w);
1221 {std::abs(a.x -
b.x), std::abs(a.y -
b.y), std::abs(a.z -
b.z), std::abs(a.w -
b.w)});
1223 return std::pow(std::pow(std::abs(a.x -
b.x),
params.exponent) +
1224 std::pow(std::abs(a.y -
b.y),
params.exponent) +
1225 std::pow(std::abs(a.z -
b.z),
params.exponent) +
1226 std::pow(std::abs(a.w -
b.w),
params.exponent),
1250 return {0.0f, 0.0f, 0.0f, coord};
1255 float cellPosition =
floorf(coord);
1256 float localPosition = coord - cellPosition;
1259 float targetOffset = 0.0f;
1260 float targetPosition = 0.0f;
1261 for (
int i = -1;
i <= 1;
i++) {
1262 float cellOffset =
i;
1263 float pointPosition = cellOffset +
1266 if (distanceToPoint < minDistance) {
1267 targetOffset = cellOffset;
1268 minDistance = distanceToPoint;
1269 targetPosition = pointPosition;
1282 const bool calc_color)
1284 float cellPosition =
floorf(coord);
1285 float localPosition = coord - cellPosition;
1287 float smoothDistance = 0.0f;
1288 float smoothPosition = 0.0f;
1289 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1291 for (
int i = -2;
i <= 2;
i++) {
1292 float cellOffset =
i;
1293 float pointPosition = cellOffset +
1299 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) /
params.smoothness);
1300 float correctionFactor =
params.smoothness * h * (1.0f - h);
1301 smoothDistance =
mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1302 correctionFactor /= 1.0f + 3.0f *
params.smoothness;
1306 smoothColor =
mix(smoothColor, cellColor, h) - correctionFactor;
1308 smoothPosition =
mix(smoothPosition, pointPosition, h) - correctionFactor;
1313 octave.
color = smoothColor;
1320 float cellPosition =
floorf(coord);
1321 float localPosition = coord - cellPosition;
1325 float offsetF1 = 0.0f;
1326 float positionF1 = 0.0f;
1327 float offsetF2 = 0.0f;
1328 float positionF2 = 0.0f;
1329 for (
int i = -1;
i <= 1;
i++) {
1330 float cellOffset =
i;
1331 float pointPosition = cellOffset +
1334 if (distanceToPoint < distanceF1) {
1335 distanceF2 = distanceF1;
1336 distanceF1 = distanceToPoint;
1337 offsetF2 = offsetF1;
1338 offsetF1 = cellOffset;
1339 positionF2 = positionF1;
1340 positionF1 = pointPosition;
1342 else if (distanceToPoint < distanceF2) {
1343 distanceF2 = distanceToPoint;
1344 offsetF2 = cellOffset;
1345 positionF2 = pointPosition;
1358 float cellPosition =
floorf(coord);
1359 float localPosition = coord - cellPosition;
1364 float distanceToMidLeft =
fabsf((midPointPosition + leftPointPosition) / 2.0f - localPosition);
1365 float distanceToMidRight =
fabsf((midPointPosition + rightPointPosition) / 2.0f - localPosition);
1367 return math::min(distanceToMidLeft, distanceToMidRight);
1372 float cellPosition =
floorf(coord);
1373 float localPosition = coord - cellPosition;
1375 float closestPoint = 0.0f;
1376 float closestPointOffset = 0.0f;
1378 for (
int i = -1;
i <= 1;
i++) {
1379 float cellOffset =
i;
1380 float pointPosition = cellOffset +
1382 float distanceToPoint =
fabsf(pointPosition - localPosition);
1383 if (distanceToPoint < minDistance) {
1384 minDistance = distanceToPoint;
1385 closestPoint = pointPosition;
1386 closestPointOffset = cellOffset;
1391 float closestPointToClosestPoint = 0.0f;
1392 for (
int i = -1;
i <= 1;
i++) {
1396 float cellOffset =
i + closestPointOffset;
1397 float pointPosition = cellOffset +
1399 float distanceToPoint =
fabsf(closestPoint - pointPosition);
1400 if (distanceToPoint < minDistance) {
1401 minDistance = distanceToPoint;
1402 closestPointToClosestPoint = pointPosition;
1406 return fabsf(closestPointToClosestPoint - closestPoint) / 2.0f;
1413 return {coord.x, coord.y, 0.0f, 0.0f};
1419 float2 localPosition = coord - cellPosition_f;
1420 int2 cellPosition =
int2(cellPosition_f);
1423 int2 targetOffset = {0, 0};
1424 float2 targetPosition = {0.0f, 0.0f};
1425 for (
int j = -1; j <= 1; j++) {
1426 for (
int i = -1;
i <= 1;
i++) {
1427 int2 cellOffset(
i, j);
1431 if (distanceToPoint < minDistance) {
1432 targetOffset = cellOffset;
1433 minDistance = distanceToPoint;
1434 targetPosition = pointPosition;
1448 const bool calc_color)
1451 float2 localPosition = coord - cellPosition_f;
1452 int2 cellPosition =
int2(cellPosition_f);
1454 float smoothDistance = 0.0f;
1455 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1456 float2 smoothPosition = {0.0f, 0.0f};
1458 for (
int j = -2; j <= 2; j++) {
1459 for (
int i = -2;
i <= 2;
i++) {
1460 int2 cellOffset(
i, j);
1468 0.5f + 0.5f * (smoothDistance - distanceToPoint) /
params.smoothness);
1469 float correctionFactor =
params.smoothness * h * (1.0f - h);
1470 smoothDistance =
mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1471 correctionFactor /= 1.0f + 3.0f *
params.smoothness;
1475 smoothColor =
mix(smoothColor, cellColor, h) - correctionFactor;
1477 smoothPosition =
mix(smoothPosition, pointPosition, h) - correctionFactor;
1483 octave.
color = smoothColor;
1491 float2 localPosition = coord - cellPosition_f;
1492 int2 cellPosition =
int2(cellPosition_f);
1496 int2 offsetF1 = {0, 0};
1497 float2 positionF1 = {0.0f, 0.0f};
1498 int2 offsetF2 = {0, 0};
1499 float2 positionF2 = {0.0f, 0.0f};
1500 for (
int j = -1; j <= 1; j++) {
1501 for (
int i = -1;
i <= 1;
i++) {
1502 int2 cellOffset(
i, j);
1506 if (distanceToPoint < distanceF1) {
1507 distanceF2 = distanceF1;
1508 distanceF1 = distanceToPoint;
1509 offsetF2 = offsetF1;
1510 offsetF1 = cellOffset;
1511 positionF2 = positionF1;
1512 positionF1 = pointPosition;
1514 else if (distanceToPoint < distanceF2) {
1515 distanceF2 = distanceToPoint;
1516 offsetF2 = cellOffset;
1517 positionF2 = pointPosition;
1532 float2 localPosition = coord - cellPosition_f;
1533 int2 cellPosition =
int2(cellPosition_f);
1535 float2 vectorToClosest = {0.0f, 0.0f};
1537 for (
int j = -1; j <= 1; j++) {
1538 for (
int i = -1;
i <= 1;
i++) {
1539 int2 cellOffset(
i, j);
1543 float distanceToPoint =
math::dot(vectorToPoint, vectorToPoint);
1544 if (distanceToPoint < minDistance) {
1545 minDistance = distanceToPoint;
1546 vectorToClosest = vectorToPoint;
1552 for (
int j = -1; j <= 1; j++) {
1553 for (
int i = -1;
i <= 1;
i++) {
1554 int2 cellOffset(
i, j);
1558 float2 perpendicularToEdge = vectorToPoint - vectorToClosest;
1559 if (
math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
1560 float distanceToEdge =
math::dot((vectorToClosest + vectorToPoint) / 2.0f,
1562 minDistance =
math::min(minDistance, distanceToEdge);
1573 float2 localPosition = coord - cellPosition_f;
1574 int2 cellPosition =
int2(cellPosition_f);
1576 float2 closestPoint = {0.0f, 0.0f};
1577 int2 closestPointOffset = {0, 0};
1578 float minDistanceSq =
FLT_MAX;
1579 for (
int j = -1; j <= 1; j++) {
1580 for (
int i = -1;
i <= 1;
i++) {
1581 int2 cellOffset(
i, j);
1585 if (distanceToPointSq < minDistanceSq) {
1586 minDistanceSq = distanceToPointSq;
1587 closestPoint = pointPosition;
1588 closestPointOffset = cellOffset;
1594 float2 closestPointToClosestPoint = {0.0f, 0.0f};
1595 for (
int j = -1; j <= 1; j++) {
1596 for (
int i = -1;
i <= 1;
i++) {
1597 if (
i == 0 && j == 0) {
1600 int2 cellOffset =
int2(
i, j) + closestPointOffset;
1604 if (distanceToPointSq < minDistanceSq) {
1605 minDistanceSq = distanceToPointSq;
1606 closestPointToClosestPoint = pointPosition;
1611 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
1618 return {coord.x, coord.y, coord.z, 0.0f};
1624 float3 localPosition = coord - cellPosition_f;
1625 int3 cellPosition =
int3(cellPosition_f);
1628 int3 targetOffset = {0, 0, 0};
1629 float3 targetPosition = {0.0f, 0.0f, 0.0f};
1630 for (
int k = -1; k <= 1; k++) {
1631 for (
int j = -1; j <= 1; j++) {
1632 for (
int i = -1;
i <= 1;
i++) {
1633 int3 cellOffset(
i, j, k);
1637 if (distanceToPoint < minDistance) {
1638 targetOffset = cellOffset;
1639 minDistance = distanceToPoint;
1640 targetPosition = pointPosition;
1655 const bool calc_color)
1658 float3 localPosition = coord - cellPosition_f;
1659 int3 cellPosition =
int3(cellPosition_f);
1661 float smoothDistance = 0.0f;
1662 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1663 float3 smoothPosition = {0.0f, 0.0f, 0.0f};
1665 for (
int k = -2; k <= 2; k++) {
1666 for (
int j = -2; j <= 2; j++) {
1667 for (
int i = -2;
i <= 2;
i++) {
1668 int3 cellOffset(
i, j, k);
1676 0.5f + 0.5f * (smoothDistance - distanceToPoint) /
params.smoothness);
1677 float correctionFactor =
params.smoothness * h * (1.0f - h);
1678 smoothDistance =
mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1679 correctionFactor /= 1.0f + 3.0f *
params.smoothness;
1683 smoothColor =
mix(smoothColor, cellColor, h) - correctionFactor;
1685 smoothPosition =
mix(smoothPosition, pointPosition, h) - correctionFactor;
1692 octave.
color = smoothColor;
1700 float3 localPosition = coord - cellPosition_f;
1701 int3 cellPosition =
int3(cellPosition_f);
1705 int3 offsetF1 = {0, 0, 0};
1706 float3 positionF1 = {0.0f, 0.0f, 0.0f};
1707 int3 offsetF2 = {0, 0, 0};
1708 float3 positionF2 = {0.0f, 0.0f, 0.0f};
1709 for (
int k = -1; k <= 1; k++) {
1710 for (
int j = -1; j <= 1; j++) {
1711 for (
int i = -1;
i <= 1;
i++) {
1712 int3 cellOffset(
i, j, k);
1716 if (distanceToPoint < distanceF1) {
1717 distanceF2 = distanceF1;
1718 distanceF1 = distanceToPoint;
1719 offsetF2 = offsetF1;
1720 offsetF1 = cellOffset;
1721 positionF2 = positionF1;
1722 positionF1 = pointPosition;
1724 else if (distanceToPoint < distanceF2) {
1725 distanceF2 = distanceToPoint;
1726 offsetF2 = cellOffset;
1727 positionF2 = pointPosition;
1743 float3 localPosition = coord - cellPosition_f;
1744 int3 cellPosition =
int3(cellPosition_f);
1746 float3 vectorToClosest = {0.0f, 0.0f, 0.0f};
1748 for (
int k = -1; k <= 1; k++) {
1749 for (
int j = -1; j <= 1; j++) {
1750 for (
int i = -1;
i <= 1;
i++) {
1751 int3 cellOffset(
i, j, k);
1755 float distanceToPoint =
math::dot(vectorToPoint, vectorToPoint);
1756 if (distanceToPoint < minDistance) {
1757 minDistance = distanceToPoint;
1758 vectorToClosest = vectorToPoint;
1765 for (
int k = -1; k <= 1; k++) {
1766 for (
int j = -1; j <= 1; j++) {
1767 for (
int i = -1;
i <= 1;
i++) {
1768 int3 cellOffset(
i, j, k);
1772 float3 perpendicularToEdge = vectorToPoint - vectorToClosest;
1773 if (
math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
1774 float distanceToEdge =
math::dot((vectorToClosest + vectorToPoint) / 2.0f,
1776 minDistance =
math::min(minDistance, distanceToEdge);
1788 float3 localPosition = coord - cellPosition_f;
1789 int3 cellPosition =
int3(cellPosition_f);
1791 float3 closestPoint = {0.0f, 0.0f, 0.0f};
1792 int3 closestPointOffset = {0, 0, 0};
1793 float minDistanceSq =
FLT_MAX;
1794 for (
int k = -1; k <= 1; k++) {
1795 for (
int j = -1; j <= 1; j++) {
1796 for (
int i = -1;
i <= 1;
i++) {
1797 int3 cellOffset(
i, j, k);
1801 if (distanceToPointSq < minDistanceSq) {
1802 minDistanceSq = distanceToPointSq;
1803 closestPoint = pointPosition;
1804 closestPointOffset = cellOffset;
1811 float3 closestPointToClosestPoint = {0.0f, 0.0f, 0.0f};
1812 for (
int k = -1; k <= 1; k++) {
1813 for (
int j = -1; j <= 1; j++) {
1814 for (
int i = -1;
i <= 1;
i++) {
1815 if (
i == 0 && j == 0 && k == 0) {
1818 int3 cellOffset =
int3(
i, j, k) + closestPointOffset;
1822 if (distanceToPointSq < minDistanceSq) {
1823 minDistanceSq = distanceToPointSq;
1824 closestPointToClosestPoint = pointPosition;
1830 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
1843 float4 localPosition = coord - cellPosition_f;
1844 int4 cellPosition =
int4(cellPosition_f);
1847 int4 targetOffset = {0, 0, 0, 0};
1848 float4 targetPosition = {0.0f, 0.0f, 0.0f, 0.0f};
1849 for (
int u = -1; u <= 1; u++) {
1850 for (
int k = -1; k <= 1; k++) {
1851 for (
int j = -1; j <= 1; j++) {
1852 for (
int i = -1;
i <= 1;
i++) {
1853 int4 cellOffset(
i, j, k, u);
1858 if (distanceToPoint < minDistance) {
1859 targetOffset = cellOffset;
1860 minDistance = distanceToPoint;
1861 targetPosition = pointPosition;
1877 const bool calc_color)
1880 float4 localPosition = coord - cellPosition_f;
1881 int4 cellPosition =
int4(cellPosition_f);
1883 float smoothDistance = 0.0f;
1884 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1885 float4 smoothPosition = {0.0f, 0.0f, 0.0f, 0.0f};
1887 for (
int u = -2; u <= 2; u++) {
1888 for (
int k = -2; k <= 2; k++) {
1889 for (
int j = -2; j <= 2; j++) {
1890 for (
int i = -2;
i <= 2;
i++) {
1891 int4 cellOffset(
i, j, k, u);
1900 0.5f + 0.5f * (smoothDistance - distanceToPoint) /
params.smoothness);
1901 float correctionFactor =
params.smoothness * h * (1.0f - h);
1902 smoothDistance =
mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1903 correctionFactor /= 1.0f + 3.0f *
params.smoothness;
1907 smoothColor =
mix(smoothColor, cellColor, h) - correctionFactor;
1909 smoothPosition =
mix(smoothPosition, pointPosition, h) - correctionFactor;
1917 octave.
color = smoothColor;
1925 float4 localPosition = coord - cellPosition_f;
1926 int4 cellPosition =
int4(cellPosition_f);
1930 int4 offsetF1 = {0, 0, 0, 0};
1931 float4 positionF1 = {0.0f, 0.0f, 0.0f, 0.0f};
1932 int4 offsetF2 = {0, 0, 0, 0};
1933 float4 positionF2 = {0.0f, 0.0f, 0.0f, 0.0f};
1934 for (
int u = -1; u <= 1; u++) {
1935 for (
int k = -1; k <= 1; k++) {
1936 for (
int j = -1; j <= 1; j++) {
1937 for (
int i = -1;
i <= 1;
i++) {
1938 int4 cellOffset(
i, j, k, u);
1943 if (distanceToPoint < distanceF1) {
1944 distanceF2 = distanceF1;
1945 distanceF1 = distanceToPoint;
1946 offsetF2 = offsetF1;
1947 offsetF1 = cellOffset;
1948 positionF2 = positionF1;
1949 positionF1 = pointPosition;
1951 else if (distanceToPoint < distanceF2) {
1952 distanceF2 = distanceToPoint;
1953 offsetF2 = cellOffset;
1954 positionF2 = pointPosition;
1971 float4 localPosition = coord - cellPosition_f;
1972 int4 cellPosition =
int4(cellPosition_f);
1974 float4 vectorToClosest = {0.0f, 0.0f, 0.0f, 0.0f};
1976 for (
int u = -1; u <= 1; u++) {
1977 for (
int k = -1; k <= 1; k++) {
1978 for (
int j = -1; j <= 1; j++) {
1979 for (
int i = -1;
i <= 1;
i++) {
1980 int4 cellOffset(
i, j, k, u);
1985 float distanceToPoint =
math::dot(vectorToPoint, vectorToPoint);
1986 if (distanceToPoint < minDistance) {
1987 minDistance = distanceToPoint;
1988 vectorToClosest = vectorToPoint;
1996 for (
int u = -1; u <= 1; u++) {
1997 for (
int k = -1; k <= 1; k++) {
1998 for (
int j = -1; j <= 1; j++) {
1999 for (
int i = -1;
i <= 1;
i++) {
2000 int4 cellOffset(
i, j, k, u);
2005 float4 perpendicularToEdge = vectorToPoint - vectorToClosest;
2006 if (
math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
2007 float distanceToEdge =
math::dot((vectorToClosest + vectorToPoint) / 2.0f,
2009 minDistance =
math::min(minDistance, distanceToEdge);
2022 float4 localPosition = coord - cellPosition_f;
2023 int4 cellPosition =
int4(cellPosition_f);
2025 float4 closestPoint = {0.0f, 0.0f, 0.0f, 0.0f};
2026 int4 closestPointOffset = {0, 0, 0, 0};
2027 float minDistanceSq =
FLT_MAX;
2028 for (
int u = -1; u <= 1; u++) {
2029 for (
int k = -1; k <= 1; k++) {
2030 for (
int j = -1; j <= 1; j++) {
2031 for (
int i = -1;
i <= 1;
i++) {
2032 int4 cellOffset(
i, j, k, u);
2037 if (distanceToPointSq < minDistanceSq) {
2038 minDistanceSq = distanceToPointSq;
2039 closestPoint = pointPosition;
2040 closestPointOffset = cellOffset;
2048 float4 closestPointToClosestPoint = {0.0f, 0.0f, 0.0f, 0.0f};
2049 for (
int u = -1; u <= 1; u++) {
2050 for (
int k = -1; k <= 1; k++) {
2051 for (
int j = -1; j <= 1; j++) {
2052 for (
int i = -1;
i <= 1;
i++) {
2053 if (
i == 0 && j == 0 && k == 0 && u == 0) {
2056 int4 cellOffset =
int4(
i, j, k, u) + closestPointOffset;
2061 if (distanceToPointSq < minDistanceSq) {
2062 minDistanceSq = distanceToPointSq;
2063 closestPointToClosestPoint = pointPosition;
2070 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
2080 const bool calc_color )
2082 float amplitude = 1.0f;
2083 float max_amplitude = 0.0f;
2087 const bool zero_input =
params.detail == 0.0f ||
params.roughness == 0.0f;
2093 params.smoothness != 0.0f) ?
2098 max_amplitude = 1.0f;
2103 max_amplitude += amplitude;
2107 scale *=
params.lacunarity;
2108 amplitude *=
params.roughness;
2112 if (remainder != 0.0f) {
2113 max_amplitude =
mix(max_amplitude, max_amplitude + amplitude, remainder);
2124 output.distance /= max_amplitude *
params.max_distance;
2125 output.color /= max_amplitude;
2129 float4{0.0f, 0.0f, 0.0f, 0.0f};
2139 float amplitude = 1.0f;
2140 float max_amplitude =
params.max_distance;
2144 const bool zero_input =
params.detail == 0.0f ||
params.roughness == 0.0f;
2154 max_amplitude =
mix(max_amplitude,
params.max_distance / scale, amplitude);
2156 scale *=
params.lacunarity;
2157 amplitude *=
params.roughness;
2161 if (remainder != 0.0f) {
2162 float lerp_amplitude =
mix(max_amplitude,
params.max_distance / scale, amplitude);
2163 max_amplitude =
mix(max_amplitude, lerp_amplitude, remainder);
2164 float lerp_distance =
mix(
2182 const bool calc_color);
2185 const bool calc_color);
2188 const bool calc_color);
2191 const bool calc_color);
2257 const float frequency,
2258 const float orientation)
2263 const float windowed_gaussian_envelope = gaussian_envelop * hann_window;
2265 const float2 frequency_vector = frequency *
float2(
cos(orientation),
sin(orientation));
2269 return windowed_gaussian_envelope * phasor;
2309 const float integral_of_gabor_squared = 0.25f;
2310 const float second_moment = 0.5f;
2322 const float frequency,
2323 const float isotropy,
2324 const float base_orientation)
2330 const float3 seed_for_orientation(cell.x, cell.y,
i * 3);
2331 const float3 seed_for_kernel_center(cell.x, cell.y,
i * 3 + 1);
2332 const float3 seed_for_weight(cell.x, cell.y,
i * 3 + 2);
2340 const float orientation = base_orientation + random_orientation * isotropy;
2343 const float2 position_in_kernel_space = position - kernel_center;
2363 const float frequency,
2364 const float isotropy,
2365 const float base_orientation)
2368 const float2 local_position = coordinates - cell_position;
2371 for (
int j = -1; j <= 1; j++) {
2372 for (
int i = -1;
i <= 1;
i++) {
2374 const float2 current_cell_position = cell_position + cell_offset;
2375 const float2 position_in_cell_space = local_position - cell_offset;
2377 current_cell_position, position_in_cell_space, frequency, isotropy, base_orientation);
2389 const float frequency,
2390 const float3 orientation)
2395 const float windowed_gaussian_envelope = gaussian_envelop * hann_window;
2397 const float3 frequency_vector = frequency * orientation;
2401 return windowed_gaussian_envelope * phasor;
2410 const float second_moment = 0.5f;
2418 const float isotropy,
2422 if (isotropy == 0.0) {
2427 float inclination =
math::acos(orientation.z);
2436 inclination += random_angles.x * isotropy;
2437 azimuth += random_angles.y * isotropy;
2447 const float frequency,
2448 const float isotropy,
2449 const float3 base_orientation)
2455 const float4 seed_for_orientation(cell.x, cell.y, cell.z,
i * 3);
2456 const float4 seed_for_kernel_center(cell.x, cell.y, cell.z,
i * 3 + 1);
2457 const float4 seed_for_weight(cell.x, cell.y, cell.z,
i * 3 + 2);
2460 base_orientation, isotropy, seed_for_orientation);
2463 const float3 position_in_kernel_space = position - kernel_center;
2482 const float frequency,
2483 const float isotropy,
2484 const float3 base_orientation)
2487 const float3 local_position = coordinates - cell_position;
2490 for (
int k = -1; k <= 1; k++) {
2491 for (
int j = -1; j <= 1; j++) {
2492 for (
int i = -1;
i <= 1;
i++) {
2494 const float3 current_cell_position = cell_position + cell_offset;
2495 const float3 position_in_cell_space = local_position - cell_offset;
2497 current_cell_position, position_in_cell_space, frequency, isotropy, base_orientation);
2507 const float frequency,
2508 const float anisotropy,
2509 const float orientation,
2514 const float2 scaled_coordinates = coordinates * scale;
2515 const float isotropy = 1.0f -
math::clamp(anisotropy, 0.0f, 1.0f);
2516 const float sanitized_frequency =
math::max(0.001f, frequency);
2519 scaled_coordinates, sanitized_frequency, isotropy, orientation);
2524 const float normalization_factor = 6.0f * standard_deviation;
2529 *r_value = (phasor.y / normalization_factor) * 0.5f + 0.5f;
2540 *r_intensity =
math::length(phasor) / normalization_factor;
2546 const float frequency,
2547 const float anisotropy,
2548 const float3 orientation,
2553 const float3 scaled_coordinates = coordinates * scale;
2554 const float isotropy = 1.0f -
math::clamp(anisotropy, 0.0f, 1.0f);
2555 const float sanitized_frequency =
math::max(0.001f, frequency);
2559 scaled_coordinates, sanitized_frequency, isotropy, normalized_orientation);
2564 const float normalization_factor = 6.0f * standard_deviation;
2569 *r_value = (phasor.y / normalization_factor) * 0.5f + 0.5f;
2580 *r_intensity =
math::length(phasor) / normalization_factor;
#define BLI_assert_unreachable()
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
static T sum(const btAlignedObjectArray< T > &items)
static unsigned long seed
VecBase< float, D > normalize(VecOp< float, D >) RET
float distance(VecOp< float, D >, VecOp< float, D >) RET
MINLINE float smoothstep(float edge0, float edge1, float x)
T length_squared(const VecBase< T, Size > &a)
T cos(const AngleRadianBase< T > &a)
T clamp(const T &a, const T &min, const T &max)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T atan2(const T &y, const T &x)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T sin(const AngleRadianBase< T > &a)
T max(const T &a, const T &b)
T mod(const T &a, const T &b)
static ATTR_NO_SIGNED_INT_OVERFLOW int4 hash_pcg4d_i(int4 v)
static float int_to_float_01(int32_t k)
template VoronoiOutput fractal_voronoi_x_fx< float4 >(const VoronoiParams ¶ms, const float4 coord, const bool calc_color)
template float fractal_voronoi_distance_to_edge< float >(const VoronoiParams ¶ms, const float coord)
float3 perlin_float3_fractal_distorted(float position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
BLI_INLINE float negate_if(float value, uint32_t condition)
static float3 hash_int3_to_float3(int3 k)
BLI_INLINE float uint_to_float_01(uint32_t k)
float perlin_ridged_multi_fractal(T p, const float detail, const float roughness, const float lacunarity, const float offset, const float gain)
static float2 compute_3d_gabor_noise(const float3 coordinates, const float frequency, const float isotropy, const float3 base_orientation)
uint32_t hash_float(float kx)
static float compute_3d_gabor_standard_deviation()
template float perlin_fbm< float3 >(float3 p, const float detail, const float roughness, const float lacunarity, const bool normalize)
static float2 compute_2d_gabor_noise_cell(const float2 cell, const float2 position, const float frequency, const float isotropy, const float base_orientation)
BLI_INLINE float perlin_distortion(float position, float strength)
float hash_float_to_float(float k)
float perlin_signed(float position)
void gabor(const float2 coordinates, const float scale, const float frequency, const float anisotropy, const float orientation, float *r_value, float *r_phase, float *r_intensity)
template float perlin_fractal_distorted< float >(float position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
BLI_INLINE void hash_bit_mix(uint32_t &a, uint32_t &b, uint32_t &c)
float4 voronoi_position(const float coord)
BLI_INLINE float fade(float t)
@ NOISE_SHD_VORONOI_MANHATTAN
@ NOISE_SHD_VORONOI_MINKOWSKI
@ NOISE_SHD_VORONOI_EUCLIDEAN
@ NOISE_SHD_VORONOI_CHEBYCHEV
VoronoiOutput voronoi_smooth_f1(const VoronoiParams ¶ms, const float coord, const bool calc_color)
float voronoi_n_sphere_radius(const VoronoiParams ¶ms, const float coord)
static float2 compute_2d_gabor_noise(const float2 coordinates, const float frequency, const float isotropy, const float base_orientation)
float perlin(float position)
BLI_INLINE float random_float_offset(float seed)
static float voronoi_distance_bound(const T a, const T b, const VoronoiParams ¶ms)
BLI_INLINE float floor_fraction(float x, int &i)
float voronoi_distance_to_edge(const VoronoiParams ¶ms, const float coord)
BLI_INLINE uint32_t float_as_uint(float f)
static float4 hash_int4_to_float4(int4 k)
template VoronoiOutput fractal_voronoi_x_fx< float3 >(const VoronoiParams ¶ms, const float3 coord, const bool calc_color)
static float2 hash_int2_to_float2(int2 k)
template float perlin_fractal_distorted< float2 >(float2 position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
BLI_INLINE float3 random_float3_offset(float seed)
static float3 hash_int2_to_float3(int2 k)
static float2 compute_3d_gabor_noise_cell(const float3 cell, const float3 position, const float frequency, const float isotropy, const float3 base_orientation)
static float3 hash_int4_to_float3(int4 k)
static float2 compute_2d_gabor_kernel(const float2 position, const float frequency, const float orientation)
VoronoiOutput fractal_voronoi_x_fx(const VoronoiParams ¶ms, const T coord, const bool calc_color)
float voronoi_distance(const float a, const float b)
static float2 compute_3d_gabor_kernel(const float3 position, const float frequency, const float3 orientation)
@ NOISE_SHD_PERLIN_MULTIFRACTAL
@ NOISE_SHD_PERLIN_RIDGED_MULTIFRACTAL
@ NOISE_SHD_PERLIN_HYBRID_MULTIFRACTAL
@ NOISE_SHD_PERLIN_HETERO_TERRAIN
template float perlin_fractal_distorted< float4 >(float4 position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
BLI_INLINE float2 random_float2_offset(float seed)
float perlin_fbm(T p, float detail, float roughness, float lacunarity, bool normalize)
VoronoiOutput voronoi_f2(const VoronoiParams ¶ms, const float coord)
float3 hash_float_to_float3(float k)
uint32_t hash(uint32_t kx)
template VoronoiOutput fractal_voronoi_x_fx< float >(const VoronoiParams ¶ms, const float coord, const bool calc_color)
template float perlin_fbm< float2 >(float2 p, const float detail, const float roughness, const float lacunarity, const bool normalize)
@ NOISE_SHD_VORONOI_N_SPHERE_RADIUS
@ NOISE_SHD_VORONOI_SMOOTH_F1
@ NOISE_SHD_VORONOI_DISTANCE_TO_EDGE
BLI_INLINE float4 random_float4_offset(float seed)
BLI_INLINE float perlin_noise(float position)
static ATTR_NO_SIGNED_INT_OVERFLOW int3 hash_pcg3d_i(int3 v)
float fractal_voronoi_distance_to_edge(const VoronoiParams ¶ms, const T coord)
BLI_INLINE void hash_bit_final(uint32_t &a, uint32_t &b, uint32_t &c)
float4 hash_float_to_float4(float4 k)
float perlin_hybrid_multi_fractal(T p, const float detail, const float roughness, const float lacunarity, const float offset, const float gain)
static float compute_2d_gabor_standard_deviation()
float perlin_select(T p, float detail, float roughness, float lacunarity, float offset, float gain, int type, bool normalize)
BLI_INLINE float noise_grad(uint32_t hash, float x)
VoronoiOutput voronoi_f1(const VoronoiParams ¶ms, const float coord)
float perlin_multi_fractal(T p, const float detail, const float roughness, const float lacunarity)
template float fractal_voronoi_distance_to_edge< float3 >(const VoronoiParams ¶ms, const float3 coord)
static constexpr int gabor_impulses_count
static T mix(T v0, T v1, float x)
float perlin_fractal_distorted(T position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
static ATTR_NO_SIGNED_INT_OVERFLOW int2 hash_pcg2d_i(int2 v)
template float perlin_fractal_distorted< float3 >(float3 position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
template VoronoiOutput fractal_voronoi_x_fx< float2 >(const VoronoiParams ¶ms, const float2 coord, const bool calc_color)
template float fractal_voronoi_distance_to_edge< float4 >(const VoronoiParams ¶ms, const float4 coord)
BLI_INLINE uint32_t hash_bit_rotate(uint32_t x, uint32_t k)
float perlin_hetero_terrain(T p, const float detail, const float roughness, const float lacunarity, const float offset)
float2 hash_float_to_float2(float2 k)
static float3 compute_3d_orientation(const float3 orientation, const float isotropy, const float4 seed)
float hash_to_float(uint32_t kx)
template float fractal_voronoi_distance_to_edge< float2 >(const VoronoiParams ¶ms, const float2 coord)
VecBase< int32_t, 4 > int4
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
VecBase< float, 3 > float3
#define ATTR_NO_SIGNED_INT_OVERFLOW
VecBase< T, 3 > xyz() const