MeshLib Documentation
Loading...
Searching...
No Matches
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 trilinear interpolation
12{
13public:
14 // Only set mesh ref
15 MRMESH_API FreeFormDeformer( VertCoords& coords, const VertBitSet& valid );
16 // Parallel calculates all points normed positions
17 // sets ref grid by initialBox, if initialBox is invalid use mesh bounding box instead
18 MRMESH_API void init( const Vector3i& resolution = Vector3i::diagonal( 2 ), const Box3f& initialBox = Box3f() );
19 // Updates ref grid point position
20 MRMESH_API void setRefGridPointPosition( const Vector3i& coordOfPointInGrid, const Vector3f& newPos );
21 // Gets ref grid point position
22 MRMESH_API const Vector3f& getRefGridPointPosition( const Vector3i& coordOfPointInGrid ) const;
23 // Parallel apply updated grid to all mesh points
24 // ensure updating render object after using it
25 MRMESH_API void apply() const;
26 // Apply updated grid to given point
27 MRMESH_API Vector3f applySinglePoint( const Vector3f& point ) const;
28 // Get one dimension index by grid coord
29 MRMESH_API int getIndex( const Vector3i& coordOfPointInGrid ) const;
30 // Get grid coord by index
31 MRMESH_API Vector3i getCoord( int index ) const;
32
33 const std::vector<Vector3f>& getAllRefGridPositions() const { return refPointsGrid_; }
34 void setAllRefGridPositions( const std::vector<Vector3f>& refPoints ) { refPointsGrid_ = refPoints; }
35 const Vector3i& getResolution() const { return resolution_; }
36private:
37 VertCoords& coords_;
38 const VertBitSet& validPoints_;
39 std::vector<Vector3f> refPointsGrid_;
40 Box3f initialBox_;
41 VertCoords normedCoords_;
42 Vector3i resolution_;
43
44 Vector3f applyToNormedPoint_( const Vector3f& normedPoint, std::vector<Vector3f>& xPlaneCache, std::vector<Vector3f>& yLineCache, std::vector<Vector3f>& tempPoints ) const;
45};
46
51{
52public:
54 MRMESH_API FreeFormBestFit( const Box3d& box, const Vector3i& resolution = Vector3i::diagonal( 2 ) );
55
57 MRMESH_API void addPair( const Vector3d& src, const Vector3d& tgt, double w = 1.0 );
58 void addPair( const Vector3f& src, const Vector3f& tgt, float w = 1.0f ) { addPair( Vector3d( src ), Vector3d( tgt ), double( w ) ); }
59
61 MRMESH_API void addOther( const FreeFormBestFit& other );
62
65 void setStabilizer( double stabilizer ) { stabilizer_ = stabilizer; }
66 double getStabilizer() const { return stabilizer_; }
67
69 [[nodiscard]] MRMESH_API std::vector<Vector3f> findBestDeformationReferenceGrid();
70private:
71 Box3d box_;
72 Vector3i resolution_;
73 size_t resXY_{ 0 };
74 size_t size_{ 0 };
75 double sumWeight_{ 0.0 };
76
77 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> accumA_;
78 Eigen::Matrix<double, Eigen::Dynamic, 3> accumB_;
79
80 std::vector<int> pascalLineX_;
81 std::vector<int> pascalLineY_;
82 std::vector<int> pascalLineZ_;
83
84 Vector3d reverseDiagonal_;
85
86 double stabilizer_{ 0.1 };
87 void stabilize_();
88};
89
91MRMESH_API std::vector<Vector3f> makeFreeFormOriginGrid( const Box3f& box, const Vector3i& resolution );
92
93// Calculates best Free Form transform to fit given source->target deformation
94// origin ref grid as box corners ( resolution parameter specifies how to divide box )
95// samplesToBox - if set used to transform source and target points to box space
96// returns new positions of ref grid
97MRMESH_API std::vector<Vector3f> findBestFreeformDeformation( const Box3f& box, const std::vector<Vector3f>& source, const std::vector<Vector3f>& target,
98 const Vector3i& resolution = Vector3i::diagonal( 2 ), const AffineXf3f* samplesToBox = nullptr );
99
100}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
#define MRMESH_CLASS
Definition MRMesh/MRMeshFwd.h:50
Definition MRFreeFormDeformer.h:51
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 MRFreeFormDeformer.h:58
double getStabilizer() const
Definition MRFreeFormDeformer.h:66
void setStabilizer(double stabilizer)
Definition MRFreeFormDeformer.h:65
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 MRFreeFormDeformer.h:12
MRMESH_API void setRefGridPointPosition(const Vector3i &coordOfPointInGrid, const Vector3f &newPos)
MRMESH_API FreeFormDeformer(VertCoords &coords, const VertBitSet &valid)
const Vector3i & getResolution() const
Definition MRFreeFormDeformer.h:35
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 MRFreeFormDeformer.h:34
MRMESH_API void apply() const
const std::vector< Vector3f > & getAllRefGridPositions() const
Definition MRFreeFormDeformer.h:33
Definition MRCameraOrientationPlugin.h:8
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.