Merge "Performance enhancement, cache glClear state" into tizen
[platform/core/uifw/dali-core.git] / dali / public-api / animation / interpolator-functions.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <boost/function.hpp>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/animation/interpolator-functions.h>
23 #include <dali/public-api/math/vector2.h>
24 #include <dali/public-api/math/vector3.h>
25 #include <dali/public-api/math/vector4.h>
26 #include <dali/public-api/math/quaternion.h>
27 #include <dali/public-api/math/matrix.h>
28 #include <dali/public-api/math/matrix3.h>
29
30 namespace
31 {
32
33 bool LerpBoolean( const bool& current, const bool& target, float progress )
34 {
35   // Not particularly useful for boolean values
36   if (progress >= 1.0f)
37   {
38     return target;
39   }
40
41   return current;
42 }
43
44 float LerpFloat( const float& current, const float& target, float progress )
45 {
46   return current + ((target - current) * progress);
47 }
48
49 int LerpInteger( const int& current, const int& target, float progress )
50 {
51   return static_cast<int>( current + ( (target - current) * progress ) + 0.5f );
52 }
53
54 Dali::Vector2 LerpVector2( const Dali::Vector2& current, const Dali::Vector2& target, float progress )
55 {
56   return current + ((target - current) * progress);
57 }
58
59 Dali::Vector3 LerpVector3( const Dali::Vector3& current, const Dali::Vector3& target, float progress )
60 {
61   return current + ((target - current) * progress);
62 }
63
64 Dali::Vector4 LerpVector4( const Dali::Vector4& current, const Dali::Vector4& target, float progress )
65 {
66   return current + ((target - current) * progress);
67 }
68
69 Dali::Quaternion SlerpQuaternion( const Dali::Quaternion& current, const Dali::Quaternion& target, float progress )
70 {
71   return Dali::Quaternion::Slerp(current, target, progress);
72 }
73
74 Dali::Matrix LerpMatrix( const Dali::Matrix& current, const Dali::Matrix& target, float progress )
75 {
76   if (progress > 0.5f)
77   {
78     return target;
79   }
80
81   return current;
82 }
83
84 Dali::Matrix3 LerpMatrix3( const Dali::Matrix3& current, const Dali::Matrix3& target, float progress )
85 {
86   if (progress > 0.5f)
87   {
88     return target;
89   }
90
91   return current;
92 }
93
94 } // unnamed namespace
95
96 namespace Dali
97 {
98
99 AnyInterpolator GetDefaultInterpolator( Property::Type type )
100 {
101   AnyInterpolator function;
102
103   switch ( type )
104   {
105     case Property::BOOLEAN:
106     {
107       function = boost::function<bool (const bool&, const bool&, float)>( &LerpBoolean );
108       break;
109     }
110
111     case Property::FLOAT:
112     {
113       function = boost::function<float (const float&, const float&, float)>( &LerpFloat );
114       break;
115     }
116
117     case Property::INTEGER:
118     {
119       function = boost::function<int (const int&, const int&, float)>( &LerpInteger );
120       break;
121     }
122
123     case Property::VECTOR2:
124     {
125       function = boost::function<Vector2 (const Vector2&, const Vector2&, float)>( &LerpVector2 );
126       break;
127     }
128
129     case Property::VECTOR3:
130     {
131       function = boost::function<Vector3 (const Vector3&, const Vector3&, float)>( &LerpVector3 );
132       break;
133     }
134
135     case Property::VECTOR4:
136     {
137       function = boost::function<Vector4 (const Vector4&, const Vector4&, float)>( &LerpVector4 );
138       break;
139     }
140
141     case Property::ROTATION:
142     {
143       function = boost::function<Quaternion (const Quaternion&, const Quaternion&, float)>( &SlerpQuaternion );
144       break;
145     }
146
147     case Property::MATRIX3:
148     {
149       function = boost::function<Matrix3 (const Matrix3&, const Matrix3&, float)>(&LerpMatrix3);
150       break;
151     }
152
153     case Property::MATRIX:
154     {
155       function = boost::function<Matrix (const Matrix&, const Matrix&, float)>(&LerpMatrix);
156       break;
157     }
158
159     default:
160       break;
161   }
162
163   return function;
164 }
165
166
167 } // namespace Dali