diff --git a/WaveOptics.Tests/WaveOpticsDepthProcessorTests.cs b/WaveOptics.Tests/WaveOpticsDepthProcessorTests.cs index 47ce9a8..137e532 100644 --- a/WaveOptics.Tests/WaveOpticsDepthProcessorTests.cs +++ b/WaveOptics.Tests/WaveOpticsDepthProcessorTests.cs @@ -177,6 +177,87 @@ public sealed class WaveOpticsDepthProcessorTests AssertIdentical(Render(manual, At(0f)), Render(effect, At(-500f))); } + [Fact] + public void ALongFocalLengthIsUsedAsSet() + { + var effect = Effect(); + effect.FocalLength.Values[0].Value = 300d; + effect.FNumber.Values[0].Value = 32d; + var manual = Effect(false); + manual.FNumber.Values[0].Value = 32d; + manual.Defocus.Values[0].Value = DepthWaves(-500f, focalLength: 300d, fNumber: 32d); + + AssertIdentical(Render(manual, At(0f)), Render(effect, At(-500f))); + } + + [Fact] + public void AShortFocalLengthIsUsedAsSet() + { + var effect = Effect(); + effect.FocalLength.Values[0].Value = 1d; + effect.FNumber.Values[0].Value = 0.5d; + var manual = Effect(false); + manual.FNumber.Values[0].Value = 0.5d; + manual.Defocus.Values[0].Value = DepthWaves(-500f, focalLength: 1d, fNumber: 0.5d); + + AssertIdentical(Render(manual, At(0f)), Render(effect, At(-500f))); + } + + [Fact] + public void AFarFocusDistanceIsUsedAsSet() + { + var effect = Effect(); + effect.FocusDistance.Values[0].Value = 500000d; + effect.FocalLength.Values[0].Value = 31d; + effect.FNumber.Values[0].Value = 1d; + var manual = Effect(false); + manual.FNumber.Values[0].Value = 1d; + manual.Defocus.Values[0].Value = DepthWaves(-999000f, focusDistance: 500000d, focalLength: 31d, fNumber: 1d); + + AssertIdentical(Render(manual, At(0f)), Render(effect, At(-999000f))); + } + + [Fact] + public void ANearFocusDistanceIsUsedAsSet() + { + var effect = Effect(); + effect.FocusDistance.Values[0].Value = 10d; + effect.FocalLength.Values[0].Value = 1d; + effect.FNumber.Values[0].Value = 2d; + var manual = Effect(false); + manual.FNumber.Values[0].Value = 2d; + manual.Defocus.Values[0].Value = DepthWaves(-500f, focusDistance: 10d, focalLength: 1d, fNumber: 2d); + + AssertIdentical(Render(manual, At(0f)), Render(effect, At(-500f))); + } + + [Theory] + [InlineData(-999000f, 10d)] + [InlineData(699f, -10d)] + public void TheTotalDefocusStopsAtTenWaves(float z, double expected) + { + var effect = Effect(); + effect.FocalLength.Values[0].Value = 300d; + effect.FNumber.Values[0].Value = 1d; + var manual = Effect(false); + manual.FNumber.Values[0].Value = 1d; + manual.Defocus.Values[0].Value = expected; + + Assert.True(Math.Abs(DepthWaves(z, focalLength: 300d, fNumber: 1d)) > 100d); + AssertIdentical(Render(manual, At(0f)), Render(effect, At(z))); + } + + [Theory] + [InlineData(nameof(WaveOpticsEffect.FocalLength))] + [InlineData(nameof(WaveOpticsEffect.FocusDistance))] + public void AValueThatIsNotANumberFallsBackToTheDefault(string setting) + { + var broken = Effect(); + ((Animation)typeof(WaveOpticsEffect).GetProperty(setting)!.GetValue(broken)!).Values[0].Value = double.NaN; + + AssertIdentical(Render(Effect(), At(-500f)), Render(broken, At(-500f))); + } + [Fact] public void AnAnimatedFocusDistancePullsTheFocusToTheItem() { diff --git a/WaveOptics.Tests/WaveOpticsDepthTests.cs b/WaveOptics.Tests/WaveOpticsDepthTests.cs index 577f391..0c44bf3 100644 --- a/WaveOptics.Tests/WaveOpticsDepthTests.cs +++ b/WaveOptics.Tests/WaveOpticsDepthTests.cs @@ -288,6 +288,54 @@ public sealed class WaveOpticsDepthTests Assert.Equal(held, WaveOpticsDepth.DefocusWaves(50d, 8d, 50d, 1000d, Wavelength), 12); } + [Fact] + public void TheMinimumDistanceIsOnePointZeroZeroOneTimesTheFocalLength() + { + Assert.Equal(1.001, WaveOpticsDepth.MinimumDistanceRatio); + + var held = WaveOpticsDepth.DefocusWaves(50d, 8d, 50.05d, 1000d, Wavelength); + + Assert.Equal(held, WaveOpticsDepth.DefocusWaves(50d, 8d, 50d, 1000d, Wavelength), 12); + Assert.Equal(held, WaveOpticsDepth.DefocusWaves(50d, 8d, 20d, 1000d, Wavelength), 12); + Assert.NotEqual(held, WaveOpticsDepth.DefocusWaves(50d, 8d, 50.1d, 1000d, Wavelength)); + Assert.Equal(-held, WaveOpticsDepth.DefocusWaves(50d, 8d, 1000d, 50d, Wavelength), 12); + Assert.NotEqual(-held, WaveOpticsDepth.DefocusWaves(50d, 8d, 1000d, 50.1d, Wavelength)); + } + + [Theory] + [InlineData(null)] + [InlineData(700f)] + public void ACameraWithAProjectionPartIsMeasuredThroughTheWholeMatrix(float? perspectiveDistance) + { + var camera = new Matrix4x4(1f, 0f, 0f, 0.0002f, 0f, 1f, 0f, 0.0001f, 0f, 0f, 1f, 0.0003f, 10f, 20f, 30f, 1.5f); + var position = new Vector3(120f, -80f, 200f); + var distance = perspectiveDistance ?? WaveOpticsDepth.BasePerspectiveDistance; + var projection = new Matrix4x4(1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f, -1f / distance, 0f, 0f, 0f, 1f); + var w = Vector4.Transform(new Vector4(position, 1f), camera * projection).W; + + Assert.Equal(distance * (double)w, DistanceOf(Draw(position, camera, perspectiveDistance)), 3); + } + + [Theory] + [InlineData(0d, false)] + [InlineData(0.5d, true)] + public void TheSignOfTheDefocusChangesThePointSpreadOnlyWithASphericalAberration(double spherical, bool differs) + { + PsfKernel Kernel(double defocus) + => new FraunhoferPsfGenerator().Generate(new PsfDescriptor(256, 64, 57, Wavelength, 8d, 4d, ApertureShape.Circular, 6, 0, 0, new WavefrontAberration(defocusWaves: defocus, sphericalWaves: spherical))).Kernel; + + var positive = Kernel(1d).Values.Span; + var negative = Kernel(-1d).Values.Span; + var difference = 0d; + for (var index = 0; index < positive.Length; index++) + difference += Math.Abs(positive[index] - negative[index]); + + if (differs) + Assert.True(difference > 0.5, difference.ToString()); + else + Assert.True(difference < 1e-9, difference.ToString()); + } + [Fact] public void AddingTheDepthDefocusKeepsTheDefocusTheUserSet() {