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

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

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

+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)