diff --git a/WaveOptics/Effects/WaveOpticsEffectProcessor.cs b/WaveOptics/Effects/WaveOpticsEffectProcessor.cs index fc4eb2e..e86e6ec 100644 --- a/WaveOptics/Effects/WaveOpticsEffectProcessor.cs +++ b/WaveOptics/Effects/WaveOpticsEffectProcessor.cs @@ -138,7 +138,7 @@ internal sealed class WaveOpticsEffectProcessor : VideoEffectProcessorBase itemHeight, in parameters); - if (!_pipeline.TryGetVisibleBounds(canvasWidth, canvasHeight, in parameters, out var rect)) + if (!_pipeline.HasKernel || !_pipeline.TryGetVisibleBounds(canvasWidth, canvasHeight, in parameters, out var rect)) { _effect.Amount = 0f; _amount = amount; diff --git a/WaveOptics/Optics/FraunhoferPsfGenerator.cs b/WaveOptics/Optics/FraunhoferPsfGenerator.cs index 031b359..e1a2fb3 100644 --- a/WaveOptics/Optics/FraunhoferPsfGenerator.cs +++ b/WaveOptics/Optics/FraunhoferPsfGenerator.cs @@ -1,4 +1,5 @@ using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -21,9 +22,17 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator | OpticalCapabilities.DirectConvolution; public PsfGenerationResult Generate(PsfDescriptor descriptor) + { + if (!TryGenerate(descriptor, out var result)) + throw new InvalidOperationException(); + return result; + } + + public bool TryGenerate(PsfDescriptor descriptor, [NotNullWhen(true)] out PsfGenerationResult? result) { ArgumentNullException.ThrowIfNull(descriptor); + result = null; var gridSize = descriptor.PupilGridSize; var area = gridSize * gridSize; var real = ArrayPool.Shared.Rent(area); @@ -33,13 +42,13 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator { var openSampleCount = BuildPupil(descriptor, real, imaginary); if (openSampleCount == 0) - throw new InvalidOperationException(); + return false; FastFourierTransform.Forward2D(real, imaginary, gridSize, gridSize); var fullEnergy = ComputeShiftedIntensity(real, imaginary, intensity, gridSize); if (!double.IsFinite(fullEnergy) || fullEnergy <= 0) - throw new InvalidOperationException(); + return false; var wavelengthMicrometers = descriptor.WavelengthNanometers / 1000d; var focalPlaneSamplePitch = wavelengthMicrometers * descriptor.FNumber * descriptor.PupilDiameterSamples / gridSize; @@ -62,7 +71,7 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator } if (!double.IsFinite(rawKernelEnergy) || rawKernelEnergy <= 0) - throw new InvalidOperationException(); + return false; var psfKernel = new PsfKernel(kernelSize, kernel); var diagnostics = new PsfDiagnostics( @@ -70,7 +79,8 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator focalPlaneSamplePitch, rawKernelEnergy / fullEnergy, Peak(psfKernel.Values.Span)); - return new PsfGenerationResult(psfKernel, diagnostics); + result = new PsfGenerationResult(psfKernel, diagnostics); + return true; } finally { diff --git a/WaveOptics/Rendering/WaveOpticsPipeline.cs b/WaveOptics/Rendering/WaveOpticsPipeline.cs index becf5ec..29cdbb9 100644 --- a/WaveOptics/Rendering/WaveOpticsPipeline.cs +++ b/WaveOptics/Rendering/WaveOpticsPipeline.cs @@ -176,11 +176,19 @@ internal sealed class WaveOpticsPipeline : IDisposable return false; } - EnsureKernel(parameters.Psf); + if (!TryEnsureKernel(parameters.Psf)) + { + _convolutionKey = null; + rect = default; + return false; + } + _convolutionKey = key; return true; } + internal bool HasKernel => _kernel.Rank > 0; + internal bool TryGetVisibleBounds(int canvasWidth, int canvasHeight, in Parameters parameters, out PixelRect rect) { rect = default; @@ -219,16 +227,17 @@ internal sealed class WaveOpticsPipeline : IDisposable in Parameters parameters) { EnsureCanvas(width, height); - EnsureKernel(parameters.Psf); + if (!TryEnsureKernel(parameters.Psf)) + throw new InvalidOperationException(); _convolutionKey = null; var sourceRect = new PixelRect(0, 0, width, height); return _host.RecordFullPipeline(source, output, _weights, in sourceRect, in _kernel, parameters.Gain); } - private void EnsureKernel(in PsfParameters psf) + private bool TryEnsureKernel(in PsfParameters psf) { if (_kernelPsf == psf) - return; + return HasKernel; var pupilGridSize = WaveOpticsSettings.GetPupilGridSize(psf.Quality); var aberration = new WavefrontAberration( @@ -250,7 +259,13 @@ internal sealed class WaveOpticsPipeline : IDisposable psf.BladeRotation, psf.Obstruction, aberration); - var result = _generator.Generate(descriptor); + if (!_generator.TryGenerate(descriptor, out var result)) + { + _kernel = default; + _kernelPsf = psf; + return false; + } + var separable = SeparableKernel.Decompose(result.Kernel.Values.Span, result.Kernel.Size, WaveOpticsSettings.SeparableResidualRatio, WaveOpticsSettings.MaximumRank); var size = separable.Size; @@ -265,6 +280,7 @@ internal sealed class WaveOpticsPipeline : IDisposable _weights.CopyFrom(_weightValues); _kernel = new Kernel(separable.Rank, size / 2); _kernelPsf = psf; + return true; } private void EnsureCanvas(int canvasWidth, int canvasHeight)