PeriDyno 1.2.1
Loading...
Searching...
No Matches
JointInfo.h
Go to the documentation of this file.
1
2#pragma once
3#include <vector>
4#include <memory>
5#include <string>
6#include "Vector.h"
7#include "OBase.h"
8#include "Module.h"
9
10
11namespace dyno {
12
13 std::vector<int> getJointDirByJointIndex(int Index, std::map<int, std::vector<int>> jointId_joint_Dir);
14
15
16 struct Pose
17 {
18 Pose() {}
19
20 Pose(std::vector<Vec3f> t, std::vector<Vec3f> s, std::vector<Quat1f> r)
21 {
22 mTranslation = t;
23 mQuatRotation = r;
24 mScale = s;
25 }
26
28 {
29 mTranslation.clear();
30 mScale.clear();
31 mQuatRotation.clear();
32 }
33
34 int size() { return mTranslation.size(); }
35
36 void resize(int s)
37 {
38 mTranslation.resize(s);
39 mQuatRotation.resize(s);
40 mScale.resize(s);
41 };
42
43 Pose& operator=(const Pose& other) {
44 if (this != &other) {
47 mScale = other.mScale;
48 }
49 return *this;
50 }
51
52 std::vector<Vec3f> mTranslation;
53 std::vector<Quat1f> mQuatRotation;
54 std::vector<Vec3f> mScale;
55 };
56
57 class JointInfo : public OBase
58 {
59 typedef int joint;
60
61 public:
62
64
66 DArray<Mat4f>& InverseBindMatrix,
67 DArray<Mat4f>& LocalMatrix,
68 DArray<Mat4f>& WorldMatrix,
69 std::vector<int>& allJoints,
70 std::map<joint, std::vector<joint>>& jointDir,
71 std::map<joint, Vec3f>& bindPoseTranslation,
72 std::map<joint, Vec3f>& bindPoseScale,
73 std::map<joint, Quat1f>& bindPoseRotation
74 );
75
77
79 DArray<Mat4f>& InverseBindMatrix,
80 std::vector<Mat4f>& LocalMatrix,
81 DArray<Mat4f>& WorldMatrix,
82 std::vector<int>& allJoints,
83 std::map<joint, std::vector<joint>>& jointDir,
84 std::map<joint, Vec3f>& bindPoseTranslation,
85 std::map<joint, Vec3f>& bindPoseScale,
86 std::map<joint, Quat1f>& bindPoseRotation
87 );
88
89 void clear()
90 {
91 mJointName.clear();
92
93
95 mBindPoseScale.clear();
96 mBindPoseRotation.clear();
97 mBindPoseRotator.clear();
98
99 mAllJoints.clear();
100 mJointDir.clear();
101
102 mMaxJointID = -1;
103 }
104
106 std::vector<Mat4f>& InverseBindMatrix,
107 std::vector<Mat4f>& LocalMatrix,
108 std::vector<Mat4f>& WorldMatrix,
109 std::vector<int>& allJoints,
110 std::map<joint, std::vector<joint>>& jointDir,
111 std::map<joint, Vec3f>& bindPoseTranslation,
112 std::map<joint, Vec3f>& bindPoseScale,
113 std::map<joint, Vec3f>& bindPoseRotation,
114 std::map<joint, Vec3f>& bindPosePreRotation
115 );
116
117
118 void setJoint(const JointInfo& j);
119
120 bool isEmpty();
121
123
124
125 void setJointName(const std::map<int,std::string> name) { this->mJointName = name; }
126
127 void setLeftHandedCoordSystem(bool islLeft) { useLeftHandedCoordSystem = islLeft; };
128
129 void setPose(Pose pose)
130 {
131 this->currentPose = pose;
133 };
134
135
136 int findJointIndexByName(const std::string& value)
137 {
138 for (const auto& pair : mJointName)
139 {
140 if (pair.second == value)
141 {
142 return pair.first; // 找到返回 key
143 }
144 }
145 return -1; // 未找到
146 }
147
148 std::vector<Mat4f> getLocalMatrix(Pose& pose)
149 {
150 std::vector<Mat4f> localMatrix(this->mMaxJointID + 1);
151
152 auto translation = pose.mTranslation;
153 auto rotation = pose.mQuatRotation;
154 auto scale = pose.mScale;
155
156 for (size_t i = 0; i < mAllJoints.size(); i++)
157 {
158 int joint = mAllJoints[i];
159
160 Mat4f r = rotation[joint].toMatrix4x4();
161 Mat4f s = Mat4f
162 (scale[joint][0], 0, 0, 0,
163 0, scale[joint][1], 0, 0,
164 0, 0, scale[joint][2], 0,
165 0, 0, 0, 1
166 );
167 Mat4f t = Mat4f
168 (1, 0, 0, translation[joint][0],
169 0, 1, 0, translation[joint][1],
170 0, 0, 1, translation[joint][2],
171 0, 0, 0, 1
172 );
173 localMatrix[joint] = t * s * r;
174
175 Mat4f c = localMatrix[joint];
176 }
177
178 return localMatrix;
179 }
180
181 public:
182
183 std::map<int, std::string> mJointName;
184
186 std::vector<Mat4f> mJointLocalMatrix;
188
189
190
192
193 std::vector<Vec3f> mBindPoseTranslation;
194 std::vector<Vec3f> mBindPoseScale;
195 std::vector<Quat<Real>> mBindPoseRotation;
196 std::vector<Vec3f> mBindPoseRotator;
197
198 std::vector<Vec3f> mBindPosePreRotator;
199 //动画:
200
201 std::vector<joint> mAllJoints;
202 std::map<joint, std::vector<joint>> mJointDir;
203
204 int mMaxJointID = -1;
205
206 private:
208 };
209
210
212 {
213 typedef int joint;
214
215 public:
216
218
220
222 std::map<joint, std::vector<Vec3f>>& jointTranslation,
223 std::map<joint, std::vector<Real>>& jointTimeCodeTranslation,
224 std::map<joint, std::vector<Vec3f>>& jointScale,
225 std::map<joint, std::vector<Real>>& jointIndexTimeCodeScale,
226 std::map<joint, std::vector<Quat1f>>& jointRotation,
227 std::map<joint, std::vector<Real>>& jointIndexRotation,
228 std::shared_ptr<JointInfo> skeleton,
229 bool loop = true
230 );
231
232
233 void clear()
234 {
235 mSkeleton = NULL;
236
239
240 mJoint_KeyId_T_X.clear();
241 mJoint_KeyId_tT_X.clear();
242 mJoint_KeyId_T_Y.clear();
243 mJoint_KeyId_tT_Y.clear();
244 mJoint_KeyId_T_Z.clear();
245 mJoint_KeyId_tT_Z.clear();
246
247 mJoint_KeyId_R_X.clear();
248 mJoint_KeyId_tR_X.clear();
249 mJoint_KeyId_R_Y.clear();
250 mJoint_KeyId_tR_Y.clear();
251 mJoint_KeyId_R_Z.clear();
252 mJoint_KeyId_tR_Z.clear();
253
254 mJoint_KeyId_S_X.clear();
255 mJoint_KeyId_tS_X.clear();
256 mJoint_KeyId_S_Y.clear();
257 mJoint_KeyId_tS_Y.clear();
258 mJoint_KeyId_S_Z.clear();
259 mJoint_KeyId_tS_Z.clear();
260
261 mTranslation.clear();
262 mScale.clear();
263 mQuatRotation.clear();
264 mRotator.clear();
265
266 mJointWorldMatrix.clear();
267
268 mTotalTime = 0;
269 currentTime = -1;
270 mLoop = true;
271 mBlendInTime = 0.0f;
272 mBlendOutTime = 0.0f;
273 mPlayRate = 1.0f;
274
275 mAnimationTime = 0.0f;
276 }
277
278 bool isValid()
279 {
280 return mTotalTime > 0;
281 }
283 {
284 return mJoint_KeyId_QuatRotation.size();
285 }
286
287 void resizeJointsData(int size)
288 {
289 mTranslation.resize(size);
290 mScale.resize(size);
291 mQuatRotation.resize(size);
292 mRotator.resize(size);
293 }
294
295
296
297 std::vector<Vec3f> getJointsTranslation(float time);
298
299 std::vector<Quat1f> getJointsRotation(float time);
300
301 std::vector<Vec3f> getJointsScale(float time);
302
303 float getTotalTime() { return mTotalTime; }
304
305 int findMaxSmallerIndex(const std::vector<float>& arr, float v);
306
307 Vec3f lerp(Vec3f v0, Vec3f v1, float weight);
308
310
311 Quat<Real> slerp(const Quat<Real>& q1, const Quat<Real>& q2, float weight);
312 Real lerp(Real v0, Real v1, float weight);
313
314 Quat<Real> nlerp(const Quat<Real>& q1, const Quat<Real>& q2, float weight);
315
316 std::vector<int> getJointDir(int Index, std::map<int, std::vector<int>> joint_Dir);
317
318 void setLoop(bool loop) { mLoop = loop; }
319
320 Pose getPose(float inTime);
321
322 void updateAnimationPose(float inTime){
323
324 auto pose = this->getPose(inTime);
325 mSkeleton->setPose(pose);
326
327 };
328
330
331 float& getBlendInTime() { return mBlendInTime; }
332
333 float& getBlendOutTime() { return mBlendOutTime; }
334
335 float& getPlayRate() { return mPlayRate; }
336
338
339 std::shared_ptr<JointInfo>& getSkeleton() { return mSkeleton; }
340
341 void setSkeleton(std::shared_ptr<JointInfo> sk) { mSkeleton = sk; }
342
343 private:
344
345 void updateJointsTransform(float time);
347
348 Real calculateMinTime(const std::map<joint, std::vector<Real>>& timeCodes);
349 Real calculateMaxTime(const std::map<joint, std::vector<Real>>& timeCodes);
350
351 void offsetTimeCodes(std::map<joint, std::vector<Real>>& timeCodes, Real offset) {
352 for (auto& it : timeCodes) {
353 for (auto& time : it.second) {
354 time -= offset;
355 }
356 }
357 }
358
359 void updateGLTFRotation(int select, Real time)
360 {
361 auto iterGLTFR = mJoint_KeyId_QuatRotation.find(select);
362 if (iterGLTFR == mJoint_KeyId_QuatRotation.end())
363 mQuatRotation[select] = mSkeleton->mBindPoseRotation[select];
364 {
365 //Rotation
366 if (iterGLTFR != mJoint_KeyId_QuatRotation.end())
367 {
368 const std::vector<Quat1f>& all_R = mJoint_KeyId_QuatRotation[select];
369 const std::vector<Real>& tTimeCode = mJoint_KeyId_tQuatRotation[select];
370
371 int tId = findMaxSmallerIndex(tTimeCode, time);
372
373 if (tId >= all_R.size() - 1) // [size-1]<=[tId]
374 {
375 mQuatRotation[select] = all_R[all_R.size() - 1];
376 }
377 else
378 {
379 float weight = (time - tTimeCode[tId]) / (tTimeCode[tId + 1] - tTimeCode[tId]);
380 mQuatRotation[select] = nlerp(all_R[tId], all_R[tId + 1], weight);
381 }
382 }
383 }
384 }
385
386
387 void updateCurrentPose(int select,Real time, std::vector<Vec3f>& currentData,std::vector<Vec3f>& bindPoseData,std::map<joint, std::vector<Real>>& animationData,std::map<joint, std::vector<Real>>& TimeCode,int channel)
388 {
389
390 auto iter = animationData.find(select);
391 if (iter == animationData.end())
392 currentData[select][channel] = bindPoseData[select][channel];
393
394 if (iter != animationData.end())
395 {
396 const std::vector<Real>& all_R = animationData[select];
397 const std::vector<Real>& tTimeCode = TimeCode[select];
398
399 int tId = findMaxSmallerIndex(tTimeCode, time);
400
401 if (tId >= all_R.size() - 1) // [size-1]<=[tId]
402 {
403 currentData[select][channel] = all_R[all_R.size() - 1];
404 }
405 else
406 {
407 float weight = (time - tTimeCode[tId]) / (tTimeCode[tId + 1] - tTimeCode[tId]);
408 currentData[select][channel] = lerp(all_R[tId], all_R[tId + 1], weight);
409 }
410 }
411
412 }
413
414 public:
415
416 std::shared_ptr<JointInfo> mSkeleton = NULL;
417
418 //Only Gltf
419 std::map<joint, std::vector<Quat1f>> mJoint_KeyId_QuatRotation;
420 std::map<joint, std::vector<Real>> mJoint_KeyId_tQuatRotation;
421
422 //fbx Translation
423 std::map<joint, std::vector<Real>> mJoint_KeyId_T_X;
424 std::map<joint, std::vector<Real>> mJoint_KeyId_tT_X;
425 std::map<joint, std::vector<Real>> mJoint_KeyId_T_Y;
426 std::map<joint, std::vector<Real>> mJoint_KeyId_tT_Y;
427 std::map<joint, std::vector<Real>> mJoint_KeyId_T_Z;
428 std::map<joint, std::vector<Real>> mJoint_KeyId_tT_Z;
429 //fbx Rotation
430 std::map<joint, std::vector<Real>> mJoint_KeyId_R_X;
431 std::map<joint, std::vector<Real>> mJoint_KeyId_tR_X;
432 std::map<joint, std::vector<Real>> mJoint_KeyId_R_Y;
433 std::map<joint, std::vector<Real>> mJoint_KeyId_tR_Y;
434 std::map<joint, std::vector<Real>> mJoint_KeyId_R_Z;
435 std::map<joint, std::vector<Real>> mJoint_KeyId_tR_Z;
436 //fbx Scale
437 std::map<joint, std::vector<Real>> mJoint_KeyId_S_X;
438 std::map<joint, std::vector<Real>> mJoint_KeyId_tS_X;
439 std::map<joint, std::vector<Real>> mJoint_KeyId_S_Y;
440 std::map<joint, std::vector<Real>> mJoint_KeyId_tS_Y;
441 std::map<joint, std::vector<Real>> mJoint_KeyId_S_Z;
442 std::map<joint, std::vector<Real>> mJoint_KeyId_tS_Z;
443
444 private:
445 //当前时间下的动画数据,在某些情况下仅记录三维软件中具有动画变化的骨骼
446 std::vector<Vec3f> mTranslation;
447 std::vector<Vec3f> mScale;
448 std::vector<Quat1f> mQuatRotation;
449 std::vector<Vec3f> mRotator;
450
452
454
456
457 bool mLoop = true;
461
463
464 };
465
466
467
468
469
470
471}
472
double Real
Definition Typedef.inl:23
int findMaxSmallerIndex(const std::vector< float > &arr, float v)
void offsetTimeCodes(std::map< joint, std::vector< Real > > &timeCodes, Real offset)
Definition JointInfo.h:351
std::vector< Vec3f > getJointsScale(float time)
Quat< Real > nlerp(const Quat< Real > &q1, const Quat< Real > &q2, float weight)
std::vector< Vec3f > mScale
Definition JointInfo.h:447
std::map< joint, std::vector< Real > > mJoint_KeyId_R_Y
Definition JointInfo.h:432
std::map< joint, std::vector< Real > > mJoint_KeyId_R_Z
Definition JointInfo.h:434
void setLoop(bool loop)
Definition JointInfo.h:318
std::map< joint, std::vector< Real > > mJoint_KeyId_T_Z
Definition JointInfo.h:427
std::map< joint, std::vector< Real > > mJoint_KeyId_tS_X
Definition JointInfo.h:438
std::vector< Quat1f > getJointsRotation(float time)
std::vector< int > getJointDir(int Index, std::map< int, std::vector< int > > joint_Dir)
std::map< joint, std::vector< Real > > mJoint_KeyId_tT_Z
Definition JointInfo.h:428
void setGLTFAnimationData(std::map< joint, std::vector< Vec3f > > &jointTranslation, std::map< joint, std::vector< Real > > &jointTimeCodeTranslation, std::map< joint, std::vector< Vec3f > > &jointScale, std::map< joint, std::vector< Real > > &jointIndexTimeCodeScale, std::map< joint, std::vector< Quat1f > > &jointRotation, std::map< joint, std::vector< Real > > &jointIndexRotation, std::shared_ptr< JointInfo > skeleton, bool loop=true)
Quat< Real > slerp(const Quat< Real > &q1, const Quat< Real > &q2, float weight)
void updateCurrentPose(int select, Real time, std::vector< Vec3f > &currentData, std::vector< Vec3f > &bindPoseData, std::map< joint, std::vector< Real > > &animationData, std::map< joint, std::vector< Real > > &TimeCode, int channel)
Definition JointInfo.h:387
std::map< joint, std::vector< Real > > mJoint_KeyId_T_Y
Definition JointInfo.h:425
std::vector< Vec3f > getJointsTranslation(float time)
void updateJointsTransform(float time)
std::map< joint, std::vector< Real > > mJoint_KeyId_R_X
Definition JointInfo.h:430
std::map< joint, std::vector< Real > > mJoint_KeyId_S_Y
Definition JointInfo.h:439
std::map< joint, std::vector< Real > > mJoint_KeyId_tS_Z
Definition JointInfo.h:442
std::map< joint, std::vector< Real > > mJoint_KeyId_tQuatRotation
Definition JointInfo.h:420
Real calculateMinTime(const std::map< joint, std::vector< Real > > &timeCodes)
std::map< joint, std::vector< Real > > mJoint_KeyId_S_Z
Definition JointInfo.h:441
std::shared_ptr< JointInfo > mSkeleton
Definition JointInfo.h:416
std::shared_ptr< JointInfo > & getSkeleton()
Definition JointInfo.h:339
void updateAnimationPose(float inTime)
Definition JointInfo.h:322
std::map< joint, std::vector< Real > > mJoint_KeyId_tT_Y
Definition JointInfo.h:426
Pose getPose(float inTime)
std::map< joint, std::vector< Real > > mJoint_KeyId_tR_X
Definition JointInfo.h:431
std::map< joint, std::vector< Real > > mJoint_KeyId_tS_Y
Definition JointInfo.h:440
std::map< joint, std::vector< Real > > mJoint_KeyId_T_X
Definition JointInfo.h:423
void setSkeleton(std::shared_ptr< JointInfo > sk)
Definition JointInfo.h:341
Transform3f updateTransform(joint jId, float time)
void resizeJointsData(int size)
Definition JointInfo.h:287
std::map< joint, std::vector< Real > > mJoint_KeyId_tR_Z
Definition JointInfo.h:435
Vec3f lerp(Vec3f v0, Vec3f v1, float weight)
Real lerp(Real v0, Real v1, float weight)
DArray< Mat4f > mJointWorldMatrix
Definition JointInfo.h:451
std::map< joint, std::vector< Quat1f > > mJoint_KeyId_QuatRotation
Definition JointInfo.h:419
Real calculateMaxTime(const std::map< joint, std::vector< Real > > &timeCodes)
std::vector< Vec3f > mTranslation
Definition JointInfo.h:446
std::map< joint, std::vector< Real > > mJoint_KeyId_S_X
Definition JointInfo.h:437
std::vector< Vec3f > mRotator
Definition JointInfo.h:449
Quat< Real > normalize(const Quat< Real > &q)
std::vector< Quat1f > mQuatRotation
Definition JointInfo.h:448
std::map< joint, std::vector< Real > > mJoint_KeyId_tR_Y
Definition JointInfo.h:433
void updateGLTFRotation(int select, Real time)
Definition JointInfo.h:359
std::map< joint, std::vector< Real > > mJoint_KeyId_tT_X
Definition JointInfo.h:424
void setLeftHandedCoordSystem(bool islLeft)
Definition JointInfo.h:127
void updateWorldMatrixByTransform()
DArray< Mat4f > mJointInverseBindMatrix
Definition JointInfo.h:185
std::vector< Mat4f > mJointLocalMatrix
Definition JointInfo.h:186
std::vector< Vec3f > mBindPosePreRotator
Definition JointInfo.h:198
std::vector< Mat4f > getLocalMatrix(Pose &pose)
Definition JointInfo.h:148
void setJoint(const JointInfo &j)
void setGltfJointInfo(DArray< Mat4f > &InverseBindMatrix, std::vector< Mat4f > &LocalMatrix, DArray< Mat4f > &WorldMatrix, std::vector< int > &allJoints, std::map< joint, std::vector< joint > > &jointDir, std::map< joint, Vec3f > &bindPoseTranslation, std::map< joint, Vec3f > &bindPoseScale, std::map< joint, Quat1f > &bindPoseRotation)
std::map< joint, std::vector< joint > > mJointDir
Definition JointInfo.h:202
std::vector< Vec3f > mBindPoseScale
Definition JointInfo.h:194
std::vector< joint > mAllJoints
Definition JointInfo.h:201
DArray< Mat4f > mJointWorldMatrix
Definition JointInfo.h:187
std::vector< Quat< Real > > mBindPoseRotation
Definition JointInfo.h:195
void setPose(Pose pose)
Definition JointInfo.h:129
std::map< int, std::string > mJointName
Definition JointInfo.h:183
JointInfo(DArray< Mat4f > &InverseBindMatrix, DArray< Mat4f > &LocalMatrix, DArray< Mat4f > &WorldMatrix, std::vector< int > &allJoints, std::map< joint, std::vector< joint > > &jointDir, std::map< joint, Vec3f > &bindPoseTranslation, std::map< joint, Vec3f > &bindPoseScale, std::map< joint, Quat1f > &bindPoseRotation)
bool useLeftHandedCoordSystem
Definition JointInfo.h:207
std::vector< Vec3f > mBindPoseRotator
Definition JointInfo.h:196
int findJointIndexByName(const std::string &value)
Definition JointInfo.h:136
std::vector< Vec3f > mBindPoseTranslation
Definition JointInfo.h:193
void SetJointInfo(std::vector< Mat4f > &InverseBindMatrix, std::vector< Mat4f > &LocalMatrix, std::vector< Mat4f > &WorldMatrix, std::vector< int > &allJoints, std::map< joint, std::vector< joint > > &jointDir, std::map< joint, Vec3f > &bindPoseTranslation, std::map< joint, Vec3f > &bindPoseScale, std::map< joint, Vec3f > &bindPoseRotation, std::map< joint, Vec3f > &bindPosePreRotation)
void setJointName(const std::map< int, std::string > name)
Definition JointInfo.h:125
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array< T, DeviceType::GPU > DArray
Definition Array.inl:89
Transform< float, 3 > Transform3f
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
std::vector< int > getJointDirByJointIndex(int Index, std::map< int, std::vector< int > > jointId_joint_Dir)
SquareMatrix< float, 4 > Mat4f
Definition Matrix4x4.h:85
Pose(std::vector< Vec3f > t, std::vector< Vec3f > s, std::vector< Quat1f > r)
Definition JointInfo.h:20
std::vector< Vec3f > mScale
Definition JointInfo.h:54
int size()
Definition JointInfo.h:34
void resize(int s)
Definition JointInfo.h:36
std::vector< Quat1f > mQuatRotation
Definition JointInfo.h:53
Pose & operator=(const Pose &other)
Definition JointInfo.h:43
std::vector< Vec3f > mTranslation
Definition JointInfo.h:52