From 1cc18c8fe51e527e80a088182fbf00a1b4ee20d8 Mon Sep 17 00:00:00 2001 From: routersys Date: Tue, 23 Jun 2026 08:40:38 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=95=E3=83=BC=E3=83=AA=E3=82=A8=E5=A4=89?= =?UTF-8?q?=E6=8F=9B=E3=81=A8=E3=82=BC=E3=83=AB=E3=83=8B=E3=82=B1=E6=B3=A2?= =?UTF-8?q?=E9=9D=A2=E3=82=92=E7=94=A8=E3=81=84=E3=81=9F=E3=83=95=E3=83=A9?= =?UTF-8?q?=E3=82=A6=E3=83=B3=E3=83=9B=E3=83=BC=E3=83=95=E3=82=A1=E3=83=BC?= =?UTF-8?q?PSF=E7=94=9F=E6=88=90=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Wave Optics/Fourier/FastFourierTransform.cs | 92 ++++++++++++ Wave Optics/Optics/FraunhoferPsfGenerator.cs | 139 +++++++++++++++++++ Wave Optics/Optics/ZernikeWavefront.cs | 34 +++++ 3 files changed, 265 insertions(+) create mode 100644 Wave Optics/Fourier/FastFourierTransform.cs create mode 100644 Wave Optics/Optics/FraunhoferPsfGenerator.cs create mode 100644 Wave Optics/Optics/ZernikeWavefront.cs diff --git a/Wave Optics/Fourier/FastFourierTransform.cs b/Wave Optics/Fourier/FastFourierTransform.cs new file mode 100644 index 0000000..562f6a2 --- /dev/null +++ b/Wave Optics/Fourier/FastFourierTransform.cs @@ -0,0 +1,92 @@ +using System.Numerics; + +namespace WaveOptics.Fourier; + +internal static class FastFourierTransform +{ + public static void Forward(Span values) => Transform(values, false); + + public static void Inverse(Span values) => Transform(values, true); + + public static void Forward2D(Complex[] values, int width, int height) => Transform2D(values, width, height, false); + + public static void Inverse2D(Complex[] values, int width, int height) => Transform2D(values, width, height, true); + + static void Transform(Span values, bool inverse) + { + var count = values.Length; + if (count == 0 || !BitOperations.IsPow2((uint)count)) + throw new ArgumentException(nameof(values)); + + var target = 0; + for (var source = 1; source < count; source++) + { + var bit = count >> 1; + while ((target & bit) != 0) + { + target ^= bit; + bit >>= 1; + } + target ^= bit; + if (source < target) + (values[source], values[target]) = (values[target], values[source]); + } + + for (var length = 2; length <= count; length <<= 1) + { + var angle = (inverse ? 2d : -2d) * Math.PI / length; + var step = Complex.FromPolarCoordinates(1d, angle); + var half = length >> 1; + for (var offset = 0; offset < count; offset += length) + { + var factor = Complex.One; + for (var index = 0; index < half; index++) + { + var even = values[offset + index]; + var odd = values[offset + index + half] * factor; + values[offset + index] = even + odd; + values[offset + index + half] = even - odd; + factor *= step; + } + } + } + + if (!inverse) + return; + + var scale = 1d / count; + for (var index = 0; index < count; index++) + values[index] *= scale; + } + + static void Transform2D(Complex[] values, int width, int height, bool inverse) + { + ArgumentNullException.ThrowIfNull(values); + if (values.Length != width * height) + throw new ArgumentException(nameof(values)); + if (!BitOperations.IsPow2((uint)width) || !BitOperations.IsPow2((uint)height)) + throw new ArgumentException(nameof(width)); + + for (var y = 0; y < height; y++) + { + var row = values.AsSpan(y * width, width); + if (inverse) + Inverse(row); + else + Forward(row); + } + + var column = new Complex[height]; + for (var x = 0; x < width; x++) + { + for (var y = 0; y < height; y++) + column[y] = values[y * width + x]; + if (inverse) + Inverse(column); + else + Forward(column); + for (var y = 0; y < height; y++) + values[y * width + x] = column[y]; + } + } +} diff --git a/Wave Optics/Optics/FraunhoferPsfGenerator.cs b/Wave Optics/Optics/FraunhoferPsfGenerator.cs new file mode 100644 index 0000000..9aaa660 --- /dev/null +++ b/Wave Optics/Optics/FraunhoferPsfGenerator.cs @@ -0,0 +1,139 @@ +using System.Numerics; +using WaveOptics.Abstractions; +using WaveOptics.Fourier; + +namespace WaveOptics.Optics; + +public sealed class FraunhoferPsfGenerator : IPsfGenerator +{ + const double TwoPi = 2 * Math.PI; + + public OpticalApiVersion ApiVersion => OpticalApiVersion.Current; + + public OpticalCapabilities Capabilities => OpticalCapabilities.MonochromaticPsf + | OpticalCapabilities.CircularAperture + | OpticalCapabilities.RegularPolygonAperture + | OpticalCapabilities.CentralObstruction + | OpticalCapabilities.ZernikeAberration + | OpticalCapabilities.DirectConvolution; + + public PsfGenerationResult Generate(PsfDescriptor descriptor) + { + ArgumentNullException.ThrowIfNull(descriptor); + + var gridSize = descriptor.PupilGridSize; + var pupilDiameter = descriptor.PupilDiameterSamples; + var pupil = new Complex[gridSize * gridSize]; + var center = gridSize / 2; + var pupilRadius = pupilDiameter / 2d; + var rotation = descriptor.BladeRotationDegrees * Math.PI / 180d; + var openSampleCount = 0; + + for (var y = 0; y < gridSize; y++) + { + var normalizedY = (y - center) / pupilRadius; + for (var x = 0; x < gridSize; x++) + { + var normalizedX = (x - center) / pupilRadius; + if (!IsInsideAperture(normalizedX, normalizedY, descriptor, rotation)) + continue; + + var waves = ZernikeWavefront.Evaluate(normalizedX, normalizedY, descriptor.Aberration); + pupil[y * gridSize + x] = Complex.FromPolarCoordinates(1d, TwoPi * waves); + openSampleCount++; + } + } + + if (openSampleCount == 0) + throw new InvalidOperationException(); + + FastFourierTransform.Forward2D(pupil, gridSize, gridSize); + var intensity = new double[pupil.Length]; + var fullEnergy = 0d; + for (var y = 0; y < gridSize; y++) + { + var sourceY = (y + center) % gridSize; + for (var x = 0; x < gridSize; x++) + { + var sourceX = (x + center) % gridSize; + var value = pupil[sourceY * gridSize + sourceX].Magnitude; + value *= value; + intensity[y * gridSize + x] = value; + fullEnergy += value; + } + } + + if (!double.IsFinite(fullEnergy) || fullEnergy <= 0) + throw new InvalidOperationException(); + + var inverseEnergy = 1d / fullEnergy; + for (var index = 0; index < intensity.Length; index++) + intensity[index] *= inverseEnergy; + + var wavelengthMicrometers = descriptor.WavelengthNanometers / 1000d; + var focalPlaneSamplePitch = wavelengthMicrometers * descriptor.FNumber * pupilDiameter / gridSize; + var kernel = new double[descriptor.KernelSize * descriptor.KernelSize]; + var kernelRadius = descriptor.KernelSize / 2; + var unnormalizedKernelEnergy = 0d; + + for (var y = 0; y < descriptor.KernelSize; y++) + { + var physicalY = (y - kernelRadius) * descriptor.SensorPixelPitchMicrometers; + var sampleY = center + physicalY / focalPlaneSamplePitch; + for (var x = 0; x < descriptor.KernelSize; x++) + { + var physicalX = (x - kernelRadius) * descriptor.SensorPixelPitchMicrometers; + var sampleX = center + physicalX / focalPlaneSamplePitch; + var value = SampleBilinear(intensity, gridSize, sampleX, sampleY); + kernel[y * descriptor.KernelSize + x] = value; + unnormalizedKernelEnergy += value; + } + } + + if (!double.IsFinite(unnormalizedKernelEnergy) || unnormalizedKernelEnergy <= 0) + throw new InvalidOperationException(); + + var psfKernel = new PsfKernel(descriptor.KernelSize, kernel); + var diagnostics = new PsfDiagnostics( + openSampleCount, + focalPlaneSamplePitch, + unnormalizedKernelEnergy, + psfKernel.Values.Span.ToArray().Max()); + return new PsfGenerationResult(psfKernel, diagnostics); + } + + static bool IsInsideAperture(double x, double y, PsfDescriptor descriptor, double rotation) + { + var radiusSquared = x * x + y * y; + var obstructionSquared = descriptor.CentralObstructionRatio * descriptor.CentralObstructionRatio; + if (radiusSquared > 1d || radiusSquared < obstructionSquared) + return false; + if (descriptor.ApertureShape == ApertureShape.Circular) + return true; + + var radius = Math.Sqrt(radiusSquared); + if (radius == 0) + return descriptor.CentralObstructionRatio == 0; + var sector = TwoPi / descriptor.BladeCount; + var angle = Math.Atan2(y, x) - rotation; + var folded = angle - sector * Math.Round(angle / sector); + var boundary = Math.Cos(Math.PI / descriptor.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; + } +} diff --git a/Wave Optics/Optics/ZernikeWavefront.cs b/Wave Optics/Optics/ZernikeWavefront.cs new file mode 100644 index 0000000..f53265e --- /dev/null +++ b/Wave Optics/Optics/ZernikeWavefront.cs @@ -0,0 +1,34 @@ +using WaveOptics.Abstractions; + +namespace WaveOptics.Optics; + +internal static class ZernikeWavefront +{ + static readonly double Sqrt3 = Math.Sqrt(3); + static readonly double Sqrt5 = Math.Sqrt(5); + static readonly double Sqrt6 = Math.Sqrt(6); + static readonly double Sqrt8 = Math.Sqrt(8); + + public static double Evaluate(double x, double y, WavefrontAberration aberration) + { + var radiusSquared = x * x + y * y; + var radiusFourth = radiusSquared * radiusSquared; + var defocus = Sqrt3 * (2 * radiusSquared - 1); + var astigmatismVertical = Sqrt6 * (x * x - y * y); + var astigmatismOblique = 2 * Sqrt6 * x * y; + var comaHorizontal = Sqrt8 * (3 * radiusSquared - 2) * x; + var comaVertical = Sqrt8 * (3 * radiusSquared - 2) * y; + var trefoilHorizontal = Sqrt8 * x * (x * x - 3 * y * y); + var trefoilVertical = Sqrt8 * y * (3 * x * x - y * y); + var spherical = Sqrt5 * (6 * radiusFourth - 6 * radiusSquared + 1); + + return aberration.DefocusWaves * defocus + + aberration.AstigmatismVerticalWaves * astigmatismVertical + + aberration.AstigmatismObliqueWaves * astigmatismOblique + + aberration.ComaHorizontalWaves * comaHorizontal + + aberration.ComaVerticalWaves * comaVertical + + aberration.TrefoilHorizontalWaves * trefoilHorizontal + + aberration.TrefoilVerticalWaves * trefoilVertical + + aberration.SphericalWaves * spherical; + } +}