-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.cpp
915 lines (738 loc) · 27.2 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
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <cfloat>
// Boost
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
// Eigen
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>
// HDF5
#include <H5Cpp.h>
// OpenMP
#include <omp.h>
// Point-triangle distance and ray-triangle intersection.
#include "triangle_point/poitri.h"
#include "triangle_ray/raytri.h"
#include "box_triangle/aabb_triangle_overlap.h"
/** \brief Compute triangle point distance and corresponding closest point.
* \param[in] point point
* \param[in] v1 first vertex
* \param[in] v2 second vertex
* \param[in] v3 third vertex
* \param[out] ray corresponding closest point
* \return distance
*/
float triangle_point_distance(const Eigen::Vector3f &point, const Eigen::Vector3f &v1, const Eigen::Vector3f &v2, const Eigen::Vector3f &v3,
Eigen::Vector3f &closest_point) {
Vec3f x0(point.data());
Vec3f x1(v1.data());
Vec3f x2(v2.data());
Vec3f x3(v3.data());
Vec3f r(0);
float distance = point_triangle_distance(x0, x1, x2, x3, r);
for (int d = 0; d < 3; d++) {
closest_point(d) = r[d];
}
return distance;
}
/** \brief Test triangle ray intersection.
* \param[in] origin origin of ray
* \param[in] dest destination of ray
* \param[in] v1 first vertex
* \param[in] v2 second vertex
* \param[in] v3 third vertex
* \return intersects
*/
bool triangle_ray_intersection(const Eigen::Vector3f &origin, const Eigen::Vector3f &dest,
const Eigen::Vector3f &v1, const Eigen::Vector3f &v2, const Eigen::Vector3f &v3, float &t) {
double _origin[3] = {origin(0), origin(1), origin(2)};
double _dir[3] = {dest(0) - origin(0), dest(1) - origin(1), dest(2) - origin(2)};
double _v1[3] = {v1(0), v1(1), v1(2)};
double _v2[3] = {v2(0), v2(1), v2(2)};
double _v3[3] = {v3(0), v3(1), v3(2)};
// t is the distance, u and v are barycentric coordinates
// http://fileadmin.cs.lth.se/cs/personal/tomas_akenine-moller/code/raytri_tam.pdf
double _t, u, v;
int success = intersect_triangle(_origin, _dir, _v1, _v2, _v3, &_t, &u, &v);
t = _t;
if (success) {
return true;
}
return false;
}
/** \brief Compute triangle box intersection.
* \param[in] min defining voxel
* \param[in] max defining voxel
* \param[in] v1 first vertex
* \param[in] v2 second vertex
* \param[in] v3 third vertex
* \return intersects
*/
bool triangle_box_intersection(const Eigen::Vector3f &min, Eigen::Vector3f &max, const Eigen::Vector3f &v1, const Eigen::Vector3f &v2, const Eigen::Vector3f &v3) {
float half_size[3] = {
(max(0) - min(0))/2.,
(max(1) - min(1))/2.,
(max(2) - min(2))/2.
};
float center[3] = {
max(0) - half_size[0],
max(1) - half_size[1],
max(2) - half_size[2]
};
float vertices[3][3] = {{v1(0), v1(1), v1(2)}, {v2(0), v2(1), v2(2)}, {v3(0), v3(1), v3(2)}};
return triBoxOverlap(center, half_size, vertices);
}
/** \brief Specifies the voxelization mode, i.e. which point of a voxel to use for SDF computation. */
enum VoxelizationMode {
CENTER = 0,
CORNER = 1
};
/** \brief Just encapsulating vertices and faces. */
class Mesh {
public:
/** \brief Empty constructor. */
Mesh() {
}
/** \brief Reading an off file and returning the vertices x, y, z coordinates and the
* face indices.
* \param[in] filepath path to the OFF file
* \param[out] mesh read mesh with vertices and faces
* \param[in] usecolor whether OFF file contains colors of faces
* \return success
*/
static bool from_off(const std::string filepath, Mesh& mesh, bool usecolor) {
std::ifstream* file = new std::ifstream(filepath.c_str());
std::string line;
std::stringstream ss;
int line_nb = 0;
std::getline(*file, line);
++line_nb;
if (line != "off" && line != "OFF") {
std::cout << "[Error] Invalid header: \"" << line << "\", " << filepath << std::endl;
return false;
}
size_t n_edges;
std::getline(*file, line);
++line_nb;
int n_vertices;
int n_faces;
ss << line;
ss >> n_vertices;
ss >> n_faces;
ss >> n_edges;
for (size_t v = 0; v < n_vertices; ++v) {
std::getline(*file, line);
++line_nb;
ss.clear();
ss.str("");
Eigen::Vector3f vertex;
ss << line;
ss >> vertex(0);
ss >> vertex(1);
ss >> vertex(2);
mesh.add_vertex(vertex);
}
size_t n;
for (size_t f = 0; f < n_faces; ++f) {
std::getline(*file, line);
++line_nb;
ss.clear();
ss.str("");
size_t n;
ss << line;
ss >> n;
if(n != 3) {
std::cout << "[Error] Not a triangle (" << n << " points) at " << (line_nb - 1) << std::endl;
return false;
}
Eigen::Vector3i face;
Eigen::Vector3i color;
color.setZero();
//std::cout << "Empty color vector: " << color(0) << " " << color(1) << " " << color(2) << std::endl;
ss >> face(0);
ss >> face(1);
ss >> face(2);
if (usecolor == true){
ss >> color(0);
ss >> color(1);
ss >> color(2);
};
//std::cout << "Color vector after reading line: " << color(0) << " " << color(1) << " " << color(2) << std::endl;
mesh.add_face(face);
mesh.add_color(color);
}
if (n_vertices != mesh.num_vertices()) {
std::cout << "[Error] Number of vertices in header differs from actual number of vertices." << std::endl;
return false;
}
if (n_faces != mesh.num_faces()) {
std::cout << "[Error] Number of faces in header differs from actual number of faces." << std::endl;
return false;
}
file->close();
delete file;
return true;
}
/** \brief Write mesh to OFF file.
* \param[in] filepath path to OFF file to write
* \return success
*/
bool to_off(const std::string filepath) {
std::ofstream* out = new std::ofstream(filepath, std::ofstream::out);
if (!static_cast<bool>(out)) {
return false;
}
(*out) << "OFF" << std::endl;
(*out) << this->num_vertices() << " " << this->num_faces() << " 0" << std::endl;
for (unsigned int v = 0; v < this->num_vertices(); v++) {
(*out) << this->vertices[v](0) << " " << this->vertices[v](1) << " " << this->vertices[v](2) << std::endl;
}
for (unsigned int f = 0; f < this->num_faces(); f++) {
(*out) << "3 " << this->faces[f](0) << " " << this->faces[f](1) << " " << this->faces[f](2) << std::endl;
/* if colors
(*out) << "3 " << this->faces[f](0) << " " << this->faces[f](1) << " " << this->faces[f](2) <<
" " << this->colors[f](0) << " " << this->colors[f](1) << " " << this->colors[f](2) << std::endl;
*/
}
out->close();
delete out;
return true;
}
/** \brief Add a vertex.
* \param[in] vertex vertex to add
*/
void add_vertex(Eigen::Vector3f& vertex) {
this->vertices.push_back(vertex);
}
/** \brief Get the number of vertices.
* \return number of vertices
*/
int num_vertices() {
return static_cast<int>(this->vertices.size());
}
/** \brief Add a face.
* \param[in] face face to add
*/
void add_face(Eigen::Vector3i& face) {
this->faces.push_back(face);
}
/** \brief Get the number of faces.
* \return number of faces
*/
int num_faces() {
return static_cast<int>(this->faces.size());
}
/** \brief Add a color.
* \param[in] color RGB color to add
*/
void add_color(Eigen::Vector3i& color) {
this->colors.push_back(color);
}
/** \brief Get the number of colors.
* \return number of colors
*/
int num_colors() {
return static_cast<int>(this->colors.size());
}
/** \brief Return colors as an array.
* \param[in] color_array array in which colors are stored
*/
void return_colors(Eigen::Tensor<int, 2, Eigen::RowMajor>& color_array) {
for (int f = 0; f < this->num_faces(); ++f) {
int r = colors[f](0);
int g = colors[f](1);
int b = colors[f](2);
for (int i = 0; i < 3; i++){
color_array(f,i) = this->colors[f](i);
}
}
}
/** \brief Translate the mesh.
* \param[in] translation translation vector
*/
void translate(const Eigen::Vector3f& translation) {
for (int v = 0; v < this->num_vertices(); ++v) {
for (int i = 0; i < 3; ++i) {
this->vertices[v](i) += translation(i);
}
}
}
/** \brief Scale the mesh.
* \param[in] scale scale vector
*/
void scale(const Eigen::Vector3f& scale) {
for (int v = 0; v < this->num_vertices(); ++v) {
for (int i = 0; i < 3; ++i) {
this->vertices[v](i) *= scale(i);
}
}
}
/** \brief Voxelize the given mesh into a SDF.
* \param[out] sdf volume to fill with sdf values
*/
void voxelize_sdf(Eigen::Tensor<float, 3, Eigen::RowMajor>& sdf, const VoxelizationMode &mode) {
int height = sdf.dimension(0);
int width = sdf.dimension(1);
int depth = sdf.dimension(2);
#pragma omp parallel
{
#pragma omp for
for (int i = 0; i < height*width*depth; i++) {
int d = i%depth;
int w = (i/depth)%width;
int h = (i/depth)/width;
sdf(h, w, d) = FLT_MAX;
// the box corresponding to this voxel
Eigen::Vector3f min(w, h, d);
Eigen::Vector3f max(w + 1, h + 1, d + 1);
Eigen::Vector3f center(w + 0.5f, h + 0.5f, d + 0.5f);
if (mode == VoxelizationMode::CORNER) {
center = Eigen::Vector3f(w, h, d);
}
// count number of intersections.
int num_intersect = 0;
for (unsigned int f = 0; f < this->num_faces(); ++f) {
Eigen::Vector3f v1 = this->vertices[this->faces[f](0)];
Eigen::Vector3f v2 = this->vertices[this->faces[f](1)];
Eigen::Vector3f v3 = this->vertices[this->faces[f](2)];
Eigen::Vector3f closest_point;
triangle_point_distance(center, v1, v2, v3, closest_point);
float distance = (center - closest_point).norm();
if (distance < sdf(h, w, d)) {
sdf(h, w, d) = distance;
}
bool intersect = triangle_ray_intersection(center, Eigen::Vector3f(0, 0, 0), v1, v2, v3, distance);
if (intersect && distance >= 0) {
num_intersect++;
}
}
if (num_intersect%2 == 1) {
sdf(h, w, d) *= -1;
}
}
}
}
/** \brief Voxelize the given mesh into an occupancy grid.
* \param[out] occ volume to fill
*/
void voxelize_occ(Eigen::Tensor<int, 3, Eigen::RowMajor>& occ, const VoxelizationMode &mode) {
int height = occ.dimension(0);
int width = occ.dimension(1);
int depth = occ.dimension(2);
#pragma omp parallel
{
#pragma omp for
for (int i = 0; i < height*width*depth; i++) {
int d = i%depth;
int w = (i/depth)%width;
int h = (i/depth)/width;
Eigen::Vector3f min(w, h, d);
Eigen::Vector3f max(w + 1, h + 1, d + 1);
for (unsigned int f = 0; f < this->num_faces(); ++f) {
Eigen::Vector3f v1 = this->vertices[this->faces[f](0)];
Eigen::Vector3f v2 = this->vertices[this->faces[f](1)];
Eigen::Vector3f v3 = this->vertices[this->faces[f](2)];
bool overlap = triangle_box_intersection(min, max, v1, v2, v3);
if (overlap) {
occ(h, w, d) = 1;
break;
}
}
}
}
}
/** \brief Voxelize the given mesh into an occupancy grid preserving color.
* \param[out] occ volume to fill
*/
void voxelize_occ_color(Eigen::Tensor<int, 3, Eigen::RowMajor>& occ, const VoxelizationMode &mode) {
int height = occ.dimension(0);
int width = occ.dimension(1);
int depth = occ.dimension(2);
#pragma omp parallel
{
#pragma omp for
for (int i = 0; i < height*width*depth; i++) {
int d = i%depth;
int w = (i/depth)%width;
int h = (i/depth)/width;
Eigen::Vector3f min(w, h, d);
Eigen::Vector3f max(w + 1, h + 1, d + 1);
for (int f = 0; f < this->num_faces(); ++f) {
Eigen::Vector3f v1 = this->vertices[this->faces[f](0)];
Eigen::Vector3f v2 = this->vertices[this->faces[f](1)];
Eigen::Vector3f v3 = this->vertices[this->faces[f](2)];
bool overlap = triangle_box_intersection(min, max, v1, v2, v3);
if (overlap) {
if (f == 0){ // since 0 are treated as not voxelized space, need to deal with face index 0
occ(h, w, d) = num_faces(); // face 0 has now a new face no. that needs to be taken into account when converting back to off
break;
}
occ(h, w, d) = f;
break;
}
}
}
}
}
private:
/** \brief Vertices as (x,y,z)-vectors. */
std::vector<Eigen::Vector3f> vertices;
/** \brief Faces as list of vertex indices. */
std::vector<Eigen::Vector3i> faces;
/** \brief Colours as RGB vectors. */
std::vector<Eigen::Vector3i> colors;
};
/** \brief Write the given set of volumes to h5 file.
* \param[in] filepath h5 file to write
* \param[in] n number of volumes
* \param[in] height height of volumes
* \param[in] width width of volumes
* \param[in] depth depth of volumes
* \param[in] dense volume data
*/
template<int RANK>
bool write_float_hdf5(const std::string filepath, Eigen::Tensor<float, RANK, Eigen::RowMajor>& tensor) {
try {
/*
* Turn off the auto-printing when failure occurs so that we can
* handle the errors appropriately
*/
H5::Exception::dontPrint();
/*
* Create a new file using H5F_ACC_TRUNC access,
* default file creation properties, and default file
* access properties.
*/
H5::H5File file(filepath, H5F_ACC_TRUNC);
/*
* Define the size of the array and create the data space for fixed
* size dataset.
*/
hsize_t rank = RANK;
hsize_t dimsf[rank];
for (int i = 0; i < rank; i++) {
dimsf[i] = tensor.dimension(i);
}
H5::DataSpace dataspace(rank, dimsf);
/*
* Define datatype for the data in the file.
* We will store little endian INT numbers.
*/
H5::IntType datatype(H5::PredType::NATIVE_FLOAT);
datatype.setOrder(H5T_ORDER_LE);
/*
* Create a new dataset within the file using defined dataspace and
* datatype and default dataset creation properties.
*/
H5::DataSet dataset = file.createDataSet("tensor", datatype, dataspace);
/*
* Write the data to the dataset using default memory space, file
* space, and transfer properties.
*/
float* data = static_cast<float*>(tensor.data());
dataset.write(data, H5::PredType::NATIVE_FLOAT);
} // end of try block
// catch failure caused by the H5File operations
catch(H5::FileIException error) {
error.printErrorStack();
return false;
}
// catch failure caused by the DataSet operations
catch(H5::DataSetIException error) {
error.printErrorStack();
return false;
}
// catch failure caused by the DataSpace operations
catch(H5::DataSpaceIException error) {
error.printErrorStack();
return false;
}
// catch failure caused by the DataSpace operations
catch(H5::DataTypeIException error) {
error.printErrorStack();
return false;
}
return true;
}
/** \brief Write the given set of volumes to h5 file.
* \param[in] filepath h5 file to write
* \param[in] n number of volumes
* \param[in] height height of volumes
* \param[in] width width of volumes
* \param[in] depth depth of volumes
* \param[in] dense volume data
*/
template<int RANK>
bool write_int_hdf5(const std::string filepath, Eigen::Tensor<int, RANK, Eigen::RowMajor>& tensor) {
try {
/*
* Turn off the auto-printing when failure occurs so that we can
* handle the errors appropriately
*/
H5::Exception::dontPrint();
/*
* Create a new file using H5F_ACC_TRUNC access,
* default file creation properties, and default file
* access properties.
*/
H5::H5File file(filepath, H5F_ACC_TRUNC);
/*
* Define the size of the array and create the data space for fixed
* size dataset.
*/
hsize_t rank = RANK;
hsize_t dimsf[rank];
for (int i = 0; i < rank; i++) {
dimsf[i] = tensor.dimension(i);
}
H5::DataSpace dataspace(rank, dimsf);
/*
* Define datatype for the data in the file.
* We will store little endian INT numbers.
*/
H5::IntType datatype(H5::PredType::NATIVE_INT);
datatype.setOrder(H5T_ORDER_LE);
/*
* Create a new dataset within the file using defined dataspace and
* datatype and default dataset creation properties.
*/
H5::DataSet dataset = file.createDataSet("tensor", datatype, dataspace);
/*
* Write the data to the dataset using default memory space, file
* space, and transfer properties.
*/
int* data = static_cast<int*>(tensor.data());
dataset.write(data, H5::PredType::NATIVE_INT);
} // end of try block
// catch failure caused by the H5File operations
catch(H5::FileIException error) {
error.printErrorStack();
return false;
}
// catch failure caused by the DataSet operations
catch(H5::DataSetIException error) {
error.printErrorStack();
return false;
}
// catch failure caused by the DataSpace operations
catch(H5::DataSpaceIException error) {
error.printErrorStack();
return false;
}
// catch failure caused by the DataSpace operations
catch(H5::DataTypeIException error) {
error.printErrorStack();
return false;
}
return true;
}
/** \brief Read all files in a directory matching the given extension.
* \param[in] directory path to directory
* \param[out] files read file paths
* \param[in] extension extension to filter for
*/
void read_directory(const boost::filesystem::path directory, std::map<int, boost::filesystem::path>& files, const std::string extension = ".off") {
files.clear();
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator it(directory); it != end; ++it) {
if (it->path().extension().string() == extension) {
int number = std::stoi(it->path().filename().string());
files.insert(std::pair<int, boost::filesystem::path>(number, it->path()));
}
}
}
/** \brief Main entrance point of the script.
* Expects one parameter, the path to the corresponding config file in config/.
*/
int main(int argc, char** argv) {
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("mode", boost::program_options::value<std::string>()->default_value("occ"), "operation mode, 'occ' or 'sdf'")
("input", boost::program_options::value<std::string>(), "input, either single OFF file or directory containing OFF files where the names correspond to integers (zero padding allowed) and are consecutively numbered starting with zero")
("height", boost::program_options::value<int>()->default_value(32), "height of volume, corresponding to y-axis (=up)")
("width", boost::program_options::value<int>()->default_value(32), "width of volume, corresponding to x-axis (=right")
("depth", boost::program_options::value<int>()->default_value(32), "depth of volume, corresponding to z-axis (=forward)")
("center", boost::program_options::bool_switch()->default_value(false), "by default, the top-left-front corner is used for SDF computation; if instead the voxel centers should be used, set this flag")
("color", boost::program_options::bool_switch()->default_value(false), "by default, color information is not kept after voxelization, set this flag if you want to voxelize with colors")
("output", boost::program_options::value<std::string>(), "output file, will be a HDF5 file containing either a N x C x height x width x depth tensor or a C x height x width x depth tensor, where N is the number of files and C=2 the number of channels, N is discarded if only a single file is processed; should have the .h5 extension");
boost::program_options::positional_options_description positionals;
positionals.add("mode", 1);
positionals.add("input", 1);
positionals.add("output", 1);
boost::program_options::variables_map parameters;
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(positionals).run(), parameters);
boost::program_options::notify(parameters);
if (parameters.find("help") != parameters.end()) {
std::cout << desc << std::endl;
return 0;
}
std::string mode = parameters["mode"].as<std::string>();
if (mode == "occ") {
std::cout << "Voxelizing occupancy grids." << std::endl;
}
else if (mode == "sdf") {
std::cout << "Voxelizing SDFs." << std::endl;
}
else {
std::cout << "Invalid mode, choose from occ or sdf." << std::endl;
return 1;
}
boost::filesystem::path input(parameters["input"].as<std::string>());
if (!boost::filesystem::is_directory(input) && !boost::filesystem::is_regular_file(input)) {
std::cout << "Input is neither directory nor file." << std::endl;
return 1;
}
boost::filesystem::path output(parameters["output"].as<std::string>());
if (boost::filesystem::is_regular_file(output)) {
std::cout << "Output file already exists; overwriting." << std::endl;
}
VoxelizationMode voxelization_mode;
if (parameters["center"].as<bool>()) {
voxelization_mode = VoxelizationMode::CENTER;
std::cout << "Using the top-left-front voxel corner for voxelization." << std::endl;
}
else {
voxelization_mode = VoxelizationMode::CORNER;
std::cout << "Using the voxel center for voxelization." << std::endl;
}
int height = parameters["height"].as<int>();
int width = parameters["width"].as<int>();
int depth = parameters["depth"].as<int>();
std::cout << "Voxelizing into " << height << " x " << width << " x " << depth << " (height x width x depth)." << std::endl;
bool color = parameters["color"].as<bool>();
if (color == true){
std::cout << "Colors will be preserved." << std::endl;
}
if (boost::filesystem::is_regular_file(input)) {
Mesh mesh;
bool success = Mesh::from_off(input.string(), mesh, color);
if (!success) {
std::cout << "Could not read " << input << "." << std::endl;
return 1;
}
std::cout << "Read " << input << "." << std::endl;
if (mode == "sdf") {
Eigen::Tensor<float, 3, Eigen::RowMajor> tensor(height, width, depth);
mesh.voxelize_sdf(tensor, voxelization_mode);
std::cout << "Voxelized " << input << "." << std::endl;
bool success = write_float_hdf5<3>(output.string(), tensor);
if (!success) {
std::cout << "Could not write " << output << "." << std::endl;
return 1;
}
}
if (mode == "occ") {
Eigen::Tensor<int, 3, Eigen::RowMajor> tensor(height, width, depth);
tensor.setZero();
if (color == true){
mesh.voxelize_occ_color(tensor, voxelization_mode);
Eigen::Tensor<int, 2, Eigen::RowMajor> color_array(mesh.num_colors(), 3);
color_array.setZero();
mesh.return_colors(color_array);
int end_pos = input.string().find(".off");
std::string color_output = input.string();
color_output = color_output.erase(end_pos, end_pos+4) + "_color.h5";
success = write_int_hdf5<2>(color_output, color_array);
if (!success) {
std::cout << "Could not write " << color_output << "." << std::endl;
return 1;
}
std::cout << "Created color reference HDF5 file " << color_output << "." << std::endl;
}
else{
mesh.voxelize_occ(tensor, voxelization_mode);
}
std::cout << "Voxelized " << input << "." << std::endl;
bool success = write_int_hdf5<3>(output.string(), tensor);
if (!success) {
std::cout << "Could not write " << output << "." << std::endl;
return 1;
}
}
std::cout << "Wrote " << output << "." << std::endl;
std::cout << "The output is a " << height << " x " << width << " x " << depth << " tensor." << std::endl;
}
else {
std::map<int, boost::filesystem::path> input_files;
read_directory(input, input_files);
if (input_files.size() <= 0) {
std::cout << "Could not find any OFF files in the input directory." << std::endl;
return 1;
}
std::cout << "Read " << input_files.size() << " files." << std::endl;
if (mode == "sdf") {
Eigen::Tensor<float, 4, Eigen::RowMajor> tensor(input_files.size(), height, width, depth);
int i = 0;
for (std::map<int, boost::filesystem::path>::iterator it = input_files.begin(); it != input_files.end(); it++) {
Mesh mesh;
bool success = Mesh::from_off(it->second.string(), mesh, color);
if (!success) {
std::cout << "Could not read " << it->second << "." << std::endl;
return 1;
}
Eigen::Tensor<float, 3, Eigen::RowMajor> slice(height, width, depth);
mesh.voxelize_sdf(slice, voxelization_mode);
tensor.chip(i, 0) = slice;
std::cout << "Voxelized " << it->second << " (" << (i + 1) << " of " << input_files.size() << ")." << std::endl;
i++;
}
bool success = write_float_hdf5<4>(output.string(), tensor);
if (!success) {
std::cout << "Could not write " << output << "." << std::endl;
return 1;
}
}
if (mode == "occ") {
Eigen::Tensor<int, 4, Eigen::RowMajor> tensor(input_files.size(), height, width, depth);
tensor.setZero();
int i = 0;
for (std::map<int, boost::filesystem::path>::iterator it = input_files.begin(); it != input_files.end(); it++) {
Mesh mesh;
bool success = Mesh::from_off(it->second.string(), mesh, color);
if (!success) {
std::cout << "Could not read " << it->second << "." << std::endl;
return 1;
}
Eigen::Tensor<int, 3, Eigen::RowMajor> slice(height, width, depth);
slice.setZero();
if (color == true){
mesh.voxelize_occ_color(slice, voxelization_mode);
tensor.chip(i, 0) = slice;
Eigen::Tensor<int, 2, Eigen::RowMajor> color_array(mesh.num_colors(), 3);
color_array.setZero();
mesh.return_colors(color_array);
int end_pos = it->second.string().find(".off");
std::string color_output = it->second.string();
color_output = color_output.erase(end_pos, end_pos+4) + "_color.h5";
success = write_int_hdf5<2>(color_output, color_array);
if (!success) {
std::cout << "Could not write " << color_output << "." << std::endl;
return 1;
}
std::cout << "Created color reference HDF5 file " << color_output << "." << std::endl;
}
else{
mesh.voxelize_occ(slice, voxelization_mode);
tensor.chip(i, 0) = slice;
}
std::cout << "Voxelized " << it->second << " (" << (i + 1) << " of " << input_files.size() << ")." << std::endl;
i++;
}
bool success = write_int_hdf5<4>(output.string(), tensor);
if (!success) {
std::cout << "Could not write " << output << "." << std::endl;
return 1;
}
}
std::cout << "Wrote " << output << "." << std::endl;
std::cout << "The output is a " << input_files.size() << " x " << height << " x " << width << " x " << depth << " tensor." << std::endl;
}
return 0;
}