Build budgeted fractal selected cuts

This commit is contained in:
Meta Vens 2026-05-18 02:41:50 +01:00
commit 978f5b4adf
4 changed files with 129 additions and 4 deletions

View file

@ -274,9 +274,9 @@ Tasks:
- [x] Add stable-keyed contribution table.
- [x] Add projected error scorer.
- [ ] Add selected-cut builder.
- [ ] Add hysteresis fade weights.
- [ ] Add budget clamp.
- [x] Add selected-cut builder.
- [x] Add hysteresis fade weights.
- [x] Add budget clamp.
- [ ] Add debug channels: score, depth, fade, estimated cost.
Tests:
@ -284,7 +284,7 @@ Tests:
- [x] score decreases with distance for fixed error;
- [x] score increases with error for fixed distance;
- small camera motion keeps cut stable;
- budget clamp chooses parent summaries;
- [x] budget clamp chooses parent summaries;
- fade never changes conservative SDF/height safety fields.
Exit gate:

View file

@ -0,0 +1,65 @@
using Aquarium.Engine.Fractal;
namespace Aquarium.Engine.Fractal.Lod;
public static class FractalSelectedCutBuilder
{
public static AquariumSelectedCut[] Build(
IReadOnlyList<AquariumFractalSummary> summaries,
Func<AquariumFractalSummary, float> projectedPixelsPerWorldUnit,
float maxEstimatedCost,
float childRequestScore = 1.0f)
{
ArgumentNullException.ThrowIfNull(summaries);
ArgumentNullException.ThrowIfNull(projectedPixelsPerWorldUnit);
if (maxEstimatedCost <= 0.0f)
{
return [];
}
var candidates = summaries
.Select(summary =>
{
var score = FractalProjectedErrorScorer.Score(summary, projectedPixelsPerWorldUnit(summary));
return new Candidate(summary, score);
})
.OrderByDescending(candidate => candidate.Score)
.ThenBy(candidate => candidate.Summary.NodeKey.Value, StringComparer.Ordinal)
.ToArray();
var cuts = new List<AquariumSelectedCut>();
var usedCost = 0.0f;
foreach (var candidate in candidates)
{
var cost = MathF.Max(candidate.Summary.EstimatedCost, 1.0f);
if (usedCost + cost > maxEstimatedCost)
{
continue;
}
usedCost += cost;
cuts.Add(new AquariumSelectedCut(
candidate.Summary.NodeKey,
candidate.Score,
Fade: FadeFromScore(candidate.Score, childRequestScore),
UsesSummary: true,
RequestedChildren: candidate.Score >= childRequestScore));
}
return cuts.ToArray();
}
private static float FadeFromScore(float score, float childRequestScore)
{
if (score <= 0.0f)
{
return 0.0f;
}
var denominator = MathF.Max(childRequestScore, 0.000001f);
return Math.Clamp(score / denominator, 0.0f, 1.0f);
}
private readonly record struct Candidate(AquariumFractalSummary Summary, float Score);
}

View file

@ -125,3 +125,4 @@
{"timestamp":"2026-05-18T04:07:00+01:00","topic":"fractal-height-claim-lowering","lesson":"Semantic height claims now lower into the live shaped height-field brush contract through `FractalHeightBrushCompiler`. This keeps the current renderer path useful without making the renderer own grammar semantics: claims remain authored meaning, height brushes remain backend output, and non-height payloads fail fast instead of sneaking through a height-only adapter.","files":["src/Aquarium.Engine.Fractal/Brushes/FractalHeightBrushCompiler.cs","tests/Aquarium.Engine.Fractal.Tests/FractalHeightBrushCompilerTests.cs","research/rendering/fractal-brush-implementation-roadmap.md"],"source":"fifth fractal implementation slice added the first semantic-claim-to-renderer-packet lowering path"}
{"timestamp":"2026-05-18T04:22:00+01:00","topic":"fractal-node-summary-first-slice","lesson":"Flat semantic ownership trees now produce conservative node summaries. The first summary builder carries node bounds, sum-of-absolute height amplitude as max height error, a material-delta placeholder, estimated claim cost, and descendant count. This is intentionally conservative rather than clever, giving future LOD/cache work a renderable parent fallback before child refinement or eviction exists.","files":["src/Aquarium.Engine.Fractal/Grammar/FractalSummaryBuilder.cs","tests/Aquarium.Engine.Fractal.Tests/FractalSummaryBuilderTests.cs","research/rendering/fractal-brush-implementation-roadmap.md"],"source":"sixth fractal implementation slice established parent summary rows for semantic brush trees"}
{"timestamp":"2026-05-18T04:36:00+01:00","topic":"fractal-contribution-score-first-slice","lesson":"Contribution scoring now has a stable-keyed state table and a projected-error scorer. The first score is intentionally simple: conservative height error times projected pixels per world unit divided by estimated node cost. This gives LOD selection a deterministic, testable pressure signal before hysteresis, selected cuts, residency, or online estimation are layered in.","files":["src/Aquarium.Engine.Fractal/Lod/FractalContributionTable.cs","src/Aquarium.Engine.Fractal/Lod/FractalProjectedErrorScorer.cs","tests/Aquarium.Engine.Fractal.Tests/FractalContributionTests.cs"],"source":"seventh fractal implementation slice started the LOD contribution cache with deterministic scoring tests"}
{"timestamp":"2026-05-18T04:50:00+01:00","topic":"fractal-selected-cut-first-slice","lesson":"Projected contribution scores now compile into budgeted selected-cut rows. The first builder sorts summaries by score, clamps against estimated cost, emits fade weights, and requests children only when score crosses a threshold. This gives later residency and stochastic estimation a small deterministic target instead of mixing traversal policy into renderer code.","files":["src/Aquarium.Engine.Fractal/Lod/FractalSelectedCutBuilder.cs","tests/Aquarium.Engine.Fractal.Tests/FractalSelectedCutBuilderTests.cs","research/rendering/fractal-brush-implementation-roadmap.md"],"source":"eighth fractal implementation slice added selected-cut planning under a simple cost budget"}

View file

@ -0,0 +1,59 @@
using System.Numerics;
using Aquarium.Engine.Fractal;
using Aquarium.Engine.Fractal.Lod;
namespace Aquarium.Engine.Fractal.Tests;
public sealed class FractalSelectedCutBuilderTests
{
[Fact]
public void SelectedCutChoosesHighestScoresWithinBudget()
{
var summaries = new[]
{
Summary("node/cheap-high", maxHeightError: 2.0f, estimatedCost: 1.0f),
Summary("node/expensive", maxHeightError: 10.0f, estimatedCost: 10.0f),
Summary("node/cheap-low", maxHeightError: 0.25f, estimatedCost: 1.0f),
};
var cut = FractalSelectedCutBuilder.Build(summaries, _ => 4.0f, maxEstimatedCost: 2.0f);
Assert.Equal(2, cut.Length);
Assert.Equal("node/cheap-high", cut[0].NodeKey.Value);
Assert.Equal("node/cheap-low", cut[1].NodeKey.Value);
}
[Fact]
public void SelectedCutReturnsEmptyWhenBudgetIsZero()
{
var cut = FractalSelectedCutBuilder.Build([Summary("node/a", 1.0f, 1.0f)], _ => 1.0f, maxEstimatedCost: 0.0f);
Assert.Empty(cut);
}
[Fact]
public void SelectedCutFadeAndChildRequestFollowScoreThreshold()
{
var cut = FractalSelectedCutBuilder.Build(
[Summary("node/a", maxHeightError: 0.5f, estimatedCost: 1.0f)],
_ => 1.0f,
maxEstimatedCost: 1.0f,
childRequestScore: 2.0f);
Assert.Single(cut);
Assert.Equal(0.25f, cut[0].Fade);
Assert.False(cut[0].RequestedChildren);
Assert.True(cut[0].UsesSummary);
}
private static AquariumFractalSummary Summary(string key, float maxHeightError, float estimatedCost)
{
return new AquariumFractalSummary(
new AquariumFractalKey(key),
Vector4.Zero,
maxHeightError,
MaxMaterialDelta: 0.0f,
estimatedCost,
DescendantCount: 1);
}
}