diff --git a/WaveOptics.Abstractions/ApertureShape.cs b/WaveOptics.Abstractions/ApertureShape.cs new file mode 100644 index 0000000..b6fe860 --- /dev/null +++ b/WaveOptics.Abstractions/ApertureShape.cs @@ -0,0 +1,7 @@ +namespace WaveOptics.Abstractions; + +public enum ApertureShape +{ + Circular, + RegularPolygon, +} diff --git a/WaveOptics.Abstractions/IPsfGenerator.cs b/WaveOptics.Abstractions/IPsfGenerator.cs new file mode 100644 index 0000000..3f084a9 --- /dev/null +++ b/WaveOptics.Abstractions/IPsfGenerator.cs @@ -0,0 +1,8 @@ +namespace WaveOptics.Abstractions; + +public interface IPsfGenerator +{ + OpticalApiVersion ApiVersion { get; } + OpticalCapabilities Capabilities { get; } + PsfGenerationResult Generate(PsfDescriptor descriptor); +} diff --git a/WaveOptics.Abstractions/OpticalApiVersion.cs b/WaveOptics.Abstractions/OpticalApiVersion.cs new file mode 100644 index 0000000..adea97a --- /dev/null +++ b/WaveOptics.Abstractions/OpticalApiVersion.cs @@ -0,0 +1,6 @@ +namespace WaveOptics.Abstractions; + +public readonly record struct OpticalApiVersion(int Major, int Minor) +{ + public static OpticalApiVersion Current { get; } = new(1, 0); +} diff --git a/WaveOptics.Abstractions/OpticalCapabilities.cs b/WaveOptics.Abstractions/OpticalCapabilities.cs new file mode 100644 index 0000000..4396f8f --- /dev/null +++ b/WaveOptics.Abstractions/OpticalCapabilities.cs @@ -0,0 +1,13 @@ +namespace WaveOptics.Abstractions; + +[Flags] +public enum OpticalCapabilities +{ + None = 0, + MonochromaticPsf = 1, + CircularAperture = 2, + RegularPolygonAperture = 4, + CentralObstruction = 8, + ZernikeAberration = 16, + DirectConvolution = 32, +} diff --git a/WaveOptics.Abstractions/PsfDescriptor.cs b/WaveOptics.Abstractions/PsfDescriptor.cs new file mode 100644 index 0000000..8f524bc --- /dev/null +++ b/WaveOptics.Abstractions/PsfDescriptor.cs @@ -0,0 +1,96 @@ +using System.Numerics; + +namespace WaveOptics.Abstractions; + +public sealed class PsfDescriptor : IEquatable +{ + public int PupilGridSize { get; } + public int PupilDiameterSamples { get; } + public int KernelSize { get; } + public double WavelengthNanometers { get; } + public double FNumber { get; } + public double SensorPixelPitchMicrometers { get; } + public ApertureShape ApertureShape { get; } + public int BladeCount { get; } + public double BladeRotationDegrees { get; } + public double CentralObstructionRatio { get; } + public WavefrontAberration Aberration { get; } + + public PsfDescriptor( + int pupilGridSize, + int pupilDiameterSamples, + int kernelSize, + double wavelengthNanometers, + double fNumber, + double sensorPixelPitchMicrometers, + ApertureShape apertureShape, + int bladeCount, + double bladeRotationDegrees, + double centralObstructionRatio, + WavefrontAberration aberration) + { + ArgumentOutOfRangeException.ThrowIfLessThan(pupilGridSize, 64); + ArgumentOutOfRangeException.ThrowIfGreaterThan(pupilGridSize, 2048); + if (!BitOperations.IsPow2((uint)pupilGridSize)) + throw new ArgumentException(nameof(pupilGridSize)); + ArgumentOutOfRangeException.ThrowIfLessThan(pupilDiameterSamples, 16); + ArgumentOutOfRangeException.ThrowIfGreaterThan(pupilDiameterSamples, pupilGridSize / 2); + ArgumentOutOfRangeException.ThrowIfLessThan(kernelSize, 3); + ArgumentOutOfRangeException.ThrowIfGreaterThan(kernelSize, 255); + if ((kernelSize & 1) == 0) + throw new ArgumentException(nameof(kernelSize)); + ValidateFiniteRange(wavelengthNanometers, 380, 780, nameof(wavelengthNanometers)); + ValidateFiniteRange(fNumber, 0.5, 64, nameof(fNumber)); + ValidateFiniteRange(sensorPixelPitchMicrometers, 0.25, 100, nameof(sensorPixelPitchMicrometers)); + if (!Enum.IsDefined(apertureShape)) + throw new ArgumentOutOfRangeException(nameof(apertureShape)); + ArgumentOutOfRangeException.ThrowIfLessThan(bladeCount, 3); + ArgumentOutOfRangeException.ThrowIfGreaterThan(bladeCount, 32); + ValidateFiniteRange(bladeRotationDegrees, -360, 360, nameof(bladeRotationDegrees)); + ValidateFiniteRange(centralObstructionRatio, 0, 0.95, nameof(centralObstructionRatio)); + + PupilGridSize = pupilGridSize; + PupilDiameterSamples = pupilDiameterSamples; + KernelSize = kernelSize; + WavelengthNanometers = wavelengthNanometers; + FNumber = fNumber; + SensorPixelPitchMicrometers = sensorPixelPitchMicrometers; + ApertureShape = apertureShape; + BladeCount = bladeCount; + BladeRotationDegrees = bladeRotationDegrees; + CentralObstructionRatio = centralObstructionRatio; + Aberration = aberration; + } + + static void ValidateFiniteRange(double value, double minimum, double maximum, string name) + { + ArgumentOutOfRangeException.ThrowIfNotEqual(double.IsFinite(value), true, name); + ArgumentOutOfRangeException.ThrowIfLessThan(value, minimum, name); + ArgumentOutOfRangeException.ThrowIfGreaterThan(value, maximum, name); + } + + public bool Equals(PsfDescriptor? other) + { + return other is not null + && PupilGridSize == other.PupilGridSize + && PupilDiameterSamples == other.PupilDiameterSamples + && KernelSize == other.KernelSize + && WavelengthNanometers.Equals(other.WavelengthNanometers) + && FNumber.Equals(other.FNumber) + && SensorPixelPitchMicrometers.Equals(other.SensorPixelPitchMicrometers) + && ApertureShape == other.ApertureShape + && BladeCount == other.BladeCount + && BladeRotationDegrees.Equals(other.BladeRotationDegrees) + && CentralObstructionRatio.Equals(other.CentralObstructionRatio) + && Aberration.Equals(other.Aberration); + } + + public override bool Equals(object? obj) => Equals(obj as PsfDescriptor); + + public override int GetHashCode() + { + var first = HashCode.Combine(PupilGridSize, PupilDiameterSamples, KernelSize, WavelengthNanometers, FNumber, SensorPixelPitchMicrometers); + var second = HashCode.Combine(ApertureShape, BladeCount, BladeRotationDegrees, CentralObstructionRatio, Aberration); + return HashCode.Combine(first, second); + } +} diff --git a/WaveOptics.Abstractions/PsfDiagnostics.cs b/WaveOptics.Abstractions/PsfDiagnostics.cs new file mode 100644 index 0000000..172ce69 --- /dev/null +++ b/WaveOptics.Abstractions/PsfDiagnostics.cs @@ -0,0 +1,7 @@ +namespace WaveOptics.Abstractions; + +public readonly record struct PsfDiagnostics( + int OpenPupilSampleCount, + double FocalPlaneSamplePitchMicrometers, + double UnnormalizedKernelEnergy, + double PeakIntensity); diff --git a/WaveOptics.Abstractions/PsfGenerationResult.cs b/WaveOptics.Abstractions/PsfGenerationResult.cs new file mode 100644 index 0000000..08eb3da --- /dev/null +++ b/WaveOptics.Abstractions/PsfGenerationResult.cs @@ -0,0 +1,3 @@ +namespace WaveOptics.Abstractions; + +public sealed record PsfGenerationResult(PsfKernel Kernel, PsfDiagnostics Diagnostics); diff --git a/WaveOptics.Abstractions/PsfKernel.cs b/WaveOptics.Abstractions/PsfKernel.cs new file mode 100644 index 0000000..6efd911 --- /dev/null +++ b/WaveOptics.Abstractions/PsfKernel.cs @@ -0,0 +1,63 @@ +namespace WaveOptics.Abstractions; + +public sealed class PsfKernel +{ + readonly double[] values; + + public int Size { get; } + public ReadOnlyMemory Values => values; + + public double this[int x, int y] + { + get + { + ArgumentOutOfRangeException.ThrowIfNegative(x); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(x, Size); + ArgumentOutOfRangeException.ThrowIfNegative(y); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(y, Size); + return values[y * Size + x]; + } + } + + public PsfKernel(int size, ReadOnlySpan values) + { + ArgumentOutOfRangeException.ThrowIfLessThan(size, 1); + if ((size & 1) == 0) + throw new ArgumentException(nameof(size)); + if (values.Length != size * size) + throw new ArgumentException(nameof(values)); + + this.values = values.ToArray(); + for (var index = 0; index < this.values.Length; index++) + { + var value = this.values[index]; + if (!double.IsFinite(value) || value < 0) + throw new ArgumentOutOfRangeException(nameof(values)); + } + + var sum = this.values.Sum(); + if (!double.IsFinite(sum) || sum <= 0) + throw new ArgumentOutOfRangeException(nameof(values)); + + for (var index = 0; index < this.values.Length; index++) + this.values[index] /= sum; + + Size = size; + } + + public double Sum() + { + var sum = 0d; + var compensation = 0d; + foreach (var value in values) + { + var corrected = value - compensation; + var next = sum + corrected; + compensation = next - sum - corrected; + sum = next; + } + return sum; + } + + public double[] ToArray() => (double[])values.Clone(); +} diff --git a/WaveOptics.Abstractions/WaveOptics.Abstractions.csproj b/WaveOptics.Abstractions/WaveOptics.Abstractions.csproj index b760144..10a44df 100644 --- a/WaveOptics.Abstractions/WaveOptics.Abstractions.csproj +++ b/WaveOptics.Abstractions/WaveOptics.Abstractions.csproj @@ -1,9 +1,12 @@ - + net10.0 enable enable + WaveOptics.Abstractions + WaveOptics.Abstractions + 1.0.0 diff --git a/WaveOptics.Abstractions/WavefrontAberration.cs b/WaveOptics.Abstractions/WavefrontAberration.cs new file mode 100644 index 0000000..fa2dbb8 --- /dev/null +++ b/WaveOptics.Abstractions/WavefrontAberration.cs @@ -0,0 +1,65 @@ +namespace WaveOptics.Abstractions; + +public readonly struct WavefrontAberration : IEquatable +{ + public double DefocusWaves { get; } + public double AstigmatismVerticalWaves { get; } + public double AstigmatismObliqueWaves { get; } + public double ComaHorizontalWaves { get; } + public double ComaVerticalWaves { get; } + public double TrefoilHorizontalWaves { get; } + public double TrefoilVerticalWaves { get; } + public double SphericalWaves { get; } + + public WavefrontAberration( + double defocusWaves = 0, + double astigmatismVerticalWaves = 0, + double astigmatismObliqueWaves = 0, + double comaHorizontalWaves = 0, + double comaVerticalWaves = 0, + double trefoilHorizontalWaves = 0, + double trefoilVerticalWaves = 0, + double sphericalWaves = 0) + { + DefocusWaves = Validate(defocusWaves, nameof(defocusWaves)); + AstigmatismVerticalWaves = Validate(astigmatismVerticalWaves, nameof(astigmatismVerticalWaves)); + AstigmatismObliqueWaves = Validate(astigmatismObliqueWaves, nameof(astigmatismObliqueWaves)); + ComaHorizontalWaves = Validate(comaHorizontalWaves, nameof(comaHorizontalWaves)); + ComaVerticalWaves = Validate(comaVerticalWaves, nameof(comaVerticalWaves)); + TrefoilHorizontalWaves = Validate(trefoilHorizontalWaves, nameof(trefoilHorizontalWaves)); + TrefoilVerticalWaves = Validate(trefoilVerticalWaves, nameof(trefoilVerticalWaves)); + SphericalWaves = Validate(sphericalWaves, nameof(sphericalWaves)); + } + + static double Validate(double value, string name) + { + ArgumentOutOfRangeException.ThrowIfNotEqual(double.IsFinite(value), true, name); + ArgumentOutOfRangeException.ThrowIfLessThan(value, -10, name); + ArgumentOutOfRangeException.ThrowIfGreaterThan(value, 10, name); + return value; + } + + public bool Equals(WavefrontAberration other) + { + return DefocusWaves.Equals(other.DefocusWaves) + && AstigmatismVerticalWaves.Equals(other.AstigmatismVerticalWaves) + && AstigmatismObliqueWaves.Equals(other.AstigmatismObliqueWaves) + && ComaHorizontalWaves.Equals(other.ComaHorizontalWaves) + && ComaVerticalWaves.Equals(other.ComaVerticalWaves) + && TrefoilHorizontalWaves.Equals(other.TrefoilHorizontalWaves) + && TrefoilVerticalWaves.Equals(other.TrefoilVerticalWaves) + && SphericalWaves.Equals(other.SphericalWaves); + } + + public override bool Equals(object? obj) => obj is WavefrontAberration other && Equals(other); + + public override int GetHashCode() + { + var first = HashCode.Combine(DefocusWaves, AstigmatismVerticalWaves, AstigmatismObliqueWaves, ComaHorizontalWaves); + var second = HashCode.Combine(ComaVerticalWaves, TrefoilHorizontalWaves, TrefoilVerticalWaves, SphericalWaves); + return HashCode.Combine(first, second); + } + + public static bool operator ==(WavefrontAberration left, WavefrontAberration right) => left.Equals(right); + public static bool operator !=(WavefrontAberration left, WavefrontAberration right) => !left.Equals(right); +}