-
Notifications
You must be signed in to change notification settings - Fork 25
/
meshmonk.hpp
269 lines (225 loc) · 16.9 KB
/
meshmonk.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#ifndef MESHMONK_HPP
#define MESHMONK_HPP
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/IO/reader/OBJReader.hh>
#include <OpenMesh/Core/IO/writer/OBJWriter.hh>
#include <OpenMesh/Tools/Decimater/DecimaterT.hh>
#include <OpenMesh/Tools/Decimater/ModQuadricT.hh>
#include <Eigen/Dense>
#include "src/PyramidNonrigidRegistration.hpp"
#include "src/RigidRegistration.hpp"
#include "src/NonrigidRegistration.hpp"
#include "src/InlierDetector.hpp"
#include "src/CorrespondenceFilter.hpp"
#include "src/SymmetricCorrespondenceFilter.hpp"
#include "src/RigidTransformer.hpp"
#include "src/ViscoElasticTransformer.hpp"
#include "src/Downsampler.hpp"
#include "src/ScaleShifter.hpp"
#include "global.hpp"
#include "src/helper_functions.hpp"
typedef OpenMesh::DefaultTraits MyTraits;
typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits> TriMesh;
typedef OpenMesh::Decimater::DecimaterT<TriMesh> DecimaterType;
typedef OpenMesh::Decimater::ModQuadricT<TriMesh>::Handle HModQuadric;
typedef Eigen::Matrix< int, Eigen::Dynamic, 3> FacesMat; //matrix Mx3 of type unsigned int
typedef Eigen::VectorXf VecDynFloat;
typedef Eigen::Matrix< float, Eigen::Dynamic, registration::NUM_FEATURES> FeatureMat; //matrix Mx6 of type float
typedef Eigen::MatrixX3f Vec3Mat;
typedef Eigen::Matrix4f Mat4Float;
namespace meshmonk{
#ifdef __cplusplus
extern "C"
#endif // __cplusplus
{
//######################################################################################
//################################ TEST SHIZZLE ######################################
//######################################################################################
/*
We're implementing this function simply to test MEX'ing in MATLAB.
*/
void test_meshmonk_mexing(FeatureMat& floatingFeatures, const FeatureMat& targetFeatures, const float multiplier = 2.0f);
/*
Raw data version of test_meshmonk_mexing()
*/
void test_meshmonk_mexing_raw(float floatingFeaturesRaw[], const float targetFeaturesRaw[],
const size_t numFloatingElements, const size_t numTargetElements,
const float multiplier = 2.0f);
//######################################################################################
//################################ REGISTRATION ######################################
//######################################################################################
/*
Full Pyramid Nonrigid Registration
This is the function you'll normally want to call to nonrigidly register a floating mesh to a target mesh.
*/
void pyramid_registration(FeatureMat& floatingFeatures, const FeatureMat& targetFeatures,
const FacesMat& floatingFaces, const FacesMat& targetFaces,
const VecDynFloat& floatingFlags, const VecDynFloat& targetFlags,
const size_t numIterations = 60, const size_t numPyramidLayers = 3,
const float downsampleFloatStart = 90, const float downsampleTargetStart = 90,
const float downsampleFloatEnd = 0, const float downsampleTargetEnd = 0,
const bool correspondencesSymmetric = true, const size_t correspondencesNumNeighbours = 5,
const float correspondencesFlagThreshold = 0.99f, const bool correspondencesEqualizePushPull = false,
const float inlierKappa = 4.0f, const bool inlierUseOrientation = true,
const float transformSigma = 3.0f,
const size_t transformNumViscousIterationsStart = 50, const size_t transformNumViscousIterationsEnd = 1,
const size_t transformNumElasticIterationsStart = 50, const size_t transformNumElasticIterationsEnd = 1);
/*
Standard Nonrigid Registration
This is the standard nonrigid registration procedure without pyramid approach, so computationally a bit slower.
*/
void nonrigid_registration(FeatureMat& floatingFeatures, const FeatureMat& targetFeatures,
const FacesMat& floatingFaces, const FacesMat& targetFaces,
const VecDynFloat& floatingFlags, const VecDynFloat& targetFlags,
const size_t numIterations = 60,
const bool correspondencesSymmetric = true, const size_t correspondencesNumNeighbours = 5,
const float correspondencesFlagThreshold = 0.99f, const bool correspondencesEqualizePushPull = false,
const float inlierKappa = 4.0f, const bool inlierUseOrientation = true,
const float transformSigma = 3.0f,
const size_t transformNumViscousIterationsStart = 50, const size_t transformNumViscousIterationsEnd = 1,
const size_t transformNumElasticIterationsStart = 50, const size_t transformNumElasticIterationsEnd = 1);
/*
Rigid Registration
*/
void rigid_registration(FeatureMat& floatingFeatures, const FeatureMat& targetFeatures,
const FacesMat& floatingFaces, const FacesMat& targetFaces,
const VecDynFloat& floatingFlags, const VecDynFloat& targetFlags,
Mat4Float& transformationMatrix,
const size_t numIterations = 20,
const bool correspondencesSymmetric = true, const size_t correspondencesNumNeighbours = 5,
const float correspondencesFlagThreshold = 0.99f, const bool correspondencesEqualizePushPull = false,
const float inlierKappa = 4.0f, const bool inlierUseOrientation = true,
const bool useScaling = false);
//######################################################################################
//############################ REGISTRATION MODULES ##################################
//######################################################################################
//# Correspondences
void compute_correspondences(const FeatureMat& floatingFeatures, const FeatureMat& targetFeatures,
const VecDynFloat& floatingFlags, const VecDynFloat& targetFlags,
FeatureMat& correspondingFeatures, VecDynFloat& correspondingFlags,
const bool symmetric = true, const size_t numNeighbours = 5,
const float flagThreshold = 0.99f, const bool equalizePushPull = false);
//# Inliers
void compute_inlier_weights(const FeatureMat& floatingFeatures, const FeatureMat& correspondingFeatures,
const VecDynFloat& correspondingFlags, VecDynFloat& inlierWeights,
const float kappa = 4.0f, const bool useOrientation = true);
//# Rigid Transformation
void compute_rigid_transformation(FeatureMat& floatingFeatures, const FeatureMat& correspondingFeatures,
const VecDynFloat& inlierWeights, Mat4Float& transformationMatrix,
const bool useScaling = false);
//# Nonrigid Transformation
void compute_nonrigid_transformation(FeatureMat& floatingFeatures, const FeatureMat& correspondingFeatures,
const FacesMat& floatingFaces, const VecDynFloat& floatingFlags,
const VecDynFloat& inlierWeights,
const size_t numSmoothingNeighbours = 10, const float sigmaSmoothing = 3.0f,
const size_t numViscousIterations = 50, const size_t numElasticIterations = 50);
//# Downsampler
void downsample_mesh(const FeatureMat& features, const FacesMat& faces,
const VecDynFloat& flags,
FeatureMat& downsampledFeatures, FacesMat& downsampledFaces,
VecDynFloat& downsampledFlags, VecDynInt& originalIndices,
const float downsampleRatio = 0.8f);
//# ScaleShifter
//## The scaleshifter is meant to transition from one scale in the pyramid to the next.
void scale_shift_mesh(const FeatureMat& previousFeatures, const VecDynInt& previousIndices,
FeatureMat& newFeatures, const VecDynInt& newIndices);
//######################################################################################
//############################### MESH OPERATIONS ####################################
//######################################################################################
void compute_normals(const Vec3Mat &inPositions, const FacesMat &inFaces,
Vec3Mat &outNormals);
//######################################################################################
//################################ INPUT/OUTPUT ######################################
//######################################################################################
// void read_obj_files(const std::string floatingMeshPath, const std::string targetMeshPath,
// FeatureMat& floatingFeatures, FeatureMat& targetFeatures,
// FacesMat& floatingFaces, FacesMat& targetFaces);
//
// void write_obj_files(FeatureMat& features, FacesMat& faces, const std::string meshPath);
//######################################################################################
//################################ MEX WRAPPING ######################################
//######################################################################################
/*
We're wrapping some functionality in the library so it can be easily mexed in Matlab
*/
void pyramid_registration_mex(float floatingFeaturesArray[], const float targetFeaturesArray[],
const size_t numFloatingElements, const size_t numTargetElements,
const int floatingFacesArray[], const int targetFacesArray[],
const size_t numFloatingFaces, const size_t numTargetFaces,
const float floatingFlagsArray[], const float targetFlagsArray[],
const size_t numIterations = 60, const size_t numPyramidLayers = 3,
const float downsampleFloatStart = 90, const float downsampleTargetStart = 90,
const float downsampleFloatEnd = 0, const float downsampleTargetEnd = 0,
const bool correspondencesSymmetric = true, const size_t correspondencesNumNeighbours = 5,
const float correspondencesFlagThreshold = 0.99f, const bool correspondencesEqualizePushPull = false,
const float inlierKappa = 4.0f, const bool inlierUseOrientation = true,
const float transformSigma = 3.0f,
const size_t transformNumViscousIterationsStart = 50, const size_t transformNumViscousIterationsEnd = 1,
const size_t transformNumElasticIterationsStart = 50, const size_t transformNumElasticIterationsEnd = 1);
void nonrigid_registration_mex(float floatingFeaturesArray[], const float targetFeaturesArray[],
const size_t numFloatingElements, const size_t numTargetElements,
const int floatingFacesArray[], const int targetFacesArray[],
const size_t numFloatingFaces, const size_t numTargetFaces,
const float floatingFlagsArray[], const float targetFlagsArray[],
const size_t numIterations = 60,
const bool correspondencesSymmetric = true, const size_t correspondencesNumNeighbours = 5,
const float correspondencesFlagThreshold = 0.99f, const bool correspondencesEqualizePushPull = false,
const float inlierKappa = 4.0f, const bool inlierUseOrientation = true,
const float transformSigma = 3.0f,
const size_t transformNumViscousIterationsStart = 50, const size_t transformNumViscousIterationsEnd = 1,
const size_t transformNumElasticIterationsStart = 50, const size_t transformNumElasticIterationsEnd = 1);
void rigid_registration_mex(float floatingFeaturesArray[], const float targetFeaturesArray[],
const size_t numFloatingElements, const size_t numTargetElements,
const int floatingFacesArray[], const int targetFacesArray[],
const size_t numFloatingFaces, const size_t numTargetFaces,
const float floatingFlagsArray[], const float targetFlagsArray[],
float transformationMatrixArray[],
const size_t numIterations = 60,
const bool correspondencesSymmetric = true, const size_t correspondencesNumNeighbours = 5,
const float correspondencesFlagThreshold = 0.99f, const bool correspondencesEqualizePushPull = false,
const float inlierKappa = 4.0f, const bool inlierUseOrientation = true,
const bool useScaling = false);
void compute_correspondences_mex(const float floatingFeaturesArray[], const float targetFeaturesArray[],
const size_t numFloatingElements, const size_t numTargetElements,
const float floatingFlagsArray[], const float targetFlagsArray[],
float correspondingFeaturesArray[], float correspondingFlagsArray[],
const bool correspondencesSymmetric = true, const size_t correspondencesNumNeighbours = 5,
const float correspondencesFlagThreshold = 0.99f, const bool correspondencesEqualizePushPull = false);
void compute_inlier_weights_mex(const float floatingFeaturesArray[], const float correspondingFeaturesArray[],
const size_t numFloatingElements,
const float correspondingFlagsArray[], float inlierWeightsArray[],
const float inlierKappa/*= 4.0f*/, const bool useOrientation/*= true*/);
void compute_rigid_transformation_mex(float floatingFeaturesArray[], const size_t numFloatingElements,
const float correspondingFeaturesArray[], const float inlierWeightsArray[],
float transformationMatrixArray[],
const bool useScaling /*= false*/);
void compute_nonrigid_transformation_mex(float floatingFeaturesArray[], const float correspondingFeaturesArray[],
const size_t numFloatingElements,
const int floatingFacesArray[], const size_t numFloatingFaces,
const float floatingFlagsArray[], const float inlierWeightsArray[],
const size_t transformNumNeighbours/*= 10*/, const float transformSigma/*= 3.0f*/,
const size_t transformNumViscousIterations/*= 50*/, const size_t transformNumElasticIterations/*= 50*/);
void downsample_mesh_mex(const float featuresArray[], const size_t numElements,
const int facesArray[], const size_t numFaces,
const float flagsArray[],
float sampledFeaturesArray[], const size_t numSampledElements,
int sampledFacesArray[], const size_t numSampledFaces,
float sampledFlagsArray[],
int originalIndicesArray[],
const float downsampleRatio/* = 0.8f*/);
void scaleshift_mesh_mex(const float oldFeaturesArray[], const size_t numOldElements,
const int oldIndicesArray[],
float newFeaturesArray[], const size_t numNewElements,
const int newIndicesArray[]);
void compute_normals_mex(const float positionsArray[], const size_t numElements,
const int facesArray[], const size_t numFaces,
float normalsArray[]);
#ifdef __cplusplus
}//extern C
#endif // __cplusplus
}//namespace meshmonk
#endif //MESHMONK_HPP