This repository was archived by the owner on Sep 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
286 lines (246 loc) · 8.75 KB
/
main.cpp
File metadata and controls
286 lines (246 loc) · 8.75 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
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <chrono>
#include <iterator>
#include <cctype>
#include <cstring>
#include "VOX.h"
#include "VOXSplit.h"
#include "Model.h"
#include "helpFunctions.h"
#include "ParamManager.h"
#include "DirUtils.h"
using namespace std;
using namespace chrono;
int singleVOX2OBJ(
string inPath, string outPath, string mtlPath,
bool split = false, float scale = 0.03125f,
bool markSides = false, bool marchingCubes = false,
const char* modelName = nullptr
);
int main(int argc, char** argv) {
//Checking args
if(argc <= 1) {
cerr << "No parameters given! Aborting..." << endl;
return 1;
}
ParamManager paramManager;
paramManager.addParamSeparator();
paramManager.addParam("-i", "--in", "Sets input VOX file", "INPUT_VOX");
paramManager.addParam("-o", "--out", "Sets output OBJ file (overrites existing file!)", "OUTPUT_OBJ");
paramManager.addParam("-id", "--in-dir", "Sets input directory for recursively find VOX files (use with -od flag, relative dir)", "INPUT_VOX_DIR");
paramManager.addParam(
"-od", "--out-dir", "Sets output directory for recursively found VOX files (use with -id flag, relative dir), copying -id dirs structure", "OUTPUT_VOX_DIR"
);
paramManager.addParamSeparator();
paramManager.addParam("-m", "--mtl", "Sets output MTL file (used in OBJs)", "INPUT_MTL");
paramManager.addParam("-cm", "--create-mtl", "Creates default MTL (overrites existing file!)", "OUTPUT_MTL");
paramManager.addParam("-t", "--texture", "Uses texture (use with -cm flag)", "INTPUT_TEXTURE");
paramManager.addParam("-s", "--split", "Takes separated parts of VOX model and splits them into separated OBJs", "");
paramManager.addParam("-as", "--auto-split", "Uses -s flag on every file with given sufinx in VOX file name (overrites -s flag)", "SUFIX");
paramManager.addParam("-sc", "--scale", "Changes scale of output OBJ, default: 0.03125", "SCALE");
paramManager.addParam("-ms", "--mark-sides", "Adds side indicator L or R to OBJ file name after spliting (use with -s or -as flags)", "");
paramManager.addParam("-mc", "--marching-cubes", "Uses Marching Cubes algorithm to generate output OBJ", "");
paramManager.addParam("-if", "--ignore-filter", "Ignores conversion of file if name/path contains given phrase (use with -id and -od)", "IGNORE_PHRASE");
//paramManager.addParam("-fl", "--fail-log", "Creates log for failed VOX conversions (use with -id and -od)", "OUTPUT_LOG");
paramManager.addParam("-t", "--time", "Shows time of VOX to OBJ conversion", "");
if(paramManager.process(argc, argv) == false)
return 1;
//--
//MTL Creation
string mtlCreatePath = paramManager.getValueOf("-cm");
if(mtlCreatePath != "") {
if(fileExists(mtlCreatePath)) {
cerr << "MTL creation file exists! Overriting!" << endl;
}
string texturePath = paramManager.getValueOf("-t");
if(texturePath == "") texturePath = "texture.png";
fstream hMTL(mtlCreatePath, ios::trunc | ios::out);
hMTL
<< "newmtl " << mtlCreatePath << endl
<< "illum 1" << endl
<< "Ka 0.000 0.000 0.000" << endl
<< "Kd 1.000 1.000 1.000" << endl
<< "Ks 0.000 0.000 0.000" << endl
<< "map_Kd " << texturePath
<< endl;
hMTL.flush();
hMTL.close();
}
//Time
bool timeShow = paramManager.hasValue("-t");
time_point<high_resolution_clock> start = high_resolution_clock::now();
time_point<high_resolution_clock> overallTime = high_resolution_clock::now();
//Convertion
bool multipleFiles = (paramManager.hasValue("-id") and paramManager.hasValue("-od"));
if(multipleFiles) {
if(isDir(paramManager.getValueOf("-id"))) {
vector<string> dirs;
vector<string> voxFiles;
string outDir = paramManager.getValueOf("-od");
string ignorePhrase = paramManager.getValueOf("-if");
bool ignoreFilter = paramManager.hasValue("-if");
//Find directories structure
dirs.push_back(paramManager.getValueOf("-id"));
getDirectoriesList(dirs);
//Copy structure
createDirectoryTree(outDir, dirs);
/////////////////////////////////
ofstream hLog;
if(paramManager.hasValue("-fl"))
hLog.open(paramManager.getValueOf("-fl"), ios::trunc | ios::out);
float scale = paramManager.hasValue("-sc")? str2float(paramManager.getValueOf("-sc")): 0.03125f;
string mtlFile = paramManager.getValueOf("-m");
bool doSplit = paramManager.hasValue("-s");
string autoSplitVal = paramManager.getValueOf("-as");
int fileIdx = 1;
for(string path : dirs) {
if(isDir(path)) {
DIR* dir = opendir(path.c_str());
dirent* dp = readdir(dir);
do {
string name = dp->d_name;
if(ignoreFilter and name.find(ignorePhrase) != string::npos) {
dp = readdir(dir);
cout << "[IGNORED] " << outDir << '/' << path << '/' << name << endl;
continue;
}
if(name != "." and name != "..") {
if(endsWith(tolower(name), ".vox")) {
if(timeShow)
start = high_resolution_clock::now();
if(not autoSplitVal.empty()) {
string ending = name.substr(0, name.length() - 4);
doSplit = endsWith(ending, autoSplitVal);
}
string out = outDir + '/' + path + '/' + name;
out.replace(out.find_last_of(".vox") - 3, 4, ".obj");
cout << "[" << (fileIdx++) << "] " << out << " => " << flush;
int result = singleVOX2OBJ(
path + "/" + name, out,
mtlFile, doSplit, scale,
paramManager.hasValue("-ms"), paramManager.hasValue("-mc"),
name.substr(0, name.length() - 4).c_str()
);
if(result > 0) {
cout << "Done! (" << result << " parts)";
} else {
cout << "Fail!!!!!!!!!!" << endl;
if(paramManager.hasValue("-fl") and hLog.good())
hLog << "Cannot convert file: " << (path + "/" + name);
}
if(timeShow)
cout << " " << duration_cast<milliseconds>(high_resolution_clock::now() - start).count() << "ms";
cout << endl;
}
}
dp = readdir(dir);
} while(dp not_eq NULL);
closedir(dir);
}
}
if(paramManager.hasValue("-fl") and hLog.good())
hLog.close();
}
} else {
if(paramManager.hasValue("-id") or paramManager.hasValue("-od")) {
cerr << "Flags -id and -od are usefull only when used together! Aborting...";
return 1;
} else {
string name = paramManager.getValueOf("-i");
bool doSplit = paramManager.hasValue("-s");
string autoSplitVal = paramManager.getValueOf("-as");
if(not autoSplitVal.empty()) {
string ending = tolower(name).substr(0, name.length() - 4);
doSplit = endsWith(ending, autoSplitVal);
}
if(singleVOX2OBJ(
name, paramManager.getValueOf("-o"),
paramManager.getValueOf("-m"), doSplit,
paramManager.hasValue("-sc")? str2float(paramManager.getValueOf("-sc")): 0.03125f,
paramManager.hasValue("-ms"), paramManager.hasValue("-mc"),
name.substr(0, name.length() - 4).c_str()
) == 0) {
return 1;
}
}
}
if(timeShow) {
cout << "Done in: " << duration_cast<seconds>(high_resolution_clock::now() - overallTime).count() << "s" << endl;
}
return 0;
}
int singleVOX2OBJ(string inPath, string outPath, string mtlPath, bool split, float scale, bool markSides, bool marchingCubes, const char* modelName) {
VOXModel vox;
Model model;
if(modelName != nullptr) {
model.name = modelName;
}
model.SetScale(scale);
if(inPath.empty()) {
cerr << "No input file given! Aborting!" << endl;
return 0;
} else if(not vox.Load(inPath)) {
cerr << "Cannot load VOX file: '" << inPath << "'... Aborting!" << endl;
return 0;
}
if(outPath != "") {
if(fileExists(outPath)) {
cerr << "Output file exists! Overriting!" << endl;
}
} else {
cerr << "No output file path given! Using '" << inPath << ".obj'!" << endl;
outPath = inPath + ".obj";
}
if(mtlPath.empty())
mtlPath = "material.mtl";
if(split) {
vector<VOXModel*> models = SplitVOX(vox);
if(models.size() > 1) {
int partNum = 0;
size_t lastOBJ = outPath.find_last_of(".obj");
if(lastOBJ != string::npos) {
outPath.replace(lastOBJ - 3, 4, "_");
}
string sideAddition;
for(VOXModel* mdl : models) {
if(marchingCubes) {
model.LoadVOXMarchingCubes(*mdl);
} else {
model.LoadVOX(*mdl);
}
if(markSides) {
vec4f center = model.Center();
if(center.x < 0) {
sideAddition = "_L";
} else if(center.x > 0) {
sideAddition = "_R";
} else {
sideAddition = "_C";
}
cout << sideAddition << ", ";
}
model.SaveOBJ(
outPath + tostring(partNum++) + sideAddition + ".obj",
mtlPath
);
delete mdl;
model.Clear();
}
partNum = models.size();
models.clear();
return partNum;
}
}
if(marchingCubes) {
model.LoadVOXMarchingCubes(vox);
} else {
model.LoadVOX(vox);
}
model.SaveOBJ(outPath, mtlPath);
return 1;
}