MeshLib
 
Loading...
Searching...
No Matches
MRMoveMeshToVoxelMaxDeriv.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#ifndef MRMESH_NO_OPENVDB
7#include "MRMatrix3.h"
8#include "MRAffineXf.h"
9#include "MRBestFitParabola.h"
10#include "MRBestFitPolynomial.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 MRMESH_API MeshOnVoxelsT( MeshType& mesh, const AffineXf3f& meshXf, const VdbVolume& volume, const AffineXf3f& volumeXf );
61
62 // Access to base data
63 MRMESH_API MeshType& mesh() const;
64
65 MRMESH_API const VdbVolume& volume() const;
66
67
68 // Cached number of valid vertices
69 MRMESH_API int numVerts() const;
70
71 // Voxel size as scalar
72 MRMESH_API float voxelSize() const;
73
74
75 // Transformation mesh to volume
76 // All points are in voxels volume space, unless otherwise is implied
78
79 MRMESH_API Vector3f xf( const Vector3f& pt ) const;
80
82
83 MRMESH_API Vector3f xfInv( const Vector3f &pt ) const;
84
85
86 // Vertex position
88
89 // Volume value
90 MRMESH_API float getValue( const Vector3f& pos ) const;
91
92 // Get offset vector (mesh normal for a vertex with `voxelSize` length)
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 MRMESH_API static float pseudoIndex( float index, int count );
98
99 MRMESH_API static float pseudoIndex( int index, int count );
100
101 MRMESH_API static float indexFromPseudoIndex( float pseudoIndex, int count );
102
103 // Get row of points with `offset` stride
104 MRMESH_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 MRMESH_API void getValues( std::vector<float>& result, const Vector3f& pos, const Vector3f& offset ) const;
108
109 // Get derivatives from result of `getValues`
110 MRMESH_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 MRMESH_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}
152#endif
int VertId
Definition MRDotNet/MRMeshFwd.h:51
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
Definition MRDotNet/MRBitSet.h:39
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:92
MRMESH_API void addPoint(T x, T y)
MRMESH_API Polynomial< T, degree > getBestPolynomial() const
Definition MRMoveMeshToVoxelMaxDeriv.h:57
static MRMESH_API PolynomialWrapperf getBestPolynomial(const std::vector< float > &values, size_t degree)
MRMESH_API AffineXf3f xfInv() const
static Parabolaf getBestParabola(auto begin, auto end)
Definition MRMoveMeshToVoxelMaxDeriv.h:113
MRMESH_API Vector3f getOffsetVector(VertId v) const
MRMESH_API const VdbVolume & volume() const
MRMESH_API Vector3f xf(const Vector3f &pt) const
MRMESH_API void getPoints(std::vector< Vector3f > &result, const Vector3f &pos, const Vector3f &offset) const
MRMESH_API Vector3f xfInv(const Vector3f &pt) const
static MRMESH_API float indexFromPseudoIndex(float pseudoIndex, int count)
static MRMESH_API float pseudoIndex(float index, int count)
static MRMESH_API void getDerivatives(std::vector< float > &result, const std::vector< float > &values)
MRMESH_API void getValues(std::vector< float > &result, const Vector3f &pos, const Vector3f &offset) const
MRMESH_API Vector3f point(VertId v) const
MRMESH_API MeshType & mesh() const
MRMESH_API MeshOnVoxelsT(const MeshOnVoxelsT &other)
MRMESH_API float getValue(const Vector3f &pos) const
static MRMESH_API float pseudoIndex(int index, int count)
MRMESH_API int numVerts() const
MRMESH_API float voxelSize() const
static Polynomialf< degree > getBestPolynomial(const std::vector< float > &values)
Definition MRMoveMeshToVoxelMaxDeriv.h:123
MRMESH_API AffineXf3f xf() const
MRMESH_API MeshOnVoxelsT(MeshType &mesh, const AffineXf3f &meshXf, const VdbVolume &volume, const AffineXf3f &volumeXf)
helper class for generalized voxel volume data access
Definition MRVoxelsVolumeAccess.h:17
Definition MRVolumeInterpolation.h:18
represents a 3-dimentional float-typed vector
Definition MRDotNet/MRVector3.h:8
auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:263
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:265
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:589
Definition MRCameraOrientationPlugin.h:7
MRMESH_API VertBitSet moveMeshToVoxelMaxDeriv(Mesh &mesh, const AffineXf3f &meshXf, const VdbVolume &volume, const AffineXf3f &volumeXf, const MoveMeshToVoxelMaxDerivSettings &settings, ProgressCallback callback={})
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
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:71
Definition MRBestFitPolynomial.h:31