From 978f5b4adf9c0dce3886cc772917a60679dec394 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 18 May 2026 02:41:50 +0100 Subject: [PATCH] Build budgeted fractal selected cuts --- .../fractal-brush-implementation-roadmap.md | 8 +-- .../Lod/FractalSelectedCutBuilder.cs | 65 +++++++++++++++++++ state/evidence.jsonl | 1 + .../FractalSelectedCutBuilderTests.cs | 59 +++++++++++++++++ 4 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 src/Aquarium.Engine.Fractal/Lod/FractalSelectedCutBuilder.cs create mode 100644 tests/Aquarium.Engine.Fractal.Tests/FractalSelectedCutBuilderTests.cs diff --git a/research/rendering/fractal-brush-implementation-roadmap.md b/research/rendering/fractal-brush-implementation-roadmap.md index ca4bb84..34a691d 100644 --- a/research/rendering/fractal-brush-implementation-roadmap.md +++ b/research/rendering/fractal-brush-implementation-roadmap.md @@ -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: diff --git a/src/Aquarium.Engine.Fractal/Lod/FractalSelectedCutBuilder.cs b/src/Aquarium.Engine.Fractal/Lod/FractalSelectedCutBuilder.cs new file mode 100644 index 0000000..48fa298 --- /dev/null +++ b/src/Aquarium.Engine.Fractal/Lod/FractalSelectedCutBuilder.cs @@ -0,0 +1,65 @@ +using Aquarium.Engine.Fractal; + +namespace Aquarium.Engine.Fractal.Lod; + +public static class FractalSelectedCutBuilder +{ + public static AquariumSelectedCut[] Build( + IReadOnlyList summaries, + Func 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(); + 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); +} diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 5d2ff00..3f38d1a 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -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"} diff --git a/tests/Aquarium.Engine.Fractal.Tests/FractalSelectedCutBuilderTests.cs b/tests/Aquarium.Engine.Fractal.Tests/FractalSelectedCutBuilderTests.cs new file mode 100644 index 0000000..a3faa61 --- /dev/null +++ b/tests/Aquarium.Engine.Fractal.Tests/FractalSelectedCutBuilderTests.cs @@ -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); + } +}