CultMath sites: exact power cell border (radical planes, midpoint form), validated cell spans, one shader name per segment function
power_border_step/power_border give the exact distance to the owner's power cell boundary: the least radical-plane distance over every other site, built from the offset and (w1-w2)(w1+w2) so it does not cancel. site_cells_border is documented as the two-winner bisector distance. Cell spans refuse non-finite positions, queries and spans over 2^24. segment_closest is public in C#; the HLSL segment functions are 3D only (no new overload sets). Tests added. Does not build the GLSL checks: CultMath.glsl and glsl-parity.json are regenerated in the next commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ee8b15ec2c
commit
bcd8a58055
9 changed files with 304 additions and 69 deletions
|
|
@ -28,12 +28,12 @@ shader semantics.
|
|||
`min`, `max`, `clamp`, `saturate`, `lerp`, `step`, `smoothstep`, `dot`,
|
||||
`cross`, `length`, `distance`, `normalize`, `reflect`, `csum`, `erf`,
|
||||
`erfinv` (scalar only), exponential
|
||||
`decay`/`damp`, intercept/segment helpers (`distance_to_segment` in 2D and 3D, with and without `out closest`), Catmull-Rom, Bezier curves, and
|
||||
`decay`/`damp`, intercept/segment helpers (`distance_to_segment` in 2D and 3D, with and without `out closest`, and `segment_closest`), Catmull-Rom, Bezier curves, and
|
||||
deterministic value-noise primitives
|
||||
- Site cells: `power_cells` and `multiplicative_cells` return the two nearest sites of a point as `CultSiteCells`,
|
||||
`soft_gabriel_gate` gates a pair of sites by the empty-disc margin. Each is a loop over the shader steps
|
||||
`cultmath_power_score` / `cultmath_multiplicative_score`, `cultmath_site_cells_empty`, `cultmath_site_cells_insert`,
|
||||
`cultmath_site_cells_border` and `cultmath_gabriel_margin`; a shader loops over its own buffer.
|
||||
`cultmath_site_cells_border`, `cultmath_power_border_step` (the exact power cell border, `power_border`) and `cultmath_gabriel_margin`; a shader loops over its own buffer.
|
||||
- `shaders/CultMath.hlsl`, a canonical HLSL mirror include for shader-side
|
||||
parity. HLSL already owns `float2`, `float3`, and `float4`; the include
|
||||
exposes `cultmath_*` functions for shared semantics HLSL does not provide.
|
||||
|
|
|
|||
|
|
@ -494,8 +494,8 @@ so static members need a qualified type (`CultMath.float3x3.Euler(...)`).
|
|||
A site is `float4(position.xyz, weight)`; a 2D consumer uses z = 0. `math.Sites.cs` scores a query point
|
||||
against a site two ways and keeps the two best:
|
||||
|
||||
- `power_score(p, site)`: value `|p - s|^2 - w^2`, gradient `2 (p - s)`. `F2 - F1` is affine in the query
|
||||
point, so `site_cells_border`'s first-order distance is the exact distance to the power bisector.
|
||||
- `power_score(p, site)`: value `|p - s|^2 - w^2`, gradient `2 (p - s)`. A power cell is the intersection of the
|
||||
half-spaces bounded by the radical planes against every other site, so `power_border` is exact.
|
||||
- `multiplicative_score(p, site)`: value `|p - s| / w`, gradient `(p - s) / (|p - s| w)`, zero at `p = s`
|
||||
(cellular's degenerate convention). Its borders are Apollonius circles, and the border distance is first order.
|
||||
|
||||
|
|
@ -503,7 +503,16 @@ A span function is a C# loop over a per-site step that has a shader twin, becaus
|
|||
span or a buffer. `site_cells_empty()` starts the fold, `site_cells_insert(cells, score, index)` folds one score
|
||||
in (strict `<` against F1, the old F1 moving to second, else strict `<` against F2, so an exact tie keeps the
|
||||
earlier index first), and `site_cells_border(cells)` reads `float4(unit normal, distance)`, with `n = grad F2 -
|
||||
grad F1` pointing toward the nearest site and `(0, 0, 0, 1.0e30)` when there is no second site. `power_cells` and
|
||||
grad F1` pointing toward the nearest site and `(0, 0, 0, 1.0e30)` when there is no second site. That border is
|
||||
the distance to the bisector of the two lowest scores, first order and for either metric; it is not the distance
|
||||
to the nearest site's cell boundary (the F2 - F1 edge error: the second-lowest score is not the nearest plane in
|
||||
general). The exact power cell border is a second pass: `power_border_step(p, owner, other, border)` takes the
|
||||
running `float4(unit normal toward the owner, distance)` and the radical plane of `owner` against `other`, with
|
||||
the distance built from the offset `owner - p` and the weight difference `(w1 - w2)(w1 + w2)` so it does not
|
||||
cancel for close sites seen from far; the cell boundary distance is the least over every site except the owner
|
||||
(the fold's nearest), and a site coincident with the owner has no plane. `power_border(p, sites)` is that loop.
|
||||
Cell spans refuse a non-finite query, site position or weight, and more than 2^24 sites (indices are floats).
|
||||
`power_cells` and
|
||||
`multiplicative_cells` are that loop over a `ReadOnlySpan<float4>` and nothing else; a shader caller writes the
|
||||
same loop over its own buffer. Every site of an explicit span is visited, so no window and no weight-ratio range
|
||||
apply. `CultSiteCells` holds the two score fields and the two site indices (floats, -1 for none).
|
||||
|
|
@ -512,7 +521,8 @@ apply. `CultSiteCells` holds the two score fields and the two site indices (floa
|
|||
circle, negative inside it, -1 when `a` and `b` coincide. `soft_gabriel_gate(sites, first, second, softness)`
|
||||
is `smoothstep(-softness, softness, least margin over every other site)` on positions alone, 1 when no other site
|
||||
exists; it is C# only. `distance_to_segment(point, start, end)` has out-free 2D and 3D forms beside the `out`
|
||||
form, all over one `segment_closest` per dimension.
|
||||
form, all over one public `segment_closest` per dimension. The shader twins keep one name each (3D only, a 2D
|
||||
caller uses z = 0), so none joins the overload rename list.
|
||||
|
||||
## Intervals
|
||||
|
||||
|
|
|
|||
|
|
@ -605,6 +605,10 @@ CultCellular cultmath_cellular(float3 p)
|
|||
// CultSiteCells cells = cultmath_site_cells_empty();
|
||||
// for (int i = 0; i < count; i++)
|
||||
// cells = cultmath_site_cells_insert(cells, cultmath_power_score(p, sites[i]), i);
|
||||
// The exact power cell border is a second loop over the buffer, skipping the owner cells.nearestSite:
|
||||
// float4 border = float4(0.0, 0.0, 0.0, 1.0e30); int owner = (int)cells.nearestSite;
|
||||
// for (int k = 0; k < count; k++) if (k != owner) border = cultmath_power_border_step(p, sites[owner], sites[k], border);
|
||||
// Shader names are unique (one 3D form of segment distance; a 2D caller uses z = 0), so none joins the overload rename list.
|
||||
// Statement for statement with math.Sites.cs (power_score, multiplicative_score, site_cells_*, gabriel_margin).
|
||||
struct CultSiteCells
|
||||
{
|
||||
|
|
@ -666,6 +670,16 @@ float4 cultmath_site_cells_border(CultSiteCells cells)
|
|||
return float4(n / len, (cells.second.w - cells.nearest.w) / len);
|
||||
}
|
||||
|
||||
float4 cultmath_power_border_step(float3 p, float4 owner, float4 other, float4 border)
|
||||
{
|
||||
float3 d = other.xyz - owner.xyz;
|
||||
float len = length(d);
|
||||
if (len <= 0.0)
|
||||
return border;
|
||||
float plane = max((dot(owner.xyz - p, d) + 0.5 * (dot(d, d) + (owner.w - other.w) * (owner.w + other.w))) / len, 0.0);
|
||||
return plane < border.w ? float4(-d / len, plane) : border;
|
||||
}
|
||||
|
||||
float cultmath_gabriel_margin(float3 a, float3 b, float3 other)
|
||||
{
|
||||
float r = cultmath_distance(a, b) * 0.5;
|
||||
|
|
@ -674,17 +688,6 @@ float cultmath_gabriel_margin(float3 a, float3 b, float3 other)
|
|||
return (cultmath_distance(other, (a + b) * 0.5) - r) / r;
|
||||
}
|
||||
|
||||
float2 cultmath_segment_closest(float2 point_value, float2 start, float2 end)
|
||||
{
|
||||
float2 segment = end - start;
|
||||
float segmentLengthSq = cultmath_lengthsq(segment);
|
||||
if (segmentLengthSq <= 0.0)
|
||||
return start;
|
||||
|
||||
float t = cultmath_saturate(dot(point_value - start, segment) / segmentLengthSq);
|
||||
return start + segment * t;
|
||||
}
|
||||
|
||||
float3 cultmath_segment_closest(float3 point_value, float3 start, float3 end)
|
||||
{
|
||||
float3 segment = end - start;
|
||||
|
|
@ -696,11 +699,6 @@ float3 cultmath_segment_closest(float3 point_value, float3 start, float3 end)
|
|||
return start + segment * t;
|
||||
}
|
||||
|
||||
float cultmath_distance_to_segment(float2 point_value, float2 start, float2 end)
|
||||
{
|
||||
return cultmath_distance(point_value, cultmath_segment_closest(point_value, start, end));
|
||||
}
|
||||
|
||||
float cultmath_distance_to_segment(float3 point_value, float3 start, float3 end)
|
||||
{
|
||||
return cultmath_distance(point_value, cultmath_segment_closest(point_value, start, end));
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ public static partial class math
|
|||
// a per-site step that has a shader twin (site_cells_empty, then site_cells_insert per site); a shader
|
||||
// cannot take a span, so shader callers write the same loop over their own buffer. One fold, one rule.
|
||||
|
||||
// Power score of a site: value |p - s|^2 - w^2, gradient 2 (p - s). With this score F2 - F1 is affine in
|
||||
// the query point, so site_cells_border's first-order distance is the exact distance to the power bisector.
|
||||
// Power score of a site: value |p - s|^2 - w^2, gradient 2 (p - s). The cell of a site is the intersection
|
||||
// of the half-spaces bounded by its radical planes against every other site (power_border_step).
|
||||
public static float4 power_score(float3 p, float4 site)
|
||||
{
|
||||
var offset = p - site.xyz;
|
||||
|
|
@ -49,10 +49,10 @@ public static partial class math
|
|||
return result;
|
||||
}
|
||||
|
||||
// float4(unit normal, distance) of the border between the two nearest sites: n = grad F2 - grad F1,
|
||||
// distance (F2 - F1) / |n|, normal n / |n|. First order; exact for power scores. n is the direction in
|
||||
// which F2 - F1 grows, into the nearest site's cell. (0, 0, 0, 1.0e30) when there is no second site or
|
||||
// |n| = 0.
|
||||
// float4(unit normal, distance) of the bisector of the two lowest scores: n = grad F2 - grad F1, distance
|
||||
// (F2 - F1) / |n|, normal n / |n| (into the nearest site's cell). First order, for either metric. It is NOT
|
||||
// the distance to the nearest site's cell boundary, which the second-lowest score does not give in general;
|
||||
// for power scores that is power_border. (0, 0, 0, 1.0e30) when there is no second site or |n| = 0.
|
||||
public static float4 site_cells_border(CultSiteCells cells)
|
||||
{
|
||||
var n = cells.second.xyz - cells.nearest.xyz;
|
||||
|
|
@ -62,6 +62,24 @@ public static partial class math
|
|||
return new float4(n / len, (cells.second.w - cells.nearest.w) / len);
|
||||
}
|
||||
|
||||
// One step of the exact power cell border. owner is the site whose cell p lies in (the fold's nearest);
|
||||
// other is another site. The radical plane of the pair is {x : |x - s1|^2 - w1^2 = |x - s2|^2 - w2^2};
|
||||
// with d = s2 - s1 the signed distance from p to it, positive on the owner's side, is
|
||||
// ((s1 - p).d + (|d|^2 + w1^2 - w2^2) / 2) / |d|. It is built from the offset s1 - p and the weight
|
||||
// difference (w1 - w2)(w1 + w2), never from a difference of two large squared distances, so it does not
|
||||
// cancel for a far query or close sites. border is the running (unit normal toward the owner, distance),
|
||||
// starting at (0, 0, 0, 1.0e30): the cell's boundary distance is the least plane distance over every other
|
||||
// site. A site coincident with the owner has no plane and leaves border unchanged.
|
||||
public static float4 power_border_step(float3 p, float4 owner, float4 other, float4 border)
|
||||
{
|
||||
var d = other.xyz - owner.xyz;
|
||||
var len = length(d);
|
||||
if (len <= 0.0f)
|
||||
return border;
|
||||
var plane = max((dot(owner.xyz - p, d) + 0.5f * (dot(d, d) + (owner.w - other.w) * (owner.w + other.w))) / len, 0.0f);
|
||||
return plane < border.w ? new float4(-d / len, plane) : border;
|
||||
}
|
||||
|
||||
// Gabriel margin of the pair (a, b) against a third site: (|other - mid| - r) / r with mid = (a + b) / 2
|
||||
// and r = |a - b| / 2. 0 on the diameter circle, negative inside it, -1 when a and b coincide.
|
||||
public static float gabriel_margin(float3 a, float3 b, float3 other)
|
||||
|
|
@ -72,39 +90,73 @@ public static partial class math
|
|||
return (distance(other, (a + b) * 0.5f) - r) / r;
|
||||
}
|
||||
|
||||
// Index floats hold integers exactly up to 2^24, so a longer span is refused.
|
||||
private const int MaxSiteSpan = 1 << 24;
|
||||
|
||||
private static bool all_finite(float3 v) => float.IsFinite(v.x) && float.IsFinite(v.y) && float.IsFinite(v.z);
|
||||
|
||||
// The one validation of a cell span: finite query, finite positions and weights, weights above zero when
|
||||
// positiveWeights, at most 2^24 sites. Messages name the parameter, never a value.
|
||||
private static void require_cell_span(float3 p, ReadOnlySpan<float4> sites, bool positiveWeights)
|
||||
{
|
||||
if (!all_finite(p))
|
||||
throw new ArgumentException("The query point must be finite.", nameof(p));
|
||||
if (sites.Length > MaxSiteSpan)
|
||||
throw new ArgumentException("A site span holds at most 2^24 sites.", nameof(sites));
|
||||
for (var i = 0; i < sites.Length; i++)
|
||||
{
|
||||
if (!all_finite(sites[i].xyz))
|
||||
throw new ArgumentException("Site positions must be finite.", nameof(sites));
|
||||
if (!float.IsFinite(sites[i].w) || (positiveWeights && !(sites[i].w > 0.0f)))
|
||||
throw new ArgumentException(positiveWeights ? "Site weights must be finite and greater than zero." : "Site weights must be finite.", nameof(sites));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The two nearest sites of <paramref name="p"/> by <see cref="power_score"/> (the power diagram), every
|
||||
/// site visited. A site is <c>float4(position.xyz, weight)</c>; use z = 0 for 2D. Ties keep the lower index first.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentException">A site weight is not finite.</exception>
|
||||
/// <exception cref="ArgumentException">The query or a site position or weight is not finite, or the span holds more than 2^24 sites.</exception>
|
||||
public static CultSiteCells power_cells(float3 p, ReadOnlySpan<float4> sites)
|
||||
{
|
||||
require_cell_span(p, sites, positiveWeights: false);
|
||||
var cells = site_cells_empty();
|
||||
for (var i = 0; i < sites.Length; i++)
|
||||
{
|
||||
if (!float.IsFinite(sites[i].w))
|
||||
throw new ArgumentException("Site weights must be finite.", nameof(sites));
|
||||
cells = site_cells_insert(cells, power_score(p, sites[i]), i);
|
||||
}
|
||||
|
||||
return cells;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The exact distance from <paramref name="p"/> to the boundary of its power cell, and the unit normal of the
|
||||
/// nearest boundary plane pointing into the cell: <c>float4(normal, distance)</c>. The cell is the
|
||||
/// intersection of the half-spaces bounded by the radical planes against every other site, so the distance is
|
||||
/// the least <see cref="power_border_step"/> over them. A site coincident with the owner has no plane; a cell
|
||||
/// with no bounding plane (one site, or only coincident ones) gives <c>(0, 0, 0, 1.0e30)</c>.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentException">As <see cref="power_cells"/>.</exception>
|
||||
public static float4 power_border(float3 p, ReadOnlySpan<float4> sites)
|
||||
{
|
||||
var border = new float4(0.0f, 0.0f, 0.0f, 1.0e30f);
|
||||
var owner = (int)power_cells(p, sites).nearestSite;
|
||||
if (owner < 0)
|
||||
return border;
|
||||
for (var k = 0; k < sites.Length; k++)
|
||||
if (k != owner)
|
||||
border = power_border_step(p, sites[owner], sites[k], border);
|
||||
return border;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The two nearest sites of <paramref name="p"/> by <see cref="multiplicative_score"/> (distance divided by
|
||||
/// weight), every site visited. A site is <c>float4(position.xyz, weight)</c>; use z = 0 for 2D.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentException">A site weight is not finite and greater than zero.</exception>
|
||||
/// <exception cref="ArgumentException">The query or a site position is not finite, a weight is not finite and greater than zero, or the span holds more than 2^24 sites.</exception>
|
||||
public static CultSiteCells multiplicative_cells(float3 p, ReadOnlySpan<float4> sites)
|
||||
{
|
||||
require_cell_span(p, sites, positiveWeights: true);
|
||||
var cells = site_cells_empty();
|
||||
for (var i = 0; i < sites.Length; i++)
|
||||
{
|
||||
if (!(float.IsFinite(sites[i].w) && sites[i].w > 0.0f))
|
||||
throw new ArgumentException("Site weights must be finite and greater than zero.", nameof(sites));
|
||||
cells = site_cells_insert(cells, multiplicative_score(p, sites[i]), i);
|
||||
}
|
||||
|
||||
return cells;
|
||||
}
|
||||
|
||||
|
|
@ -114,7 +166,7 @@ public static partial class math
|
|||
/// least <see cref="gabriel_margin"/> over every other site). 1 when no other site exists.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentOutOfRangeException">An index is outside the span.</exception>
|
||||
/// <exception cref="ArgumentException">The indices are equal, or softness is not finite and greater than zero.</exception>
|
||||
/// <exception cref="ArgumentException">The indices are equal, softness is not finite and greater than zero, or a site position is not finite.</exception>
|
||||
public static float soft_gabriel_gate(ReadOnlySpan<float4> sites, int first, int second, float softness)
|
||||
{
|
||||
if (first < 0 || first >= sites.Length)
|
||||
|
|
@ -126,6 +178,10 @@ public static partial class math
|
|||
if (!(float.IsFinite(softness) && softness > 0.0f))
|
||||
throw new ArgumentException("Softness must be finite and greater than zero.", nameof(softness));
|
||||
|
||||
for (var k = 0; k < sites.Length; k++)
|
||||
if (!all_finite(sites[k].xyz))
|
||||
throw new ArgumentException("Site positions must be finite.", nameof(sites));
|
||||
|
||||
var margin = float.PositiveInfinity;
|
||||
for (var k = 0; k < sites.Length; k++)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -330,7 +330,8 @@ public static partial class math
|
|||
public static float distance_to_segment(float3 point, float3 start, float3 end) =>
|
||||
distance(point, segment_closest(point, start, end));
|
||||
|
||||
private static float2 segment_closest(float2 point_value, float2 start, float2 end)
|
||||
/// <summary>The point of the segment <paramref name="start"/>-<paramref name="end"/> nearest <paramref name="point_value"/>; a degenerate segment is its start.</summary>
|
||||
public static float2 segment_closest(float2 point_value, float2 start, float2 end)
|
||||
{
|
||||
var segment = end - start;
|
||||
var segmentLengthSq = lengthsq(segment);
|
||||
|
|
@ -341,7 +342,8 @@ public static partial class math
|
|||
return start + segment * t;
|
||||
}
|
||||
|
||||
private static float3 segment_closest(float3 point_value, float3 start, float3 end)
|
||||
/// <summary>The point of the segment <paramref name="start"/>-<paramref name="end"/> nearest <paramref name="point_value"/>; a degenerate segment is its start.</summary>
|
||||
public static float3 segment_closest(float3 point_value, float3 start, float3 end)
|
||||
{
|
||||
var segment = end - start;
|
||||
var segmentLengthSq = lengthsq(segment);
|
||||
|
|
|
|||
|
|
@ -797,16 +797,24 @@ static int4 clamp(int4 x, int4 a, int4 b) { if (a.x > b.x || a.y > b.y || a.z >
|
|||
new Family("mat3 cultmath_snoise_hessian(vec3)", "ulp-bounded, coordinate bands 1e2 to 4e6", r => new object[] { BandPoint3(r, 1.0e2, 4.0e6) }, SpanAllowanceRead, Verify: cases => AssertSpanAllowance(cases, hessian: true, small: false)),
|
||||
// Segment distance in 2D and 3D (one segment in eight degenerate), then the site cells: scores, fold step
|
||||
// over states built by inserting 0-3 random scores (one in four with an exactly tied score), border, margin.
|
||||
new Family("float cultmath_distance_to_segment(vec2, vec2, vec2)", "ulp-bounded", r =>
|
||||
{
|
||||
var start = Point2(r);
|
||||
return new object[] { Point2(r), start, r.Next(8) == 0 ? start : Point2(r) };
|
||||
}),
|
||||
new Family("float cultmath_distance_to_segment(vec3, vec3, vec3)", "ulp-bounded", r =>
|
||||
{
|
||||
var start = Point3(r);
|
||||
return new object[] { Point3(r), start, r.Next(8) == 0 ? start : Point3(r) };
|
||||
}),
|
||||
new Family("vec3 cultmath_segment_closest(vec3, vec3, vec3)", "ulp-bounded", r =>
|
||||
{
|
||||
var start = Point3(r);
|
||||
return new object[] { Point3(r), start, r.Next(8) == 0 ? start : Point3(r) };
|
||||
}),
|
||||
// The border step: owner and other random (one in six coincident), a running border either empty or a
|
||||
// random (normal, distance).
|
||||
new Family("vec4 cultmath_power_border_step(vec3, vec4, vec4, vec4)", "ulp-bounded", r =>
|
||||
{
|
||||
var owner = Site(r, 0.1f, 20.0f);
|
||||
var border = r.Next(2) == 0 ? new float4(0.0f, 0.0f, 0.0f, 1.0e30f) : new float4(Point3(r), Uniform(r, 0.0f, 50.0f));
|
||||
return new object[] { Point3(r), owner, r.Next(6) == 0 ? owner : Site(r, 0.1f, 20.0f), border };
|
||||
}),
|
||||
new Family("vec4 cultmath_power_score(vec3, vec4)", "ulp-bounded", r => new object[] { Point3(r), Site(r, 0.1f, 20.0f) }),
|
||||
new Family("vec4 cultmath_multiplicative_score(vec3, vec4)", "ulp-bounded", r => new object[] { Point3(r), Site(r, 0.1f, 100.0f, logWeight: true) }),
|
||||
new Family("CultSiteCells cultmath_site_cells_insert(CultSiteCells, vec4, int)", "ulp-bounded", r =>
|
||||
|
|
@ -837,7 +845,6 @@ static int4 clamp(int4 x, int4 a, int4 b) { if (a.x > b.x || a.y > b.y || a.z >
|
|||
return new float3((float)v[0], (float)v[1], (float)v[2]);
|
||||
}
|
||||
|
||||
private static float2 Point2(System.Random r) => new(Uniform(r, -50.0f, 50.0f), Uniform(r, -50.0f, 50.0f));
|
||||
|
||||
// A site float4(position.xyz, weight): the weight uniform in [lo, hi], or log-uniform when logWeight.
|
||||
private static float4 Site(System.Random r, float lo, float hi, bool logWeight = false)
|
||||
|
|
|
|||
|
|
@ -34,7 +34,8 @@ public sealed class HlslSourceCompatibilityTests
|
|||
Assert.NotNull(ShaderFunction(shader, "cultmath_site_cells_insert", siteCells, typeof(float4), typeof(int)));
|
||||
Assert.NotNull(ShaderFunction(shader, "cultmath_site_cells_border", siteCells));
|
||||
Assert.NotNull(ShaderFunction(shader, "cultmath_gabriel_margin", typeof(float3), typeof(float3), typeof(float3)));
|
||||
Assert.NotNull(ShaderFunction(shader, "cultmath_distance_to_segment", typeof(float2), typeof(float2), typeof(float2)));
|
||||
Assert.NotNull(ShaderFunction(shader, "cultmath_power_border_step", typeof(float3), typeof(float4), typeof(float4), typeof(float4)));
|
||||
Assert.NotNull(ShaderFunction(shader, "cultmath_segment_closest", typeof(float3), typeof(float3), typeof(float3)));
|
||||
Assert.NotNull(ShaderFunction(shader, "cultmath_distance_to_segment", typeof(float3), typeof(float3), typeof(float3)));
|
||||
Assert.NotNull(ShaderFunction(shader, "cultmath_snoise_grad", typeof(float3)));
|
||||
Assert.NotNull(ShaderFunction(shader, "cultmath_snoise_hessian", typeof(float3)));
|
||||
|
|
@ -187,7 +188,7 @@ public sealed class HlslSourceCompatibilityTests
|
|||
}
|
||||
|
||||
Assert.True(mismatches.Count == 0, $"{mismatches.Count} mismatches:{Environment.NewLine}{string.Join(Environment.NewLine, mismatches.Take(12))}");
|
||||
Assert.Equal(135, compared.Count);
|
||||
Assert.Equal(136, compared.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -95,10 +95,11 @@ public sealed class SiteCellsTests
|
|||
Assert.Equal(1.0f, cells.secondSite);
|
||||
}
|
||||
|
||||
// TN2: F2 - F1 is affine for power scores, so the border distance is the distance to the bisector plane
|
||||
// {x : |x - s1|^2 - w1^2 = |x - s2|^2 - w2^2}, computed here in double from the two winning sites.
|
||||
// TN2: for power scores F2 - F1 is affine, so site_cells_border is the distance to the bisector plane of the
|
||||
// two winners {x : |x - s1|^2 - w1^2 = |x - s2|^2 - w2^2}, computed here in double. That is not the distance
|
||||
// to the nearest site's cell boundary (PowerBorderIsTheLeastRadicalPlaneDistance).
|
||||
[Fact]
|
||||
public void PowerBorderIsTheDistanceToThePowerBisector()
|
||||
public void SiteCellsBorderIsTheDistanceToTheWinnersBisector()
|
||||
{
|
||||
var r = new System.Random(0xB0DE);
|
||||
var compared = 0;
|
||||
|
|
@ -315,4 +316,166 @@ public sealed class SiteCellsTests
|
|||
Assert.Equal(expected, got, 1.0e-4 * (1.0 + expected));
|
||||
}
|
||||
}
|
||||
|
||||
// The cell of the owner is the intersection of the half-spaces of its radical planes. Reference in double,
|
||||
// written here: x.d = c with d = s2 - s1, c = (|s2|^2 - |s1|^2 + w1^2 - w2^2) / 2, signed distance (c - p.d) / |d|.
|
||||
private static (double Distance, double[] Normal) CellBoundary(float3 p, float4[] sites, int owner)
|
||||
{
|
||||
var best = double.PositiveInfinity;
|
||||
var normal = new double[3];
|
||||
var s1 = new[] { (double)sites[owner].x, sites[owner].y, sites[owner].z };
|
||||
var q = new[] { (double)p.x, p.y, p.z };
|
||||
for (var k = 0; k < sites.Length; k++)
|
||||
{
|
||||
var s2 = new[] { (double)sites[k].x, sites[k].y, sites[k].z };
|
||||
var d = new[] { s2[0] - s1[0], s2[1] - s1[1], s2[2] - s1[2] };
|
||||
var dd = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
|
||||
if (k == owner || dd == 0.0)
|
||||
continue;
|
||||
double w1 = sites[owner].w, w2 = sites[k].w;
|
||||
var c = (s2.Sum(v => v * v) - s1.Sum(v => v * v) + w1 * w1 - w2 * w2) / 2.0;
|
||||
var distance = (c - (q[0] * d[0] + q[1] * d[1] + q[2] * d[2])) / Math.Sqrt(dd);
|
||||
if (distance < best)
|
||||
{
|
||||
best = distance;
|
||||
normal = new[] { -d[0] / Math.Sqrt(dd), -d[1] / Math.Sqrt(dd), -d[2] / Math.Sqrt(dd) };
|
||||
}
|
||||
}
|
||||
|
||||
return (best, normal);
|
||||
}
|
||||
|
||||
// Spans with duplicated sites, zero weights, collinear sites and single-site spans.
|
||||
private static float4[] AwkwardSites(System.Random r)
|
||||
{
|
||||
var count = 1 + r.Next(9);
|
||||
var collinear = r.Next(5) == 0;
|
||||
var sites = new float4[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var weight = r.Next(5) == 0 ? 0.0f : 0.5f + 2.5f * r.NextSingle();
|
||||
sites[i] = i > 0 && r.Next(4) == 0 ? sites[r.Next(i)]
|
||||
: collinear ? new float4(Span(r, 10.0f), 0.0f, 0.0f, weight)
|
||||
: new float4(Span(r, 10.0f), Span(r, 10.0f), Span(r, 10.0f), weight);
|
||||
}
|
||||
|
||||
return sites;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PowerBorderIsTheLeastRadicalPlaneDistance()
|
||||
{
|
||||
var r = new System.Random(0xCE11);
|
||||
var finite = 0;
|
||||
var unbounded = 0;
|
||||
for (var n = 0; n < 6000; n++)
|
||||
{
|
||||
var sites = AwkwardSites(r);
|
||||
var p = Point(r);
|
||||
var reference = sites.Select(s => PowerReference(p, s)).ToArray();
|
||||
var order = Enumerable.Range(0, sites.Length).OrderBy(i => reference[i]).ThenBy(i => i).ToArray();
|
||||
if (order.Length > 1 && reference[order[1]] - reference[order[0]] < 1.0e-2 && reference[order[1]] != reference[order[0]])
|
||||
continue;
|
||||
var (distance, normal) = CellBoundary(p, sites, order[0]);
|
||||
var border = power_border(p, sites);
|
||||
if (double.IsPositiveInfinity(distance))
|
||||
{
|
||||
Assert.Equal(1.0e30f, border.w);
|
||||
unbounded++;
|
||||
continue;
|
||||
}
|
||||
|
||||
Assert.InRange(border.w, distance - 1.0e-4 * Math.Abs(distance) - 1.0e-4, distance + 1.0e-4 * Math.Abs(distance) + 1.0e-4);
|
||||
finite++;
|
||||
// The normal is the minimising plane's, pointing into the cell, when that plane is clear of the rest.
|
||||
var others = Enumerable.Range(0, sites.Length).Where(k => k != order[0]).Select(k => CellBoundary(p, new[] { sites[order[0]], sites[k] }, 0).Distance).Where(v => !double.IsPositiveInfinity(v)).OrderBy(v => v).ToArray();
|
||||
if (others.Length > 1 && others[1] - others[0] > 1.0e-2)
|
||||
{
|
||||
Assert.InRange(border.x, normal[0] - 1.0e-3, normal[0] + 1.0e-3);
|
||||
Assert.InRange(border.y, normal[1] - 1.0e-3, normal[1] + 1.0e-3);
|
||||
Assert.InRange(border.z, normal[2] - 1.0e-3, normal[2] + 1.0e-3);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(finite > 2500 && unbounded > 50, $"{finite} bounded and {unbounded} unbounded cells were compared");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ADuplicateOfTheOwnerLeavesTheCellBorderToTheOtherSites()
|
||||
{
|
||||
var a = new float4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
var far = new float4(10.0f, 0.0f, 0.0f, 1.0f);
|
||||
// p is nearer the owner's duplicate than anything else: the duplicate is the second-nearest site.
|
||||
var border = power_border(new float3(1.0f, 0.0f, 0.0f), new[] { a, a, far });
|
||||
Assert.Equal(4.0f, border.w, 1.0e-4f);
|
||||
Assert.Equal(-1.0f, border.x, 1.0e-5f);
|
||||
// Only coincident sites: the cell has no bounding plane.
|
||||
Assert.Equal(1.0e30f, power_border(new float3(1.0f, 0.0f, 0.0f), new[] { a, a }).w);
|
||||
Assert.Equal(1.0e30f, power_border(new float3(1.0f, 0.0f, 0.0f), new[] { a }).w);
|
||||
Assert.Equal(1.0e30f, power_border(new float3(1.0f, 0.0f, 0.0f), ReadOnlySpan<float4>.Empty).w);
|
||||
}
|
||||
|
||||
// Close sites seen from a far query: the plane distance must not cancel (the old gradient-difference form
|
||||
// had 1.5e-4 relative error at this geometry and 3.5e-3 at coordinates near 1e4).
|
||||
[Theory]
|
||||
[InlineData(0.0f)]
|
||||
[InlineData(10000.0f)]
|
||||
public void PowerBorderKeepsItsPrecisionForCloseSitesAndAFarQuery(float shift)
|
||||
{
|
||||
var sites = new[] { new float4(shift - 1.453f, shift + 1.986f, 0.0f, 13.085f), new float4(shift - 1.539f, shift + 2.103f, 0.0f, 5.113f) };
|
||||
var p = new float3(shift - 154.2f, shift + 188.9f, 0.0f);
|
||||
var expected = CellBoundary(p, sites, (int)power_cells(p, sites).nearestSite).Distance;
|
||||
Assert.True(expected > 100.0);
|
||||
Assert.InRange(power_border(p, sites).w, expected * (1.0 - 1.0e-5), expected * (1.0 + 1.0e-5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CoincidentGabrielPairHasMarginMinusOne()
|
||||
{
|
||||
var a = new float3(1.0f, 2.0f, 3.0f);
|
||||
foreach (var other in new[] { a, new float3(0.0f, 0.0f, 0.0f), new float3(9.0f, -4.0f, 2.0f) })
|
||||
Assert.Equal(-1.0f, gabriel_margin(a, a, other));
|
||||
|
||||
var pair = new[] { new float4(a, 1.0f), new float4(a, 1.0f), new float4(5.0f, 5.0f, 5.0f, 1.0f) };
|
||||
Assert.Equal(0.0f, soft_gabriel_gate(pair, 0, 1, 0.15f));
|
||||
// smoothstep(-3, 3, -1) = 3t^2 - 2t^3 at t = 1/3.
|
||||
Assert.Equal(7.0 / 27.0, soft_gabriel_gate(pair, 0, 1, 3.0f), 1.0e-5);
|
||||
Assert.Equal(1.0f, soft_gabriel_gate(pair[..2], 0, 1, 0.15f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CellSpansRefuseNonFinitePositionsAndQueriesAndOverlongSpans()
|
||||
{
|
||||
var good = new float4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
var origin = new float3(0.0f, 0.0f, 0.0f);
|
||||
foreach (var bad in new[] { float.NaN, float.PositiveInfinity, float.NegativeInfinity })
|
||||
{
|
||||
var site = new[] { new float4(bad, 0.0f, 0.0f, 1.0f), good, new float4(2.0f, 0.0f, 0.0f, 1.0f) };
|
||||
Assert.Equal("sites", Assert.Throws<ArgumentException>(() => power_cells(origin, site)).ParamName);
|
||||
Assert.Equal("sites", Assert.Throws<ArgumentException>(() => multiplicative_cells(origin, site)).ParamName);
|
||||
Assert.Equal("sites", Assert.Throws<ArgumentException>(() => power_border(origin, site)).ParamName);
|
||||
Assert.Equal("sites", Assert.Throws<ArgumentException>(() => soft_gabriel_gate(site, 1, 2, 0.1f)).ParamName);
|
||||
var query = new float3(0.0f, bad, 0.0f);
|
||||
Assert.Equal("p", Assert.Throws<ArgumentException>(() => power_cells(query, new[] { good })).ParamName);
|
||||
Assert.Equal("p", Assert.Throws<ArgumentException>(() => multiplicative_cells(query, new[] { good })).ParamName);
|
||||
Assert.Equal("p", Assert.Throws<ArgumentException>(() => power_border(query, new[] { good })).ParamName);
|
||||
}
|
||||
|
||||
// Indices are floats, exact only to 2^24: a longer span is refused before any site is read.
|
||||
var one = new[] { good };
|
||||
Assert.Equal("sites", Assert.Throws<ArgumentException>(() => power_cells(origin, System.Runtime.InteropServices.MemoryMarshal.CreateReadOnlySpan(ref one[0], (1 << 24) + 1))).ParamName);
|
||||
Assert.Equal("sites", Assert.Throws<ArgumentException>(() => multiplicative_cells(origin, System.Runtime.InteropServices.MemoryMarshal.CreateReadOnlySpan(ref one[0], (1 << 24) + 1))).ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SegmentClosestIsTheClampedFootOfThePerpendicular()
|
||||
{
|
||||
var start = new float3(0.0f, 0.0f, 0.0f);
|
||||
var end = new float3(4.0f, 0.0f, 0.0f);
|
||||
Assert.Equal(new float3(2.0f, 0.0f, 0.0f), segment_closest(new float3(2.0f, 3.0f, 1.0f), start, end));
|
||||
Assert.Equal(end, segment_closest(new float3(9.0f, 3.0f, 1.0f), start, end));
|
||||
Assert.Equal(start, segment_closest(new float3(-9.0f, 3.0f, 1.0f), start, end));
|
||||
Assert.Equal(start, segment_closest(new float3(2.0f, 3.0f, 1.0f), start, start));
|
||||
Assert.Equal(new float2(4.0f, 0.0f), segment_closest(new float2(9.0f, 3.0f), new float2(0.0f, 0.0f), new float2(4.0f, 0.0f)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -605,6 +605,10 @@ CultCellular cultmath_cellular(float3 p)
|
|||
// CultSiteCells cells = cultmath_site_cells_empty();
|
||||
// for (int i = 0; i < count; i++)
|
||||
// cells = cultmath_site_cells_insert(cells, cultmath_power_score(p, sites[i]), i);
|
||||
// The exact power cell border is a second loop over the buffer, skipping the owner cells.nearestSite:
|
||||
// float4 border = float4(0.0, 0.0, 0.0, 1.0e30); int owner = (int)cells.nearestSite;
|
||||
// for (int k = 0; k < count; k++) if (k != owner) border = cultmath_power_border_step(p, sites[owner], sites[k], border);
|
||||
// Shader names are unique (one 3D form of segment distance; a 2D caller uses z = 0), so none joins the overload rename list.
|
||||
// Statement for statement with math.Sites.cs (power_score, multiplicative_score, site_cells_*, gabriel_margin).
|
||||
struct CultSiteCells
|
||||
{
|
||||
|
|
@ -666,6 +670,16 @@ float4 cultmath_site_cells_border(CultSiteCells cells)
|
|||
return float4(n / len, (cells.second.w - cells.nearest.w) / len);
|
||||
}
|
||||
|
||||
float4 cultmath_power_border_step(float3 p, float4 owner, float4 other, float4 border)
|
||||
{
|
||||
float3 d = other.xyz - owner.xyz;
|
||||
float len = length(d);
|
||||
if (len <= 0.0)
|
||||
return border;
|
||||
float plane = max((dot(owner.xyz - p, d) + 0.5 * (dot(d, d) + (owner.w - other.w) * (owner.w + other.w))) / len, 0.0);
|
||||
return plane < border.w ? float4(-d / len, plane) : border;
|
||||
}
|
||||
|
||||
float cultmath_gabriel_margin(float3 a, float3 b, float3 other)
|
||||
{
|
||||
float r = cultmath_distance(a, b) * 0.5;
|
||||
|
|
@ -674,17 +688,6 @@ float cultmath_gabriel_margin(float3 a, float3 b, float3 other)
|
|||
return (cultmath_distance(other, (a + b) * 0.5) - r) / r;
|
||||
}
|
||||
|
||||
float2 cultmath_segment_closest(float2 point_value, float2 start, float2 end)
|
||||
{
|
||||
float2 segment = end - start;
|
||||
float segmentLengthSq = cultmath_lengthsq(segment);
|
||||
if (segmentLengthSq <= 0.0)
|
||||
return start;
|
||||
|
||||
float t = cultmath_saturate(dot(point_value - start, segment) / segmentLengthSq);
|
||||
return start + segment * t;
|
||||
}
|
||||
|
||||
float3 cultmath_segment_closest(float3 point_value, float3 start, float3 end)
|
||||
{
|
||||
float3 segment = end - start;
|
||||
|
|
@ -696,11 +699,6 @@ float3 cultmath_segment_closest(float3 point_value, float3 start, float3 end)
|
|||
return start + segment * t;
|
||||
}
|
||||
|
||||
float cultmath_distance_to_segment(float2 point_value, float2 start, float2 end)
|
||||
{
|
||||
return cultmath_distance(point_value, cultmath_segment_closest(point_value, start, end));
|
||||
}
|
||||
|
||||
float cultmath_distance_to_segment(float3 point_value, float3 start, float3 end)
|
||||
{
|
||||
return cultmath_distance(point_value, cultmath_segment_closest(point_value, start, end));
|
||||
|
|
|
|||
Loading…
Reference in a new issue