点像の標本化で位置の計算を列と行に分ける

・光学系を変えたフレームの点像分布関数の計算を高速化-M
このコミットが含まれているのは:
routersys committed 2026-10-07 17:59:33 +09:00
1 親 0caa4699a2
コミット a645d93ab3
2ファイル変更
+108 -20

検索条件に一致するファイルはありません

+66
ファイルの表示
@@ -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);
}
}
+42 -20
ファイルの表示
@@ -130,14 +130,52 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator
var kernelRadius = kernelSize / 2;
var rawKernelEnergy = 0d;
Span<double> positions = stackalloc double[kernelSize];
Span<int> lower = stackalloc int[kernelSize];
Span<int> upper = stackalloc int[kernelSize];
Span<double> 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;
}
}