スペクトルの基底と波長ごとの表を追加する

このコミットが含まれているのは:
routersys committed 2026-10-12 20:37:58 +09:00
1 親 0957b9c1f2
コミット cd433e5d75
4ファイル変更
+539

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

+17
ファイルの表示
@@ -67,4 +67,21 @@ public sealed class AllocationTests
Assert.Equal(0, allocated);
}
[Theory]
[InlineData(DispersionLaw.Grating)]
[InlineData(DispersionLaw.Prism)]
public void FillingTheSpectralTableAllocatesNothing(DispersionLaw law)
{
var table = new Vector4[Spectrum.SpectralTableLength];
var step = 0;
var allocated = Allocation.Of(() =>
{
step++;
Spectrum.FillSpectral(table, Spectrum.MinSteps + step % (Spectrum.MaxSteps - Spectrum.MinSteps + 1), 400f + step % 300, law);
});
Assert.Equal(0, allocated);
}
}
+357
ファイルの表示
@@ -0,0 +1,357 @@
using System.Numerics;
namespace SpectrumColorShift.Tests;
public sealed class SpectralSpectrumTests
{
static Vector4[] Table(int steps, float reference, DispersionLaw law)
{
var table = new Vector4[Spectrum.SpectralTableLength];
Spectrum.FillSpectral(table, steps, reference, law);
return table;
}
static Vector4 Gain(Vector4[] table, int index) => table[index * Spectrum.EntriesPerStep];
static Vector4 Basis(Vector4[] table, int index) => table[index * Spectrum.EntriesPerStep + 1];
static double Wavelength(int index, int steps) => 400d + 300d * index / (steps - 1);
static double Offset(DispersionLaw law, double wavelength, double reference)
=> law == DispersionLaw.Prism ? 1d / (wavelength * wavelength) - 1d / (reference * reference) : reference - wavelength;
static (double X, double Y, double Z) Integrate(Func<int, double> spectrum)
{
double x = 0d, y = 0d, z = 0d;
for (var index = 0; index < SpectralBasis.Count; index++)
{
var wavelength = SpectralBasis.First + SpectralBasis.Interval * index;
var (cx, cy, cz) = Spectrum.ColorMatching(wavelength);
var weight = spectrum(index) * SpectralBasis.IlluminantAt(wavelength) * (index == 0 || index == SpectralBasis.Count - 1 ? 0.5 : 1d);
x += weight * cx;
y += weight * cy;
z += weight * cz;
}
return (x, y, z);
}
static double[] RgbOf(ReadOnlySpan<double> xyz, double[] white)
{
var primaries = new (double X, double Y)[] { (0.64, 0.33), (0.30, 0.60), (0.15, 0.06) };
var columns = primaries.Select(p => new[] { p.X / p.Y, 1d, (1d - p.X - p.Y) / p.Y }).ToArray();
var scale = Solve(columns, white);
var matrix = columns.Select((column, c) => column.Select(value => value * scale[c]).ToArray()).ToArray();
return Solve(matrix, xyz.ToArray());
}
static double[] Solve(double[][] columns, double[] target)
{
double[,] m = new double[3, 3];
for (var c = 0; c < 3; c++)
for (var r = 0; r < 3; r++)
m[r, c] = columns[c][r];
var det = m[0, 0] * (m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1]) - m[0, 1] * (m[1, 0] * m[2, 2] - m[1, 2] * m[2, 0]) + m[0, 2] * (m[1, 0] * m[2, 1] - m[1, 1] * m[2, 0]);
var result = new double[3];
for (var c = 0; c < 3; c++)
{
var t = (double[,])m.Clone();
for (var r = 0; r < 3; r++)
t[r, c] = target[r];
result[c] = (t[0, 0] * (t[1, 1] * t[2, 2] - t[1, 2] * t[2, 1]) - t[0, 1] * (t[1, 0] * t[2, 2] - t[1, 2] * t[2, 0]) + t[0, 2] * (t[1, 0] * t[2, 1] - t[1, 1] * t[2, 0])) / det;
}
return result;
}
static Vector3 BasisAt(int index) => SpectralBasis.At(SpectralBasis.First + SpectralBasis.Interval * index);
[Fact]
public void TheIlluminantIsNormalizedTo100AtFiveHundredSixtyNanometers()
{
Assert.Equal(100d, SpectralBasis.IlluminantAt(560d), 1e-4);
Assert.Equal(82.7549d, SpectralBasis.IlluminantAt(400d), 1e-4);
Assert.Equal(71.6091d, SpectralBasis.IlluminantAt(700d), 1e-4);
}
[Fact]
public void TheIlluminantHasTheChromaticityOfD65()
{
var (x, y, z) = Integrate(_ => 1d);
var sum = x + y + z;
Assert.Equal(0.3127d, x / sum, 1e-3);
Assert.Equal(0.3290d, y / sum, 1e-3);
}
[Fact]
public void TheBasisSpectraAreNeverNegativeAndSumToOneAtEveryWavelength()
{
for (var index = 0; index < SpectralBasis.Count; index++)
{
var basis = BasisAt(index);
Assert.True(basis.X >= 0f && basis.Y >= 0f && basis.Z >= 0f, $"{index}");
Assert.Equal(1d, basis.X + basis.Y + basis.Z, 1e-6);
}
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
public void TheBasisSpectraReproduceTheSrgbPrimaries(int channel)
{
var white = Integrate(_ => 1d);
var normalization = Integrate(_ => 1d).Y;
var whiteXyz = new[] { white.X / normalization, white.Y / normalization, white.Z / normalization };
var xyz = Integrate(index => channel switch { 0 => BasisAt(index).X, 1 => BasisAt(index).Y, _ => BasisAt(index).Z });
var rgb = RgbOf([xyz.X / normalization, xyz.Y / normalization, xyz.Z / normalization], whiteXyz);
for (var component = 0; component < 3; component++)
Assert.Equal(component == channel ? 1d : 0d, rgb[component], 1e-5);
}
[Theory]
[InlineData(0, 600d, 700d)]
[InlineData(1, 500d, 580d)]
[InlineData(2, 400d, 480d)]
public void EachBasisSpectrumLivesInItsOwnPartOfTheSpectrum(int channel, double from, double to)
{
var peak = Enumerable.Range(0, SpectralBasis.Count)
.MaxBy(index => channel switch { 0 => BasisAt(index).X, 1 => BasisAt(index).Y, _ => BasisAt(index).Z });
Assert.InRange(SpectralBasis.First + SpectralBasis.Interval * peak, from, to);
}
[Fact]
public void TheBasisIsSmoothEnoughForTheSampledSpectrum()
{
for (var channel = 0; channel < 3; channel++)
for (var index = 1; index < SpectralBasis.Count - 1; index++)
{
var before = BasisAt(index - 1);
var here = BasisAt(index);
var after = BasisAt(index + 1);
var bend = channel switch
{
0 => before.X - 2f * here.X + after.X,
1 => before.Y - 2f * here.Y + after.Y,
_ => before.Z - 2f * here.Z + after.Z,
};
Assert.InRange(Math.Abs(bend), 0f, 0.05f);
}
}
[Fact]
public void TheBasisInterpolatesLinearlyBetweenItsSamples()
{
var left = BasisAt(10);
var right = BasisAt(11);
var middle = SpectralBasis.At(SpectralBasis.First + SpectralBasis.Interval * 10.5);
Assert.Equal((left.X + right.X) / 2f, middle.X, 1e-6f);
Assert.Equal((left.Y + right.Y) / 2f, middle.Y, 1e-6f);
Assert.Equal((left.Z + right.Z) / 2f, middle.Z, 1e-6f);
Assert.Equal(BasisAt(0), SpectralBasis.At(100d));
Assert.Equal(BasisAt(SpectralBasis.Count - 1), SpectralBasis.At(900d));
}
[Theory]
[InlineData(8, 550f, DispersionLaw.Grating)]
[InlineData(32, 550f, DispersionLaw.Grating)]
[InlineData(64, 550f, DispersionLaw.Grating)]
[InlineData(13, 400f, DispersionLaw.Prism)]
[InlineData(40, 700f, DispersionLaw.Prism)]
[InlineData(25, 620f, DispersionLaw.Prism)]
public void TheGainsAndTheBasisReproduceEveryColorWhenNothingMoves(int steps, float reference, DispersionLaw law)
{
var table = Table(steps, reference, law);
var identity = new double[3, 3];
for (var index = 0; index < steps; index++)
{
var gain = Gain(table, index);
var basis = Basis(table, index);
double[] g = [gain.X, gain.Y, gain.Z];
double[] b = [basis.X, basis.Y, basis.Z];
for (var row = 0; row < 3; row++)
for (var column = 0; column < 3; column++)
identity[row, column] += g[row] * b[column];
}
for (var row = 0; row < 3; row++)
for (var column = 0; column < 3; column++)
Assert.Equal(row == column ? 1d : 0d, identity[row, column], 2e-5);
}
[Fact]
public void EveryStepCountFromEightToSixtyFourReproducesWhiteAndKeepsTheCoverageNormalized()
{
for (var steps = Spectrum.MinSteps; steps <= Spectrum.MaxSteps; steps++)
{
var table = Table(steps, 550f, DispersionLaw.Grating);
var red = 0d;
var green = 0d;
var blue = 0d;
var coverage = 0d;
for (var index = 0; index < steps; index++)
{
var gain = Gain(table, index);
var basis = Basis(table, index);
var amplitude = basis.X + basis.Y + basis.Z;
red += gain.X * amplitude;
green += gain.Y * amplitude;
blue += gain.Z * amplitude;
coverage += gain.W;
}
Assert.True(Math.Abs(red - 1d) < 2e-5 && Math.Abs(green - 1d) < 2e-5 && Math.Abs(blue - 1d) < 2e-5, $"{steps} steps");
Assert.Equal(1d, coverage, 1e-5);
}
}
[Theory]
[InlineData(8)]
[InlineData(20)]
[InlineData(63)]
public void EntriesBeyondTheStepsAreCleared(int steps)
{
var table = Enumerable.Repeat(new Vector4(9f), Spectrum.SpectralTableLength).ToArray();
Spectrum.FillSpectral(table, steps, 550f, DispersionLaw.Grating);
Assert.All(table.Skip(steps * Spectrum.EntriesPerStep), entry => Assert.Equal(Vector4.Zero, entry));
Assert.All(table.Take(steps * Spectrum.EntriesPerStep), entry => Assert.NotEqual(new Vector4(9f), entry));
}
[Theory]
[InlineData(DispersionLaw.Grating, 550f)]
[InlineData(DispersionLaw.Grating, 450f)]
[InlineData(DispersionLaw.Grating, 650f)]
[InlineData(DispersionLaw.Prism, 550f)]
[InlineData(DispersionLaw.Prism, 400f)]
[InlineData(DispersionLaw.Prism, 700f)]
public void TheShiftCoefficientFollowsTheDispersionLaw(DispersionLaw law, float reference)
{
const int steps = 24;
var table = Table(steps, reference, law);
var norm = Math.Max(Math.Abs(Offset(law, 400d, reference)), Math.Abs(Offset(law, 700d, reference)));
for (var index = 0; index < steps; index++)
Assert.Equal(Offset(law, Wavelength(index, steps), reference) / norm, Basis(table, index).W, 1e-5);
}
[Theory]
[InlineData(DispersionLaw.Grating, 550f, 1f, -1f)]
[InlineData(DispersionLaw.Grating, 650f, 1f, -0.2f)]
[InlineData(DispersionLaw.Grating, 450f, 0.2f, -1f)]
[InlineData(DispersionLaw.Grating, 400f, 0f, -1f)]
[InlineData(DispersionLaw.Grating, 700f, 1f, 0f)]
[InlineData(DispersionLaw.Prism, 700f, 1f, 0f)]
public void TheSpectrumEndsReachAtMostTheFullDistance(DispersionLaw law, float reference, float shortEnd, float longEnd)
{
var table = Table(16, reference, law);
Assert.Equal(shortEnd, Basis(table, 0).W, 1e-5);
Assert.Equal(longEnd, Basis(table, 15).W, 1e-5);
}
[Theory]
[InlineData(DispersionLaw.Grating)]
[InlineData(DispersionLaw.Prism)]
public void TheShiftCoefficientFallsAsTheWavelengthGrows(DispersionLaw law)
{
var table = Table(Spectrum.MaxSteps, 550f, law);
for (var index = 1; index < Spectrum.MaxSteps; index++)
Assert.True(Basis(table, index).W < Basis(table, index - 1).W, $"{index}");
}
[Theory]
[InlineData(DispersionLaw.Grating)]
[InlineData(DispersionLaw.Prism)]
public void TheSampleAtTheReferenceWavelengthDoesNotMove(DispersionLaw law)
{
var table = Table(13, 550f, law);
Assert.Equal(0f, Basis(table, 6).W);
Assert.NotEqual(0f, Basis(table, 5).W);
Assert.NotEqual(0f, Basis(table, 7).W);
}
[Fact]
public void ThePrismLawSpreadsBlueMoreThanRed()
{
var table = Table(16, 550f, DispersionLaw.Prism);
Assert.InRange(-Basis(table, 15).W, 0.40f, 0.46f);
Assert.True(Basis(table, 0).W > -Basis(table, 15).W * 2f);
}
[Fact]
public void TheTwoLawsGiveDifferentCoefficients()
{
var grating = Table(24, 550f, DispersionLaw.Grating);
var prism = Table(24, 550f, DispersionLaw.Prism);
Assert.NotEqual(Basis(grating, 5).W, Basis(prism, 5).W);
Assert.NotEqual(Basis(grating, 18).W, Basis(prism, 18).W);
}
[Fact]
public void TheCoverageWeightsFollowTheLuminousEfficiency()
{
var table = Table(32, 550f, DispersionLaw.Grating);
var centroid = Enumerable.Range(0, 32).Sum(index => Wavelength(index, 32) * Gain(table, index).W);
Assert.InRange(centroid, 548d, 560d);
Assert.True(Gain(table, 0).W < Gain(table, 16).W / 100f);
Assert.True(Gain(table, 31).W < Gain(table, 16).W / 10f);
}
[Theory]
[InlineData(300f, 400f)]
[InlineData(400f, 400f)]
[InlineData(550f, 550f)]
[InlineData(700f, 700f)]
[InlineData(900f, 700f)]
[InlineData(float.PositiveInfinity, 700f)]
[InlineData(float.NegativeInfinity, 400f)]
[InlineData(float.NaN, 550f)]
public void TheReferenceStaysWithinTheSpectrum(float requested, float expected)
{
Assert.Equal(expected, Spectrum.ClampReference(requested));
Assert.Equal(Table(16, expected, DispersionLaw.Grating), Table(16, requested, DispersionLaw.Grating));
}
[Theory]
[InlineData(int.MinValue, 8)]
[InlineData(9, 12)]
[InlineData(33, 36)]
[InlineData(61, 64)]
[InlineData(int.MaxValue, 64)]
public void ThePaddedStepsFillWholeUnrolledIterations(int requested, int expected)
{
Assert.Equal(expected, Spectrum.Padded(requested));
Assert.Equal(0, Spectrum.Padded(requested) % Spectrum.Unroll);
}
[Theory]
[InlineData(int.MinValue, 8)]
[InlineData(7, 8)]
[InlineData(8, 8)]
[InlineData(40, 40)]
[InlineData(64, 64)]
[InlineData(65, 64)]
[InlineData(int.MaxValue, 64)]
public void TheStepsStayWithinTheirRange(int requested, int expected)
{
Assert.Equal(expected, Spectrum.ClampSteps(requested));
Assert.Equal(Table(expected, 550f, DispersionLaw.Grating), Table(requested, 550f, DispersionLaw.Grating));
}
}
+86
ファイルの表示
@@ -0,0 +1,86 @@
using System.Numerics;
namespace SpectrumColorShift;
internal static class SpectralBasis
{
public const double First = 400.0;
public const double Interval = 5.0;
public const int Count = 61;
private static ReadOnlySpan<float> Red =>
[
0.192512361f, 0.16935988f, 0.146196031f, 0.123059145f, 0.100109343f, 0.0777127425f, 0.0564703934f, 0.0371695051f,
0.0207353365f, 0.00830780452f, 0.00137841186f, 7.5479875e-07f, 9.01286255e-07f, 1.06360691e-06f, 5.45645767e-07f, 3.57413785e-07f,
2.57572257e-07f, 2.02100568e-07f, 1.641348e-07f, 1.31969263e-07f, 1.0800669e-07f, 9.0420405e-08f, 7.66890265e-08f, 6.68081003e-08f,
5.99273434e-08f, 5.42758659e-08f, 4.43178079e-08f, 4.17441579e-08f, 4.91125451e-08f, 5.06339275e-08f, 4.67123455e-08f, 6.02243933e-08f,
3.32734569e-08f, 0.00218982151f, 0.0301830399f, 0.0943962603f, 0.194844971f, 0.323868219f, 0.468912693f, 0.615343668f,
0.748929449f, 0.858055159f, 0.935683229f, 0.980840619f, 0.99893757f, 0.999999877f, 0.999999636f, 0.999999509f,
0.999999413f, 0.999999252f, 0.999999011f, 0.999998673f, 0.999998148f, 0.999997337f, 0.99999599f, 0.999993519f,
0.99998865f, 0.999971924f, 0.99992753f, 0.999857504f, 0.99977574f,
];
private static ReadOnlySpan<float> Green =>
[
1.81116187e-05f, 4.76598665e-06f, 2.06583504e-06f, 1.14787887e-06f, 7.01859312e-07f, 4.9802927e-07f, 3.91446971e-07f, 2.9659085e-07f,
2.51710459e-07f, 1.52383012e-07f, 9.82642489e-08f, 0.0030716171f, 0.0200009702f, 0.0564915036f, 0.114178186f, 0.191829222f,
0.285986432f, 0.391619914f, 0.502778259f, 0.613202335f, 0.716844931f, 0.808340572f, 0.883459214f, 0.939549555f,
0.975987013f, 0.994593092f, 0.999999794f, 0.999999852f, 0.999999666f, 0.99999969f, 0.999999848f, 0.999548594f,
0.992259651f, 0.973278248f, 0.929308352f, 0.85554009f, 0.753853787f, 0.631069348f, 0.497157023f, 0.363430426f,
0.240949244f, 0.139096042f, 0.0643157669f, 0.0191591542f, 0.00106195131f, 4.56685713e-08f, 1.58152571e-07f, 2.22023037e-07f,
2.65629655e-07f, 3.38276201e-07f, 4.47483004e-07f, 6.00414965e-07f, 8.37904682e-07f, 1.20521489e-06f, 1.81520325e-06f, 2.94309258e-06f,
5.13254026e-06f, 1.23204315e-05f, 3.2743966e-05f, 6.68620547e-05f, 0.000107581877f,
];
private static ReadOnlySpan<float> Blue =>
[
0.807469527f, 0.830635354f, 0.853801904f, 0.876939707f, 0.899889955f, 0.92228676f, 0.943529215f, 0.962830198f,
0.979264412f, 0.991692043f, 0.99862149f, 0.996927628f, 0.979998128f, 0.943507433f, 0.885821269f, 0.808170421f,
0.71401331f, 0.608379884f, 0.497221577f, 0.386797533f, 0.283154961f, 0.191659337f, 0.11654071f, 0.0604503786f,
0.0240129266f, 0.00540685411f, 1.61548711e-07f, 1.06184152e-07f, 2.84808058e-07f, 2.59130901e-07f, 1.04914227e-07f, 0.000451346134f,
0.00774031585f, 0.0245319302f, 0.0405086082f, 0.0500636496f, 0.051301242f, 0.0450624333f, 0.0339302845f, 0.021225906f,
0.0101213068f, 0.00284879946f, 1.00408131e-06f, 2.26836029e-07f, 4.78697571e-07f, 7.77024557e-08f, 2.06003424e-07f, 2.68621395e-07f,
3.21384151e-07f, 4.09280481e-07f, 5.4140573e-07f, 7.26436417e-07f, 1.01373333e-06f, 1.45801888e-06f, 2.19525189e-06f, 3.5375357e-06f,
6.21747073e-06f, 1.57553175e-05f, 3.9726355e-05f, 7.56335619e-05f, 0.000116677997f,
];
private static ReadOnlySpan<float> D65 =>
[
82.7549f, 87.1204f, 91.486f, 92.4589f, 93.4318f, 90.057f, 86.6823f, 95.7736f,
104.865f, 110.936f, 117.008f, 117.41f, 117.812f, 116.336f, 114.861f, 115.392f,
115.923f, 112.367f, 108.811f, 109.082f, 109.354f, 108.578f, 107.802f, 106.296f,
104.79f, 106.239f, 107.689f, 106.047f, 104.405f, 104.225f, 104.046f, 102.023f,
100f, 98.1671f, 96.3342f, 96.0611f, 95.788f, 92.2368f, 88.6856f, 89.3459f,
90.0062f, 89.8026f, 89.5991f, 88.6489f, 87.6987f, 85.4936f, 83.2886f, 83.4939f,
83.6992f, 81.863f, 80.0268f, 80.1207f, 80.2146f, 81.2462f, 82.2778f, 80.281f,
78.2842f, 74.0027f, 69.7213f, 70.6652f, 71.6091f,
];
public static Vector3 At(double wavelength)
{
var (index, fraction) = Locate(wavelength);
return new Vector3(
Lerp(Red, index, fraction),
Lerp(Green, index, fraction),
Lerp(Blue, index, fraction));
}
public static double IlluminantAt(double wavelength)
{
var (index, fraction) = Locate(wavelength);
return Lerp(D65, index, fraction);
}
private static (int Index, float Fraction) Locate(double wavelength)
{
var position = Math.Clamp((wavelength - First) / Interval, 0.0, Count - 1);
var index = Math.Min((int)position, Count - 2);
return (index, (float)(position - index));
}
private static float Lerp(ReadOnlySpan<float> table, int index, float fraction)
=> table[index] + (table[index + 1] - table[index]) * fraction;
}
+79
ファイルの表示
@@ -7,12 +7,17 @@ internal static class Spectrum
public const int MinSteps = 8;
public const int MaxSteps = 64;
public const int DefaultSteps = 32;
public const int EntriesPerStep = 2;
public const int Unroll = 4;
public const int SpectralTableLength = MaxSteps * EntriesPerStep;
public const float ShortestWavelength = 400f;
public const float LongestWavelength = 700f;
public const float DefaultReferenceWavelength = 550f;
public static int ClampSteps(int steps) => Math.Clamp(steps, MinSteps, MaxSteps);
public static int Padded(int steps) => (ClampSteps(steps) + Unroll - 1) / Unroll * Unroll;
public static float ClampReference(float wavelength)
=> float.IsNaN(wavelength) ? DefaultReferenceWavelength : Math.Clamp(wavelength, ShortestWavelength, LongestWavelength);
@@ -70,6 +75,80 @@ internal static class Spectrum
table[steps..].Clear();
}
private static double Wavelength(int index, int steps)
=> ShortestWavelength + (LongestWavelength - ShortestWavelength) * index / (steps - 1);
private static (double X, double Y, double Z) Emission(int index, int steps)
{
var wavelength = Wavelength(index, steps);
var weight = (index == 0 || index == steps - 1 ? 0.5 : 1.0) * SpectralBasis.IlluminantAt(wavelength);
var (x, y, z) = ColorMatching(wavelength);
return (weight * x, weight * y, weight * z);
}
public static void FillSpectral(Span<Vector4> table, int steps, float referenceWavelength, DispersionLaw law)
{
steps = ClampSteps(steps);
var reference = ClampReference(referenceWavelength);
var norm = Math.Max(
Math.Abs(Displacement(law, ShortestWavelength, reference)),
Math.Abs(Displacement(law, LongestWavelength, reference)));
Span<double> matrix = stackalloc double[9];
Span<double> inverse = stackalloc double[9];
matrix.Clear();
var luminance = 0.0;
for (var index = 0; index < steps; index++)
{
var (x, y, z) = Emission(index, steps);
var basis = SpectralBasis.At(Wavelength(index, steps));
matrix[0] += x * basis.X;
matrix[1] += x * basis.Y;
matrix[2] += x * basis.Z;
matrix[3] += y * basis.X;
matrix[4] += y * basis.Y;
matrix[5] += y * basis.Z;
matrix[6] += z * basis.X;
matrix[7] += z * basis.Y;
matrix[8] += z * basis.Z;
luminance += y;
}
Invert(matrix, inverse);
for (var index = 0; index < steps; index++)
{
var wavelength = Wavelength(index, steps);
var (x, y, z) = Emission(index, steps);
var basis = SpectralBasis.At(wavelength);
var gain = new Vector3(
(float)(inverse[0] * x + inverse[1] * y + inverse[2] * z),
(float)(inverse[3] * x + inverse[4] * y + inverse[5] * z),
(float)(inverse[6] * x + inverse[7] * y + inverse[8] * z));
table[index * EntriesPerStep] = new Vector4(gain, (float)(y / luminance));
table[index * EntriesPerStep + 1] = new Vector4(basis, (float)(Displacement(law, wavelength, reference) / norm));
}
table[(steps * EntriesPerStep)..].Clear();
}
private static void Invert(ReadOnlySpan<double> m, Span<double> inverse)
{
var c0 = m[4] * m[8] - m[5] * m[7];
var c1 = m[5] * m[6] - m[3] * m[8];
var c2 = m[3] * m[7] - m[4] * m[6];
var scale = 1.0 / (m[0] * c0 + m[1] * c1 + m[2] * c2);
inverse[0] = c0 * scale;
inverse[1] = (m[2] * m[7] - m[1] * m[8]) * scale;
inverse[2] = (m[1] * m[5] - m[2] * m[4]) * scale;
inverse[3] = c1 * scale;
inverse[4] = (m[0] * m[8] - m[2] * m[6]) * scale;
inverse[5] = (m[2] * m[3] - m[0] * m[5]) * scale;
inverse[6] = c2 * scale;
inverse[7] = (m[1] * m[6] - m[0] * m[7]) * scale;
inverse[8] = (m[0] * m[4] - m[1] * m[3]) * scale;
}
private static double Displacement(DispersionLaw law, double wavelength, double reference)
=> law == DispersionLaw.Prism
? 1.0 / (wavelength * wavelength) - 1.0 / (reference * reference)