MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMesh/MRLaplacian.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRBitSet.h"
4#include "MRVector.h"
5#include "MRVector3.h"
6#include "MREnums.h"
7
8#pragma warning(push)
9#pragma warning(disable: 4068) // unknown pragmas
10#pragma warning(disable: 4127) // conditional expression is constant
11#pragma warning(disable: 4464) // relative include path contains '..'
12#pragma warning(disable: 5054) // operator '|': deprecated between enumerations of different types
13#pragma clang diagnostic push
14#pragma clang diagnostic ignored "-Wdeprecated-anon-enum-enum-conversion"
15#pragma clang diagnostic ignored "-Wunknown-warning-option" // for next one
16#pragma clang diagnostic ignored "-Wunused-but-set-variable" // for newer clang
17#include <Eigen/SparseCore>
18#pragma clang diagnostic pop
19#pragma warning(pop)
20
21namespace MR
22{
23
32{
33public:
34 enum class RememberShape
35 {
36 Yes, // true Laplacian mode when initial mesh shape is remembered and copied in apply
37 No // ignore initial mesh shape in the region and just position vertices smoothly in the region
38 };
39
40 MRMESH_API explicit Laplacian( Mesh & mesh );
41 Laplacian( const MeshTopology & topology, VertCoords & points ) : topology_( topology ), points_( points ) { }
42
45 MRMESH_API void init( const VertBitSet & freeVerts, EdgeWeights weights, VertexMass vmass = VertexMass::Unit,
47
50 MRMESH_API void fixVertex( VertId v, bool smooth = true );
51
54 MRMESH_API void fixVertex( VertId v, const Vector3f & fixedPos, bool smooth = true );
55
58
61
63 MRMESH_API void applyToScalar( VertScalars & scalarField );
64
66 [[nodiscard]] const VertBitSet & region() const { return region_; }
67
69 [[nodiscard]] const VertBitSet & freeVerts() const { return freeVerts_; }
70
72 [[nodiscard]] const VertBitSet & firstLayerFixedVerts() const { assert( solverValid_ ); return firstLayerFixedVerts_; }
73
74private:
75 // updates solver_ only
76 void updateSolver_();
77
78 // updates rhs_ only
79 void updateRhs_();
80
81 template <typename I, typename G, typename S>
82 void prepareRhs_( I && iniRhs, G && g, S && s );
83
84 const MeshTopology & topology_;
85 VertCoords & points_;
86
87 // all initially free vertices and the first layer of vertices around them
88 VertBitSet region_;
89
90 // currently free vertices
91 VertBitSet freeVerts_;
92
93 // fixed vertices where no smoothness is required
94 VertBitSet fixedSharpVertices_;
95
96 // fixed vertices from the first layer around free vertices
97 VertBitSet firstLayerFixedVerts_;
98
99 // for all vertices in the region
100 struct Equation
101 {
102 Vector3d rhs; // equation right hand side
103 double centerCoeff = 0; // coefficient on matrix diagonal
104 int firstElem = 0; // index in nonZeroElements_
105 };
106 std::vector<Equation> equations_;
107
108 struct Element
109 {
110 double coeff = 0;
111 VertId neiVert;
112 };
113 std::vector<Element> nonZeroElements_;
114
115 // map from vertex index to matrix row/col
116 Vector< int, VertId > regionVert2id_;
117 Vector< int, VertId > freeVert2id_;
118
119 using SparseMatrix = Eigen::SparseMatrix<double,Eigen::RowMajor>;
120 SparseMatrix M_;
121
122 // if true then we do not need to recompute solver_ in the apply
123 bool solverValid_ = false;
124 using SparseMatrixColMajor = Eigen::SparseMatrix<double,Eigen::ColMajor>;
125
126 // interface needed to hide implementation headers
127 class Solver
128 {
129 public:
130 virtual ~Solver() = default;
131 virtual void compute( const SparseMatrixColMajor& A ) = 0;
132 virtual Eigen::VectorXd solve( const Eigen::VectorXd& rhs ) = 0;
133 };
134 std::unique_ptr<Solver> solver_;
135
136 // if true then we do not need to recompute rhs_ in the apply
137 bool rhsValid_ = false;
138 Eigen::VectorXd rhs_[3];
139};
140
141} //namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:79
Definition MRMesh/MRLaplacian.h:32
const VertBitSet & freeVerts() const
return currently free vertices
Definition MRMesh/MRLaplacian.h:69
MRMESH_API void apply()
given fixed vertices, computes positions of remaining region vertices
const VertBitSet & firstLayerFixedVerts() const
return fixed vertices from the first layer around free vertices
Definition MRMesh/MRLaplacian.h:72
MRMESH_API void fixVertex(VertId v, const Vector3f &fixedPos, bool smooth=true)
MRMESH_API void updateSolver()
if you manually call this method after initialization and fixing vertices then next apply call will b...
RememberShape
Definition MRMesh/MRLaplacian.h:35
MRMESH_API Laplacian(Mesh &mesh)
Laplacian(const MeshTopology &topology, VertCoords &points)
Definition MRMesh/MRLaplacian.h:41
MRMESH_API void init(const VertBitSet &freeVerts, EdgeWeights weights, VertexMass vmass=VertexMass::Unit, RememberShape rem=Laplacian::RememberShape::Yes)
const VertBitSet & region() const
return all initially free vertices and the first layer of vertices around them
Definition MRMesh/MRLaplacian.h:66
MRMESH_API void fixVertex(VertId v, bool smooth=true)
MRMESH_API void applyToScalar(VertScalars &scalarField)
given a pre-resized scalar field with set values in fixed vertices, computes the values in free verti...
Definition MRMesh/MRMeshTopology.h:18
EdgeWeights
determines the weight of each edge in applications like Laplacian
Definition MREnums.h:18
I
Definition MRMesh/MRMeshFwd.h:121
VertexMass
determines the weight or mass of each vertex in applications like Laplacian
Definition MREnums.h:8
@ Unit
all vertices have same mass=1
Definition MRMesh/MRMesh.h:22