implicit_smooth.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
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         //the amount of smoothness, useful only if we set the mass matrix
00072         ScalarType lambda;
00073         //the use of mass matrix to keep the mesh close to its original position
00074         //(weighted per area distributed on vertices)
00075         bool useMassMatrix;
00076         //this bool is used to fix the border vertices of the mesh or not
00077         bool fixBorder;
00078         //this bool is used to set if cotangent weight is used, this flag to false means uniform laplacian
00079         bool useCotWeight;
00080         //use this weight for the laplacian when the cotangent one is not used
00081         ScalarType lapWeight;
00082         //the set of fixed vertices
00083         std::vector<int> FixedV;
00084         //the set of faces for barycentric constraints
00085         std::vector<FaceConstraint> ConstrainedF;
00086         //the degree of laplacian
00087         int degree;
00088         //this is to say if we smooth the positions or the quality
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         //collect fixed vert
00141         if (SParam.fixBorder)
00142         {
00143             //add penalization constra
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         //add additional fixed vertices constraint
00151         To_Fix.insert(To_Fix.end(),SParam.FixedV.begin(),SParam.FixedV.end());
00152 
00153         //sort and make them unique
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             //get the index of the current constraint
00190             int IndexConstraint=baseIndex+i;
00191 
00192             //add one hard constraint
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             //then add all the weights to impose the constraint
00201             for (int j=0;j<mesh.face[FaceN].VN();j++)
00202             {
00203                 //get the current weight
00204                 ScalarType currW=SParam.ConstrainedF[i].BarycentricW[j];
00205 
00206                 //get the index of the current vertex
00207                 int FaceVert=vcg::tri::Index(mesh,mesh.face[FaceN].V(j));
00208 
00209                 //then add the constraints componentwise
00210                 for (int k=0;k<3;k++)
00211                 {
00212                     //multiply times 3 per component
00213                     int IndexV=(FaceVert*3)+k;
00214 
00215                     //get the index of the current constraint
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                     //this to avoid the 1 on diagonal last entry of mass matrix
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                 //get the index of the current constraint
00233                 int ComponentConstraint=(IndexConstraint*3)+j;
00234 
00235                 //get per component value
00236                 ScalarType ComponentV=SParam.ConstrainedF[i].TargetPos.V(j);
00237 
00238                 //add the diagonal value
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         //calculate the size of the system
00252         int matr_size=mesh.vert.size()+SParam.ConstrainedF.size();
00253 
00254         //the laplacian and the mass matrix
00255         Eigen::SparseMatrix<ScalarType> L,M,B;
00256 
00257         //initialize the mass matrix
00258         std::vector<std::pair<int,int> > IndexM;
00259         std::vector<ScalarType> ValuesM;
00260 
00261         //add the entries for mass matrix
00262         if (SParam.useMassMatrix)
00263             MeshToMatrix<MeshType>::MassMatrixEntry(mesh,IndexM,ValuesM,!SParam.SmoothQ);
00264 
00265         //then add entries for lagrange mult due to barycentric constraints
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         //add the hard constraints
00288         CollectHardConstraints(mesh,SParam,IndexM,ValuesM,SParam.SmoothQ);
00289 
00290         //initialize sparse mass matrix
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         //initialize the barycentric matrix
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         //then also collect hard constraints
00304         if (!SParam.SmoothQ)
00305         {
00306             CollectBarycentricConstraints(mesh,SParam,IndexB,ValuesB,IndexRhs,ValuesRhs);
00307             //initialize sparse constraint matrix
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         //get the entries for laplacian matrix
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         //initialize sparse laplacian matrix
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         //then solve the system
00327         Eigen::SparseMatrix<ScalarType> S = (M + B + SParam.lambda*L);
00328 
00329         //SimplicialLDLT
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         //set the first part of the matrix with vertex values
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         //then set the second part by considering RHS gien by barycentric constraint
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         //solve the system
00368         V = solver.solve(M*V).eval();
00369 
00370         //then copy back values
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 }//end namespace vcg
00392 
00393 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:55