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#

Range attributes on every clamped model property, proved against Normalize() by library_check

Alexander Graef5d287bd04125
diff --git a/CLAUDE.md b/CLAUDE.md
index aaf393a..047f3a0 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -48,8 +48,10 @@ harnesses run.
string, `XmlSerializer` with sparse XML (attributes, repeated children, `[DefaultValue]`), Segoe UI 9 pt, the
dnSpy look (its sources are GPL: look, do not copy).
- **Model conventions**: a new property gets its XML attribute with a default, `[Category]`/`[DisplayName]`/
- `[Description]`, a `///` summary, a clamp in `Normalize()`, and `Label.`/`Hint.`/`Enum.<Type>.<Member>` keys in
- all four string tables. Randomness from `Project.Seed` through `Pcg32`; generated data is never persisted.
+ `[Description]`, a `///` summary, a clamp in `Normalize()` with the matching `[Range]` beside the default (2026-10-21:
+ the bounds are readable from outside, `library_check` proves each against `Normalize()`; a bound that depends on another
+ value stays in code alone), and `Label.`/`Hint.`/`Enum.<Type>.<Member>` keys in all four string tables. Randomness from
+ `Project.Seed` through `Pcg32`; generated data is never persisted.
- **C# style**: modern and concise, expression-bodied accessors, no `this.`, no `IDisposable` on interfaces,
`out` not `ref`, tabs, Allman; `///` docs state threading, units and coordinate systems. `grep -E`, not `-P`.
- **Log decisions at Debug** (scratch requests, stale marks, postponed re-runs, export steps). Info and up reach
@@ -205,7 +207,7 @@ numbers: no GPU JPEG, no OpenCL colouring at presentation, no `Vector256`, no Di
## Harnesses
-`library_check` (100+ checks: engines, store, renderer, schemes, probes, scratch, reduced preview, importer, export, parts, group move, open edges).
+`library_check` (100+ checks: engines, store, renderer, schemes, probes, scratch, reduced preview, importer, export, parts, group move, open edges, ranges).
UI, all on `Common.ps1`, with their check counts: `PreviewCheck` 43, `RunCheck` 71, `LanguageCheck` 11,
`ProbeGraphCheck` 39, `ImportCheck` 10, `ExportCheck` 38, `PreviewQualityCheck` 35, `SceneImageCheck` 8,
`ColorPickerCheck` 13, `KeyboardCheck` 13, `AboutCheck` 18, `SelectionCheck` 10. Test projects `tiny*.wdproj` sit beside them. A harness
diff --git a/WaveDudeNext/Model/Display.cs b/WaveDudeNext/Model/Display.cs
index cd91535..24fc53b 100644
--- a/WaveDudeNext/Model/Display.cs
+++ b/WaveDudeNext/Model/Display.cs
@@ -16,6 +16,7 @@
// <https://www.gnu.org/licenses/>.
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
namespace WaveDudeNext.Model;
@@ -72,6 +73,7 @@ public sealed class Display : ObservableObject
/// <summary>First simulation step of the exported range (and of loop playback); 0 is the start of the run.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, int.MaxValue)]
[Category("Export")]
[DisplayName("From step")]
[Description("First simulation step of the exported range; 0 is the start of the run.")]
@@ -84,6 +86,7 @@ public sealed class Display : ObservableObject
/// <summary>Last simulation step of the exported range (and of loop playback); 0 means the end of the run.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, int.MaxValue)]
[Category("Export")]
[DisplayName("To step")]
[Description("Last simulation step of the exported range; 0 means the end of the run.")]
@@ -181,6 +184,7 @@ public sealed class DisplayLayer : ObservableObject
/// <summary>Gain applied to the values before the colour lookup.</summary>
[XmlAttribute]
[DefaultValue(DefaultBrightness)]
+ [Range(1e-4, 1e5)]
[Category("Rendering")]
[Description("Gain applied to the values before the colour lookup.")]
public double Brightness
@@ -192,6 +196,7 @@ public sealed class DisplayLayer : ObservableObject
/// <summary>Offset applied before the colour lookup; values at or below it map to the first colour.</summary>
[XmlAttribute]
[DefaultValue(0.0)]
+ [Range(-10.0, 1.0)]
[Category("Rendering")]
[DisplayName("Black level")]
[Description("Offset applied before the colour lookup; values at or below it map to the first colour.")]
@@ -231,6 +236,7 @@ public sealed class DisplayLayer : ObservableObject
/// <summary>Multiplier of the outline's brightness, 0 to <see cref="MaxOverlayGain"/>; 1 shows the edge picture as computed, 0 hides it.</summary>
[XmlAttribute]
[DefaultValue(DefaultOverlayGain)]
+ [Range(0.0, MaxOverlayGain)]
[Category("Scene outline")]
[DisplayName("Outline brightness")]
[Description("Multiplier of the outline: 1 as computed, higher lifts faint edges, 0 hides it.")]
@@ -285,6 +291,7 @@ public sealed class Video : ObservableObject
/// <summary>Frames per second of the exported video.</summary>
[XmlAttribute]
[DefaultValue(DefaultFps)]
+ [Range(1, MaxFps)]
[Category("Video")]
[DisplayName("Frame rate")]
[Description("Frames per second of the exported video.")]
@@ -297,6 +304,7 @@ public sealed class Video : ObservableObject
/// <summary>Encoder quality, 1 to 100.</summary>
[XmlAttribute]
[DefaultValue(DefaultQuality)]
+ [Range(1, 100)]
[Category("Video")]
[Description("Encoder quality, 1 to 100.")]
public int Quality
diff --git a/WaveDudeNext/Model/OpticalPart.cs b/WaveDudeNext/Model/OpticalPart.cs
index 70e458d..c3b2b5a 100644
--- a/WaveDudeNext/Model/OpticalPart.cs
+++ b/WaveDudeNext/Model/OpticalPart.cs
@@ -17,6 +17,7 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
namespace WaveDudeNext.Model;
@@ -100,6 +101,7 @@ public abstract class OpticalPart : ObservableObject, IMovable
[XmlAttribute]
[Category("Position")]
[Description("X of the part's centre in canvas pixels.")]
+ [Range(0, int.MaxValue)]
public int X
{
get => _x;
@@ -110,6 +112,7 @@ public abstract class OpticalPart : ObservableObject, IMovable
[XmlAttribute]
[Category("Position")]
[Description("Y of the part's centre in canvas pixels.")]
+ [Range(0, int.MaxValue)]
public int Y
{
get => _y;
@@ -119,6 +122,7 @@ public abstract class OpticalPart : ObservableObject, IMovable
/// <summary>Rotation about the centre in degrees, counter-clockwise on the screen; 0 points the part's axis along +x.</summary>
[XmlAttribute]
[DefaultValue(0.0)]
+ [Range(-180.0, 180.0, MinimumIsExclusive = true)]
[Category("Position")]
[DisplayName("Angle")]
[Description("Rotation about the centre in degrees, counter-clockwise on the screen. 0 points the part's axis to the right.")]
@@ -299,6 +303,7 @@ public sealed class LensPart : OpticalPart
/// <summary>Refractive index of the glass, 1.00 to 2.55 in steps of 0.01; not used by a mirror.</summary>
[XmlAttribute]
[DefaultValue(DefaultIndex)]
+ [Range(MinIndex, MaxIndex)]
[Category("Material")]
[DisplayName("Refractive index")]
[Description("Refractive index of the glass, 1.00 to 2.55 in steps of 0.01.")]
@@ -311,6 +316,7 @@ public sealed class LensPart : OpticalPart
/// <summary>Thickness in pixels of the anti-reflection band just inside the outline, with the index √n; 0 for none. A quarter-wave coating is λ / (4 √n) thick.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, MaxCoating)]
[Category("Material")]
[DisplayName("AR coating")]
[Description("Thickness in pixels of the anti-reflection band just inside the outline, with the index √n; 0 for none. A quarter-wave coating is λ / (4 √n) pixels thick, λ being the wavelength the source form shows.")]
@@ -326,6 +332,7 @@ public sealed class LensPart : OpticalPart
/// </summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, MaxAbsorption)]
[Category("Material")]
[Description("How strongly the part absorbs waves, 0 (not at all) to 255, as the scene image's blue channel. It rises from nothing at the outline over the absorption ramp, so a wave enters the part instead of being reflected. A mirror ignores it.")]
public int Absorption
@@ -337,6 +344,7 @@ public sealed class LensPart : OpticalPart
/// <summary>Depth in pixels over which the absorption rises from 0 at the outline to <see cref="Absorption"/>; at most half the part's thickness takes effect.</summary>
[XmlAttribute]
[DefaultValue(DefaultAbsorptionRamp)]
+ [Range(1, MaxAbsorptionRamp)]
[Category("Material")]
[DisplayName("Absorption ramp")]
[Description("Depth in pixels over which the absorption rises from nothing at the outline to its full value; the longer, the less of a wave is reflected. A part thinner than twice the ramp reaches its full absorption in its middle.")]
@@ -349,6 +357,7 @@ public sealed class LensPart : OpticalPart
/// <summary>Extent across the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultDiameter)]
+ [Range(2, int.MaxValue)]
[Category("Shape")]
[Description("Extent across the axis in pixels.")]
public int Diameter
@@ -418,6 +427,7 @@ public sealed class LensPart : OpticalPart
/// <summary>Distance between the two surfaces on the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultThickness)]
+ [Range(1, int.MaxValue)]
[Category("Shape")]
[DisplayName("Centre thickness")]
[Description("Distance between the two surfaces on the axis in pixels.")]
@@ -582,6 +592,7 @@ public abstract class ShapePart : OpticalPart
/// <summary>Refractive index of the glass, 1.00 to 2.55 in steps of 0.01; not used by a mirror.</summary>
[XmlAttribute]
[DefaultValue(DefaultIndex)]
+ [Range(MinIndex, MaxIndex)]
[Category("Material")]
[DisplayName("Refractive index")]
[Description("Refractive index of the glass, 1.00 to 2.55 in steps of 0.01.")]
@@ -594,6 +605,7 @@ public abstract class ShapePart : OpticalPart
/// <summary>Thickness in pixels of the anti-reflection band just inside the outline, with the index √n; 0 for none.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, MaxCoating)]
[Category("Material")]
[DisplayName("AR coating")]
[Description("Thickness in pixels of the anti-reflection band just inside the outline, with the index √n; 0 for none. A quarter-wave coating is λ / (4 √n) pixels thick, λ being the wavelength the source form shows.")]
@@ -609,6 +621,7 @@ public abstract class ShapePart : OpticalPart
/// </summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, MaxAbsorption)]
[Category("Material")]
[Description("How strongly the part absorbs waves, 0 (not at all) to 255, as the scene image's blue channel. It rises from nothing at the outline over the absorption ramp, so a wave enters the part instead of being reflected. A mirror ignores it.")]
public int Absorption
@@ -620,6 +633,7 @@ public abstract class ShapePart : OpticalPart
/// <summary>Depth in pixels over which the absorption rises from 0 at the outline to <see cref="Absorption"/>; at most half the part's thickness takes effect.</summary>
[XmlAttribute]
[DefaultValue(DefaultAbsorptionRamp)]
+ [Range(1, MaxAbsorptionRamp)]
[Category("Material")]
[DisplayName("Absorption ramp")]
[Description("Depth in pixels over which the absorption rises from nothing at the outline to its full value; the longer, the less of a wave is reflected. A part thinner than twice the ramp reaches its full absorption in its middle.")]
@@ -673,6 +687,7 @@ public sealed class PolygonPart : ShapePart
/// <summary>Number of sides, 3 to 64.</summary>
[XmlAttribute]
[DefaultValue(DefaultSides)]
+ [Range(MinSides, MaxSides)]
[Category("Shape")]
[DisplayName("Number of sides")]
[Description("Number of sides, 3 to 64.")]
@@ -685,6 +700,7 @@ public sealed class PolygonPart : ShapePart
/// <summary>Length of each side in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultSide)]
+ [Range(2, int.MaxValue)]
[Category("Shape")]
[DisplayName("Side length")]
[Description("Length of each side in pixels.")]
@@ -741,6 +757,7 @@ public sealed class RectanglePart : ShapePart
/// <summary>Extent along the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultSize)]
+ [Range(1, int.MaxValue)]
[Category("Shape")]
[Description("Extent along the axis in pixels.")]
public int Length
@@ -758,6 +775,7 @@ public sealed class RectanglePart : ShapePart
/// <summary>Extent across the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultSize)]
+ [Range(1, int.MaxValue)]
[Category("Shape")]
[Description("Extent across the axis in pixels.")]
public int Width
@@ -836,6 +854,7 @@ public sealed class EllipsePart : ShapePart
/// <summary>Diameter along the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultSize)]
+ [Range(1, int.MaxValue)]
[Category("Shape")]
[Description("Diameter along the axis in pixels.")]
public int Length
@@ -853,6 +872,7 @@ public sealed class EllipsePart : ShapePart
/// <summary>Diameter across the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultSize)]
+ [Range(1, int.MaxValue)]
[Category("Shape")]
[Description("Diameter across the axis in pixels.")]
public int Width
@@ -963,6 +983,7 @@ public sealed class TrianglePart : ShapePart
/// <summary>Length of the base in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultBase)]
+ [Range(2, int.MaxValue)]
[Category("Shape")]
[Description("Length of the base in pixels; the base lies across the axis, toward a source on the left.")]
public int Base
@@ -991,6 +1012,7 @@ public sealed class TrianglePart : ShapePart
/// <summary>Base angle at the first end of the base (-y'), in degrees; typed for the isosceles and custom types.</summary>
[XmlAttribute]
[DefaultValue(60.0)]
+ [Range(1.0, MaxAngle)]
[Category("Shape")]
[DisplayName("Angle A")]
[Description("Base angle at the first end of the base, in degrees. Typed for the isosceles and custom types; the presets fix it.")]
@@ -1009,6 +1031,7 @@ public sealed class TrianglePart : ShapePart
/// <summary>Base angle at the second end of the base (+y'), in degrees; typed for the custom type only.</summary>
[XmlAttribute]
[DefaultValue(60.0)]
+ [Range(1.0, MaxAngle)]
[Category("Shape")]
[DisplayName("Angle B")]
[Description("Base angle at the second end of the base, in degrees. Typed for the custom type; the others fix it.")]
@@ -1153,6 +1176,7 @@ public sealed class FiberPart : OpticalPart
/// <summary>Refractive index of the core, 1.00 to 2.55.</summary>
[XmlAttribute]
[DefaultValue(DefaultCoreIndex)]
+ [Range(MinIndex, MaxIndex)]
[Category("Core")]
[DisplayName("Core index")]
[Description("Refractive index of the core, 1.00 to 2.55 in steps of 0.01.")]
@@ -1165,6 +1189,7 @@ public sealed class FiberPart : OpticalPart
/// <summary>Diameter of the core in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultCoreDiameter)]
+ [Range(2, int.MaxValue)]
[Category("Core")]
[DisplayName("Core diameter")]
[Description("Diameter of the core in pixels.")]
@@ -1177,6 +1202,7 @@ public sealed class FiberPart : OpticalPart
/// <summary>Refractive index of the cladding, 1.00 to 2.55; below the core's for guiding.</summary>
[XmlAttribute]
[DefaultValue(DefaultCladdingIndex)]
+ [Range(MinIndex, MaxIndex)]
[Category("Cladding")]
[DisplayName("Cladding index")]
[Description("Refractive index of the cladding, 1.00 to 2.55 in steps of 0.01. Below the core's index, the fibre guides.")]
@@ -1189,6 +1215,7 @@ public sealed class FiberPart : OpticalPart
/// <summary>Thickness of the cladding around the core in pixels; 0 for a bare core.</summary>
[XmlAttribute]
[DefaultValue(DefaultCladdingThickness)]
+ [Range(0, int.MaxValue)]
[Category("Cladding")]
[DisplayName("Cladding thickness")]
[Description("Thickness of the cladding around the core in pixels; 0 for a bare core.")]
@@ -1201,6 +1228,7 @@ public sealed class FiberPart : OpticalPart
/// <summary>Thickness in pixels of the anti-reflection band just inside the outer boundary, with the index √n of the cladding (or the core when bare); 0 for none.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, MaxCoating)]
[Category("Cladding")]
[DisplayName("AR coating")]
[Description("Thickness in pixels of the anti-reflection band just inside the outer boundary, with the index √n; 0 for none.")]
@@ -1298,6 +1326,7 @@ public sealed class BraggReflectorPart : OpticalPart
/// <summary>Number of layer pairs, 1 to 200.</summary>
[XmlAttribute]
[DefaultValue(DefaultPairs)]
+ [Range(1, MaxPairs)]
[Category("Layers")]
[DisplayName("Layer pairs")]
[Description("Number of layer pairs, 1 to 200.")]
@@ -1310,6 +1339,7 @@ public sealed class BraggReflectorPart : OpticalPart
/// <summary>Thickness of the first layer of each pair in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultThickness)]
+ [Range(1, int.MaxValue)]
[Category("Layers")]
[DisplayName("Thickness 1")]
[Description("Thickness of the first layer of each pair in pixels. A quarter-wave layer is λ / (4 n) pixels thick.")]
@@ -1322,6 +1352,7 @@ public sealed class BraggReflectorPart : OpticalPart
/// <summary>Thickness of the second layer of each pair in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultThickness)]
+ [Range(1, int.MaxValue)]
[Category("Layers")]
[DisplayName("Thickness 2")]
[Description("Thickness of the second layer of each pair in pixels. A quarter-wave layer is λ / (4 n) pixels thick.")]
@@ -1334,6 +1365,7 @@ public sealed class BraggReflectorPart : OpticalPart
/// <summary>Refractive index of the first layer of each pair, 1.00 to 2.55.</summary>
[XmlAttribute]
[DefaultValue(DefaultIndex1)]
+ [Range(MinIndex, MaxIndex)]
[Category("Layers")]
[DisplayName("Index 1")]
[Description("Refractive index of the first layer of each pair, 1.00 to 2.55 in steps of 0.01.")]
@@ -1346,6 +1378,7 @@ public sealed class BraggReflectorPart : OpticalPart
/// <summary>Refractive index of the second layer of each pair, 1.00 to 2.55.</summary>
[XmlAttribute]
[DefaultValue(DefaultIndex2)]
+ [Range(MinIndex, MaxIndex)]
[Category("Layers")]
[DisplayName("Index 2")]
[Description("Refractive index of the second layer of each pair, 1.00 to 2.55 in steps of 0.01.")]
@@ -1358,6 +1391,7 @@ public sealed class BraggReflectorPart : OpticalPart
/// <summary>Extent of the stack across the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultWidth)]
+ [Range(2, int.MaxValue)]
[Category("Shape")]
[Description("Extent of the stack across the axis in pixels.")]
public int Width
@@ -1426,6 +1460,7 @@ public sealed class GrinLensPart : OpticalPart
/// <summary>Extent along the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultLength)]
+ [Range(1, int.MaxValue)]
[Category("Shape")]
[Description("Extent along the axis in pixels.")]
public int Length
@@ -1437,6 +1472,7 @@ public sealed class GrinLensPart : OpticalPart
/// <summary>Extent across the axis in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultDiameter)]
+ [Range(2, int.MaxValue)]
[Category("Shape")]
[Description("Extent across the axis in pixels.")]
public int Diameter
@@ -1448,6 +1484,7 @@ public sealed class GrinLensPart : OpticalPart
/// <summary>Refractive index on the axis, 1.00 to 2.55.</summary>
[XmlAttribute]
[DefaultValue(DefaultCentreIndex)]
+ [Range(MinIndex, MaxIndex)]
[Category("Material")]
[DisplayName("Centre index")]
[Description("Refractive index on the axis, 1.00 to 2.55 in steps of 0.01.")]
@@ -1460,6 +1497,7 @@ public sealed class GrinLensPart : OpticalPart
/// <summary>Refractive index at the edge, 1.00 to 2.55; the profile between is parabolic.</summary>
[XmlAttribute]
[DefaultValue(DefaultEdgeIndex)]
+ [Range(MinIndex, MaxIndex)]
[Category("Material")]
[DisplayName("Edge index")]
[Description("Refractive index at the edge, 1.00 to 2.55 in steps of 0.01. The profile between the axis and the edge is parabolic.")]
@@ -1472,6 +1510,7 @@ public sealed class GrinLensPart : OpticalPart
/// <summary>Thickness in pixels of the anti-reflection band just inside the outline, with the index √n of the local index; 0 for none.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, MaxCoating)]
[Category("Material")]
[DisplayName("AR coating")]
[Description("Thickness in pixels of the anti-reflection band just inside the outline, with the index √n; 0 for none.")]
diff --git a/WaveDudeNext/Model/Probe.cs b/WaveDudeNext/Model/Probe.cs
index ea26a15..9aa089f 100644
--- a/WaveDudeNext/Model/Probe.cs
+++ b/WaveDudeNext/Model/Probe.cs
@@ -16,6 +16,7 @@
// <https://www.gnu.org/licenses/>.
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
namespace WaveDudeNext.Model;
@@ -316,6 +317,7 @@ public sealed class Probe : ObservableObject, IMovable
/// <summary>Width of the graph box in canvas pixels; used when <see cref="BoxSize"/> is <see cref="ProbeBoxSize.Custom"/>, omitted from the file otherwise.</summary>
[XmlAttribute]
[DefaultValue(DefaultBoxWidth)]
+ [Range(MinBoxWidth, MaxBoxSide)]
[Category("Graph")]
[DisplayName("Box width")]
[Description("Width of the graph box in canvas pixels; custom box size only. A box larger than the canvas is shrunk to fit it, aspect kept.")]
@@ -328,6 +330,7 @@ public sealed class Probe : ObservableObject, IMovable
/// <summary>Height of the graph box in canvas pixels; used when <see cref="BoxSize"/> is <see cref="ProbeBoxSize.Custom"/>, omitted from the file otherwise.</summary>
[XmlAttribute]
[DefaultValue(DefaultBoxHeight)]
+ [Range(MinBoxHeight, MaxBoxSide)]
[Category("Graph")]
[DisplayName("Box height")]
[Description("Height of the graph box in canvas pixels; custom box size only. A box larger than the canvas is shrunk to fit it, aspect kept.")]
@@ -426,6 +429,7 @@ public sealed class Probe : ObservableObject, IMovable
/// </summary>
[XmlAttribute]
[DefaultValue(DefaultDecades)]
+ [Range(MinDecades, MaxDecades)]
[Category("Axes")]
[DisplayName("Decades")]
[Description("Decades a logarithmic value axis spans below its top; smaller values, zero included, sit on the bottom line.")]
@@ -443,6 +447,7 @@ public sealed class Probe : ObservableObject, IMovable
/// <summary>Width of the sliding step window for <see cref="ProbeScaleMode.Running"/>, in steps.</summary>
[XmlAttribute]
[DefaultValue(DefaultWindow)]
+ [Range(MinWindow, MaxWindow)]
[Category("Axes")]
[DisplayName("Window")]
[Description("Width of the sliding step window, in steps.")]
@@ -455,6 +460,7 @@ public sealed class Probe : ObservableObject, IMovable
/// <summary>Step at which recording starts; 0 records from the first step.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, int.MaxValue)]
[Category("General")]
[DisplayName("Start after step")]
[Description("Step at which recording starts; 0 records from the first step.")]
diff --git a/WaveDudeNext/Model/Scene.cs b/WaveDudeNext/Model/Scene.cs
index 0608a12..cb752a2 100644
--- a/WaveDudeNext/Model/Scene.cs
+++ b/WaveDudeNext/Model/Scene.cs
@@ -16,6 +16,7 @@
// <https://www.gnu.org/licenses/>.
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
namespace WaveDudeNext.Model;
@@ -180,6 +181,7 @@ public sealed class Scene : ObservableObject
/// <summary>Width of a blank canvas in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultWidth)]
+ [Range(MinSize, MaxSize)]
[Category("Canvas")]
[Description("Width of a blank canvas in pixels.")]
public int Width
@@ -191,6 +193,7 @@ public sealed class Scene : ObservableObject
/// <summary>Height of a blank canvas in pixels.</summary>
[XmlAttribute]
[DefaultValue(DefaultHeight)]
+ [Range(MinSize, MaxSize)]
[Category("Canvas")]
[Description("Height of a blank canvas in pixels.")]
public int Height
@@ -206,6 +209,7 @@ public sealed class Scene : ObservableObject
/// </summary>
[XmlAttribute]
[DefaultValue(DefaultDampingEdge)]
+ [Range(-MaxDampingEdge, MaxDampingEdge)]
[Category("Canvas")]
[DisplayName("Damping edge")]
[Description("Thickness in pixels of the absorbing frame: positive inside the canvas, where it is visible, negative outside it, where it is not. 0 turns it off.")]
@@ -230,6 +234,7 @@ public sealed class Scene : ObservableObject
/// <summary>Refractive index of a blank canvas, 1.0 to 2.55; the wave speed is 1/n cells per step.</summary>
[XmlAttribute]
[DefaultValue(DefaultRefractiveIndex)]
+ [Range(MinRefractiveIndex, MaxRefractiveIndex)]
[Category("Canvas")]
[DisplayName("Refractive index")]
[Description("Refractive index of a blank canvas, 1.0 to 2.55. The wave speed is 1/n cells per step.")]
@@ -254,6 +259,7 @@ public sealed class Scene : ObservableObject
/// <summary>Peak amplitude of the image sources.</summary>
[XmlAttribute]
[DefaultValue(DefaultImageSourceScale)]
+ [Range(0.0, MaxImageSourceScale)]
[Category("Sources")]
[DisplayName("Image source amplitude")]
[Description("Peak amplitude of the image sources.")]
diff --git a/WaveDudeNext/Model/Simulation.cs b/WaveDudeNext/Model/Simulation.cs
index e6fd345..a2386f6 100644
--- a/WaveDudeNext/Model/Simulation.cs
+++ b/WaveDudeNext/Model/Simulation.cs
@@ -16,6 +16,7 @@
// <https://www.gnu.org/licenses/>.
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
namespace WaveDudeNext.Model;
@@ -52,6 +53,7 @@ public sealed class Simulation : ObservableObject
/// <summary>Total number of simulation steps.</summary>
[XmlAttribute]
[DefaultValue(DefaultSteps)]
+ [Range(1, MaxSteps)]
[Category("Run")]
[Description("Total number of simulation steps.")]
public int Steps
@@ -63,6 +65,7 @@ public sealed class Simulation : ObservableObject
/// <summary>Render a frame, export it and sample the probes every this many steps.</summary>
[XmlAttribute]
[DefaultValue(DefaultVisualizeEvery)]
+ [Range(1, MaxVisualizeEvery)]
[Category("Run")]
[DisplayName("Visualize every")]
[Description("Render a frame, export it and sample the probes every this many steps.")]
@@ -87,6 +90,7 @@ public sealed class Simulation : ObservableObject
/// <summary>Nominal wave speed in pixels per step; only used to estimate wavelengths in the editor.</summary>
[XmlAttribute]
[DefaultValue(DefaultWaveSpeed)]
+ [Range(1e-6, 1e6)]
[Category("Solver")]
[DisplayName("Wave speed")]
[Description("Nominal wave speed in pixels per step; only used to estimate wavelengths in the editor.")]
@@ -99,6 +103,7 @@ public sealed class Simulation : ObservableObject
/// <summary>Averaging window factor of the intensity: the engine averages over this × 100 / lowest source level steps.</summary>
[XmlAttribute]
[DefaultValue(DefaultTimeDecay)]
+ [Range(1.0, 100.0)]
[Category("Solver")]
[DisplayName("Intensity time decay")]
[Description("Averaging window factor of the intensity: the engine averages over this × 100 / lowest source level steps.")]
@@ -111,6 +116,7 @@ public sealed class Simulation : ObservableObject
/// <summary>Multiplier of the level-to-frequency mapping: ω = level / 255 × 0.5 × scale, in radians per step.</summary>
[XmlAttribute]
[DefaultValue(DefaultSourceFrequencyScale)]
+ [Range(0.0, 1000.0)]
[Category("Solver")]
[DisplayName("Source frequency scale")]
[Description("Multiplier of the level-to-frequency mapping: ω = level / 255 × 0.5 × scale, in radians per step.")]
@@ -123,6 +129,7 @@ public sealed class Simulation : ObservableObject
/// <summary>Multiplier applied to the intensity values the engine produces.</summary>
[XmlAttribute]
[DefaultValue(DefaultIntensityScale)]
+ [Range(1e-7, 1e6)]
[Category("Solver")]
[DisplayName("Intensity scale")]
[Description("Multiplier applied to the intensity values the engine produces.")]
diff --git a/WaveDudeNext/Model/Source.cs b/WaveDudeNext/Model/Source.cs
index 2095228..b4aef41 100644
--- a/WaveDudeNext/Model/Source.cs
+++ b/WaveDudeNext/Model/Source.cs
@@ -16,6 +16,7 @@
// <https://www.gnu.org/licenses/>.
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
namespace WaveDudeNext.Model;
@@ -118,6 +119,7 @@ public sealed class Source : ObservableObject, IMovable
/// <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)]
+ [Range(MinLevel, MaxLevel)]
[Category("Wave")]
[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
@@ -129,6 +131,7 @@ public sealed class Source : ObservableObject, IMovable
/// <summary>Amplitude in percent, 0 to 100, decimals allowed: the engines scale the source by a real factor, and whole percents were only a spin-box setting in Wave Dude 2D.</summary>
[XmlAttribute]
[DefaultValue(DefaultAmplitude)]
+ [Range(0.0, 100.0)]
[Category("Wave")]
[Description("Amplitude in percent, 0 to 100, decimals allowed.")]
public double Amplitude
@@ -140,6 +143,7 @@ public sealed class Source : ObservableObject, IMovable
/// <summary>Phase offset in degrees, -180 to 180, decimals allowed.</summary>
[XmlAttribute]
[DefaultValue(0.0)]
+ [Range(-180.0, 180.0)]
[Category("Wave")]
[Description("Phase offset in degrees, -180 to 180.")]
public double Phase
@@ -174,6 +178,7 @@ public sealed class Source : ObservableObject, IMovable
/// <summary>Length of each burst in cycles of the source frequency; pulsed mode only.</summary>
[XmlAttribute]
[DefaultValue(DefaultWidth)]
+ [Range(1, int.MaxValue)]
[Category("Pulsing")]
[DisplayName("Pulse width")]
[Description("Length of each burst in cycles of the source frequency; pulsed mode only.")]
@@ -186,6 +191,7 @@ public sealed class Source : ObservableObject, IMovable
/// <summary>Length of the gap between bursts in cycles; pulsed mode only.</summary>
[XmlAttribute]
[DefaultValue(DefaultWidth)]
+ [Range(1, int.MaxValue)]
[Category("Pulsing")]
[DisplayName("Off width")]
[Description("Length of the gap between bursts in cycles; pulsed mode only.")]
@@ -198,6 +204,7 @@ public sealed class Source : ObservableObject, IMovable
/// <summary>Simulation step at which the source fades out; <see cref="NeverDetach"/> keeps it on for the whole run.</summary>
[XmlAttribute]
[DefaultValue(NeverDetach)]
+ [Range(NeverDetach, int.MaxValue)]
[Category("General")]
[DisplayName("Detach after step")]
[Description("Simulation step at which the source fades out; -1 keeps it on for the whole run.")]
@@ -370,6 +377,7 @@ public sealed class SourceArray : ObservableObject
/// <summary>Number of emitters across; odd counts keep the centre emitter on the source position.</summary>
[XmlAttribute]
[DefaultValue(1)]
+ [Range(1, int.MaxValue)]
[Category("Grid")]
[DisplayName("Columns")]
[Description("Number of emitters across. Odd counts keep the centre emitter on the source position.")]
@@ -382,6 +390,7 @@ public sealed class SourceArray : ObservableObject
/// <summary>Number of emitters down.</summary>
[XmlAttribute]
[DefaultValue(1)]
+ [Range(1, int.MaxValue)]
[Category("Grid")]
[DisplayName("Rows")]
[Description("Number of emitters down.")]
@@ -418,6 +427,7 @@ public sealed class SourceArray : ObservableObject
/// <summary>Rotation of the grid about the source position in degrees, counter-clockwise as seen on the screen; 0 keeps the rows horizontal (2026-10-14, the user's request).</summary>
[XmlAttribute]
[DefaultValue(0.0)]
+ [Range(-180.0, 180.0, MinimumIsExclusive = true)]
[Category("Grid")]
[DisplayName("Rotation")]
[Description("Rotation of the grid about the source position in degrees, counter-clockwise on the screen. 0 keeps the rows horizontal.")]
@@ -430,6 +440,7 @@ public sealed class SourceArray : ObservableObject
/// <summary>Half-range of the per-emitter frequency offset in levels; 0 turns it off.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, int.MaxValue)]
[Category("Randomisation")]
[DisplayName("Frequency")]
[Description("Each emitter's level is offset by up to ± this amount. 0 turns it off.")]
@@ -442,6 +453,7 @@ public sealed class SourceArray : ObservableObject
/// <summary>Half-range of the per-emitter phase offset in degrees; 0 turns it off.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, int.MaxValue)]
[Category("Randomisation")]
[DisplayName("Phase")]
[Description("Each emitter's phase is offset by up to ± this many degrees. 0 turns it off.")]
@@ -454,6 +466,7 @@ public sealed class SourceArray : ObservableObject
/// <summary>Half-range of the gap offset in cycles, re-drawn by the engine every period; 0 turns it off.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, int.MaxValue)]
[Category("Randomisation")]
[DisplayName("Off width")]
[Description("Each gap between bursts is offset by up to ± this many cycles, re-drawn every period. 0 turns it off.")]
@@ -466,6 +479,7 @@ public sealed class SourceArray : ObservableObject
/// <summary>Half-range of the per-emitter position jitter in canvas pixels, independent for X and Y; 0 turns it off.</summary>
[XmlAttribute]
[DefaultValue(0)]
+ [Range(0, int.MaxValue)]
[Category("Randomisation")]
[DisplayName("Position")]
[Description("Each emitter is moved by up to ± this many pixels in X and in Y. 0 turns it off.")]
diff --git a/WaveDudeNext/Rendering/ColorScheme.cs b/WaveDudeNext/Rendering/ColorScheme.cs
index 70337b0..fbd440a 100644
--- a/WaveDudeNext/Rendering/ColorScheme.cs
+++ b/WaveDudeNext/Rendering/ColorScheme.cs
@@ -18,6 +18,7 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Xml.Serialization;
using WaveDudeNext.Model;
@@ -61,6 +62,7 @@ public sealed class ColorStop : ObservableObject
[XmlAttribute("At")]
[Category("Stop")]
[Description("Position along the gradient, 0 at the lowest value to 1 at the highest.")]
+ [Range(0.0, 1.0)]
public double Position
{
get => _position;
diff --git a/_tools/library_check/Program.cs b/_tools/library_check/Program.cs
index 190e338..34bfaf0 100644
--- a/_tools/library_check/Program.cs
+++ b/_tools/library_check/Program.cs
@@ -15,9 +15,11 @@
// version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
// <https://www.gnu.org/licenses/>.
+using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Diagnostics;
using System.Numerics.Tensors;
+using System.Reflection;
using System.Runtime.InteropServices;
using NLog;
using WaveDudeNext.Engine;
@@ -257,6 +259,7 @@ internal static class Program
CheckMirror(W, H);
CheckDecimalLevels(W, H);
CheckMovable();
+ CheckRanges();
CheckOpenEdges(W, H);
CheckFrameStore(scene);
CheckPreviewQuality(scene, W, H);
@@ -274,6 +277,109 @@ internal static class Program
return _failures == 0 ? 0 : 1;
}
+ /// <summary>
+ /// Every <see cref="RangeAttribute"/> on the model agrees with its owner's Normalize(): a value below or above the
+ /// range lands inside it, and a value at an inclusive bound stays as it is. Each leg runs on a fresh instance, so
+ /// dependent clamps (a triangle's second base angle) see the state the case prepares.
+ /// </summary>
+ private static void CheckRanges()
+ {
+ Console.WriteLine("--- [Range] attributes against Normalize()");
+ static (Action Normalize, object[] Targets) Part(OpticalPart part) => (part.Normalize, [part]);
+ (string Name, Func<(Action Normalize, object[] Targets)> Make)[] cases =
+ [
+ ("Scene", () => { var s = new Scene(); return (s.Normalize, [s]); }),
+ ("Simulation", () => { var s = new Simulation(); return (s.Normalize, [s]); }),
+ ("Display", () => { var d = new Display(); return (d.Normalize, [d, d.Field, d.Intensity, d.Video]); }),
+ ("Source", () => { var s = new Source { Array = new SourceArray() }; return (s.Normalize, [s, s.Array]); }),
+ ("Probe", () => { var p = new Probe(); return (p.Normalize, [p]); }),
+ ("ColorStop", () =>
+ {
+ var scheme = new ColorScheme();
+ var stop = new ColorStop(0.5, new Rgb(1, 2, 3));
+ scheme.Stops.Add(stop);
+ return (scheme.Normalize, [stop]);
+ }),
+ ("LensPart", () => Part(new LensPart())),
+ ("PolygonPart", () => Part(new PolygonPart())),
+ ("RectanglePart", () => Part(new RectanglePart())),
+ ("EllipsePart", () => Part(new EllipsePart())),
+ // a custom triangle with the first angle at its minimum leaves the second its full range
+ ("TrianglePart", () => Part(new TrianglePart { Type = TriangleType.Custom, AngleA = 1.0 })),
+ ("OutlinePart", () => Part(new OutlinePart())),
+ ("FiberPart", () => Part(new FiberPart())),
+ ("BraggReflectorPart", () => Part(new BraggReflectorPart())),
+ ("GrinLensPart", () => Part(new GrinLensPart())),
+ ];
+ int total = 0;
+ foreach ((string name, Func<(Action Normalize, object[] Targets)> make) in cases)
+ {
+ var failures = new List<string>();
+ int count = 0;
+ object[] shape = make().Targets;
+ for (int t = 0; t < shape.Length; t++)
+ {
+ foreach (PropertyInfo property in shape[t].GetType().GetProperties())
+ {
+ if (property.GetCustomAttribute<RangeAttribute>() is not { } range)
+ {
+ continue;
+ }
+ count++;
+ bool integer = property.PropertyType == typeof(int);
+ double min = Convert.ToDouble(range.Minimum, CultureInfo.InvariantCulture);
+ double max = Convert.ToDouble(range.Maximum, CultureInfo.InvariantCulture);
+ // the value to set and what Normalize() must leave: the bound itself, or NaN for "inside the range"
+ var legs = new List<(string Leg, double Set, double Expect)>();
+ if (min > (integer ? int.MinValue : double.MinValue))
+ {
+ legs.Add(("below", integer ? min - 1 : min - Math.Max(1.0, Math.Abs(min)), double.NaN));
+ }
+ if (max < (integer ? int.MaxValue : double.MaxValue))
+ {
+ legs.Add(("above", integer ? max + 1 : max + Math.Max(1.0, Math.Abs(max)), double.NaN));
+ }
+ if (!range.MinimumIsExclusive)
+ {
+ legs.Add(("at min", min, min));
+ }
+ if (!range.MaximumIsExclusive)
+ {
+ legs.Add(("at max", max, max));
+ }
+ foreach ((string leg, double set, double expect) in legs)
+ {
+ string where = $"{shape[t].GetType().Name}.{property.Name} {leg} ({range.Minimum}..{range.Maximum}): set {set}";
+ try
+ {
+ (Action normalize, object[] targets) = make();
+ object target = targets[t];
+ property.SetValue(target, integer ? (object)(int)set : set);
+ normalize();
+ double got = Convert.ToDouble(property.GetValue(target), CultureInfo.InvariantCulture);
+ bool inside = (range.MinimumIsExclusive ? got > min : got >= min) && (range.MaximumIsExclusive ? got < max : got <= max);
+ if (!(double.IsNaN(expect) ? inside : got == expect))
+ {
+ failures.Add($"{where}, got {got}");
+ }
+ }
+ catch (Exception e)
+ {
+ failures.Add($"{where}, threw {e.GetType().Name}: {e.Message}");
+ }
+ }
+ }
+ }
+ total += count;
+ Check(count > 0 && failures.Count == 0, $"{name}: {count} [Range] attribute(s) agree with Normalize()");
+ foreach (string failure in failures)
+ {
+ Console.WriteLine(" " + failure);
+ }
+ }
+ Console.WriteLine($" {total} ranged properties");
+ }
+
/// <summary>"bench-render [w h]": what colouring a stored frame costs, per layer, at that size (the presenter's cost per picture).</summary>
private static void BenchRender(int w, int h, bool load = false)
{