From a645d93ab3adb01d9095eb32bd5c960772f4cc35 Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 7 Oct 2026 17:27:25 +0900 Subject: [PATCH] =?UTF-8?q?=E7=82=B9=E5=83=8F=E3=81=AE=E6=A8=99=E6=9C=AC?= =?UTF-8?q?=E5=8C=96=E3=81=A7=E4=BD=8D=E7=BD=AE=E3=81=AE=E8=A8=88=E7=AE=97?= =?UTF-8?q?=E3=82=92=E5=88=97=E3=81=A8=E8=A1=8C=E3=81=AB=E5=88=86=E3=81=91?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・光学系を変えたフレームの点像分布関数の計算を高速化-M --- WaveOptics.Tests/KernelSamplingTests.cs | 66 +++++++++++++++++++++ WaveOptics/Optics/FraunhoferPsfGenerator.cs | 62 ++++++++++++------- 2 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 WaveOptics.Tests/KernelSamplingTests.cs diff --git a/WaveOptics.Tests/KernelSamplingTests.cs b/WaveOptics.Tests/KernelSamplingTests.cs new file mode 100644 index 0000000..b13a508 --- /dev/null +++ b/WaveOptics.Tests/KernelSamplingTests.cs @@ -0,0 +1,66 @@ +using WaveOptics.Abstractions; +using WaveOptics.Optics; + +namespace WaveOptics.Tests.Psf; + +public sealed class KernelSamplingTests +{ + static double[] Grid(int size, int seed) + { + var random = new Random(seed); + var values = new double[size * size]; + for (var index = 0; index < values.Length; index++) + values[index] = random.NextDouble() * Math.Exp(-random.NextDouble() * 12); + return values; + } + + static double Bilinear(double[] values, int size, double x, double y) + { + if (x < 0 || y < 0 || x > size - 1 || y > size - 1) + return 0; + + var x0 = (int)Math.Floor(x); + var y0 = (int)Math.Floor(y); + var x1 = Math.Min(x0 + 1, size - 1); + var y1 = Math.Min(y0 + 1, size - 1); + var tx = x - x0; + var ty = y - y0; + var top = values[y0 * size + x0] * (1 - tx) + values[y0 * size + x1] * tx; + var bottom = values[y1 * size + x0] * (1 - tx) + values[y1 * size + x1] * tx; + return top * (1 - ty) + bottom * ty; + } + + [Theory] + [InlineData(128, 5, 4, 8, 1)] + [InlineData(256, 15, 4, 8, 1)] + [InlineData(256, 30, 2, 16, 0.86)] + [InlineData(512, 63, 4, 8, 1.19)] + [InlineData(256, 40, 100, 0.5, 1)] + [InlineData(256, 20, 0.25, 64, 0.9)] + [InlineData(64, 31, 4, 8, 1.3)] + public void TheKernelIsTheBilinearInterpolationOfTheGridAtTheScaledPositions(int gridSize, int radius, double pitch, double fNumber, double scale) + { + var specification = new PsfSpecification(gridSize, gridSize / 4, radius * 2 + 1, 550, fNumber, pitch, ApertureShape.Circular, 6, 0, 0, default); + var grid = Grid(gridSize, gridSize + radius); + var kernel = new double[specification.KernelSize * specification.KernelSize]; + var pixelPitch = specification.SensorPixelPitchMicrometers; + var focalPitch = FraunhoferPsfGenerator.FocalPlaneSamplePitch(in specification); + + var energy = FraunhoferPsfGenerator.SampleKernel(in specification, grid, kernel, scale); + + var expectedEnergy = 0d; + for (var y = 0; y < specification.KernelSize; y++) + { + var sampleY = gridSize / 2 + (y - radius) * pixelPitch / focalPitch * scale; + for (var x = 0; x < specification.KernelSize; x++) + { + var sampleX = gridSize / 2 + (x - radius) * pixelPitch / focalPitch * scale; + var expected = Bilinear(grid, gridSize, sampleX, sampleY); + Assert.Equal(expected, kernel[y * specification.KernelSize + x]); + expectedEnergy += expected; + } + } + + Assert.Equal(expectedEnergy, energy); + } +} diff --git a/WaveOptics/Optics/FraunhoferPsfGenerator.cs b/WaveOptics/Optics/FraunhoferPsfGenerator.cs index 47cfb4a..02a852a 100644 --- a/WaveOptics/Optics/FraunhoferPsfGenerator.cs +++ b/WaveOptics/Optics/FraunhoferPsfGenerator.cs @@ -130,14 +130,52 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator var kernelRadius = kernelSize / 2; var rawKernelEnergy = 0d; + Span positions = stackalloc double[kernelSize]; + Span lower = stackalloc int[kernelSize]; + Span upper = stackalloc int[kernelSize]; + Span fractions = stackalloc double[kernelSize]; + for (var index = 0; index < kernelSize; index++) + { + var position = SamplePosition(index, kernelRadius, center, specification.SensorPixelPitchMicrometers, focalPlaneSamplePitch, positionScale); + positions[index] = position; + if (position < 0 || position > gridSize - 1) + continue; + + var floor = (int)Math.Floor(position); + lower[index] = floor; + upper[index] = Math.Min(floor + 1, gridSize - 1); + fractions[index] = position - floor; + } + for (var y = 0; y < kernelSize; y++) { - var sampleY = SamplePosition(y, kernelRadius, center, specification.SensorPixelPitchMicrometers, focalPlaneSamplePitch, positionScale); + var row = kernel.Slice(y * kernelSize, kernelSize); + var sampleY = positions[y]; + if (sampleY < 0 || sampleY > gridSize - 1) + { + row.Clear(); + continue; + } + + var topRow = lower[y] * gridSize; + var bottomRow = upper[y] * gridSize; + var ty = fractions[y]; for (var x = 0; x < kernelSize; x++) { - var sampleX = SamplePosition(x, kernelRadius, center, specification.SensorPixelPitchMicrometers, focalPlaneSamplePitch, positionScale); - var value = SampleBilinear(intensity, gridSize, sampleX, sampleY); - kernel[y * kernelSize + x] = value; + var sampleX = positions[x]; + if (sampleX < 0 || sampleX > gridSize - 1) + { + row[x] = 0; + continue; + } + + var left = lower[x]; + var right = upper[x]; + var tx = fractions[x]; + var top = intensity[topRow + left] * (1 - tx) + intensity[topRow + right] * tx; + var bottom = intensity[bottomRow + left] * (1 - tx) + intensity[bottomRow + right] * tx; + var value = top * (1 - ty) + bottom * ty; + row[x] = value; rawKernelEnergy += value; } } @@ -227,20 +265,4 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator var boundary = Math.Cos(Math.PI / specification.BladeCount) / Math.Cos(folded); return radius <= boundary; } - - static double SampleBilinear(double[] values, int size, double x, double y) - { - if (x < 0 || y < 0 || x > size - 1 || y > size - 1) - return 0; - - var x0 = (int)Math.Floor(x); - var y0 = (int)Math.Floor(y); - var x1 = Math.Min(x0 + 1, size - 1); - var y1 = Math.Min(y0 + 1, size - 1); - var tx = x - x0; - var ty = y - y0; - var top = values[y0 * size + x0] * (1 - tx) + values[y0 * size + x1] * tx; - var bottom = values[y1 * size + x0] * (1 - tx) + values[y1 * size + x1] * tx; - return top * (1 - ty) + bottom * ty; - } }