File: implicit_smooth.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (393 lines) | stat: -rw-r--r-- 13,827 bytes parent folder | download | duplicates (2)
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
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef __VCG_IMPLICIT_SMOOTHER
#define __VCG_IMPLICIT_SMOOTHER

#include <Eigen/Sparse>
#include <vcg/complex/algorithms/mesh_to_matrix.h>
#include <vcg/complex/algorithms/update/quality.h>
#include <vcg/complex/algorithms/smooth.h>

#define PENALTY 10000

namespace vcg{


template <class MeshType>
class ImplicitSmoother
{
    typedef typename MeshType::FaceType FaceType;
    typedef typename MeshType::VertexType VertexType;
    typedef typename MeshType::CoordType CoordType;
    typedef typename MeshType::ScalarType ScalarType;
    typedef typename Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic> MatrixXm;


public:

    struct FaceConstraint
    {
        int numF;
        std::vector<ScalarType > BarycentricW;
        CoordType TargetPos;

        FaceConstraint()
        {
            numF=-1;
        }

        FaceConstraint(int _numF,
                       const std::vector<ScalarType > &_BarycentricW,
                       const CoordType &_TargetPos)
        {
            numF=_numF;
            BarycentricW= std::vector<ScalarType > (_BarycentricW.begin(),_BarycentricW.end());
            TargetPos=_TargetPos;
        }
    };

    struct Parameter
    {
        //the amount of smoothness, useful only if we set the mass matrix
        ScalarType lambda;
        //the use of mass matrix to keep the mesh close to its original position
        //(weighted per area distributed on vertices)
        bool useMassMatrix;
        //this bool is used to fix the border vertices of the mesh or not
        bool fixBorder;
        //this bool is used to set if cotangent weight is used, this flag to false means uniform laplacian
        bool useCotWeight;
        //use this weight for the laplacian when the cotangent one is not used
        ScalarType lapWeight;
        //the set of fixed vertices
        std::vector<int> FixedV;
        //the set of faces for barycentric constraints
        std::vector<FaceConstraint> ConstrainedF;
        //the degree of laplacian
        int degree;
        //this is to say if we smooth the positions or the quality
        bool SmoothQ;

        Parameter()
        {
            degree=1;
            lambda=0.2;
            useMassMatrix=true;
            fixBorder=false;
            useCotWeight=false;
            lapWeight=1;
            SmoothQ=false;
        }
    };

private:


    static void InitSparse(const std::vector<std::pair<int,int> > &Index,
                           const std::vector<ScalarType> &Values,
                           const int m,
                           const int n,
                           Eigen::SparseMatrix<ScalarType>& X)
    {
        assert(Index.size()==Values.size());

        std::vector<Eigen::Triplet<ScalarType> > IJV;
        IJV.reserve(Index.size());

        for(size_t i= 0;i<Index.size();i++)
        {
            int row=Index[i].first;
            int col=Index[i].second;
            ScalarType val=Values[i];

            assert(row<m);
            assert(col<n);

            IJV.push_back(Eigen::Triplet<ScalarType>(row,col,val));
        }
        X.resize(m,n);
        X.setFromTriplets(IJV.begin(),IJV.end());
    }


    static void CollectHardConstraints(MeshType &mesh,const Parameter &SParam,
                                       std::vector<std::pair<int,int> > &IndexC,
                                       std::vector<ScalarType> &WeightC,
                                       bool SmoothQ=false)
    {
        std::vector<int> To_Fix;

        //collect fixed vert
        if (SParam.fixBorder)
        {
            //add penalization constra
            for (size_t i=0;i<mesh.vert.size();i++)
            {
                if (!mesh.vert[i].IsB())continue;
                To_Fix.push_back(i);
            }
        }
        //add additional fixed vertices constraint
        To_Fix.insert(To_Fix.end(),SParam.FixedV.begin(),SParam.FixedV.end());

        //sort and make them unique
        std::sort(To_Fix.begin(),To_Fix.end());
        typename std::vector<int>::iterator it= std::unique (To_Fix.begin(), To_Fix.end());
        To_Fix.resize( std::distance(To_Fix.begin(),it) );

        for (size_t i=0;i<To_Fix.size();i++)
        {
            if (!SmoothQ)
            {
                for (int j=0;j<3;j++)
                {
                    int IndexV=(To_Fix[i]*3)+j;
                    IndexC.push_back(std::pair<int,int>(IndexV,IndexV));
                    WeightC.push_back((ScalarType)PENALTY);
                }
            }else
            {
                int IndexV=To_Fix[i];
                IndexC.push_back(std::pair<int,int>(IndexV,IndexV));
                WeightC.push_back((ScalarType)PENALTY);
            }

        }
    }

    static void CollectBarycentricConstraints(MeshType &mesh,
                                              const Parameter &SParam,
                                              std::vector<std::pair<int,int> > &IndexC,
                                              std::vector<ScalarType> &WeightC,
                                              std::vector<int> &IndexRhs,
                                              std::vector<ScalarType> &ValueRhs)
    {
        ScalarType penalty;
        int baseIndex=mesh.vert.size();
        for (size_t i=0;i<SParam.ConstrainedF.size();i++)
        {
            //get the index of the current constraint
            int IndexConstraint=baseIndex+i;

            //add one hard constraint
            int FaceN=SParam.ConstrainedF[i].numF;
            assert(FaceN>=0);
            assert(FaceN<(int)mesh.face.size());
            assert(mesh.face[FaceN].VN()==(int)SParam.ConstrainedF[i].BarycentricW.size());
            penalty=ScalarType(1) - SParam.lapWeight;
            assert(penalty>ScalarType(0) && penalty<ScalarType(1));

            //then add all the weights to impose the constraint
            for (int j=0;j<mesh.face[FaceN].VN();j++)
            {
                //get the current weight
                ScalarType currW=SParam.ConstrainedF[i].BarycentricW[j];

                //get the index of the current vertex
                int FaceVert=vcg::tri::Index(mesh,mesh.face[FaceN].V(j));

                //then add the constraints componentwise
                for (int k=0;k<3;k++)
                {
                    //multiply times 3 per component
                    int IndexV=(FaceVert*3)+k;

                    //get the index of the current constraint
                    int ComponentConstraint=(IndexConstraint*3)+k;
                    IndexC.push_back(std::pair<int,int>(ComponentConstraint,IndexV));

                    WeightC.push_back(currW*penalty);

                    IndexC.push_back(std::pair<int,int>(IndexV,ComponentConstraint));
                    WeightC.push_back(currW*penalty);

                    //this to avoid the 1 on diagonal last entry of mass matrix
                    IndexC.push_back(std::pair<int,int>(ComponentConstraint,ComponentConstraint));
                    WeightC.push_back(-1);
                }
            }

            for (int j=0;j<3;j++)
            {
                //get the index of the current constraint
                int ComponentConstraint=(IndexConstraint*3)+j;

                //get per component value
                ScalarType ComponentV=SParam.ConstrainedF[i].TargetPos.V(j);

                //add the diagonal value
                IndexRhs.push_back(ComponentConstraint);
                ValueRhs.push_back(ComponentV*penalty);
            }

        }
    }

public:


    static void Compute(MeshType &mesh, Parameter &SParam)
    {
        //calculate the size of the system
        int matr_size=mesh.vert.size()+SParam.ConstrainedF.size();

        //the laplacian and the mass matrix
        Eigen::SparseMatrix<ScalarType> L,M,B;

        //initialize the mass matrix
        std::vector<std::pair<int,int> > IndexM;
        std::vector<ScalarType> ValuesM;

        //add the entries for mass matrix
        if (SParam.useMassMatrix)
            MeshToMatrix<MeshType>::MassMatrixEntry(mesh,IndexM,ValuesM,!SParam.SmoothQ);

        //then add entries for lagrange mult due to barycentric constraints
        for (size_t i=0;i<SParam.ConstrainedF.size();i++)
        {
            int baseIndex=(mesh.vert.size()+i)*3;

            if (SParam.SmoothQ)
                baseIndex=(mesh.vert.size()+i);

            if (SParam.SmoothQ)
            {
                IndexM.push_back(std::pair<int,int>(baseIndex,baseIndex));
                ValuesM.push_back(1);
            }
            else
            {
                for (int j=0;j<3;j++)
                {
                    IndexM.push_back(std::pair<int,int>(baseIndex+j,baseIndex+j));
                    ValuesM.push_back(1);
                }
            }
        }
        //add the hard constraints
        CollectHardConstraints(mesh,SParam,IndexM,ValuesM,SParam.SmoothQ);

        //initialize sparse mass matrix
        if (!SParam.SmoothQ)
            InitSparse(IndexM,ValuesM,matr_size*3,matr_size*3,M);
        else
            InitSparse(IndexM,ValuesM,matr_size,matr_size,M);

        //initialize the barycentric matrix
        std::vector<std::pair<int,int> > IndexB;
        std::vector<ScalarType> ValuesB;

        std::vector<int> IndexRhs;
        std::vector<ScalarType> ValuesRhs;

        //then also collect hard constraints
        if (!SParam.SmoothQ)
        {
            CollectBarycentricConstraints(mesh,SParam,IndexB,ValuesB,IndexRhs,ValuesRhs);
            //initialize sparse constraint matrix
            InitSparse(IndexB,ValuesB,matr_size*3,matr_size*3,B);
        }
        else
            InitSparse(IndexB,ValuesB,matr_size,matr_size,B);

        //get the entries for laplacian matrix
        std::vector<std::pair<int,int> > IndexL;
        std::vector<ScalarType> ValuesL;
        MeshToMatrix<MeshType>::GetLaplacianMatrix(mesh,IndexL,ValuesL,SParam.useCotWeight,SParam.lapWeight,!SParam.SmoothQ);

        //initialize sparse laplacian matrix
        if (!SParam.SmoothQ)
            InitSparse(IndexL,ValuesL,matr_size*3,matr_size*3,L);
        else
            InitSparse(IndexL,ValuesL,matr_size,matr_size,L);

        for (int i=0;i<(SParam.degree-1);i++)L=L*L;

        //then solve the system
        Eigen::SparseMatrix<ScalarType> S = (M + B + SParam.lambda*L);

        //SimplicialLDLT
        Eigen::SimplicialCholesky<Eigen::SparseMatrix<ScalarType > > solver(S);
        assert(solver.info() == Eigen::Success);

        MatrixXm V;
        if (!SParam.SmoothQ)
            V=MatrixXm(matr_size*3,1);
        else
            V=MatrixXm(matr_size,1);

        //set the first part of the matrix with vertex values
        if (!SParam.SmoothQ)
        {
            for (size_t i=0;i<mesh.vert.size();i++)
            {
                int index=i*3;
                V(index,0)=mesh.vert[i].P().X();
                V(index+1,0)=mesh.vert[i].P().Y();
                V(index+2,0)=mesh.vert[i].P().Z();
            }
        }
        else
        {
            for (size_t i=0;i<mesh.vert.size();i++)
            {
                int index=i;
                V(index,0)=mesh.vert[i].Q();
            }
        }

        //then set the second part by considering RHS gien by barycentric constraint
        for (size_t i=0;i<IndexRhs.size();i++)
        {
            int index=IndexRhs[i];
            ScalarType val=ValuesRhs[i];
            V(index,0)=val;
        }

        //solve the system
        V = solver.solve(M*V).eval();

        //then copy back values
        if (!SParam.SmoothQ)
        {
            for (size_t i=0;i<mesh.vert.size();i++)
            {
                int index=i*3;
                mesh.vert[i].P().X()=V(index,0);
                mesh.vert[i].P().Y()=V(index+1,0);
                mesh.vert[i].P().Z()=V(index+2,0);
            }
        }else
        {
            for (size_t i=0;i<mesh.vert.size();i++)
            {
                int index=i;
                mesh.vert[i].Q()=V(index,0);
            }
        }
    }
};

}//end namespace vcg

#endif