-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_scene.cpp
More file actions
877 lines (835 loc) · 34.1 KB
/
parse_scene.cpp
File metadata and controls
877 lines (835 loc) · 34.1 KB
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
#include "parse_scene.h"
#include "3rdparty/pugixml.hpp"
#include "compute_normals.h"
#include "flexception.h"
#include "parse_obj.h"
#include "parse_ply.h"
#include "parse_serialized.h"
#include "transform.h"
#include <map>
#include <regex>
#include <vector>
const float c_default_fov = 45.0;
const int c_default_res = 256;
const std::string c_default_filename = "image.exr";
enum class FovAxis {
X,
Y,
DIAGONAL,
SMALLER,
LARGER
};
struct ParsedLookAtXform {
float3 lookfrom = float3{0, 0, 0};
float3 lookat = float3{0, 0, -1};
float3 up = float3{0, 1, 0};
};
inline float3 sRGB_to_RGB(const float3 &srgb) {
// https://en.wikipedia.org/wiki/SRGB#From_sRGB_to_CIE_XYZ
float3 rgb = srgb;
rgb.x = rgb.x <= float(0.04045) ? rgb.x / float(12.92) : pow((rgb.x + float(0.055)) / float(1.055), float(2.4));
rgb.y = rgb.y <= float(0.04045) ? rgb.y / float(12.92) : pow((rgb.y + float(0.055)) / float(1.055), float(2.4));
rgb.z = rgb.z <= float(0.04045) ? rgb.z / float(12.92) : pow((rgb.z + float(0.055)) / float(1.055), float(2.4));
return rgb;
}
std::vector<std::string> split_string(const std::string &str, const std::regex &delim_regex) {
std::sregex_token_iterator first{begin(str), end(str), delim_regex, -1}, last;
std::vector<std::string> list{first, last};
return list;
}
float3 parse_vector3(const std::string &value) {
std::vector<std::string> list = split_string(value, std::regex("(,| )+"));
float3 v;
if (list.size() == 1) {
v.x = std::stof(list[0]);
v.y = std::stof(list[0]);
v.z = std::stof(list[0]);
} else if (list.size() == 3) {
v.x = std::stof(list[0]);
v.y = std::stof(list[1]);
v.z = std::stof(list[2]);
} else {
Error("parse_vector3 failed");
}
return v;
}
auto parse_default_map(const std::string &value,
const std::map<std::string, std::string> &default_map) {
if (value.length() > 0) {
if (value[0] == '$') {
auto it = default_map.find(value.substr(1));
if (it == default_map.end()) {
Error(std::string("Reference default variable ") + value + std::string(" not found."));
}
return it;
}
}
return default_map.end();
}
void parse_default_map(pugi::xml_node node,
std::map<std::string, std::string> &default_map) {
if (node.attribute("name")) {
std::string name = node.attribute("name").value();
if (node.attribute("value")) {
std::string value = node.attribute("value").value();
default_map[name] = value;
}
}
}
int parse_integer(const std::string &value,
const std::map<std::string, std::string> &default_map) {
if (auto it = parse_default_map(value, default_map); it != default_map.end()) {
return std::stoi(it->second);
}
return std::stoi(value);
}
float parse_float(const std::string &value,
const std::map<std::string, std::string> &default_map) {
if (auto it = parse_default_map(value, default_map); it != default_map.end()) {
return std::stof(it->second);
}
return std::stof(value);
}
std::string parse_string(const std::string &value,
const std::map<std::string, std::string> &default_map) {
if (auto it = parse_default_map(value, default_map); it != default_map.end()) {
return it->second;
}
return value;
}
bool parse_boolean(const std::string &value) {
if (value == "true") {
return true;
} else if (value == "false") {
return false;
} else {
Error("parse_boolean failed");
return false;
}
}
bool parse_boolean(const std::string &value,
const std::map<std::string, std::string> &default_map) {
if (auto it = parse_default_map(value, default_map); it != default_map.end()) {
return parse_boolean(it->second);
}
return parse_boolean(value);
}
float3 parse_vector3(const std::string &value,
const std::map<std::string, std::string> &default_map) {
if (auto it = parse_default_map(value, default_map); it != default_map.end()) {
return parse_vector3(it->second);
}
return parse_vector3(value);
}
float3 parse_srgb(const std::string &value) {
float3 srgb;
if (value.size() == 7 && value[0] == '#') {
char *end_ptr = NULL;
// parse hex code (#abcdef)
int encoded = strtol(value.c_str()+1, &end_ptr, 16);
if (*end_ptr != '\0') {
Error(std::string("Invalid SRGB value: ") + value);
}
srgb.x = ((encoded & 0xFF0000) >> 16) / 255.0f;
srgb.y = ((encoded & 0x00FF00) >> 8) / 255.0f;
srgb.z = (encoded & 0x0000FF) / 255.0f;
} else {
Error(std::string("Unknown SRGB format: ") + value);
}
return srgb;
}
float3 parse_srgb(const std::string &value,
const std::map<std::string, std::string> &default_map) {
if (auto it = parse_default_map(value, default_map); it != default_map.end()) {
return parse_srgb(it->second);
}
return parse_srgb(value);
}
Matrix4x4 parse_matrix4x4(const std::string &value) {
std::vector<std::string> list = split_string(value, std::regex("(,| )+"));
if (list.size() != 16) {
Error("parse_matrix4x4 failed");
}
Matrix4x4 m;
int k = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
m(i, j) = std::stof(list[k++]);
}
}
return m;
}
Matrix4x4 parse_matrix4x4(const std::string &value,
const std::map<std::string, std::string> &default_map) {
if (auto it = parse_default_map(value, default_map); it != default_map.end()) {
return parse_matrix4x4(it->second);
}
return parse_matrix4x4(value);
}
Matrix4x4 parse_transform(pugi::xml_node node,
const std::map<std::string, std::string> &default_map) {
Matrix4x4 tform = Matrix4x4::identity();
for (auto child : node.children()) {
std::string name = to_lowercase(child.name());
if (name == "scale") {
float x = 1.0;
float y = 1.0;
float z = 1.0;
if (!child.attribute("x").empty()) {
x = parse_float(child.attribute("x").value(), default_map);
}
if (!child.attribute("y").empty()) {
y = parse_float(child.attribute("y").value(), default_map);
}
if (!child.attribute("z").empty()) {
z = parse_float(child.attribute("z").value(), default_map);
}
if (!child.attribute("value").empty()) {
float3 v = parse_vector3(
child.attribute("value").value(), default_map);
x = v.x; y = v.y; z = v.z;
}
tform = scale(float3{x, y, z}) * tform;
} else if (name == "translate") {
float x = 0.0;
float y = 0.0;
float z = 0.0;
if (!child.attribute("x").empty()) {
x = parse_float(child.attribute("x").value(), default_map);
}
if (!child.attribute("y").empty()) {
y = parse_float(child.attribute("y").value(), default_map);
}
if (!child.attribute("z").empty()) {
z = parse_float(child.attribute("z").value(), default_map);
}
if (!child.attribute("value").empty()) {
float3 v = parse_vector3(
child.attribute("value").value(), default_map);
x = v.x; y = v.y; z = v.z;
}
tform = translate(float3{x, y, z}) * tform;
} else if (name == "rotate") {
float x = 0.0;
float y = 0.0;
float z = 0.0;
float angle = 0.0;
if (!child.attribute("x").empty()) {
x = parse_float(child.attribute("x").value(), default_map);
}
if (!child.attribute("y").empty()) {
y = parse_float(child.attribute("y").value(), default_map);
}
if (!child.attribute("z").empty()) {
z = parse_float(child.attribute("z").value(), default_map);
}
if (!child.attribute("angle").empty()) {
angle = parse_float(child.attribute("angle").value(), default_map);
}
tform = rotate(angle, make_float3(x, y, z)) * tform;
} else if (name == "lookat") {
float3 pos = parse_vector3(
child.attribute("origin").value(), default_map);
float3 target = parse_vector3(
child.attribute("target").value(), default_map);
float3 up = parse_vector3(
child.attribute("up").value(), default_map);
tform = look_at(pos, target, up) * tform;
} else if (name == "matrix") {
Matrix4x4 trans = parse_matrix4x4(
std::string(child.attribute("value").value()), default_map);
tform = trans * tform;
}
}
return tform;
}
std::tuple<int /* width */, int /* height */, std::string /* filename */>
parse_film(pugi::xml_node node, const std::map<std::string, std::string> &default_map) {
int width = c_default_res, height = c_default_res;
std::string filename = c_default_filename;
for (auto child : node.children()) {
std::string type = child.name();
std::string name = child.attribute("name").value();
if (name == "width") {
width = parse_integer(child.attribute("value").value(), default_map);
} else if (name == "height") {
height = parse_integer(child.attribute("value").value(), default_map);
} else if (name == "filename") {
filename = parse_string(child.attribute("value").value(), default_map);
}
}
return std::make_tuple(width, height, filename);
}
ParsedLookAtXform parse_lookat(pugi::xml_node node,
const std::map<std::string, std::string> &default_map) {
ParsedLookAtXform lookat_xform;
for (auto child : node.children()) {
std::string name = to_lowercase(child.name());
if (name == "lookat") {
lookat_xform.lookfrom = parse_vector3(
child.attribute("origin").value(), default_map);
lookat_xform.lookat = parse_vector3(
child.attribute("target").value(), default_map);
lookat_xform.up = parse_vector3(
child.attribute("up").value(), default_map);
} else {
Error(std::string("Only support LookAt transform in a sensor."));
}
}
return lookat_xform;
}
std::tuple<ParsedCamera, std::string /* output filename */, int /* sample_count */>
parse_sensor(pugi::xml_node node,
const std::map<std::string, std::string> &default_map) {
ParsedLookAtXform lookat_xform;
float fov = c_default_fov;
int width = c_default_res, height = c_default_res;
std::string filename = c_default_filename;
FovAxis fov_axis = FovAxis::X;
int sample_count = 16;
std::string type = node.attribute("type").value();
if (type == "perspective") {
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "fov") {
fov = parse_float(child.attribute("value").value(), default_map);
} else if (name == "toWorld" || name == "to_world") {
lookat_xform = parse_lookat(child, default_map);
} else if (name == "fovAxis" || name == "fov_axis") {
std::string value = child.attribute("value").value();
if (value == "x") {
fov_axis = FovAxis::X;
} else if (value == "y") {
fov_axis = FovAxis::Y;
} else if (value == "diagonal") {
fov_axis = FovAxis::DIAGONAL;
} else if (value == "smaller") {
fov_axis = FovAxis::SMALLER;
} else if (value == "larger") {
fov_axis = FovAxis::LARGER;
} else {
Error(std::string("Unknown fovAxis value: ") + value);
}
}
}
} else {
Error(std::string("Unsupported sensor: ") + type);
}
for (auto child : node.children()) {
if (std::string(child.name()) == "film") {
std::tie(width, height, filename) = parse_film(child, default_map);
} else if (std::string(child.name()) == "sampler") {
std::string name = child.attribute("type").value();
if (name != "independent") {
std::cerr << "Warning: the renderer currently only supports independent samplers." << std::endl;
}
for (auto grand_child : child.children()) {
std::string name = grand_child.attribute("name").value();
if (name == "sampleCount" || name == "sample_count") {
sample_count = parse_integer(
grand_child.attribute("value").value(), default_map);
}
}
}
}
// convert to vertical FOV
if (fov_axis == FovAxis::X ||
(fov_axis == FovAxis::SMALLER && width < height) ||
(fov_axis == FovAxis::LARGER && height < width)) {
fov = degrees(2 * atan(
tan(radians(fov) / 2) * height / float(width)));
} else if (fov_axis == FovAxis::DIAGONAL) {
float aspect = float(height) / width;
float diagonal = 2 * tan(radians(fov) / 2);
float height = diagonal / sqrt(1 + 1 / (aspect * aspect));
fov = degrees(2 * atan(height / 2));
}
return std::make_tuple(ParsedCamera{lookat_xform.lookfrom,
lookat_xform.lookat,
lookat_xform.up,
fov,
width, height},
filename,
sample_count);
}
ParsedColor parse_texture(pugi::xml_node node,
const std::map<std::string, std::string> &default_map) {
std::string type = node.attribute("type").value();
if (type == "bitmap") {
std::string filename = "";
float uscale = 1;
float vscale = 1;
float uoffset = 0;
float voffset = 0;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "filename") {
filename = parse_string(
child.attribute("value").value(), default_map);
} else if (name == "uvscale") {
uscale = vscale = parse_float(
child.attribute("value").value(), default_map);
} else if (name == "uscale") {
uscale = parse_float(
child.attribute("value").value(), default_map);
} else if (name == "vscale") {
vscale = parse_float(
child.attribute("value").value(), default_map);
} else if (name == "uoffset") {
uoffset = parse_float(
child.attribute("value").value(), default_map);
} else if (name == "voffset") {
voffset = parse_float(
child.attribute("value").value(), default_map);
}
}
fs::path path(filename);
if (path.is_relative()) {
path = fs::current_path() / path;
}
return ParsedImageTexture{path,
uscale, vscale, uoffset, voffset};
}
Error(std::string("Unknown texture type: ") + type);
return ParsedColor{};
}
ParsedColor parse_color(
pugi::xml_node node,
std::map<std::string /* name id */, ParsedColor> &texture_map,
const std::map<std::string, std::string> &default_map) {
std::string type = node.name();
if (type == "rgb") {
return ParsedColor(parse_vector3(node.attribute("value").value(), default_map));
} else if (type == "srgb") {
float3 srgb = parse_srgb(node.attribute("value").value(), default_map);
return ParsedColor(sRGB_to_RGB(srgb));
} else if (type == "ref") {
// referencing a texture
std::string ref_id = node.attribute("id").value();
auto t_it = texture_map.find(ref_id);
if (t_it == texture_map.end()) {
Error(std::string("Texture not found. ID = ") + ref_id);
}
return t_it->second;
} else if (type == "texture") {
return parse_texture(node, default_map);
} else {
Error(std::string("Unknown spectrum texture type:") + type);
return ParsedColor(float3{0, 0, 0});
}
}
float3 parse_intensity(pugi::xml_node node,
const std::map<std::string, std::string> &default_map) {
std::string type = node.name();
if (type == "rgb") {
return parse_vector3(
node.attribute("value").value(), default_map);
} else if (type == "srgb") {
float3 srgb = parse_srgb(
node.attribute("value").value(), default_map);
return sRGB_to_RGB(srgb);
}
return float3{1, 1, 1};
}
std::tuple<std::string /* ID */, ParsedMaterial> parse_bsdf(
pugi::xml_node node,
std::map<std::string /* name id */, ParsedColor> &texture_map,
const std::map<std::string, std::string> &default_map,
const std::string &parent_id = "") {
std::string type = node.attribute("type").value();
std::string id = parent_id;
if (!node.attribute("id").empty()) {
id = node.attribute("id").value();
}
if (type == "twosided") {
// In torrey, all BSDFs are twosided.
for (auto child : node.children()) {
if (std::string(child.name()) == "bsdf") {
return parse_bsdf(child, texture_map, default_map, id);
}
}
} else if (type == "diffuse") {
ParsedColor reflectance(float3{0.5, 0.5, 0.5});
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "reflectance") {
reflectance = parse_color(
child, texture_map, default_map);
}
}
return std::make_tuple(id, ParsedDiffuse{reflectance});
} else if (type == "mirror") {
ParsedColor reflectance(float3{1, 1, 1});
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "reflectance") {
reflectance = parse_color(
child, texture_map, default_map);
}
}
return std::make_tuple(id, ParsedMirror{reflectance});
} else if (type == "plastic") {
ParsedColor reflectance(float3{0.5, 0.5, 0.5});
float eta = 1.5;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "reflectance") {
reflectance = parse_color(
child, texture_map, default_map);
} else if (name == "ior" || name == "eta") {
eta = parse_float(child.attribute("value").value(), default_map);
}
}
return std::make_tuple(id, ParsedPlastic{eta, reflectance});
} else if (type == "phong") {
ParsedColor reflectance(float3{0.5, 0.5, 0.5});
float exponent = 5;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "reflectance") {
reflectance = parse_color(
child, texture_map, default_map);
} else if (name == "exponent" || name == "alpha") {
exponent = parse_float(child.attribute("value").value(), default_map);
}
}
return std::make_tuple(id, ParsedPhong{reflectance, exponent});
} else if (type == "blinn" || type == "blinnphong") {
ParsedColor reflectance(float3{0.5, 0.5, 0.5});
float exponent = 5;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "reflectance") {
reflectance = parse_color(
child, texture_map, default_map);
} else if (name == "exponent" || name == "alpha") {
exponent = parse_float(child.attribute("value").value(), default_map);
}
}
return std::make_tuple(id, ParsedBlinnPhong{reflectance, exponent});
} else if (type == "blinn_microfacet" || type == "blinnphong_microfacet") {
ParsedColor reflectance(float3{0.5, 0.5, 0.5});
float exponent = 5;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "reflectance") {
reflectance = parse_color(
child, texture_map, default_map);
} else if (name == "exponent" || name == "alpha") {
exponent = parse_float(child.attribute("value").value(), default_map);
}
}
return std::make_tuple(id, ParsedBlinnPhongMicrofacet{reflectance, exponent});
} else {
Error(std::string("Unknown BSDF: ") + type);
}
return std::make_tuple("", ParsedMaterial{});
}
ParsedLight parse_emitter(pugi::xml_node node,
const std::map<std::string, std::string> &default_map) {
std::string type = node.attribute("type").value();
if (type == "point") {
float3 position = float3{0, 0, 0};
float3 intensity = float3{1, 1, 1};
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "position") {
if (!child.attribute("x").empty()) {
position.x = parse_float(child.attribute("x").value(), default_map);
}
if (!child.attribute("y").empty()) {
position.y = parse_float(child.attribute("y").value(), default_map);
}
if (!child.attribute("z").empty()) {
position.z = parse_float(child.attribute("z").value(), default_map);
}
} else if (name == "intensity") {
intensity = parse_intensity(child, default_map);
}
}
return ParsedPointLight{position, intensity};
} else {
Error(std::string("Unknown emitter: ") + type);
}
}
ParsedShape parse_shape(pugi::xml_node node,
std::vector<ParsedMaterial> &materials,
std::map<std::string /* name id */, int /* index id */> &material_map,
std::map<std::string /* name id */, ParsedColor> &texture_map,
std::vector<ParsedLight> &lights,
const std::vector<ParsedShape> &shapes,
const std::map<std::string, std::string> &default_map) {
// First, parse the material inside the shape and get the material ID.
int material_id = -1;
for (auto child : node.children()) {
std::string name = child.name();
if (name == "ref") {
// A reference to a BSDF
std::string name_value = child.attribute("name").value();
pugi::xml_attribute id = child.attribute("id");
if (id.empty()) {
Error("Material reference id not specified.");
}
auto it = material_map.find(id.value());
if (it == material_map.end()) {
Error(std::string("Material reference ") + id.value() + std::string(" not found."));
}
material_id = it->second;
} else if (name == "bsdf") {
ParsedMaterial m;
std::string material_name;
std::tie(material_name, m) = parse_bsdf(
child, texture_map, default_map);
if (!material_name.empty()) {
material_map[material_name] = materials.size();
}
material_id = materials.size();
materials.push_back(m);
}
}
ParsedShape shape;
std::string type = node.attribute("type").value();
if (type == "obj") {
std::string filename;
Matrix4x4 to_world = Matrix4x4::identity();
bool face_normals = false;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "filename") {
filename = parse_string(child.attribute("value").value(), default_map);
} else if (name == "toWorld" || name == "to_world") {
if (std::string(child.name()) == "transform") {
to_world = parse_transform(child, default_map);
}
} else if (name == "faceNormals" || name == "face_normals") {
face_normals = parse_boolean(
child.attribute("value").value(), default_map);
}
}
shape = parse_obj(filename, to_world);
ParsedTriangleMesh &mesh = std::get<ParsedTriangleMesh>(shape);
if (face_normals) {
mesh.normals = std::vector<float3>{};
} else {
if (mesh.normals.size() == 0) {
mesh.normals = compute_normals(mesh.positions, mesh.indices);
}
}
} else if (type == "ply") {
std::string filename;
int shape_index = 0;
Matrix4x4 to_world = Matrix4x4::identity();
bool face_normals = false;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "filename") {
filename = parse_string(child.attribute("value").value(), default_map);
} else if (name == "toWorld" || name == "to_world") {
if (std::string(child.name()) == "transform") {
to_world = parse_transform(child, default_map);
}
} else if (name == "shapeIndex" || name == "shape_index") {
shape_index = parse_integer(child.attribute("value").value(), default_map);
} else if (name == "faceNormals" || name == "face_normals") {
face_normals = parse_boolean(
child.attribute("value").value(), default_map);
}
}
shape = parse_ply(filename, to_world);
ParsedTriangleMesh &mesh = std::get<ParsedTriangleMesh>(shape);
if (face_normals) {
mesh.normals = std::vector<float3>{};
} else {
if (mesh.normals.size() == 0) {
mesh.normals = compute_normals(mesh.positions, mesh.indices);
}
}
} else if (type == "serialized") {
std::string filename;
int shape_index = 0;
Matrix4x4 to_world = Matrix4x4::identity();
bool face_normals = false;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "filename") {
filename = parse_string(child.attribute("value").value(), default_map);
} else if (name == "toWorld" || name == "to_world") {
if (std::string(child.name()) == "transform") {
to_world = parse_transform(child, default_map);
}
} else if (name == "shapeIndex" || name == "shape_index") {
shape_index = parse_integer(child.attribute("value").value(), default_map);
} else if (name == "faceNormals" || name == "face_normals") {
face_normals = parse_boolean(
child.attribute("value").value(), default_map);
}
}
shape = parse_serialized(filename, shape_index, to_world);
ParsedTriangleMesh &mesh = std::get<ParsedTriangleMesh>(shape);
if (face_normals) {
mesh.normals = std::vector<float3>{};
} else {
if (mesh.normals.size() == 0) {
mesh.normals = compute_normals(mesh.positions, mesh.indices);
}
}
} else if (type == "sphere") {
float3 center{0, 0, 0};
float radius = 1;
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "center") {
center = float3{
parse_float(child.attribute("x").value(), default_map),
parse_float(child.attribute("y").value(), default_map),
parse_float(child.attribute("z").value(), default_map)};
} else if (name == "radius") {
radius = parse_float(child.attribute("value").value(), default_map);
}
}
shape = ParsedSphere{{}, center, radius};
} else if (type == "rectangle") {
// Create a triangle mesh
Matrix4x4 to_world = Matrix4x4::identity();
bool flip_normals = false;
ParsedTriangleMesh mesh;
mesh.positions = {
float3{-1, -1, 0}, float3{ 1, -1, 0}, float3{ 1, 1, 0}, float3{-1, 1, 0}
};
mesh.indices = {
int3{0, 1, 2}, int3{0, 2, 3}
};
mesh.uvs = {
float2{0, 0}, float2{1, 0}, float2{1, 1}, float2{0, 1}
};
mesh.normals = {
float3{0, 0, 1}, float3{0, 0, 1}, float3{0, 0, 1}, float3{0, 0, 1}
};
for (auto child : node.children()) {
std::string name = child.attribute("name").value();
if (name == "toWorld" || name == "to_world") {
if (std::string(child.name()) == "transform") {
to_world = parse_transform(child, default_map);
}
} else if (name == "flipNormals" || name == "flip_normals") {
flip_normals = parse_boolean(child.attribute("value").value(), default_map);
}
}
if (flip_normals) {
for (auto &n : mesh.normals) {
n = -n;
}
}
for (auto &p : mesh.positions) {
p = xform_point(to_world, p);
}
for (auto &n : mesh.normals) {
n = xform_normal(inverse(to_world), n);
}
shape = mesh;
} else {
Error(std::string("Unknown shape:") + type);
}
set_material_id(shape, material_id);
// Parse area light emitter
for (auto child : node.children()) {
std::string name = child.name();
if (name == "emitter") {
float3 radiance{1, 1, 1};
for (auto grand_child : child.children()) {
std::string name = grand_child.attribute("name").value();
if (name == "radiance") {
radiance = parse_intensity(grand_child, default_map);
}
}
set_area_light_id(shape, lights.size());
lights.push_back(
ParsedDiffuseAreaLight{(int)shapes.size() /* shape ID */, radiance});
}
}
return shape;
}
ParsedScene parse_scene(pugi::xml_node node) {
ParsedCamera camera{
float3{0, 0, 0},
float3{0, 0, -1},
float3{0, 1, 0},
c_default_fov, // FOV
c_default_res, c_default_res // width/height
};
std::vector<ParsedMaterial> materials;
std::vector<ParsedLight> lights;
std::vector<ParsedShape> shapes;
std::string filename = "image.exr";
// For <default> tags
// e.g., <default name="spp" value="4096"/> will map "spp" to "4096"
std::map<std::string, std::string> default_map;
std::map<std::string /* name id */, ParsedColor> texture_map;
std::map<std::string /* name id */, int /* index id */> material_map;
float3 background_color = float3{0.5, 0.5, 0.5};
int sample_count = 16;
for (auto child : node.children()) {
std::string name = child.name();
if (name == "default") {
parse_default_map(child, default_map);
} else if (name == "sensor") {
std::tie(camera, filename, sample_count) =
parse_sensor(child, default_map);
} else if (name == "bsdf") {
std::string material_name;
ParsedMaterial m;
std::tie(material_name, m) = parse_bsdf(
child, texture_map, default_map);
if (!material_name.empty()) {
material_map[material_name] = materials.size();
materials.push_back(m);
}
} else if (name == "emitter") {
lights.push_back(parse_emitter(child, default_map));
} else if (name == "shape") {
shapes.push_back(
parse_shape(child,
materials,
material_map,
texture_map,
lights,
shapes,
default_map));
} else if (name == "texture") {
std::string id = child.attribute("id").value();
if (texture_map.find(id) != texture_map.end()) {
Error(std::string("Duplicated texture ID:") + id);
}
texture_map[id] = parse_texture(child, default_map);
} else if (name == "background") {
for (auto grandchild : child.children()) {
std::string name = grandchild.attribute("name").value();
if (name == "radiance") {
background_color = parse_intensity(grandchild, default_map);
}
}
}
}
return ParsedScene{camera,
materials,
lights,
shapes,
background_color,
sample_count};
}
ParsedScene parse_scene(const fs::path &filename) {
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
if (!result) {
std::cerr << "Error description: " << result.description() << std::endl;
std::cerr << "Error offset: " << result.offset << std::endl;
Error("Parse error");
}
// back up the current working directory and switch to the parent folder of the file
fs::path old_path = fs::current_path();
fs::current_path(filename.parent_path());
ParsedScene scene = parse_scene(doc.child("scene"));
// switch back to the old current working directory
fs::current_path(old_path);
return scene;
}