色ごとの波長の帯から点像を求める部品を加える
このコミットが含まれているのは:
4ファイル変更
+495
検索条件に一致するファイルはありません
@@ -0,0 +1,252 @@
|
||||
using WaveOptics.Abstractions;
|
||||
using WaveOptics.Effects;
|
||||
using WaveOptics.Optics;
|
||||
|
||||
namespace WaveOptics.Tests.Psf;
|
||||
|
||||
public sealed class ChromaticKernelSamplerTests
|
||||
{
|
||||
readonly FraunhoferKernelSampler single = new();
|
||||
readonly ChromaticKernelSampler sampler = new();
|
||||
|
||||
static PsfSpecification Specification(
|
||||
int gridSize = 256,
|
||||
int kernelRadius = 15,
|
||||
double fNumber = 8,
|
||||
double pixelPitch = 4,
|
||||
ApertureShape shape = ApertureShape.Circular,
|
||||
int blades = 6,
|
||||
double obstruction = 0,
|
||||
WavefrontAberration aberration = default,
|
||||
double wavelength = SpectralPlan.ReferenceWavelength)
|
||||
=> new(gridSize, gridSize / 4, kernelRadius * 2 + 1, wavelength, fNumber, pixelPitch, shape, blades, 0, obstruction, aberration);
|
||||
|
||||
static double Radius(ReadOnlySpan<double> kernel, double fraction)
|
||||
{
|
||||
var size = (int)Math.Sqrt(kernel.Length);
|
||||
var center = size / 2;
|
||||
var pairs = new List<(double Distance, double Value)>();
|
||||
for (var index = 0; index < kernel.Length; index++)
|
||||
pairs.Add((Math.Sqrt(Math.Pow(index % size - center, 2) + Math.Pow(index / size - center, 2)), kernel[index]));
|
||||
pairs.Sort((left, right) => left.Distance.CompareTo(right.Distance));
|
||||
var total = 0d;
|
||||
foreach (var (distance, value) in pairs)
|
||||
{
|
||||
total += value;
|
||||
if (total >= fraction)
|
||||
return distance;
|
||||
}
|
||||
|
||||
return pairs[^1].Distance;
|
||||
}
|
||||
|
||||
static double Distance(ReadOnlySpan<double> left, ReadOnlySpan<double> right)
|
||||
{
|
||||
var sum = 0d;
|
||||
for (var index = 0; index < left.Length; index++)
|
||||
sum += Math.Abs(left[index] - right[index]);
|
||||
return sum;
|
||||
}
|
||||
|
||||
(double[] Red, double[] Green, double[] Blue) Sample(in PsfSpecification specification, WaveOpticsColorMode mode, WaveOpticsQuality quality)
|
||||
{
|
||||
var area = specification.KernelSize * specification.KernelSize;
|
||||
var red = new double[area];
|
||||
var green = new double[area];
|
||||
var blue = new double[area];
|
||||
Assert.True(sampler.TrySample(in specification, mode, quality, red, green, blue));
|
||||
return (red, green, blue);
|
||||
}
|
||||
|
||||
public static TheoryData<string> CaseNames => ["clear", "defocus", "coma", "polygon", "obstructed", "astigmatism"];
|
||||
|
||||
static PsfSpecification Case(string name)
|
||||
=> name switch
|
||||
{
|
||||
"clear" => Specification(),
|
||||
"defocus" => Specification(aberration: new WavefrontAberration(defocusWaves: 1.5)),
|
||||
"coma" => Specification(kernelRadius: 30, aberration: new WavefrontAberration(comaHorizontalWaves: 1.2, sphericalWaves: -0.4)),
|
||||
"polygon" => Specification(shape: ApertureShape.RegularPolygon, blades: 7, fNumber: 16, pixelPitch: 2),
|
||||
"obstructed" => Specification(gridSize: 512, obstruction: 0.5, aberration: new WavefrontAberration(defocusWaves: 2)),
|
||||
_ => Specification(aberration: new WavefrontAberration(astigmatismVerticalWaves: 0.8, astigmatismObliqueWaves: -0.6)),
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CaseNames))]
|
||||
public void TheGreenKernelOfThePrimariesIsTheMonochromeKernelAtTheReferenceWavelength(string name)
|
||||
{
|
||||
var specification = Case(name);
|
||||
var expected = new double[specification.KernelSize * specification.KernelSize];
|
||||
Assert.True(single.TrySample(in specification, expected));
|
||||
|
||||
var (_, green, _) = Sample(in specification, WaveOpticsColorMode.Primaries, WaveOpticsQuality.Standard);
|
||||
|
||||
Assert.Equal(expected, green);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CaseNames))]
|
||||
public void EveryChannelOfEveryModeIsANormalizedNonNegativeKernel(string name)
|
||||
{
|
||||
var specification = Case(name);
|
||||
|
||||
foreach (var mode in new[] { WaveOpticsColorMode.Primaries, WaveOpticsColorMode.Broadband })
|
||||
{
|
||||
foreach (var quality in Enum.GetValues<WaveOpticsQuality>())
|
||||
{
|
||||
var (red, green, blue) = Sample(in specification, mode, quality);
|
||||
|
||||
foreach (var kernel in new[] { red, green, blue })
|
||||
{
|
||||
Assert.Equal(1d, kernel.Sum(), 9);
|
||||
Assert.All(kernel, value => Assert.True(value >= 0d && double.IsFinite(value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AGeometricBlurHasTheSameSizeInEveryChannelWhereAFixedNumberOfWavesDoesNot()
|
||||
{
|
||||
var aberration = new WavefrontAberration(defocusWaves: 3);
|
||||
var specification = Specification(gridSize: 512, kernelRadius: 63, aberration: aberration);
|
||||
|
||||
var (red, green, blue) = Sample(in specification, WaveOpticsColorMode.Primaries, WaveOpticsQuality.High);
|
||||
|
||||
var chromaticRatio = Radius(red, 0.9) / Radius(blue, 0.9);
|
||||
Assert.InRange(chromaticRatio, 0.94, 1.06);
|
||||
Assert.InRange(Radius(green, 0.9) / Radius(blue, 0.9), 0.94, 1.06);
|
||||
var redWaves = new double[red.Length];
|
||||
var blueWaves = new double[red.Length];
|
||||
var redSpecification = Specification(gridSize: 512, kernelRadius: 63, aberration: aberration, wavelength: SpectralPlan.RedWavelength);
|
||||
var blueSpecification = Specification(gridSize: 512, kernelRadius: 63, aberration: aberration, wavelength: SpectralPlan.BlueWavelength);
|
||||
Assert.True(single.TrySample(in redSpecification, redWaves));
|
||||
Assert.True(single.TrySample(in blueSpecification, blueWaves));
|
||||
Assert.True(Radius(redWaves, 0.9) / Radius(blueWaves, 0.9) > 1.2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithoutAberrationTheDiffractionPatternGrowsWithTheWavelength()
|
||||
{
|
||||
var specification = Specification(kernelRadius: 40, fNumber: 16, pixelPitch: 2);
|
||||
|
||||
var (red, green, blue) = Sample(in specification, WaveOpticsColorMode.Primaries, WaveOpticsQuality.Standard);
|
||||
|
||||
var redPeak = red.Max();
|
||||
var greenPeak = green.Max();
|
||||
var bluePeak = blue.Max();
|
||||
Assert.True(redPeak < greenPeak && greenPeak < bluePeak, $"{redPeak} {greenPeak} {bluePeak}");
|
||||
var expected = Math.Pow(SpectralPlan.BlueWavelength / SpectralPlan.RedWavelength, 2);
|
||||
Assert.InRange(redPeak / bluePeak, expected * 0.85, expected * 1.15);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsColorMode.Primaries, 1e-6)]
|
||||
[InlineData(WaveOpticsColorMode.Broadband, 0.05)]
|
||||
public void AVanishingAberrationGivesTheKernelOfNoAberration(WaveOpticsColorMode mode, double tolerance)
|
||||
{
|
||||
var clear = Specification(kernelRadius: 30);
|
||||
var nearlyClear = Specification(kernelRadius: 30, aberration: new WavefrontAberration(defocusWaves: 1e-9));
|
||||
|
||||
var (clearRed, clearGreen, clearBlue) = Sample(in clear, mode, WaveOpticsQuality.Standard);
|
||||
var (nearRed, nearGreen, nearBlue) = Sample(in nearlyClear, mode, WaveOpticsQuality.High);
|
||||
|
||||
Assert.True(Distance(clearRed, nearRed) < tolerance, $"{Distance(clearRed, nearRed)}");
|
||||
Assert.True(Distance(clearGreen, nearGreen) < tolerance, $"{Distance(clearGreen, nearGreen)}");
|
||||
Assert.True(Distance(clearBlue, nearBlue) < tolerance, $"{Distance(clearBlue, nearBlue)}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithoutAberrationTheQualityOnlyChangesTheGridNotTheWavelengthSampling()
|
||||
{
|
||||
var specification = Specification(kernelRadius: 30);
|
||||
|
||||
var draft = Sample(in specification, WaveOpticsColorMode.Broadband, WaveOpticsQuality.Draft);
|
||||
var high = Sample(in specification, WaveOpticsColorMode.Broadband, WaveOpticsQuality.High);
|
||||
|
||||
Assert.Equal(draft.Red, high.Red);
|
||||
Assert.Equal(draft.Green, high.Green);
|
||||
Assert.Equal(draft.Blue, high.Blue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ABroadbandKernelIsASmoothedVersionOfThePrimaryKernel()
|
||||
{
|
||||
var specification = Specification(kernelRadius: 30, fNumber: 16, pixelPitch: 2);
|
||||
|
||||
var (_, primary, _) = Sample(in specification, WaveOpticsColorMode.Primaries, WaveOpticsQuality.Standard);
|
||||
var (_, broadband, _) = Sample(in specification, WaveOpticsColorMode.Broadband, WaveOpticsQuality.Standard);
|
||||
|
||||
Assert.InRange(Distance(primary, broadband), 0.005, 0.8);
|
||||
Assert.Equal(primary.Sum(), broadband.Sum(), 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ALowerQualityApproachesTheHighestQualityOnlyAsCloselyAsItsNodesAllow()
|
||||
{
|
||||
var specification = Specification(gridSize: 256, kernelRadius: 30, aberration: new WavefrontAberration(comaHorizontalWaves: 1d, defocusWaves: 1d));
|
||||
|
||||
var draft = Sample(in specification, WaveOpticsColorMode.Broadband, WaveOpticsQuality.Draft);
|
||||
var standard = Sample(in specification, WaveOpticsColorMode.Broadband, WaveOpticsQuality.Standard);
|
||||
var high = Sample(in specification, WaveOpticsColorMode.Broadband, WaveOpticsQuality.High);
|
||||
|
||||
var draftDistance = Distance(draft.Blue, high.Blue) + Distance(draft.Green, high.Green) + Distance(draft.Red, high.Red);
|
||||
var standardDistance = Distance(standard.Blue, high.Blue) + Distance(standard.Green, high.Green) + Distance(standard.Red, high.Red);
|
||||
Assert.True(standardDistance < draftDistance, $"{standardDistance} {draftDistance}");
|
||||
Assert.True(draftDistance < 0.45, $"{draftDistance}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheChannelsOfAnAberratedLensDifferWhileTheirSumsStayEqual()
|
||||
{
|
||||
var specification = Case("coma");
|
||||
|
||||
var (red, green, blue) = Sample(in specification, WaveOpticsColorMode.Primaries, WaveOpticsQuality.Standard);
|
||||
|
||||
Assert.True(Distance(red, green) > 0.01);
|
||||
Assert.True(Distance(blue, green) > 0.01);
|
||||
Assert.Equal(red.Sum(), blue.Sum(), 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ARequestForTheMonochromeModeIsRejected()
|
||||
{
|
||||
var specification = Specification();
|
||||
var area = specification.KernelSize * specification.KernelSize;
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => sampler.TrySample(in specification, WaveOpticsColorMode.Monochrome, WaveOpticsQuality.Standard, new double[area], new double[area], new double[area]));
|
||||
Assert.Throws<ArgumentException>(() => sampler.TrySample(in specification, WaveOpticsColorMode.Primaries, WaveOpticsQuality.Standard, new double[area], new double[area - 1], new double[area]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AClosedApertureFailsWithoutThrowing()
|
||||
{
|
||||
var specification = Specification(gridSize: 64, shape: ApertureShape.RegularPolygon, blades: 4, obstruction: 0.95, kernelRadius: 3);
|
||||
var area = specification.KernelSize * specification.KernelSize;
|
||||
|
||||
var sampled = sampler.TrySample(in specification, WaveOpticsColorMode.Broadband, WaveOpticsQuality.Draft, new double[area], new double[area], new double[area]);
|
||||
|
||||
Assert.False(sampled);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsColorMode.Primaries, "clear")]
|
||||
[InlineData(WaveOpticsColorMode.Broadband, "clear")]
|
||||
[InlineData(WaveOpticsColorMode.Primaries, "coma")]
|
||||
[InlineData(WaveOpticsColorMode.Broadband, "coma")]
|
||||
public void AWarmSamplerAllocatesNoManagedMemory(WaveOpticsColorMode mode, string name)
|
||||
{
|
||||
var specification = Case(name);
|
||||
var area = specification.KernelSize * specification.KernelSize;
|
||||
var red = new double[area];
|
||||
var green = new double[area];
|
||||
var blue = new double[area];
|
||||
Assert.True(sampler.TrySample(in specification, mode, WaveOpticsQuality.High, red, green, blue));
|
||||
|
||||
var before = GC.GetAllocatedBytesForCurrentThread();
|
||||
for (var round = 0; round < 3; round++)
|
||||
Assert.True(sampler.TrySample(in specification, mode, WaveOpticsQuality.High, red, green, blue));
|
||||
|
||||
Assert.Equal(0, GC.GetAllocatedBytesForCurrentThread() - before);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using WaveOptics.Effects;
|
||||
using WaveOptics.Optics;
|
||||
|
||||
namespace WaveOptics.Tests.Psf;
|
||||
|
||||
public sealed class SpectralPlanTests
|
||||
{
|
||||
[Fact]
|
||||
public void ThePrimariesUseOneWavelengthPerChannelWithFullWeight()
|
||||
{
|
||||
var plan = SpectralPlan.For(WaveOpticsColorMode.Primaries, WaveOpticsQuality.High, false);
|
||||
|
||||
Assert.Equal(3, plan.Length);
|
||||
double[] wavelengths = [SpectralPlan.RedWavelength, SpectralPlan.GreenWavelength, SpectralPlan.BlueWavelength];
|
||||
for (var channel = 0; channel < 3; channel++)
|
||||
{
|
||||
var node = Assert.Single(plan[channel]);
|
||||
Assert.Equal(wavelengths[channel], node.Wavelength);
|
||||
var sample = Assert.Single(node.Samples);
|
||||
Assert.Equal(wavelengths[channel], sample.Wavelength);
|
||||
Assert.Equal(1d, sample.Weight);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheGreenPrimaryIsTheReferenceWavelength()
|
||||
{
|
||||
Assert.Equal(SpectralPlan.ReferenceWavelength, SpectralPlan.GreenWavelength);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsQuality.Draft, 1, 25)]
|
||||
[InlineData(WaveOpticsQuality.Standard, 3, 27)]
|
||||
[InlineData(WaveOpticsQuality.High, 5, 25)]
|
||||
public void ABroadbandIsDividedIntoNodesAndSamplesByQuality(WaveOpticsQuality quality, int nodes, int samples)
|
||||
{
|
||||
var plan = SpectralPlan.For(WaveOpticsColorMode.Broadband, quality, false);
|
||||
|
||||
foreach (var channel in plan)
|
||||
{
|
||||
Assert.Equal(nodes, channel.Length);
|
||||
Assert.Equal(samples, SpectralPlan.SampleCount(channel));
|
||||
Assert.True(SpectralPlan.SampleCount(channel) <= SpectralPlan.MaximumSamples);
|
||||
Assert.All(channel, node => Assert.True(node.Samples.Length <= SpectralPlan.MaximumSamples));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsQuality.Draft)]
|
||||
[InlineData(WaveOpticsQuality.Standard)]
|
||||
[InlineData(WaveOpticsQuality.High)]
|
||||
public void TheWeightsOfABandSumToOneAndFollowAGaussian(WaveOpticsQuality quality)
|
||||
{
|
||||
var plan = SpectralPlan.For(WaveOpticsColorMode.Broadband, quality, false);
|
||||
double[] centers = [SpectralPlan.RedWavelength, SpectralPlan.GreenWavelength, SpectralPlan.BlueWavelength];
|
||||
|
||||
for (var channel = 0; channel < 3; channel++)
|
||||
{
|
||||
var samples = plan[channel].SelectMany(node => node.Samples).ToArray();
|
||||
Assert.Equal(1d, samples.Sum(sample => sample.Weight), 12);
|
||||
Assert.Equal(centers[channel], samples.Sum(sample => sample.Weight * sample.Wavelength), 3);
|
||||
var middle = samples[samples.Length / 2];
|
||||
Assert.Equal(centers[channel], middle.Wavelength, 9);
|
||||
Assert.Equal(samples.Max(sample => sample.Weight), middle.Weight);
|
||||
Assert.InRange(samples[0].Wavelength, centers[channel] - SpectralPlan.BandSpan * SpectralPlan.BandSigma, centers[channel] - 2d * SpectralPlan.BandSigma);
|
||||
Assert.True(samples.Zip(samples.Skip(1)).All(pair => pair.Second.Wavelength > pair.First.Wavelength));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ANodeSitsAtTheMiddleSampleOfItsGroup()
|
||||
{
|
||||
var plan = SpectralPlan.For(WaveOpticsColorMode.Broadband, WaveOpticsQuality.Standard, false);
|
||||
|
||||
foreach (var channel in plan)
|
||||
{
|
||||
foreach (var node in channel)
|
||||
Assert.Equal(node.Samples[node.Samples.Length / 2].Wavelength, node.Wavelength);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsQuality.Standard)]
|
||||
[InlineData(WaveOpticsQuality.High)]
|
||||
public void WithoutAberrationABroadbandUsesOneNodePerChannel(WaveOpticsQuality quality)
|
||||
{
|
||||
var plan = SpectralPlan.For(WaveOpticsColorMode.Broadband, quality, true);
|
||||
|
||||
Assert.All(plan, channel => Assert.Single(channel));
|
||||
Assert.Same(SpectralPlan.For(WaveOpticsColorMode.Broadband, WaveOpticsQuality.Draft, false), plan);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheMonochromeModeHasNoPlan()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => SpectralPlan.For(WaveOpticsColorMode.Monochrome, WaveOpticsQuality.Standard, false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using WaveOptics.Abstractions;
|
||||
using WaveOptics.Effects;
|
||||
|
||||
namespace WaveOptics.Optics;
|
||||
|
||||
internal sealed class ChromaticKernelSampler
|
||||
{
|
||||
readonly FraunhoferKernelSampler sampler = new();
|
||||
double[] scratch = [];
|
||||
readonly double[] scales = new double[SpectralPlan.MaximumSamples];
|
||||
|
||||
public bool TrySample(
|
||||
in PsfSpecification reference,
|
||||
WaveOpticsColorMode mode,
|
||||
WaveOpticsQuality quality,
|
||||
Span<double> red,
|
||||
Span<double> green,
|
||||
Span<double> blue)
|
||||
{
|
||||
if (red.Length != green.Length || red.Length != blue.Length)
|
||||
throw new ArgumentException(null, nameof(green));
|
||||
|
||||
var bands = SpectralPlan.For(mode, quality, IsAberrationFree(reference.Aberration));
|
||||
if (scratch.Length < red.Length)
|
||||
scratch = new double[red.Length];
|
||||
for (var channel = 0; channel < SpectralPlan.ChannelCount; channel++)
|
||||
{
|
||||
var kernel = channel switch { 0 => red, 1 => green, _ => blue };
|
||||
kernel.Clear();
|
||||
foreach (var node in bands[channel])
|
||||
{
|
||||
if (!ComputeNodeIntensity(in reference, node))
|
||||
return false;
|
||||
|
||||
foreach (var sample in node.Samples)
|
||||
{
|
||||
var scale = node.Wavelength / sample.Wavelength;
|
||||
var values = scratch.AsSpan(0, kernel.Length);
|
||||
if (!sampler.TrySampleKernel(in reference, scale, values))
|
||||
return false;
|
||||
for (var index = 0; index < kernel.Length; index++)
|
||||
kernel[index] += sample.Weight * values[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ComputeNodeIntensity(in PsfSpecification reference, SpectralNode node)
|
||||
{
|
||||
var count = node.Samples.Length;
|
||||
for (var index = 0; index < count; index++)
|
||||
scales[index] = node.Wavelength / node.Samples[index].Wavelength;
|
||||
var phaseScale = SpectralPlan.ReferenceWavelength / node.Wavelength;
|
||||
return sampler.TryComputeIntensity(in reference, reference.PupilDiameterSamples * phaseScale, phaseScale, scales.AsSpan(0, count));
|
||||
}
|
||||
|
||||
static bool IsAberrationFree(WavefrontAberration aberration)
|
||||
=> aberration.DefocusWaves == 0d
|
||||
&& aberration.AstigmatismVerticalWaves == 0d
|
||||
&& aberration.AstigmatismObliqueWaves == 0d
|
||||
&& aberration.ComaHorizontalWaves == 0d
|
||||
&& aberration.ComaVerticalWaves == 0d
|
||||
&& aberration.TrefoilHorizontalWaves == 0d
|
||||
&& aberration.TrefoilVerticalWaves == 0d
|
||||
&& aberration.SphericalWaves == 0d;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using WaveOptics.Effects;
|
||||
|
||||
namespace WaveOptics.Optics;
|
||||
|
||||
internal readonly record struct SpectralSample(double Wavelength, double Weight);
|
||||
|
||||
internal readonly record struct SpectralNode(double Wavelength, SpectralSample[] Samples);
|
||||
|
||||
internal static class SpectralPlan
|
||||
{
|
||||
public const double ReferenceWavelength = 550d;
|
||||
public const double RedWavelength = 610d;
|
||||
public const double GreenWavelength = 550d;
|
||||
public const double BlueWavelength = 465d;
|
||||
public const double BandSigma = 21d;
|
||||
public const double BandSpan = 2.5;
|
||||
public const int ChannelCount = 3;
|
||||
public const int MaximumSamples = 27;
|
||||
|
||||
static readonly SpectralNode[][] Primaries =
|
||||
[
|
||||
[new(RedWavelength, [new(RedWavelength, 1d)])],
|
||||
[new(GreenWavelength, [new(GreenWavelength, 1d)])],
|
||||
[new(BlueWavelength, [new(BlueWavelength, 1d)])],
|
||||
];
|
||||
|
||||
static readonly SpectralNode[][][] Broadband =
|
||||
[
|
||||
Bands(1, 25),
|
||||
Bands(3, 9),
|
||||
Bands(5, 5),
|
||||
];
|
||||
|
||||
public static SpectralNode[][] For(WaveOpticsColorMode mode, WaveOpticsQuality quality, bool aberrationFree)
|
||||
=> mode switch
|
||||
{
|
||||
WaveOpticsColorMode.Primaries => Primaries,
|
||||
WaveOpticsColorMode.Broadband => Broadband[aberrationFree ? (int)WaveOpticsQuality.Draft : (int)quality],
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(mode)),
|
||||
};
|
||||
|
||||
public static int SampleCount(SpectralNode[] nodes)
|
||||
{
|
||||
var count = 0;
|
||||
foreach (var node in nodes)
|
||||
count += node.Samples.Length;
|
||||
return count;
|
||||
}
|
||||
|
||||
static SpectralNode[][] Bands(int nodeCount, int samplesPerNode)
|
||||
=> [Band(RedWavelength, nodeCount, samplesPerNode), Band(GreenWavelength, nodeCount, samplesPerNode), Band(BlueWavelength, nodeCount, samplesPerNode)];
|
||||
|
||||
static SpectralNode[] Band(double center, int nodeCount, int samplesPerNode)
|
||||
{
|
||||
var total = nodeCount * samplesPerNode;
|
||||
var samples = new SpectralSample[total];
|
||||
var sum = 0d;
|
||||
for (var index = 0; index < total; index++)
|
||||
{
|
||||
var offset = -BandSpan + (index + 0.5) * 2d * BandSpan / total;
|
||||
var weight = Math.Exp(-0.5 * offset * offset);
|
||||
samples[index] = new SpectralSample(center + BandSigma * offset, weight);
|
||||
sum += weight;
|
||||
}
|
||||
|
||||
var nodes = new SpectralNode[nodeCount];
|
||||
for (var node = 0; node < nodeCount; node++)
|
||||
{
|
||||
var group = samples.AsSpan(node * samplesPerNode, samplesPerNode).ToArray();
|
||||
for (var index = 0; index < group.Length; index++)
|
||||
group[index] = group[index] with { Weight = group[index].Weight / sum };
|
||||
nodes[node] = new SpectralNode(group[samplesPerNode / 2].Wavelength, group);
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする