1 #ifndef __DALI_INTERNAL_PROGRESS_VALUE_H__
2 #define __DALI_INTERNAL_PROGRESS_VALUE_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/math/quaternion.h>
23 #include <dali/public-api/math/vector3.h>
24 #include <dali/public-api/math/radian.h>
25 #include <dali/public-api/math/degree.h>
34 * Progress / value pair for animating channels (properties) with keyframes
40 ProgressValue (float progress, T value)
41 : mProgress(progress),
50 float GetProgress () const
55 const T& GetValue () const
61 float mProgress; ///< Progress this value applies to animation channel
62 T mValue; ///< value this animation channel should take
65 typedef ProgressValue<Quaternion> ProgressQuaternion;
66 typedef std::vector<ProgressQuaternion> ProgressQuaternionContainer;
68 typedef ProgressValue<AngleAxis> ProgressAngleAxis;
69 typedef std::vector<AngleAxis> ProgressAngleAxisContainer;
71 typedef ProgressValue<bool> ProgressBoolean;
72 typedef std::vector<ProgressBoolean> ProgressBooleanContainer;
74 typedef ProgressValue<int> ProgressInteger;
75 typedef std::vector<ProgressInteger> ProgressIntegerContainer;
77 typedef ProgressValue<unsigned int> ProgressUnsignedInteger;
78 typedef std::vector<ProgressUnsignedInteger> ProgressUnsignedIntegerContainer;
80 typedef ProgressValue<float> ProgressNumber;
81 typedef std::vector<ProgressNumber> ProgressNumberContainer;
83 typedef ProgressValue<Vector2> ProgressVector2;
84 typedef std::vector<ProgressVector2> ProgressVector2Container;
86 typedef ProgressValue<Vector3> ProgressVector3;
87 typedef std::vector<ProgressVector3> ProgressVector3Container;
89 typedef ProgressValue<Vector4> ProgressVector4;
90 typedef std::vector<ProgressVector4> ProgressVector4Container;
92 inline void Interpolate (Quaternion& result, const Quaternion& a, const Quaternion& b, float progress)
94 result = Quaternion::Slerp(a, b, progress);
97 inline void Interpolate (AngleAxis& result, const AngleAxis& a, const AngleAxis& b, float progress)
99 Quaternion q1(a.angle, a.axis);
100 Quaternion q2(b.angle, b.axis);
102 Quaternion iq = Quaternion::Slerp(q1, q2, progress);
103 iq.ToAxisAngle(result.axis, result.angle);
107 inline void Interpolate (bool& result, bool a, bool b, float progress)
109 result = progress < 0.5f ? a : b;
112 inline void Interpolate (int& result, int a, int b, float progress)
114 result = static_cast<int>(a + (b - a) * progress + 0.5f);
117 inline void Interpolate (unsigned int& result, unsigned int a, unsigned int b, float progress)
119 result = static_cast<unsigned int>(a + (b - a) * progress + 0.5f);
122 inline void Interpolate (float& result, float a, float b, float progress)
124 result = a + (b-a) * progress;
127 inline void Interpolate (Vector2& result, const Vector2& a, const Vector2& b, float progress)
129 result = a + (b-a) * progress;
132 inline void Interpolate (Vector3& result, const Vector3& a, const Vector3& b, float progress)
134 result = a + (b-a) * progress;
137 inline void Interpolate (Vector4& result, const Vector4& a, const Vector4& b, float progress)
139 result = a + (b-a) * progress;
142 /* Cubic Interpolation (Catmull-Rom spline) between values p1 and p2. p0 and p3 are prev and next values
143 * and are used as control points to calculate tangent of the curve at interpolation points.
145 * f(t) = a3*t^3 + a2*t^2 + a1*t + a0
146 * Restrictions: f(0)=p1 f(1)=p2 f'(0)=(p2-p0)*0.5 f'(1)=(p3-p1)*0.5
149 inline void CubicInterpolate( int& result, int p0, int p1, int p2, int p3, float progress )
151 float a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
152 float a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
153 float a1 = (p2-p0)*0.5f;
155 result = static_cast<int>( a3*progress*progress*progress + a2*progress*progress + a1*progress + p1 + 0.5f );
158 inline void CubicInterpolate( unsigned int& result, unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3, float progress )
160 float a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
161 float a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
162 float a1 = (p2-p0)*0.5f;
164 result = static_cast<unsigned int>( a3*progress*progress*progress + a2*progress*progress + a1*progress + p1 + 0.5f );
167 inline void CubicInterpolate( float& result, float p0, float p1, float p2, float p3, float progress )
169 float a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
170 float a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
171 float a1 = (p2-p0)*0.5f;
173 result = a3*progress*progress*progress + a2*progress*progress + a1*progress + p1;
176 inline void CubicInterpolate( Vector2& result, const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3, float progress )
178 Vector2 a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
179 Vector2 a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
180 Vector2 a1 = (p2-p0)*0.5f;
182 result = a3*progress*progress*progress + a2*progress*progress + a1*progress + p1;
185 inline void CubicInterpolate( Vector3& result, const Vector3& p0, const Vector3& p1, const Vector3& p2, const Vector3& p3, float progress )
187 Vector3 a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
188 Vector3 a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
189 Vector3 a1 = (p2-p0)*0.5f;
191 result = a3*progress*progress*progress + a2*progress*progress + a1*progress + p1;
194 inline void CubicInterpolate( Vector4& result, const Vector4& p0, const Vector4& p1, const Vector4& p2, const Vector4& p3, float progress )
196 Vector4 a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
197 Vector4 a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
198 Vector4 a1 = (p2-p0)*0.5f;
200 result = a3*progress*progress*progress + a2*progress*progress + a1*progress + p1;
203 inline void CubicInterpolate( bool& result, bool p0, bool p1, bool p2, bool p3, float progress )
205 Interpolate( result, p1, p2, progress);
208 inline void CubicInterpolate( Quaternion& result, const Quaternion& p0, const Quaternion& p1, const Quaternion& p2, const Quaternion& p3, float progress )
210 Interpolate( result, p1, p2, progress);
213 inline void CubicInterpolate( AngleAxis& result, const AngleAxis& p0, const AngleAxis& p1, const AngleAxis& p2, const AngleAxis& p3, float progress )
215 Interpolate( result, p1, p2, progress);
218 } // namespace Internal
222 #endif //__DALI_PROGRESS_VALUE_H__