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#
The scene pixels are composed on a worker; the UI thread takes only the finished pictures
Alexander Graef8487a08773c1
diff --git a/CLAUDE.md b/CLAUDE.mdindex cbf15a3..721ff9b 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -448,12 +448,15 @@ Example 7 (2880×1080 in a 458×172 picture, both layers, playing): 16-bit 7.3single-threaded (9 ms a frame in a sequence job on half the cores), in-house 9.8 ms on ten cores (15 in a job);MP4 job 16 ms a frame. Part painter on a Full HD canvas (ten cores): a 400 × 330 outline 30 ms, a 400 px lens 5, a hexagon 8, an S-bent fibre across the canvas 51 (156 chords on the cell grid; 1.3 s without it);the glowing edges 22 ms; the preview's scene pictures about 100 ms more, so the scene rebuild is coalesced at background priority.-**Dragging a part is choppy because of that composition** (2026-11-03, the log of a drag in Example 16: every coalesced-update repaints all four rectangles, one the whole canvas, "composed in 115–270 ms" on the UI thread, then 20–30 ms of-pictures; the painter spends 35–60 ns a pixel on ten cores even on a plain rectangle); the remedies on the table are a-worker thread with a snapshot of the parts (`OpticalPart.Clone` copies the point and node lists), a faster painter-(profile first), or no repaint while the mouse button is held; not an incremental painter, since a coating's band rises-from the index beneath it. Laptop (2 cores, HD 520, HDD): iGPU 4.3 ms a step against CPU 14.7; the ball+**The composition runs on a worker since 2026-11-03** (the user's choice, after the log of a drag in Example 16: every+coalesced update repainted all four rectangles, one the whole canvas, "composed in 115–270 ms" on the UI thread, then+20–30 ms of pictures, and the handle jumped): `MainWindow.ComposeScene` clones the parts (`OpticalPart.Clone` copies the+point and node lists) and paints on the thread pool, one composition in flight, the newest request after it, the UI+thread taking only the finished pixels (20–24 ms a time, measured on a burst of ten nudges: three compositions of 395,+212 and 128 ms on the worker); `LoadSceneImage` (a run or scratch request) joins an in-flight composition of the same+inputs rather than painting again. Still open: the painter itself, 35–60 ns a pixel on ten cores even on a plain+rectangle (an interior fast path, a cheaper mirror test; profile first), which sets the picture's refresh rate during a+drag; not an incremental painter, since a coating's band rises from the index beneath it. Laptop (2 cores, HD 520, HDD): iGPU 4.3 ms a step against CPU 14.7; the balllens run 105 s full quality (disk-bound), 61 s at half resolution with every second frame, in RAM. Settled by thesenumbers: no GPU JPEG, no OpenCL colouring at presentation, no `Vector256`, no Direct3D display path.diff --git a/WaveDudeNext.WPF/MainWindow.Live.cs b/WaveDudeNext.WPF/MainWindow.Live.csindex 597c462..ae1fa42 100644--- a/WaveDudeNext.WPF/MainWindow.Live.cs+++ b/WaveDudeNext.WPF/MainWindow.Live.cs@@ -149,6 +149,8 @@ public partial class MainWindow_imageCache = null;_embeddedCache = null;_composedCache = null;+ // a new set of parts: a composition of the old project still on its worker must not land under the new one's key+ _partsVersion++;ForgetExportPaths();// the new project's own schemes and scene image; the pane does not exist yet when the constructor sets the first projectDispatcher.InvokeAsync(() =>@@ -462,9 +464,11 @@ public partial class MainWindow/// <summary>/// Hands the preview the scene pixels (null for a canvas without parts, or when the image cannot be read: that is- /// logged once the run reports it). Coalesced at background priority: a drag of a part fires a change per mouse- /// move, and composing the scene plus rebuilding its pictures is far too slow to do for each (2026-10-19); the- /// overlay draws the selected part's outline itself, so what the user drags stays live meanwhile.+ /// logged once the run reports it). Coalesced at background priority and composed on a worker (2026-11-03, the user's+ /// choice): a drag of a part fires a change per mouse move, and composing the scene took 115–270 ms of the UI thread+ /// for Example 16's four rectangles, so the handle jumped; now one composition is in flight at a time, the newest+ /// request follows it, and the UI thread only takes the finished pixels (20–30 ms for the pictures). The overlay+ /// draws the selected part's outline itself, so what the user drags stays live meanwhile./// </summary>private void UpdateScenePixels(){@@ -476,49 +480,108 @@ public partial class MainWindowDispatcher.InvokeAsync(() =>{_scenePixelsPending = false;- UpdateScenePixelsNow();+ ComposeScene();}, DispatcherPriority.Background);}private bool _scenePixelsPending;- private void UpdateScenePixelsNow()+ /// <summary>The composition on a worker with the key its result is cached under, or null when none is in flight. UI thread only.</summary>+ private (SceneKey Key, Task<ScenePixels?> Task)? _composition;++ /// <summary>Set by a request that arrives while a composition is in flight: another one follows it.</summary>+ private bool _composeAgain;++ /// <summary>+ /// Composes the scene on a worker from a snapshot of its inputs (the parts cloned: <see cref="OpticalPart.Clone"/>+ /// copies the point and node lists too, so the worker shares nothing with the model) and hands the preview the result+ /// on the UI thread, again while requests arrived meanwhile. A failure of the painter surfaces here like any other.+ /// </summary>+ private async void ComposeScene(){if (previewPane == null){return;}- try+ if (_composition != null){+ _composeAgain = true;+ return;+ }+ do+ {+ _composeAgain = false;+ ScenePixels? source;+ try+ {+ source = DecodeSceneImage();+ }+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or NotSupportedException or FileFormatException or RunSpecException)+ {+ Logger.Debug("Scene image not shown: {Message}", ex.Message);+ previewPane.ScenePixels = null;+ return;+ }+ Scene scene = Project.Scene;+ var key = new SceneKey(source, _partsVersion, scene.Width, scene.Height, scene.RefractiveIndex);long started = System.Diagnostics.Stopwatch.GetTimestamp();- ScenePixels? pixels = LoadSceneImage();+ if (!TryCachedScene(key, out ScenePixels? composed))+ {+ List<OpticalPart> parts = Project.Parts.Select(part => part.Clone()).ToList();+ Task<ScenePixels?> task = Task.Run(() => PartPainter.Paint(key.Source, key.Width, key.Height, key.Background, parts));+ _composition = (key, task);+ try+ {+ composed = await task;+ }+ finally+ {+ _composition = null;+ }+ _composedCache = (key.Source, key.PartsVersion, key.Width, key.Height, key.Background, composed);+ }long painted = System.Diagnostics.Stopwatch.GetTimestamp();- previewPane.ScenePixels = pixels;- Logger.Debug("Scene pixels updated: composed in {Paint:0.0} ms, pictures in {Pictures:0.0} ms", System.Diagnostics.Stopwatch.GetElapsedTime(started, painted).TotalMilliseconds, System.Diagnostics.Stopwatch.GetElapsedTime(painted).TotalMilliseconds);+ previewPane.ScenePixels = composed;+ Logger.Debug("Scene pixels updated: composed on a worker in {Paint:0.0} ms, pictures in {Pictures:0.0} ms", System.Diagnostics.Stopwatch.GetElapsedTime(started, painted).TotalMilliseconds, System.Diagnostics.Stopwatch.GetElapsedTime(painted).TotalMilliseconds);}- catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or NotSupportedException or FileFormatException or RunSpecException)+ while (_composeAgain);+ }++ /// <summary>What a composed scene depends on: the decoded image (by reference), the parts' version, the canvas and the background index.</summary>+ private readonly record struct SceneKey(ScenePixels? Source, int PartsVersion, int Width, int Height, double Background);++ /// <summary>The composed scene cached for these inputs, when it is the one.</summary>+ private bool TryCachedScene(SceneKey key, out ScenePixels? composed)+ {+ if (_composedCache is { } cached && ReferenceEquals(cached.Source, key.Source) && cached.PartsVersion == key.PartsVersion+ && cached.Width == key.Width && cached.Height == key.Height && cached.Background == key.Background){- Logger.Debug("Scene image not shown: {Message}", ex.Message);- previewPane.ScenePixels = null;+ composed = cached.Composed;+ return true;}+ composed = null;+ return false;}/// <summary>/// The scene as the engines, the outline and the export see it: the decoded image (decoded once per file version)/// with the enabled optical parts painted in, or the parts on a blank canvas; null for a canvas without parts./// Composed once per (image, parts version, canvas size, background index): an unchanged scene returns the same- /// reference, which <see cref="RunSpec.SameSceneAs"/> compares, so the scratch runner keeps its engine.+ /// reference, which <see cref="RunSpec.SameSceneAs"/> compares, so the scratch runner keeps its engine. A worker+ /// painting these very inputs (<see cref="ComposeScene"/>) is waited for rather than painting them again./// </summary>private ScenePixels? LoadSceneImage(){ScenePixels? source = DecodeSceneImage();Scene scene = Project.Scene;- if (_composedCache is { } cached && ReferenceEquals(cached.Source, source) && cached.PartsVersion == _partsVersion- && cached.Width == scene.Width && cached.Height == scene.Height && cached.Background == scene.RefractiveIndex)+ var key = new SceneKey(source, _partsVersion, scene.Width, scene.Height, scene.RefractiveIndex);+ if (TryCachedScene(key, out ScenePixels? composed)){- return cached.Composed;+ return composed;}- ScenePixels? composed = PartPainter.Paint(source, scene.Width, scene.Height, scene.RefractiveIndex, Project.Parts);+ composed = _composition is { } composing && composing.Key == key+ ? composing.Task.GetAwaiter().GetResult()+ : PartPainter.Paint(source, scene.Width, scene.Height, scene.RefractiveIndex, Project.Parts);_composedCache = (source, _partsVersion, scene.Width, scene.Height, scene.RefractiveIndex, composed);return composed;}