From 310fd9d409739411cb1950828eb779e13a78a45b Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 7 Oct 2026 17:49:39 +0900 Subject: [PATCH] =?UTF-8?q?=E5=BA=83=E5=B8=AF=E5=9F=9F=E3=81=AE=E8=BF=91?= =?UTF-8?q?=E4=BC=BC=E3=81=A8=E8=89=B2=E3=81=94=E3=81=A8=E3=81=AE=E8=AA=A4?= =?UTF-8?q?=E5=B7=AE=E3=81=AE=E4=B8=8A=E9=99=90=E3=82=92=E8=A9=A6=E9=A8=93?= =?UTF-8?q?=E3=81=A7=E5=9B=BA=E3=82=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ChromaticReferenceTests.cs | 13 +++ WaveOptics.Tests/BroadbandAccuracyTests.cs | 84 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 WaveOptics.Tests/BroadbandAccuracyTests.cs diff --git a/SpectralConvolution/SpectralConvolution.Tests/ChromaticReferenceTests.cs b/SpectralConvolution/SpectralConvolution.Tests/ChromaticReferenceTests.cs index ba2caba..0b5e92e 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/ChromaticReferenceTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/ChromaticReferenceTests.cs @@ -137,4 +137,17 @@ public sealed class ChromaticReferenceTests Assert.Throws(() => ConvolutionBound.ChromaticAbsolute(128, unit, -1d)); Assert.Throws(() => ConvolutionBound.ChromaticAbsolute(128, unit, double.NaN)); } + + [Theory] + [InlineData(64, 0.304, 0.170)] + [InlineData(128, 0.704, 0.393)] + [InlineData(256, 1.598, 0.892)] + [InlineData(512, 3.576, 1.997)] + public void TheWorstCaseTileOfTheChromaticPathStaysWithinTheDerivedNumberOfLevels(int size, double gpuLevels, double cpuLevels) + { + var norm = Math.Sqrt(2d * size * size); + + Assert.Equal(gpuLevels, ConvolutionBound.ChromaticRelative(size, ConvolutionBound.GpuOperationError) * norm * 255d, 3); + Assert.Equal(cpuLevels, ConvolutionBound.ChromaticRelative(size, ConvolutionBound.CpuOperationError) * norm * 255d, 3); + } } diff --git a/WaveOptics.Tests/BroadbandAccuracyTests.cs b/WaveOptics.Tests/BroadbandAccuracyTests.cs new file mode 100644 index 0000000..ad57b7b --- /dev/null +++ b/WaveOptics.Tests/BroadbandAccuracyTests.cs @@ -0,0 +1,84 @@ +using WaveOptics.Abstractions; +using WaveOptics.Effects; +using WaveOptics.Optics; + +namespace WaveOptics.Tests.Psf; + +public sealed class BroadbandAccuracyTests +{ + const int DenseSamples = 61; + + static PsfSpecification Specification(WavefrontAberration aberration) + => new(512, 128, 63 * 2 + 1, SpectralPlan.ReferenceWavelength, 8, 4, ApertureShape.Circular, 6, 0, 0, aberration); + + static double[] DenseKernel(in PsfSpecification specification, double center) + { + var sampler = new FraunhoferKernelSampler(); + var area = specification.KernelSize * specification.KernelSize; + var kernel = new double[area]; + var values = new double[area]; + var weights = new double[DenseSamples]; + var sum = 0d; + for (var index = 0; index < DenseSamples; index++) + { + var offset = -SpectralPlan.BandSpan + 2d * SpectralPlan.BandSpan * index / (DenseSamples - 1); + weights[index] = Math.Exp(-0.5 * offset * offset); + sum += weights[index]; + } + + for (var index = 0; index < DenseSamples; index++) + { + var offset = -SpectralPlan.BandSpan + 2d * SpectralPlan.BandSpan * index / (DenseSamples - 1); + var wavelength = center + SpectralPlan.BandSigma * offset; + var phaseScale = SpectralPlan.ReferenceWavelength / wavelength; + Assert.True(sampler.TryComputeIntensity(in specification, specification.PupilDiameterSamples * phaseScale, phaseScale, [1d])); + Assert.True(sampler.TrySampleKernel(in specification, 1d, values)); + for (var element = 0; element < area; element++) + kernel[element] += weights[index] / sum * values[element]; + } + + return kernel; + } + + static double Distance(ReadOnlySpan left, ReadOnlySpan right) + { + var sum = 0d; + for (var index = 0; index < left.Length; index++) + sum += Math.Abs(left[index] - right[index]); + return sum; + } + + public static TheoryData Names => ["defocus", "coma", "spherical"]; + + static WavefrontAberration Aberration(string name) + => name switch + { + "defocus" => new WavefrontAberration(defocusWaves: 3), + "coma" => new WavefrontAberration(comaHorizontalWaves: 1), + _ => new WavefrontAberration(sphericalWaves: -1, defocusWaves: 0.5), + }; + + [Theory] + [MemberData(nameof(Names))] + public void TheQualityTiersApproachTheDenseBroadbandKernelInTheirOwnOrder(string name) + { + var specification = Specification(Aberration(name)); + var area = specification.KernelSize * specification.KernelSize; + var sampler = new ChromaticKernelSampler(); + double[] distances = new double[3]; + foreach (var quality in Enum.GetValues()) + { + var red = new double[area]; + var green = new double[area]; + var blue = new double[area]; + Assert.True(sampler.TrySample(in specification, WaveOpticsColorMode.Broadband, quality, red, green, blue)); + distances[(int)quality] = Math.Max(Distance(green, DenseKernel(in specification, SpectralPlan.GreenWavelength)), Distance(blue, DenseKernel(in specification, SpectralPlan.BlueWavelength))); + } + + Assert.True(distances[0] > distances[1], $"{name}: {string.Join(" ", distances)}"); + Assert.True(distances[1] > distances[2], $"{name}: {string.Join(" ", distances)}"); + Assert.InRange(distances[0], 0.06, 0.1); + Assert.InRange(distances[1], 0.025, 0.07); + Assert.InRange(distances[2], 0.01, 0.03); + } +}