PeriDyno 1.2.1
Loading...
Searching...
No Matches
AnimationDriver.cpp
Go to the documentation of this file.
1#include "AnimationDriver.h"
2
3namespace dyno
4{
6
7 template<typename TDataType>
10 {
11 this->varCacheEvent()->setValue(false);
12 }
13
14 template<typename TDataType>
16 {
17 auto AnimationInfo = this->inJointAnimationInfo()->constDataPtr();
18 auto skeleton = AnimationInfo->getSkeleton();
19 if (!skeleton)
20 {
21 printf("Error : No Skeleton \n");
22 }
23
24 float inputTimeEnd = AnimationInfo->getTotalTime();
25
26 auto topo = TypeInfo::cast<DiscreteElements<DataType3f>>(this->inTopology()->getDataPtr());
27
28 auto& d_hinge = topo->hingeJoints();
29 CArray<HingeJoint> c_hinge;
30 c_hinge.assign(d_hinge);
31
32 std::map<int, std::vector<std::vector<Real>*>> animRot;
33 std::map<int, std::vector<std::vector<Real>*>> animTime;
34
35
36 auto binding = this->varBindingConfiguration()->getValue();
37
38
39 for (size_t i = 0; i < binding.size(); i++)
40 {
41
42
43 auto name = binding[i].JointName;
44 auto hingeId = binding[i].JointId;
45
46 auto boneId = skeleton->findJointIndexByName(name);
47 if (boneId != -1)
48 {
49 std::vector<std::vector<Real>*> tempV(3);
50 std::vector<std::vector<Real>*> tempT(3);
51 animRot[hingeId] = tempV;
52 animTime[hingeId] = tempT;
53
54 animRot[hingeId][0] = &AnimationInfo->mJoint_KeyId_R_X[boneId];
55 animTime[hingeId][0] = &AnimationInfo->mJoint_KeyId_tR_X[boneId];
56
57 animRot[hingeId][1] = &AnimationInfo->mJoint_KeyId_R_Y[boneId];
58 animTime[hingeId][1] = &AnimationInfo->mJoint_KeyId_tR_Y[boneId];
59
60 animRot[hingeId][2] = &AnimationInfo->mJoint_KeyId_R_Z[boneId];
61 animTime[hingeId][2] = &AnimationInfo->mJoint_KeyId_tR_Z[boneId];
62
63 }
64 }
65
66
67 move += this->inDeltaTime()->getValue() * this->varSpeed()->getValue();
68
69 float weight = 0;
70 int keyFrame = -1;
71
72
73 float angleDivede = -1.0;
74
75 Real epsilonAngle = M_PI / 360;
76
77 switch (event.key)
78 {
80
81 angleDivede = -1.0;
82 break;
83
85
86 angleDivede = 1.0;
87
88 break;
89 default:
90 return;
91 break;
92
93 }
94
95 auto lerp = [](float a, float b, float t) {
96 return a + t * (b - a);
97 };
98
99
100 for (size_t i = 0; i < binding.size(); i++)
101 {
102 int hingeId = binding[i].JointId;
103 int axis = binding[i].Axis;
104 float intensity = binding[i].Intensity;
105
106 if (!animRot[hingeId].size())
107 continue;
108
109 Vec3i keyFrame = Vec3i(-1);
110 Vec3f weight = Vec3f(-1);
111
112 getFrameAndWeight(move, keyFrame, weight,animTime[hingeId]);
113
114 std::vector<Real>& animX = *animRot[hingeId][0];
115 std::vector<Real>& animY = *animRot[hingeId][1];
116 std::vector<Real>& animZ = *animRot[hingeId][2];
117
118 std::vector<Real>& currentAnimation = animX;
119
120 switch (axis)
121 {
122 case 0:
123 currentAnimation = animX;
124 break;
125 case 1:
126 currentAnimation = animY;
127 break;
128 case 2:
129 currentAnimation = animZ;
130 break;
131 default:
132 break;
133 }
134
135 Real angle = 0;
136 if (weight[axis] == -1) {
137 angle = currentAnimation[keyFrame[axis]] * M_PI / 180 / angleDivede;
138 }
139 else {
140 angle = lerp(currentAnimation[keyFrame[axis]], currentAnimation[keyFrame[axis] + 1], weight[axis]) * M_PI / 180 / angleDivede * intensity;
141 }
142
143 c_hinge[hingeId].setRange(angle, angle + epsilonAngle);
144 }
145
146 d_hinge.assign(c_hinge);
147
148
149 }
150
151
152
154}
#define DEFINE_CLASS(name)
Definition Object.h:140
#define IMPLEMENT_TCLASS(name, T1)
Definition Object.h:103
#define M_PI
Definition Typedef.inl:36
void onEvent(PKeyboardEvent event) override
void getFrameAndWeight(float time, Vec3i &keyFrame, Vec3f &weight, std::vector< std::vector< Real > * > animTime)
TA * cast(TB *b)
Definition Typedef.inl:286
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Vector< int, 3 > Vec3i
Definition Vector3D.h:95
DYN_FUNC T lerp(Array< T, deviceType > &array1d, float x, LerpMode mode=LerpMode::REPEAT)
Definition Lerp.h:36
@ PKEY_S
Definition InputModule.h:79
@ PKEY_W
Definition InputModule.h:83
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
PKeyboardType key