-
Notifications
You must be signed in to change notification settings - Fork 5
/
PrescribeEdgeJumps.h
415 lines (305 loc) · 16.6 KB
/
PrescribeEdgeJumps.h
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
413
414
415
//
// PrescribeEdgeJumps.h
// testigl
//
// Created by Amir Vaxman on 22/12/14.
// Copyright (c) 2014 Amir Vaxman. All rights reserved.
//
#ifndef MoebiusCode_PrescribeEdgeJumps_h
#define MoebiusCode_PrescribeEdgeJumps_h
#include <Eigen/Core>
#include <hedra/quaternionic_operations.h>
#include <hedra/quaternionic_derivatives.h>
class PrescribeEdgeJumps3D{
public:
Eigen::MatrixXi D, F;
Eigen::MatrixXd OrigVq;
Eigen::MatrixXi FaceCornerIndices; //rows of f1, f2, zi, zj (oriented)
Eigen::VectorXd PresJumps;
Eigen::VectorXd InitSolution;
Eigen::VectorXd Firstcd;
double PresFactor;
double CloseFactor;
double ConstTolerance;
double SolutionSize;
Eigen::RowVector4d UnitQuat;
bool isExactMC;
//intermediate variables
Eigen::VectorXd PresVec;
Eigen::VectorXd CloseVec;
Eigen::VectorXd CompVec;
Eigen::VectorXd ImagVec;
Eigen::VectorXd FirstcdVec;
Eigen::VectorXd MCVec;
Eigen::VectorXd TotalVec;
Eigen::VectorXd ConstVec;
int PresTriOffset, PresRowOffset;
int CloseTriOffset, CloseRowOffset;
int CompTriOffset, CompRowOffset;
int ImagTriOffset, ImagRowOffset;
int FirstcdTriOffset, FirstcdRowOffset;
int MCTriOffset, MCRowOffset;
Eigen::VectorXi GradRows, GradCols;
Eigen::VectorXd GradValues;
void Initialize(const Eigen::MatrixXd& inOrigVq,
const Eigen::MatrixXi& inD,
const Eigen::MatrixXi& inF,
Eigen::MatrixXi& inFaceCornerIndices,
const bool& inisExactMC){
using namespace Eigen;
using namespace std;
F=inF; D=inD;
FaceCornerIndices=inFaceCornerIndices;
OrigVq=inOrigVq;
isExactMC=inisExactMC;
SolutionSize=2*4*F.rows();
UnitQuat<<1.0,0.0,0.0,0.0;
PresVec.resize(4*2*FaceCornerIndices.rows());
CloseVec.resize(SolutionSize);
CompVec.resize(4*FaceCornerIndices.rows());
ImagVec.resize(F.rows());
FirstcdVec.resize(8);
Firstcd.resize(8);
Firstcd<<0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0;
if (isExactMC)
MCVec.resize(2*FaceCornerIndices.rows());
else
MCVec.resize(0);
TotalVec.resize(PresVec.size()+CloseVec.size()+CompVec.size()+ImagVec.size()+FirstcdVec.size()+MCVec.size());
ConstVec.resize(CompVec.size()+ImagVec.size()+FirstcdVec.size()+MCVec.size());
ConstTolerance=10e-7;
//***********************Creating Gradient Pattern**************************************/
if (!isExactMC){
GradRows.resize(128*FaceCornerIndices.rows()+SolutionSize+64*FaceCornerIndices.rows()+8*F.rows()+8);
GradCols.resize(GradRows.size());
GradValues.resize(GradRows.size());
} //not currently doing exact MC error reproduction
/**************************Prescription Energy*******************************/
PresTriOffset=0;
PresRowOffset=0;
Vector4i c1TriPoses; c1TriPoses<<0,16,32,48;
Vector4i d1TriPoses; d1TriPoses<<4,20,36,52;
Vector4i c2TriPoses; c2TriPoses<<8,24,40,56;
Vector4i d2TriPoses; d2TriPoses<<12,28,44,60;
int PresTriCounter=PresTriOffset;
for (int i=0;i<FaceCornerIndices.rows();i++){
int Colc1=4*(2*FaceCornerIndices(i,0));
int Cold1=4*(2*FaceCornerIndices(i,0)+1);
int Colc2=4*(2*FaceCornerIndices(i,1));
int Cold2=4*(2*FaceCornerIndices(i,1)+1);
int CurrRowOffset=PresRowOffset+4*2*i;
hedra::quatDerivativeIndices(GradRows, GradCols, PresTriCounter, c1TriPoses, CurrRowOffset, Colc1);
hedra::quatDerivativeIndices(GradRows, GradCols, PresTriCounter, d1TriPoses, CurrRowOffset, Cold1);
hedra::quatDerivativeIndices(GradRows, GradCols, PresTriCounter, c2TriPoses, CurrRowOffset, Colc2);
hedra::quatDerivativeIndices(GradRows, GradCols, PresTriCounter, d2TriPoses, CurrRowOffset, Cold2);
CurrRowOffset=PresRowOffset+4*(2*i+1);
hedra::quatDerivativeIndices(GradRows, GradCols, PresTriCounter+64, c1TriPoses, CurrRowOffset, Colc1);
hedra::quatDerivativeIndices(GradRows, GradCols, PresTriCounter+64, d1TriPoses, CurrRowOffset, Cold1);
hedra::quatDerivativeIndices(GradRows, GradCols, PresTriCounter+64, c2TriPoses, CurrRowOffset, Colc2);
hedra::quatDerivativeIndices(GradRows, GradCols, PresTriCounter+64, d2TriPoses, CurrRowOffset, Cold2);
PresTriCounter+=128;
}
/**********************Closeness Energy***************************************/
CloseTriOffset=PresTriOffset+128*FaceCornerIndices.rows();
CloseRowOffset=PresRowOffset+4*2*FaceCornerIndices.rows();
for (int i=0;i<SolutionSize;i++){
GradRows(CloseTriOffset+i)=CloseRowOffset+i;
GradCols(CloseTriOffset+i)=i;
GradValues(CloseTriOffset+i)=CloseFactor;
}
/************************Compatibility Constraint*****************************/
CompTriOffset=CloseTriOffset+SolutionSize;
CompRowOffset=CloseRowOffset+SolutionSize;
int CompTriCounter=CompTriOffset;
for (int i=0;i<FaceCornerIndices.rows();i++){
int Colc1=4*(2*FaceCornerIndices(i,0));
int Cold1=4*(2*FaceCornerIndices(i,0)+1);
int Colc2=4*(2*FaceCornerIndices(i,1));
int Cold2=4*(2*FaceCornerIndices(i,1)+1);
int CurrRowOffset=CompRowOffset+4*i;
hedra::quatDerivativeIndices(GradRows, GradCols, CompTriCounter, c1TriPoses, CurrRowOffset, Colc1);
hedra::quatDerivativeIndices(GradRows, GradCols, CompTriCounter, d1TriPoses, CurrRowOffset, Cold1);
hedra::quatDerivativeIndices(GradRows, GradCols, CompTriCounter, c2TriPoses, CurrRowOffset, Colc2);
hedra::quatDerivativeIndices(GradRows, GradCols, CompTriCounter, d2TriPoses, CurrRowOffset, Cold2);
CompTriCounter+=64;
}
/************************Imaginarity Constraint******************************/
ImagTriOffset=CompTriOffset+64*FaceCornerIndices.rows();
ImagRowOffset=CompRowOffset+4*FaceCornerIndices.rows();
for (int i=0;i<F.rows();i++){
for (int j=0;j<4;j++){
GradRows(ImagTriOffset+4*2*i+j)=ImagRowOffset+i;
GradCols(ImagTriOffset+4*2*i+j)=4*(2*i)+j;
GradRows(ImagTriOffset+4*(2*i+1)+j)=ImagRowOffset+i;
GradCols(ImagTriOffset+4*(2*i+1)+j)=4*(2*i+1)+j;
}
}
/***************************First cd constraints*****************************/
FirstcdTriOffset=ImagTriOffset+8*F.rows();
FirstcdRowOffset=ImagRowOffset+F.rows();
int FirstcdTriCounter=FirstcdTriOffset;
for (int j=0;j<8;j++){
GradRows(FirstcdTriOffset+j)=FirstcdRowOffset+j;
GradCols(FirstcdTriOffset+j)=j;
GradValues(FirstcdTriOffset+j)=1.0;
}
if (isExactMC){
//not currently doing
}
/*MatrixXi GradIndexMat(GradRows.size(),3);
for (int i=0;i<GradRows.size();i++)
GradIndexMat(i)=i;
GradIndexMat.col(1)=GradRows;
GradIndexMat.col(2)=GradCols;
cout<<"GradIndices: "<<GradIndexMat<<endl;
cout<<"PresRowOffset: "<<PresRowOffset<<endl;
cout<<"CloseRowOffset: "<<CloseRowOffset<<endl;
cout<<"CompRowOffset: "<<CompRowOffset<<endl;
cout<<"ImagRowOffset: "<<ImagRowOffset<<endl;
cout<<"FirstcdRowOffset: "<<FirstcdRowOffset<<endl;*/
}
void UpdateEnergy(const Eigen::VectorXd& CurrSolution)
{
using namespace Eigen;
using namespace std;
for (int i=0;i<FaceCornerIndices.rows();i++){
RowVector4d c1=CurrSolution.segment(4*2*FaceCornerIndices(i,0),4).transpose();
RowVector4d d1=CurrSolution.segment(4*(2*FaceCornerIndices(i,0)+1),4).transpose();
RowVector4d c2=CurrSolution.segment(4*2*FaceCornerIndices(i,1),4).transpose();
RowVector4d d2=CurrSolution.segment(4*(2*FaceCornerIndices(i,1)+1),4).transpose();
RowVector4d zi=OrigVq.row(FaceCornerIndices(i,2));
RowVector4d zj=OrigVq.row(FaceCornerIndices(i,3));
RowVector4d zij=zj-zi;
RowVector4d G1i=QMult(c1,zi)+d1;
RowVector4d G2i=QMult(c2,zi)+d2;
RowVector4d G1j=QMult(c1,zj)+d1;
RowVector4d G2j=QMult(c2,zj)+d2;
RowVector4d g=PresJumps.segment(4*i,4);
PresVec.segment(4*(2*i),4)=PresFactor*((QMult(G2i,g)-G1i).transpose());
PresVec.segment(4*(2*i+1),4)=PresFactor*((QMult(QMult(G1j, zij), QMult(QConj(g), QInv(zij)))-G2j).transpose());
}
CloseVec<<CloseFactor*(CurrSolution-InitSolution);
UpdateConstraints(CurrSolution);
TotalVec<<PresVec, CloseVec, ConstVec;
}
void UpdateGradient(const Eigen::VectorXd& CurrSolution){
using namespace Eigen;
using namespace std;
PresTriOffset=0;
PresRowOffset=0;
Vector4i c1TriPoses; c1TriPoses<<0,16,32,48;
Vector4i d1TriPoses; d1TriPoses<<4,20,36,52;
Vector4i c2TriPoses; c2TriPoses<<8,24,40,56;
Vector4i d2TriPoses; d2TriPoses<<12,28,44,60;
/**************************Prescription Gradient********************************************/
int PresTriCounter=PresTriOffset;
for (int i=0;i<FaceCornerIndices.rows();i++){
RowVector4d zi=OrigVq.row(FaceCornerIndices(i,2));
RowVector4d zj=OrigVq.row(FaceCornerIndices(i,3));
RowVector4d zij=zj-zi;
RowVector4d izij=QInv(zij);
RowVector4d g=PresJumps.segment(4*i,4);
//derivative of G2i*g-G1i=(c2*zi+d2)*g-c1*zi-d1
hedra::quatDerivativeValues(GradValues, PresTriCounter, c1TriPoses, PresFactor*UnitQuat, -zi, false, false);
hedra::quatDerivativeValues(GradValues, PresTriCounter, d1TriPoses, PresFactor*UnitQuat, -UnitQuat, false, false);
hedra::quatDerivativeValues(GradValues, PresTriCounter, c2TriPoses, PresFactor*UnitQuat, QMult(zi,g), false, false);
hedra::quatDerivativeValues(GradValues, PresTriCounter, d2TriPoses, PresFactor*UnitQuat, g, false, false);
//derivatives of G1j*e*conj(g)*inv(e)-G2j=(c1*zj+d1)*zij*conj(g)*inv(zij)-c2*zj-d2
hedra::quatDerivativeValues(GradValues, PresTriCounter+64, c1TriPoses, PresFactor*UnitQuat, QMult(QMult(zj, zij), QMult(QConj(g), izij)), false, false);
hedra::quatDerivativeValues(GradValues, PresTriCounter+64, d1TriPoses, PresFactor*UnitQuat, QMult(zij, QMult(QConj(g), izij)), false, false);
hedra::quatDerivativeValues(GradValues, PresTriCounter+64, c2TriPoses, PresFactor*UnitQuat, -zj, false, false);
hedra::quatDerivativeValues(GradValues, PresTriCounter+64, d2TriPoses, PresFactor*UnitQuat, -UnitQuat, false, false);
PresTriCounter+=128;
}
//closeness is constant
/***************************Compatibility Gradient*****************************************/
int CompTriCounter=CompTriOffset;
for (int i=0;i<FaceCornerIndices.rows();i++){
RowVector4d c1=CurrSolution.segment(4*2*FaceCornerIndices(i,0),4).transpose();
RowVector4d d1=CurrSolution.segment(4*(2*FaceCornerIndices(i,0)+1),4).transpose();
RowVector4d c2=CurrSolution.segment(4*2*FaceCornerIndices(i,1),4).transpose();
RowVector4d d2=CurrSolution.segment(4*(2*FaceCornerIndices(i,1)+1),4).transpose();
RowVector4d zi=OrigVq.row(FaceCornerIndices(i,2));
RowVector4d zj=OrigVq.row(FaceCornerIndices(i,3));
RowVector4d zij=zj-zi;
RowVector4d izij=QInv(zij);
RowVector4d G1i=QMult(c1,zi)+d1;
RowVector4d G2i=QMult(c2,zi)+d2;
RowVector4d G1j=QMult(c1,zj)+d1;
RowVector4d G2j=QMult(c2,zj)+d2;
//derivatives of Gi1*inv(zij)*conj(Gj1)-Gi2*inv(zij)*conj(Gj2)=
//(c1*zi+d1)*inv(zij)*(conj(zj)*conj(c1)+conj(d1))-(c2*zi+d2)*inv(zij)*(conj(zj)*conj(c2)+conj(d2))
//c1 derivatives
hedra::quatDerivativeValues(GradValues, CompTriCounter, c1TriPoses, UnitQuat, QMult(zi,QMult(izij,QConj(G1j))), false, false);
hedra::quatDerivativeValues(GradValues, CompTriCounter, c1TriPoses, QMult(G1i,QMult(izij,QConj(zj))), UnitQuat, true, true);
//d1 derivatives
hedra::quatDerivativeValues(GradValues, CompTriCounter, d1TriPoses, UnitQuat, QMult(izij,QConj(G1j)), false, false);
hedra::quatDerivativeValues(GradValues, CompTriCounter, d1TriPoses, QMult(G1i,izij), UnitQuat, true, true);
//c2 derivatives
hedra::quatDerivativeValues(GradValues, CompTriCounter, c2TriPoses, -UnitQuat, QMult(zi,QMult(izij,QConj(G2j))), false, false);
hedra::quatDerivativeValues(GradValues, CompTriCounter, c2TriPoses, QMult(G2i,QMult(izij,QConj(zj))), -UnitQuat, true, true);
//d2 derivatives
hedra::quatDerivativeValues(GradValues, CompTriCounter, d2TriPoses, -UnitQuat, QMult(izij,QConj(G2j)), false, false);
hedra::quatDerivativeValues(GradValues, CompTriCounter, d2TriPoses, QMult(G2i,izij), -UnitQuat, true, true);
CompTriCounter+=64;
}
/*****************************Imaginarity Gradient***********************/
for (int i=0;i<F.rows();i++){
//derivative of re(c*conj(d))=0=rc*rd+<Vc,Vd>
RowVector4d c=CurrSolution.segment(4*2*i,4);
RowVector4d d=CurrSolution.segment(4*(2*i+1),4);
for (int j=0;j<4;j++){
GradValues(ImagTriOffset+4*2*i+j)=d(j);
GradValues(ImagTriOffset+4*(2*i+1)+j)=c(j);
}
}
//firstcd is constant
}
void Reformulate(int CurrIter, int MaxIterations, const Eigen::VectorXd& CurrSolution, double PrevError)
{
using namespace Eigen;
using namespace std;
double rate=ConstVec.lpNorm<Infinity>()/PrevError;
double ReduceRate=min(rate/2.0,1.0);
InitSolution=CurrSolution;
PresFactor*=0.75-0.25*(1.0-ReduceRate);
}
void UpdateConstraints(const Eigen::VectorXd& CurrSolution)
{
using namespace Eigen;
using namespace std;
for (int i=0;i<FaceCornerIndices.rows();i++){
RowVector4d c1=CurrSolution.segment(4*2*FaceCornerIndices(i,0),4).transpose();
RowVector4d d1=CurrSolution.segment(4*(2*FaceCornerIndices(i,0)+1),4).transpose();
RowVector4d c2=CurrSolution.segment(4*2*FaceCornerIndices(i,1),4).transpose();
RowVector4d d2=CurrSolution.segment(4*(2*FaceCornerIndices(i,1)+1),4).transpose();
RowVector4d zi=OrigVq.row(FaceCornerIndices(i,2));
RowVector4d zj=OrigVq.row(FaceCornerIndices(i,3));
RowVector4d zij=zj-zi;
RowVector4d G1i=QMult(c1,zi)+d1;
RowVector4d G2i=QMult(c2,zi)+d2;
RowVector4d G1j=QMult(c1,zj)+d1;
RowVector4d G2j=QMult(c2,zj)+d2;
CompVec.segment(4*i,4)=QMult(QMult(G1i, QInv(zij)), QConj(G1j))-QMult(QMult(G2i, QInv(zij)), QConj(G2j));
}
for (int i=0;i<F.rows();i++){
RowVector4d c=CurrSolution.segment(4*2*i,4).transpose();
RowVector4d d=CurrSolution.segment(4*(2*i+1),4).transpose();
ImagVec(i)=QMult(c,QConj(d))(0);
}
FirstcdVec<<CurrSolution.segment(0,8)-Firstcd;
/*if (isExactMC){
MCVec(2*i)=G2i.squaredNorm()*g.squaredNorm()-G1i.squaredNorm();
MCVec(2*i+1)=G1j.squaredNorm()*g.squaredNorm()-G2j.squaredNorm();
}*/
if (!isExactMC)
ConstVec<<CompVec, ImagVec, FirstcdVec;
else
ConstVec<<CompVec, ImagVec, FirstcdVec, MCVec;
}
bool isTerminate()
{
return(ConstVec.lpNorm<Eigen::Infinity>() < ConstTolerance);
}
};
#endif