forked from TomohikoMukai/SrtRbfNode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoseVariable.h
392 lines (376 loc) · 10.3 KB
/
PoseVariable.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
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
#ifndef POSE_VARIABLE_H
#define POSE_VARIABLE_H
#pragma once
#include <maya/MVector.h>
#include <maya/MQuaternion.h>
#include <maya/MTransformationMatrix.h>
#include <vector>
#include <maya/MGlobal.h>
#include <maya/MMatrix.h>
struct PoseVariable
{
MVector scale;
MQuaternion rotate;
MVector translate;
PoseVariable()
: scale(1, 1, 1),
rotate(0, 0, 0, 1),
translate(0, 0, 0)
{
}
PoseVariable(
const PoseVariable& src)
: scale(src.scale),
rotate(src.rotate),
translate(src.translate)
{
}
PoseVariable&
operator =(
const PoseVariable& src)
{
scale = src.scale;
rotate = src.rotate;
translate = src.translate;
return *this;
}
static PoseVariable
fromMatrix(
const MTransformationMatrix& tm)
{
PoseVariable c;
double sv[3];
tm.getScale(sv, MSpace::kTransform);
c.scale = MVector(sv[0], sv[1], sv[2]);
tm.getRotationQuaternion(c.rotate.x, c.rotate.y, c.rotate.z, c.rotate.w);
c.translate = tm.getTranslation(MSpace::kTransform);
c.truncateEpsilon();
return c;
}
static MMatrix
toMatrix(
const PoseVariable& pv)
{
MTransformationMatrix tm = MTransformationMatrix::identity;
double sv[3] = { pv.scale.x, pv.scale.y, pv.scale.z };
tm.setScale(sv, MSpace::kTransform);
tm.setRotationQuaternion(pv.rotate.x, pv.rotate.y, pv.rotate.z, pv.rotate.w);
tm.setTranslation(pv.translate, MSpace::kTransform);
return tm.asMatrix();
}
public:
PoseVariable&
ontoHemisphere(
MQuaternion pole = MQuaternion::identity)
{
if (qdot(pole, rotate) < 0)
{
rotate = -rotate;
}
else if (rotate.x == -1 || rotate.y == -1 || rotate.z == -1)
{
rotate = -rotate;
}
return *this;
}
PoseVariable&
truncateEpsilon(
double epsilon = 1.0e-9)
{
for (int i = 0; i < 4; ++i)
{
rotate[i] = std::abs(rotate[i]) < epsilon ? 0 : rotate[i];
}
return *this;
}
public:
static double
qdot(
MQuaternion a,
MQuaternion b)
{
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
static double
vdot(
MVector a,
MVector b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static MQuaternion
qlndiff(
MQuaternion a,
MQuaternion b)
{
MQuaternion qd = a.conjugate() * b;
return qd.log();
}
static double
lqdistsq(
MQuaternion a,
MQuaternion b)
{
MQuaternion qd = a.log() - b.log();
return qdot(qd, qd);
}
static double
qangle(
MQuaternion a,
MQuaternion b)
{
return 2.0 * std::acos(qdot(a, b));
}
static double
qangleShortest(
MQuaternion a,
MQuaternion b)
{
return 2.0 * std::acos(std::min(std::abs(qdot(a, b)), 1.0));
}
static double
linear(
double d,
double p)
{
return d;
}
static double
thinplate(
double d,
double p)
{
if (std::abs(d) < 1.0e-6)
{
return 0;
}
return std::pow(d, 2) * std::log(d);
}
static double
gaussian(
double d,
double p)
{
return std::exp(-d * d / p);
}
static double
dissimilarity(
const std::vector<PoseVariable>& a,
const std::vector<PoseVariable>& b,
int distType = 0,
double ws = 1.0,
double wr = 1.0,
double wt = 1.0)
{
if (distType == 3) //Frobenious norm of diff matrix
{
double fnrm = 0.0;
for (int i = 0; i < a.size(); ++i)
{
MMatrix dm = toMatrix(a[i]) - toMatrix(b[i]);
for (int j = 0; j < 16; ++j)
{
fnrm += std::pow(dm(j / 4, j % 4), 2.0);
}
}
return fnrm <= 0 ? 0.0 : std::sqrt(fnrm);
}
double sqe = 0.0; // weighted Euclidean norm
for (int i = 0; i < a.size(); ++i)
{
MVector sv = a[i].scale - b[i].scale;
MVector tv = a[i].translate - b[i].translate;
double dssq = vdot(sv, sv);
double dtsq = vdot(tv, tv);
sqe += ws * dssq + wt * dtsq;
}
switch (distType)
{
case 1: // Euclidean distance in tangent vector space
for (int i = 0; i < a.size(); ++i)
{
double drsq = PoseVariable::lqdistsq(a[i].rotate, b[i].rotate);
sqe += wr * drsq;
}
break;
case 2: // Shortest angle
for (int i = 0; i < a.size(); ++i)
{
double dr = PoseVariable::qangleShortest(a[i].rotate, b[i].rotate);
sqe += wr * dr * dr;
}
break;
case 0: // Angle on 3-hemisphere
default:
for (int i = 0; i < a.size(); ++i)
{
double dr = PoseVariable::qangle(a[i].rotate, b[i].rotate);
sqe += wr * dr * dr;
}
break;
}
return std::sqrt(sqe);
}
static double
kernel(
const std::vector<PoseVariable>& a,
const std::vector<PoseVariable>& b,
int rbfType = 1,
int distType = 0,
double ws = 1.0,
double wr = 10.0,
double wt = 1.0)
{
static std::function<double(double, double)> ftable[3] = {
linear, thinplate, gaussian };
double d = dissimilarity(a, b, distType, ws, wr, wt);
return ftable[rbfType](d, 10.0);
}
public:
static void
setPoseTo(
MPlug& plug,
int offset,
const PoseVariable& pose)
{
offset *= 10;
plug.elementByLogicalIndex(offset + 0).setValue(pose.scale.x);
plug.elementByLogicalIndex(offset + 1).setValue(pose.scale.y);
plug.elementByLogicalIndex(offset + 2).setValue(pose.scale.z);
plug.elementByLogicalIndex(offset + 3).setValue(pose.rotate.x);
plug.elementByLogicalIndex(offset + 4).setValue(pose.rotate.y);
plug.elementByLogicalIndex(offset + 5).setValue(pose.rotate.z);
plug.elementByLogicalIndex(offset + 6).setValue(pose.rotate.w);
plug.elementByLogicalIndex(offset + 7).setValue(pose.translate.x);
plug.elementByLogicalIndex(offset + 8).setValue(pose.translate.y);
plug.elementByLogicalIndex(offset + 9).setValue(pose.translate.z);
}
static void
setPoseTo(
MPlug& plug,
int eid,
int iid,
int numInputs,
const PoseVariable& pose)
{
setPoseTo(plug, eid * numInputs + iid, pose);
}
static void
setPosesTo(
MPlug& plug,
int eid,
int numInputs,
const std::vector<PoseVariable>& poses)
{
for (int iid = 0; iid < numInputs; ++iid)
{
setPoseTo(plug, eid * numInputs + iid, poses[iid]);
}
}
static PoseVariable
getPoseFrom(
MPlug& plug,
int offset)
{
offset *= 10;
PoseVariable pose;
pose.scale.x = plug.elementByLogicalIndex(offset + 0).asDouble();
pose.scale.y = plug.elementByLogicalIndex(offset + 1).asDouble();
pose.scale.z = plug.elementByLogicalIndex(offset + 2).asDouble();
pose.rotate.x = plug.elementByLogicalIndex(offset + 3).asDouble();
pose.rotate.y = plug.elementByLogicalIndex(offset + 4).asDouble();
pose.rotate.z = plug.elementByLogicalIndex(offset + 5).asDouble();
pose.rotate.w = plug.elementByLogicalIndex(offset + 6).asDouble();
pose.translate.x = plug.elementByLogicalIndex(offset + 7).asDouble();
pose.translate.y = plug.elementByLogicalIndex(offset + 8).asDouble();
pose.translate.z = plug.elementByLogicalIndex(offset + 9).asDouble();
return pose;
}
static PoseVariable
getPoseFrom(
MPlug& plug,
int eid,
int iid,
int numInputs)
{
return getPoseFrom(plug, eid * numInputs + iid);
}
static std::vector<PoseVariable>
getPosesFrom(
MPlug& plug,
int eid,
int numInputs)
{
std::vector<PoseVariable> retval;
for (int iid = 0; iid < numInputs; ++iid)
{
retval.push_back(getPoseFrom(plug, eid, iid, numInputs));
}
return retval;
}
static MVector
getScaleFrom(
MPlug& plug,
int offset)
{
offset *= 10;
MVector scale;
scale.x = plug.elementByLogicalIndex(offset + 0).asDouble();
scale.y = plug.elementByLogicalIndex(offset + 1).asDouble();
scale.z = plug.elementByLogicalIndex(offset + 2).asDouble();
return scale;
}
static MVector
getScaleFrom(
MPlug& plug,
int eid,
int iid,
int numInputs)
{
return getScaleFrom(plug, eid * numInputs + iid);
}
static MQuaternion
getRotateFrom(
MPlug& plug,
int offset)
{
offset *= 10;
MQuaternion rotate;
rotate.x = plug.elementByLogicalIndex(offset + 3).asDouble();
rotate.y = plug.elementByLogicalIndex(offset + 4).asDouble();
rotate.z = plug.elementByLogicalIndex(offset + 5).asDouble();
rotate.w = plug.elementByLogicalIndex(offset + 6).asDouble();
return rotate;
}
static MQuaternion
getRotateFrom(
MPlug& plug,
int eid,
int iid,
int numInputs)
{
return getRotateFrom(plug, eid * numInputs + iid);
}
static MVector
getTranslateFrom(
MPlug& plug,
int offset)
{
offset *= 10;
MVector translate;
translate.x = plug.elementByLogicalIndex(offset + 7).asDouble();
translate.y = plug.elementByLogicalIndex(offset + 8).asDouble();
translate.z = plug.elementByLogicalIndex(offset + 9).asDouble();
return translate;
}
static MVector
getTranslateFrom(
MPlug& plug,
int eid,
int iid,
int numInputs)
{
return getTranslateFrom(plug, eid * numInputs + iid);
}
};
#endif //POSE_VARIABLE_H