Skip to content

Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.

C#

A source's frequency level and phase take decimals; the level is capped at 255

Alexander Graef4e68e303691b
diff --git a/CLAUDE.md b/CLAUDE.md
index 647adbb..deb8cba 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -96,7 +96,9 @@ FrameStore: native memory up to the budget, the rest in a temp file (%LOCALAPPDA
`Reset`, `SetEmitters`, `SetProbes`. Numerics are Jeroen's (dx = dt = 1, leapfrog, 9-point or 4th-order
Laplacian, soft sources, EMA intensity; a scene pixel with red 0 is a mirror, n = 1000, his V0.30 rule,
2026-10-18), verified CPU = GPU by the harness; seeds from `Pcg32` make CPU = GPU
- bit for bit on random gaps. The importer's fallbacks differ from the model's defaults on purpose.
+ bit for bit on random gaps. A source's frequency level and phase take decimals (2026-10-19, Jeroen's point 10); the
+ level is capped at 255, above which the grid cannot sample the wave; object sources carry their frequency as a float on
+ the GPU, the 256-entry table serves the image sources. The importer's fallbacks differ from the model's defaults on purpose.
- **Frames vs steps**: frame k is the state after step `k × VisualizeEvery`.
- **The engine choice is a user setting** (Simulation > Use the GPU, default on, CPU fallback logged).
- **Frame store**: budget = free memory minus 2 GB, capped by Simulation > RAM cache size (Auto or a share); the
diff --git a/WaveDudeNext.WPF/Converters/WavelengthConverter.cs b/WaveDudeNext.WPF/Converters/WavelengthConverter.cs
index f9d394f..1d62d4a 100644
--- a/WaveDudeNext.WPF/Converters/WavelengthConverter.cs
+++ b/WaveDudeNext.WPF/Converters/WavelengthConverter.cs
@@ -32,7 +32,7 @@ public sealed class WavelengthConverter : IMultiValueConverter
{
public object Convert(object?[] values, Type targetType, object? parameter, CultureInfo culture)
{
- if (values.Length < 4 || values[0] is not int level || values[1] is not double waveSpeed
+ if (values.Length < 4 || values[0] is not double level || values[1] is not double waveSpeed
|| values[2] is not double scale || values[3] is not bool fourthOrder)
{
return "\u2013";
diff --git a/WaveDudeNext.WPF/Strings/Strings.DE.xaml b/WaveDudeNext.WPF/Strings/Strings.DE.xaml
index f7dac9c..786fb96 100644
--- a/WaveDudeNext.WPF/Strings/Strings.DE.xaml
+++ b/WaveDudeNext.WPF/Strings/Strings.DE.xaml
@@ -434,7 +434,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="Label.Source.Y">Y</s:String>
<s:String x:Key="Hint.Source.Y">Y des Strahlers bzw. der Anordnungsmitte, in Leinwandpixeln.</s:String>
<s:String x:Key="Label.Source.Frequency">Frequenz</s:String>
- <s:String x:Key="Hint.Source.Frequency">Frequenz als Stufe von 1 bis 255, dieselbe Skala wie der Grünkanal der Bildquellen.</s:String>
+ <s:String x:Key="Hint.Source.Frequency">Frequenz als Stufe bis 255, Dezimalstellen erlaubt, dieselbe Skala wie der Grünkanal der Bildquellen. Über 255 kann das Gitter die Welle nicht mehr abtasten.</s:String>
<s:String x:Key="Label.Source.Amplitude">Amplitude</s:String>
<s:String x:Key="Hint.Source.Amplitude">Amplitude in Prozent, 0 bis 100.</s:String>
<s:String x:Key="Label.Source.Phase">Phase</s:String>
diff --git a/WaveDudeNext.WPF/Strings/Strings.EN.xaml b/WaveDudeNext.WPF/Strings/Strings.EN.xaml
index f83381a..75f3b1f 100644
--- a/WaveDudeNext.WPF/Strings/Strings.EN.xaml
+++ b/WaveDudeNext.WPF/Strings/Strings.EN.xaml
@@ -436,7 +436,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="Label.Source.Y">Y</s:String>
<s:String x:Key="Hint.Source.Y">Y of the emitter, or of the array centre, in canvas pixels.</s:String>
<s:String x:Key="Label.Source.Frequency">Frequency</s:String>
- <s:String x:Key="Hint.Source.Frequency">Frequency as a level from 1 to 255, the same scale as the green channel of image sources.</s:String>
+ <s:String x:Key="Hint.Source.Frequency">Frequency as a level up to 255, decimals allowed, the same scale as the green channel of image sources. Above 255 the grid cannot sample the wave.</s:String>
<s:String x:Key="Label.Source.Amplitude">Amplitude</s:String>
<s:String x:Key="Hint.Source.Amplitude">Amplitude in percent, 0 to 100.</s:String>
<s:String x:Key="Label.Source.Phase">Phase</s:String>
diff --git a/WaveDudeNext.WPF/Strings/Strings.FR.xaml b/WaveDudeNext.WPF/Strings/Strings.FR.xaml
index e48e90b..635e93c 100644
--- a/WaveDudeNext.WPF/Strings/Strings.FR.xaml
+++ b/WaveDudeNext.WPF/Strings/Strings.FR.xaml
@@ -434,7 +434,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="Label.Source.Y">Y</s:String>
<s:String x:Key="Hint.Source.Y">Y de l'émetteur, ou du centre du réseau, en pixels de toile.</s:String>
<s:String x:Key="Label.Source.Frequency">Fréquence</s:String>
- <s:String x:Key="Hint.Source.Frequency">Fréquence comme niveau de 1 à 255, la même échelle que le canal vert des sources de l'image.</s:String>
+ <s:String x:Key="Hint.Source.Frequency">Fréquence sous forme de niveau jusqu'à 255, décimales admises, la même échelle que le canal vert des sources d'image. Au-delà de 255, la grille ne peut plus échantillonner l'onde.</s:String>
<s:String x:Key="Label.Source.Amplitude">Amplitude</s:String>
<s:String x:Key="Hint.Source.Amplitude">Amplitude en pour cent, 0 à 100.</s:String>
<s:String x:Key="Label.Source.Phase">Phase</s:String>
diff --git a/WaveDudeNext.WPF/Strings/Strings.NL.xaml b/WaveDudeNext.WPF/Strings/Strings.NL.xaml
index 488eb2e..bd163d3 100644
--- a/WaveDudeNext.WPF/Strings/Strings.NL.xaml
+++ b/WaveDudeNext.WPF/Strings/Strings.NL.xaml
@@ -434,7 +434,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="Label.Source.Y">Y</s:String>
<s:String x:Key="Hint.Source.Y">Y van de emitter, of van het rastercentrum, in canvaspixels.</s:String>
<s:String x:Key="Label.Source.Frequency">Frequentie</s:String>
- <s:String x:Key="Hint.Source.Frequency">Frequentie als niveau van 1 tot 255, dezelfde schaal als het groene kanaal van afbeeldingsbronnen.</s:String>
+ <s:String x:Key="Hint.Source.Frequency">Frequentie als niveau tot 255, decimalen toegestaan, dezelfde schaal als het groene kanaal van beeldbronnen. Boven 255 kan het raster de golf niet meer bemonsteren.</s:String>
<s:String x:Key="Label.Source.Amplitude">Amplitude</s:String>
<s:String x:Key="Hint.Source.Amplitude">Amplitude in procent, 0 tot 100.</s:String>
<s:String x:Key="Label.Source.Phase">Fase</s:String>
diff --git a/WaveDudeNext.WPF/Views/SourceView.xaml b/WaveDudeNext.WPF/Views/SourceView.xaml
index 4084728..aea6e9c 100644
--- a/WaveDudeNext.WPF/Views/SourceView.xaml
+++ b/WaveDudeNext.WPF/Views/SourceView.xaml
@@ -69,7 +69,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<Label Style="{StaticResource FormSection}" Content="{DynamicResource SectionWave}" />
<controls:FormPanel>
<TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:Source.Frequency}" />
- <controls:NumberBox Text="{Binding Frequency}" Minimum="1" Maximum="255" ToolTip="{meta:Hint model:Source.Frequency}" />
+ <controls:NumberBox Text="{Binding Frequency}" Minimum="0.01" Maximum="255" ToolTip="{meta:Hint model:Source.Frequency}" />
<TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:Source.Amplitude}" />
<controls:NumberBox Text="{Binding Amplitude}" Minimum="0" Maximum="100" ToolTip="{meta:Hint model:Source.Amplitude}" />
diff --git a/WaveDudeNext/Engine/Emitter.cs b/WaveDudeNext/Engine/Emitter.cs
index f579a89..3a4dd48 100644
--- a/WaveDudeNext/Engine/Emitter.cs
+++ b/WaveDudeNext/Engine/Emitter.cs
@@ -53,7 +53,7 @@ public sealed record Emitter
/// Frequency as a source level 1..255, the same scale as the brightness of image-based sources. The
/// engines map it to an angular frequency of <c>Level / 255 × 0.5 × SourceFrequencyScale</c> radians per step.
/// </summary>
- public required int Level { get; init; }
+ public required double Level { get; init; }
/// <summary>Peak field amplitude 0..1 (the source's 0..100 % divided by 100).</summary>
public required double Amplitude { get; init; }
diff --git a/WaveDudeNext/Engine/GpuWaveSimulator.cs b/WaveDudeNext/Engine/GpuWaveSimulator.cs
index 216dbc6..38532f4 100644
--- a/WaveDudeNext/Engine/GpuWaveSimulator.cs
+++ b/WaveDudeNext/Engine/GpuWaveSimulator.cs
@@ -319,6 +319,7 @@ public sealed class GpuWaveSimulator : IWaveEngine, IDisposable
private void Build(SceneData scene, EngineOptions options)
{
double sourceFrequencyScale = options.SourceFrequencyScale;
+ _sourceFrequencyScale = sourceFrequencyScale;
float sourceAmplitude = (float)options.SourceAmplitude;
bool use4thOrderLaplacian = options.FourthOrderLaplacian;
double timeDecayFactor = options.TimeDecay;
@@ -436,22 +437,22 @@ public sealed class GpuWaveSimulator : IWaveEngine, IDisposable
/// canvas, one entry per element. Uploads one table per <see cref="Emitter"/> property (see the
/// <c>apply_object_sources</c> kernel comment for the layout) and binds the two
/// <c>apply_object_sources</c> kernels, all registered in <see cref="_emitters"/> so <see cref="SetEmitters"/>
- /// can replace them. The frequency table (<see cref="_frequencyBuffer"/>, indexed by level) is shared with the
- /// image sources since both use the same level-to-frequency scale. The cycle anchors start at 0 and every
+ /// can replace them. Each object source carries its angular frequency as a float, computed by the image sources'
+ /// level formula from its decimal level (2026-10-19; the 256-entry table serves the image sources only). The cycle anchors start at 0 and every
/// source gets a non-zero xorshift32 seed.
/// </summary>
private void BuildObjectSourceKernels(List<Emitter> objectSources)
{
int n = _objSourceCount;
- int[] index = new int[n], level = new int[n], mode = new int[n], pulseShape = new int[n];
+ int[] index = new int[n], mode = new int[n], pulseShape = new int[n];
int[] onWidth = new int[n], offWidth = new int[n], detachStep = new int[n];
int[] offWidthBase = new int[n], randomizeOffWidthAmount = new int[n], rngState = new int[n];
- float[] amplitude = new float[n], phase = new float[n], cycleAnchor = new float[n];
+ float[] omega = new float[n], amplitude = new float[n], phase = new float[n], cycleAnchor = new float[n];
for (int i = 0; i < n; i++)
{
Emitter source = objectSources[i];
index[i] = (source.Y + 1) * _stride + source.X + 1;
- level[i] = source.Level;
+ omega[i] = (float)(source.Level / 255.0 * 0.5 * _sourceFrequencyScale);
amplitude[i] = (float)source.Amplitude;
phase[i] = (float)source.PhaseRadians;
mode[i] = source.Mode == SourceMode.Pulsed ? 1 : 0;
@@ -469,7 +470,7 @@ public sealed class GpuWaveSimulator : IWaveEngine, IDisposable
// (per launch) and 12 the count (scalar), see the apply_object_sources signature.
IntPtr[] buffers = new IntPtr[17];
buffers[1] = CreateBuffer(index, _emitters);
- buffers[2] = CreateBuffer(level, _emitters);
+ buffers[2] = CreateBuffer(omega, _emitters);
buffers[3] = CreateBuffer(amplitude, _emitters);
buffers[4] = CreateBuffer(phase, _emitters);
buffers[5] = CreateBuffer(mode, _emitters);
@@ -602,7 +603,7 @@ public sealed class GpuWaveSimulator : IWaveEngine, IDisposable
// apply_object_sources (Sources tab): soft-blends the object sources into the field, one element per
// work item k < n, same math as WaveSimulator.ApplySources/ComputePulsePosition/PulseEnvelope on the CPU.
// All per-source tables are indexed by k:
- // idx, level, amp, phase cell index, frequency level (into freq), amplitude, phase (radians)
+ // idx, omega, amp, phase cell index, angular frequency (radians per step), amplitude, phase (radians)
// mode 0 = continuous (envelope always 1), 1 = pulsed (Gaussian or smoothed-square
// envelope, "off" preceding "on" within each onWidth/offWidth cycle of the
// element's own frequency)
@@ -621,7 +622,7 @@ public sealed class GpuWaveSimulator : IWaveEngine, IDisposable
// object-source buffers, offWidth/cycleAnchor/rngState are genuinely
// mutated by the GPU across steps
// The result is u[idx] = 0.9·u + 0.1·sin(phase + f·t)·amp·envelope·detachFade.
- source.AppendLine("__kernel void apply_object_sources(__global float* u, __global const int* idx, __global const int* level,");
+ source.AppendLine("__kernel void apply_object_sources(__global float* u, __global const int* idx, __global const float* omega,");
source.AppendLine(" __global const float* amp, __global const float* phase, __global const int* mode,");
source.AppendLine(" __global const int* pulseShape, __global const int* onWidth, __global int* offWidth,");
source.AppendLine(" __global const int* detachStep,");
@@ -643,7 +644,7 @@ public sealed class GpuWaveSimulator : IWaveEngine, IDisposable
source.AppendLine(" if (stepsSinceDetach >= " + ramp + ".0f) return;");
source.AppendLine(" if (stepsSinceDetach > 0.0f) detachFade = 1.0f - stepsSinceDetach / " + ramp + ".0f;");
source.AppendLine(" }");
- source.AppendLine(" float f = freq[level[k]];");
+ source.AppendLine(" float f = omega[k];");
source.AppendLine(" float envelope = 1.0f;");
source.AppendLine(" if (mode[k] != 0) {");
source.AppendLine(" int ow = offWidth[k];");
@@ -1434,6 +1435,9 @@ public sealed class GpuWaveSimulator : IWaveEngine, IDisposable
/// <summary>The emitters inside the canvas as last installed, so <see cref="Reset"/> can rebuild their on-card state.</summary>
private List<Emitter> _objSources = [];
+ /// <summary>The scale of the level-to-frequency mapping, for the object sources' decimal levels (the image sources use the table).</summary>
+ private double _sourceFrequencyScale;
+
/// <summary>Per level 0..255: whether an image source uses it (the object sources are added per <see cref="SetEmitters"/> call).</summary>
private readonly bool[] _levelUsed = new bool[256];
diff --git a/WaveDudeNext/Engine/WaveSimulator.cs b/WaveDudeNext/Engine/WaveSimulator.cs
index 0a8a555..00bab06 100644
--- a/WaveDudeNext/Engine/WaveSimulator.cs
+++ b/WaveDudeNext/Engine/WaveSimulator.cs
@@ -99,6 +99,7 @@ public sealed class WaveSimulator : IWaveEngine, IDisposable
public WaveSimulator(SceneData scene, EngineOptions options, IReadOnlyList<Emitter>? emitters = null)
{
double sourceFrequencyScale = options.SourceFrequencyScale;
+ _sourceFrequencyScale = sourceFrequencyScale;
bool trackIntensity = options.TrackIntensity;
bool use4thOrderLaplacian = options.FourthOrderLaplacian;
_sourceAmplitude = options.SourceAmplitude;
@@ -480,9 +481,9 @@ public sealed class WaveSimulator : IWaveEngine, IDisposable
/// </summary>
/// <param name="levelUsed">Per level 0..255: whether an image source uses it.</param>
/// <param name="objectSources">Object sources to consider as well.</param>
- internal static int LowestSourceLevel(bool[] levelUsed, IEnumerable<Emitter> objectSources)
+ internal static double LowestSourceLevel(bool[] levelUsed, IEnumerable<Emitter> objectSources)
{
- int minActiveLevel = 0;
+ double minActiveLevel = 0;
for (int level = 1; level <= 255; level++)
{
if (levelUsed[level])
@@ -508,7 +509,7 @@ public sealed class WaveSimulator : IWaveEngine, IDisposable
/// <see cref="Simulation.TimeDecay"/> counts steps at a nominal source level of
/// <see cref="NominalDecayLevel"/>. Shared with <see cref="GpuWaveSimulator"/>.
/// </summary>
- internal static float IntensityRetention(double timeDecayFactor, int lowestLevel)
+ internal static float IntensityRetention(double timeDecayFactor, double lowestLevel)
{
double tauSteps = Math.Max(1.0, timeDecayFactor * NominalDecayLevel / lowestLevel);
return (float)Math.Exp(-1.0 / tauSteps);
@@ -560,7 +561,7 @@ public sealed class WaveSimulator : IWaveEngine, IDisposable
detachFade = (float)(1.0 - stepsSinceDetach / Emitter.DetachRampSteps);
}
}
- double freq = _levelFrequency[source.Level];
+ double freq = LevelFrequency(source.Level);
double envelope = 1.0;
if (source.Mode != SourceMode.Continuous)
{
@@ -934,9 +935,15 @@ public sealed class WaveSimulator : IWaveEngine, IDisposable
/// <summary>Green level (1..255) of every image source, indexing <see cref="_levelFrequency"/>.</summary>
private readonly byte[] _sourceLevel;
- /// <summary>Angular frequency in radians per step for each level 0..255: level/255 × 0.5 × sourceFrequencyScale.</summary>
+ /// <summary>Angular frequency in radians per step for each image level 0..255: level/255 × 0.5 × sourceFrequencyScale.</summary>
private readonly double[] _levelFrequency = new double[256];
+ /// <summary>The scale of the level-to-frequency mapping, for the object sources' decimal levels.</summary>
+ private readonly double _sourceFrequencyScale;
+
+ /// <summary>Angular frequency in radians per step of a decimal <paramref name="level"/>: the table's formula, rounded to float as the GPU holds it, so CPU = GPU.</summary>
+ private double LevelFrequency(double level) => (float)(level / 255.0 * 0.5 * _sourceFrequencyScale);
+
/// <summary>Which levels have at least one image source; only those get a value computed per step.</summary>
private readonly bool[] _levelUsed = new bool[256];
diff --git a/WaveDudeNext/Model/LegacyProject.cs b/WaveDudeNext/Model/LegacyProject.cs
index b9fe78b..d5a6744 100644
--- a/WaveDudeNext/Model/LegacyProject.cs
+++ b/WaveDudeNext/Model/LegacyProject.cs
@@ -186,9 +186,9 @@ public static class LegacyProject
notes?.Add($"Source '{source.Name}' had no picked position in the original project; it ran at ({source.X}, {source.Y}) there and does here.");
}
source.DetachAfter = Bool(item, "detach_source", false) ? Int(item, "detach_after_steps", 1000) : Source.NeverDetach;
- source.Frequency = Int(item, "frequency", Source.DefaultLevel);
+ source.Frequency = Double(item, "frequency", Source.DefaultLevel);
source.Amplitude = Int(item, "amplitude", Source.DefaultAmplitude);
- source.Phase = Int(item, "phase_deg", 0);
+ source.Phase = Double(item, "phase_deg", 0.0);
source.Mode = Text(item, "mode", "continuous") == "pulsed" ? SourceMode.Pulsed : SourceMode.Continuous;
source.PulseShape = Text(item, "pulse_shape", "gaussian") == "smoothed_square" ? PulseShape.SmoothedSquare : PulseShape.Gaussian;
source.PulseWidth = Int(item, "pulse_width_cycles", Source.DefaultWidth);
diff --git a/WaveDudeNext/Model/Simulation.cs b/WaveDudeNext/Model/Simulation.cs
index 2e975fe..e6fd345 100644
--- a/WaveDudeNext/Model/Simulation.cs
+++ b/WaveDudeNext/Model/Simulation.cs
@@ -152,7 +152,7 @@ public sealed class Simulation : ObservableObject
public double WavelengthOf(int level) => Wavelength(level, WaveSpeed, SourceFrequencyScale);
/// <summary>The formula behind <see cref="WavelengthOf"/> for callers that bind the three inputs separately.</summary>
- public static double Wavelength(int level, double waveSpeed, double sourceFrequencyScale)
+ public static double Wavelength(double level, double waveSpeed, double sourceFrequencyScale)
{
double cyclesPerStep = level / 255.0 * 0.5 * sourceFrequencyScale / (2.0 * Math.PI);
return cyclesPerStep > 0 ? waveSpeed / cyclesPerStep : double.NaN;
diff --git a/WaveDudeNext/Model/Source.cs b/WaveDudeNext/Model/Source.cs
index 065e9d5..75cf2e7 100644
--- a/WaveDudeNext/Model/Source.cs
+++ b/WaveDudeNext/Model/Source.cs
@@ -41,17 +41,18 @@ public enum PulseShape
/// <summary>
/// One wave source placed on the canvas: a single emitter, or a composite array of emitters
/// when <see cref="Array"/> is set. Units are those of the editor: positions in canvas pixels,
-/// frequency as a 1..255 level, amplitude in percent, phase in degrees, widths in cycles of the
-/// source frequency, detach time in simulation steps.
+/// frequency as a level up to 255 with decimals (2026-10-19, Jeroen's point 10: the whole-number levels were a
+/// remnant of the 8-bit image channel; above 255 the grid cannot sample the wave), amplitude in percent, phase in
+/// degrees with decimals, widths in cycles of the source frequency, detach time in simulation steps.
/// </summary>
public sealed class Source : ObservableObject
{
- /// <summary>Smallest frequency level.</summary>
- public const int MinLevel = 1;
- /// <summary>Largest frequency level.</summary>
- public const int MaxLevel = 255;
+ /// <summary>Smallest frequency level: just above 0, a wave of any length.</summary>
+ public const double MinLevel = 0.01;
+ /// <summary>Largest frequency level: the green channel's 255, above which the grid cannot sample the wave.</summary>
+ public const double MaxLevel = 255.0;
/// <summary>Default <see cref="Frequency"/>.</summary>
- public const int DefaultLevel = 100;
+ public const double DefaultLevel = 100.0;
/// <summary>Default <see cref="Amplitude"/> in percent.</summary>
public const int DefaultAmplitude = 100;
/// <summary>Default <see cref="PulseWidth"/> and <see cref="OffWidth"/> in cycles.</summary>
@@ -63,9 +64,9 @@ public sealed class Source : ObservableObject
private bool _enabled = true;
private int _x;
private int _y;
- private int _frequency = DefaultLevel;
+ private double _frequency = DefaultLevel;
private int _amplitude = DefaultAmplitude;
- private int _phase;
+ private double _phase;
private SourceMode _mode = SourceMode.Continuous;
private PulseShape _pulseShape = PulseShape.Gaussian;
private int _pulseWidth = DefaultWidth;
@@ -114,12 +115,12 @@ public sealed class Source : ObservableObject
set => SetProperty(ref _y, value);
}
- /// <summary>Frequency as a level 1..255, the same scale as the green channel of image sources (ω = level / 255 × 0.5 × <see cref="Simulation.SourceFrequencyScale"/>).</summary>
+ /// <summary>Frequency as a level up to 255, decimals allowed, the same scale as the green channel of image sources (ω = level / 255 × 0.5 × <see cref="Simulation.SourceFrequencyScale"/>).</summary>
[XmlAttribute]
[DefaultValue(DefaultLevel)]
[Category("Wave")]
- [Description("Frequency as a level from 1 to 255, the same scale as the green channel of image sources.")]
- public int Frequency
+ [Description("Frequency as a level up to 255, decimals allowed, the same scale as the green channel of image sources. Above 255 the grid cannot sample the wave.")]
+ public double Frequency
{
get => _frequency;
set => SetProperty(ref _frequency, value);
@@ -136,12 +137,12 @@ public sealed class Source : ObservableObject
set => SetProperty(ref _amplitude, value);
}
- /// <summary>Phase offset in degrees, -180 to 180.</summary>
+ /// <summary>Phase offset in degrees, -180 to 180, decimals allowed.</summary>
[XmlAttribute]
- [DefaultValue(0)]
+ [DefaultValue(0.0)]
[Category("Wave")]
[Description("Phase offset in degrees, -180 to 180.")]
- public int Phase
+ public double Phase
{
get => _phase;
set => SetProperty(ref _phase, value);
@@ -265,9 +266,9 @@ public sealed class Source : ObservableObject
public void Normalize()
{
Name ??= "";
- Frequency = Math.Clamp(Frequency, MinLevel, MaxLevel);
+ Frequency = double.IsFinite(Frequency) ? Math.Clamp(Frequency, MinLevel, MaxLevel) : DefaultLevel;
Amplitude = Math.Clamp(Amplitude, 0, 100);
- Phase = Math.Clamp(Phase, -180, 180);
+ Phase = double.IsFinite(Phase) ? Math.Clamp(Phase, -180.0, 180.0) : 0.0;
PulseWidth = Math.Max(1, PulseWidth);
OffWidth = Math.Max(1, OffWidth);
if (DetachAfter < 0)
@@ -312,7 +313,7 @@ public sealed class Source : ObservableObject
X = x + Jitter(rng, array.RandomPosition),
Y = y + Jitter(rng, array.RandomPosition),
Level = Math.Clamp(Frequency + Jitter(rng, array.RandomFrequency), MinLevel, MaxLevel),
- Phase = Math.Clamp(Phase + Jitter(rng, array.RandomPhase), -180, 180),
+ Phase = Math.Clamp(Phase + Jitter(rng, array.RandomPhase), -180.0, 180.0),
OffWidth = Math.Max(1, OffWidth + Jitter(rng, array.RandomOffWidth)),
});
}
@@ -505,11 +506,11 @@ public sealed class SourceElement
/// <summary>Y in canvas pixels.</summary>
public int Y { get; init; }
- /// <summary>Frequency level 1..255.</summary>
- public int Level { get; init; } = Source.DefaultLevel;
+ /// <summary>Frequency level, up to 255, decimals allowed.</summary>
+ public double Level { get; init; } = Source.DefaultLevel;
- /// <summary>Phase offset in degrees.</summary>
- public int Phase { get; init; }
+ /// <summary>Phase offset in degrees, decimals allowed.</summary>
+ public double Phase { get; init; }
/// <summary>Gap between bursts in cycles (pulsed mode).</summary>
public int OffWidth { get; init; } = Source.DefaultWidth;
diff --git a/_tools/library_check/Program.cs b/_tools/library_check/Program.cs
index ab0bf71..8354ad2 100644
--- a/_tools/library_check/Program.cs
+++ b/_tools/library_check/Program.cs
@@ -255,6 +255,7 @@ internal static class Program
CheckDeterminism(scene);
CheckEmitterMapping();
CheckMirror(W, H);
+ CheckDecimalLevels(W, H);
CheckFrameStore(scene);
CheckPreviewQuality(scene, W, H);
CheckRenderer();
@@ -625,6 +626,55 @@ internal static class Program
}
}
+ private static void CheckDecimalLevels(int W, int H)
+ {
+ Console.WriteLine("--- decimal frequency and phase");
+ var project = new Project();
+ project.Scene.Width = W;
+ project.Scene.Height = H;
+ Source source = project.AddSource();
+ source.X = 20;
+ source.Y = 24;
+ source.Frequency = 120.5;
+ source.Phase = 12.5;
+ RunSpec spec = RunSpec.Create(project, null, preferGpu: false);
+ Check(spec.Emitters.Length == 1 && spec.Emitters[0].Level == 120.5 && Math.Abs(spec.Emitters[0].PhaseRadians - 12.5 * Math.PI / 180.0) < 1e-12, "a decimal frequency and phase reach the emitter as typed");
+ source.Frequency = 300;
+ source.Phase = -200;
+ source.Normalize();
+ Check(source.Frequency == 255 && source.Phase == -180, "the level is capped at 255 (the grid cannot sample above it), the phase at -180");
+ source.Frequency = 0;
+ source.Normalize();
+ Check(source.Frequency == Source.MinLevel, "a level of 0 becomes the least level");
+ source.Frequency = 120.5;
+ source.Phase = 12.5;
+ using (var stream = new MemoryStream())
+ {
+ project.Save(stream);
+ string xml = System.Text.Encoding.UTF8.GetString(stream.ToArray());
+ stream.Position = 0;
+ Project back = Project.Load(stream);
+ Check(xml.Contains("Frequency=\"120.5\"") && xml.Contains("Phase=\"12.5\"") && back.Sources[0].Frequency == 120.5 && back.Sources[0].Phase == 12.5, "decimals round-trip through the file");
+ }
+ SceneData scene = SceneData.Empty(W, H, 0);
+ var emitters = new List<Emitter> { Make(20, 24, 120, 1.0) with { Level = 120.5, PhaseRadians = 12.5 * Math.PI / 180.0 } };
+ float[] cpu = RunCpu(scene, emitters, 150);
+ float[] whole = RunCpu(scene, [Make(20, 24, 120, 1.0)], 150);
+ Check(MaxAbs(cpu) > 0.01f && MaxDiff(cpu, whole) > 1e-3f, "a level of 120.5 runs and differs from 120");
+ GpuWaveSimulator? gpu = GpuWaveSimulator.TryCreate(scene, new EngineOptions(1.0, 1.0, false, false, 10.0, 1.0), emitters, out string problems);
+ if (gpu == null)
+ {
+ Console.WriteLine(" skip GPU: " + problems);
+ return;
+ }
+ using (gpu)
+ {
+ Run(gpu, 150);
+ float diff = MaxDiff(cpu, gpu.FieldData);
+ Check(diff < 1e-3f * Math.Max(1f, MaxAbs(cpu)), "GPU agrees on a decimal level and phase (max diff " + diff + ")");
+ }
+ }
+
/// <summary>Max |u| over the canvas columns <paramref name="x0"/>..<paramref name="x1"/> of a padded field.</summary>
private static float MaxAbsColumns(float[] padded, int W, int H, int x0, int x1)
{