forked from sxyu/avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvatarHelpers.cpp
314 lines (292 loc) · 12.4 KB
/
AvatarHelpers.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "internal/AvatarHelpers.h"
#include <fstream>
#include <iostream>
#include <cmath>
#include <limits>
#include <string>
namespace ark {
/** Hand-written faster function to load a saved PCL point cloud directly
* into an Eigen vector, where points are stored: x1 y1 z1 x2 y2 z2 ...
* The reason we flatten the cloud instead of using a matrix is to make it
* easier to add in shape keys, which would otherwise need to be tensors */
Eigen::VectorXd loadPCDToPointVectorFast(const std::string& path) {
std::ifstream pcd(path);
int nPoints = -1;
while (pcd) {
std::string label;
pcd >> label;
if (label == "DATA") {
if (nPoints < 0) {
std::cerr << "ERROR: invalid PCD file at " << path
<< ": no WIDTH field before data, so "
"we don't know how many points there are!\n";
std::exit(0);
}
pcd >> label;
if (label != "ascii") {
std::cerr << "ERROR: non-ascii PCD not supported! File " << path
<< "\n";
std::exit(0);
}
break;
} else if (label == "WIDTH") {
pcd >> nPoints;
pcd.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else {
std::string _;
std::getline(pcd, _);
}
}
if (!pcd || nPoints < 0) {
std::cerr << "ERROR: invalid PCD file at " << path
<< ": unexpected EOF\n";
std::exit(0);
}
Eigen::VectorXd result(nPoints * 3);
for (int i = 0; i < nPoints * 3; ++i) {
pcd >> result(i);
}
return result;
}
/** Spherical to rectangular coords */
void fromSpherical(double rho, double theta, double phi, Eigen::Vector3d& out) {
out[0] = rho * sin(phi) * cos(theta);
out[1] = rho * cos(phi);
out[2] = rho * sin(phi) * sin(theta);
}
template <class T>
void paintTriangleBary(cv::Mat& output, const cv::Size& image_size,
const std::vector<cv::Point2f>& projected,
const cv::Vec3i& face, const float* zvec, float maxz) {
std::pair<double, int> yf[3] = {{projected[face(0)].y, 0},
{projected[face(1)].y, 1},
{projected[face(2)].y, 2}};
std::sort(yf, yf + 3);
// reorder points for convenience
auto a = projected[face(yf[0].second)], b = projected[face(yf[1].second)],
c = projected[face(yf[2].second)];
a.y = std::floor(a.y);
c.y = std::ceil(c.y);
if (a.y == c.y) return;
int minyi = std::max<int>(a.y, 0),
maxyi = std::min<int>(c.y, image_size.height - 1),
midyi = std::floor(b.y);
float az = zvec[yf[0].second], bz = zvec[yf[1].second],
cz = zvec[yf[2].second];
float denom =
1.0f / ((b.x - c.x) * (a.y - c.y) + (c.y - b.y) * (a.x - c.x));
if (a.y != b.y) {
float mhi = (c.x - a.x) / (c.y - a.y);
float bhi = a.x - a.y * mhi;
float mlo = (b.x - a.x) / (b.y - a.y);
float blo = a.x - a.y * mlo;
if (b.x > c.x) {
std::swap(mlo, mhi);
std::swap(blo, bhi);
}
for (int i = minyi; i <= std::min(midyi, image_size.height - 1); ++i) {
int minxi = std::max<int>(std::floor(mlo * i + blo), 0),
maxxi = std::min<int>(std::ceil(mhi * i + bhi),
image_size.width - 1);
if (minxi > maxxi) continue;
float w1v = (b.x - c.x) * (i - c.y);
float w2v = (c.x - a.x) * (i - c.y);
T* ptr = output.ptr<T>(i);
for (int j = minxi; j <= maxxi; ++j) {
float w1 = (w1v + (c.y - b.y) * (j - c.x)) * denom;
float w2 = (w2v + (a.y - c.y) * (j - c.x)) * denom;
ptr[j] = T(std::min(
std::max(w1 * az + w2 * bz + (1.f - w1 - w2) * cz, 0.0f),
maxz));
}
}
}
if (b.y != c.y) {
float mhi = (c.x - a.x) / (c.y - a.y);
float bhi = a.x - a.y * mhi;
float mlo = (c.x - b.x) / (c.y - b.y);
float blo = b.x - b.y * mlo;
if (b.x > a.x) {
std::swap(mlo, mhi);
std::swap(blo, bhi);
}
for (int i = std::max(midyi, 0) + (a.y != b.y); i <= maxyi; ++i) {
int minxi = std::max<int>(std::floor(mlo * i + blo), 0),
maxxi = std::min<int>(std::ceil(mhi * i + bhi),
image_size.width - 1);
if (minxi > maxxi) continue;
float w1v = (b.x - c.x) * (i - c.y);
float w2v = (c.x - a.x) * (i - c.y);
T* ptr = output.ptr<T>(i);
for (int j = minxi; j <= maxxi; ++j) {
float w1 = (w1v + (c.y - b.y) * (j - c.x)) * denom;
float w2 = (w2v + (a.y - c.y) * (j - c.x)) * denom;
ptr[j] = T(std::min(
std::max(w1 * az + w2 * bz + (1.f - w1 - w2) * cz, 0.0f),
maxz));
}
}
}
}
template void paintTriangleBary<int>(cv::Mat&, const cv::Size&,
const std::vector<cv::Point2f>&,
const cv::Vec3i&, const float*, float);
template void paintTriangleBary<float>(cv::Mat&, const cv::Size&,
const std::vector<cv::Point2f>&,
const cv::Vec3i&, const float*, float);
template void paintTriangleBary<uint8_t>(cv::Mat&, const cv::Size&,
const std::vector<cv::Point2f>&,
const cv::Vec3i&, const float*, float);
/** Paint projected triangle on part mask (CV_8U) using nearest neighbors interp
*/
void paintPartsTriangleNN(
cv::Mat& output_assigned_joint_mask, const cv::Size& image_size,
const std::vector<cv::Point2f>& projected,
const std::vector<std::vector<std::pair<double, int>>>& assigned_joint,
const cv::Vec3i& face, const std::vector<int>& part_map) {
std::pair<double, int> xf[3] = {{projected[face[0]].x, 0},
{projected[face[1]].x, 1},
{projected[face[2]].x, 2}};
std::sort(xf, xf + 3);
// reorder points for convenience
auto a = projected[face[xf[0].second]], b = projected[face[xf[1].second]],
c = projected[face[xf[2].second]];
a.x = std::floor(a.x);
c.x = std::ceil(c.x);
if (a.x == c.x) return;
auto assigned_a = assigned_joint[face[xf[0].second]][0].second,
assigned_b = assigned_joint[face[xf[1].second]][0].second,
assigned_c = assigned_joint[face[xf[2].second]][0].second;
if (part_map.size()) {
assigned_a = part_map[assigned_a];
assigned_b = part_map[assigned_b];
assigned_c = part_map[assigned_c];
}
int minxi = std::max<int>(a.x, 0),
maxxi = std::min<int>(c.x, image_size.width - 1),
midxi = std::floor(b.x);
if (a.x != b.x) {
double mhi = (c.y - a.y) / (c.x - a.x);
double bhi = a.y - a.x * mhi;
double mlo = (b.y - a.y) / (b.x - a.x);
double blo = a.y - a.x * mlo;
if (b.y > c.y) {
std::swap(mlo, mhi);
std::swap(blo, bhi);
}
for (int i = minxi; i <= std::min(midxi, image_size.width - 1); ++i) {
int minyi = std::max<int>(std::floor(mlo * i + blo), 0),
maxyi = std::min<int>(std::ceil(mhi * i + bhi),
image_size.height - 1);
if (minyi > maxyi) continue;
for (int j = minyi; j <= maxyi; ++j) {
auto& out = output_assigned_joint_mask.at<uint8_t>(j, i);
int dista = (a.x - i) * (a.x - i) + (a.y - j) * (a.y - j);
int distb = (b.x - i) * (b.x - i) + (b.y - j) * (b.y - j);
int distc = (c.x - i) * (c.x - i) + (c.y - j) * (c.y - j);
if (dista < distb && dista < distc) {
out = assigned_a;
} else if (distb < distc) {
out = assigned_b;
} else {
out = assigned_c;
}
}
}
}
if (b.x != c.x) {
double mhi = (c.y - a.y) / (c.x - a.x);
double bhi = a.y - a.x * mhi;
double mlo = (c.y - b.y) / (c.x - b.x);
double blo = b.y - b.x * mlo;
if (b.y > a.y) {
std::swap(mlo, mhi);
std::swap(blo, bhi);
}
for (int i = std::max(midxi, 0) + 1; i <= maxxi; ++i) {
int minyi = std::max<int>(std::floor(mlo * i + blo), 0),
maxyi = std::min<int>(std::ceil(mhi * i + bhi),
image_size.height - 1);
if (minyi > maxyi) continue;
double w1v = (b.y - c.y) * (i - c.x);
double w2v = (c.y - a.y) * (i - c.x);
for (int j = minyi; j <= maxyi; ++j) {
auto& out = output_assigned_joint_mask.at<uint8_t>(j, i);
int dista = (a.x - i) * (a.x - i) + (a.y - j) * (a.y - j);
int distb = (b.x - i) * (b.x - i) + (b.y - j) * (b.y - j);
int distc = (c.x - i) * (c.x - i) + (c.y - j) * (c.y - j);
if (dista < distb && dista < distc) {
out = assigned_a;
} else if (distb < distc) {
out = assigned_b;
} else {
out = assigned_c;
}
}
}
}
}
template <class T>
void paintTriangleSingleColor(cv::Mat& output_image, const cv::Size& image_size,
const std::vector<cv::Point2f>& projected,
const cv::Vec3i& face, T color) {
std::pair<double, int> yf[3] = {{projected[face[0]].y, 0},
{projected[face[1]].y, 1},
{projected[face[2]].y, 2}};
std::sort(yf, yf + 3);
// reorder points for convenience
auto a = projected[face[yf[0].second]], b = projected[face[yf[1].second]],
c = projected[face[yf[2].second]];
a.y = std::floor(a.y);
c.y = std::ceil(c.y);
if (a.y == c.y) return;
int minyi = std::max<int>(a.y, 0),
maxyi = std::min<int>(c.y, image_size.height - 1),
midyi = std::floor(b.y);
if (a.y != b.y) {
double mhi = (c.x - a.x) / (c.y - a.y);
double bhi = a.x - a.y * mhi;
double mlo = (b.x - a.x) / (b.y - a.y);
double blo = a.x - a.y * mlo;
if (b.x > c.x) {
std::swap(mlo, mhi);
std::swap(blo, bhi);
}
for (int i = minyi; i <= std::min(midyi, image_size.height - 1); ++i) {
int minxi = std::max<int>(std::floor(mlo * i + blo), 0),
maxxi = std::min<int>(std::ceil(mhi * i + bhi),
image_size.width - 1);
if (minxi > maxxi) continue;
T* ptr = output_image.ptr<T>(i);
std::fill(ptr + minxi, ptr + maxxi, color);
}
}
if (b.y != c.y) {
double mhi = (c.x - a.x) / (c.y - a.y);
double bhi = a.x - a.y * mhi;
double mlo = (c.x - b.x) / (c.y - b.y);
double blo = b.x - b.y * mlo;
if (b.x > a.x) {
std::swap(mlo, mhi);
std::swap(blo, bhi);
}
for (int i = std::max(midyi, 0) + 1; i <= maxyi; ++i) {
int minxi = std::max<int>(std::floor(mlo * i + blo), 0),
maxxi = std::min<int>(std::ceil(mhi * i + bhi),
image_size.width - 1);
if (minxi > maxxi) continue;
T* ptr = output_image.ptr<T>(i);
std::fill(ptr + minxi, ptr + maxxi, color);
}
}
}
template void paintTriangleSingleColor<int>(cv::Mat&, const cv::Size&,
const std::vector<cv::Point2f>&,
const cv::Vec3i&, int);
template void paintTriangleSingleColor<float>(cv::Mat&, const cv::Size&,
const std::vector<cv::Point2f>&,
const cv::Vec3i&, float);
template void paintTriangleSingleColor<uint8_t>(cv::Mat&, const cv::Size&,
const std::vector<cv::Point2f>&,
const cv::Vec3i&, uint8_t);
} // namespace ark