00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __VCG_IMPLICIT_SMOOTHER
00024 #define __VCG_IMPLICIT_SMOOTHER
00025
00026 #include <eigenlib/Eigen/Sparse>
00027 #include <vcg/complex/algorithms/mesh_to_matrix.h>
00028 #include <vcg/complex/algorithms/update/quality.h>
00029 #include <vcg/complex/algorithms/smooth.h>
00030
00031 #define PENALTY 10000
00032
00033 namespace vcg{
00034
00035
00036 template <class MeshType>
00037 class ImplicitSmoother
00038 {
00039 typedef typename MeshType::FaceType FaceType;
00040 typedef typename MeshType::VertexType VertexType;
00041 typedef typename MeshType::CoordType CoordType;
00042 typedef typename MeshType::ScalarType ScalarType;
00043 typedef typename Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic> MatrixXm;
00044
00045
00046 public:
00047
00048 struct FaceConstraint
00049 {
00050 int numF;
00051 std::vector<ScalarType > BarycentricW;
00052 CoordType TargetPos;
00053
00054 FaceConstraint()
00055 {
00056 numF=-1;
00057 }
00058
00059 FaceConstraint(int _numF,
00060 const std::vector<ScalarType > &_BarycentricW,
00061 const CoordType &_TargetPos)
00062 {
00063 numF=_numF;
00064 BarycentricW= std::vector<ScalarType > (_BarycentricW.begin(),_BarycentricW.end());
00065 TargetPos=_TargetPos;
00066 }
00067 };
00068
00069 struct Parameter
00070 {
00071
00072 ScalarType lambda;
00073
00074
00075 bool useMassMatrix;
00076
00077 bool fixBorder;
00078
00079 bool useCotWeight;
00080
00081 ScalarType lapWeight;
00082
00083 std::vector<int> FixedV;
00084
00085 std::vector<FaceConstraint> ConstrainedF;
00086
00087 int degree;
00088
00089 bool SmoothQ;
00090
00091 Parameter()
00092 {
00093 degree=1;
00094 lambda=0.2;
00095 useMassMatrix=true;
00096 fixBorder=false;
00097 useCotWeight=false;
00098 lapWeight=1;
00099 SmoothQ=false;
00100 }
00101 };
00102
00103 private:
00104
00105
00106 static void InitSparse(const std::vector<std::pair<int,int> > &Index,
00107 const std::vector<ScalarType> &Values,
00108 const int m,
00109 const int n,
00110 Eigen::SparseMatrix<ScalarType>& X)
00111 {
00112 assert(Index.size()==Values.size());
00113
00114 std::vector<Eigen::Triplet<ScalarType> > IJV;
00115 IJV.reserve(Index.size());
00116
00117 for(size_t i= 0;i<Index.size();i++)
00118 {
00119 int row=Index[i].first;
00120 int col=Index[i].second;
00121 ScalarType val=Values[i];
00122
00123 assert(row<m);
00124 assert(col<n);
00125
00126 IJV.push_back(Eigen::Triplet<ScalarType>(row,col,val));
00127 }
00128 X.resize(m,n);
00129 X.setFromTriplets(IJV.begin(),IJV.end());
00130 }
00131
00132
00133 static void CollectHardConstraints(MeshType &mesh,const Parameter &SParam,
00134 std::vector<std::pair<int,int> > &IndexC,
00135 std::vector<ScalarType> &WeightC,
00136 bool SmoothQ=false)
00137 {
00138 std::vector<int> To_Fix;
00139
00140
00141 if (SParam.fixBorder)
00142 {
00143
00144 for (size_t i=0;i<mesh.vert.size();i++)
00145 {
00146 if (!mesh.vert[i].IsB())continue;
00147 To_Fix.push_back(i);
00148 }
00149 }
00150
00151 To_Fix.insert(To_Fix.end(),SParam.FixedV.begin(),SParam.FixedV.end());
00152
00153
00154 std::sort(To_Fix.begin(),To_Fix.end());
00155 typename std::vector<int>::iterator it= std::unique (To_Fix.begin(), To_Fix.end());
00156 To_Fix.resize( std::distance(To_Fix.begin(),it) );
00157
00158 for (size_t i=0;i<To_Fix.size();i++)
00159 {
00160 if (!SmoothQ)
00161 {
00162 for (int j=0;j<3;j++)
00163 {
00164 int IndexV=(To_Fix[i]*3)+j;
00165 IndexC.push_back(std::pair<int,int>(IndexV,IndexV));
00166 WeightC.push_back((ScalarType)PENALTY);
00167 }
00168 }else
00169 {
00170 int IndexV=To_Fix[i];
00171 IndexC.push_back(std::pair<int,int>(IndexV,IndexV));
00172 WeightC.push_back((ScalarType)PENALTY);
00173 }
00174
00175 }
00176 }
00177
00178 static void CollectBarycentricConstraints(MeshType &mesh,
00179 const Parameter &SParam,
00180 std::vector<std::pair<int,int> > &IndexC,
00181 std::vector<ScalarType> &WeightC,
00182 std::vector<int> &IndexRhs,
00183 std::vector<ScalarType> &ValueRhs)
00184 {
00185 ScalarType penalty;
00186 int baseIndex=mesh.vert.size();
00187 for (size_t i=0;i<SParam.ConstrainedF.size();i++)
00188 {
00189
00190 int IndexConstraint=baseIndex+i;
00191
00192
00193 int FaceN=SParam.ConstrainedF[i].numF;
00194 assert(FaceN>=0);
00195 assert(FaceN<(int)mesh.face.size());
00196 assert(mesh.face[FaceN].VN()==(int)SParam.ConstrainedF[i].BarycentricW.size());
00197 penalty=ScalarType(1) - SParam.lapWeight;
00198 assert(penalty>ScalarType(0) && penalty<ScalarType(1));
00199
00200
00201 for (int j=0;j<mesh.face[FaceN].VN();j++)
00202 {
00203
00204 ScalarType currW=SParam.ConstrainedF[i].BarycentricW[j];
00205
00206
00207 int FaceVert=vcg::tri::Index(mesh,mesh.face[FaceN].V(j));
00208
00209
00210 for (int k=0;k<3;k++)
00211 {
00212
00213 int IndexV=(FaceVert*3)+k;
00214
00215
00216 int ComponentConstraint=(IndexConstraint*3)+k;
00217 IndexC.push_back(std::pair<int,int>(ComponentConstraint,IndexV));
00218
00219 WeightC.push_back(currW*penalty);
00220
00221 IndexC.push_back(std::pair<int,int>(IndexV,ComponentConstraint));
00222 WeightC.push_back(currW*penalty);
00223
00224
00225 IndexC.push_back(std::pair<int,int>(ComponentConstraint,ComponentConstraint));
00226 WeightC.push_back(-1);
00227 }
00228 }
00229
00230 for (int j=0;j<3;j++)
00231 {
00232
00233 int ComponentConstraint=(IndexConstraint*3)+j;
00234
00235
00236 ScalarType ComponentV=SParam.ConstrainedF[i].TargetPos.V(j);
00237
00238
00239 IndexRhs.push_back(ComponentConstraint);
00240 ValueRhs.push_back(ComponentV*penalty);
00241 }
00242
00243 }
00244 }
00245
00246 public:
00247
00248
00249 static void Compute(MeshType &mesh, Parameter &SParam)
00250 {
00251
00252 int matr_size=mesh.vert.size()+SParam.ConstrainedF.size();
00253
00254
00255 Eigen::SparseMatrix<ScalarType> L,M,B;
00256
00257
00258 std::vector<std::pair<int,int> > IndexM;
00259 std::vector<ScalarType> ValuesM;
00260
00261
00262 if (SParam.useMassMatrix)
00263 MeshToMatrix<MeshType>::MassMatrixEntry(mesh,IndexM,ValuesM,!SParam.SmoothQ);
00264
00265
00266 for (size_t i=0;i<SParam.ConstrainedF.size();i++)
00267 {
00268 int baseIndex=(mesh.vert.size()+i)*3;
00269
00270 if (SParam.SmoothQ)
00271 baseIndex=(mesh.vert.size()+i);
00272
00273 if (SParam.SmoothQ)
00274 {
00275 IndexM.push_back(std::pair<int,int>(baseIndex,baseIndex));
00276 ValuesM.push_back(1);
00277 }
00278 else
00279 {
00280 for (int j=0;j<3;j++)
00281 {
00282 IndexM.push_back(std::pair<int,int>(baseIndex+j,baseIndex+j));
00283 ValuesM.push_back(1);
00284 }
00285 }
00286 }
00287
00288 CollectHardConstraints(mesh,SParam,IndexM,ValuesM,SParam.SmoothQ);
00289
00290
00291 if (!SParam.SmoothQ)
00292 InitSparse(IndexM,ValuesM,matr_size*3,matr_size*3,M);
00293 else
00294 InitSparse(IndexM,ValuesM,matr_size,matr_size,M);
00295
00296
00297 std::vector<std::pair<int,int> > IndexB;
00298 std::vector<ScalarType> ValuesB;
00299
00300 std::vector<int> IndexRhs;
00301 std::vector<ScalarType> ValuesRhs;
00302
00303
00304 if (!SParam.SmoothQ)
00305 {
00306 CollectBarycentricConstraints(mesh,SParam,IndexB,ValuesB,IndexRhs,ValuesRhs);
00307
00308 InitSparse(IndexB,ValuesB,matr_size*3,matr_size*3,B);
00309 }
00310 else
00311 InitSparse(IndexB,ValuesB,matr_size,matr_size,B);
00312
00313
00314 std::vector<std::pair<int,int> > IndexL;
00315 std::vector<ScalarType> ValuesL;
00316 MeshToMatrix<MeshType>::GetLaplacianMatrix(mesh,IndexL,ValuesL,SParam.useCotWeight,SParam.lapWeight,!SParam.SmoothQ);
00317
00318
00319 if (!SParam.SmoothQ)
00320 InitSparse(IndexL,ValuesL,matr_size*3,matr_size*3,L);
00321 else
00322 InitSparse(IndexL,ValuesL,matr_size,matr_size,L);
00323
00324 for (int i=0;i<(SParam.degree-1);i++)L=L*L;
00325
00326
00327 Eigen::SparseMatrix<ScalarType> S = (M + B + SParam.lambda*L);
00328
00329
00330 Eigen::SimplicialCholesky<Eigen::SparseMatrix<ScalarType > > solver(S);
00331 assert(solver.info() == Eigen::Success);
00332
00333 MatrixXm V;
00334 if (!SParam.SmoothQ)
00335 V=MatrixXm(matr_size*3,1);
00336 else
00337 V=MatrixXm(matr_size,1);
00338
00339
00340 if (!SParam.SmoothQ)
00341 {
00342 for (size_t i=0;i<mesh.vert.size();i++)
00343 {
00344 int index=i*3;
00345 V(index,0)=mesh.vert[i].P().X();
00346 V(index+1,0)=mesh.vert[i].P().Y();
00347 V(index+2,0)=mesh.vert[i].P().Z();
00348 }
00349 }
00350 else
00351 {
00352 for (size_t i=0;i<mesh.vert.size();i++)
00353 {
00354 int index=i;
00355 V(index,0)=mesh.vert[i].Q();
00356 }
00357 }
00358
00359
00360 for (size_t i=0;i<IndexRhs.size();i++)
00361 {
00362 int index=IndexRhs[i];
00363 ScalarType val=ValuesRhs[i];
00364 V(index,0)=val;
00365 }
00366
00367
00368 V = solver.solve(M*V).eval();
00369
00370
00371 if (!SParam.SmoothQ)
00372 {
00373 for (size_t i=0;i<mesh.vert.size();i++)
00374 {
00375 int index=i*3;
00376 mesh.vert[i].P().X()=V(index,0);
00377 mesh.vert[i].P().Y()=V(index+1,0);
00378 mesh.vert[i].P().Z()=V(index+2,0);
00379 }
00380 }else
00381 {
00382 for (size_t i=0;i<mesh.vert.size();i++)
00383 {
00384 int index=i;
00385 mesh.vert[i].Q()=V(index,0);
00386 }
00387 }
00388 }
00389 };
00390
00391 }
00392
00393 #endif