SO3Engine
ALKeyFrame.cpp
Go to the documentation of this file.
2
3namespace SO3
4{
5
7{
8 mTime = time;
9 mHasPos = false;
10 mHasRot = false;
11 mHasScl = false;
12}
13
17
19{
20 return mHasPos;
21}
22
24{
25 return mHasScl;
26}
27
29{
30 return mHasRot;
31}
32
34{
35 return mTime;
36}
37
38void ALKeyFrame::setPosition(aiVectorKey positionkey)
39{
40 mPosition = positionkey;
41 mHasPos = true;
42}
43
44void ALKeyFrame::setScale(aiVectorKey scaleKey)
45{
46 mScale = scaleKey;
47 mHasScl = true;
48}
49
50void ALKeyFrame::setRotation(aiQuatKey rotationKey)
51{
52 mRotation = rotationKey;
53 mHasRot = true;
54}
55
57{
58 return mPosition;
59}
60
62{
63 return mScale;
64}
65
67{
68 return mRotation;
69}
70
71template<typename T> T ALKeyFrame::linearInterpolate(T tm, T ta, T va, T tb, T vb)
72{
73 T a = (va-vb)/(ta-tb);
74 T b = vb-a*tb;
75 return a*tm+b;
76}
77
78void ALKeyFrame::createInterpolation(std::vector<ALKeyFrame> keyframes, aiNode* initNode)
79{
80 aiVector3D ipos;
81 aiQuaternion irotation;
82 aiVector3D iscale;
83 initNode->mTransformation.Decompose(iscale, irotation, ipos);
84
85 if(!mHasPos)
86 {
87 ALKeyFrame prevKey(0);
88 bool pfound = false;
89 for (unsigned int i = 0; i < keyframes.size(); ++i)
90 {
91 ALKeyFrame tkey = keyframes[i];
92 if ((tkey.getTime() <= mTime) && tkey.hasPosition())
93 {
94 prevKey = tkey;
95 pfound = true;
96 }
97 else if (tkey.getTime() > mTime)
98 break;
99 }
100
101 ALKeyFrame nextKey(0);
102 bool nfound = false;
103 for (unsigned int i = (keyframes.size() - 1); i >= 0; --i)
104 {
105 ALKeyFrame tkey = keyframes[i];
106 if ((tkey.getTime() > mTime) && tkey.hasPosition())
107 {
108 nextKey = tkey;
109 nfound = true;
110 }
111 else if (tkey.getTime() <= mTime)
112 break;
113 }
114
115 if (!pfound)
116 {
117 prevKey.setPosition(aiVectorKey(0, ipos));
118
119 pfound = true;
120 }
121
122 if (pfound && !nfound)
123 {
124 mPosition = prevKey.getPosition();
125 }
126 else if (pfound && nfound)
127 {
128 mPosition.mValue.x = static_cast<float>(linearInterpolate<double>(mTime, prevKey.getTime(), prevKey.getPosition().mValue.x,
129 nextKey.getTime(), nextKey.getPosition().mValue.x));
130
131 mPosition.mValue.y = static_cast<float>(linearInterpolate<double>(mTime, prevKey.getTime(), prevKey.getPosition().mValue.y,
132 nextKey.getTime(), nextKey.getPosition().mValue.y));
133
134 mPosition.mValue.z = static_cast<float>(linearInterpolate<double>(mTime, prevKey.getTime(), prevKey.getPosition().mValue.z,
135 nextKey.getTime(), nextKey.getPosition().mValue.z));
136 }
137 }
138
139
140 if(!mHasScl)
141 {
142 ALKeyFrame prevKey(0);
143 bool pfound = false;
144 for (unsigned int i = 0; i < keyframes.size(); ++i)
145 {
146 ALKeyFrame tkey = keyframes[i];
147 if ((tkey.getTime() <= mTime) && tkey.hasScale())
148 {
149 prevKey = tkey;
150 pfound = true;
151 }
152 else if (tkey.getTime() > mTime)
153 break;
154 }
155
156 ALKeyFrame nextKey(0);
157 bool nfound = false;
158 for (unsigned int i = (keyframes.size() - 1); i >= 0; --i)
159 {
160 ALKeyFrame tkey = keyframes[i];
161 if ((tkey.getTime() > mTime) && tkey.hasScale())
162 {
163 nextKey = tkey;
164 nfound = true;
165 }
166 else if (tkey.getTime() <= mTime)
167 break;
168 }
169
170 if (!pfound)
171 {
172 prevKey.setScale(aiVectorKey(0, iscale));
173
174 pfound = true;
175 }
176
177 if (pfound && !nfound)
178 {
179 mScale = prevKey.getScale();
180 }
181 else if (pfound && nfound)
182 {
183 mScale.mValue.x = static_cast<float>(linearInterpolate<double>(mTime, prevKey.getTime(), prevKey.getScale().mValue.x,
184 nextKey.getTime(), nextKey.getScale().mValue.x));
185
186 mScale.mValue.y = static_cast<float>(linearInterpolate<double>(mTime, prevKey.getTime(), prevKey.getScale().mValue.y,
187 nextKey.getTime(), nextKey.getScale().mValue.y));
188
189 mScale.mValue.z = static_cast<float>(linearInterpolate<double>(mTime, prevKey.getTime(), prevKey.getScale().mValue.z,
190 nextKey.getTime(), nextKey.getScale().mValue.z));
191 }
192 }
193
194 if(!mHasRot)
195 {
196 ALKeyFrame prevKey(0);
197 bool pfound = false;
198 for (unsigned int i = 0; i < keyframes.size(); ++i)
199 {
200 ALKeyFrame tkey = keyframes[i];
201 if ((tkey.getTime() <= mTime) && tkey.hasRotation())
202 {
203 prevKey = tkey;
204 pfound = true;
205 }
206 else if (tkey.getTime() > mTime)
207 break;
208 }
209
210 ALKeyFrame nextKey(0);
211 bool nfound = false;
212 for (unsigned int i = (keyframes.size() - 1); i >= 0; --i)
213 {
214 ALKeyFrame tkey = keyframes[i];
215 if ((tkey.getTime() > mTime) && tkey.hasRotation())
216 {
217 nextKey = tkey;
218 nfound = true;
219 }
220 else if (tkey.getTime() <= mTime)
221 break;
222 }
223
224 if (!pfound)
225 {
226 prevKey.setRotation(aiQuatKey(0, irotation));
227
228 pfound = true;
229 }
230
231 if (pfound && !nfound)
232 {
233 mRotation = prevKey.getRotation();
234 }
235 else if (pfound && nfound)
236 {
237 mRotation.mValue.Interpolate(mRotation.mValue, prevKey.getRotation().mValue, nextKey.getRotation().mValue, static_cast<float>(fabs((prevKey.getTime() - nextKey.getTime()) / mTime)));
238 }
239 }
240}
241
242}
void createInterpolation(std::vector< ALKeyFrame > keyframes, aiNode *initNode)
void setRotation(aiQuatKey rotationKey)
aiQuatKey getRotation()
void setPosition(aiVectorKey positionkey)
ALKeyFrame(double time)
Definition ALKeyFrame.cpp:6
aiVectorKey getPosition()
void setScale(aiVectorKey scaleKey)
aiVectorKey getScale()