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 lens fills its diameter: radii too curved for the diameter and centre thickness are raised to fit

Alexander Graef156829533a37
diff --git a/OpticalParts.md b/OpticalParts.md
index 958ed0a..02213fd 100644
--- a/OpticalParts.md
+++ b/OpticalParts.md
@@ -15,7 +15,10 @@ under `ShapePart` (mirror, index, coating), each its own kind, element and form:
side length, a face centred on -x'), `RectanglePart` (length, width, a Square button), `TrianglePart` (base across the
axis toward -x', two base angles, a type preset: equilateral, right isosceles, 30-60-90, isosceles, custom; the apex angle
shown), `OutlinePart` (points). The "+ Part" menu lists the optics, a separator, the shapes. The handle glyph is the
-source's circular crosshair with an inner ring (a lens-shaped glyph read as squashed). Not built: stage 3 (`PartCheck.ps1`, the three
+source's circular crosshair with an inner ring (a lens-shaped glyph read as squashed). A lens's diameter and centre
+thickness are its bulk: radii too curved for them are raised to the smallest that fits (`LensPart.FitRadii`), so the lens
+always fills its diameter; the form shows the edge thickness (a diameter of 2000 with radii of 300 had given a tiny lens).
+A "Concave (diverging)" box shows the lens's state from the lensmaker's sign and, ticked, flips both radii. Not built: stage 3 (`PartCheck.ps1`, the three
examples) and the selected part's accent outline. The open points at the end still stand.
## What is asked
diff --git a/WaveDudeNext.WPF/Converters/LensEdgeConverter.cs b/WaveDudeNext.WPF/Converters/LensEdgeConverter.cs
new file mode 100644
index 0000000..b3505a0
--- /dev/null
+++ b/WaveDudeNext.WPF/Converters/LensEdgeConverter.cs
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Copyright (C) 2026 Alexander Graef <[email protected]>
+//
+// This file is part of WaveDude Next, a 2D wave simulator.
+//
+// Based on the work of
+// JJM Vleggaar (Huygens Optics), author of Wave Dude 2D, Copyright (C) 2026
+// <https://www.youtube.com/@HuygensOptics>, <https://www.huygensoptics.com>
+// Manuel (0x23, Diffraction Limited), author of WaveSimulator2D
+// <https://www.youtube.com/@DiffractionLimited>, <https://github.com/0x23/WaveSimulator2D>
+//
+// WaveDude Next is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free Software
+// Foundation, either version 3 of the License, or (at your option) any later
+// version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
+// <https://www.gnu.org/licenses/>.
+
+using System.Globalization;
+using System.Windows.Data;
+using WaveDudeNext.Model;
+
+namespace WaveDudeNext.WPF.Converters;
+
+/// <summary>
+/// The edge thickness of a lens part as text ("5.6 px"), computed on a throw-away part so the model's own rule
+/// (<see cref="LensPart.FitRadii"/>) decides. Inputs: the diameter, the centre thickness, R1, R2; re-evaluated on each edit.
+/// </summary>
+public sealed class LensEdgeConverter : IMultiValueConverter
+{
+ /// <inheritdoc/>
+ public object? Convert(object?[] values, Type targetType, object? parameter, CultureInfo culture)
+ {
+ if (values.Length < 4 || values[0] is not int diameter || values[1] is not int thickness || values[2] is not double r1 || values[3] is not double r2)
+ {
+ return null;
+ }
+ var probe = new LensPart { Diameter = diameter, Thickness = thickness, R1 = r1, R2 = r2 };
+ return string.Format(culture, "{0:0.#} px", probe.EdgeThickness);
+ }
+
+ /// <inheritdoc/>
+ public object[] ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) =>
+ throw new NotSupportedException();
+}
diff --git a/WaveDudeNext.WPF/Strings/Strings.DE.xaml b/WaveDudeNext.WPF/Strings/Strings.DE.xaml
index 54b7786..55f02ee 100644
--- a/WaveDudeNext.WPF/Strings/Strings.DE.xaml
+++ b/WaveDudeNext.WPF/Strings/Strings.DE.xaml
@@ -267,6 +267,8 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="TipSquare">Die Breite gleich der Länge setzen</s:String>
<s:String x:Key="LabelApexAngle">Winkel an der Spitze</s:String>
<s:String x:Key="TipApexAngle">Der Winkel an der Spitze: 180° minus die beiden Grundwinkel</s:String>
+ <s:String x:Key="LabelEdgeThickness">Randdicke</s:String>
+ <s:String x:Key="TipEdgeThickness">Dicke am Rand: die Mittendicke abzüglich dessen, was die konvexen Flächen nehmen, zuzüglich dessen, was die konkaven hinzufügen; 0, wenn sich die Flächen am Rand treffen</s:String>
<s:String x:Key="SectionGraph">Diagramm</s:String>
<s:String x:Key="SectionAxes">Achsen</s:String>
<s:String x:Key="ButtonNewSeed">_Neuer Startwert</s:String>
@@ -526,10 +528,12 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="Hint.LensPart.Coating">Dicke in Pixeln des entspiegelnden Bands direkt innerhalb des Umrisses mit dem Index √n; 0 für keines. Eine λ/4-Schicht ist λ / (4 √n) Pixel dick, λ die Wellenlänge aus dem Quellenformular.</s:String>
<s:String x:Key="Label.LensPart.Diameter">Durchmesser</s:String>
<s:String x:Key="Hint.LensPart.Diameter">Ausdehnung quer zur Achse in Pixeln.</s:String>
+ <s:String x:Key="Label.LensPart.IsConcave">Konkav (zerstreuend)</s:String>
+ <s:String x:Key="Hint.LensPart.IsConcave">Ob die Linse zerstreut, ihre Flächen also nach innen gewölbt sind. Anhaken dreht die Linse um, indem beide Radien ihr Vorzeichen wechseln; eine ebene Fläche bleibt eben.</s:String>
<s:String x:Key="Label.LensPart.R1">R1</s:String>
- <s:String x:Key="Hint.LensPart.R1">Radius der ersten Fläche in Pixeln: positiv wölbt sich nach außen, negativ ist konkav, 0 ist eben.</s:String>
+ <s:String x:Key="Hint.LensPart.R1">Radius der ersten Fläche in Pixeln: positiv wölbt sich nach außen, negativ ist konkav, 0 ist eben. Ein für Durchmesser und Mittendicke zu stark gekrümmter Radius wird auf den kleinsten passenden angehoben.</s:String>
<s:String x:Key="Label.LensPart.R2">R2</s:String>
- <s:String x:Key="Hint.LensPart.R2">Radius der zweiten Fläche in Pixeln: negativ wölbt sich nach außen, positiv ist konkav, 0 ist eben.</s:String>
+ <s:String x:Key="Hint.LensPart.R2">Radius der zweiten Fläche in Pixeln: negativ wölbt sich nach außen, positiv ist konkav, 0 ist eben. Ein für Durchmesser und Mittendicke zu stark gekrümmter Radius wird auf den kleinsten passenden angehoben.</s:String>
<s:String x:Key="Label.LensPart.Thickness">Mittendicke</s:String>
<s:String x:Key="Hint.LensPart.Thickness">Abstand der beiden Flächen auf der Achse in Pixeln.</s:String>
<s:String x:Key="Label.ShapePart.Mirror">Spiegel</s:String>
diff --git a/WaveDudeNext.WPF/Strings/Strings.EN.xaml b/WaveDudeNext.WPF/Strings/Strings.EN.xaml
index 94685b0..352ca7b 100644
--- a/WaveDudeNext.WPF/Strings/Strings.EN.xaml
+++ b/WaveDudeNext.WPF/Strings/Strings.EN.xaml
@@ -269,6 +269,8 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="TipSquare">Make the width equal to the length</s:String>
<s:String x:Key="LabelApexAngle">Apex angle</s:String>
<s:String x:Key="TipApexAngle">The angle at the apex: 180° minus the two base angles</s:String>
+ <s:String x:Key="LabelEdgeThickness">Edge thickness</s:String>
+ <s:String x:Key="TipEdgeThickness">Thickness at the rim: the centre thickness less what the convex surfaces take, plus what the concave ones add; 0 when the surfaces meet at the rim</s:String>
<s:String x:Key="SectionGraph">Graph</s:String>
<s:String x:Key="SectionAxes">Axes</s:String>
<s:String x:Key="ButtonNewSeed">_New seed</s:String>
@@ -528,10 +530,12 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="Hint.LensPart.Coating">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.</s:String>
<s:String x:Key="Label.LensPart.Diameter">Diameter</s:String>
<s:String x:Key="Hint.LensPart.Diameter">Extent across the axis in pixels.</s:String>
+ <s:String x:Key="Label.LensPart.IsConcave">Concave (diverging)</s:String>
+ <s:String x:Key="Hint.LensPart.IsConcave">Whether the lens diverges, its surfaces curving inward. Ticking it turns the lens around by flipping the sign of both radii; a flat surface stays flat.</s:String>
<s:String x:Key="Label.LensPart.R1">R1</s:String>
- <s:String x:Key="Hint.LensPart.R1">Radius of the first surface in pixels: positive bulges outward, negative is concave, 0 is flat.</s:String>
+ <s:String x:Key="Hint.LensPart.R1">Radius of the first surface in pixels: positive bulges outward, negative is concave, 0 is flat. A radius too curved for the diameter and the centre thickness is raised to the smallest that fits.</s:String>
<s:String x:Key="Label.LensPart.R2">R2</s:String>
- <s:String x:Key="Hint.LensPart.R2">Radius of the second surface in pixels: negative bulges outward, positive is concave, 0 is flat.</s:String>
+ <s:String x:Key="Hint.LensPart.R2">Radius of the second surface in pixels: negative bulges outward, positive is concave, 0 is flat. A radius too curved for the diameter and the centre thickness is raised to the smallest that fits.</s:String>
<s:String x:Key="Label.LensPart.Thickness">Centre thickness</s:String>
<s:String x:Key="Hint.LensPart.Thickness">Distance between the two surfaces on the axis in pixels.</s:String>
<s:String x:Key="Label.ShapePart.Mirror">Mirror</s:String>
diff --git a/WaveDudeNext.WPF/Strings/Strings.FR.xaml b/WaveDudeNext.WPF/Strings/Strings.FR.xaml
index be6c35f..b8c333b 100644
--- a/WaveDudeNext.WPF/Strings/Strings.FR.xaml
+++ b/WaveDudeNext.WPF/Strings/Strings.FR.xaml
@@ -267,6 +267,8 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="TipSquare">Rendre la largeur égale à la longueur</s:String>
<s:String x:Key="LabelApexAngle">Angle au sommet</s:String>
<s:String x:Key="TipApexAngle">L'angle au sommet : 180° moins les deux angles à la base</s:String>
+ <s:String x:Key="LabelEdgeThickness">Épaisseur au bord</s:String>
+ <s:String x:Key="TipEdgeThickness">Épaisseur au bord : l'épaisseur au centre moins ce que prennent les surfaces convexes, plus ce qu'ajoutent les concaves ; 0 quand les surfaces se rejoignent au bord</s:String>
<s:String x:Key="SectionGraph">Graphique</s:String>
<s:String x:Key="SectionAxes">Axes</s:String>
<s:String x:Key="ButtonNewSeed">_Nouvelle graine</s:String>
@@ -526,10 +528,12 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="Hint.LensPart.Coating">Épaisseur en pixels de la bande antireflet juste à l'intérieur du contour, d'indice √n ; 0 pour aucune. Une couche quart d'onde fait λ / (4 √n) pixels, λ étant la longueur d'onde affichée dans le formulaire de la source.</s:String>
<s:String x:Key="Label.LensPart.Diameter">Diamètre</s:String>
<s:String x:Key="Hint.LensPart.Diameter">Étendue perpendiculaire à l'axe en pixels.</s:String>
+ <s:String x:Key="Label.LensPart.IsConcave">Concave (divergente)</s:String>
+ <s:String x:Key="Hint.LensPart.IsConcave">Si la lentille est divergente, ses surfaces courbées vers l'intérieur. Cocher retourne la lentille en changeant le signe des deux rayons ; une surface plane reste plane.</s:String>
<s:String x:Key="Label.LensPart.R1">R1</s:String>
- <s:String x:Key="Hint.LensPart.R1">Rayon de la première surface en pixels : positif bombé vers l'extérieur, négatif concave, 0 plan.</s:String>
+ <s:String x:Key="Hint.LensPart.R1">Rayon de la première surface en pixels : positif bombé vers l'extérieur, négatif concave, 0 plan. Un rayon trop courbé pour le diamètre et l'épaisseur au centre est porté au plus petit qui convient.</s:String>
<s:String x:Key="Label.LensPart.R2">R2</s:String>
- <s:String x:Key="Hint.LensPart.R2">Rayon de la seconde surface en pixels : négatif bombé vers l'extérieur, positif concave, 0 plan.</s:String>
+ <s:String x:Key="Hint.LensPart.R2">Rayon de la seconde surface en pixels : négatif bombé vers l'extérieur, positif concave, 0 plan. Un rayon trop courbé pour le diamètre et l'épaisseur au centre est porté au plus petit qui convient.</s:String>
<s:String x:Key="Label.LensPart.Thickness">Épaisseur au centre</s:String>
<s:String x:Key="Hint.LensPart.Thickness">Distance entre les deux surfaces sur l'axe en pixels.</s:String>
<s:String x:Key="Label.ShapePart.Mirror">Miroir</s:String>
diff --git a/WaveDudeNext.WPF/Strings/Strings.NL.xaml b/WaveDudeNext.WPF/Strings/Strings.NL.xaml
index 5fa3746..b9a1061 100644
--- a/WaveDudeNext.WPF/Strings/Strings.NL.xaml
+++ b/WaveDudeNext.WPF/Strings/Strings.NL.xaml
@@ -267,6 +267,8 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="TipSquare">De breedte gelijk aan de lengte maken</s:String>
<s:String x:Key="LabelApexAngle">Tophoek</s:String>
<s:String x:Key="TipApexAngle">De hoek aan de top: 180° min de twee basishoeken</s:String>
+ <s:String x:Key="LabelEdgeThickness">Randdikte</s:String>
+ <s:String x:Key="TipEdgeThickness">Dikte aan de rand: de middendikte min wat de bolle oppervlakken nemen, plus wat de holle toevoegen; 0 als de oppervlakken elkaar aan de rand raken</s:String>
<s:String x:Key="SectionGraph">Grafiek</s:String>
<s:String x:Key="SectionAxes">Assen</s:String>
<s:String x:Key="ButtonNewSeed">_Nieuwe seed</s:String>
@@ -526,10 +528,12 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<s:String x:Key="Hint.LensPart.Coating">Dikte in pixels van de antireflectieband net binnen de omtrek, met index √n; 0 voor geen. Een kwartgolfcoating is λ / (4 √n) pixels dik, λ de golflengte die het bronformulier toont.</s:String>
<s:String x:Key="Label.LensPart.Diameter">Diameter</s:String>
<s:String x:Key="Hint.LensPart.Diameter">Afmeting dwars op de as in pixels.</s:String>
+ <s:String x:Key="Label.LensPart.IsConcave">Hol (divergerend)</s:String>
+ <s:String x:Key="Hint.LensPart.IsConcave">Of de lens divergeert, dus de oppervlakken naar binnen gekromd zijn. Aanvinken keert de lens om door het teken van beide stralen te wisselen; een vlak oppervlak blijft vlak.</s:String>
<s:String x:Key="Label.LensPart.R1">R1</s:String>
- <s:String x:Key="Hint.LensPart.R1">Straal van het eerste oppervlak in pixels: positief bolt naar buiten, negatief is hol, 0 is vlak.</s:String>
+ <s:String x:Key="Hint.LensPart.R1">Straal van het eerste oppervlak in pixels: positief bolt naar buiten, negatief is hol, 0 is vlak. Een straal die te sterk gekromd is voor de diameter en de middendikte wordt verhoogd tot de kleinste die past.</s:String>
<s:String x:Key="Label.LensPart.R2">R2</s:String>
- <s:String x:Key="Hint.LensPart.R2">Straal van het tweede oppervlak in pixels: negatief bolt naar buiten, positief is hol, 0 is vlak.</s:String>
+ <s:String x:Key="Hint.LensPart.R2">Straal van het tweede oppervlak in pixels: negatief bolt naar buiten, positief is hol, 0 is vlak. Een straal die te sterk gekromd is voor de diameter en de middendikte wordt verhoogd tot de kleinste die past.</s:String>
<s:String x:Key="Label.LensPart.Thickness">Middendikte</s:String>
<s:String x:Key="Hint.LensPart.Thickness">Afstand tussen de twee oppervlakken op de as in pixels.</s:String>
<s:String x:Key="Label.ShapePart.Mirror">Spiegel</s:String>
diff --git a/WaveDudeNext.WPF/Views/LensPartView.xaml b/WaveDudeNext.WPF/Views/LensPartView.xaml
index 503dc19..82a376c 100644
--- a/WaveDudeNext.WPF/Views/LensPartView.xaml
+++ b/WaveDudeNext.WPF/Views/LensPartView.xaml
@@ -21,11 +21,13 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WaveDudeNext.WPF.Controls"
+ xmlns:converters="clr-namespace:WaveDudeNext.WPF.Converters"
xmlns:meta="clr-namespace:WaveDudeNext.WPF.Markup"
xmlns:views="clr-namespace:WaveDudeNext.WPF.Views"
xmlns:model="clr-namespace:WaveDudeNext.Model;assembly=WaveDudeNext">
<UserControl.Resources>
+ <converters:LensEdgeConverter x:Key="EdgeThickness" />
<!-- a mirror has no index and no coating -->
<Style x:Key="GlassOnly" TargetType="{x:Type controls:NumberBox}" BasedOn="{StaticResource {x:Type controls:NumberBox}}">
<Style.Triggers>
@@ -59,6 +61,10 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:LensPart.Diameter}" />
<controls:NumberBox Text="{Binding Diameter}" Minimum="2" Increment="10" ToolTip="{meta:Hint model:LensPart.Diameter}" AutomationProperties.AutomationId="PartDiameter" />
+ <!-- the lens's state in one box: on for a diverging lens; ticking it flips both radii -->
+ <CheckBox controls:FormPanel.FullWidth="True" Content="{meta:Label model:LensPart.IsConcave}"
+ IsChecked="{Binding IsConcave}" ToolTip="{meta:Hint model:LensPart.IsConcave}" AutomationProperties.AutomationId="PartConcave" />
+
<TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:LensPart.R1}" />
<controls:NumberBox Text="{Binding R1}" Increment="10" ToolTip="{meta:Hint model:LensPart.R1}" AutomationProperties.AutomationId="PartR1" />
@@ -67,6 +73,19 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:LensPart.Thickness}" />
<controls:NumberBox Text="{Binding Thickness}" Minimum="1" ToolTip="{meta:Hint model:LensPart.Thickness}" AutomationProperties.AutomationId="PartThickness" />
+
+ <!-- read-only: what the radii leave at the rim; 0 when the surfaces meet there -->
+ <TextBlock Style="{StaticResource FormLabel}" Text="{DynamicResource LabelEdgeThickness}" />
+ <TextBlock VerticalAlignment="Center" ToolTip="{DynamicResource TipEdgeThickness}" AutomationProperties.AutomationId="PartEdgeThickness">
+ <TextBlock.Text>
+ <MultiBinding Converter="{StaticResource EdgeThickness}">
+ <Binding Path="Diameter" />
+ <Binding Path="Thickness" />
+ <Binding Path="R1" />
+ <Binding Path="R2" />
+ </MultiBinding>
+ </TextBlock.Text>
+ </TextBlock>
</controls:FormPanel>
</StackPanel>
</ScrollViewer>
diff --git a/WaveDudeNext/Model/OpticalPart.cs b/WaveDudeNext/Model/OpticalPart.cs
index 9b9ee76..de83371 100644
--- a/WaveDudeNext/Model/OpticalPart.cs
+++ b/WaveDudeNext/Model/OpticalPart.cs
@@ -202,7 +202,10 @@ public sealed class PartPoint : ObservableObject
/// A lens, or a curved or flat mirror when <see cref="Mirror"/> is set: the strip of <see cref="Diameter"/> across
/// the axis between two spherical surfaces of radii <see cref="R1"/> (left, facing -x') and <see cref="R2"/>
/// (right), <see cref="Thickness"/> apart on the axis. A radius is signed: R1 &gt; 0 and R2 &lt; 0 bulge outward
-/// (biconvex), 0 is flat, and |R| is at least half the diameter. Where two convex surfaces cross, the lens ends.
+/// (biconvex), 0 is flat. The diameter and the centre thickness are the lens's bulk; the radii fit them: a radius
+/// too curved for them is raised to the smallest that fits (<see cref="FitRadii"/>), so the lens always fills its
+/// diameter and the edge thickness is never negative (the user's point, 2026-10-18: a diameter of 2000 with the
+/// default radii gave a tiny lens, since the surfaces crossed long before the rim).
/// </summary>
public sealed class LensPart : OpticalPart
{
@@ -274,28 +277,62 @@ public sealed class LensPart : OpticalPart
set => SetProperty(ref _diameter, value);
}
- /// <summary>Radius of the first surface (facing -x') in pixels: positive bulges outward, negative is concave, 0 is flat.</summary>
+ /// <summary>Radius of the first surface (facing -x') in pixels: positive bulges outward, negative is concave, 0 is flat; raised when too curved for the diameter and thickness.</summary>
[XmlAttribute]
[DefaultValue(DefaultR1)]
[Category("Shape")]
[DisplayName("R1")]
- [Description("Radius of the first surface in pixels: positive bulges outward, negative is concave, 0 is flat.")]
+ [Description("Radius of the first surface in pixels: positive bulges outward, negative is concave, 0 is flat. A radius too curved for the diameter and the centre thickness is raised to the smallest that fits.")]
public double R1
{
get => _r1;
- set => SetProperty(ref _r1, value);
+ set
+ {
+ if (SetProperty(ref _r1, value))
+ {
+ OnPropertyChanged(nameof(IsConcave));
+ }
+ }
}
- /// <summary>Radius of the second surface (facing +x') in pixels: negative bulges outward, positive is concave, 0 is flat.</summary>
+ /// <summary>Radius of the second surface (facing +x') in pixels: negative bulges outward, positive is concave, 0 is flat; raised when too curved for the diameter and thickness.</summary>
[XmlAttribute]
[DefaultValue(DefaultR2)]
[Category("Shape")]
[DisplayName("R2")]
- [Description("Radius of the second surface in pixels: negative bulges outward, positive is concave, 0 is flat.")]
+ [Description("Radius of the second surface in pixels: negative bulges outward, positive is concave, 0 is flat. A radius too curved for the diameter and the centre thickness is raised to the smallest that fits.")]
public double R2
{
get => _r2;
- set => SetProperty(ref _r2, value);
+ set
+ {
+ if (SetProperty(ref _r2, value))
+ {
+ OnPropertyChanged(nameof(IsConcave));
+ }
+ }
+ }
+
+ /// <summary>
+ /// Whether the lens diverges: the lensmaker's sum of curvatures, 1/R1 − 1/R2, is negative (a flat surface counts
+ /// 0). Setting it to the other state turns the lens around by flipping the sign of both radii, so biconvex becomes
+ /// biconcave and plano-convex plano-concave (the user's proposal, 2026-10-18). Not stored: the radii are.
+ /// </summary>
+ [XmlIgnore]
+ [Category("Shape")]
+ [DisplayName("Concave (diverging)")]
+ [Description("Whether the lens diverges, its surfaces curving inward. Ticking it turns the lens around by flipping the sign of both radii; a flat surface stays flat.")]
+ public bool IsConcave
+ {
+ get => (R1 == 0.0 ? 0.0 : 1.0 / R1) - (R2 == 0.0 ? 0.0 : 1.0 / R2) < 0.0;
+ set
+ {
+ if (value != IsConcave)
+ {
+ R1 = -R1;
+ R2 = -R2;
+ }
+ }
}
/// <summary>Distance between the two surfaces on the axis in pixels.</summary>
@@ -311,20 +348,79 @@ public sealed class LensPart : OpticalPart
}
/// <inheritdoc/>
- public override double AxisReach => Thickness / 2.0 + Math.Max(Sag(R1), Sag(R2));
+ public override double AxisReach
+ {
+ get
+ {
+ (double r1, double r2) = FitRadii(Diameter, Thickness, R1, R2);
+ double half = Math.Max(2, Diameter) / 2.0;
+ return Math.Max(1, Thickness) / 2.0 + Math.Max(Sag(r1, half), Sag(r2, half));
+ }
+ }
- /// <summary>How far a surface of radius <paramref name="r"/> bows out beyond its vertex at the lens edge, in pixels.</summary>
- private double Sag(double r)
+ /// <summary>Thickness at the rim in pixels: the centre thickness less what the convex surfaces take, plus what the concave ones add; never negative after <see cref="FitRadii"/>.</summary>
+ [XmlIgnore]
+ [Browsable(false)]
+ public double EdgeThickness
{
- if (r == 0.0)
+ get
+ {
+ (double r1, double r2) = FitRadii(Diameter, Thickness, R1, R2);
+ double half = Math.Max(2, Diameter) / 2.0;
+ return Math.Max(1, Thickness) - Math.CopySign(Sag(r1, half), r1) + Math.CopySign(Sag(r2, half), r2);
+ }
+ }
+
+ /// <summary>How far a surface of radius <paramref name="radius"/> bows out beyond its vertex at <paramref name="half"/> (half the diameter) from the axis, in pixels; 0 for a flat surface.</summary>
+ public static double Sag(double radius, double half)
+ {
+ if (radius == 0.0)
{
return 0.0;
}
- double a = Math.Abs(r);
- double h = Math.Min(a, Diameter / 2.0);
+ double a = Math.Abs(radius);
+ double h = Math.Min(a, half);
return a - Math.Sqrt(a * a - h * h);
}
+ /// <summary>
+ /// The radii as the lens uses them: a non-finite or zero radius is flat; a magnitude below half the diameter
+ /// becomes half the diameter (the arc must span the rim); and when the convex surfaces (R1 &gt; 0, R2 &lt; 0)
+ /// would bow out by more than the centre thickness plus what the concave ones add, they are flattened in
+ /// proportion to the radius at which their sags just fit, so the surfaces meet at the rim and not before it.
+ /// Shared by <see cref="Normalize"/>, the painter and the readouts, so all see the same lens.
+ /// </summary>
+ public static (double R1, double R2) FitRadii(int diameter, int thickness, double r1, double r2)
+ {
+ double half = Math.Max(2, diameter) / 2.0;
+ r1 = GuardRadius(r1, half);
+ r2 = GuardRadius(r2, half);
+ double sag1 = Sag(r1, half);
+ double sag2 = Sag(r2, half);
+ double convex = (r1 > 0.0 ? sag1 : 0.0) + (r2 < 0.0 ? sag2 : 0.0);
+ double budget = Math.Max(1, thickness) + (r1 < 0.0 ? sag1 : 0.0) + (r2 > 0.0 ? sag2 : 0.0);
+ if (convex > budget)
+ {
+ double scale = budget / convex;
+ if (r1 > 0.0)
+ {
+ r1 = RadiusForSag(half, sag1 * scale);
+ }
+ if (r2 < 0.0)
+ {
+ r2 = -RadiusForSag(half, sag2 * scale);
+ }
+ }
+ return (r1, r2);
+ }
+
+ /// <summary>The radius whose sag at <paramref name="half"/> from the axis is <paramref name="sag"/>, (h² + s²) / 2s, rounded up to a whole pixel (a flatter surface still fits).</summary>
+ private static double RadiusForSag(double half, double sag) => Math.Ceiling((half * half + sag * sag) / (2.0 * sag));
+
+ /// <summary>A radius of at least <paramref name="half"/> in magnitude, its sign kept; a non-finite or zero one is flat.</summary>
+ private static double GuardRadius(double radius, double half) =>
+ !double.IsFinite(radius) || radius == 0.0 ? 0.0 : Math.Abs(radius) < half ? Math.CopySign(half, radius) : radius;
+
/// <inheritdoc/>
public override void Normalize()
{
@@ -333,19 +429,9 @@ public sealed class LensPart : OpticalPart
Coating = Math.Clamp(Coating, 0, MaxCoating);
Diameter = Math.Max(2, Diameter);
Thickness = Math.Max(1, Thickness);
- R1 = NormalizeRadius(R1);
- R2 = NormalizeRadius(R2);
- }
-
- /// <summary>A radius of at least half the diameter in magnitude, its sign kept; 0 stays flat.</summary>
- private double NormalizeRadius(double r)
- {
- if (!double.IsFinite(r) || r == 0.0)
- {
- return 0.0;
- }
- double least = Diameter / 2.0;
- return Math.Abs(r) < least ? Math.CopySign(least, r) : r;
+ (double r1, double r2) = FitRadii(Diameter, Thickness, R1, R2);
+ R1 = r1;
+ R2 = r2;
}
/// <inheritdoc/>
diff --git a/WaveDudeNext/Parts/PartShape.cs b/WaveDudeNext/Parts/PartShape.cs
index 2f53422..0840954 100644
--- a/WaveDudeNext/Parts/PartShape.cs
+++ b/WaveDudeNext/Parts/PartShape.cs
@@ -168,8 +168,7 @@ public abstract class PartShape
_half = Math.Max(1, lens.Diameter) / 2.0;
_vertex1 = -Math.Max(1, lens.Thickness) / 2.0;
_vertex2 = -_vertex1;
- _r1 = GuardRadius(lens.R1, _half);
- _r2 = GuardRadius(lens.R2, _half);
+ (_r1, _r2) = LensPart.FitRadii(lens.Diameter, lens.Thickness, lens.R1, lens.R2);
_index = GuardIndex(lens.RefractiveIndex);
IsMirror = lens.Mirror;
Coating = lens.Mirror ? 0 : GuardCoating(lens.Coating);
@@ -184,11 +183,7 @@ public abstract class PartShape
protected override double BodyIndex(double x, double y) => _index;
/// <summary>How far a surface of radius <paramref name="radius"/> bows out beyond its vertex at the lens edge.</summary>
- private double Sag(double radius) => radius == 0.0 ? 0.0 : Math.Abs(radius) - Math.Sqrt(radius * radius - _half * _half);
-
- /// <summary>A radius of at least <paramref name="half"/> in magnitude, its sign kept; 0 stays flat (the model's rule, applied without writing back).</summary>
- private static double GuardRadius(double radius, double half) =>
- !double.IsFinite(radius) || radius == 0.0 ? 0.0 : Math.Abs(radius) < half ? Math.CopySign(half, radius) : radius;
+ private double Sag(double radius) => LensPart.Sag(radius, _half);
/// <summary>The least of: inside the first surface, inside the second, within the half-diameter.</summary>
protected override double Depth(double x, double y) =>
diff --git a/_tools/library_check/Program.cs b/_tools/library_check/Program.cs
index 4a8f797..1e4e6ba 100644
--- a/_tools/library_check/Program.cs
+++ b/_tools/library_check/Program.cs
@@ -1619,9 +1619,52 @@ internal static class Program
int mirrorRow = Enumerable.Range(0, W).Count(x => RedAt(painted, x, 60) == 0);
Check(mirrorRow == 40 && painted.Bgra.Where((_, i) => i % 4 == 2).All(red => red is 0 or 100), $"as a mirror the lens is red 0 by the pixel-centre test ({mirrorRow} px on the axis), nothing in between");
lens.Mirror = false;
- lens.R1 = 10;
+ lens.R1 = -10;
lens.Normalize();
- Check(lens.R1 == 50 && lens.R2 == -300, "Normalize: |R| below half the diameter becomes half the diameter, the sign kept");
+ Check(lens.R1 == -50 && lens.R2 == -300, "Normalize: a concave radius below half the diameter becomes half the diameter, the sign kept");
+ // the lens's bulk is its diameter and centre thickness; radii too curved for them are raised so the surfaces meet
+ // at the rim, not before it (the user's point: a diameter of 2000 with radii of 300 gave a tiny lens)
+ lens.Diameter = 100;
+ lens.Thickness = 20;
+ lens.R1 = 50;
+ lens.R2 = -50;
+ lens.Normalize();
+ Check(lens.R1 == 130 && lens.R2 == -130 && Math.Abs(lens.EdgeThickness) < 1e-9, $"two semicircular surfaces on a 100 × 20 lens are flattened to R ±130, where their sags of 10 just fit ({lens.R1}, {lens.R2})");
+ painted = PartPainter.Paint(image, W, H, 1.0, project.Parts)!;
+ rows = Enumerable.Range(0, H).Count(y => Enumerable.Range(0, W).Any(x => RedAt(painted, x, y) > 100));
+ centreRow = Enumerable.Range(0, W).Count(x => RedAt(painted, x, 60) == 150);
+ Check(rows >= 98 && rows <= 100 && centreRow == 20, $"the fitted lens fills its diameter ({rows} rows) and keeps its centre thickness ({centreRow} px)");
+ lens.R1 = 300;
+ lens.R2 = 0;
+ lens.Normalize();
+ Check(lens.R1 == 300 && lens.R2 == 0 && Math.Abs(lens.EdgeThickness - (20 - LensPart.Sag(300, 50))) < 1e-9, "a plano-convex lens whose sag fits is left alone; the edge thickness is what the one surface leaves");
+ lens.R1 = 60;
+ lens.R2 = 60;
+ lens.Normalize();
+ Check(lens.R1 == 60 && lens.R2 == 60 && Math.Abs(lens.EdgeThickness - 20) < 1e-9, "a meniscus of equal radii: the convex surface fits since the concave one adds what it takes, the edge as thick as the centre");
+ lens.Diameter = 2000;
+ lens.Thickness = 40;
+ lens.R1 = 300;
+ lens.R2 = -300;
+ lens.Normalize();
+ Check(Math.Abs(lens.R1 - 25010) < 1e-9 && Math.Abs(lens.R2 + 25010) < 1e-9 && Math.Abs(lens.EdgeThickness) < 1e-9, "the user's case: a 2000 × 40 lens gets radii of 25010 and fills its diameter");
+ lens.Diameter = 100;
+ lens.Thickness = 40;
+ lens.R1 = 300;
+ lens.R2 = -300;
+ Check(!lens.IsConcave, "a biconvex lens is not concave");
+ lens.IsConcave = true;
+ Check(lens.IsConcave && lens.R1 == -300 && lens.R2 == 300, "ticking Concave flips both radii: biconcave");
+ lens.IsConcave = true;
+ Check(lens.R1 == -300, "setting the state it already has changes nothing");
+ lens.R1 = 300;
+ lens.R2 = 0;
+ Check(!lens.IsConcave, "plano-convex is not concave");
+ lens.IsConcave = true;
+ Check(lens.R1 == -300 && lens.R2 == 0 && lens.IsConcave, "plano-convex turned around is plano-concave, the flat side still flat");
+ lens.R1 = 100;
+ lens.R2 = 60;
+ Check(lens.IsConcave, "a meniscus whose concave side curves more is concave (diverging)");
project.Parts.Clear();
// the shapes: a regular hexagon's area, a rectangle, triangles, an outline; then a GRIN lens, a Bragg stack, a fibre