PeriDyno 1.0.0
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 hierarchicalScene = this->inHierarchicalScene()->constDataPtr();
18
19 float inputTimeStart = hierarchicalScene->mTimeStart;
20 float inputTimeEnd = hierarchicalScene->mTimeEnd;
21
22 auto topo = TypeInfo::cast<DiscreteElements<DataType3f>>(this->inTopology()->getDataPtr());
23
24 auto& d_hinge = topo->hingeJoints();
25 CArray<HingeJoint> c_hinge;
26 c_hinge.assign(d_hinge);
27
28 std::vector<std::vector<Real>*> animRot;
29 std::vector<std::vector<Real>*> animTime;
30
31 animRot.resize(c_hinge.size());
32 animTime.resize(c_hinge.size());
33
34
35 auto hinge_DriveObjName = this->varDriverName()->getValue();
36
37
38 for (size_t i = 0; i < hinge_DriveObjName.size(); i++)
39 {
40 auto name = hinge_DriveObjName[i];
41 auto bone = hierarchicalScene->getObjectByName(name);
42 if(bone)
43 {
44 animRot[i] = bone->m_Rotation_Values;
45 animTime[i] = bone->m_Rotation_Times;
46 }
47 }
48
49 move += this->inDeltaTime()->getValue() * this->varSpeed()->getValue();
50
51 float weight = 0;
52 int keyFrame = -1;
53
54 float currentFrameinAnim = fmod(move, inputTimeEnd - inputTimeStart) + inputTimeStart;
55
56 for (size_t hingeId = 0; hingeId < animTime.size(); hingeId++)
57 {
58 if (!animTime[hingeId])
59 continue;
60
61 auto animTimeZ = animTime[hingeId][2];
62
63 if (currentFrameinAnim < animTimeZ[0])
64 {
65 weight = -1;
66 keyFrame = 1;
67 }
68 if (currentFrameinAnim > animTimeZ[animTimeZ.size()-1])
69 {
70 weight = -1;
71 keyFrame = animTimeZ.size()-1;
72 }
73
74 for (size_t j = 0; j < animTimeZ.size(); j++)
75 {
76 if (currentFrameinAnim > animTimeZ[j] && currentFrameinAnim <= animTimeZ[j + 1])
77 {
78 float v = (currentFrameinAnim - animTimeZ[j]) / (animTimeZ[j + 1] - animTimeZ[j]);
79 weight = v;
80 keyFrame = j;
81 break;
82 }
83 }
84 }
85
86
87
88 float angleDivede = -1.0;
89
90 Real epsilonAngle = M_PI / 360;
91
92 switch (event.key)
93 {
95
96 angleDivede = -1.0;
97 break;
98
100
101 angleDivede = 1.0;
102
103 break;
104 default:
105 return;
106 break;
107
108 }
109
110 auto lerp = [](float a, float b, float t) {
111 return a + t * (b - a);
112 };
113
114
115
116 if (keyFrame != -1)
117 {
118 for (size_t hingeId = 0; hingeId < animRot.size(); hingeId++)
119 {
120 if (!animRot[hingeId])
121 continue;
122 auto animX = animRot[hingeId][0];
123 auto animY = animRot[hingeId][1];
124 auto animZ = animRot[hingeId][2];
125
126 Real angle = 0;
127 if (weight == -1){
128 angle = animZ[keyFrame] * M_PI / 180 / angleDivede;
129 }
130 else {
131 angle = lerp(animZ[keyFrame], animZ[keyFrame + 1], weight) * M_PI / 180 / angleDivede;
132 }
133
134 c_hinge[hingeId].setRange(angle, angle + epsilonAngle);
135 }
136 }
137
138
139 d_hinge.assign(c_hinge);
140
141
142 }
143
144
145
147}
#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
TA * cast(TB *b)
Definition Typedef.inl:286
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
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
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
PKeyboardType key