-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1602 lines (1314 loc) · 85.3 KB
/
main.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
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STBI_MSC_SECURE_CRT
#include "external/stb/stb_image_write.h"
#include "external/fmath/fmath.hpp"
#include "vec3.h"
#include "util.h"
#include "etc.h"
#include "hdr.h"
#include "test.h"
#include "XYZ.h"
#include "bbox.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <atomic>
// #define TEST
class FloatImage
{
private:
size_t width_, height_;
std::vector<hmath::Double3> data_;
std::vector<hmath::Double3> data2_;
std::vector<uint32_t> sample_map_;
public:
FloatImage(size_t w, size_t h) : width_(w), height_(h), data_(width_ * height_), data2_(width_ * height_), sample_map_(width_ * height_)
{
}
size_t width() const { return width_; }
size_t height() const { return height_; }
hmath::Double3& operator()(int x, int y)
{
return data_[x + y * width_];
}
hmath::Double3& data2(int x, int y)
{
return data2_[x + y * width_];
}
uint32_t& samples(int x, int y)
{
return sample_map_[x + y * width_];
}
};
namespace
{
const hmath::Double3 rgb2y(0.2126, 0.7152, 0.0722);
}
// この辺過去のコードもってきただけ
namespace filter
{
void rgb2YCoCg(float r, float g, float b, float *Y, float *Co, float *Cg) {
//*Y = 1 / 4.0f * r + 1 / 2.0f * g + 1 / 4.0f * b;
//*Co = 1 / 2.0f * r + 0 / 1.0f * g - 1 / 2.0f * b;
//*Cg = -1 / 4.0f * r + 1 / 2.0f * g - 1 / 4.0f * b;
*Y = r;
*Co = g;
*Cg = b;
}
void YCoCg2rgb(float Y, float Co, float Cg, float *r, float *g, float *b) {
/*
*r = Y + Co - Cg;
*g = Y + 0 + Cg;
*b = Y - Co - Cg;
*/
*r = Y;
*g = Co;
*b = Cg;
}
void set_vector(std::vector<float>& arr, const int x, const int y, const int ch, const int width, const int height, float *vector, int learn_radius) {
const int size = learn_radius * 2 + 1;
for (int oy = -learn_radius; oy <= learn_radius; ++oy) {
for (int ox = -learn_radius; ox <= learn_radius; ++ox) {
const int nx = ox + x;
const int ny = oy + y;
if (0 <= nx && nx < width && 0 <= ny && ny < height) {
vector[(oy + learn_radius) * size + (ox + learn_radius)] = arr[(ny * width + nx) * 3 + ch];
}
}
}
}
float length(float *v0, float *v1, int size) {
float sum = 0;
for (int i = 0; i < size; ++i) {
const float a = v0[i] - v1[i];
sum += a * a;
}
return sum;
}
void nlm(std::vector<float>& arr0, std::vector<float>& arr1, const int width, const int height) {
const float sigma = 0.3f;
// 変換
for (int iy = 0; iy < height; ++iy) {
for (int ix = 0; ix < width; ++ix) {
const int idx = iy * width + ix;
const float r = arr0[idx * 3 + 0];
const float g = arr0[idx * 3 + 1];
const float b = arr0[idx * 3 + 2];
rgb2YCoCg(r, g, b, &arr0[idx * 3 + 0], &arr0[idx * 3 + 1], &arr0[idx * 3 + 2]);
}
}
// NLM
for (int iy = 0; iy < height; ++iy) {
std::cout << "Y: " << iy << " \r";
std::vector<std::thread> thread_list;
int thread_id = 0;
for (int begin_x = 0; begin_x < width; begin_x += 16) {
int end_x = begin_x + 16;
if (end_x >= width)
end_x = width;
std::thread thread([thread_id, begin_x, end_x, iy, width, height, sigma, &arr0, &arr1]() {
hetc::set_thread_group(thread_id);
const int learn_radius = 3;
const int size = learn_radius * 2 + 1;
const int compare_raidus = 6;
for (int ix = begin_x; ix < end_x; ++ix) {
for (int ch = 0; ch < 3; ++ch) {
float vector0[size * size] = { 0 };
set_vector(arr0, ix, iy, ch, width, height, vector0, learn_radius);
const int compare_size = compare_raidus * 2 + 1;
float weight_map[compare_size * compare_size] = { 0 };
float value_map[compare_size * compare_size] = { 0 };
// 探索
for (int oy = -compare_raidus; oy <= compare_raidus; ++oy) {
for (int ox = -compare_raidus; ox <= compare_raidus; ++ox) {
const int nx = ox + ix;
const int ny = oy + iy;
const int compare_idx = (oy + compare_raidus) * compare_size + (ox + compare_raidus);
if (0 <= nx && nx < width && 0 <= ny && ny < height) {
float vector1[size * size] = { 0 };
set_vector(arr0, nx, ny, ch, width, height, vector1, learn_radius);
// 重み計算
value_map[compare_idx] = arr0[(ny * width + nx) * 3 + ch];
weight_map[compare_idx] = length(vector0, vector1, size * size);
}
else {
weight_map[compare_idx] = -1;
}
}
}
// 結果計算
float sum = 0;
float total_weight = 0;
for (int cy = 0; cy < compare_size; ++cy) {
for (int cx = 0; cx < compare_size; ++cx) {
const int compare_idx = cy * compare_size + cx;
if (weight_map[compare_idx] < 0)
continue;
const float weight = exp(-weight_map[compare_idx] / (sigma * sigma));
sum += value_map[compare_idx] * weight;
total_weight += weight;
}
}
if (total_weight > 0)
sum /= total_weight;
arr1[(iy * width + ix) * 3 + ch] = sum;
}
const int idx = iy * width + ix;
const float Y = arr1[idx * 3 + 0];
const float Co = arr1[idx * 3 + 1];
const float Cg = arr1[idx * 3 + 2];
YCoCg2rgb(Y, Co, Cg, &arr1[idx * 3 + 0], &arr1[idx * 3 + 1], &arr1[idx * 3 + 2]);
}
});
thread_list.push_back(std::move(thread));
++thread_id;
}
for (auto& th : thread_list) {
th.join();
}
}
}
void median(std::vector<float>& input, std::vector<float>& output, const int width, const int height)
{
for (int iy = 0; iy < height; ++iy) {
std::cout << "Y: " << iy << " \r";
for (int ix = 0; ix < width; ++ix) {
for (int ch = 0; ch < 3; ++ch) {
float p[9] = {};
int index = 0;
for (int oy = -1; oy <= 1; ++oy) {
for (int ox = -1; ox <= 1; ++ox) {
int current_x = ix + ox;
if (current_x < 0)
current_x = 0;
else if (width <= current_x)
current_x = width - 1;
int current_y = iy + oy;
if (current_y < 0)
current_y = 0;
else if (height <= current_y)
current_y = height - 1;
p[index] = input[(current_x + current_y * width) * 3 + ch];
++index;
}
}
std::sort(p, p + 9);
output[(ix + iy * width) * 3 + ch] = p[4];
}
}
}
}
}
void save_image(const char* filename, FloatImage& image, bool enable_filter = false)
{
const auto Width = image.width();
const auto Height = image.height();
uint64_t total = 0;
std::vector<float> tonemapped_image(Width * Height * 3);
std::vector<uint8_t> uint8_tonemapped_image(Width * Height * 3);
std::vector<float> fdata(Width * Height * 3);
std::vector<float> fdata2(Width * Height * 3);
fmath::PowGenerator degamma(1.2f /* ちょいコントラスト強める */ / 2.2f);
for (int iy = 0; iy < Height; ++iy) {
for (int ix = 0; ix < Width; ++ix) {
auto index = ix + iy * Width;
const double N = image.samples(ix, iy);
auto p = image(ix, iy) / N;
total += image.samples(ix, iy);
//const double lumi = dot(p, rgb2y);
//p[0] = p[1] = p[2] = image.data2(ix, iy)[0] / (N - 1) - (N / (N - 1)) * lumi * lumi;
fdata[index * 3 + 0] = (float)p[0];
fdata[index * 3 + 1] = (float)p[1];
fdata[index * 3 + 2] = (float)p[2];
}
}
for (int iy = 0; iy < Height; ++iy) {
for (int ix = 0; ix < Width; ++ix) {
auto index = ix + iy * Width;
fdata2[index * 3 + 0] = (degamma.get(hmath::clamp((float)fdata[index * 3 + 0], 0.0f, 1.0f)));
fdata2[index * 3 + 1] = (degamma.get(hmath::clamp((float)fdata[index * 3 + 1], 0.0f, 1.0f)));
fdata2[index * 3 + 2] = (degamma.get(hmath::clamp((float)fdata[index * 3 + 2], 0.0f, 1.0f)));
}
}
if (enable_filter) {
// filter::median(fdata2, tonemapped_image, Width, Height);
for (int iy = 0; iy < Height; ++iy) {
for (int ix = 0; ix < Width; ++ix) {
auto index = ix + iy * Width;
uint8_tonemapped_image[index * 3 + 0] = (uint8_t)(fdata2[index * 3 + 0] * 255);
uint8_tonemapped_image[index * 3 + 1] = (uint8_t)(fdata2[index * 3 + 1] * 255);
uint8_tonemapped_image[index * 3 + 2] = (uint8_t)(fdata2[index * 3 + 2] * 255);
}
}
}
else {
for (int iy = 0; iy < Height; ++iy) {
for (int ix = 0; ix < Width; ++ix) {
auto index = ix + iy * Width;
uint8_tonemapped_image[index * 3 + 0] = (uint8_t)(fdata2[index * 3 + 0] * 255);
uint8_tonemapped_image[index * 3 + 1] = (uint8_t)(fdata2[index * 3 + 1] * 255);
uint8_tonemapped_image[index * 3 + 2] = (uint8_t)(fdata2[index * 3 + 2] * 255);
}
}
}
double average = (double)total / (Width * Height);
std::cout << "Average: " << average << " samples/pixel" << std::endl;
stbi_write_png(filename, (int)Width, (int)Height, 3, uint8_tonemapped_image.data(), (int)(Width * sizeof(uint8_t) * 3));
// hhdr::save("hoge.hdr", fdata.data(), (int)Width, (int)Height, false);
}
namespace volume
{
template<typename real>
constexpr real phase_Henyey_Greenstein(real cost, real g)
{
return (float)((1.0 / (4.0 * hmath::pi<real>())) * (1 - g * g) / pow(1 + g * g - 2 * g * cost, 3.0 / 2.0));
}
template<typename real, typename rng>
void sample_phase_HenyayGreenstein(real g, rng& rng, real& theta, real& phi)
{
const real u0 = rng.next01();
const real s = 2.0f * u0 - 1;
const real k = (1 - g * g) / (1 + g * s);
const real mu = (1.0f / 2.0f / g) * (1 + g * g - k * k);
theta = acos(hmath::clamp(mu, (real)-1, (real)1));
const real u1 = rng.next01();
phi = 2 * hmath::pi<real>() * u1;
}
template<typename real>
constexpr real phase_Mie(real cost, real g)
{
return (1.0f / (4.0f * hmath::pi<real>())) * (3.0f / 2.0f) * (1 - g * g) / (2 + g * g) * (1 + cost * cost) / pow(1 + g * g - 2 * g * cost, 3.0f / 2.0f);
}
template<typename real, typename transmittance, typename rng>
real delta_tracking(const transmittance& coeff, rng& rng)
{
const real bound = coeff.bound();
auto inv_majorant = 1.0f / coeff.majorant();
real t = 0;
do {
t -= /*log(1.0f - rng.next01())*/ fmath::log(1.0f - rng.next01()) * inv_majorant;
} while (coeff(t) * inv_majorant < rng.next01() && t < bound);
return t;
}
template<typename real, typename transmittance, typename rng>
real ratio_tracking_estimator(const transmittance& coeff, rng& rng)
{
const real bound = coeff.bound();
auto inv_majorant = 1.0f / coeff.majorant();
real t = 0;
real T = 1;
do {
t -= /*log(1.0f - rng.next01())*/ fmath::log(1.0f - rng.next01()) * inv_majorant;
if (t >= bound)
break;
T = T * (1.0f - coeff(t) * inv_majorant);
} while (true);
return T;
}
constexpr float MieScale = 1;
constexpr float scattering_Mie_base = 2.5f; /* [1/m] */
constexpr float H_Mie = 10; /* [m] */
constexpr float Albedo = 0.75f; // 0.999f;
struct HeightExpTable
{
static constexpr int N = 256;
float data[N];
HeightExpTable()
{
for (int i = 0; i < N; ++i) {
data[i] = exp(-(i * 0.25f) / H_Mie);
}
}
float sample(float h) const
{
int i = h * (1.0f / 0.25f);
if (i < 0)
i = 0;
if (N <= i)
i = N - 1;
return data[i];
}
} heightExpTable;
struct VolumeTable
{
float majorant_ = 0;
// octree作るため、2ベキかつ全部同じサイズという前提
static constexpr int NX = 64;
static constexpr int NY = 64;
static constexpr int NZ = 64;
// extinction coeff
float data[NX * NY * NZ];
hmath::Float3 index2pos(int x, int y, int z) const
{
return hmath::Float3(x - NX / 2.0f, y, z - NZ / 2.0f) + hmath::Float3(1, 1, 1) * 0.5f;
}
int pos2index(const hmath::Float3& p) const
{
int ix = hmath::clamp((int)(p[0] + NX / 2.0f), 0, NX - 1);
int iy = hmath::clamp((int)p[1], 0, NY - 1);
int iz = hmath::clamp((int)(p[2] + NZ / 2.0f), 0, NZ - 1);
return index2flat(ix, iy, iz);
}
int index2flat(int x, int y, int z) const
{
return x + y * NX + z * NX * NY;
}
// Octreeも作る
struct Octree
{
hrt::BBox box;
float majorant = 0;
bool leaf = false;
Octree* child[2][2][2] = {};
};
hmath::Float3 index2pos_bbox(int x, int y, int z) const
{
return hmath::Float3(x - NX / 2.0f, y, z - NZ / 2.0f);
}
Octree* construct(int depth, int begin_x, int end_x, int begin_y, int end_y, int begin_z, int end_z)
{
Octree* current = new Octree; // プログラム終了時まで開放しないのでdeleteしない(行儀悪いね)
current->box = hrt::BBox(index2pos_bbox(begin_x, begin_y, begin_z), index2pos_bbox(end_x, end_y, end_z));
int edge_x = (end_x - begin_x) / 2;
int edge_y = (end_y - begin_y) / 2;
int edge_z = (end_z - begin_z) / 2;
if (edge_x == 0 && edge_y == 0 && edge_z == 0) {
// leaf
current->leaf = true;
current->majorant = data[index2flat(begin_x, begin_y, begin_z)];
return current;
}
float max_majorant = -1;
float min_majorant = std::numeric_limits<float>::infinity();
for (int iz = 0; iz < 2; ++iz) {
for (int iy = 0; iy < 2; ++iy) {
for (int ix = 0; ix < 2; ++ix) {
Octree* child = construct(
depth + 1,
begin_x + ix * edge_x, begin_x + ix * edge_x + edge_x,
begin_y + iy * edge_y, begin_y + iy * edge_y + edge_y,
begin_z + iz * edge_z, begin_z + iz * edge_z + edge_z);
if (child != nullptr) {
current->majorant = std::max(current->majorant, child->majorant);
current->child[ix][iy][iz] = child;
max_majorant = std::max(max_majorant, child->majorant);
min_majorant = std::min(min_majorant, child->majorant);
}
}
}
}
// もし、childのmajorantの差が一定の幅以下なら、まとめてしまう
if ((max_majorant - min_majorant) < 0.1f) {
//if (depth >= 4) {
current->leaf = true;
for (int iz = 0; iz < 2; ++iz) {
for (int iy = 0; iy < 2; ++iy) {
for (int ix = 0; ix < 2; ++ix) {
delete current->child[ix][iy][iz];
current->child[ix][iy][iz] = nullptr;
}
}
}
}
return current;
}
Octree* root = nullptr;
void build()
{
root = construct(0, 0, NX, 0, NY, 0, NZ);
// std::cout << "HOGE";
}
const Octree* octree_lut[NX * NY * NZ] = {};
VolumeTable()
{
hmath::Rng rng;
for (int iz = 0; iz < NZ; ++iz) {
for (int iy = 0; iy < NY; ++iy) {
for (int ix = 0; ix < NX; ++ix) {
auto p = index2pos(ix, iy, iz);
float scale = 1;
const float h = p[1];
constexpr float solid = 1.0f;
constexpr float void_space = 0.01f;
scale = void_space;
// 下
if (abs(p[0]) < 3.0f && abs(p[1]) < 2.0f && p[1] > 1.0f) {
scale = solid;
}
// 右
auto k = p - hmath::Float3(-8, 8, -8);
if (abs(k[2]) < 3.0f && abs(k[1]) < 5.0f)
scale = solid;
if (abs(k[2]) < 3.0f && abs(k[1]) < 1.0f) {
scale = void_space;
}
// 右縦
k = p - hmath::Float3(10, 0, -8);
if (abs(k[0]) < 2.0f && abs(k[2]) < 3.0f) {
scale = solid * 5.0f;
if (rng.next01() < 0.7f)
scale = void_space;
}
if (abs(k[1]) < 1.0f) {
scale = void_space;
}
// 上
k = p - hmath::Float3(0, 9, 0);
if (abs(k[0]) < 4.0f && abs(k[1]) < 3.0f && k[1] > 1.0f) {
scale = solid;
}
// 縦
k = p - hmath::Float3(-1, 0, 8);
if (abs(k[0]) < 2.0f && abs(k[2]) < 2.0f) {
scale = solid;
}
const auto extinction_coeff = scale * MieScale * scattering_Mie_base * exp(-h / H_Mie);
data[index2flat(ix, iy, iz)] = extinction_coeff;
majorant_ = std::max(majorant_, extinction_coeff);
}
}
}
build();
for (int iz = 0; iz < NZ; ++iz) {
for (int iy = 0; iy < NY; ++iy) {
for (int ix = 0; ix < NX; ++ix) {
auto p = index2pos(ix, iy, iz);
auto ptr = inside(p);
octree_lut[index2flat(ix, iy, iz)] = ptr;
}
}
}
}
float majorant() const { return majorant_; }
float sample(const hmath::Float3& p) const
{
if (!root->box.inside(p))
return 0;
return data[pos2index(p)];
}
const Octree* inside_sub(const Octree* current, const hmath::Float3& p) const
{
if (!current->box.inside(p)) {
return nullptr;
}
if (current->leaf) {
return current;
}
//return current;
for (int iz = 0; iz < 2; ++iz) {
for (int iy = 0; iy < 2; ++iy) {
for (int ix = 0; ix < 2; ++ix) {
auto* ptr = current->child[ix][iy][iz];
if (ptr) {
auto* ret = inside_sub(ptr, p);
if (ret)
return ret;
}
}
}
}
return current;
}
const Octree* inside(const hmath::Float3& p) const
{
return inside_sub(root, p);
}
const Octree* fast_inside(const hmath::Float3& p) const
{
if (!root->box.inside(p))
return nullptr;
return octree_lut[pos2index(p)];
}
} volumeTable;
#if 0
const real bound = coeff.bound();
auto inv_majorant = 1.0f / coeff.majorant();
real t = 0;
do {
t -= /*log(1.0f - rng.next01())*/ fmath::log(1.0f - rng.next01()) * inv_majorant;
} while (coeff(t) * inv_majorant < rng.next01() && t < bound);
return t;
#endif
float adaptive_delta_tracking(const hmath::Float3& org, const hmath::Float3& dir, float bound, hmath::Rng& rng)
{
auto current_org = org;
auto current_dir = dir;
#if 0
// delta tracking
auto inv_majorant = 1 / volumeTable.majorant();
float t = 0;
do {
t -= fmath::log(1.0f - rng.next01()) * inv_majorant;
if (t > bound)
return t;
if (volumeTable.sample(current_org + t * current_dir) * inv_majorant >= rng.next01()) {
return t;
}
} while (1);
#endif
#if 1
// 初回チェック
auto* node = volumeTable.fast_inside(current_org);
if (node == nullptr) {
float t0, t1;
if (!volumeTable.root->box.check_intersect(current_org, current_dir, &t0, &t1)) {
return std::numeric_limits<float>::infinity();
}
// volume内部に侵入
float progress = t0 + 1e-3f;
current_org = current_org + progress * current_dir;
return progress + adaptive_delta_tracking(current_org, current_dir, bound - progress, rng);
}
float total_t = 0;
for (;;) {
auto* node = volumeTable.fast_inside(current_org);
if (!node)
return std::numeric_limits<float>::infinity();
float t0, t1;
node->box.check_intersect(current_org, current_dir, &t0, &t1);
if (t0 <= 0.0f)
t0 = t1;
// delta tracking
if (node->majorant == 0) {
total_t += t0;
current_org = current_org + (t0 + 1e-3f) * current_dir;
}
else {
auto inv_majorant = 1.0f / node->majorant;
float t = 0;
do {
t -= fmath::log(1.0f - rng.next01()) * inv_majorant;
if (t > t0) {
total_t += t0;
current_org = current_org + (t0 + 1e-3f) * current_dir;
break;
}
if (total_t + t > bound) {
return total_t + t;
}
if (volumeTable.sample(current_org + t * current_dir) * inv_majorant >= rng.next01()) {
return total_t + t;
}
} while (1);
}
}
#endif
}
template<typename Vec3>
struct TransmittanceCoeffEvaluator
{
private:
float majorant_;
static constexpr float a = 0.3f;
const float cosa = cos(a);
const float sina = sin(a);
template<bool majorant = false>
float extinction_Mie(Vec3 p) const
{
float ext_coeff = volumeTable.sample(p);
if (majorant)
ext_coeff = volumeTable.majorant();
return ext_coeff;
}
/*
static constexpr float absorp = 0.0001f;
template<bool majorant = false>
float absorption_Mie(Vec3 p) const
{
return scattering_Mie<majorant>(p) * absorp;
}
*/
public:
Vec3 org;
Vec3 dir;
float bnd;
TransmittanceCoeffEvaluator(const Vec3& o, const Vec3& d, float b) :
org(o), dir(d), bnd(b)
{
//majorant_ = (scattering_Mie<true>({}) + absorption_Mie<true>({}));
majorant_ = (extinction_Mie<true>({}));
}
float majorant() const
{
return majorant_;
}
float bound() const
{
return bnd;
}
/*
float get_scattering_Mie(float t) const
{
const auto pos = org + t * dir;
return scattering_Mie(pos);
}
float get_absorption_Mie(float t) const
{
const auto pos = org + t * dir;
return absorption_Mie(pos);
}
*/
float operator()(float t) const
{
const auto pos = org + t * dir;
auto sc = extinction_Mie(pos);
return sc /* + sc * absorp */;
}
};
}
namespace integrator
{
using namespace hmath;
struct Ray
{
Float3 org;
Float3 dir;
};
// from smallpt
struct Sphere
{
double rad;
Float3 p;
Sphere(double rad_, Float3 p_) :
rad(rad_), p(p_) {}
double intersect(const Ray &r) const
{
Float3 op = p - r.org;
double t, eps = 1e-4, b = dot(op, r.dir), det = b * b - dot(op, op) + rad * rad;
if (det < 0) return 0; else det = sqrt(det);
return (t = b - det) > eps ? t : ((t = b + det) > eps ? t : 0);
}
};
Sphere spheres[] = {
Sphere(4.0f, Float3(0.0f, 6.0f, 0.0f)),
};
bool intersect_plane(const Float3&n, const Float3& o, const Float3& org, const Float3& dir, float &t)
{
auto d = dot(n, dir);
if (d < -1e-6f) {
t = dot(o - org, n) / d;
return (t >= 0);
}
return false;
}
struct IntersectionInfo
{
bool hit = false;
Float3 normal;
int id = -1;
float t = std::numeric_limits<float>::infinity();
};
IntersectionInfo intersect(const Ray &r)
{
//id = 0;
/*
double n = sizeof(spheres) / sizeof(Sphere), d, inf = t = 1e20;
for (int i = int(n);i--;) if ((d = spheres[i].intersect(r)) && d < t) { t = d;id = i; }
return t < inf;
*/
//return intersectPlane(Float3(0, 1, 0), Float3(0, 0, 0), r.org, r.dir, t);
IntersectionInfo info;
float p_t = std::numeric_limits<float>::infinity();
if (intersect_plane(Float3(0, 1, 0), Float3(0, 0, 0), r.org, r.dir, p_t)) {
if (p_t < info.t) {
info.normal = Float3(0, 1, 0);
info.t = p_t;
info.id = 0;
}
}
float s_t = (float)spheres[0].intersect(r);
if (s_t != 0) {
if (s_t < info.t) {
info.normal = normalize((r.org + s_t * r.dir) - spheres[0].p);
info.t = s_t;
info.id = 1;
}
}
info.hit = info.id != -1;
return info;
}
constexpr float DefaultMinWavelength = 380.0f;
constexpr float DefaultMaxWavelength = 750.0f;
constexpr int PlaneSection = 16;
constexpr float DefaultG = 0.25f;
struct PlaneTable
{
float table[PlaneSection];
PlaneTable()
{
Rng rng;
for (int i = 0; i < PlaneSection; ++i)
table[i] = rng.next01() * 4.0f + 8.0f;
}
} g_planetable;
Float3 hsv2rgb(const Float3& hsv)
{
auto h = hsv[0];
auto s = hsv[1];
auto v = hsv[2];
float r = v;
float g = v;
float b = v;
if (s > 0.0f) {
h *= 6.0f;
int i = (int)h;
float f = h - (float)i;
switch (i) {
default:
case 0:
g *= 1 - s * (1 - f);
b *= 1 - s;
break;
case 1:
r *= 1 - s * f;
b *= 1 - s;
break;
case 2:
r *= 1 - s;
b *= 1 - s * (1 - f);
break;
case 3:
r *= 1 - s;
g *= 1 - s * f;
break;
case 4:
r *= 1 - s * (1 - f);
g *= 1 - s;
break;
case 5:
g *= 1 - s;
b *= 1 - s * f;
break;
}
}
return Float3{ r,g,b };
}
Float3 get_plane_emission(float u, float v, float time)
{
float length = sqrt(u * u + v * v);
auto angle = time * 0.005f + (atan2(v, u) + pi<float>()) / (2 * pi<float>());
const int section = (int)(angle * PlaneSection) % PlaneSection;
const int length_section = (int)(length / 0.1f);
if (length < 5.2f) {
if (4.6f < length && length < 4.9f)
return 2.0f * Float3(1, 1, 1);
return {};
}
Rng rng;
rng.set_seed(((uint64_t)section << 32) + length_section);
if (rng.next() % 32 == 0) {
return 2.0f * Float3(1, 1, 1);
}
if (g_planetable.table[section] < length)
return {};
// 色相
//auto rgb = hsv2rgb(Float3((section + 8) % PlaneSection / (float)PlaneSection, 1, 1));
//return rgb;
return 2.0f * Float3(1, 1, 1);
}
Float3 cosine_weighted(Rng &random, const Float3& normal, const Float3& tangent, const Float3& binormal) {
const float phi = random.next01() * 2.0f * pi<float>();
const float r2 = random.next01(), r2s = sqrt(r2);
const float tx = r2s * cos(phi);
const float ty = r2s * sin(phi);
const float tz = sqrt(1.0f - r2);
return tz * normal + tx * tangent + ty * binormal;
}
// ここに素晴らしいintegratorを書く
Float3 get_radiance(int w, int h, int x, int y, uint64_t seed)
{
Rng rng;
rng.set_seed(seed);
const float current_time = rng.next01();
// data
constexpr float max_wavelength = DefaultMaxWavelength; /* [nm] */