/
SpectralQuantity.cs
318 lines (267 loc) · 10.9 KB
/
SpectralQuantity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace At.Matus.BevMetrology
{
public class SpectralQuantity
{
public string Name { get; private set; }
public double MinWavelength => spectralValues.First().Lambda;
public double MaxWavelength => spectralValues.Last().Lambda;
public double MinValue => GetMinValue();
public double MaxValue => GetMaxValue();
public int NumberOfValues => spectralValues.Count;
public double CCT => ColorTemperature.CCT;
public double TD => DistributionTemperature.TD;
public ColorCoordinates Color => CalculateColor();
public ColorTemperature ColorTemperature => CalculateCct();
public DistributionTemperature DistributionTemperature => CalculateTD();
public SpectralQuantity(string name) => Name = name.Trim();
public void AddValue(SpectralQuantityValue spectralValue)
{
spectralValues.Add(spectralValue);
spectralValues.Sort();
}
public void AddValue(double wavelength, double value) => AddValue(new SpectralQuantityValue(wavelength, value));
public double GetValueFor(double wavelength)
{
// linear interpolation between adjecent data points
if (wavelength < MinWavelength)
return double.NaN;
if (wavelength > MaxWavelength)
return double.NaN;
int index0 = spectralValues.FindLastIndex(x => x.Lambda <= wavelength);
if (index0 < 0)
return double.NaN;
int index1 = spectralValues.FindIndex(x => x.Lambda >= wavelength);
if (index1 < 0)
return double.NaN;
// we hit a existing wavelength value by chance
if (index0 == index1)
return spectralValues[index0].Value;
double x0 = spectralValues[index0].Lambda;
double x1 = spectralValues[index1].Lambda;
double y0 = spectralValues[index0].Value;
double y1 = spectralValues[index1].Value;
// two values with the same wavelength
if (x0 == x1)
return (y0 + y1) / 2.0;
double y = y0 + ((wavelength - x0) * (y1 - y0) / (x1 - x0));
return y;
}
public double GetValueFor(int wavelength) => GetValueFor((double)wavelength);
public void ClearAllValues() => spectralValues.Clear();
public static SpectralQuantity FromCieIlluminantA()
{
SpectralQuantity spectrum = new SpectralQuantity("CIE Illuminant A");
for (int l = 300; l <= 830; l++)
{
spectrum.AddValue(new SpectralQuantityValue((double)l, BevCie.CieIlluminantA(l)));
}
return spectrum;
}
public static SpectralQuantity FromCieIlluminantD65()
{
SpectralQuantity spectrum = new SpectralQuantity("CIE Illuminant D65");
for (int l = 300; l <= 830; l++)
{
spectrum.AddValue(new SpectralQuantityValue((double)l, BevCie.CieIlluminantD65(l)));
}
return spectrum;
}
public static SpectralQuantity FromCieIlluminantD50()
{
SpectralQuantity spectrum = new SpectralQuantity("CIE Illuminant D50");
for (int l = 300; l <= 830; l++)
{
spectrum.AddValue(new SpectralQuantityValue((double)l, BevCie.CieIlluminantD50(l)));
}
return spectrum;
}
public static SpectralQuantity LoadFromCsv(string filename)
{
SpectralQuantity spectrum = new SpectralQuantity(Path.GetFileNameWithoutExtension(filename));
var reader = new StreamReader(File.OpenRead(filename));
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
char[] delimiter = { ',', ';', ' ', '\t' };
var tokens = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length == 2)
{
double x = MyParse(tokens[0]);
double y = MyParse(tokens[1]);
if (!double.IsNaN(x) && !double.IsNaN(y))
{
spectrum.AddValue(new SpectralQuantityValue(x, y));
}
}
}
reader.Close();
return spectrum;
}
public SpectralQuantity Randomize(double constPart, double relPart)
{
var randomizedSpectralQuantity = new SpectralQuantity($"{Name} - randomized");
foreach (var value in spectralValues)
{
double r1 = RandomUtil.GetUniformNoise(relPart);
double r2 = RandomUtil.GetUniformNoise(constPart);
double newValue = value.Value * (1.0 + r1) + r2;
if (newValue < 0) newValue = 0.0;
randomizedSpectralQuantity.AddValue(value.Lambda, newValue);
}
return randomizedSpectralQuantity;
}
private double GetMinValue()
{
double temp = double.MaxValue;
foreach (var s in spectralValues)
{
if (s.Value < temp) temp = s.Value;
}
return temp;
}
private double GetMaxValue()
{
double temp = double.MinValue;
foreach (var s in spectralValues)
{
if (s.Value > temp) temp = s.Value;
}
return temp;
}
private static double MyParse(string token)
{
if (double.TryParse(token, out double value))
return value;
else
return double.NaN;
}
private ColorCoordinates CalculateColor()
{
double X2 = BevCie.Integrate(GetValueFor, BevCie.CieX2);
double Y2 = BevCie.Integrate(GetValueFor, BevCie.CieY2);
double Z2 = BevCie.Integrate(GetValueFor, BevCie.CieZ2);
return new ColorCoordinates(X2, Y2, Z2);
}
#region Correlated color temperature (CCT) stuff
private ColorTemperature CalculateCct()
{
double cctTemp = EstimateCCT(Color, 500, 100000, 100);
double[] tPrec = { 100, 10, 1, 0.1, 0.01 };
foreach (var deltaT in tPrec)
{
cctTemp = EstimateCCT(Color, cctTemp - deltaT, cctTemp + deltaT, deltaT / 10);
}
var colorT = CalculateColorPlanck(cctTemp);
double distance = ChromaDistance(colorT.uPrime, colorT.vPrime, Color.uPrime, Color.vPrime);
return new ColorTemperature(cctTemp, distance);
}
private double EstimateCCT(ColorCoordinates color, double tMin, double tMax, double deltat)
{
double distanceMin = double.PositiveInfinity;
double CCT = double.NaN;
for (double T = tMin; T <= tMax; T = T + deltat)
{
var colorT = SpectralQuantity.CalculateColorPlanck(T);
double distance = ChromaDistance(colorT.uPrime, colorT.vPrime, color.uPrime, color.vPrime);
if (distance < distanceMin)
{
distanceMin = distance;
CCT = T;
}
}
return CCT;
}
private static double ChromaDistance(double up, double vp, double u, double v)
{
double us = (up - u) * (up - u);
double vs = (vp - v) * (vp - v);
return Math.Sqrt(us + vs * (4.0 / 9.0));
}
private static ColorCoordinates CalculateColorPlanck(double t)
{
double LPlanck(int lamb) => BevCie.LPlanck(t, lamb);
double X2 = BevCie.Integrate(LPlanck, BevCie.CieX2);
double Y2 = BevCie.Integrate(LPlanck, BevCie.CieY2);
double Z2 = BevCie.Integrate(LPlanck, BevCie.CieZ2);
return new ColorCoordinates(X2, Y2, Z2);
}
#endregion
#region Distribution temperature (TD) stuff
private const double lowerWavelengthLimitTD = 360;
private const double upperWavelengthLimitTD = 830;
private const double scalingWavelengthTD = 560;
private DistributionTemperature CalculateTD()
{
double dtTemp = FitTD(500, 100000, 100);
double[] tPrec = { 100, 10, 1, 0.1, 0.01 };
foreach (var deltaT in tPrec)
{
dtTemp = FitTD(dtTemp - deltaT, dtTemp + deltaT, deltaT / 10);
}
double maxDev = MaxDeviationOfFit(dtTemp);
return new DistributionTemperature(dtTemp, maxDev);
}
private double MaxDeviationOfFit(double temperature)
{
double a = ScalingFactor(temperature, scalingWavelengthTD);
double maxDev = 0;
double dev;
foreach (var sqv in spectralValues)
{
if (sqv.Lambda < lowerWavelengthLimitTD || sqv.Lambda > upperWavelengthLimitTD)
{
dev = 0;
}
else
{
dev = Math.Abs((sqv.Value - a * BevCie.LPlanck(temperature, sqv.Lambda)) / sqv.Value);
}
if (dev > maxDev)
{
maxDev = dev;
}
}
return maxDev;
}
private double FitTD(double tMin, double tMax, double deltat)
{
double distanceMin = double.PositiveInfinity;
double TD = double.NaN;
for (double T = tMin; T <= tMax; T = T + deltat)
{
double distance = GoodnessTD(T);
if (distance < distanceMin)
{
distanceMin = distance;
TD = T;
}
}
return TD;
}
private double ScalingFactor(double temperature, double wavelength)
{
double st = GetValueFor(wavelength);
double sp = BevCie.LPlanck(temperature, wavelength);
return st / sp; // TODO DANGER!
}
private double GoodnessTD(double temperature)
{
double a = ScalingFactor(temperature, scalingWavelengthTD);
return BevCie.Integrate(FitFunction);
double FitFunction(int wavelength) //TODO will not work for UV, IR !
{
if (wavelength < lowerWavelengthLimitTD) return 0;
if (wavelength > upperWavelengthLimitTD) return 0;
double st = GetValueFor(wavelength);
double sp = BevCie.LPlanck(temperature, wavelength);
return (1 - st / (a * sp)) * (1 - st / (a * sp));
}
}
#endregion
private readonly List<SpectralQuantityValue> spectralValues = new List<SpectralQuantityValue>();
}
}