-
Notifications
You must be signed in to change notification settings - Fork 4
/
smpl.h
301 lines (294 loc) · 12.8 KB
/
smpl.h
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "cnpy.h"
#include <iostream>
#include <queue> // î÷åðåäü
#include <stack> // ñòåê
#include <vector>
#include <set>
#include <omp.h>
#define COUT_VAR(x) std::cout << #x"=" << x << std::endl;
#define SHOW_IMG(x) cv::namedWindow(#x);cv::imshow(#x,x);cv::waitKey(20);
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
class smpl
{
//---------------------------------------------------------------------------
// Simple class for triangle mesh normals computarion
//---------------------------------------------------------------------------
class NormalsComputer
{
private:
// vector of vertices shared triangles
// vertex normals compured by averaging face normals
std::vector < std::set<unsigned int> > sharedTriangles;
std::vector<unsigned int> faces;
public:
//-------------------
// constructor
//-------------------
NormalsComputer(std::vector<unsigned int>& faces)
{
this->faces.assign(faces.begin(), faces.end());
// need to compute once per mesh
computeSharedTriangles(faces, sharedTriangles);
}
//-------------------
// destructor
//-------------------
~NormalsComputer()
{
}
//--------------------------------------------------------------------------
// compute vertex normals
// input: vertices as array of floats in v1x,v1y,v1z,...vmx,vmy,vmz format
//--------------------------------------------------------------------------
void getVertexNormals(unsigned int nVertices, float* vertices, float* verticesNormals)
{
computeVertexNormals(faces.size(), nVertices, faces.data(), vertices, verticesNormals);
}
//--------------------------------------------------------------------------
// same for std::vector type
//--------------------------------------------------------------------------
void getVertexNormals(std::vector<float>& vertices, std::vector<float>& verticesNormals)
{
computeVertexNormals(faces, vertices, verticesNormals);
}
//--------------------------------------------------------------------------
// same for face normals
//--------------------------------------------------------------------------
void getFaceNormals(std::vector<float>& vertices, std::vector<float>& normals)
{
computeFaceNormals(faces, vertices, normals);
}
private:
//--------------------------------------------------------------------------
// compute trianges shared by each vertex
//--------------------------------------------------------------------------
void computeSharedTriangles(std::vector<unsigned int>& faces, std::vector < std::set<unsigned int> >& result)
{
unsigned int max_ind = *std::max_element(faces.begin(), faces.end()) + 1;
result.resize(max_ind);
for (unsigned int i = 0; i < faces.size() / 3; ++i)
{
unsigned int a = faces.data()[i * 3 + 0];
unsigned int b = faces.data()[i * 3 + 1];
unsigned int c = faces.data()[i * 3 + 2];
if (a == b || a == c || b == c)
{
std::cout << "Incorrect triangle !" << std::endl;
}
result[a].insert(i);
result[b].insert(i);
result[c].insert(i);
}
}
//--------------------------------------------------------------------------
// compute one face normsl
//--------------------------------------------------------------------------
void computeNormal(float* a, float* b, float* c, float* normal)
{
float v1[3], v2[3];
v1[0] = b[0] - a[0];
v1[1] = b[1] - a[1];
v1[2] = b[2] - a[2];
v2[0] = b[0] - c[0];
v2[1] = b[1] - c[1];
v2[2] = b[2] - c[2];
normal[0] = v1[1] * v2[2] - v1[2] * v2[1];
normal[1] = v1[2] * v2[0] - v1[0] * v2[2];
normal[2] = v1[0] * v2[1] - v1[1] * v2[0];
float nx = normal[0];// / norm;
float ny = normal[1];// / norm;
float nz = normal[2];// / norm;
float norm = sqrt(nx * nx + ny * ny + nz * nz);
//
normal[0] = -nx;
normal[1] = -ny;
normal[2] = -nz;
}
//--------------------------------------------------------------------------
// compute all face normals
//--------------------------------------------------------------------------
void computeFaceNormals(std::vector<unsigned int>& faces, std::vector<float>& vertices, std::vector<float>& normals)
{
normals.clear();
normals.resize(faces.size(), 0);
#pragma omp parallel for
for (unsigned int i = 0; i < faces.size() / 3; ++i)
{
float* a;
float* b;
float* c;
float* N;
unsigned int a_ind = faces[3 * i + 0];
unsigned int b_ind = faces[3 * i + 1];
unsigned int c_ind = faces[3 * i + 2];
if (a_ind == b_ind || a_ind == c_ind || b_ind == c_ind)
{
std::cout << "Incorrect triangle !" << std::endl;
}
a = vertices.data() + a_ind * 3;
b = vertices.data() + b_ind * 3;
c = vertices.data() + c_ind * 3;
computeNormal(a, b, c, normals.data() + i * 3);
}
}
//--------------------------------------------------------------------------
// compute vertex normals by averaging shared face normals
//--------------------------------------------------------------------------
void computeVertexNormals(std::vector<unsigned int>& faces, std::vector<float>& vertices, std::vector<float>& result)
{
result.resize(vertices.size());
std::fill(result.begin(), result.end(), 0);
std::vector<float> faceNormals;
computeFaceNormals(faces, vertices, faceNormals);
for (unsigned int i = 0; i < sharedTriangles.size(); ++i)
{
std::set<unsigned int>::iterator it;
for (it = sharedTriangles[i].begin(); it != sharedTriangles[i].end(); ++it)
{
unsigned int t = *it;
result[i * 3 + 0] += faceNormals[t * 3 + 0];
result[i * 3 + 1] += faceNormals[t * 3 + 1];
result[i * 3 + 2] += faceNormals[t * 3 + 2];
}
float n = sqrt(result[i * 3 + 0] * result[i * 3 + 0] + result[i * 3 + 1] * result[i * 3 + 1] + result[i * 3 + 2] * result[i * 3 + 2]);
if (n > 0)
{
result[i * 3 + 0] /= n;
result[i * 3 + 1] /= n;
result[i * 3 + 2] /= n;
}
else
{
std::cout << "zero normal" << std::endl;
}
}
}
//--------------------------------------------------------------------------
// smae as before for pointers
//--------------------------------------------------------------------------
void computeVertexNormals(unsigned int nFaces, unsigned int nVertices, unsigned int* faces, float* vertices, float* result)
{
std::vector<unsigned int> vfaces(nFaces);
std::vector<float> vvertices(nVertices);
memcpy(vfaces.data(), faces, nFaces * sizeof(unsigned int));
memcpy(vvertices.data(), vertices, nVertices * sizeof(float));
std::vector<float> vresult;
computeVertexNormals(vfaces, vvertices, vresult);
memcpy(result, vresult.data(), vresult.size() * sizeof(float));
}
};
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
class Skeleton
{
public:
int root_ind;
std::vector<int> l1;// = { -1,0,1,2,0,4,5,0,7,8,0,10,11,0,13,14 };
std::vector<int> l2;// = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
std::vector<int> level;
std::vector<int> has_child;
std::vector < std::vector<int> > paths;
std::vector < std::vector<int> > mas;
std::vector < std::vector<int> > chains;
std::vector<float> joints_rots_vector;
cnpy::NpyArray template_joints_abs_coords;
cnpy::NpyArray template_joints_rel_coords;
cnpy::NpyArray posed_joints_rel_coords;
cnpy::NpyArray joint_rel_transforms;
cnpy::NpyArray joint_abs_transforms;
// thit need to apply pose diiven pca shape deformations
cnpy::NpyArray pose_maps;
cnpy::NpyArray hands_mean;
//-----------------------------------
// skeleton
//-----------------------------------
struct Edge
{
int begin;
int end;
};
//-----------------------------------
// constructor
//-----------------------------------
Skeleton();
void parse(cnpy::NpyArray& Kinematic_tree);
void setPose(std::vector<float>& joints_rots_vector);
};
public:
int face_index_num;
int vertex_num;
int shape_basis_dim;
int pose_basis_dim;
int joint_num;
cnpy::npz_t npz_map;
cnpy::NpyArray kinematicTree;
cnpy::NpyArray faceIndices;
cnpy::NpyArray vertices_template;
cnpy::NpyArray shapeBlendBasis;
cnpy::NpyArray poseBlendBasis;
cnpy::NpyArray jointRegressor;
cnpy::NpyArray weights;
// used for hand model only
cnpy::NpyArray hands_components;
cnpy::NpyArray hands_coeffs;
// ----------------------------
// for MANO_left.npz should be:
//face_index_num=1538
//vertex_num=778
//shape_basis_dim=10
//pose_basis_dim=135
//joint_num=16
//kinematicTree shape=[2,16]
//faceIndices shape [1538,3]
//vertices_template shape:[778,3]
//shapeBlendBasis shape:[778,3,10]
//poseBlendBasis shape:[778,3,135]
//jointRegressor shape:[16,778]
//weights shape:[778,16]
// ----------------------------
cnpy::NpyArray Beta;
cnpy::NpyArray v_shaped;
cnpy::NpyArray v_posed;
cnpy::NpyArray posed_vertices;
cnpy::NpyArray vetexNormals;
cnpy::NpyArray jointCoords;
Skeleton skeleton;
std::shared_ptr<NormalsComputer> normalsComputer;
smpl();
~smpl();
public:
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
void loadModel(std::string filename);
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
void computeBlendShape(cnpy::NpyArray& Beta, cnpy::NpyArray& MeanShape, cnpy::NpyArray& ShapeBlendBasis, cnpy::NpyArray& V_Shaped);
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
void smpl::computeBlendPose(cnpy::NpyArray& PoseMap, cnpy::NpyArray& V_Shaped, cnpy::NpyArray& PoseBlendBasis, cnpy::NpyArray& V_Posed);
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
void BlendTransforms(cnpy::NpyArray& weights, cnpy::NpyArray& joint_rel_transforms, cnpy::NpyArray& blendedTransforms);
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
void LinearBlend(cnpy::NpyArray& template_vertices, cnpy::NpyArray& weights, cnpy::NpyArray& joint_rel_transforms, cnpy::NpyArray& posed_vertices);
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
void computeJointsFromShape(cnpy::NpyArray& Shape, cnpy::NpyArray& JointRegressor, cnpy::NpyArray& JointCoords);
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
void updateMesh(std::vector<float>& shape, std::vector<float>& psoe);
};
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------