forked from sxyu/avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
333 lines (295 loc) · 10.6 KB
/
Util.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "Util.h"
#include "UtilCnpy.h"
#include <fstream>
#include <cstdlib>
#include <boost/filesystem.hpp>
#include <Eigen/Dense>
#include <opencv2/imgcodecs.hpp>
#include "Calibration.h"
namespace ark {
namespace util {
std::vector<std::string> split(const std::string& string_in,
char const* delimiters, bool ignore_empty,
bool trim) {
char* buffer = new char[string_in.size() + 1];
strcpy(buffer, string_in.c_str());
std::vector<std::string> output;
for (char* token = strtok(buffer, delimiters); token != NULL;
token = strtok(NULL, delimiters)) {
output.emplace_back(token);
util::trim(*output.rbegin());
if (ignore_empty && output.rbegin()->empty()) output.pop_back();
}
delete[] buffer;
return output;
}
std::vector<std::string> split(const char* string_in, char const* delimiters,
bool ignore_empty, bool trim) {
return split(std::string(string_in), delimiters, ignore_empty, trim);
}
// trimming functions from: https://stackoverflow.com/questions/216823/
// trim from start (in place)
void ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](int ch) { return !std::isspace(ch); }));
}
// trim from end (in place)
void rtrim(std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](int ch) { return !std::isspace(ch); })
.base(),
s.end());
}
// trim from both ends (in place)
void trim(std::string& s) {
ltrim(s);
rtrim(s);
}
void upper(std::string& s) {
for (size_t i = 0; i < s.size(); ++i) s[i] = std::toupper(s[i]);
}
void lower(std::string& s) {
for (size_t i = 0; i < s.size(); ++i) s[i] = std::tolower(s[i]);
}
std::string resolveRootPath(const std::string& root_path) {
static const std::string TEST_PATH = "data/avatar-model/extract.py";
static const int MAX_LEVELS = 3;
static std::string rootDir = "\n";
if (rootDir == "\n") {
rootDir.clear();
const char* env = std::getenv("OPENARK_DIR");
if (env) {
// use environmental variable if exists and works
rootDir = env;
// auto append slash
if (!rootDir.empty() && rootDir.back() != '/' &&
rootDir.back() != '\\')
rootDir.push_back('/');
std::ifstream test_ifs(rootDir + TEST_PATH);
if (!test_ifs) rootDir.clear();
}
const char* env2 = std::getenv("SMPLSYNTH_DIR");
if (env2) {
// use environmental variable if exists and works
rootDir = env2;
// auto append slash
if (!rootDir.empty() && rootDir.back() != '/' &&
rootDir.back() != '\\')
rootDir.push_back('/');
std::ifstream test_ifs(rootDir + TEST_PATH);
if (!test_ifs) rootDir.clear();
}
// else check current directory and parents
if (rootDir.empty()) {
for (int i = 0; i < MAX_LEVELS; ++i) {
std::ifstream test_ifs(rootDir + TEST_PATH);
if (test_ifs) break;
rootDir.append("../");
}
}
}
typedef boost::filesystem::path path;
return (path(rootDir) / path(root_path)).string();
}
cv::Vec3b paletteColor(int color_index, bool bgr) {
using cv::Vec3b;
static const Vec3b palette[] = {
Vec3b(0, 220, 255), Vec3b(177, 13, 201), Vec3b(94, 255, 34),
Vec3b(54, 65, 255), Vec3b(64, 255, 255), Vec3b(217, 116, 0),
Vec3b(27, 133, 255), Vec3b(190, 18, 240), Vec3b(20, 31, 210),
Vec3b(75, 20, 133), Vec3b(255, 219, 127), Vec3b(204, 204, 57),
Vec3b(112, 153, 61), Vec3b(64, 204, 46), Vec3b(112, 255, 1),
Vec3b(170, 170, 170), Vec3b(225, 30, 42)};
Vec3b color =
palette[color_index % (int)(sizeof palette / sizeof palette[0])];
return bgr ? color : Vec3b(color[2], color[1], color[0]);
}
Eigen::Matrix<float, 3, Eigen::Dynamic> paletteColorTable(int num_colors,
bool bgr) {
Eigen::Matrix<float, 3, Eigen::Dynamic> colors(3, num_colors);
for (size_t i = 0; i < num_colors; ++i) {
const Vec3b v = util::paletteColor(i, bgr);
for (int j = 0; j < 3; ++j) {
colors(j, i) = (float)v[j] / 255.f;
}
}
return colors;
}
cv::Vec4d getCameraIntrinFromXYZ(const cv::Mat& xyz_map) {
int rows = xyz_map.rows, cols = xyz_map.cols;
Eigen::MatrixXd A(rows * cols, 2);
Eigen::MatrixXd b(rows * cols, 1);
cv::Vec4d result;
// fx cx
const cv::Vec3f* ptr;
for (int r = 0; r < rows; ++r) {
ptr = xyz_map.ptr<cv::Vec3f>(r);
for (int c = 0; c < cols; ++c) {
const int i = r * cols + c;
A(i, 0) = ptr[c][0];
A(i, 1) = ptr[c][2];
b(i) = c * ptr[c][2];
}
}
Eigen::Vector2d wx = A.colPivHouseholderQr().solve(b);
result[0] = wx[0];
result[1] = wx[1];
// fy cy
for (int r = 0; r < rows; ++r) {
ptr = xyz_map.ptr<cv::Vec3f>(r);
for (int c = 0; c < cols; ++c) {
const int i = r * cols + c;
A(i, 0) = ptr[c][1];
A(i, 1) = ptr[c][2];
b(i) = r * ptr[c][2];
}
}
Eigen::Vector2d wy = A.colPivHouseholderQr().solve(b);
result[2] = wy[0];
result[3] = wy[1];
return result;
}
void readDepth(const std::string& path, cv::Mat& m, bool allow_exr) {
if (allow_exr && path.size() > 4 &&
!path.compare(path.size() - 4, path.size(), ".exr")) {
// Read .exr instead
m = cv::imread(path, cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
return;
}
std::ifstream ifs(path, std::ios::binary | std::ios::in);
ushort wid, hi;
util::read_bin(ifs, hi);
util::read_bin(ifs, wid);
m = cv::Mat::zeros(hi, wid, CV_32FC1);
int zr = 0;
for (int i = 0; i < hi; ++i) {
float* ptr = m.ptr<float>(i);
for (int j = 0; j < wid; ++j) {
if (zr)
--zr;
else {
if (!ifs) break;
float x;
util::read_bin(ifs, x);
if (x >= 0) {
ptr[j] = x;
} else {
zr = (int)(-x) - 1;
}
}
}
}
}
void readXYZ(const std::string& path, cv::Mat& m, const CameraIntrin& intrin,
bool allow_exr) {
readDepth(path, m, allow_exr);
if (!m.empty() && m.channels() == 1) {
m = intrin.depthToXYZ(m);
}
}
void writeDepth(const std::string& image_path, cv::Mat& depth_map) {
std::ofstream ofsd(image_path, std::ios::binary | std::ios::out);
if (ofsd) {
util::write_bin(ofsd, (ushort)depth_map.rows);
util::write_bin(ofsd, (ushort)depth_map.cols);
int zrun = 0;
for (int i = 0; i < depth_map.rows; ++i) {
const float* ptr = depth_map.ptr<float>(i);
for (int j = 0; j < depth_map.cols; ++j) {
if (ptr[j] == 0) {
++zrun;
continue;
} else {
if (zrun >= 1) {
util::write_bin(ofsd, (float)(-zrun));
}
zrun = 0;
util::write_bin(
ofsd, ptr[j]); // util::write_bin(ofsd, ptr[j][1]);
// writeBinary(ofsd, ptr[j][2]);
}
}
}
ofsd.close();
}
}
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
loadFloatMatrix(const cnpy::NpyArray& raw, size_t r, size_t c) {
size_t dwidth = raw.word_size;
_ARK_ASSERT(dwidth == 4 || dwidth == 8);
if (raw.fortran_order) {
if (dwidth == 4) {
return Eigen::Map<const Eigen::MatrixXf>(raw.data<float>(), r, c)
.template cast<double>();
} else {
return Eigen::Map<const Eigen::MatrixXd>(raw.data<double>(), r, c);
}
} else {
if (dwidth == 4) {
return Eigen::Map<const Eigen::Matrix<
float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
raw.data<float>(), r, c)
.template cast<double>();
} else {
return Eigen::Map<const Eigen::Matrix<
double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
raw.data<double>(), r, c);
}
}
}
Eigen::Matrix<uint32_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
loadUintMatrix(const cnpy::NpyArray& raw, size_t r, size_t c) {
size_t dwidth = raw.word_size;
_ARK_ASSERT(dwidth == 4 || dwidth == 8);
if (raw.fortran_order) {
if (dwidth == 4) {
return Eigen::template Map<const Eigen::Matrix<
uint32_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>(
raw.data<uint32_t>(), r, c);
} else {
return Eigen::template Map<const Eigen::Matrix<
uint64_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>(
raw.data<uint64_t>(), r, c)
.template cast<uint32_t>();
}
} else {
if (dwidth == 4) {
return Eigen::template Map<const Eigen::Matrix<
uint32_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
raw.data<uint32_t>(), r, c);
} else {
return Eigen::template Map<const Eigen::Matrix<
uint64_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
raw.data<uint64_t>(), r, c)
.template cast<uint32_t>();
}
}
}
void assertShape(const cnpy::NpyArray& m, std::initializer_list<size_t> shape) {
_ARK_ASSERT_EQ(m.shape.size(), shape.size());
size_t idx = 0;
for (auto& dim : shape) {
if (dim != ANY_SHAPE) _ARK_ASSERT_EQ(m.shape[idx], dim);
++idx;
}
}
} // namespace util
namespace random_util {
float uniform(float min_inc, float max_exc) {
thread_local static std::mt19937 rg(std::random_device{}());
return uniform(rg, min_inc, max_exc);
}
float randn(float mean, float variance) {
thread_local static std::mt19937 rg(std::random_device{}());
return randn(rg, mean, variance);
}
float uniform(std::mt19937& rg, float min_inc, float max_exc) {
std::uniform_real_distribution<float> uniform(min_inc, max_exc);
return uniform(rg);
}
float randn(std::mt19937& rg, float mean, float variance) {
std::normal_distribution<float> normal(mean, variance);
return normal(rg);
}
} // namespace random_util
} // namespace ark