MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMesh/MRFreeFormDeformer.h
Go to the documentation of this file.
1#pragma once
2#include "MRMeshFwd.h"
3#include "MRVector3.h"
4#include "MRBox.h"
5#include "MRVector.h"
6#include "MRMeshEigen.h"
7
8namespace MR
9{
10// Class for deforming mesh using Bernstein interpolation
12{
13public:
14 // Only set mesh ref
15 MRMESH_API FreeFormDeformer( VertCoords& coords, const VertBitSet& valid );
16 MRMESH_API FreeFormDeformer( Mesh& mesh, const VertBitSet* region = nullptr );
17 // Calculates all points' normalized positions in parallel
18 // sets ref grid by initialBox, if initialBox is invalid uses mesh bounding box instead
19 MRMESH_API void init( const Vector3i& resolution = Vector3i::diagonal( 2 ), const Box3f& initialBox = Box3f() );
20 // Updates ref grid point position
21 MRMESH_API void setRefGridPointPosition( const Vector3i& coordOfPointInGrid, const Vector3f& newPos );
22 // Gets ref grid point position
23 MRMESH_API const Vector3f& getRefGridPointPosition( const Vector3i& coordOfPointInGrid ) const;
24 // Apply updated grid to all mesh points in parallel
25 // ensure updating render object after using it
26 MRMESH_API void apply() const;
27 // Apply updated grid to given point
28 MRMESH_API Vector3f applySinglePoint( const Vector3f& point ) const;
29 // Get one dimension index by grid coord
30 MRMESH_API int getIndex( const Vector3i& coordOfPointInGrid ) const;
31 // Get grid coord by index
32 MRMESH_API Vector3i getCoord( int index ) const;
33
34 const std::vector<Vector3f>& getAllRefGridPositions() const { return refPointsGrid_; }
35 void setAllRefGridPositions( const std::vector<Vector3f>& refPoints ) { refPointsGrid_ = refPoints; }
36 const Vector3i& getResolution() const { return resolution_; }
37private:
38 VertCoords& coords_;
39 const VertBitSet& validPoints_;
40 std::vector<Vector3f> refPointsGrid_;
41 Box3f initialBox_;
42 VertCoords normedCoords_;
43 Vector3i resolution_;
44
45 Vector3f applyToNormedPoint_( const Vector3f& normedPoint, std::vector<Vector3f>& xPlaneCache, std::vector<Vector3f>& yLineCache, std::vector<Vector3f>& tempPoints ) const;
46};
47
52{
53public:
55 MRMESH_API FreeFormBestFit( const Box3d& box, const Vector3i& resolution = Vector3i::diagonal( 2 ) );
56
58 MRMESH_API void addPair( const Vector3d& src, const Vector3d& tgt, double w = 1.0 );
59 void addPair( const Vector3f& src, const Vector3f& tgt, float w = 1.0f ) { addPair( Vector3d( src ), Vector3d( tgt ), double( w ) ); }
60
62 MRMESH_API void addOther( const FreeFormBestFit& other );
63
66 void setStabilizer( double stabilizer ) { stabilizer_ = stabilizer; }
67 double getStabilizer() const { return stabilizer_; }
68
70 [[nodiscard]] MRMESH_API std::vector<Vector3f> findBestDeformationReferenceGrid();
71private:
72 Box3d box_;
73 Vector3i resolution_;
74 size_t resXY_{ 0 };
75 size_t size_{ 0 };
76 double sumWeight_{ 0.0 };
77
78 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> accumA_;
79 Eigen::Matrix<double, Eigen::Dynamic, 3> accumB_;
80
81 std::vector<int> pascalLineX_;
82 std::vector<int> pascalLineY_;
83 std::vector<int> pascalLineZ_;
84
85 Vector3d reverseDiagonal_;
86
87 double stabilizer_{ 0.1 };
88 void stabilize_();
89};
90
92MRMESH_API std::vector<Vector3f> makeFreeFormOriginGrid( const Box3f& box, const Vector3i& resolution );
93
94// Calculates best Free Form transform to fit given source->target deformation
95// origin ref grid as box corners ( resolution parameter specifies how to divide box )
96// samplesToBox - if set used to transform source and target points to box space
97// returns new positions of ref grid
98MRMESH_API std::vector<Vector3f> findBestFreeformDeformation( const Box3f& box, const std::vector<Vector3f>& source, const std::vector<Vector3f>& target,
99 const Vector3i& resolution = Vector3i::diagonal( 2 ), const AffineXf3f* samplesToBox = nullptr );
100
101}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:79
#define MRMESH_CLASS
Definition MRMesh/MRMeshFwd.h:83
Definition MRMesh/MRFreeFormDeformer.h:52
MRMESH_API void addPair(const Vector3d &src, const Vector3d &tgt, double w=1.0)
add pair of source and target point to accumulator
void addPair(const Vector3f &src, const Vector3f &tgt, float w=1.0f)
Definition MRMesh/MRFreeFormDeformer.h:59
double getStabilizer() const
Definition MRMesh/MRFreeFormDeformer.h:67
void setStabilizer(double stabilizer)
Definition MRMesh/MRFreeFormDeformer.h:66
MRMESH_API std::vector< Vector3f > findBestDeformationReferenceGrid()
finds best grid points positions to align source points to target points
MRMESH_API void addOther(const FreeFormBestFit &other)
adds other instance of FreeFormBestFit if it has same ref grid
MRMESH_API FreeFormBestFit(const Box3d &box, const Vector3i &resolution=Vector3i::diagonal(2))
initialize the class, compute cached values and reserve space for matrices
Definition MRMesh/MRFreeFormDeformer.h:12
MRMESH_API void setRefGridPointPosition(const Vector3i &coordOfPointInGrid, const Vector3f &newPos)
MRMESH_API FreeFormDeformer(VertCoords &coords, const VertBitSet &valid)
MRMESH_API FreeFormDeformer(Mesh &mesh, const VertBitSet *region=nullptr)
const Vector3i & getResolution() const
Definition MRMesh/MRFreeFormDeformer.h:36
MRMESH_API const Vector3f & getRefGridPointPosition(const Vector3i &coordOfPointInGrid) const
MRMESH_API Vector3i getCoord(int index) const
MRMESH_API int getIndex(const Vector3i &coordOfPointInGrid) const
MRMESH_API Vector3f applySinglePoint(const Vector3f &point) const
MRMESH_API void init(const Vector3i &resolution=Vector3i::diagonal(2), const Box3f &initialBox=Box3f())
void setAllRefGridPositions(const std::vector< Vector3f > &refPoints)
Definition MRMesh/MRFreeFormDeformer.h:35
MRMESH_API void apply() const
const std::vector< Vector3f > & getAllRefGridPositions() const
Definition MRMesh/MRFreeFormDeformer.h:34
MRMESH_API std::vector< Vector3f > findBestFreeformDeformation(const Box3f &box, const std::vector< Vector3f > &source, const std::vector< Vector3f > &target, const Vector3i &resolution=Vector3i::diagonal(2), const AffineXf3f *samplesToBox=nullptr)
MRMESH_API std::vector< Vector3f > makeFreeFormOriginGrid(const Box3f &box, const Vector3i &resolution)
Returns positions of grid points in given box with given resolution.
Definition MRMesh/MRMesh.h:23