-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbw.cpp
More file actions
258 lines (243 loc) · 7.73 KB
/
bbw.cpp
File metadata and controls
258 lines (243 loc) · 7.73 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
// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2016 Alec Jacobson <alecjacobson@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "bbw.h"
#include "min_quad_with_fixed.h"
#include "harmonic.h"
#include "parallel_for.h"
#include <Eigen/Sparse>
#include <iostream>
#include <mutex>
#include <cstdio>
igl::BBWData::BBWData():
partition_unity(false),
W0(),
active_set_params(),
verbosity(0)
{
// We know that the Bilaplacian is positive semi-definite
active_set_params.Auu_pd = true;
}
void igl::BBWData::print()
{
using namespace std;
cout<<"partition_unity: "<<partition_unity<<endl;
cout<<"W0=["<<endl<<W0<<endl<<"];"<<endl;
}
template <
typename DerivedV,
typename DerivedEle,
typename Derivedb,
typename Derivedbc,
typename DerivedW>
IGL_INLINE bool igl::bbw(
const Eigen::PlainObjectBase<DerivedV> & V,
const Eigen::PlainObjectBase<DerivedEle> & Ele,
const Eigen::PlainObjectBase<Derivedb> & b,
const Eigen::PlainObjectBase<Derivedbc> & bc,
igl::BBWData & data,
Eigen::PlainObjectBase<DerivedW> & W
)
{
using namespace std;
using namespace Eigen;
assert(!data.partition_unity && "partition_unity not implemented yet");
// number of domain vertices
int n = V.rows();
// number of handles
int m = bc.cols();
// Build biharmonic operator
Eigen::SparseMatrix<typename DerivedV::Scalar> Q;
harmonic(V,Ele,2,Q);
W.derived().resize(n,m);
// No linear terms
VectorXd c = VectorXd::Zero(n);
// No linear constraints
SparseMatrix<typename DerivedW::Scalar> A(0,n),Aeq(0,n),Aieq(0,n);
VectorXd Beq(0,1),Bieq(0,1);
// Upper and lower box constraints (Constant bounds)
VectorXd ux = VectorXd::Ones(n);
VectorXd lx = VectorXd::Zero(n);
active_set_params eff_params = data.active_set_params;
if(data.verbosity >= 1)
{
cout<<"BBW: max_iter: "<<data.active_set_params.max_iter<<endl;
cout<<"BBW: eff_max_iter: "<<eff_params.max_iter<<endl;
}
if(data.verbosity >= 1)
{
cout<<"BBW: Computing initial weights for "<<m<<" handle"<<
(m!=1?"s":"")<<"."<<endl;
}
min_quad_with_fixed_data<typename DerivedW::Scalar > mqwf;
min_quad_with_fixed_precompute(Q,b,Aeq,true,mqwf);
min_quad_with_fixed_solve(mqwf,c,bc,Beq,W);
// decrement
eff_params.max_iter--;
bool error = false;
// Loop over handles
std::mutex critical;
const auto & optimize_weight = [&](const int i)
{
// Quicker exit for paralle_for
if(error)
{
return;
}
if(data.verbosity >= 1)
{
std::lock_guard<std::mutex> lock(critical);
cout<<"BBW: Computing weight for handle "<<i+1<<" out of "<<m<<
"."<<endl;
}
VectorXd bci = bc.col(i);
VectorXd Wi;
// use initial guess
Wi = W.col(i);
SolverStatus ret = active_set(
Q,c,b,bci,Aeq,Beq,Aieq,Bieq,lx,ux,eff_params,Wi);
switch(ret)
{
case SOLVER_STATUS_CONVERGED:
break;
case SOLVER_STATUS_MAX_ITER:
#ifdef IGL_BBW_DEBUG
cerr<<"active_set: max iter without convergence."<<endl;
#endif
break;
case SOLVER_STATUS_ERROR:
default:
#ifdef IGL_BBW_DEBUG
cerr<<"active_set error."<<endl;
#endif
error = true;
}
W.col(i) = Wi;
};
parallel_for(m,optimize_weight,2);
if(error)
{
return false;
}
#ifndef NDEBUG
const double min_rowsum = W.rowwise().sum().array().abs().minCoeff();
if(min_rowsum < 0.1)
{
cerr<<"bbw.cpp: Warning, minimum row sum is very low. Consider more "
"active set iterations or enforcing partition of unity."<<endl;
}
#endif
return true;
}
template <
typename DerivedV,
typename DerivedEle,
typename Derivedb,
typename Derivedbc,
typename DerivedW>
IGL_INLINE bool igl::bbw(
const Eigen::PlainObjectBase<DerivedV>& V,
const Eigen::PlainObjectBase<DerivedEle>& Ele,
const Eigen::PlainObjectBase<Derivedb>& b,
const Eigen::PlainObjectBase<Derivedbc>& bc,
double minValue,
double maxValue,
igl::BBWData& data,
Eigen::PlainObjectBase<DerivedW>& W
)
{
using namespace std;
using namespace Eigen;
assert(!data.partition_unity && "partition_unity not implemented yet");
// number of domain vertices
int n = V.rows();
// number of handles
int m = bc.cols();
// Build biharmonic operator
Eigen::SparseMatrix<typename DerivedV::Scalar> Q;
harmonic(V, Ele, 2, Q);
W.derived().resize(n, m);
// No linear terms
VectorXd c = VectorXd::Zero(n);
// No linear constraints
SparseMatrix<typename DerivedW::Scalar> A(0, n), Aeq(0, n), Aieq(0, n);
VectorXd Beq(0, 1), Bieq(0, 1);
// Upper and lower box constraints (Constant bounds)
VectorXd ux = maxValue * VectorXd::Ones(n);
VectorXd lx = minValue * VectorXd::Zero(n);
active_set_params eff_params = data.active_set_params;
if (data.verbosity >= 1)
{
cout << "BBW: max_iter: " << data.active_set_params.max_iter << endl;
cout << "BBW: eff_max_iter: " << eff_params.max_iter << endl;
}
if (data.verbosity >= 1)
{
cout << "BBW: Computing initial weights for " << m << " handle" <<
(m != 1 ? "s" : "") << "." << endl;
}
min_quad_with_fixed_data<typename DerivedW::Scalar > mqwf;
min_quad_with_fixed_precompute(Q, b, Aeq, true, mqwf);
min_quad_with_fixed_solve(mqwf, c, bc, Beq, W);
// decrement
eff_params.max_iter--;
bool error = false;
// Loop over handles
std::mutex critical;
const auto& optimize_weight = [&](const int i)
{
// Quicker exit for paralle_for
if (error)
{
return;
}
if (data.verbosity >= 1)
{
std::lock_guard<std::mutex> lock(critical);
cout << "BBW: Computing weight for handle " << i + 1 << " out of " << m <<
"." << endl;
}
VectorXd bci = bc.col(i);
VectorXd Wi;
// use initial guess
Wi = W.col(i);
SolverStatus ret = active_set(
Q, c, b, bci, Aeq, Beq, Aieq, Bieq, lx, ux, eff_params, Wi);
switch (ret)
{
case SOLVER_STATUS_CONVERGED:
break;
case SOLVER_STATUS_MAX_ITER:
cerr << "active_set: max iter without convergence." << endl;
break;
case SOLVER_STATUS_ERROR:
default:
cerr << "active_set error." << endl;
error = true;
}
W.col(i) = Wi;
};
parallel_for(m, optimize_weight, 2);
if (error)
{
return false;
}
#ifndef NDEBUG
const double min_rowsum = W.rowwise().sum().array().abs().minCoeff();
if (min_rowsum < 0.1)
{
cerr << "bbw.cpp: Warning, minimum row sum is very low. Consider more "
"active set iterations or enforcing partition of unity." << endl;
}
#endif
return true;
}
#ifdef IGL_STATIC_LIBRARY
// Explicit template instantiation
template bool igl::bbw<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, igl::BBWData&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::bbw<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, double, double, igl::BBWData&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
#endif