Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / progress-value.h
1 #ifndef DALI_INTERNAL_PROGRESS_VALUE_H
2 #define DALI_INTERNAL_PROGRESS_VALUE_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/math/angle-axis.h>
23 #include <dali/public-api/math/quaternion.h>
24 #include <dali/public-api/math/vector2.h>
25 #include <dali/public-api/math/vector3.h>
26 #include <dali/public-api/math/vector4.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 /**
35  * Progress / value pair for animating channels (properties) with keyframes
36  */
37 template <typename T>
38 class ProgressValue
39 {
40 public:
41   ProgressValue (float progress, T value)
42   : mProgress(progress),
43     mValue (value)
44   {
45   }
46
47   ~ProgressValue() = default;
48
49   float GetProgress () const
50   {
51     return mProgress;
52   }
53
54   const T& GetValue () const
55   {
56     return mValue;
57   }
58
59 public:
60   float mProgress;   ///< Progress this value applies to animation channel
61   T mValue;          ///< value this animation channel should take
62 };
63
64 inline void Interpolate (Quaternion& result, const Quaternion& a, const Quaternion& b, float progress)
65 {
66   result = Quaternion::Slerp(a, b, progress);
67 }
68
69 inline void Interpolate (AngleAxis& result, const AngleAxis& a, const AngleAxis& b, float progress)
70 {
71   Quaternion q1(a.angle, a.axis);
72   Quaternion q2(b.angle, b.axis);
73
74   Quaternion iq = Quaternion::Slerp(q1, q2, progress);
75   iq.ToAxisAngle(result.axis, result.angle);
76 }
77
78
79 inline void Interpolate (bool& result, bool a, bool b, float progress)
80 {
81   result = progress < 0.5f ? a : b;
82 }
83
84 inline void Interpolate (int32_t& result, int a, int b, float progress)
85 {
86   result = static_cast<int>(static_cast<float>( a ) + static_cast<float>(b - a) * progress + 0.5f);
87 }
88
89 inline void Interpolate (float& result, float a, float b, float progress)
90 {
91   result = a + (b-a) * progress;
92 }
93
94 inline void Interpolate (Vector2& result, const Vector2& a,  const Vector2& b, float progress)
95 {
96   result = a + (b-a) * progress;
97 }
98
99 inline void Interpolate (Vector3& result, const Vector3& a, const Vector3& b, float progress)
100 {
101   result = a + (b-a) * progress;
102 }
103
104 inline void Interpolate (Vector4& result, const Vector4& a, const Vector4& b, float progress)
105 {
106   result = a + (b-a) * progress;
107 }
108
109 /* Cubic Interpolation (Catmull-Rom spline) between values p1 and p2. p0 and p3 are prev and next values
110  * and are used as control points to calculate tangent of the curve at interpolation points.
111  *
112  * f(t) = a3*t^3 + a2*t^2 + a1*t + a0
113  * Restrictions: f(0)=p1   f(1)=p2   f'(0)=(p2-p0)*0.5   f'(1)=(p3-p1)*0.5
114  */
115
116 inline void CubicInterpolate( int32_t& result, int32_t p0, int32_t p1, int32_t p2, int32_t p3, float progress )
117 {
118   float a3 = static_cast<float>( p3 ) * 0.5f - static_cast<float>( p2 ) * 1.5f + static_cast<float>( p1 ) * 1.5f - static_cast<float>( p0 ) * 0.5f;
119   float a2 = static_cast<float>( p0 ) - static_cast<float>( p1 ) * 2.5f + static_cast<float>( p2 ) * 2.0f - static_cast<float>( p3 ) * 0.5f;
120   float a1 = static_cast<float>( p2 - p0 ) * 0.5f;
121
122   result = static_cast<int>( a3*progress*progress*progress + a2*progress*progress + a1*progress + static_cast<float>( p1 ) + 0.5f );
123 }
124
125 inline void CubicInterpolate( float& result, float p0, float p1, float  p2, float  p3, float progress )
126 {
127   float a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
128   float a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
129   float a1 = (p2-p0)*0.5f;
130
131   result = a3*progress*progress*progress + a2*progress*progress + a1*progress + p1;
132 }
133
134 inline void CubicInterpolate( Vector2& result, const Vector2& p0, const Vector2& p1, const Vector2&  p2, const Vector2&  p3, float progress )
135 {
136   Vector2 a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
137   Vector2 a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
138   Vector2 a1 = (p2-p0)*0.5f;
139
140   result = a3*progress*progress*progress + a2*progress*progress + a1*progress + p1;
141 }
142
143 inline void CubicInterpolate( Vector3& result, const Vector3& p0, const Vector3& p1, const Vector3&  p2, const Vector3&  p3, float progress )
144 {
145   Vector3 a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
146   Vector3 a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
147   Vector3 a1 = (p2-p0)*0.5f;
148
149   result = a3*progress*progress*progress + a2*progress*progress + a1*progress + p1;
150 }
151
152 inline void CubicInterpolate( Vector4& result, const Vector4& p0, const Vector4& p1, const Vector4&  p2, const Vector4&  p3, float progress )
153 {
154   Vector4 a3 = p3*0.5f - p2*1.5f + p1*1.5f - p0*0.5f;
155   Vector4 a2 = p0 - p1*2.5f + p2*2.0f - p3*0.5f;
156   Vector4 a1 = (p2-p0)*0.5f;
157
158   result = a3*progress*progress*progress + a2*progress*progress + a1*progress + p1;
159 }
160
161 inline void CubicInterpolate( bool& result, bool p0, bool p1, bool  p2, bool  p3, float progress )
162 {
163   Interpolate( result, p1, p2, progress);
164 }
165
166 inline void CubicInterpolate( Quaternion& result, const Quaternion& p0, const Quaternion& p1, const Quaternion& p2, const Quaternion& p3, float progress )
167 {
168   Interpolate( result, p1, p2, progress);
169 }
170
171 inline void CubicInterpolate( AngleAxis& result, const AngleAxis& p0, const AngleAxis& p1, const AngleAxis& p2, const AngleAxis& p3, float progress )
172 {
173   Interpolate( result, p1, p2, progress);
174 }
175
176 } // namespace Internal
177
178 } // namespace Dali
179
180 #endif // DALI_INTERNAL_PROGRESS_VALUE_H