Extract temporal spatial evidence reservoir
This commit is contained in:
parent
5b4c13efab
commit
7fbff66f7b
9 changed files with 391 additions and 123 deletions
|
|
@ -5,7 +5,7 @@ namespace Aquarium.Engine.Fractal.Temporal;
|
|||
|
||||
public sealed class TemporalGaussianAccumulator
|
||||
{
|
||||
private readonly Dictionary<string, TemporalGaussianTrack> tracks = [];
|
||||
private readonly TemporalSpatialEvidenceReservoir reservoir;
|
||||
|
||||
public TemporalGaussianAccumulator(
|
||||
float accumulationWindowSeconds,
|
||||
|
|
@ -27,145 +27,62 @@ public sealed class TemporalGaussianAccumulator
|
|||
throw new ArgumentOutOfRangeException(nameof(smoothing), smoothing, "Smoothing must be in (0, 1].");
|
||||
}
|
||||
|
||||
AccumulationWindowSeconds = accumulationWindowSeconds;
|
||||
PresentationDelaySeconds = presentationDelaySeconds;
|
||||
Smoothing = smoothing;
|
||||
reservoir = new TemporalSpatialEvidenceReservoir(accumulationWindowSeconds, presentationDelaySeconds, smoothing);
|
||||
}
|
||||
|
||||
public float AccumulationWindowSeconds { get; }
|
||||
public float AccumulationWindowSeconds => reservoir.AccumulationWindowSeconds;
|
||||
|
||||
public float PresentationDelaySeconds { get; }
|
||||
public float PresentationDelaySeconds => reservoir.PresentationDelaySeconds;
|
||||
|
||||
public float Smoothing { get; }
|
||||
public float Smoothing => reservoir.Smoothing;
|
||||
|
||||
public void Observe(IEnumerable<TemporalGaussianObservation> observations)
|
||||
{
|
||||
foreach (var observation in observations)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(observation.StableKey))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tracks.TryGetValue(observation.StableKey, out var current))
|
||||
{
|
||||
tracks[observation.StableKey] = current.Update(observation, Smoothing);
|
||||
}
|
||||
else
|
||||
{
|
||||
tracks[observation.StableKey] = TemporalGaussianTrack.Create(observation);
|
||||
}
|
||||
}
|
||||
reservoir.Observe(observations.Select(observation => new TemporalSpatialEvidenceObservation(
|
||||
observation.StableKey,
|
||||
observation.Center,
|
||||
observation.Radii,
|
||||
observation.Orientation,
|
||||
observation.ColorOpacity,
|
||||
new Vector4(
|
||||
MathF.Max(0.0001f, observation.Falloff),
|
||||
MathF.Max(0.0001f, observation.ShapePower),
|
||||
0.0f,
|
||||
0.0f),
|
||||
observation.Confidence,
|
||||
observation.ObservedTimeSeconds,
|
||||
observation.FieldId)));
|
||||
}
|
||||
|
||||
public AquariumTemporalGaussianField BuildField(float renderTimeSeconds)
|
||||
{
|
||||
var presentationTime = renderTimeSeconds - PresentationDelaySeconds;
|
||||
var cutoff = presentationTime - AccumulationWindowSeconds;
|
||||
var gaussians = new List<AquariumTemporalSdfGaussian>(tracks.Count);
|
||||
var expired = new List<string>();
|
||||
var snapshot = reservoir.BuildSnapshot(renderTimeSeconds);
|
||||
var gaussians = new AquariumTemporalSdfGaussian[snapshot.Samples.Count];
|
||||
|
||||
foreach (var (key, track) in tracks)
|
||||
for (var index = 0; index < snapshot.Samples.Count; index++)
|
||||
{
|
||||
if (track.LastObservedTimeSeconds < cutoff)
|
||||
{
|
||||
expired.Add(key);
|
||||
continue;
|
||||
}
|
||||
|
||||
var age = MathF.Max(0.0f, presentationTime - track.LastObservedTimeSeconds);
|
||||
var historyWeight = Math.Clamp(1.0f - (age / AccumulationWindowSeconds), 0.0f, 1.0f) * track.Confidence;
|
||||
var predictedCenter = track.Center + track.Velocity * (presentationTime - track.LastObservedTimeSeconds);
|
||||
var previousCenter = predictedCenter - track.Velocity * (1.0f / 60.0f);
|
||||
gaussians.Add(new AquariumTemporalSdfGaussian(
|
||||
track.StableKey,
|
||||
predictedCenter,
|
||||
previousCenter,
|
||||
track.Velocity,
|
||||
track.Radii,
|
||||
track.Orientation,
|
||||
track.ColorOpacity,
|
||||
track.Confidence,
|
||||
historyWeight,
|
||||
track.LastObservedTimeSeconds,
|
||||
track.Falloff,
|
||||
track.ShapePower,
|
||||
track.FieldId));
|
||||
}
|
||||
|
||||
foreach (var key in expired)
|
||||
{
|
||||
tracks.Remove(key);
|
||||
var sample = snapshot.Samples[index];
|
||||
gaussians[index] = new AquariumTemporalSdfGaussian(
|
||||
sample.StableKey,
|
||||
sample.Center,
|
||||
sample.PreviousCenter,
|
||||
sample.Velocity,
|
||||
sample.Radii,
|
||||
sample.Orientation,
|
||||
sample.Payload0,
|
||||
sample.Confidence,
|
||||
sample.HistoryWeight,
|
||||
sample.LastObservedTimeSeconds,
|
||||
sample.Payload1.X,
|
||||
sample.Payload1.Y,
|
||||
sample.FieldId);
|
||||
}
|
||||
|
||||
return new AquariumTemporalGaussianField
|
||||
{
|
||||
Gaussians = gaussians.OrderByDescending(item => item.HistoryWeight).ToArray(),
|
||||
Gaussians = gaussians,
|
||||
AccumulationWindowSeconds = AccumulationWindowSeconds,
|
||||
PresentationDelaySeconds = PresentationDelaySeconds,
|
||||
};
|
||||
}
|
||||
|
||||
private readonly record struct TemporalGaussianTrack(
|
||||
string StableKey,
|
||||
Vector3 Center,
|
||||
Vector3 Velocity,
|
||||
Vector3 Radii,
|
||||
Quaternion Orientation,
|
||||
Vector4 ColorOpacity,
|
||||
float Confidence,
|
||||
float LastObservedTimeSeconds,
|
||||
float Falloff,
|
||||
float ShapePower,
|
||||
int FieldId)
|
||||
{
|
||||
public static TemporalGaussianTrack Create(TemporalGaussianObservation observation)
|
||||
{
|
||||
return new TemporalGaussianTrack(
|
||||
observation.StableKey,
|
||||
observation.Center,
|
||||
Vector3.Zero,
|
||||
SanitizeRadii(observation.Radii),
|
||||
SanitizeOrientation(observation.Orientation),
|
||||
observation.ColorOpacity,
|
||||
Math.Clamp(observation.Confidence, 0.0f, 1.0f),
|
||||
observation.ObservedTimeSeconds,
|
||||
MathF.Max(0.0001f, observation.Falloff),
|
||||
MathF.Max(0.0001f, observation.ShapePower),
|
||||
observation.FieldId);
|
||||
}
|
||||
|
||||
public TemporalGaussianTrack Update(TemporalGaussianObservation observation, float smoothing)
|
||||
{
|
||||
var dt = MathF.Max(0.0001f, observation.ObservedTimeSeconds - LastObservedTimeSeconds);
|
||||
var measuredVelocity = (observation.Center - Center) / dt;
|
||||
var alpha = smoothing * Math.Clamp(observation.Confidence, 0.0f, 1.0f);
|
||||
var updatedCenter = Vector3.Lerp(Center, observation.Center, alpha);
|
||||
return this with
|
||||
{
|
||||
Center = updatedCenter,
|
||||
Velocity = Vector3.Lerp(Velocity, measuredVelocity, alpha),
|
||||
Radii = Vector3.Lerp(Radii, SanitizeRadii(observation.Radii), alpha),
|
||||
Orientation = SanitizeOrientation(Quaternion.Slerp(Orientation, SanitizeOrientation(observation.Orientation), alpha)),
|
||||
ColorOpacity = Vector4.Lerp(ColorOpacity, observation.ColorOpacity, alpha),
|
||||
Confidence = Math.Clamp((Confidence * 0.85f) + (observation.Confidence * 0.15f), 0.0f, 1.0f),
|
||||
LastObservedTimeSeconds = observation.ObservedTimeSeconds,
|
||||
Falloff = MathF.Max(0.0001f, observation.Falloff),
|
||||
ShapePower = MathF.Max(0.0001f, observation.ShapePower),
|
||||
FieldId = observation.FieldId,
|
||||
};
|
||||
}
|
||||
|
||||
private static Vector3 SanitizeRadii(Vector3 radii)
|
||||
{
|
||||
return Vector3.Max(radii, new Vector3(0.0001f));
|
||||
}
|
||||
|
||||
private static Quaternion SanitizeOrientation(Quaternion orientation)
|
||||
{
|
||||
return orientation.LengthSquared() <= 0.000001f
|
||||
? Quaternion.Identity
|
||||
: Quaternion.Normalize(orientation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
using System.Numerics;
|
||||
|
||||
namespace Aquarium.Engine.Fractal.Temporal;
|
||||
|
||||
public readonly record struct TemporalSpatialEvidenceObservation(
|
||||
string StableKey,
|
||||
Vector3 Center,
|
||||
Vector3 Radii,
|
||||
Quaternion Orientation,
|
||||
Vector4 Payload0,
|
||||
Vector4 Payload1,
|
||||
float Confidence,
|
||||
float ObservedTimeSeconds,
|
||||
int FieldId = 0);
|
||||
|
||||
public readonly record struct TemporalSpatialEvidenceSample(
|
||||
string StableKey,
|
||||
Vector3 Center,
|
||||
Vector3 PreviousCenter,
|
||||
Vector3 Velocity,
|
||||
Vector3 Radii,
|
||||
Quaternion Orientation,
|
||||
Vector4 Payload0,
|
||||
Vector4 Payload1,
|
||||
float Confidence,
|
||||
float HistoryWeight,
|
||||
float LastObservedTimeSeconds,
|
||||
int FieldId);
|
||||
|
||||
public sealed class TemporalSpatialEvidenceSnapshot
|
||||
{
|
||||
public TemporalSpatialEvidenceSnapshot(
|
||||
IReadOnlyList<TemporalSpatialEvidenceSample> samples,
|
||||
float accumulationWindowSeconds,
|
||||
float presentationDelaySeconds)
|
||||
{
|
||||
Samples = samples;
|
||||
AccumulationWindowSeconds = accumulationWindowSeconds;
|
||||
PresentationDelaySeconds = presentationDelaySeconds;
|
||||
}
|
||||
|
||||
public IReadOnlyList<TemporalSpatialEvidenceSample> Samples { get; }
|
||||
|
||||
public float AccumulationWindowSeconds { get; }
|
||||
|
||||
public float PresentationDelaySeconds { get; }
|
||||
}
|
||||
|
|
@ -0,0 +1,195 @@
|
|||
using System.Numerics;
|
||||
|
||||
namespace Aquarium.Engine.Fractal.Temporal;
|
||||
|
||||
public sealed class TemporalSpatialEvidenceReservoir
|
||||
{
|
||||
private readonly Dictionary<string, TemporalSpatialEvidenceTrack> tracks = [];
|
||||
|
||||
public TemporalSpatialEvidenceReservoir(
|
||||
float accumulationWindowSeconds,
|
||||
float presentationDelaySeconds,
|
||||
float smoothing = 0.35f,
|
||||
int maxTrackCount = int.MaxValue)
|
||||
{
|
||||
if (!float.IsFinite(accumulationWindowSeconds) || accumulationWindowSeconds <= 0.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(accumulationWindowSeconds), accumulationWindowSeconds, "Accumulation window must be positive.");
|
||||
}
|
||||
|
||||
if (!float.IsFinite(presentationDelaySeconds) || presentationDelaySeconds < 0.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(presentationDelaySeconds), presentationDelaySeconds, "Presentation delay must be non-negative.");
|
||||
}
|
||||
|
||||
if (!float.IsFinite(smoothing) || smoothing <= 0.0f || smoothing > 1.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(smoothing), smoothing, "Smoothing must be in (0, 1].");
|
||||
}
|
||||
|
||||
if (maxTrackCount <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(maxTrackCount), maxTrackCount, "Track budget must be positive.");
|
||||
}
|
||||
|
||||
AccumulationWindowSeconds = accumulationWindowSeconds;
|
||||
PresentationDelaySeconds = presentationDelaySeconds;
|
||||
Smoothing = smoothing;
|
||||
MaxTrackCount = maxTrackCount;
|
||||
}
|
||||
|
||||
public float AccumulationWindowSeconds { get; }
|
||||
|
||||
public float PresentationDelaySeconds { get; }
|
||||
|
||||
public float Smoothing { get; }
|
||||
|
||||
public int MaxTrackCount { get; }
|
||||
|
||||
public void Observe(IEnumerable<TemporalSpatialEvidenceObservation> observations)
|
||||
{
|
||||
foreach (var observation in observations)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(observation.StableKey))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tracks.TryGetValue(observation.StableKey, out var current))
|
||||
{
|
||||
tracks[observation.StableKey] = current.Update(observation, Smoothing);
|
||||
}
|
||||
else
|
||||
{
|
||||
tracks[observation.StableKey] = TemporalSpatialEvidenceTrack.Create(observation);
|
||||
}
|
||||
}
|
||||
|
||||
TrimToBudget();
|
||||
}
|
||||
|
||||
public TemporalSpatialEvidenceSnapshot BuildSnapshot(float renderTimeSeconds)
|
||||
{
|
||||
var presentationTime = renderTimeSeconds - PresentationDelaySeconds;
|
||||
var cutoff = presentationTime - AccumulationWindowSeconds;
|
||||
var samples = new List<TemporalSpatialEvidenceSample>(tracks.Count);
|
||||
var expired = new List<string>();
|
||||
|
||||
foreach (var (key, track) in tracks)
|
||||
{
|
||||
if (track.LastObservedTimeSeconds < cutoff)
|
||||
{
|
||||
expired.Add(key);
|
||||
continue;
|
||||
}
|
||||
|
||||
var age = MathF.Max(0.0f, presentationTime - track.LastObservedTimeSeconds);
|
||||
var historyWeight = Math.Clamp(1.0f - (age / AccumulationWindowSeconds), 0.0f, 1.0f) * track.Confidence;
|
||||
var predictedCenter = track.Center + track.Velocity * (presentationTime - track.LastObservedTimeSeconds);
|
||||
var previousCenter = predictedCenter - track.Velocity * (1.0f / 60.0f);
|
||||
samples.Add(new TemporalSpatialEvidenceSample(
|
||||
track.StableKey,
|
||||
predictedCenter,
|
||||
previousCenter,
|
||||
track.Velocity,
|
||||
track.Radii,
|
||||
track.Orientation,
|
||||
track.Payload0,
|
||||
track.Payload1,
|
||||
track.Confidence,
|
||||
historyWeight,
|
||||
track.LastObservedTimeSeconds,
|
||||
track.FieldId));
|
||||
}
|
||||
|
||||
foreach (var key in expired)
|
||||
{
|
||||
tracks.Remove(key);
|
||||
}
|
||||
|
||||
return new TemporalSpatialEvidenceSnapshot(
|
||||
samples.OrderByDescending(item => item.HistoryWeight).ToArray(),
|
||||
AccumulationWindowSeconds,
|
||||
PresentationDelaySeconds);
|
||||
}
|
||||
|
||||
private void TrimToBudget()
|
||||
{
|
||||
if (tracks.Count <= MaxTrackCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var evict = tracks.Values
|
||||
.OrderBy(track => track.Confidence)
|
||||
.ThenBy(track => track.LastObservedTimeSeconds)
|
||||
.Take(tracks.Count - MaxTrackCount)
|
||||
.Select(track => track.StableKey)
|
||||
.ToArray();
|
||||
|
||||
foreach (var key in evict)
|
||||
{
|
||||
tracks.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly record struct TemporalSpatialEvidenceTrack(
|
||||
string StableKey,
|
||||
Vector3 Center,
|
||||
Vector3 Velocity,
|
||||
Vector3 Radii,
|
||||
Quaternion Orientation,
|
||||
Vector4 Payload0,
|
||||
Vector4 Payload1,
|
||||
float Confidence,
|
||||
float LastObservedTimeSeconds,
|
||||
int FieldId)
|
||||
{
|
||||
public static TemporalSpatialEvidenceTrack Create(TemporalSpatialEvidenceObservation observation)
|
||||
{
|
||||
return new TemporalSpatialEvidenceTrack(
|
||||
observation.StableKey,
|
||||
observation.Center,
|
||||
Vector3.Zero,
|
||||
SanitizeRadii(observation.Radii),
|
||||
SanitizeOrientation(observation.Orientation),
|
||||
observation.Payload0,
|
||||
observation.Payload1,
|
||||
Math.Clamp(observation.Confidence, 0.0f, 1.0f),
|
||||
observation.ObservedTimeSeconds,
|
||||
observation.FieldId);
|
||||
}
|
||||
|
||||
public TemporalSpatialEvidenceTrack Update(TemporalSpatialEvidenceObservation observation, float smoothing)
|
||||
{
|
||||
var dt = MathF.Max(0.0001f, observation.ObservedTimeSeconds - LastObservedTimeSeconds);
|
||||
var measuredVelocity = (observation.Center - Center) / dt;
|
||||
var alpha = smoothing * Math.Clamp(observation.Confidence, 0.0f, 1.0f);
|
||||
var updatedCenter = Vector3.Lerp(Center, observation.Center, alpha);
|
||||
return this with
|
||||
{
|
||||
Center = updatedCenter,
|
||||
Velocity = Vector3.Lerp(Velocity, measuredVelocity, alpha),
|
||||
Radii = Vector3.Lerp(Radii, SanitizeRadii(observation.Radii), alpha),
|
||||
Orientation = SanitizeOrientation(Quaternion.Slerp(Orientation, SanitizeOrientation(observation.Orientation), alpha)),
|
||||
Payload0 = Vector4.Lerp(Payload0, observation.Payload0, alpha),
|
||||
Payload1 = Vector4.Lerp(Payload1, observation.Payload1, alpha),
|
||||
Confidence = Math.Clamp((Confidence * 0.85f) + (observation.Confidence * 0.15f), 0.0f, 1.0f),
|
||||
LastObservedTimeSeconds = observation.ObservedTimeSeconds,
|
||||
FieldId = observation.FieldId,
|
||||
};
|
||||
}
|
||||
|
||||
private static Vector3 SanitizeRadii(Vector3 radii)
|
||||
{
|
||||
return Vector3.Max(radii, new Vector3(0.0001f));
|
||||
}
|
||||
|
||||
private static Quaternion SanitizeOrientation(Quaternion orientation)
|
||||
{
|
||||
return orientation.LengthSquared() <= 0.000001f
|
||||
? Quaternion.Identity
|
||||
: Quaternion.Normalize(orientation);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue