-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnPoseSymmetricMaster.cpp
398 lines (355 loc) · 12 KB
/
UnPoseSymmetricMaster.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "UnPoseSymmetricMaster.h"
#include <vector>
#include <string>
#include <filesystem>
#include "cpprest/json.h"
#include "readGoZFile.h"
#include "calculateEulerAnglesForSymmetrize.h"
#include "Eigen/Core"
#include "Eigen/Geometry"
////
// implementation
////
#if defined(_WIN32) || defined(_WIN64)
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT __attribute__((visibility("default")))
#endif
namespace
{
void readCache(std::filesystem::path &metaPath, web::json::value &meta, std::filesystem::path &cachePath, web::json::value &cache)
{
////
// initialize
////
metaPath.clear();
meta = web::json::value();
cachePath.clear();
cache = web::json::value();
////
// parse meta
////
const std::filesystem::path tmpDir = std::filesystem::temp_directory_path();
metaPath = tmpDir;
metaPath.append("UnPoseSymmetricMeta.json");
if (std::filesystem::exists(metaPath))
{
// json for metadata exists
std::ifstream metaIn(metaPath);
meta = web::json::value::parse(metaIn);
metaIn.close();
}
else
{
// json for metadata does NOT exist
// default path for cache
cachePath = tmpDir;
cachePath.append("UnPoseSymmetricCache.json");
std::string pStr = cachePath.string();
utility::string_t v(pStr.begin(), pStr.end());
meta[_XPLATSTR("path")] = web::json::value(v);
// write metadata to file
std::ofstream metaOut(metaPath);
meta.serialize(metaOut);
metaOut.close();
}
////
// parse cache
////
cachePath = std::filesystem::path(meta[_XPLATSTR("path")].as_string());
if (std::filesystem::exists(cachePath))
{
// json for cache exists
std::ifstream cacheIn(cachePath);
cache = web::json::value::parse(cacheIn);
cacheIn.close();
}
else
{
// json for cache does NOT exist
// empty json
cache = web::json::value();
// write cache to file
std::ofstream cacheOut(cachePath);
cache.serialize(cacheOut);
cacheOut.close();
}
}
} // namespace
extern "C" DLLEXPORT float version(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
return 1.0f;
}
extern "C" DLLEXPORT float initialize(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
const std::filesystem::path tmpDir = std::filesystem::temp_directory_path();
std::filesystem::path metaPath = tmpDir;
metaPath.append("UnPoseSymmetricMeta.json");
if (std::filesystem::exists(metaPath))
{
std::filesystem::remove_all(metaPath);
}
std::filesystem::path cachePath = tmpDir;
cachePath.append("UnPoseSymmetricCache.json");
if (std::filesystem::exists(cachePath))
{
std::filesystem::remove_all(cachePath);
}
return 1.0f;
}
extern "C" DLLEXPORT float isCached(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
// find and parse tmp file
std::filesystem::path metaPath, cachePath;
web::json::value meta, cache;
readCache(metaPath, meta, cachePath, cache);
// parse parameters
#if defined(_WIN32) || defined(_WIN64)
std::filesystem::path inputGoZFileName(someText);
#else
// for macOS, zsc adds meaningless prefix
std::string tmpStr(someText);
std::filesystem::path inputGoZFileName(tmpStr.substr(2));
#endif
utility::string_t key;
{
std::string fileName = inputGoZFileName.filename().string();
key = utility::string_t(fileName.begin(), fileName.end());
}
if (cache.has_field(key))
{
return 1.0f;
}
else
{
return 0.0f;
}
}
extern "C" DLLEXPORT float getCachedRotation(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
union
{
char c[sizeof(float)];
float f;
} loader;
// find and parse tmp file
std::filesystem::path metaPath, cachePath;
web::json::value meta, cache;
readCache(metaPath, meta, cachePath, cache);
// parse parameters
#if defined(_WIN32) || defined(_WIN64)
std::filesystem::path inputGoZFileName(someText);
#else
// for macOS, zsc adds meaningless prefix
std::string tmpStr(someText);
std::filesystem::path inputGoZFileName(tmpStr.substr(2));
#endif
utility::string_t key;
{
std::string fileName = inputGoZFileName.filename().string();
key = utility::string_t(fileName.begin(), fileName.end());
}
Eigen::Matrix<double, 3, 1> eulerZYX, invEulerZYX, eulerZYX_Zb, invEulerZYX_Zb;
Eigen::Matrix<double, 1, 3> translateXYZ, invTranslateXYZ;
double extraRotX = 0;
double invExtraRotX = 0;
if (cache.has_field(key))
{
// hit
eulerZYX << cache[key][_XPLATSTR("rotZ")].as_double(), cache[key][_XPLATSTR("rotY")].as_double(), cache[key][_XPLATSTR("rotX")].as_double();
invEulerZYX << cache[key][_XPLATSTR("invRotZ")].as_double(), cache[key][_XPLATSTR("invRotY")].as_double(), cache[key][_XPLATSTR("invRotX")].as_double();
translateXYZ << cache[key][_XPLATSTR("offX")].as_double(), cache[key][_XPLATSTR("offY")].as_double(), cache[key][_XPLATSTR("offZ")].as_double();
invTranslateXYZ << cache[key][_XPLATSTR("invOffX")].as_double(), cache[key][_XPLATSTR("invOffY")].as_double(), cache[key][_XPLATSTR("invOffZ")].as_double();
extraRotX = cache[key][_XPLATSTR("extraRotX")].as_double();
invExtraRotX = cache[key][_XPLATSTR("invExtraRotX")].as_double();
}
else
{
// no hit
// calculate symmetrization
Mesh<double, int> mesh;
FromZ::readGoZFile(inputGoZFileName.string(), mesh.meshName, mesh.V, mesh.F, mesh.UV, mesh.VC, mesh.M, mesh.G);
// remove temporary file
std::filesystem::remove_all(inputGoZFileName);
Eigen::Matrix<double, 3, 3> rotMatrix;
if (calculateEulerAnglesForSymmetrize(mesh, rotMatrix, translateXYZ))
{
eulerZYX = rotMatrix.eulerAngles(2, 1, 0);
invEulerZYX.setZero();
invTranslateXYZ.setZero();
}
else
{
return 0.0f;
}
}
web::json::value update;
memcpy(loader.c, outputBuffer + sizeof(float) * 0, sizeof(float));
invExtraRotX -= static_cast<double>(loader.f);
eulerZYX_Zb = eulerZYX;
eulerZYX_Zb *= 180.0;
eulerZYX_Zb /= M_PI;
// I don't fully understand, but eulerAngles in Eigen is left-handed?
eulerZYX_Zb(0) *= -1;
invEulerZYX_Zb = invEulerZYX;
invEulerZYX_Zb *= 180.0;
invEulerZYX_Zb /= M_PI;
// I don't fully understand, but eulerAngles in Eigen is left-handed?
invEulerZYX_Zb(0) *= -1;
// pose -> symmetry
loader.f = static_cast<float>(eulerZYX_Zb(0));
memcpy(outputBuffer + sizeof(float) * 0, loader.c, sizeof(float));
loader.f = static_cast<float>(eulerZYX_Zb(1));
memcpy(outputBuffer + sizeof(float) * 1, loader.c, sizeof(float));
loader.f = static_cast<float>(eulerZYX_Zb(2));
memcpy(outputBuffer + sizeof(float) * 2, loader.c, sizeof(float));
loader.f = static_cast<float>(extraRotX);
memcpy(outputBuffer + sizeof(float) * 3, loader.c, sizeof(float));
// symmetry -> pose
loader.f = static_cast<float>(invEulerZYX_Zb(0));
memcpy(outputBuffer + sizeof(float) * 4, loader.c, sizeof(float));
loader.f = static_cast<float>(invEulerZYX_Zb(1));
memcpy(outputBuffer + sizeof(float) * 5, loader.c, sizeof(float));
loader.f = static_cast<float>(invEulerZYX_Zb(2));
memcpy(outputBuffer + sizeof(float) * 6, loader.c, sizeof(float));
loader.f = static_cast<float>(invExtraRotX);
memcpy(outputBuffer + sizeof(float) * 7, loader.c, sizeof(float));
loader.f = static_cast<float>(translateXYZ(0) + invTranslateXYZ(0));
memcpy(outputBuffer + sizeof(float) * 8, loader.c, sizeof(float));
loader.f = static_cast<float>(translateXYZ(1) + invTranslateXYZ(1));
memcpy(outputBuffer + sizeof(float) * 9, loader.c, sizeof(float));
loader.f = static_cast<float>(translateXYZ(2) + invTranslateXYZ(2));
memcpy(outputBuffer + sizeof(float) * 10, loader.c, sizeof(float));
update[_XPLATSTR("rotZ")] = web::json::value(-invEulerZYX(0));
update[_XPLATSTR("rotY")] = web::json::value(-invEulerZYX(1));
update[_XPLATSTR("rotX")] = web::json::value(-invEulerZYX(2));
update[_XPLATSTR("extraRotX")] = web::json::value(-invExtraRotX);
update[_XPLATSTR("invRotZ")] = web::json::value(-eulerZYX(0));
update[_XPLATSTR("invRotY")] = web::json::value(-eulerZYX(1));
update[_XPLATSTR("invRotX")] = web::json::value(-eulerZYX(2));
update[_XPLATSTR("invExtraRotX")] = web::json::value(-extraRotX);
update[_XPLATSTR("offX")] = web::json::value(-invTranslateXYZ(0));
update[_XPLATSTR("offY")] = web::json::value(-invTranslateXYZ(1));
update[_XPLATSTR("offZ")] = web::json::value(-invTranslateXYZ(2));
update[_XPLATSTR("invOffX")] = web::json::value(-translateXYZ(0));
update[_XPLATSTR("invOffY")] = web::json::value(-translateXYZ(1));
update[_XPLATSTR("invOffZ")] = web::json::value(-translateXYZ(2));
// write json to cache file.
cache[key] = update;
std::ofstream cacheOut(cachePath);
cache.serialize(cacheOut);
cacheOut.close();
return 1.0f;
}
extern "C" DLLEXPORT float eraseCachedRotation(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
// find and parse tmp file
std::filesystem::path metaPath, cachePath;
web::json::value meta, cache;
readCache(metaPath, meta, cachePath, cache);
// parse parameters
#if defined(_WIN32) || defined(_WIN64)
std::filesystem::path inputGoZFileName(someText);
#else
// for macOS, zsc adds meaningless prefix
std::string tmpStr(someText);
std::filesystem::path inputGoZFileName(tmpStr.substr(2));
#endif
utility::string_t key;
{
std::string fileName = inputGoZFileName.filename().string();
key = utility::string_t(fileName.begin(), fileName.end());
}
if (cache.has_field(key))
{
cache.erase(key);
std::ofstream cacheOut(cachePath);
cache.serialize(cacheOut);
cacheOut.close();
return 1.0f;
}
else
{
return 0.0f;
}
}
extern "C" DLLEXPORT float loadCacheFile(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
std::filesystem::path metaPath, cachePath, cachePath_;
web::json::value meta, meta_, cache, cache_;
readCache(metaPath, meta, cachePath, cache);
meta_ = meta;
// parse parameters
#if defined(_WIN32) || defined(_WIN64)
std::filesystem::path cacheFileName(someText);
#else
// for macOS, zsc adds meaningless prefix
std::string tmpStr(someText);
std::filesystem::path cacheFileName(tmpStr.substr(2));
#endif
std::string pStr = cacheFileName.string();
utility::string_t v(pStr.begin(), pStr.end());
meta_[_XPLATSTR("path")] = web::json::value(v);
// temporary update path for cache
{
std::ofstream metaOut(metaPath);
meta_.serialize(metaOut);
metaOut.close();
}
readCache(metaPath, meta_, cachePath_, cache_);
// write current cache to new file
{
std::ofstream metaOut(metaPath);
meta.serialize(metaOut);
metaOut.close();
}
{
std::ofstream cacheOut(cachePath);
cache_.serialize(cacheOut);
cacheOut.close();
}
return 1.0f;
}
extern "C" DLLEXPORT float selectCacheFile(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
std::filesystem::path metaPath, cachePath;
web::json::value meta, cache;
readCache(metaPath, meta, cachePath, cache);
// std::filesystem::remove_all(cachePath);
if (optValue > 0)
{
// switch to default
// erase current json
std::filesystem::remove_all(metaPath);
web::json::value cache_;
// readCache automatically create cache file in default path
readCache(metaPath, meta, cachePath, cache_);
// write current cache to new file
std::ofstream cacheOut(cachePath);
cache.serialize(cacheOut);
cacheOut.close();
}
else
{
// parse parameters
#if defined(_WIN32) || defined(_WIN64)
std::filesystem::path cacheFileName(someText);
#else
// for macOS, zsc adds meaningless prefix
std::string tmpStr(someText);
std::filesystem::path cacheFileName(tmpStr.substr(2));
#endif
std::string pStr = cacheFileName.string();
utility::string_t v(pStr.begin(), pStr.end());
meta[_XPLATSTR("path")] = web::json::value(v);
// write current meta to new file
std::ofstream metaOut(metaPath);
meta.serialize(metaOut);
metaOut.close();
// write current cache to new file
std::ofstream cacheOut(cacheFileName);
cache.serialize(cacheOut);
cacheOut.close();
}
return 1.0f;
}