-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainCmd.cpp
More file actions
executable file
·225 lines (170 loc) · 6.45 KB
/
mainCmd.cpp
File metadata and controls
executable file
·225 lines (170 loc) · 6.45 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
///////////////////////////////////////////////////////////////////////////////
// Dem Bones - Skinning Decomposition Library //
// Copyright (c) 2019, Electronic Arts. All rights reserved. //
///////////////////////////////////////////////////////////////////////////////
#include <DemBones/DemBonesExt.h>
#include <DemBones/MatBlocks.h>
#include "NumpyReader.h"
#include "FbxReader.h"
#include "FbxWriter.h"
#include "LogMsg.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <pybind11/eigen.h>
using namespace std;
using namespace Eigen;
using namespace Dem;
class MyDemBones: public DemBonesExt<double, float> { // _Scalar = double, _AnimeshScalar = float
public:
double tolerance;
int patience;
double rmse_err;
MyDemBones(): tolerance(1e-3), patience(3) { nIters=100; }
void compute() {
prevErr=-1;
np=patience;
DemBonesExt<double, float>::compute();
}
void cbIterBegin() {
msg(1, " Iter #"<<iter<<": ");
}
bool cbIterEnd() {
rmse_err=rmse();
msg(1, "RMSE = "<<rmse_err << " | Error not degraded: " << (rmse_err<prevErr*(1+weightEps)) << " | Convergence rate below tolerance: " << ((prevErr-rmse_err)<tolerance*prevErr) << "\n");
if ((rmse_err<prevErr*(1+weightEps))&&((prevErr-rmse_err)<tolerance*prevErr)) {
np--;
if (np==0) {
msg(1, " Convergence is reached!\n");
return true;
}
} else np=patience;
prevErr=rmse_err;
return false;
}
void cbInitSplitBegin() {
msg(1, ">");
}
void cbInitSplitEnd() {
msg(1, nB);
}
void cbWeightsBegin() {
msg(1, "Updating weights");
}
void cbWeightsEnd() {
msg(1, " Done! ");
}
void cbTranformationsBegin() {
msg(1, "Updating trans");
}
void cbTransformationsEnd() {
msg(1, " Done! ");
}
bool cbTransformationsIterEnd() {
msg(1, ".");
return false;
}
bool cbWeightsIterEnd() {
msg(1, ".");
return false;
}
bool writeFBX(string outFile){
cout << "Outfile" << outFile << endl;
return writeFBXs(outFile, *this);
}
void load_data(MatrixX vert_data,vector< vector<int> > face_data){
clear(); // Remove all previous data
// # Check if output and input is provided
msg(1, "Reading Numpy array Rows:" << vert_data.rows() << "Vertices:" << vert_data.cols());
readNumpy(vert_data,face_data,*this);
return;
}
pybind11::tuple run_ssdr(int init_bones=30,string outFile=""){
msg(1, "Parameters:\n");
msg(1, " nInitIters = "<< nInitIters << "\n");
msg(1, " nIters = "<< nIters << "\n");
msg(1, " tolerance = "<< tolerance << "\n");
msg(1, " patience = "<< patience << "\n");
msg(1, " nTransIters = "<< nTransIters << "\n");
msg(1, " nWeightsIters = "<< nWeightsIters << "\n");
msg(1, " bindUpdate = "<< bindUpdate << "\n");
switch (bindUpdate) {
case 0: msg(1, " (no update)"); break;
case 1: msg(1, " (update joint positions)"); break;
case 2: msg(1, " (regroup joints under one root)"); break;
};
msg(1, " transAffine = "<< transAffine<< "\n");
msg(1, " transAffineNorm = "<< transAffineNorm<< "\n");
msg(1, " nnz = "<< nnz<< "\n");
msg(1, " weightsSmooth = "<< weightsSmooth<< "\n");
msg(1, " weightsSmoothStep = "<< weightsSmoothStep<< "\n");
if (nB==0) {
nB = init_bones;
msg(1, "Initializing bones:" << nB);
init();
msg(1, "\n");
}
msg(1, " nBones (target) = "<< nB <<"\n");
msg(1, "Computing Skinning Decomposition:\n");
compute();
if (!writeFBX(outFile) and outFile!="") return pybind11::make_tuple();
return pybind11::make_tuple(this->w,this->m,this->rmse_err);
}
private:
double prevErr;
int np;
};
PYBIND11_MODULE(pyssdr, handle){
handle.doc() = "Python Wrapper for SSDR\nDem Bones - (c) Electronic Arts 2019\n - This tool only handles clean input data, i.e. only one piece of geometry with one skinCluster and no excessive joint.\n\
- To hard-lock the transformations of bones: in the input fbx files, create bool attributes for joint nodes (bones) with name \"demLock\" and set the value to \"true\".\n\
- To soft-lock skinning weights of vertices: in the input fbx files, paint per-vertex colors in gray-scale. The closer the color to white, the more skinning weights of the vertex are preserved.", '=', "1.2.0";
pybind11::class_<MyDemBones>(
handle,"MyDemBones"
)
.def(pybind11::init<>())
.def("load_data",&MyDemBones::load_data)
.def("run_ssdr",&MyDemBones::run_ssdr)
// Hyperparmaters
.def_readwrite("weightsSmoothStep",&MyDemBones::weightsSmoothStep)
.def_readwrite("weightsSmooth",&MyDemBones::weightsSmooth)
.def_readwrite("nnz",&MyDemBones::nnz)
.def_readwrite("nWeightsIters",&MyDemBones::nWeightsIters)
.def_readwrite("transAffineNorm",&MyDemBones::transAffineNorm)
.def_readwrite("transAffine",&MyDemBones::transAffine)
.def_readwrite("bindUpdate",&MyDemBones::bindUpdate)
.def_readwrite("nTransIters",&MyDemBones::nTransIters)
.def_readwrite("patience",&MyDemBones::patience)
.def_readwrite("tolerance",&MyDemBones::tolerance)
.def_readwrite("nIters",&MyDemBones::nIters)
.def_readwrite("nInitIters",&MyDemBones::nInitIters)
.def_readwrite("nB",&MyDemBones::nB)
.def_readwrite("nV",&MyDemBones::nV)
.def_readwrite("nF",&MyDemBones::nF)
// Data variables
.def_readwrite("w",&MyDemBones::w)
.def_readwrite("m",&MyDemBones::m)
.def_readwrite("keep_bones",&MyDemBones::keep_bones)
.def_readwrite("mTm",&MyDemBones::mTm)
.def_readwrite("label",&MyDemBones::label)
.def_readwrite("lockW",&MyDemBones::lockW)
.def_readwrite("lockM",&MyDemBones::lockM)
// Commands
.def("init",&MyDemBones::init)
.def("computeTransFromLabel",&MyDemBones::computeTransFromLabel)
.def("computeLabel",&MyDemBones::computeLabel)
.def("pruneBones",&MyDemBones::pruneBones)
.def("labelToWeights",&MyDemBones::labelToWeights)
.def("compute",&MyDemBones::compute)
.def("rmse",&MyDemBones::rmse)
.def("clear",&MyDemBones::clear)
.def("vertex_rmse",&MyDemBones::vertex_rmse)
.def("vertex_max_rmse",&MyDemBones::vertex_rmse)
.def("rmse_from_cluster",&MyDemBones::rmse_from_cluster)
.def("compute_reconstruction",&MyDemBones::compute_reconstruction)
.def("computeWeights",&MyDemBones::computeWeights)
.def("computeTranformations",&MyDemBones::computeTranformations)
.def("compute_errorVtxBoneALL",&MyDemBones::compute_errorVtxBoneALL)
.def("errorVtxBone",&MyDemBones::errorVtxBone)
.def("cbIterEnd",&MyDemBones::cbIterEnd)
.def("writeFBX",&MyDemBones::writeFBX);
}