-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsmpltrim.cpp
357 lines (328 loc) · 13.5 KB
/
smpltrim.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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <Eigen/Core>
#include <Eigen/src/Core/IO.h>
#include "Avatar.h"
namespace {
void writePCD(const std::string& path,
const Eigen::Ref<const ark::CloudType>& data,
const std::vector<int>& indices) {
std::ofstream ofs(path);
ofs << "VERSION .7\nFIELDS x y z\nSIZE 4 4 4\nTYPE F F F\nCOUNT 1 1 1\n"
"WIDTH "
<< std::to_string(indices.size())
<< "\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0\n"
"POINTS "
<< std::to_string(indices.size()) << "\nDATA ascii\n";
ofs << std::fixed << std::setprecision(18);
for (int i : indices) {
ofs << data(0, i) << " " << data(1, i) << " " << data(2, i) << "\n";
}
}
std::vector<std::string> SMPL_JOINT_NAMES = {
"PELVIS", "L_HIP", "R_HIP", "SPINE1", "L_KNEE", "R_KNEE",
"SPINE2", "L_ANKLE", "R_ANKLE", "SPINE3", "L_FOOT", "R_FOOT",
"NECK", "L_COLLAR", "R_COLLAR", "HEAD", "L_SHOULDER", "R_SHOULDER",
"L_ELBOW", "R_ELBOW", "L_WRIST", "R_WRIST", "L_HAND", "R_HAND"};
} // namespace
int main(int argc, char** argv) {
std::cout << "WARNING: this tool is not updated for the new model format "
"(.npz)\n";
namespace po = boost::program_options;
std::string outputPath;
double thresh;
std::string rootName;
std::vector<std::string> deleteJoints;
po::options_description desc("Option arguments");
po::options_description descPositional(
"OpenARK SMPL partial avatar model creator (c) Alex Yu 2019\nPosition "
"arguments");
po::options_description descCombined("");
desc.add_options()("help", "produce help message")("names,n",
"print joint names")(
",t", po::value<double>(&thresh)->default_value(0.6),
"Threshold on total remaining joint weight (after removing joints) for "
"keeping a joint")(
",r", po::value<std::string>(&rootName)->default_value("PELVIS"),
"New root joint id, e.g. -r SPINE1")(
",d",
po::value<std::vector<std::string>>(&deleteJoints)
->multitoken()
->zero_tokens()
->composing(),
"Joint id to delete (can be specified multiple times, like -d L_HIP -d "
"R_HIP)");
descPositional.add_options()(
"output_path", po::value<std::string>(&outputPath)->required(),
"Where to write output model files");
descCombined.add(descPositional);
descCombined.add(desc);
po::variables_map vm;
po::positional_options_description posopt;
posopt.add("output_path", 1);
try {
po::store(po::command_line_parser(argc, argv)
.options(descCombined)
.positional(posopt)
.run(),
vm);
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
std::cerr << descPositional << "\n" << desc << "\n";
return 1;
}
if (vm.count("help")) {
std::cout << descPositional << "\n" << desc << "\n";
return 0;
}
if (vm.count("names")) {
for (const auto& name : SMPL_JOINT_NAMES) {
std::cout << name << " ";
}
std::cout << "\n";
return 0;
}
try {
po::notify(vm);
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
std::cerr << descPositional << "\n" << desc << "\n";
return 1;
}
using namespace boost::filesystem;
path outPath(outputPath);
path shapekeysPath = outPath / "shapekey";
if (!boost::filesystem::exists(outPath)) {
boost::filesystem::create_directories(outPath);
}
if (!boost::filesystem::exists(shapekeysPath)) {
boost::filesystem::create_directories(shapekeysPath);
}
ark::AvatarModel model;
ark::Avatar ava(model);
ava.update();
std::vector<bool> keepJoints(model.numJoints(), false);
std::vector<int> jointIndices;
std::vector<int> revJointIndices(model.numJoints(), -1);
for (auto& c : rootName) c = std::toupper(c);
int root =
std::find(SMPL_JOINT_NAMES.begin(), SMPL_JOINT_NAMES.end(), rootName) -
SMPL_JOINT_NAMES.begin();
if ((size_t)root == SMPL_JOINT_NAMES.size()) {
std::cerr << rootName << ": not a valid joint name (root)\n";
return 0;
}
keepJoints[root] = true;
jointIndices.push_back(root);
revJointIndices[root] = 0;
for (int i = root + 1; i < model.numJoints(); ++i) {
keepJoints[i] = keepJoints[model.parent(i)];
}
for (std::string& delName : deleteJoints) {
for (auto& c : delName) c = std::toupper(c);
int delId = std::find(SMPL_JOINT_NAMES.begin(), SMPL_JOINT_NAMES.end(),
delName) -
SMPL_JOINT_NAMES.begin();
if ((size_t)delId == SMPL_JOINT_NAMES.size()) {
std::cerr << delName << ": not a valid joint name (delete)\n";
return 0;
}
if (delId <= root) {
std::cerr << delName << ": cannot delete root or parent of root\n";
return 0;
}
std::vector<bool> delJoints(model.numJoints(), false);
delJoints[delId] = true;
keepJoints[delId] = false;
for (int i = delId + 1; i < model.numJoints(); ++i) {
delJoints[i] = delJoints[model.parent(i)];
if (delJoints[i]) {
keepJoints[i] = false;
}
}
}
for (int i = root + 1; i < model.numJoints(); ++i) {
if (keepJoints[i]) {
revJointIndices[i] = jointIndices.size();
jointIndices.push_back(i);
}
}
std::vector<int> pointIndices;
std::vector<int> revPointIndices(model.numPoints(), -1);
pointIndices.reserve(model.numPoints());
std::vector<std::vector<std::pair<double, int>>> pointAssignments(
model.numPoints());
for (int i = 0; i < model.numPoints(); ++i) {
double total = 0.0;
for (auto& assign : model.assignedJoints[i]) {
if (keepJoints[assign.second]) {
pointAssignments[i].emplace_back(assign);
total += assign.first;
}
}
if (pointAssignments[i].size() > 0 && total > thresh) {
revPointIndices[i] = pointIndices.size();
pointIndices.push_back(i);
for (auto& assign : pointAssignments[i]) {
assign.first /= total;
}
}
}
// Write skeleton.txt
std::ofstream skel((outPath / "skeleton.txt").string());
skel << std::fixed << std::setprecision(18);
skel << jointIndices.size() << " " << pointIndices.size() << "\n";
skel << std::fixed << std::setprecision(18);
for (size_t i = 0; i < jointIndices.size(); ++i) {
skel << i << " ";
if (model.parent[jointIndices[i]] != -1)
skel << revJointIndices[model.parent[jointIndices[i]]] << " ";
else
skel << -1 << " ";
skel << SMPL_JOINT_NAMES[jointIndices[i]] << " "
<< model.initialJointPos(0, jointIndices[i]) << " "
<< model.initialJointPos(1, jointIndices[i]) << " "
<< model.initialJointPos(2, jointIndices[i]) << "\n";
}
for (size_t i = 0; i < pointIndices.size(); ++i) {
skel << pointAssignments[pointIndices[i]].size();
for (auto& assign : pointAssignments[pointIndices[i]]) {
skel << " " << revJointIndices[assign.second] << " "
<< assign.first;
}
skel << "\n";
}
skel.close();
// Write model.pcd
Eigen::Map<ark::CloudType> baseCloud(model.baseCloud.data(), 3,
model.numPoints());
writePCD((outPath / "model.pcd").string(), baseCloud, pointIndices);
// Write shape key.pcds
for (int i = 0; i < model.numShapeKeys(); ++i) {
std::stringstream ss_key_id;
ss_key_id << std::setw(3) << std::setfill('0') << std::to_string(i);
Eigen::Map<ark::CloudType> keyCloud(
model.keyClouds.data() + model.keyClouds.rows() * i, 3,
model.numPoints());
writePCD(
(shapekeysPath / ("shape" + ss_key_id.str() + ".pcd")).string(),
keyCloud, pointIndices);
}
// Prepare and write joint shape regressor
if (!model.useJointShapeRegressor) {
Eigen::Map<ark::CloudType> P(model.baseCloud.data(), 3,
model.baseCloud.rows() / 3);
model.jointShapeRegBase.resize(3 * model.numJoints());
Eigen::Map<ark::CloudType> PJ(model.jointShapeRegBase.data(), 3,
model.numJoints());
PJ = P * model.jointRegressor;
model.jointShapeReg.resize(model.numJoints() * 3, model.numShapeKeys());
for (int i = 0; i < model.numShapeKeys(); ++i) {
Eigen::Map<ark::CloudType> Si(
model.keyClouds.data() + model.keyClouds.rows() * i, 3,
model.keyClouds.rows() / 3);
Eigen::Map<ark::CloudType> PJ(model.jointShapeRegBase.data(), 3,
model.numJoints());
ark::CloudType SiJ = Si * model.jointRegressor;
Eigen::Map<Eigen::VectorXd> SiJvec(SiJ.data(),
model.numJoints() * 3);
model.jointShapeReg.col(i).noalias() = SiJvec;
}
}
std::ofstream jsr((outPath / "joint_shape_regressor.txt").string());
jsr << std::fixed << std::setprecision(18);
jsr << model.numShapeKeys() << "\n";
for (int i = root; i < model.numJoints(); ++i) {
if (keepJoints[i]) {
if (i > root) jsr << " ";
jsr << model.jointShapeRegBase(i * 3) << " "
<< model.jointShapeRegBase(i * 3 + 1) << " "
<< model.jointShapeRegBase(i * 3 + 2);
}
}
jsr << "\n";
for (int i = root; i < model.numJoints(); ++i) {
if (keepJoints[i]) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < model.numShapeKeys(); ++k) {
if (k) jsr << " ";
jsr << model.jointShapeReg(i * 3 + j, k);
}
jsr << "\n";
}
}
}
jsr.close();
if (model.hasMesh()) {
// Write mesh.txt
std::ofstream meshFile((outPath / "mesh.txt").string());
int count = 0;
for (int i = 0; i < model.mesh.cols(); ++i) {
count += (revPointIndices[model.mesh(0, i)] >= 0 &&
revPointIndices[model.mesh(1, i)] >= 0 &&
revPointIndices[model.mesh(2, i)] >= 0);
}
meshFile << count << "\n";
for (int i = 0; i < model.mesh.cols(); ++i) {
int i1 = revPointIndices[model.mesh(0, i)];
int i2 = revPointIndices[model.mesh(1, i)];
int i3 = revPointIndices[model.mesh(2, i)];
if (i1 >= 0 && i2 >= 0 && i3 >= 0) {
meshFile << i1 << " " << i2 << " " << i3 << "\n";
}
}
meshFile.close();
}
if (model.hasPosePrior()) {
// Write pose_prior.txt
std::ofstream ppFile((outPath / "pose_prior.txt").string());
ppFile << std::fixed << std::setprecision(18);
ppFile << model.posePrior.numComponents() << " "
<< jointIndices.size() * 3 - 3 << "\n";
// Weights
for (int i = 0; i < model.posePrior.numComponents(); ++i) {
if (i) ppFile << " ";
ppFile << model.posePrior.weight(i);
}
ppFile << "\n";
// Means
for (int i = 0; i < model.posePrior.numComponents(); ++i) {
for (int j : jointIndices) {
if (j == root) continue;
if (j != jointIndices[1]) ppFile << " ";
ppFile << model.posePrior.mean(i, (j - 1) * 3) << " "
<< model.posePrior.mean(i, (j - 1) * 3 + 1) << " "
<< model.posePrior.mean(i, (j - 1) * 3 + 2);
}
ppFile << "\n";
}
// Covs
for (int i = 0; i < model.posePrior.numComponents(); ++i) {
for (int j : jointIndices) {
if (j == root) continue;
for (int off = 0; off < 3; ++off) {
for (int k : jointIndices) {
if (k == root) continue;
if (k != jointIndices[1]) ppFile << " ";
ppFile << model.posePrior.cov[i]((j - 1) * 3 + off,
(k - 1) * 3)
<< " "
<< model.posePrior.cov[i]((j - 1) * 3 + off,
(k - 1) * 3 + 1)
<< " "
<< model.posePrior.cov[i]((j - 1) * 3 + off,
(k - 1) * 3 + 2);
}
ppFile << "\n";
}
}
}
ppFile.close();
}
return 0;
}