diff --git a/SpectralConvolution/SpectralConvolution.Tests/ComplexLinesTests.cs b/SpectralConvolution/SpectralConvolution.Tests/ComplexLinesTests.cs new file mode 100644 index 0000000..a54ae16 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution.Tests/ComplexLinesTests.cs @@ -0,0 +1,68 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace SpectralConvolution.Tests; + +public sealed class ComplexLinesTests +{ + static long Distance(ComplexLines lines) + => Unsafe.ByteOffset(ref MemoryMarshal.GetReference(lines.Real), ref MemoryMarshal.GetReference(lines.Imaginary)); + + [Theory] + [InlineData(128)] + [InlineData(512)] + [InlineData(31 * 128)] + [InlineData(81 * 256)] + [InlineData(81 * 512)] + [InlineData(256 * 256)] + [InlineData(512 * 512)] + public void TheImaginaryPartStartsAtAFixedOffsetWithinTheFourKilobytePeriod(int count) + { + var lines = new ComplexLines(); + + lines.Ensure(count); + + Assert.Equal(1536L, Distance(lines) % 4096); + Assert.True(Distance(lines) >= (count + 64L) * sizeof(double), $"{Distance(lines)}"); + } + + [Fact] + public void TheTwoPartsDoNotOverlap() + { + var lines = new ComplexLines(); + lines.Ensure(1000); + + lines.Real.Fill(1d); + lines.Imaginary.Fill(2d); + + Assert.All(lines.Real.ToArray(), value => Assert.Equal(1d, value)); + Assert.All(lines.Imaginary.ToArray(), value => Assert.Equal(2d, value)); + } + + [Fact] + public void ABufferThatIsLargeEnoughIsKeptAndAGrownOneKeepsTheOffset() + { + var lines = new ComplexLines(); + lines.Ensure(100); + + lines.Ensure(50); + + Assert.Equal(100, lines.Real.Length); + Assert.Equal(100, lines.Imaginary.Length); + + lines.Ensure(4096); + + Assert.Equal(4096, lines.Real.Length); + Assert.Equal(4096, lines.Imaginary.Length); + Assert.Equal(1536L, Distance(lines) % 4096); + } + + [Fact] + public void AnUnusedBufferIsEmpty() + { + var lines = new ComplexLines(); + + Assert.True(lines.Real.IsEmpty); + Assert.True(lines.Imaginary.IsEmpty); + } +} diff --git a/SpectralConvolution/SpectralConvolution/ComplexLines.cs b/SpectralConvolution/SpectralConvolution/ComplexLines.cs new file mode 100644 index 0000000..d563378 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution/ComplexLines.cs @@ -0,0 +1,28 @@ +namespace SpectralConvolution; + +internal sealed class ComplexLines +{ + const int Period = 512; + const int Offset = 192; + const int MinimumSeparation = 64; + + double[] storage = []; + int length; + int separation; + + public Span Real => storage.AsSpan(0, length); + + public Span Imaginary => storage.AsSpan(length + separation, length); + + public void Ensure(int count) + { + if (length >= count) + return; + + length = count; + separation = (Offset - count % Period + Period) % Period; + if (separation < MinimumSeparation) + separation += Period; + storage = new double[count * 2 + separation]; + } +} diff --git a/SpectralConvolution/SpectralConvolution/KernelSpectrum.cs b/SpectralConvolution/SpectralConvolution/KernelSpectrum.cs index a929a19..b0e6b21 100644 --- a/SpectralConvolution/SpectralConvolution/KernelSpectrum.cs +++ b/SpectralConvolution/SpectralConvolution/KernelSpectrum.cs @@ -4,11 +4,9 @@ namespace SpectralConvolution; internal sealed class KernelSpectrum { + readonly ComplexLines rows = new(); + readonly ComplexLines column = new(); double[] kernel = []; - double[] rowReal = []; - double[] rowImaginary = []; - double[] columnReal = []; - double[] columnImaginary = []; Float2[] twiddles = []; Float2[] spectrum = []; @@ -47,16 +45,8 @@ internal sealed class KernelSpectrum Radius = 0; if (kernel.Length < kernelArea) kernel = new double[kernelArea]; - if (rowReal.Length < rowArea) - { - rowReal = new double[rowArea]; - rowImaginary = new double[rowArea]; - } - if (columnReal.Length < size) - { - columnReal = new double[size]; - columnImaginary = new double[size]; - } + rows.Ensure(rowArea); + column.Ensure(size); if (spectrum.Length < size * size) spectrum = new Float2[size * size]; if (twiddles.Length < size / 2) @@ -72,10 +62,12 @@ internal sealed class KernelSpectrum } var mask = size - 1; + var rowReal = rows.Real; + var rowImaginary = rows.Imaginary; for (var row = 0; row < kernelSize; row++) { - var realLine = rowReal.AsSpan(row * size, size); - var imaginaryLine = rowImaginary.AsSpan(row * size, size); + var realLine = rowReal.Slice(row * size, size); + var imaginaryLine = rowImaginary.Slice(row * size, size); realLine.Clear(); imaginaryLine.Clear(); for (var offsetX = -radius; offsetX <= radius; offsetX++) @@ -85,8 +77,8 @@ internal sealed class KernelSpectrum } var half = size / 2; - var real = columnReal.AsSpan(0, size); - var imaginary = columnImaginary.AsSpan(0, size); + var real = column.Real[..size]; + var imaginary = column.Imaginary[..size]; for (var frequencyX = 0; frequencyX <= half; frequencyX++) { real.Clear(); diff --git a/WaveOptics/Optics/FraunhoferKernelSampler.cs b/WaveOptics/Optics/FraunhoferKernelSampler.cs index 84c6b1f..927dda5 100644 --- a/WaveOptics/Optics/FraunhoferKernelSampler.cs +++ b/WaveOptics/Optics/FraunhoferKernelSampler.cs @@ -4,11 +4,9 @@ namespace WaveOptics.Optics; internal sealed class FraunhoferKernelSampler { - double[] real = []; - double[] imaginary = []; + readonly ComplexLines grid = new(); + readonly ComplexLines line = new(); double[] intensity = []; - double[] columnReal = []; - double[] columnImaginary = []; bool[] sampledColumns = []; public bool TrySample(in PsfSpecification specification, Span kernel) @@ -20,6 +18,10 @@ internal sealed class FraunhoferKernelSampler var gridSize = specification.PupilGridSize; EnsureCapacity(gridSize); + var real = grid.Real; + var imaginary = grid.Imaginary; + var columnReal = line.Real; + var columnImaginary = line.Imaginary; var center = gridSize / 2; var reach = (int)(pupilDiameter / 2d); var firstRow = Math.Max(center - reach, 0); @@ -28,7 +30,7 @@ internal sealed class FraunhoferKernelSampler return false; for (var row = firstRow; row <= lastRow; row++) - FastFourierTransform.Forward(real.AsSpan(row * gridSize, gridSize), imaginary.AsSpan(row * gridSize, gridSize)); + FastFourierTransform.Forward(real.Slice(row * gridSize, gridSize), imaginary.Slice(row * gridSize, gridSize)); MarkSampledColumns(in specification, center, positionScales); for (var shiftedColumn = 0; shiftedColumn < gridSize; shiftedColumn++) @@ -37,17 +39,17 @@ internal sealed class FraunhoferKernelSampler continue; var column = (shiftedColumn + center) % gridSize; - columnReal.AsSpan(0, firstRow).Clear(); - columnImaginary.AsSpan(0, firstRow).Clear(); + columnReal[..firstRow].Clear(); + columnImaginary[..firstRow].Clear(); for (var row = firstRow; row <= lastRow; row++) { columnReal[row] = real[row * gridSize + column]; columnImaginary[row] = imaginary[row * gridSize + column]; } - columnReal.AsSpan(lastRow + 1, gridSize - lastRow - 1).Clear(); - columnImaginary.AsSpan(lastRow + 1, gridSize - lastRow - 1).Clear(); + columnReal.Slice(lastRow + 1, gridSize - lastRow - 1).Clear(); + columnImaginary.Slice(lastRow + 1, gridSize - lastRow - 1).Clear(); - FastFourierTransform.Forward(columnReal.AsSpan(0, gridSize), columnImaginary.AsSpan(0, gridSize)); + FastFourierTransform.Forward(columnReal[..gridSize], columnImaginary[..gridSize]); var split = gridSize - center; for (var row = 0; row < split; row++) { @@ -101,17 +103,11 @@ internal sealed class FraunhoferKernelSampler void EnsureCapacity(int gridSize) { var area = gridSize * gridSize; - if (real.Length < area) - { - real = new double[area]; - imaginary = new double[area]; + grid.Ensure(area); + line.Ensure(gridSize); + if (intensity.Length < area) intensity = new double[area]; - } - if (columnReal.Length < gridSize) - { - columnReal = new double[gridSize]; - columnImaginary = new double[gridSize]; + if (sampledColumns.Length < gridSize) sampledColumns = new bool[gridSize]; - } } } diff --git a/WaveOptics/Optics/FraunhoferPsfGenerator.cs b/WaveOptics/Optics/FraunhoferPsfGenerator.cs index 02a852a..3314cc7 100644 --- a/WaveOptics/Optics/FraunhoferPsfGenerator.cs +++ b/WaveOptics/Optics/FraunhoferPsfGenerator.cs @@ -74,10 +74,10 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator } } - internal static int BuildPupil(in PsfSpecification specification, double[] real, double[] imaginary, int firstRow, int lastRow) + internal static int BuildPupil(in PsfSpecification specification, Span real, Span imaginary, int firstRow, int lastRow) => BuildPupil(in specification, specification.PupilDiameterSamples, 1d, real, imaginary, firstRow, lastRow); - internal static int BuildPupil(in PsfSpecification specification, double pupilDiameter, double phaseScale, double[] real, double[] imaginary, int firstRow, int lastRow) + internal static int BuildPupil(in PsfSpecification specification, double pupilDiameter, double phaseScale, Span real, Span imaginary, int firstRow, int lastRow) { var gridSize = specification.PupilGridSize; var center = gridSize / 2;