MeshLib Documentation
Loading...
Searching...
No Matches
MRMoveMeshToVoxelMaxDeriv.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVoxelsFwd.h"
4
7#include "MRMesh/MRMatrix3.h"
8#include "MRMesh/MRAffineXf.h"
11
12
13namespace MR
14{
15
17{
19 int iters = 30;
20
23 int samplePoints = 6;
24
26 int degree = 3;
27
31 float outlierThreshold = 1.f;
32
35
38};
39
40
45 Mesh& mesh, const AffineXf3f& meshXf,
46 const VdbVolume& volume, const AffineXf3f& volumeXf,
47 const MoveMeshToVoxelMaxDerivSettings& settings,
48 ProgressCallback callback = {}
49 );
50
51
52
53// Helper class to organize mesh and voxels volume access and build point sequences
54// Note: this class is not thread-safe but accessing same volume from different instances is ok
55template <typename MeshType>
57{
58public:
59 MRVOXELS_API MeshOnVoxelsT( MeshType& mesh, const AffineXf3f& meshXf, const VdbVolume& volume, const AffineXf3f& volumeXf );
61
62 // Access to base data
63 MRVOXELS_API MeshType& mesh() const;
64
65 MRVOXELS_API const VdbVolume& volume() const;
66
67
68 // Cached number of valid vertices
70
71 // Voxel size as scalar
72 MRVOXELS_API float voxelSize() const;
73
74
75 // Transformation mesh to volume
76 // All points are in voxels volume space, unless otherwise is implied
77 MRVOXELS_API AffineXf3f xf() const;
78
79 MRVOXELS_API Vector3f xf( const Vector3f& pt ) const;
80
81 MRVOXELS_API AffineXf3f xfInv() const;
82
83 MRVOXELS_API Vector3f xfInv( const Vector3f &pt ) const;
84
85
86 // Vertex position
87 MRVOXELS_API Vector3f point( VertId v ) const;
88
89 // Volume value
90 MRVOXELS_API float getValue( const Vector3f& pos ) const;
91
92 // Get offset vector (mesh normal for a vertex with `voxelSize` length)
93 MRVOXELS_API Vector3f getOffsetVector( VertId v ) const;
94
95 // Get a pseudo-index for a zero-based point index in a zero-centered row of `count` points
96 // Pseudo-index is a signed number; for whole index, is is whole or half-whole
97 MRVOXELS_API static float pseudoIndex( float index, int count );
98
99 MRVOXELS_API static float pseudoIndex( int index, int count );
100
101 MRVOXELS_API static float indexFromPseudoIndex( float pseudoIndex, int count );
102
103 // Get row of points with `offset` stride
104 MRVOXELS_API void getPoints( std::vector<Vector3f>& result, const Vector3f& pos, const Vector3f& offset ) const;
105
106 // Get volume values for a row of points
107 MRVOXELS_API void getValues( std::vector<float>& result, const Vector3f& pos, const Vector3f& offset ) const;
108
109 // Get derivatives from result of `getValues`
110 MRVOXELS_API static void getDerivatives( std::vector<float>& result, const std::vector<float>& values );
111
112 // Get best fit parabola in pseudo-index space for a zero-centered array
113 static Parabolaf getBestParabola( auto begin, auto end )
114 {
115 BestFitParabola<float> bestFitParabola;
116 auto size = std::distance( begin, end );
117 for ( auto it = begin; it != end; ++it )
118 bestFitParabola.addPoint( pseudoIndex( int( it - begin ), int( size ) ), *it );
119 return bestFitParabola.getBestParabola();
120 }
121
122 template <size_t degree>
123 static Polynomialf<degree> getBestPolynomial( const std::vector<float>& values )
124 {
126 for ( size_t i = 0; i < values.size(); ++i )
127 bestFit.addPoint( pseudoIndex( int( i ), int( values.size() ) ), values[i] );
128 auto poly = bestFit.getBestPolynomial().template cast<float>();
129 return poly;
130 }
131
132 MRVOXELS_API static PolynomialWrapperf getBestPolynomial( const std::vector<float>& values, size_t degree );
133
134private:
135 MeshType& mesh_;
136 const VdbVolume& volume_;
137 float voxelSize_;
140 AffineXf3f xf_, xfInv_;
141 Matrix3f xfNormal_;
142 bool noXf_; // Xf is unit or translation
143 int numVerts_;
144};
145
146
149
150
151}
#define MRVOXELS_API
Definition MRVoxelsFwd.h:13
accumulates a number of (x,y) points to find the best-least-squares parabola approximating them
Definition MRBestFitParabola.h:12
Parabola< T > getBestParabola(T tol=std::numeric_limits< T >::epsilon()) const
computes the best approximating parabola from the accumulated points;
Definition MRBestFitParabola.h:45
void addPoint(T x, T y)
accumulates one more point for parabola fitting
Definition MRBestFitParabola.h:29
Definition MRBestFitPolynomial.h:95
MRMESH_API void addPoint(T x, T y)
MRMESH_API Polynomial< T, degree > getBestPolynomial() const
Definition MRMoveMeshToVoxelMaxDeriv.h:57
MRVOXELS_API AffineXf3f xf() const
MRVOXELS_API void getPoints(std::vector< Vector3f > &result, const Vector3f &pos, const Vector3f &offset) const
static Parabolaf getBestParabola(auto begin, auto end)
Definition MRMoveMeshToVoxelMaxDeriv.h:113
MRVOXELS_API Vector3f point(VertId v) const
MRVOXELS_API int numVerts() const
static MRVOXELS_API PolynomialWrapperf getBestPolynomial(const std::vector< float > &values, size_t degree)
MRVOXELS_API float getValue(const Vector3f &pos) const
MRVOXELS_API MeshType & mesh() const
MRVOXELS_API const VdbVolume & volume() const
MRVOXELS_API void getValues(std::vector< float > &result, const Vector3f &pos, const Vector3f &offset) const
MRVOXELS_API Vector3f getOffsetVector(VertId v) const
MRVOXELS_API Vector3f xfInv(const Vector3f &pt) const
MRVOXELS_API float voxelSize() const
static MRVOXELS_API float indexFromPseudoIndex(float pseudoIndex, int count)
static MRVOXELS_API float pseudoIndex(float index, int count)
MRVOXELS_API MeshOnVoxelsT(MeshType &mesh, const AffineXf3f &meshXf, const VdbVolume &volume, const AffineXf3f &volumeXf)
MRVOXELS_API Vector3f xf(const Vector3f &pt) const
MRVOXELS_API MeshOnVoxelsT(const MeshOnVoxelsT &other)
static MRVOXELS_API float pseudoIndex(int index, int count)
static MRVOXELS_API void getDerivatives(std::vector< float > &result, const std::vector< float > &values)
static Polynomialf< degree > getBestPolynomial(const std::vector< float > &values)
Definition MRMoveMeshToVoxelMaxDeriv.h:123
MRVOXELS_API AffineXf3f xfInv() const
helper class for generalized voxel volume data access
Definition MRVoxelsVolumeAccess.h:14
Definition MRVolumeInterpolation.h:18
auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:280
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:282
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:576
Definition MRCameraOrientationPlugin.h:8
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
tl::expected< T, E > Expected
Definition MRExpected.h:58
MRVOXELS_API Expected< VertBitSet > moveMeshToVoxelMaxDeriv(Mesh &mesh, const AffineXf3f &meshXf, const VdbVolume &volume, const AffineXf3f &volumeXf, const MoveMeshToVoxelMaxDerivSettings &settings, ProgressCallback callback={})
Definition MRMesh/MRMesh.h:23
Definition MRMoveMeshToVoxelMaxDeriv.h:17
float intermediateSmoothForce
force of the smoothing (relaxation) of vector field of shifts on each iteration
Definition MRMoveMeshToVoxelMaxDeriv.h:34
float preparationSmoothForce
force of initial smoothing of vertices, before applying the algorithm
Definition MRMoveMeshToVoxelMaxDeriv.h:37
float outlierThreshold
Definition MRMoveMeshToVoxelMaxDeriv.h:31
int iters
number of iterations. Each iteration moves vertex only slightly and smooths the vector field of shift...
Definition MRMoveMeshToVoxelMaxDeriv.h:19
int samplePoints
Definition MRMoveMeshToVoxelMaxDeriv.h:23
int degree
degree of the polynomial used to fit sampled points. Must be in range [3; 6]
Definition MRMoveMeshToVoxelMaxDeriv.h:26
This is a unifying interface for a polynomial of some degree, known only in runtime.
Definition MRBestFitPolynomial.h:74
Definition MRBestFitPolynomial.h:32