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#pragma clang diagnostic ignored "-Wdeprecated-declarations"
18#include <Eigen/SparseCore>
19#pragma clang diagnostic pop
20#pragma warning(pop)
21
22namespace MR
23{
24
34{
35public:
36 enum class RememberShape
37 {
38 Yes, // true Laplacian mode when initial mesh shape is remembered and copied in apply
39 No // ignore initial mesh shape in the region and just position vertices smoothly in the region
40 };
41
42 MRMESH_API explicit Laplacian( Mesh & mesh );
43 Laplacian( const MeshTopology & topology, VertCoords & points ) : topology_( topology ), points_( points ) { }
44
47 MRMESH_API void init( const VertBitSet & freeVerts, EdgeWeights weights, VertexMass vmass = VertexMass::Unit,
49
52 MRMESH_API void fixVertex( VertId v, bool smooth = true );
53
56 MRMESH_API void fixVertex( VertId v, const Vector3f & fixedPos, bool smooth = true );
57
60
63
65 MRMESH_API void applyToScalar( VertScalars & scalarField );
66
68 [[nodiscard]] const VertBitSet & region() const { return region_; }
69
71 [[nodiscard]] const VertBitSet & freeVerts() const { return freeVerts_; }
72
74 [[nodiscard]] const VertBitSet & firstLayerFixedVerts() const { assert( solverValid_ ); return firstLayerFixedVerts_; }
75
76private:
77 // updates solver_ only
78 void updateSolver_();
79
80 // updates rhs_ only
81 void updateRhs_();
82
83 template <typename I, typename G, typename S>
84 void prepareRhs_( I && iniRhs, G && g, S && s );
85
86 const MeshTopology & topology_;
87 VertCoords & points_;
88
89 // all initially free vertices and the first layer of vertices around them
90 VertBitSet region_;
91
92 // currently free vertices
93 VertBitSet freeVerts_;
94
95 // fixed vertices where no smoothness is required
96 VertBitSet fixedSharpVertices_;
97
98 // fixed vertices from the first layer around free vertices
99 VertBitSet firstLayerFixedVerts_;
100
101 // for all vertices in the region
102 struct Equation
103 {
104 Vector3d rhs; // equation right hand side
105 double centerCoeff = 0; // coefficient on matrix diagonal
106 int firstElem = 0; // index in nonZeroElements_
107 };
108 std::vector<Equation> equations_;
109
110 struct Element
111 {
112 double coeff = 0;
113 VertId neiVert;
114 };
115 std::vector<Element> nonZeroElements_;
116
117 // map from vertex index to matrix row/col
118 Vector< int, VertId > regionVert2id_;
119 Vector< int, VertId > freeVert2id_;
120
121 using SparseMatrix = Eigen::SparseMatrix<double,Eigen::RowMajor>;
122 SparseMatrix M_;
123
124 // if true then we do not need to recompute solver_ in the apply
125 bool solverValid_ = false;
126 using SparseMatrixColMajor = Eigen::SparseMatrix<double,Eigen::ColMajor>;
127
128 // interface needed to hide implementation headers
129 class Solver
130 {
131 public:
132 virtual ~Solver() = default;
133 virtual void compute( const SparseMatrixColMajor& A ) = 0;
134 virtual Eigen::VectorXd solve( const Eigen::VectorXd& rhs ) = 0;
135 };
136 std::unique_ptr<Solver> solver_;
137
138 // if true then we do not need to recompute rhs_ in the apply
139 bool rhsValid_ = false;
140 Eigen::VectorXd rhs_[3];
141};
142
143} //namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:79
Definition MRMesh/MRLaplacian.h:34
const VertBitSet & freeVerts() const
return currently free vertices
Definition MRMesh/MRLaplacian.h:71
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:74
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:37
MRMESH_API Laplacian(Mesh &mesh)
Laplacian(const MeshTopology &topology, VertCoords &points)
Definition MRMesh/MRLaplacian.h:43
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:68
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
Definition MRCameraOrientationPlugin.h:8
EdgeWeights
determines the weight of each edge in applications like Laplacian
Definition MREnums.h:18
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