-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSHLib.cpp
3356 lines (2748 loc) · 193 KB
/
SHLib.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// SH Utility Library
//------------------------------------------------------------------------------
#include "SHLib.h"
#include "ZHLib.h" // only for WindowScale, kZH_Y, so could decouple.
using namespace SHL;
using namespace ZHL;
namespace
{
const float kFourPi = 4 * vl_pi;
// The normalization constants themselves are given by
// sqrtf(((2l + 1) (l - m)!) / (4 pi (l + m)!))
// However, we also roll in any overall multipliers from the
// basis function itself. (E.g., the b.f. for Y_22 is (x^2 - y^2) / 2.)
// normalization constants basis function:
const float kSH_Y_00 = sqrtf( 1 / ( kFourPi)); // 1
// 3, 3 -> 1, 1
const float kSH_Y_10 = sqrtf( 3 / ( kFourPi)); // y
const float kSH_Y_11 = sqrtf( 3 / ( kFourPi)); // z
const float kSH_Y_12 = sqrtf( 3 / ( kFourPi)); // x
// 15, 15, 5 -> 3, 3, 1
const float kSH_Y_20 = sqrtf(15 / ( kFourPi)); // xy
const float kSH_Y_21 = sqrtf(15 / ( kFourPi)); // yz
const float kSH_Y_22 = sqrtf( 5 / ( 4 * kFourPi)); // (3z^2 - 1)
const float kSH_Y_23 = sqrtf(15 / ( kFourPi)); // xz
const float kSH_Y_24 = sqrtf(15 / ( 4 * kFourPi)); // (x^2 - y^2)
// 35/2 105 21/2 7 -> 5/2, 15, 3/2, 1
const float kSH_Y_30 = sqrtf( 35 / ( 8 * kFourPi)); // y (3x^2 - y^2)
const float kSH_Y_31 = sqrtf(105 / ( 1 * kFourPi)); // z xy
const float kSH_Y_32 = sqrtf( 21 / ( 8 * kFourPi)); // y (5z^2 - 1)
const float kSH_Y_33 = sqrtf( 7 / ( 4 * kFourPi)); // z (5z^2 - 3)
const float kSH_Y_34 = sqrtf( 21 / ( 8 * kFourPi)); // x (5z^2 - 1)
const float kSH_Y_35 = sqrtf(105 / ( 4 * kFourPi)); // z (x^2 - y^2)
const float kSH_Y_36 = sqrtf( 35 / ( 8 * kFourPi)); // x (x^2 - 3y^2)
// 315, 315/2, 45, 45/2, 9 -> 35, 35/2, 5, 5/2, 1
const float kSH_Y_40 = sqrtf(315 / ( 4 * kFourPi)); // (x^2 - y^2) xy
const float kSH_Y_41 = sqrtf(315 / ( 8 * kFourPi)); // (3x^2 - y^2) yz
const float kSH_Y_42 = sqrtf( 45 / ( 4 * kFourPi)); // (7z^2 - 1) xy
const float kSH_Y_43 = sqrtf( 45 / ( 8 * kFourPi)); // (7z^2 - 3) yz
const float kSH_Y_44 = sqrtf( 9 / (64 * kFourPi)); // (3 - 30z^2 + 35z^4)
const float kSH_Y_45 = sqrtf( 45 / ( 8 * kFourPi)); // (7z^2 - 3) xz
const float kSH_Y_46 = sqrtf( 45 / (16 * kFourPi)); // (7z^2 - 1) (x^2 -y^2)
const float kSH_Y_47 = sqrtf(315 / ( 8 * kFourPi)); // (x^2 - 3y^2) xz
const float kSH_Y_48 = sqrtf(315 / (64 * kFourPi)); // (x^4 - 6 x^2 y^2 + y^4)
// diffuse reflectance convolution weights.
// Multiply bands by these constants to convert incident radiance to exit
// radiance for a diffuse surface.
//const float kSH_A0 = 1.0f;
const float kSH_A1 = (2.0f / 3.0f);
const float kSH_A2 = (1.0f / 4.0f);
//const float kSH_A3 = 0.0f;
const float kSH_A4 = -(1.0f / 24.0f);
}
namespace
{
/*
SH function:
m > 0 sqrt(2) * K(l, m) * cos(|m| * phi) * P(l, |m|, cos(theta))
m < 0 sqrt(2) * K(l, m) * sin(|m| * phi) * P(l, |m|, cos(theta))
m = 0 K(l, 0) * P(l, 0, cos(theta))
If r^ = (x, y, z), then
z = cos(theta)
x = cos(phi)
y = sin(phi)
sin(n phi) = sum(k=0..n) binom(k, n) cos(phi)^k sin(phi)^(n-k) sin(1/2(n-k)pi)
cos(n phi) = sum(k=0..n) binom(k, n) cos(phi)^k sin(phi)^(n-k) cos(1/2(n-k)pi)
binom(k, n) = n! / k!(n-k)!
So for m = 2,
sin(2 phi) = 1 y^2 0 + 2 xy 1 + 1 x^2 0 = 2xy
cos(2 phi) = 1 y^2 1 + 2 xy 0 + 1 x^2 1 = y^2 + x^2
and so on.
*/
uint32_t kFactorialTable[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 };
inline float fact(int n)
{
if (n < 13)
return float(kFactorialTable[n]);
float result = float(kFactorialTable[12]);
while (n >= 13)
result *= n--;
return result;
}
inline float SH_K(int l, int pm)
{
if (pm == 0) // factorials cancel out
return sqrtf(float(2 * l + 1) / kFourPi);
float top = float(2 * l + 1) * fact(l - pm);
float bottom = kFourPi * fact(l + pm);
return sqrtf(top / bottom);
}
float SH_P(int l, int m, float x)
{
float pmm = 1.0f;
if (m > 0)
{
float tm1 = sqrtf((1.0f - x) * (1.0f + x));
float fact = 1.0f;
for (int i = 1; i <= m; i++)
{
pmm *= (-fact) * tm1;
fact += 2.0f;
}
}
if (l == m)
return pmm;
float pmmp1 = x * float(2 * m + 1) * pmm;
if (l == m + 1)
return pmmp1;
float pll = 0.0f;
for (int ll = m + 2; ll <= l; ll++)
{
pll = (float(2 * ll - 1) * x * pmmp1 - float(ll + m - 1) * pmm);
pll /= float(ll - m);
pmm = pmmp1;
pmmp1 = pll;
}
return pll;
}
}
float SHL::SH(int l, int m, float theta, float phi)
{
const float kRoot2 = sqrtf(2.0f);
int pm = abs(m);
float term1 = SH_K(l, pm) * SH_P(l, pm, cosf(theta));
if (m == 0)
return term1;
phi += vl_pi;
if (m > 0)
return term1 * kRoot2 * cosf(pm * phi);
else
return term1 * kRoot2 * sinf(pm * phi);
}
namespace
{
// Chebyshev recurrence relations for sin(nx) / cos(nx) given sin(x) / cos(x)
inline float ChebyshevN(int n, float c, float n1, float n2)
{
if (n == 0)
return n1;
for (int i = 1; i < n; i++)
{
float n0 = n1;
n1 = n2;
n2 = 2 * c * n1 - n0;
}
return n2;
}
inline float SinN(int n, float s, float c)
{
return ChebyshevN(n, c, 0.0f, s);
}
inline float CosN(int n, float c)
{
return ChebyshevN(n, c, 1.0f, c);
}
const float kRoot2 = sqrtf(2.0f);
}
float SHL::SH(int l, int m, Vec3f dir) // dir must be normalised
{
float cosTheta = dir[2];
int pm = abs(m);
float term1 = SH_K(l, pm) * SH_P(l, pm, cosTheta);
if (m == 0)
return term1;
float rxy = sqrtf(1.0f - cosTheta * cosTheta);
float cosPhi, sinPhi;
rxy += 1e-16f;
float invSinTheta = 1.0f / rxy; // epsilon to avoid 0 divide
// minus signs recapitulate += pi from SH(polar).
cosPhi = -dir[0] * invSinTheta;
if (m > 0)
return term1 * kRoot2 * CosN(pm, cosPhi);
sinPhi = -dir[1] * invSinTheta;
return term1 * kRoot2 * SinN(pm, sinPhi, cosPhi);
}
const float SHL::kSH_Y[] =
{
sqrtf( 1 / ( kFourPi)), // 1
sqrtf( 3 / ( kFourPi)), // y
sqrtf( 3 / ( kFourPi)), // z
sqrtf( 3 / ( kFourPi)), // x
sqrtf( 15 / ( kFourPi)), // xy
sqrtf( 15 / ( kFourPi)), // yz
sqrtf( 5 / ( 4 * kFourPi)), // (3z^2 - 1)
sqrtf( 15 / ( kFourPi)), // xz
sqrtf( 15 / ( 4 * kFourPi)), // (x^2 - y^2)
sqrtf( 35 / ( 8 * kFourPi)), // y (3x^2 - y^2)
sqrtf(105 / ( 1 * kFourPi)), // z xy
sqrtf( 21 / ( 8 * kFourPi)), // y (5z^2 - 1)
sqrtf( 7 / ( 4 * kFourPi)), // z (5z^2 - 3)
sqrtf( 21 / ( 8 * kFourPi)), // x (5z^2 - 1)
sqrtf(105 / ( 4 * kFourPi)), // z (x^2 - y^2)
sqrtf( 35 / ( 8 * kFourPi)), // x (x^2 - 3y^2)
sqrtf(315 / ( 4 * kFourPi)), // (x^2 - y^2) xy
sqrtf(315 / ( 8 * kFourPi)), // (3x^2 - y^2) yz
sqrtf( 45 / ( 4 * kFourPi)), // (7z^2 - 1) xy
sqrtf( 45 / ( 8 * kFourPi)), // (7z^2 - 3) yz
sqrtf( 9 / (64 * kFourPi)), // (3 - 30z^2 + 35z^4)
sqrtf( 45 / ( 8 * kFourPi)), // (7z^2 - 3) xz
sqrtf( 45 / (16 * kFourPi)), // (7z^2 - 1) (x^2 -y^2)
sqrtf(315 / ( 8 * kFourPi)), // (x^2 - 3y^2) xz
sqrtf(315 / (64 * kFourPi)), // (x^4 - 6 x^2 y^2 + y^4)
};
const float* SHL::kSH_Y0 = kSH_Y + 0;
const float* SHL::kSH_Y1 = kSH_Y + 1;
const float* SHL::kSH_Y2 = kSH_Y + 4;
const float* SHL::kSH_Y3 = kSH_Y + 9;
const float* SHL::kSH_Y4 = kSH_Y + 16;
// Main SH utils
namespace
{
template<typename T> inline T SampleSH(Vec3f v, int numBands, const T coeffs[])
{
VL_ASSERT(numBands >= 1);
T s(coeffs[0] * kSH_Y_00);
if (numBands < 2)
return s;
const float& x = v[0];
const float& y = v[1];
const float& z = v[2];
float x2 = sqr(x);
float y2 = sqr(y);
float z2 = sqr(z);
s += coeffs[1] * kSH_Y_10 * y;
s += coeffs[2] * kSH_Y_11 * z;
s += coeffs[3] * kSH_Y_12 * x;
if (numBands < 3)
return s;
float xy = x * y;
float yz = y * z;
float xz = x * z;
s += coeffs[4] * kSH_Y_20 * xy;
s += coeffs[5] * kSH_Y_21 * yz;
s += coeffs[6] * kSH_Y_22 * (3.0f * z2 - 1.0f);
s += coeffs[7] * kSH_Y_23 * xz;
s += coeffs[8] * kSH_Y_24 * (x2 - y2);
if (numBands < 4)
return s;
s += coeffs[ 9] * kSH_Y_30 * (3.0f * x2 - y2) * y;
s += coeffs[10] * kSH_Y_31 * xy * z;
s += coeffs[11] * kSH_Y_32 * (5.0f * z2 - 1.0f) * y;
s += coeffs[12] * kSH_Y_33 * (5.0f * z2 - 3.0f) * z;
s += coeffs[13] * kSH_Y_34 * (5.0f * z2 - 1.0f) * x;
s += coeffs[14] * kSH_Y_35 * (x2 - y2) * z;
s += coeffs[15] * kSH_Y_36 * (x2 - 3.0f * y2) * x;
if (numBands < 5)
return s;
s += coeffs[16] * kSH_Y_40 * (x2 - y2) * xy;
s += coeffs[17] * kSH_Y_41 * (3.0f * x2 - y2) * yz;
s += coeffs[18] * kSH_Y_42 * (7.0f * z2 - 1.0f) * xy;
s += coeffs[19] * kSH_Y_43 * (7.0f * z2 - 3.0f) * yz;
s += coeffs[20] * kSH_Y_44 * (3.0f - 30.0f * z2 + 35.0f * z2 * z2);
s += coeffs[21] * kSH_Y_45 * (7.0f * z2 - 3.0f) * xz;
s += coeffs[22] * kSH_Y_46 * (7.0f * z2 - 1.0f) * (x2 - y2);
s += coeffs[23] * kSH_Y_47 * (x2 - 3.0f * y2) * xz;
s += coeffs[24] * kSH_Y_48 * (x2 * x2 - 6.0f * x2 * y2 + y2 * y2);
if (numBands < 6)
return s;
coeffs += 25;
for (int l = 5; l < numBands; l++)
for (int m = -l; m <= l; m++)
s += (*coeffs++) * SH(l, m, v);
return s;
}
}
float SHL::SampleSH(Vec3f v, int numBands, const float coeffs[])
{
return ::SampleSH<float>(v, numBands, coeffs);
}
Vec4f SHL::SampleSH(Vec3f v, int numBands, const Vec4f coeffs[])
{
return ::SampleSH<Vec4f>(v, numBands, coeffs);
}
void SHL::CalcSHWeights(Vec3f v, int numBands, float w[])
{
VL_ASSERT(numBands >= 1);
w[0] = kSH_Y_00;
if (numBands < 2)
return;
const float& x = v[0];
const float& y = v[1];
const float& z = v[2];
float x2 = sqr(x);
float y2 = sqr(y);
float z2 = sqr(z);
w[1] = kSH_Y_10 * y;
w[2] = kSH_Y_11 * z;
w[3] = kSH_Y_12 * x;
if (numBands < 3)
return;
float xy = x * y;
float yz = y * z;
float xz = x * z;
w[4] = kSH_Y_20 * xy;
w[5] = kSH_Y_21 * yz;
w[6] = kSH_Y_22 * (3.0f * z2 - 1.0f);
w[7] = kSH_Y_23 * xz;
w[8] = kSH_Y_24 * (x2 - y2);
if (numBands < 4)
return;
w[ 9] = kSH_Y_30 * (3.0f * x2 - y2) * y;
w[10] = kSH_Y_31 * xy * z;
w[11] = kSH_Y_32 * (5.0f * z2 - 1.0f) * y;
w[12] = kSH_Y_33 * (5.0f * z2 - 3.0f) * z;
w[13] = kSH_Y_34 * (5.0f * z2 - 1.0f) * x;
w[14] = kSH_Y_35 * (x2 - y2) * z;
w[15] = kSH_Y_36 * (x2 - 3.0f * y2) * x;
if (numBands < 5)
return;
w[16] = kSH_Y_40 * (x2 - y2) * xy;
w[17] = kSH_Y_41 * (3.0f * x2 - y2) * yz;
w[18] = kSH_Y_42 * (7.0f * z2 - 1.0f) * xy;
w[19] = kSH_Y_43 * (7.0f * z2 - 3.0f) * yz;
w[20] = kSH_Y_44 * (3.0f - 30.0f * z2 + 35.0f * z2 * z2);
w[21] = kSH_Y_45 * (7.0f * z2 - 3.0f) * xz;
w[22] = kSH_Y_46 * (7.0f * z2 - 1.0f) * (x2 - y2);
w[23] = kSH_Y_47 * (x2 - 3.0f * y2) * xz;
w[24] = kSH_Y_48 * (x2 * x2 - 6.0f * x2 * y2 + y2 * y2);
if (numBands < 6)
return;
w += 25;
for (int l = 5; l < numBands; l++)
for (int m = -l; m <= l; m++)
*w++ = SH(l, m, v);
}
void SHL::AddSHSample(float s, Vec3f v, int numBands, float coeffs[])
{
VL_ASSERT(numBands >= 1);
coeffs[0] += s * kSH_Y_00;
if (numBands < 2)
return;
const float& x = v[0];
const float& y = v[1];
const float& z = v[2];
coeffs[1] += s * kSH_Y_10 * y;
coeffs[2] += s * kSH_Y_11 * z;
coeffs[3] += s * kSH_Y_12 * x;
if (numBands < 3)
return;
float x2 = sqr(x);
float y2 = sqr(y);
float z2 = sqr(z);
float xy = x * y;
float yz = y * z;
float xz = x * z;
coeffs[4] += s * kSH_Y_20 * xy;
coeffs[5] += s * kSH_Y_21 * yz;
coeffs[6] += s * kSH_Y_22 * (3.0f * z2 - 1.0f);
coeffs[7] += s * kSH_Y_23 * xz;
coeffs[8] += s * kSH_Y_24 * (x2 - y2);
if (numBands < 4)
return;
coeffs[ 9] += s * kSH_Y_30 * (3.0f * x2 - y2) * y;
coeffs[10] += s * kSH_Y_31 * xy * z;
coeffs[11] += s * kSH_Y_32 * (5.0f * z2 - 1.0f) * y;
coeffs[12] += s * kSH_Y_33 * (5.0f * z2 - 3.0f) * z;
coeffs[13] += s * kSH_Y_34 * (5.0f * z2 - 1.0f) * x;
coeffs[14] += s * kSH_Y_35 * (x2 - y2) * z;
coeffs[15] += s * kSH_Y_36 * (x2 - 3.0f * y2) * x;
if (numBands < 5)
return;
coeffs[16] += s * kSH_Y_40 * (x2 - y2) * xy;
coeffs[17] += s * kSH_Y_41 * (3.0f * x2 - y2) * yz;
coeffs[18] += s * kSH_Y_42 * (7.0f * z2 - 1.0f) * xy;
coeffs[19] += s * kSH_Y_43 * (7.0f * z2 - 3.0f) * yz;
coeffs[20] += s * kSH_Y_44 * (3.0f - 30.0f * z2 + 35.0f * z2 * z2);
coeffs[21] += s * kSH_Y_45 * (7.0f * z2 - 3.0f) * xz;
coeffs[22] += s * kSH_Y_46 * (7.0f * z2 - 1.0f) * (x2 - y2);
coeffs[23] += s * kSH_Y_47 * (x2 - 3.0f * y2) * xz;
coeffs[24] += s * kSH_Y_48 * (x2 * x2 - 6.0f * x2 * y2 + y2 * y2);
if (numBands < 6)
return;
coeffs += 25;
for (int l = 5; l < numBands; l++)
for (int m = -l; m <= l; m++)
*coeffs++ = s * SH(l, m, v);
}
void SHL::AddSHSample(Vec4f s, Vec3f v, int numBands, Vec4f coeffs[])
{
VL_ASSERT(numBands >= 1);
coeffs[0] += s * kSH_Y_00;
if (numBands < 2)
return;
const float& x = v[0];
const float& y = v[1];
const float& z = v[2];
coeffs[1] += s * kSH_Y_10 * y;
coeffs[2] += s * kSH_Y_11 * z;
coeffs[3] += s * kSH_Y_12 * x;
if (numBands < 3)
return;
float x2 = sqr(x);
float y2 = sqr(y);
float z2 = sqr(z);
float xy = x * y;
float yz = y * z;
float xz = x * z;
coeffs[4] += s * kSH_Y_20 * xy;
coeffs[5] += s * kSH_Y_21 * yz;
coeffs[6] += s * kSH_Y_22 * (3.0f * z2 - 1.0f);
coeffs[7] += s * kSH_Y_23 * xz;
coeffs[8] += s * kSH_Y_24 * (x2 - y2);
if (numBands < 4)
return;
coeffs[ 9] += s * kSH_Y_30 * (3.0f * x2 - y2) * y;
coeffs[10] += s * kSH_Y_31 * xy * z;
coeffs[11] += s * kSH_Y_32 * (5.0f * z2 - 1.0f) * y;
coeffs[12] += s * kSH_Y_33 * (5.0f * z2 - 3.0f) * z;
coeffs[13] += s * kSH_Y_34 * (5.0f * z2 - 1.0f) * x;
coeffs[14] += s * kSH_Y_35 * (x2 - y2) * z;
coeffs[15] += s * kSH_Y_36 * (x2 - 3.0f * y2) * x;
if (numBands < 5)
return;
coeffs[16] += s * kSH_Y_40 * (x2 - y2) * xy;
coeffs[17] += s * kSH_Y_41 * (3.0f * x2 - y2) * yz;
coeffs[18] += s * kSH_Y_42 * (7.0f * z2 - 1.0f) * xy;
coeffs[19] += s * kSH_Y_43 * (7.0f * z2 - 3.0f) * yz;
coeffs[20] += s * kSH_Y_44 * (3.0f - 30.0f * z2 + 35.0f * z2 * z2);
coeffs[21] += s * kSH_Y_45 * (7.0f * z2 - 3.0f) * xz;
coeffs[22] += s * kSH_Y_46 * (7.0f * z2 - 1.0f) * (x2 - y2);
coeffs[23] += s * kSH_Y_47 * (x2 - 3.0f * y2) * xz;
coeffs[24] += s * kSH_Y_48 * (x2 * x2 - 6.0f * x2 * y2 + y2 * y2);
if (numBands < 6)
return;
coeffs += 25;
for (int l = 5; l < numBands; l++)
for (int m = -l; m <= l; m++)
*coeffs++ = s * SH(l, m, v);
}
namespace
{
template<typename T> inline void AddSHSampleHemi(T sIn, Vec3f v, int numBands, T coeffs[])
{
VL_ASSERT(numBands >= 1);
T s(2.0f * sIn);
coeffs[0] += s * kSH_Y_00;
if (numBands < 2)
return;
const float& x = v[0];
const float& y = v[1];
coeffs[1] += s * kSH_Y_10 * y;
coeffs[3] += s * kSH_Y_12 * x;
if (numBands < 3)
return;
const float& z = v[2];
float x2 = sqr(x);
float y2 = sqr(y);
float z2 = sqr(z);
float xy = x * y;
coeffs[4] += s * kSH_Y_20 * xy;
coeffs[6] += s * kSH_Y_22 * (3.0f * z2 - 1.0f);
coeffs[8] += s * kSH_Y_24 * (x2 - y2);
if (numBands < 4)
return;
coeffs[ 9] += s * kSH_Y_30 * (3.0f * x2 - y2) * y;
coeffs[11] += s * kSH_Y_32 * (5.0f * z2 - 1.0f ) * y;
coeffs[13] += s * kSH_Y_34 * (5.0f * z2 - 1.0f ) * x;
coeffs[15] += s * kSH_Y_36 * ( x2 - 3.0f * y2) * x;
if (numBands < 5)
return;
coeffs[16] += s * kSH_Y_40 * (x2 - y2) * xy;
coeffs[18] += s * kSH_Y_42 * (7.0f * z2 - 1.0f) * xy;
coeffs[20] += s * kSH_Y_44 * (3.0f - 30.0f * z2 + 35.0f * z2 * z2);
coeffs[22] += s * kSH_Y_46 * (7.0f * z2 - 1.0f) * (x2 - y2);
coeffs[24] += s * kSH_Y_48 * (x2 * x2 - 6.0f * x2 * y2 + y2 * y2);
if (numBands < 6)
return;
coeffs += 25;
for (int l = 5; l < numBands; l++)
{
for (int m = -l; m <= l; m += 2)
coeffs[m + l] += s * SH(l, m, v);
coeffs += 2 * l + 1;
}
}
}
void SHL::AddSHSampleHemi(float s, Vec3f v, int numBands, float coeffs[])
{
::AddSHSampleHemi<float>(s, v, numBands, coeffs);
}
void SHL::AddSHSampleHemi(Vec4f s, Vec3f v, int numBands, Vec4f coeffs[])
{
::AddSHSampleHemi<Vec4f>(s, v, numBands, coeffs);
}
namespace
{
// The VS compiler amongst others doesn't convert sqrt to constants at compile time,
// so we pull out these constants to avoid lots of sqrt calls in Rotate*.
// You'd think constexpr would solve this, but not in over a decade.
const float kSqrt02_01 = sqrt( 2.0 / 1.0);
const float kSqrt01_02 = sqrt( 1.0 / 2.0);
const float kSqrt03_02 = sqrt( 3.0 / 2.0);
const float kSqrt01_03 = sqrt( 1.0 / 3.0);
const float kSqrt02_03 = sqrt( 2.0 / 3.0);
const float kSqrt04_03 = sqrt( 4.0 / 3.0);
const float kSqrt01_04 = sqrt( 1.0 / 4.0);
const float kSqrt03_04 = sqrt( 3.0 / 4.0);
const float kSqrt05_04 = sqrt( 5.0 / 4.0);
const float kSqrt01_05 = sqrt( 1.0 / 5.0);
const float kSqrt02_05 = sqrt( 2.0 / 5.0);
const float kSqrt03_05 = sqrt( 3.0 / 5.0);
const float kSqrt04_05 = sqrt( 4.0 / 5.0);
const float kSqrt06_05 = sqrt( 6.0 / 5.0);
const float kSqrt08_05 = sqrt( 8.0 / 5.0);
const float kSqrt09_05 = sqrt( 9.0 / 5.0);
const float kSqrt01_06 = sqrt( 1.0 / 6.0);
const float kSqrt05_06 = sqrt( 5.0 / 6.0);
const float kSqrt07_06 = sqrt( 7.0 / 6.0);
const float kSqrt02_07 = sqrt(02.0 / 7.0);
const float kSqrt06_07 = sqrt( 6.0 / 7.0);
const float kSqrt10_07 = sqrt(10.0 / 7.0);
const float kSqrt12_07 = sqrt(12.0 / 7.0);
const float kSqrt15_07 = sqrt(15.0 / 7.0);
const float kSqrt16_07 = sqrt(16.0 / 7.0);
const float kSqrt01_08 = sqrt( 1.0 / 8.0);
const float kSqrt03_08 = sqrt( 3.0 / 8.0);
const float kSqrt05_08 = sqrt( 5.0 / 8.0);
const float kSqrt07_08 = sqrt( 7.0 / 8.0);
const float kSqrt09_08 = sqrt( 9.0 / 8.0);
const float kSqrt05_09 = sqrt( 5.0 / 9.0);
const float kSqrt08_09 = sqrt( 8.0 / 9.0);
const float kSqrt01_10 = sqrt( 1.0 / 10.0);
const float kSqrt03_10 = sqrt( 3.0 / 10.0);
const float kSqrt07_10 = sqrt( 7.0 / 10.0);
const float kSqrt09_10 = sqrt( 9.0 / 10.0);
const float kSqrt01_12 = sqrt( 1.0 / 12.0);
const float kSqrt07_12 = sqrt( 7.0 / 12.0);
const float kSqrt11_12 = sqrt(11.0 / 12.0);
const float kSqrt01_14 = sqrt( 1.0 / 14.0);
const float kSqrt03_14 = sqrt( 3.0 / 14.0);
const float kSqrt15_14 = sqrt(15.0 / 14.0);
const float kSqrt04_15 = sqrt( 4.0 / 15.0);
const float kSqrt07_15 = sqrt( 7.0 / 10.0);
const float kSqrt14_15 = sqrt(14.0 / 15.0);
const float kSqrt16_15 = sqrt(16.0 / 15.0);
const float kSqrt01_16 = sqrt( 1.0 / 16.0);
const float kSqrt03_16 = sqrt( 3.0 / 16.0);
const float kSqrt07_16 = sqrt( 7.0 / 16.0);
const float kSqrt15_16 = sqrt(15.0 / 16.0);
const float kSqrt01_18 = sqrt( 1.0 / 18.0);
const float kSqrt01_24 = sqrt( 1.0 / 24.0);
const float kSqrt03_25 = sqrt( 3.0 / 25.0);
const float kSqrt09_25 = sqrt( 9.0 / 25.0);
const float kSqrt14_25 = sqrt(14.0 / 25.0);
const float kSqrt16_25 = sqrt(16.0 / 25.0);
const float kSqrt18_25 = sqrt(18.0 / 25.0);
const float kSqrt21_25 = sqrt(21.0 / 25.0);
const float kSqrt24_25 = sqrt(24.0 / 25.0);
const float kSqrt03_28 = sqrt( 3.0 / 28.0);
const float kSqrt05_28 = sqrt( 5.0 / 28.0);
const float kSqrt01_30 = sqrt( 1.0 / 30.0);
const float kSqrt01_32 = sqrt( 1.0 / 32.0);
const float kSqrt03_32 = sqrt( 3.0 / 32.0);
const float kSqrt15_32 = sqrt(15.0 / 32.0);
const float kSqrt21_32 = sqrt(21.0 / 32.0);
const float kSqrt11_36 = sqrt(11.0 / 36.0);
const float kSqrt35_36 = sqrt(35.0 / 36.0);
const float kSqrt01_50 = sqrt( 1.0 / 50.0);
const float kSqrt03_50 = sqrt( 3.0 / 50.0);
const float kSqrt21_50 = sqrt(21.0 / 50.0);
const float kSqrt15_56 = sqrt(15.0 / 56.0);
const float kSqrt01_60 = sqrt( 1.0 / 60.0);
const float kSqrt01_112 = sqrt( 1.0 / 112.0);
const float kSqrt03_112 = sqrt( 3.0 / 112.0);
const float kSqrt15_112 = sqrt(15.0 / 112.0);
template<typename T> void RotateZHToSH(const Vec3f& dir, int n, const T* zcoeffs, T* coeffs)
{
VL_ASSERT(zcoeffs != coeffs);
VL_ASSERT(n >= 2 && n <= 8);
coeffs[0] = zcoeffs[0];
if (n < 2)
return;
coeffs++;
float sh1[3] = { dir[1], dir[2], dir[0] };
coeffs[0] = zcoeffs[1] * sh1[0];
coeffs[1] = zcoeffs[1] * sh1[1];
coeffs[2] = zcoeffs[1] * sh1[2];
if (n < 3)
return;
coeffs += 3;
float sh2[5];
sh2[0] = kSqrt03_04 * (sh1[2] * sh1[0] + sh1[0] * sh1[2]);
sh2[1] = kSqrt03_04 * (sh1[1] * sh1[0] + sh1[0] * sh1[1]);
sh2[2] = -kSqrt01_04 * (sh1[2] * sh1[2] + sh1[0] * sh1[0]) + sh1[1] * sh1[1];
sh2[3] = kSqrt03_04 * (sh1[1] * sh1[2] + sh1[2] * sh1[1]);
sh2[4] = kSqrt03_04 * (sh1[2] * sh1[2] - sh1[0] * sh1[0]);
for (int i = 0; i < 5; i++)
coeffs[i] = zcoeffs[2] * sh2[i];
if (n < 4)
return;
coeffs += 5;
float sh3[7];
sh3[0] = kSqrt05_06 * (sh1[2] * sh2[0] + sh1[0] * sh2[4]);
sh3[1] = kSqrt05_09 * (sh1[1] * sh2[0] + (sh1[2] * sh2[1] + sh1[0] * sh2[3]));
sh3[2] = kSqrt08_09 * sh1[1] * sh2[1] + kSqrt02_03 * sh1[0] * sh2[2] - kSqrt01_18 * (sh1[2] * sh2[0] - sh1[0] * sh2[4]);
sh3[3] = sh1[1] * sh2[2] - kSqrt01_03 * (sh1[2] * sh2[3] + sh1[0] * sh2[1]);
sh3[4] = kSqrt08_09 * sh1[1] * sh2[3] + kSqrt02_03 * sh1[2] * sh2[2] - kSqrt01_18 * (sh1[2] * sh2[4] + sh1[0] * sh2[0]);
sh3[5] = kSqrt05_09 * (sh1[1] * sh2[4] + (sh1[2] * sh2[3] - sh1[0] * sh2[1]));
sh3[6] = kSqrt05_06 * (sh1[2] * sh2[4] - sh1[0] * sh2[0]);
for (int i = 0; i < 7; i++)
coeffs[i] = zcoeffs[3] * sh3[i];
if (n < 5)
return;
coeffs += 7;
float sh4[9];
sh4[0] = kSqrt07_08 * (sh1[2] * sh3[0] + sh1[0] * sh3[6]);
sh4[1] = kSqrt07_16 * sh1[1] * sh3[0] + kSqrt21_32 * (sh1[2] * sh3[1] + sh1[0] * sh3[5]);
sh4[2] = kSqrt03_04 * sh1[1] * sh3[1] + kSqrt15_32 * (sh1[2] * sh3[2] + sh1[0] * sh3[4]) - kSqrt01_32 * (sh1[2] * sh3[0] - sh1[0] * sh3[6]);
sh4[3] = kSqrt15_16 * sh1[1] * sh3[2] + kSqrt05_08 * sh1[0] * sh3[3] - kSqrt03_32 * (sh1[2] * sh3[1] - sh1[0] * sh3[5]);
sh4[4] = sh1[1] * sh3[3] - kSqrt03_08 * (sh1[2] * sh3[4] + sh1[0] * sh3[2]);
sh4[5] = kSqrt15_16 * sh1[1] * sh3[4] + kSqrt05_08 * sh1[2] * sh3[3] - kSqrt03_32 * (sh1[2] * sh3[5] + sh1[0] * sh3[1]);
sh4[6] = kSqrt03_04 * sh1[1] * sh3[5] + kSqrt15_32 * (sh1[2] * sh3[4] - sh1[0] * sh3[2]) - kSqrt01_32 * (sh1[2] * sh3[6] + sh1[0] * sh3[0]);
sh4[7] = kSqrt07_16 * sh1[1] * sh3[6] + kSqrt21_32 * (sh1[2] * sh3[5] - sh1[0] * sh3[1]);
sh4[8] = kSqrt07_08 * (sh1[2] * sh3[6] - sh1[0] * sh3[0]);
for (int i = 0; i < 9; i++)
coeffs[i] = zcoeffs[4] * sh4[i];
if (n < 6)
return;
coeffs += 9;
float sh5[11];
sh5[ 0] = kSqrt09_10 * (sh1[2] * sh4[0] + sh1[0] * sh4[8]);
sh5[ 1] = kSqrt09_25 * sh1[1] * sh4[0] + kSqrt18_25 * (sh1[2] * sh4[1] + sh1[0] * sh4[7]);
sh5[ 2] = kSqrt16_25 * sh1[1] * sh4[1] + kSqrt14_25 * (sh1[2] * sh4[2] + sh1[0] * sh4[6]) - kSqrt01_50 * (sh1[2] * sh4[0] - sh1[0] * sh4[8]);
sh5[ 3] = kSqrt21_25 * sh1[1] * sh4[2] + kSqrt21_50 * (sh1[2] * sh4[3] + sh1[0] * sh4[5]) - kSqrt03_50 * (sh1[2] * sh4[1] - sh1[0] * sh4[7]);
sh5[ 4] = kSqrt24_25 * sh1[1] * sh4[3] + kSqrt03_05 * sh1[0] * sh4[4] - kSqrt03_25 * (sh1[2] * sh4[2] - sh1[0] * sh4[6]);
sh5[ 5] = sh1[1] * sh4[4] - kSqrt02_05 * (sh1[2] * sh4[5] + sh1[0] * sh4[3]);
sh5[ 6] = kSqrt24_25 * sh1[1] * sh4[5] + kSqrt03_05 * sh1[2] * sh4[4] - kSqrt03_25 * (sh1[2] * sh4[6] + sh1[0] * sh4[2]);
sh5[ 7] = kSqrt21_25 * sh1[1] * sh4[6] + kSqrt21_50 * (sh1[2] * sh4[5] - sh1[0] * sh4[3]) - kSqrt03_50 * (sh1[2] * sh4[7] + sh1[0] * sh4[1]);
sh5[ 8] = kSqrt16_25 * sh1[1] * sh4[7] + kSqrt14_25 * (sh1[2] * sh4[6] - sh1[0] * sh4[2]) - kSqrt01_50 * (sh1[2] * sh4[8] + sh1[0] * sh4[0]);
sh5[ 9] = kSqrt09_25 * sh1[1] * sh4[8] + kSqrt18_25 * (sh1[2] * sh4[7] - sh1[0] * sh4[1]);
sh5[10] = kSqrt09_10 * (sh1[2] * sh4[8] - sh1[0] * sh4[0]);
for (int i = 0; i < 11; i++)
coeffs[i] = zcoeffs[5] * sh5[i];
if (n < 7)
return;
coeffs += 11;
float sh6[13];
sh6[ 0] = kSqrt11_12 * (sh1[2] * sh5[0] + sh1[0] * sh5[10]);
sh6[ 1] = kSqrt11_36 * sh1[1] * sh5[0] + sqrtf(55.0 / 72.0) * (sh1[2] * sh5[1] + sh1[0] * sh5[9]);
sh6[ 2] = kSqrt05_09 * sh1[1] * sh5[1] + kSqrt05_08 * (sh1[2] * sh5[2] + sh1[0] * sh5[8]) - sqrtf(1.0 / 72.0) * (sh1[2] * sh5[0] - sh1[0] * sh5[10]);
sh6[ 3] = kSqrt03_04 * sh1[1] * sh5[2] + kSqrt01_02 * (sh1[2] * sh5[3] + sh1[0] * sh5[7]) - kSqrt01_24 * (sh1[2] * sh5[1] - sh1[0] * sh5[9]);
sh6[ 4] = kSqrt08_09 * sh1[1] * sh5[3] + sqrtf(7.0 / 18.0) * (sh1[2] * sh5[4] + sh1[0] * sh5[6]) - kSqrt01_12 * (sh1[2] * sh5[2] - sh1[0] * sh5[8]);
sh6[ 5] = kSqrt35_36 * sh1[1] * sh5[4] + kSqrt07_12 * sh1[0] * sh5[5] - sqrtf(5.0 / 36.0) * (sh1[2] * sh5[3] - sh1[0] * sh5[7]);
sh6[ 6] = sh1[1] * sh5[5] - sqrtf(5.0 / 12.0) * (sh1[2] * sh5[6] + sh1[0] * sh5[4]);
sh6[ 7] = kSqrt35_36 * sh1[1] * sh5[6] + kSqrt07_12 * sh1[2] * sh5[5] - sqrtf(5.0 / 36.0) * (sh1[2] * sh5[7] + sh1[0] * sh5[3]);
sh6[ 8] = kSqrt08_09 * sh1[1] * sh5[7] + sqrtf(7.0 / 18.0) * (sh1[2] * sh5[6] - sh1[0] * sh5[4]) - kSqrt01_12 * (sh1[2] * sh5[8] + sh1[0] * sh5[2]);
sh6[ 9] = kSqrt03_04 * sh1[1] * sh5[8] + kSqrt01_02 * (sh1[2] * sh5[7] - sh1[0] * sh5[3]) - kSqrt01_24 * (sh1[2] * sh5[9] + sh1[0] * sh5[1]);
sh6[10] = kSqrt05_09 * sh1[1] * sh5[9] + kSqrt05_08 * (sh1[2] * sh5[8] - sh1[0] * sh5[2]) - sqrtf(1.0 / 72.0) * (sh1[2] * sh5[10] + sh1[0] * sh5[0]);
sh6[11] = kSqrt11_36 * sh1[1] * sh5[10] + sqrtf(55.0 / 72.0) * (sh1[2] * sh5[9] - sh1[0] * sh5[1]);
sh6[12] = kSqrt11_12 * (sh1[2] * sh5[10] - sh1[0] * sh5[0]);
for (int i = 0; i < 13; i++)
coeffs[i] = zcoeffs[6] * sh6[i];
if (n < 8)
return;
coeffs += 13;
zcoeffs += 7;
for (int l = 8; l < n; l++)
{
T zl = *zcoeffs++ * sqrtf(kFourPi / (2 * l + 1));
for (int m = -l; m <= +l; m++)
*coeffs++ = zl * SH(l, m, dir);
}
}
}
void SHL::RotateZHToSH(const Vec3f& dir, int n, const float zcoeffs[], float coeffs[])
{
#ifdef REFERENCE
for (int l = 0; l < n; l++)
{
float zl = *zcoeffs++ * sqrtf(kFourPi / (2 * l + 1));
for (int m = -l; m <= +l; m++)
*coeffs++ = zl * SH(l, m, dir);
}
#else
::RotateZHToSH<float>(dir, n, zcoeffs, coeffs);
#endif
}
void SHL::RotateZHToSH(const Vec3f& dir, int n, const Vec4f* zcoeffs, Vec4f* coeffs)
{
::RotateZHToSH<Vec4f>(dir, n, zcoeffs, coeffs);
}
namespace
{
template<typename T> void RotateZHToSHAdd(const Vec3f& dir, int n, const T* zcoeffs, T* coeffs)
{
VL_ASSERT(zcoeffs != coeffs);
VL_ASSERT(n >= 2 && n <= 8);
coeffs[0] += zcoeffs[0];
if (n < 2)
return;
coeffs++;
float sh1[3] = { dir[1], dir[2], dir[0] };
coeffs[0] += zcoeffs[1] * sh1[0];
coeffs[1] += zcoeffs[1] * sh1[1];
coeffs[2] += zcoeffs[1] * sh1[2];
if (n < 3)
return;
coeffs += 3;
float sh2[5];
sh2[0] = kSqrt03_04 * (sh1[2] * sh1[0] + sh1[0] * sh1[2]);
sh2[1] = kSqrt03_04 * (sh1[1] * sh1[0] + sh1[0] * sh1[1]);
sh2[2] = -kSqrt01_04 * (sh1[2] * sh1[2] + sh1[0] * sh1[0]) + sh1[1] * sh1[1];
sh2[3] = kSqrt03_04 * (sh1[1] * sh1[2] + sh1[2] * sh1[1]);
sh2[4] = kSqrt03_04 * (sh1[2] * sh1[2] - sh1[0] * sh1[0]);
for (int i = 0; i < 5; i++)
coeffs[i] += zcoeffs[2] * sh2[i];
if (n < 4)
return;
coeffs += 5;
float sh3[7];
sh3[0] = kSqrt05_06 * (sh1[2] * sh2[0] + sh1[0] * sh2[4]);
sh3[1] = kSqrt05_09 * (sh1[1] * sh2[0] + (sh1[2] * sh2[1] + sh1[0] * sh2[3]));
sh3[2] = kSqrt08_09 * sh1[1] * sh2[1] + kSqrt02_03 * sh1[0] * sh2[2] - kSqrt01_18 * (sh1[2] * sh2[0] - sh1[0] * sh2[4]);
sh3[3] = sh1[1] * sh2[2] - kSqrt01_03 * (sh1[2] * sh2[3] + sh1[0] * sh2[1]);
sh3[4] = kSqrt08_09 * sh1[1] * sh2[3] + kSqrt02_03 * sh1[2] * sh2[2] - kSqrt01_18 * (sh1[2] * sh2[4] + sh1[0] * sh2[0]);
sh3[5] = kSqrt05_09 * (sh1[1] * sh2[4] + (sh1[2] * sh2[3] - sh1[0] * sh2[1]));
sh3[6] = kSqrt05_06 * (sh1[2] * sh2[4] - sh1[0] * sh2[0]);
for (int i = 0; i < 7; i++)
coeffs[i] += zcoeffs[3] * sh3[i];
if (n < 5)
return;
coeffs += 7;
float sh4[9];
sh4[0] = kSqrt07_08 * (sh1[2] * sh3[0] + sh1[0] * sh3[6]);
sh4[1] = kSqrt07_16 * sh1[1] * sh3[0] + kSqrt21_32 * (sh1[2] * sh3[1] + sh1[0] * sh3[5]);
sh4[2] = kSqrt03_04 * sh1[1] * sh3[1] + kSqrt15_32 * (sh1[2] * sh3[2] + sh1[0] * sh3[4]) - kSqrt01_32 * (sh1[2] * sh3[0] - sh1[0] * sh3[6]);
sh4[3] = kSqrt15_16 * sh1[1] * sh3[2] + kSqrt05_08 * sh1[0] * sh3[3] - kSqrt03_32 * (sh1[2] * sh3[1] - sh1[0] * sh3[5]);
sh4[4] = sh1[1] * sh3[3] - kSqrt03_08 * (sh1[2] * sh3[4] + sh1[0] * sh3[2]);
sh4[5] = kSqrt15_16 * sh1[1] * sh3[4] + kSqrt05_08 * sh1[2] * sh3[3] - kSqrt03_32 * (sh1[2] * sh3[5] + sh1[0] * sh3[1]);
sh4[6] = kSqrt03_04 * sh1[1] * sh3[5] + kSqrt15_32 * (sh1[2] * sh3[4] - sh1[0] * sh3[2]) - kSqrt01_32 * (sh1[2] * sh3[6] + sh1[0] * sh3[0]);
sh4[7] = kSqrt07_16 * sh1[1] * sh3[6] + kSqrt21_32 * (sh1[2] * sh3[5] - sh1[0] * sh3[1]);
sh4[8] = kSqrt07_08 * (sh1[2] * sh3[6] - sh1[0] * sh3[0]);
for (int i = 0; i < 9; i++)
coeffs[i] += zcoeffs[4] * sh4[i];
if (n < 6)
return;
coeffs += 9;
float sh5[11];
sh5[ 0] = kSqrt09_10 * (sh1[2] * sh4[0] + sh1[0] * sh4[8]);
sh5[ 1] = kSqrt09_25 * sh1[1] * sh4[0] + kSqrt18_25 * (sh1[2] * sh4[1] + sh1[0] * sh4[7]);
sh5[ 2] = kSqrt16_25 * sh1[1] * sh4[1] + kSqrt14_25 * (sh1[2] * sh4[2] + sh1[0] * sh4[6]) - kSqrt01_50 * (sh1[2] * sh4[0] - sh1[0] * sh4[8]);
sh5[ 3] = kSqrt21_25 * sh1[1] * sh4[2] + kSqrt21_50 * (sh1[2] * sh4[3] + sh1[0] * sh4[5]) - kSqrt03_50 * (sh1[2] * sh4[1] - sh1[0] * sh4[7]);
sh5[ 4] = kSqrt24_25 * sh1[1] * sh4[3] + kSqrt03_05 * sh1[0] * sh4[4] - kSqrt03_25 * (sh1[2] * sh4[2] - sh1[0] * sh4[6]);
sh5[ 5] = sh1[1] * sh4[4] - kSqrt02_05 * (sh1[2] * sh4[5] + sh1[0] * sh4[3]);
sh5[ 6] = kSqrt24_25 * sh1[1] * sh4[5] + kSqrt03_05 * sh1[2] * sh4[4] - kSqrt03_25 * (sh1[2] * sh4[6] + sh1[0] * sh4[2]);
sh5[ 7] = kSqrt21_25 * sh1[1] * sh4[6] + kSqrt21_50 * (sh1[2] * sh4[5] - sh1[0] * sh4[3]) - kSqrt03_50 * (sh1[2] * sh4[7] + sh1[0] * sh4[1]);
sh5[ 8] = kSqrt16_25 * sh1[1] * sh4[7] + kSqrt14_25 * (sh1[2] * sh4[6] - sh1[0] * sh4[2]) - kSqrt01_50 * (sh1[2] * sh4[8] + sh1[0] * sh4[0]);
sh5[ 9] = kSqrt09_25 * sh1[1] * sh4[8] + kSqrt18_25 * (sh1[2] * sh4[7] - sh1[0] * sh4[1]);
sh5[10] = kSqrt09_10 * (sh1[2] * sh4[8] - sh1[0] * sh4[0]);
for (int i = 0; i < 11; i++)
coeffs[i] += zcoeffs[5] * sh5[i];
if (n < 7)
return;
coeffs += 11;
float sh6[13];
sh6[ 0] = kSqrt11_12 * (sh1[2] * sh5[0] + sh1[0] * sh5[10]);
sh6[ 1] = kSqrt11_36 * sh1[1] * sh5[0] + sqrtf(55.0 / 72.0) * (sh1[2] * sh5[1] + sh1[0] * sh5[9]);
sh6[ 2] = kSqrt05_09 * sh1[1] * sh5[1] + kSqrt05_08 * (sh1[2] * sh5[2] + sh1[0] * sh5[8]) - sqrtf(1.0 / 72.0) * (sh1[2] * sh5[0] - sh1[0] * sh5[10]);
sh6[ 3] = kSqrt03_04 * sh1[1] * sh5[2] + kSqrt01_02 * (sh1[2] * sh5[3] + sh1[0] * sh5[7]) - kSqrt01_24 * (sh1[2] * sh5[1] - sh1[0] * sh5[9]);
sh6[ 4] = kSqrt08_09 * sh1[1] * sh5[3] + sqrtf(7.0 / 18.0) * (sh1[2] * sh5[4] + sh1[0] * sh5[6]) - kSqrt01_12 * (sh1[2] * sh5[2] - sh1[0] * sh5[8]);
sh6[ 5] = kSqrt35_36 * sh1[1] * sh5[4] + kSqrt07_12 * sh1[0] * sh5[5] - sqrtf(5.0 / 36.0) * (sh1[2] * sh5[3] - sh1[0] * sh5[7]);
sh6[ 6] = sh1[1] * sh5[5] - sqrtf(5.0 / 12.0) * (sh1[2] * sh5[6] + sh1[0] * sh5[4]);
sh6[ 7] = kSqrt35_36 * sh1[1] * sh5[6] + kSqrt07_12 * sh1[2] * sh5[5] - sqrtf(5.0 / 36.0) * (sh1[2] * sh5[7] + sh1[0] * sh5[3]);
sh6[ 8] = kSqrt08_09 * sh1[1] * sh5[7] + sqrtf(7.0 / 18.0) * (sh1[2] * sh5[6] - sh1[0] * sh5[4]) - kSqrt01_12 * (sh1[2] * sh5[8] + sh1[0] * sh5[2]);
sh6[ 9] = kSqrt03_04 * sh1[1] * sh5[8] + kSqrt01_02 * (sh1[2] * sh5[7] - sh1[0] * sh5[3]) - kSqrt01_24 * (sh1[2] * sh5[9] + sh1[0] * sh5[1]);
sh6[10] = kSqrt05_09 * sh1[1] * sh5[9] + kSqrt05_08 * (sh1[2] * sh5[8] - sh1[0] * sh5[2]) - sqrtf(1.0 / 72.0) * (sh1[2] * sh5[10] + sh1[0] * sh5[0]);
sh6[11] = kSqrt11_36 * sh1[1] * sh5[10] + sqrtf(55.0 / 72.0) * (sh1[2] * sh5[9] - sh1[0] * sh5[1]);
sh6[12] = kSqrt11_12 * (sh1[2] * sh5[10] - sh1[0] * sh5[0]);
for (int i = 0; i < 13; i++)
coeffs[i] += zcoeffs[6] * sh6[i];
if (n < 8)
return;
coeffs += 13;
zcoeffs += 7;
for (int l = 8; l < n; l++)
{
T zl = *zcoeffs++ * sqrtf(kFourPi / (2 * l + 1));
for (int m = -l; m <= +l; m++)
*coeffs++ += zl * SH(l, m, dir);
}
}
}
void SHL::RotateZHToSHAdd(const Vec3f& dir, int n, const float zcoeffs[], float coeffs[])
{
::RotateZHToSHAdd<float>(dir, n, zcoeffs, coeffs);
}
void SHL::RotateZHToSHAdd(const Vec3f& dir, int n, const Vec4f zcoeffs[], Vec4f coeffs[])
{
::RotateZHToSHAdd<Vec4f>(dir, n, zcoeffs, coeffs);
}
namespace