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#

library_check finds every part kind and fails a clamped number without a [Range]

Alexander Graef01774190db6e
diff --git a/CLAUDE.md b/CLAUDE.md
index 3bac2cb..d4f5307 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -52,7 +52,8 @@ harnesses run.
- **Model conventions**: a new property gets its XML attribute with a default, `[Category]`/`[DisplayName]`/
`[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
+ value stays in code alone and is named in its `dependentBounds`; since 2026-10-22 it finds every part kind itself and
+ fails a persisted number that `Normalize()` clamps without a `[Range]`), 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`.
diff --git a/_tools/library_check/Program.cs b/_tools/library_check/Program.cs
index c8d3afd..ba6dbac 100644
--- a/_tools/library_check/Program.cs
+++ b/_tools/library_check/Program.cs
@@ -21,6 +21,7 @@ using System.Diagnostics;
using System.Numerics.Tensors;
using System.Reflection;
using System.Runtime.InteropServices;
+using System.Xml.Serialization;
using NLog;
using WaveDudeNext.Engine;
using WaveDudeNext.Export;
@@ -288,7 +289,8 @@ internal static class Program
/// <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.
+ /// dependent clamps (a triangle's second base angle) see the state the case prepares. Every part kind is found by
+ /// reflection, and a persisted number that Normalize() clamps without a [Range] fails its owner (<see cref="CheckUnranged"/>).
/// </summary>
private static void CheckRanges()
{
@@ -308,19 +310,18 @@ internal static class Program
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())),
- ("XorGatePart", () => Part(new XorGatePart())),
- ("ZonePlatePart", () => Part(new ZonePlatePart())),
+ // every part kind, found rather than listed, so a new one cannot be left out; a custom triangle with the first
+ // angle at its minimum leaves the second its full range
+ .. typeof(OpticalPart).Assembly.GetTypes()
+ .Where(type => type.IsSubclassOf(typeof(OpticalPart)) && !type.IsAbstract)
+ .OrderBy(type => type.Name, StringComparer.Ordinal)
+ .Select(type => (type.Name, (Func<(Action, object[])>)(() => Part(type == typeof(TrianglePart)
+ ? new TrianglePart { Type = TriangleType.Custom, AngleA = 1.0 }
+ : (OpticalPart)Activator.CreateInstance(type)!)))),
];
+ // clamped by Normalize() against a bound that depends on another value, which stays in code alone (CLAUDE.md)
+ // (a probe's value axis falls back to -1..1 when its top is not above its bottom)
+ HashSet<string> dependentBounds = ["Probe.YMin", "Probe.YMax"];
int total = 0;
foreach ((string name, Func<(Action Normalize, object[] Targets)> make) in cases)
{
@@ -333,6 +334,7 @@ internal static class Program
{
if (property.GetCustomAttribute<RangeAttribute>() is not { } range)
{
+ CheckUnranged(make, t, property, dependentBounds, failures);
continue;
}
count++;
@@ -381,7 +383,7 @@ internal static class Program
}
}
total += count;
- Check(count > 0 && failures.Count == 0, $"{name}: {count} [Range] attribute(s) agree with Normalize()");
+ Check(count > 0 && failures.Count == 0, $"{name}: {count} [Range] attribute(s) agree with Normalize(), and no persisted number is clamped without one");
foreach (string failure in failures)
{
Console.WriteLine(" " + failure);
@@ -390,6 +392,40 @@ internal static class Program
Console.WriteLine($" {total} ranged properties");
}
+ /// <summary>
+ /// A persisted number without a <see cref="RangeAttribute"/> must come out of Normalize() as it went in, far below and
+ /// far above any sensible value; one that is clamped lacks its attribute, unless its bound depends on another value
+ /// (<paramref name="dependentBounds"/>, "Type.Property"). Failures are added to <paramref name="failures"/>.
+ /// </summary>
+ private static void CheckUnranged(Func<(Action Normalize, object[] Targets)> make, int t, PropertyInfo property, HashSet<string> dependentBounds, List<string> failures)
+ {
+ bool integer = property.PropertyType == typeof(int);
+ if (!(integer || property.PropertyType == typeof(double)) || !property.CanWrite || property.GetCustomAttribute<XmlAttributeAttribute>() is null
+ || dependentBounds.Contains($"{property.DeclaringType!.Name}.{property.Name}") || dependentBounds.Contains($"{property.ReflectedType!.Name}.{property.Name}"))
+ {
+ return;
+ }
+ foreach (double set in (double[])[-1_000_000, 1_000_000])
+ {
+ string where = $"{property.ReflectedType!.Name}.{property.Name} has no [Range]: set {set}";
+ try
+ {
+ (Action normalize, object[] targets) = make();
+ property.SetValue(targets[t], integer ? (object)(int)set : set);
+ normalize();
+ double got = Convert.ToDouble(property.GetValue(targets[t]), CultureInfo.InvariantCulture);
+ if (got != set)
+ {
+ failures.Add($"{where}, Normalize() clamped it to {got}");
+ }
+ }
+ catch (Exception e)
+ {
+ failures.Add($"{where}, threw {e.GetType().Name}: {e.Message}");
+ }
+ }
+ }
+
/// <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)
{