-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.cpp
215 lines (173 loc) · 6.66 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
#include "includes/args.h"
#include "includes/CompFab.h"
#include "includes/Mesh.h"
#include "includes/utils.h"
#include <tclap/CmdLine.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
// #include <cstdlib>
#include <cstdlib>
enum FileFormat { obj, binvox };
struct VoxelizerArgs : Args {
// path to files
std::string input, output;
FileFormat format;
bool double_thick;
// voxelization settings
int size;
// width, height, depth;
int samples;
};
// construct the command line arguments
VoxelizerArgs * parseArgs(int argc, char *argv[]) {
VoxelizerArgs * args = new VoxelizerArgs();
// Define the command line object
TCLAP::CmdLine cmd("A simple voxelization utility.", ' ', "0.0");
TCLAP::UnlabeledValueArg<std::string> input( "input", "path to .obj mesh", true, "", "string");
TCLAP::UnlabeledValueArg<std::string> output("output","path to save voxel grid", true, "", "string");
TCLAP::ValueArg<std::string> format("f", "format","voxel grid save format - obj|binvox", false, "binvox", "string");
TCLAP::ValueArg<int> size( "r","resolution", "voxelization resolution", false, 32, "int");
TCLAP::ValueArg<int> samples( "s","samples", "number of sample rays per vertex", false, -1, "int");
TCLAP::MultiSwitchArg verbosity( "v", "verbose", "Verbosity level. Multiple flags for more verbosity.");
TCLAP::SwitchArg double_thick( "d", "double", "Flag for processing double-thick meshes. Uses (num_intersections/2)%2 for occupancy checking.", false);
// Add args to command line object and parse
cmd.add(input); cmd.add(output); // order matters for positional args
cmd.add(size); cmd.add(format);
// cmd.add(width); cmd.add(height); cmd.add(depth);
cmd.add(verbosity); cmd.add(samples); cmd.add(double_thick);
cmd.parse( argc, argv );
// store in wrapper struct
args->input = input.getValue();
args->output = output.getValue();
args->size = size.getValue();
// args->width = width.getValue();
// args->height = height.getValue();
// args->depth = depth.getValue();
args->samples = samples.getValue();
args->verbosity = verbosity.getValue();
args->double_thick = double_thick.getValue();
args->debug(1) << "input: " << args->input << std::endl;
args->debug(1) << "output: " << args->output << std::endl;
char fl = format.getValue().at(0);
if (fl == 'b' || fl == 'B') {
args->format = binvox;
args->debug(1) << "save format: binvox" << std::endl;
} else if (fl == 'o' || fl == 'O') {
args->format = obj;
args->debug(1) << "save format: obj" << std::endl;
} else {
args->debug(0) << "Unknown file format specified, use one of: (o) obj, (b) binvox" << std::endl;
}
// args->debug(1) << "format: " << args->format << std::endl;
args->debug(1) << "size: " << args->size << std::endl;
// args->debug(1) << "width: " << args->width << std::endl;
// args->debug(1) << "height: " << args->height << std::endl;
// args->debug(1) << "depth: " << args->height << std::endl;
args->debug(1) << "samples: " << args->samples << std::endl;
args->debug(1) << "verbosity: " << args->verbosity << std::endl;
if (args->double_thick) args->debug(1) << "Processing mesh as double-thick." << std::endl;
return args;
}
typedef std::vector<CompFab::Triangle> TriangleList;
TriangleList g_triangleList;
CompFab::VoxelGrid *g_voxelGrid;
bool loadMesh(const char *filename, unsigned int dim)
{
g_triangleList.clear();
Mesh *tempMesh = new Mesh(filename, true);
CompFab::Vec3 v1, v2, v3;
//copy triangles to global list
for(unsigned int tri =0; tri<tempMesh->t.size(); ++tri)
{
v1 = tempMesh->v[tempMesh->t[tri][0]];
v2 = tempMesh->v[tempMesh->t[tri][1]];
v3 = tempMesh->v[tempMesh->t[tri][2]];
g_triangleList.push_back(CompFab::Triangle(v1,v2,v3));
}
//Create Voxel Grid
CompFab::Vec3 bbMax, bbMin;
BBox(*tempMesh, bbMin, bbMax);
//Build Voxel Grid
double bbX = bbMax[0] - bbMin[0];
double bbY = bbMax[1] - bbMin[1];
double bbZ = bbMax[2] - bbMin[2];
double spacing;
if(bbX > bbY && bbX > bbZ)
{
spacing = bbX/(double)(dim-2);
} else if(bbY > bbX && bbY > bbZ) {
spacing = bbY/(double)(dim-2);
} else {
spacing = bbZ/(double)(dim-2);
}
CompFab::Vec3 hspacing(0.5*spacing, 0.5*spacing, 0.5*spacing);
g_voxelGrid = new CompFab::VoxelGrid(bbMin-hspacing, dim, dim, dim, spacing);
delete tempMesh;
return true;
}
void saveVoxelsToObj(const char * outfile)
{
Mesh box;
Mesh mout;
int nx = g_voxelGrid->m_dimX;
int ny = g_voxelGrid->m_dimY;
int nz = g_voxelGrid->m_dimZ;
double spacing = g_voxelGrid->m_spacing;
CompFab::Vec3 hspacing(0.5*spacing, 0.5*spacing, 0.5*spacing);
for (int ii = 0; ii < nx; ii++) {
for (int jj = 0; jj < ny; jj++) {
for (int kk = 0; kk < nz; kk++) {
if(!g_voxelGrid->isInside(ii,jj,kk)){
continue;
}
CompFab::Vec3 coord(0.5f + ((double)ii)*spacing, 0.5f + ((double)jj)*spacing, 0.5f+((double)kk)*spacing);
CompFab::Vec3 box0 = coord - hspacing;
CompFab::Vec3 box1 = coord + hspacing;
makeCube(box, box0, box1);
mout.append(box);
}
}
}
mout.save_obj(outfile);
}
bool save(VoxelizerArgs *args) {
switch (args->format) {
case obj:
saveVoxelsToObj((args->output + ".obj").c_str());
break;
case binvox:
g_voxelGrid->save_binvox((args->output + ".binvox").c_str());
break;
default:
args->debug(0) << "Failed to save - no file type specified." << std::endl;
return false;
}
return true;
}
extern void kernel_wrapper(int samples, int w, int h, int d, CompFab::VoxelGrid *g_voxelGrid, std::vector<CompFab::Triangle> triangles, bool double_thick);
int main(int argc, char *argv[])
{
VoxelizerArgs *args = parseArgs(argc, argv);
args->debug(0) << "\nLoading Mesh" << std::endl;
loadMesh(args->input.c_str(), args->size);
clock_t start = clock();
args->debug(0) << "Voxelizing in the GPU, this might take a while." << std::endl;
if (args->samples > -1) args->debug(0) << "Randomly choosing " << args->samples << " directions." << std::endl;
kernel_wrapper(args->samples, args->size, args->size, args->size, g_voxelGrid, g_triangleList, args->double_thick);
// Summary: teapot.obj (9000 triangles) @ 512x512x512, 3 samples in: 15 seconds
args->debug(0) << "Summary: "
<< utils::split(args->input, '/').back()
<< " (" << g_triangleList.size() << " triangles)"
<< " @ " << g_voxelGrid->m_dimX << "x" << g_voxelGrid->m_dimY << "x" << g_voxelGrid->m_dimZ;
if (args->samples > 0)
args->debug(0) << ", " << args->samples << " samples" ;
else args->debug(0) << ", 1 sample" ;
args->debug(0) << " in: " << float( clock() - start ) / CLOCKS_PER_SEC << " seconds" << std::endl;
args->debug(0) << "Saving Results." << std::endl;
if (!save(args)) {
args->debug(0) << "Failed to save! Exiting." << std::endl;
};
return 0;
}