Merge "Prevents an invalid texture cache being interrogated for a texture. This ...
[platform/core/uifw/dali-core.git] / dali / public-api / animation / constraints.h
1 #ifndef __DALI_CONSTRAINTS_H__
2 #define __DALI_CONSTRAINTS_H__
3
4 /*
5  * Copyright (c) 2015 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/animation/constraint.h>
23 #include <dali/public-api/math/vector3.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <dali/public-api/math/matrix.h>
27 #include <dali/public-api/math/matrix3.h>
28 #include <dali/public-api/object/property-input.h>
29
30 namespace Dali
31 {
32 /**
33  * @addtogroup dali-core-animation
34  * @{
35  */
36
37 /**
38  * @brief EqualToConstraint
39  *
40  * f(current, property) = property
41  */
42 struct EqualToConstraint
43 {
44   /**
45    * @brief Constructor.
46    */
47   EqualToConstraint()
48   { }
49
50   /**
51    * @brief override functor for float properties
52    *
53    * @param[in, out] current The current property value, the constrained value is set
54    * @param[in] inputs Contains the property to copy
55    * @return The copy of the input property
56    */
57   void operator()( float& current, const PropertyInputContainer& inputs )
58   {
59     current = inputs[0]->GetFloat();
60   }
61
62   /**
63    * @brief override functor for float properties
64    *
65    * @param[in] current The current property value, the constrained value is set
66    * @param[in] inputs Contains the property to copy
67    * @return The copy of the input property
68    */
69   void operator()( Vector2& current, const PropertyInputContainer& inputs )
70   {
71     current = inputs[0]->GetVector2();
72   }
73
74   /**
75    * @brief override functor for float properties
76    *
77    * @param[in,out] current The current property value, the constrained value is set
78    * @param[in] inputs Contains the property to copy
79    * @return The copy of the input property
80    */
81   void operator()( Vector3& current, const PropertyInputContainer& inputs )
82   {
83     current = inputs[0]->GetVector3();
84   }
85
86   /**
87    * @brief override functor for float properties
88    *
89    * @param[in,out] current The current property value, the constrained value is set
90    * @param[in] inputs Contains the property to copy
91    * @return The copy of the input property
92    */
93   void operator()( Vector4& current, const PropertyInputContainer& inputs )
94   {
95     current = inputs[0]->GetVector4();
96   }
97
98   /**
99    * @brief override functor for float properties
100    *
101    * @param[in,out] current The current property value, the constrained value is set
102    * @param[in] inputs Contains the property to copy
103    * @return The copy of the input property
104    */
105   void operator()( Quaternion& current, const PropertyInputContainer& inputs )
106   {
107     current = inputs[0]->GetQuaternion();
108   }
109
110   /**
111    * @brief override functor for float properties
112    *
113    * @param[in,out] current The current property value
114    * @param[in] inputs Contains the property to copy
115    * @return The copy of the input property
116    */
117   void operator()( Matrix3& current, const PropertyInputContainer& inputs )
118   {
119     current = inputs[0]->GetMatrix3();
120   }
121
122   /**
123    * @brief override functor for float properties
124    *
125    * @param[in,out] current The current property value, the constrained value is set
126    * @param[in] inputs Contains the property to copy
127    * @return The copy of the input property
128    */
129   void operator()( Matrix& current, const PropertyInputContainer& inputs )
130   {
131     current = inputs[0]->GetMatrix();
132   }
133
134 };
135
136 /**
137  * @brief RelativeToConstraint for Vector3 properties
138  *
139  * current = property * scale
140  */
141 struct RelativeToConstraint
142 {
143   /**
144    * @brief Constructor.
145    */
146   RelativeToConstraint( float scale )
147   : mScale( scale, scale, scale ) { }
148
149   /**
150    * @brief Constructor.
151    */
152   RelativeToConstraint( const Vector3& scale )
153   : mScale( scale ) { }
154
155   /**
156    * @brief Functor.
157    */
158   void operator()( Vector3& current, const PropertyInputContainer& inputs )
159   {
160     current = inputs[0]->GetVector3() * mScale;
161   }
162
163   Vector3 mScale; ///< Component-wise scale factor
164 };
165
166 /**
167  * @brief RelativeToConstraint for float properties
168  */
169 struct RelativeToConstraintFloat
170 {
171   /**
172    * @brief Constructor.
173    */
174   RelativeToConstraintFloat( float scale )
175   : mScale( scale ) { }
176
177   /**
178    * @brief Functor.
179    */
180   void operator()( float& current, const PropertyInputContainer& inputs )
181   {
182     current = inputs[0]->GetFloat() * mScale;
183   }
184
185   float mScale; ///< Scale factor
186 };
187
188 /**
189  * @brief Constraint function to aim a camera at a target.
190  *
191  * Constraint which sets camera's orientation given camera world position
192  * and a target world position.  Uses target's up vector to orient the
193  * constrained actor along the vector between camera position and
194  * target position.
195  *
196  * @param[in,out] current The current orientation property value, the constrained value is set.
197  * @param[in] inputs Contains the World position of the target, the World position of the camera, and the world orientation of the target
198  * @return The orientation of the camera
199  */
200 inline void LookAt( Quaternion& current, const PropertyInputContainer& inputs )
201 {
202   const PropertyInput& targetPosition( *inputs[0] );
203   const PropertyInput& cameraPosition( *inputs[1] );
204   const PropertyInput& targetOrientation( *inputs[2] );
205
206   Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
207   vForward.Normalize();
208
209   const Quaternion& targetOrientationQ = targetOrientation.GetQuaternion();
210
211   Vector3 targetY(targetOrientationQ.Rotate(Vector3::YAXIS));
212   targetY.Normalize();
213
214   // Camera Right vector is perpendicular to forward & target up
215   Vector3 vX = targetY.Cross(vForward);
216   vX.Normalize();
217
218   // Camera Up vector is perpendicular to forward and right
219   Vector3 vY = vForward.Cross(vX);
220   vY.Normalize();
221
222   current = Quaternion( vX, vY, vForward );
223 }
224
225 /**
226  * @}
227  */
228 } // namespace Dali
229
230 #endif // __DALI_CONSTRAINTS_H__