-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredFD.cpp
412 lines (340 loc) · 12 KB
/
redFD.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <igl/writeOBJ.h>
#include <igl/barycenter.h>
#include <igl/readOFF.h>
#include <igl/readMESH.h>
#include <igl/readOBJ.h>
#include <igl/slice.h>
#include <igl/setdiff.h>
#include <json.hpp>
#include "mesh.h"
#include "redArap.h"
#include "elastic.h"
// #include "solver.h"
using json = nlohmann::json;
using namespace Eigen;
using namespace std;
json j_input;
std::vector<int> getMaxVerts_Axis_Tolerance(MatrixXd& mV, int dim, double tolerance=1e-5){
auto maxX = mV.col(dim).maxCoeff();
std::vector<int> maxV;
for(unsigned int ii=0; ii<mV.rows(); ++ii) {
if(fabs(mV(ii,dim) - maxX) < tolerance) {
maxV.push_back(ii);
}
}
return maxV;
}
std::vector<int> getMinVerts_Axis_Tolerance(MatrixXd& mV, int dim, double tolerance=1e-5){
auto maxX = mV.col(dim).minCoeff();
std::vector<int> maxV;
for(unsigned int ii=0; ii<mV.rows(); ++ii) {
if(fabs(mV(ii,dim) - maxX) < tolerance) {
maxV.push_back(ii);
}
}
return maxV;
}
void getMaxTets_Axis_Tolerance(std::vector<int>& ibones, MatrixXd& mV, MatrixXi& mT, double dim, double tolerance = 1-5){
auto maxX = mV.col(dim).maxCoeff();
for(int i=0; i< mT.rows(); i++){
Vector3d centre = (mV.row(mT.row(i)[0])+ mV.row(mT.row(i)[1]) + mV.row(mT.row(i)[2])+ mV.row(mT.row(i)[3]))/4.0;
if (fabs(centre[dim] - maxX)< tolerance){
ibones.push_back(i);
}
}
}
VectorXd get_w(VectorXd& r0, VectorXd& r){
VectorXd w = VectorXd::Zero(r0.size()/3);
for(int i=0; i<r0.size()/9; i++){
Matrix3d R0, R;
R0<<r0[9*i+0],r0[9*i+1],r0[9*i+2],
r0[9*i+3],r0[9*i+4],r0[9*i+5],
r0[9*i+6],r0[9*i+7],r0[9*i+8];
R<<r[9*i+0],r[9*i+1],r[9*i+2],
r[9*i+3],r[9*i+4],r[9*i+5],
r[9*i+6],r[9*i+7],r[9*i+8];
Matrix3d exp_brac_w = R0.transpose()*R;
Matrix3d brac_w = exp_brac_w.log();
w[3*i+0] = brac_w(2,1);
w[3*i+1] = brac_w(0,2);
w[3*i+2] = brac_w(1,0);
}
return w;
}
//CHECK E,x-------------
VectorXd Ex(Mesh& mesh, Reduced_Arap& arap, double E0, double eps){
VectorXd z = mesh.red_x();
VectorXd fake = VectorXd::Zero(z.size());
#pragma omp parallel for
for(int i=0; i<fake.size(); i++){
z[i] += 0.5*eps;
double Eleft = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
z[i] -= 0.5*eps;
z[i] -= 0.5*eps;
double Eright = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
z[i] += 0.5*eps;
fake[i] = (Eleft - Eright)/eps;
}
return fake;
}
//-----------------------
//CHECK E,r-------------
VectorXd Er(Mesh& mesh, Reduced_Arap& arap, double E0, double eps){
VectorXd z = mesh.red_x();
VectorXd fake = VectorXd::Zero(mesh.red_w().size());
for(int i=0; i<fake.size(); i++){
mesh.red_w()[i] += 0.5*eps;
// mesh.setGlobalF(true, false, false);
double Eleft = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_w()[i] -= 0.5*eps;
mesh.red_w()[i] -= 0.5*eps;
// mesh.setGlobalF(true, false, false);
double Eright = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_w()[i] += 0.5*eps;
fake[i] = (Eleft - Eright)/(eps);
}
// mesh.setGlobalF(true, false, false);
return fake;
}
//-----------------------
//CHECK E,s-------------
VectorXd Es(Mesh& mesh, Reduced_Arap& arap, double E0, double eps){
VectorXd fake = VectorXd::Zero(mesh.red_s().size());
for(int i=0; i<fake.size(); i++){
mesh.red_s()[i] += 0.5*eps;
double Eleft = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_s()[i] -= 0.5*eps;
mesh.red_s()[i] -= 0.5*eps;
double Eright = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_s()[i] += 0.5*eps;
fake[i] = (Eleft - Eright)/eps;
}
return fake;
}
//-----------------------
//CHECK Exx--------------
MatrixXd Exx(Mesh& mesh, Reduced_Arap& arap, double E0, double eps){
MatrixXd fake = MatrixXd::Zero(mesh.red_x().size(), mesh.red_x().size());
VectorXd z = mesh.red_x();
for(int i=0; i<fake.rows(); i++){
for(int j=0; j<fake.cols(); j++){
z[i] += eps;
z[j] += eps;
double Eij = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
z[i] -= eps;
z[j] -= eps;
z[i] += eps;
double Ei = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
z[i] -= eps;
z[j] += eps;
double Ej = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
z[j] -= eps;
fake(i,j) = ((Eij - Ei - Ej + E0)/(eps*eps));
}
}
return fake;
}
//-----------------------
//CHECK Exr/Erx-------------
MatrixXd Exr(Mesh& mesh, Reduced_Arap& arap, double E0, double eps){
MatrixXd fake = MatrixXd::Zero(mesh.red_x().size(), mesh.red_w().size());
VectorXd z = mesh.red_x();
for(int i=0; i<fake.rows(); i++){
for(int j=0; j<fake.cols(); j++){
mesh.red_w()[j] += eps;
z[i] += eps;
double Eij = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_w()[j] -= eps;
z[i] -= eps;
mesh.red_w()[j] += eps;
double Ei = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_w()[j] -= eps;
z[i] += eps;
double Ej = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
z[i] -= eps;
// cout<<Eij<<", "<<Ei<<", "<<Ej<<", "<<E0<<endl;
fake(i,j) = ((Eij - Ei - Ej + E0)/(eps*eps));
}
// exit(0);
}
return fake;
}
//-----------------------
//CHECK Exs-------------
MatrixXd Exs(Mesh& mesh, Reduced_Arap& arap, double E0, double eps){
MatrixXd fake = MatrixXd::Zero(mesh.red_x().size(), mesh.red_s().size());
VectorXd z = mesh.red_x();
for(int i=0; i<fake.rows(); i++){
for(int j=0; j<fake.cols(); j++){
mesh.red_s()[j] += eps;
z[i] += eps;
// mesh.setGlobalF(false, true, false);
double Eij = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_s()[j] -= eps;
z[i] -= eps;
mesh.red_s()[j] += eps;
// mesh.setGlobalF(false, true, false);
double Ei = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_s()[j] -= eps;
z[i] += eps;
// mesh.setGlobalF(false, true, false);
double Ej = arap.Energy(mesh, z, mesh.red_w(), mesh.red_r(), mesh.red_s());
z[i] -= eps;
fake(i,j) = ((Eij - Ei - Ej + E0)/(eps*eps));
}
}
// mesh.setGlobalF(false, true, false);
return fake;
}
//-----------------------
//CHECK Err--------------
MatrixXd Err(Mesh& mesh, Reduced_Arap& arap, double E0, double eps){
MatrixXd fake = MatrixXd::Zero(mesh.red_w().size(), mesh.red_w().size());
for(int i=0; i<fake.rows(); i++){
for(int j=0; j<fake.cols(); j++){
mesh.red_w()[j] += eps;
mesh.red_w()[i] += eps;
// mesh.setGlobalF(true, false, false);
double Eij = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_w()[j] -= eps;
mesh.red_w()[i] -= eps;
mesh.red_w()[j] += eps;
// mesh.setGlobalF(true, false, false);
double Ei = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_w()[j] -= eps;
mesh.red_w()[i] += eps;
// mesh.setGlobalF(true, false, false);
double Ej = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_w()[i] -= eps;
fake(i,j) = ((Eij - Ei - Ej + E0)/(eps*eps));
}
}
// mesh.setGlobalF(true, false, false);
return fake;
}
//-----------------------
//CHECK Ers--------------
MatrixXd Ers(Mesh& mesh, Reduced_Arap& arap, double E0, double eps){
MatrixXd fake = MatrixXd::Zero(mesh.red_w().size(), mesh.red_s().size());
for(int i=0; i<fake.rows(); i++){
for(int j=0; j<fake.cols(); j++){
mesh.red_w()[i] += eps;
mesh.red_s()[j] += eps;
double Eij = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_s()[j] -= eps;
mesh.red_w()[i] -= eps;
mesh.red_w()[i] += eps;
double Ei = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_w()[i] -= eps;
mesh.red_s()[j] += eps;
double Ej = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
mesh.red_s()[j] -= eps;
fake(i,j) = ((Eij - Ei - Ej + E0)/(eps*eps));
}
}
// for(int i=0; i<fake.cols(); i++){
// mesh.red_s()[i] += 0.5*eps;
// VectorXd Eleft = arap.constTimeEr(mesh);
// mesh.red_s()[i] -= 0.5*eps;
// mesh.red_s()[i] -= 0.5*eps;
// VectorXd Eright = arap.constTimeEr(mesh);
// mesh.red_s()[i] += 0.5*eps;
// fake.col(i) = (Eleft - Eright)/eps;
// }
return fake;
}
//-----------------------
int checkRedARAP(Mesh& mesh, Reduced_Arap& arap){
double eps = j_input["fd_eps"];
double E0 = arap.Energy(mesh, mesh.red_x(), mesh.red_w(), mesh.red_r(), mesh.red_s());
cout<<"E0: "<<E0<<endl;
arap.Gradients(mesh);
cout<<"Ex"<<endl;
VectorXd fakeEx = Ex(mesh, arap, E0, eps);
cout<<(arap.Ex().transpose()-fakeEx.transpose()).norm()<<endl<<endl;
cout<<"Er"<<endl;
VectorXd fakeEr = Er(mesh, arap, E0, eps);
cout<<fakeEr.transpose()<<endl;
cout<<arap.Er().transpose()<<endl;
cout<<(arap.Er().transpose()-fakeEr.transpose()).norm()<<endl<<endl;
cout<<"Es"<<endl;
VectorXd fakeEs = Es(mesh, arap,E0, eps);
cout<<(arap.Es().transpose() - fakeEs.transpose()).norm()<<endl<<endl;
arap.Hessians(mesh);
cout<<"Err"<<endl;
MatrixXd fakeErr = Err(mesh, arap, E0, eps);
cout<<arap.Err()<<endl;
cout<<(fakeErr-arap.Err()).norm()<<endl;
cout<<endl;
MatrixXd fakeExx = Exx(mesh, arap, E0, eps);
cout<<"Exx"<<endl;
cout<<(fakeExx-MatrixXd(arap.Exx())).norm()<<endl<<endl;
cout<<endl<<endl;
MatrixXd fakeExr = Exr(mesh, arap, E0, eps);
cout<<"Exr"<<endl;
cout<<arap.Exr()<<endl;
cout<<(fakeExr-arap.Exr()).norm()<<endl<<endl;
cout<<endl<<endl;
cout<<"Exs"<<endl;
MatrixXd fakeExs = Exs(mesh, arap, E0, eps);
cout<<arap.Exs()<<endl;
cout<<(fakeExs-arap.Exs()).norm()<<endl<<endl;
cout<<endl;
cout<<"Ers"<<endl;
MatrixXd fakeErs = Ers(mesh, arap, E0, eps);
cout<<arap.Ers()<<endl<<endl;
cout<<fakeErs<<endl;
cout<<(fakeErs-arap.Ers()).norm()<<endl<<endl;
cout<<endl;
arap.Jacobians(mesh);
}
int main(int argc, char *argv[]){
std::cout<<"-----Configs-------"<<std::endl;
json j_config_parameters;
std::ifstream i("../input/input.json");
i >> j_input;
MatrixXd V;
MatrixXi T;
MatrixXi F;
igl::readMESH(j_input["mesh_file"], V, T, F);
cout<<"V size: "<<V.rows()<<endl;
cout<<"T size: "<<T.rows()<<endl;
cout<<"F size: "<<F.rows()<<endl;
std::vector<int> bone1={};
getMaxTets_Axis_Tolerance(bone1, V, T, 1, 3);
VectorXi bone1vec = VectorXi::Map(bone1.data(), bone1.size());
VectorXi all(T.rows());
MatrixXd Uvec(all.size(), 3);
for(int i=0; i<T.rows(); i++){
all[i] = i;
Uvec.row(i) = Vector3d::UnitY();
}
VectorXi muscle1;
VectorXi shit;
igl::setdiff(all, bone1vec, muscle1, shit);
std::vector<VectorXi> bones = {bone1vec};
std::vector<VectorXi> muscles = {muscle1};
std::vector<int> fix_bones = {0};
std::vector<int> mov = {};
std::cout<<"-----Mesh-------"<<std::endl;
Mesh* mesh = new Mesh(T, V, fix_bones, mov, bones, muscles, Uvec, j_input);
std::cout<<"-----ARAP-----"<<std::endl;
Reduced_Arap* redarap = new Reduced_Arap(*mesh);
std::cout<<"-----Neo-------"<<std::endl;
Elastic* neo = new Elastic(*mesh);
cout<<"Done with init, setting values for s, r, x"<<endl;
std::vector<double> s = {1.22027, 0.495075 , 1.1781 , 0.00974582 ,
0.000871698 , 0.00620995 , 1.2249 , 0.467115 , 1.21814 ,-0.00114247 ,
0.0016028, -0.0318313 , 1.16293, 0.514761 , 1.16153, -0.00925684,
0.0640231, -0.00245551, 1.16368 , 0.549197, 1.16291, -0.0314069,
0.0661537, -0.0237411 , 1.21513 , 0.478347 , 1.22011 , 0.0469281,
-0.00796292, 0.0371727 , 1.17835 , 0.494537 , 1.22059, -0.012968,
-0.00408742 , 0.0147056};
cout<<s.size()<<", "<<mesh->red_s().size()<<endl;
for(int i=0; i<s.size(); i++){
mesh->red_s()[i] = s[i];
}
redarap->minimize(*mesh);
checkRedARAP(*mesh, *redarap);
// checkElastic(*mesh, *neo);
}