-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathobj-magic.cpp
More file actions
297 lines (272 loc) · 11.2 KB
/
obj-magic.cpp
File metadata and controls
297 lines (272 loc) · 11.2 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
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <limits>
#include <map>
#include "../glm/vec2.hpp"
#include "../glm/vec3.hpp"
#include "../glm/mat3x3.hpp"
#include "../glm/mat4x4.hpp"
#include "../glm/gtc/matrix_transform.hpp"
#include "../glm/gtx/component_wise.hpp"
#include "args.hpp"
#define APPNAME "obj-magic"
#define VERSION "v0.5"
#define EPSILON 0.00001f
#define W 12
using namespace glm;
std::string toString(vec3 vec) {
std::ostringstream oss;
oss << std::right << std::setw(W) << vec.x << std::setw(W) << vec.y << std::setw(W) << vec.z;
return oss.str();
}
template<typename T> inline bool isZero(T v) { v = abs(v); return v.x < EPSILON && v.y < EPSILON && v.z < EPSILON; }
template<typename T> inline bool isOne(T v) { return isZero(v - T(1)); }
template<typename T> inline bool isEqual(T a, T b) { return isZero(a - b); }
int main(int argc, char* argv[]) {
Args args(argc, argv);
if (args.opt('v', "version")) {
std::cerr << APPNAME << " " << VERSION << std::endl;
return EXIT_SUCCESS;
}
if (args.opt('h', "help") || argc < 3) {
std::cerr << "Usage: " << args.app() << " PARAM [PARAM...] FILE [FILE...]" << std::endl;
std::cerr << "Parameters:" << std::endl;
std::cerr << " -h --help print this help and exit" << std::endl;
std::cerr << " -v --version print version and exit" << std::endl;
std::cerr << " -o --out FILE put output to FILE instead of stdout (if 1 input given)" << std::endl;
std::cerr << " -O --overwrite edit input file directly, overwriting it" << std::endl;
std::cerr << " -i --info print info about the object and exit" << std::endl;
std::cerr << " -n --normalize-normals renormalize all normals" << std::endl;
std::cerr << " -n --invert-normals invert all normals" << std::endl;
std::cerr << " -c --center[xyz] center object" << std::endl;
std::cerr << " -s --scale[xyz] SCALE scale object SCALE amount" << std::endl;
std::cerr << " --scaleuv[xy] SCALE multiply texture coords by SCALE amount" << std::endl;
std::cerr << " --invertuv[xy] make texture coord 1-original" << std::endl;
std::cerr << " --mirror[xyz] mirror object" << std::endl;
std::cerr << " --translate[xyz] AMOUNT translate AMOUNT amount" << std::endl;
std::cerr << " --rotate[xyz] AMOUNT rotate along axis AMOUNT degrees" << std::endl;
std::cerr << " --fit[xyz] AMOUNT uniformly scale to fit AMOUNT in dimension" << std::endl;
std::cerr << " --resize[xyz] AMOUNT non-uniformly scale to fit AMOUNT in dimension" << std::endl;
std::cerr << std::endl;
std::cerr << "Multiple input files will force --overwrite mode." << std::endl;
std::cerr << "[xyz] - long option suffixed with x, y or z operates only on that axis." << std::endl;
std::cerr << "No suffix (or short form) assumes all axes." << std::endl;
std::cerr << "Example: " << args.app() << " --scale 0.5 model.obj" << std::endl;
std::cerr << " or: " << args.app() << " --mirrorx model.obj" << std::endl;
return args.opt('h', "help") ? EXIT_SUCCESS : EXIT_FAILURE;
}
bool info = args.opt('i', "info");
bool normalize_normals = args.opt('n', "normalize-normals");
vec3 normal_scale = args.opt(' ', "invert-normals") ? vec3(-1.0f) : vec3(1.0);
// Output stream handling
std::vector<std::string> files = args.orphans();
auto removed_files_it = std::remove_if(files.begin(), files.end(), [](const std::string& file) { return file.find(".obj") == std::string::npos; });
files.erase(removed_files_it, files.end());
if (files.empty()) {
std::cerr << "Need at least one input file!" << std::endl;
return EXIT_FAILURE;
}
std::string outfile = args.arg<std::string>('o', "out");
std::ofstream fout;
bool inPlaceOutput = false;
if (files.size() > 1) {
inPlaceOutput = !info;
if (!outfile.empty()) {
std::cerr << "Can't use -o / --out option with multiple input files." << std::endl;
return EXIT_FAILURE;
}
} else if (outfile == files[0] || args.opt('O', "overwrite")) { // In-place
inPlaceOutput = true;
} else if (!outfile.empty()) {
fout.open(outfile.c_str());
if (fout.fail()) {
std::cerr << "Failed to open file " << outfile << " for output" << std::endl;
return EXIT_FAILURE;
}
}
vec3 scale(args.arg('s', "scale", 1.0f));
scale.x *= args.arg(' ', "scalex", 1.0f);
scale.y *= args.arg(' ', "scaley", 1.0f);
scale.z *= args.arg(' ', "scalez", 1.0f);
vec2 scaleUv(args.arg(' ', "scaleuv", 1.0f));
scaleUv.x *= args.arg(' ', "scaleuvx", 1.0f);
scaleUv.y *= args.arg(' ', "scaleuvy", 1.0f);
bool flipUvX = args.opt(' ', "invertuv") || args.opt(' ', "invertuvx");
bool flipUvY = args.opt(' ', "invertuv") || args.opt(' ', "invertuvy");
vec3 translate(args.arg(' ', "translate", 0.0f));
translate.x += args.arg(' ', "translatex", 0.0f);
translate.y += args.arg(' ', "translatey", 0.0f);
translate.z += args.arg(' ', "translatez", 0.0f);
vec3 center;
if (args.opt('c', "center")) center = vec3(1);
if (args.opt(' ', "centerx")) center.x = 1;
if (args.opt(' ', "centery")) center.y = 1;
if (args.opt(' ', "centerz")) center.z = 1;
ivec3 mirror(1);
if (args.opt(' ', "mirror")) mirror = ivec3(-1);
if (args.opt(' ', "mirrorx")) mirror.x = -1;
if (args.opt(' ', "mirrory")) mirror.y = -1;
if (args.opt(' ', "mirrorz")) mirror.z = -1;
vec3 fit;
fit.x = args.arg(' ', "fitx", 0.0f);
fit.y = args.arg(' ', "fity", 0.0f);
fit.z = args.arg(' ', "fitz", 0.0f);
if (args.arg(' ', "fit", 0.0f))
fit = vec3(args.arg(' ', "fit", 0.0f));
vec3 resize;
resize.x = args.arg(' ', "resizex", 0.0f);
resize.y = args.arg(' ', "resizey", 0.0f);
resize.z = args.arg(' ', "resizez", 0.0f);
if (args.arg(' ', "resize", 0.0f))
resize = vec3(args.arg(' ', "resize", 0.0f));
vec3 rotangles(args.arg(' ', "rotate", 0.0f));
rotangles.x += args.arg(' ', "rotatex", 0.0f);
rotangles.y += args.arg(' ', "rotatey", 0.0f);
rotangles.z += args.arg(' ', "rotatez", 0.0f);
mat4 temprot(1.0f);
if (rotangles.x != 0.0f) temprot = rotate(temprot, rotangles.x, vec3(1,0,0));
if (rotangles.y != 0.0f) temprot = rotate(temprot, rotangles.y, vec3(0,1,0));
if (rotangles.z != 0.0f) temprot = rotate(temprot, rotangles.z, vec3(0,0,1));
mat3 rotation(temprot);
bool infoHeaderDone = false;
for (const std::string& infile : files) {
std::stringstream sout;
std::ifstream file(infile.c_str(), std::ios::binary);
std::ostream& out = inPlaceOutput ? sout : (outfile.empty() ? std::cout : fout);
if (!file.is_open()) {
std::cerr << "Failed to open file " << infile << std::endl;
return EXIT_FAILURE;
}
std::string row;
// Analyzing pass
bool analyze = info || (center.length() > 0.0f) || (fit.length() > 0.0f) || (resize.length() > 0.0f);
if (analyze) {
vec3 lbound(std::numeric_limits<float>::max());
vec3 ubound(-std::numeric_limits<float>::max());
std::map<std::string, unsigned> materials;
unsigned long long v_count = 0, vt_count = 0, vn_count = 0, f_count = 0, p_count = 0, l_count = 0, o_count = 0;
while (getline(file, row)) {
std::istringstream srow(row);
vec3 in;
std::string tempst;
if (row.substr(0,2) == "v ") { // Vertices
srow >> tempst >> in.x >> in.y >> in.z;
lbound = min(in, lbound);
ubound = max(in, ubound);
++v_count;
}
else if (row.substr(0,3) == "vt ") ++vt_count;
else if (row.substr(0,3) == "vn ") ++vn_count;
else if (row.substr(0,2) == "p ") ++p_count;
else if (row.substr(0,2) == "l ") ++l_count;
else if (row.substr(0,2) == "f ") ++f_count;
else if (row.substr(0,2) == "o ") ++o_count;
else if (row.substr(0,7) == "usemtl ") materials[row.substr(7)]++;
}
center *= (lbound + ubound) * 0.5f;
// Output info?
if (info) {
if (!infoHeaderDone) {
out << APPNAME << " " << VERSION << std::endl;
infoHeaderDone = true;
} else out << std::endl;
out << std::endl;
out << "Filename: " << infile << std::endl;
out << "Vertices: " << v_count << std::endl;
out << "TexCoords: " << vt_count << std::endl;
out << "Normals: " << vn_count << std::endl;
out << "Faces: " << f_count << std::endl;
out << "Points: " << p_count << std::endl;
out << "Lines: " << l_count << std::endl;
out << "Named objects: " << o_count << std::endl;
out << "Materials: " << materials.size() << std::endl;
out << " " << std::right << std::setw(W) << "x" << std::setw(W) << "y" << std::setw(W) << "z" << std::endl;
out << "Center: " << toString((lbound + ubound) * 0.5f) << std::endl;
out << "Size: " << toString(ubound - lbound) << std::endl;
out << "Lower bounds: " << toString(lbound) << std::endl;
out << "Upper bounds: " << toString(ubound) << std::endl;
continue;
}
if (fit.length()) {
vec3 size = ubound - lbound;
float fitScale = 1.f;
if (args.arg(' ', "fit", 0.f)) fitScale = args.arg(' ', "fit", 0.f) / compMax(size);
else if (fit.x) fitScale = fit.x / size.x;
else if (fit.y) fitScale = fit.y / size.y;
else if (fit.z) fitScale = fit.z / size.z;
scale *= fitScale;
}
if (resize.length()) {
vec3 size = ubound - lbound;
vec3 resizeScale(1, 1, 1);
if (resize.x) resizeScale.x = resize.x / size.x;
if (resize.y) resizeScale.y = resize.y / size.y;
if (resize.z) resizeScale.z = resize.z / size.z;
scale *= resizeScale;
}
}
auto outputUnmodifiedRow = [](std::ostream& out, const std::string& row) {
// getline stops at \n, so there might be \r hiding in there if we are reading CRLF files
int last = row.size() - 1;
if (last >= 0 && row[last] == '\r')
out << row.substr(0, last) << std::endl;
else out << row << std::endl;
};
// Output pass
file.clear();
file.seekg(0, std::ios::beg);
while (getline(file, row)) {
std::istringstream srow(row);
vec3 in;
std::string tempst;
if (row.substr(0,2) == "v ") { // Vertices
srow >> tempst >> in.x >> in.y >> in.z;
vec3 old = in;
in -= center;
in *= mirror;
in *= scale;
in = rotation * in;
in += translate;
if (old != in)
out << "v " << in.x << " " << in.y << " " << in.z << std::endl;
else outputUnmodifiedRow(out, row);
} else if (row.substr(0,3) == "vt ") { // Tex coords
srow >> tempst >> in.x >> in.y;
vec3 old = in;
if (flipUvX) in.x = 1.0f - in.x;
if (flipUvY) in.y = 1.0f - in.y;
in.x *= scaleUv.x;
in.y *= scaleUv.y;
if (old != in)
out << "vt " << in.x << " " << in.y << std::endl;
else outputUnmodifiedRow(out, row);
} else if (row.substr(0,3) == "vn ") { // Normals
srow >> tempst >> in.x >> in.y >> in.z;
vec3 old = in;
in *= normal_scale;
if (normalize_normals) in = normalize(in);
if (old != in)
out << "vn " << in.x << " " << in.y << " " << in.z << std::endl;
else outputUnmodifiedRow(out, row);
} else {
outputUnmodifiedRow(out, row);
}
}
if (inPlaceOutput) {
file.close();
std::ofstream finplaceout;
finplaceout.open(infile.c_str());
if (finplaceout.fail()) {
std::cerr << "Failed to open file " << infile << " for output" << std::endl;
return EXIT_FAILURE;
}
finplaceout << sout.rdbuf();
}
}
return EXIT_SUCCESS;
}