diff --git a/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs b/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs index bf5df92..c831a21 100644 --- a/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs +++ b/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs @@ -82,6 +82,41 @@ public sealed class FraunhoferKernelSamplerTests Assert.Empty(mismatches); } + [Theory] + [InlineData(256, 15, 8d, 4d, 1d)] + [InlineData(512, 40, 8d, 4d, 0.8d)] + [InlineData(128, 40, 8d, 4d, 1.31d)] + [InlineData(128, 63, 0.5d, 100d, 1d)] + [InlineData(256, 3, 64d, 0.25d, 0.5d)] + [InlineData(64, 40, 8d, 4d, 1d)] + public void TheTransposedSamplingIsTheRowMajorSampling(int gridSize, int radius, double fNumber, double pixelPitch, double positionScale) + { + var specification = PsfSpecification.Of(Descriptor(gridSize: gridSize, diameter: gridSize / 4, kernelRadius: radius, fNumber: fNumber, pixelPitch: pixelPitch)); + var random = new Random(7); + var rowMajor = new double[gridSize * gridSize]; + var transposed = new double[gridSize * gridSize]; + for (var row = 0; row < gridSize; row++) + { + for (var column = 0; column < gridSize; column++) + { + var value = random.NextDouble() * random.NextDouble(); + rowMajor[row * gridSize + column] = value; + transposed[column * gridSize + row] = value; + } + } + + var size = specification.KernelSize; + var expected = new double[size * size]; + var actual = new double[size * size]; + actual.AsSpan().Fill(double.NaN); + + var expectedEnergy = FraunhoferPsfGenerator.SampleKernel(in specification, rowMajor, expected, positionScale); + var actualEnergy = FraunhoferPsfGenerator.SampleKernelTransposed(in specification, transposed, actual, positionScale); + + Assert.Equal(BitConverter.DoubleToInt64Bits(expectedEnergy), BitConverter.DoubleToInt64Bits(actualEnergy)); + Assert.True(expected.AsSpan().SequenceEqual(actual)); + } + [Fact] public void AClosedApertureIsNotSampled() { diff --git a/WaveOptics/Optics/FraunhoferKernelSampler.cs b/WaveOptics/Optics/FraunhoferKernelSampler.cs index 927dda5..7d74229 100644 --- a/WaveOptics/Optics/FraunhoferKernelSampler.cs +++ b/WaveOptics/Optics/FraunhoferKernelSampler.cs @@ -51,18 +51,9 @@ internal sealed class FraunhoferKernelSampler FastFourierTransform.Forward(columnReal[..gridSize], columnImaginary[..gridSize]); var split = gridSize - center; - for (var row = 0; row < split; row++) - { - var a = columnReal[row]; - var b = columnImaginary[row]; - intensity[(row + center) * gridSize + shiftedColumn] = a * a + b * b; - } - for (var row = split; row < gridSize; row++) - { - var a = columnReal[row]; - var b = columnImaginary[row]; - intensity[(row - split) * gridSize + shiftedColumn] = a * a + b * b; - } + var destination = intensity.AsSpan(shiftedColumn * gridSize, gridSize); + FraunhoferPsfGenerator.Power(columnReal[..split], columnImaginary[..split], destination[center..]); + FraunhoferPsfGenerator.Power(columnReal[split..gridSize], columnImaginary[split..gridSize], destination[..center]); } return true; @@ -70,7 +61,7 @@ internal sealed class FraunhoferKernelSampler public bool TrySampleKernel(in PsfSpecification specification, double positionScale, Span kernel) { - var energy = FraunhoferPsfGenerator.SampleKernel(in specification, intensity, kernel, positionScale); + var energy = FraunhoferPsfGenerator.SampleKernelTransposed(in specification, intensity, kernel, positionScale); if (!double.IsFinite(energy) || energy <= 0) return false; diff --git a/WaveOptics/Optics/FraunhoferPsfGenerator.cs b/WaveOptics/Optics/FraunhoferPsfGenerator.cs index 3314cc7..4171993 100644 --- a/WaveOptics/Optics/FraunhoferPsfGenerator.cs +++ b/WaveOptics/Optics/FraunhoferPsfGenerator.cs @@ -121,20 +121,13 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator internal static double SamplePosition(int index, int kernelRadius, int center, double pixelPitch, double focalPlaneSamplePitch, double positionScale = 1d) => center + (index - kernelRadius) * pixelPitch / focalPlaneSamplePitch * positionScale; - internal static double SampleKernel(in PsfSpecification specification, double[] intensity, Span kernel, double positionScale = 1d) + static void PrepareSamples(in PsfSpecification specification, double positionScale, Span positions, Span lower, Span upper, Span fractions) { var gridSize = specification.PupilGridSize; var focalPlaneSamplePitch = FocalPlaneSamplePitch(in specification); var center = gridSize / 2; - var kernelSize = specification.KernelSize; - 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 kernelRadius = specification.KernelSize / 2; + for (var index = 0; index < positions.Length; index++) { var position = SamplePosition(index, kernelRadius, center, specification.SensorPixelPitchMicrometers, focalPlaneSamplePitch, positionScale); positions[index] = position; @@ -146,6 +139,87 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator upper[index] = Math.Min(floor + 1, gridSize - 1); fractions[index] = position - floor; } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static double Blend(double upperLeft, double upperRight, double lowerLeft, double lowerRight, double tx, double ty) + { + var top = upperLeft * (1 - tx) + upperRight * tx; + var bottom = lowerLeft * (1 - tx) + lowerRight * tx; + return top * (1 - ty) + bottom * ty; + } + + internal static double SampleKernelTransposed(in PsfSpecification specification, double[] intensity, Span kernel, double positionScale = 1d) + { + var gridSize = specification.PupilGridSize; + var kernelSize = specification.KernelSize; + + Span positions = stackalloc double[kernelSize]; + Span lower = stackalloc int[kernelSize]; + Span upper = stackalloc int[kernelSize]; + Span fractions = stackalloc double[kernelSize]; + PrepareSamples(in specification, positionScale, positions, lower, upper, fractions); + + for (var x = 0; x < kernelSize; x++) + { + var sampleX = positions[x]; + var insideX = sampleX >= 0 && sampleX <= gridSize - 1; + var leftColumn = lower[x] * gridSize; + var rightColumn = upper[x] * gridSize; + var tx = fractions[x]; + for (var y = 0; y < kernelSize; y++) + { + var sampleY = positions[y]; + if (!insideX || sampleY < 0 || sampleY > gridSize - 1) + { + kernel[y * kernelSize + x] = 0; + continue; + } + + var top = lower[y]; + var bottom = upper[y]; + kernel[y * kernelSize + x] = Blend( + intensity[leftColumn + top], + intensity[rightColumn + top], + intensity[leftColumn + bottom], + intensity[rightColumn + bottom], + tx, + fractions[y]); + } + } + + var first = kernelSize; + var last = -1; + for (var index = 0; index < kernelSize; index++) + { + if (positions[index] < 0 || positions[index] > gridSize - 1) + continue; + + first = Math.Min(first, index); + last = index; + } + + var rawKernelEnergy = 0d; + for (var y = first; y <= last; y++) + { + for (var x = first; x <= last; x++) + rawKernelEnergy += kernel[y * kernelSize + x]; + } + + return rawKernelEnergy; + } + + internal static double SampleKernel(in PsfSpecification specification, double[] intensity, Span kernel, double positionScale = 1d) + { + var gridSize = specification.PupilGridSize; + var kernelSize = specification.KernelSize; + var rawKernelEnergy = 0d; + + Span positions = stackalloc double[kernelSize]; + Span lower = stackalloc int[kernelSize]; + Span upper = stackalloc int[kernelSize]; + Span fractions = stackalloc double[kernelSize]; + PrepareSamples(in specification, positionScale, positions, lower, upper, fractions); for (var y = 0; y < kernelSize; y++) { @@ -171,10 +245,7 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator 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; + var value = Blend(intensity[topRow + left], intensity[topRow + right], intensity[bottomRow + left], intensity[bottomRow + right], fractions[x], ty); row[x] = value; rawKernelEnergy += value; } @@ -203,7 +274,7 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator return fullEnergy; } - static double Power(ReadOnlySpan real, ReadOnlySpan imaginary, Span intensity) + internal static double Power(ReadOnlySpan real, ReadOnlySpan imaginary, Span intensity) { var count = real.Length; ref var realBase = ref MemoryMarshal.GetReference(real);