Merge "Klockwork fixes: Distance field iteration Checking for null...
[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) 2014 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/vector3.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/public-api/math/quaternion.h>
25 #include <dali/public-api/math/matrix.h>
26 #include <dali/public-api/math/matrix3.h>
27 #include <dali/public-api/object/property-input.h>
28
29 namespace Dali
30 {
31
32 /**
33  * @brief Scale To Fit constraint.
34  *
35  * Scales an Actor, such that it fits within it's Parent's Size.
36  * f(current, size, parentSize) = parentSize / size
37  */
38 struct ScaleToFitConstraint
39 {
40   /**
41    * @brief Constructor.
42    */
43   ScaleToFitConstraint()
44   { }
45
46   /**
47    * @brief Functor operator
48    *
49    * @param[in] current The actor's current scale
50    * @param[in] sizeProperty The actor's current size
51    * @param[in] parentSizeProperty The parent's current size.
52    * @return The actor's new scale
53    */
54   Vector3 operator()(const Vector3&    current,
55                      const PropertyInput& sizeProperty,
56                      const PropertyInput& parentSizeProperty)
57   {
58     const Vector3 size = sizeProperty.GetVector3();
59     const Vector3 parentSize = parentSizeProperty.GetVector3();
60
61     return Vector3( fabsf(size.x) > Math::MACHINE_EPSILON_1 ? (parentSize.x / size.x) : 0.0f,
62                     fabsf(size.y) > Math::MACHINE_EPSILON_1 ? (parentSize.y / size.y) : 0.0f,
63                     fabsf(size.z) > Math::MACHINE_EPSILON_1 ? (parentSize.z / size.z) : 0.0f );
64   }
65 };
66
67 /**
68  * @brief Scale To Fit Keep Aspect Ratio constraint.
69  *
70  * Scales an Actor, such that it fits within its Parent's Size Keeping the aspect ratio.
71  * f(current, size, parentSize) = Vector3( min( parentSizeX / sizeX, min( parentSizeY / sizeY, parentSizeZ / sizeZ ) )
72  */
73 struct ScaleToFitKeepAspectRatioConstraint
74 {
75   /**
76    * @brief Constructor.
77    */
78   ScaleToFitKeepAspectRatioConstraint()
79   { }
80
81   /**
82    * @brief Functor operator
83    *
84    * @param[in] current The actor's current scale
85    * @param[in] sizeProperty The actor's current size
86    * @param[in] parentSizeProperty The parent's current size.
87    * @return The actor's new scale
88    */
89   Vector3 operator()(const Vector3&    current,
90                      const PropertyInput& sizeProperty,
91                      const PropertyInput& parentSizeProperty)
92   {
93     return FitKeepAspectRatio( parentSizeProperty.GetVector3(), sizeProperty.GetVector3() );
94   }
95 };
96
97 /**
98  * @brief Scale To Fill XY Keep Aspect Ratio constraint.
99  *
100  * Scales an Actor, such that it fill its Parent's Size in the X and Y coordinates Keeping the aspect ratio.
101  * f(current, size, parentSize) = Vector3( max( parentSizeX / sizeX, max( parentSizeY / sizeY, parentSizeZ / sizeZ ) )
102  */
103 struct ScaleToFillXYKeepAspectRatioConstraint
104 {
105   /**
106    * @brief Constructor.
107    */
108   ScaleToFillXYKeepAspectRatioConstraint()
109   { }
110
111   /**
112    * @param[in] current The actor's current scale
113    * @param[in] sizeProperty The actor's current size
114    * @param[in] parentSizeProperty The parent's current size.
115    * @return The actor's new scale
116    */
117   Vector3 operator()(const Vector3&    current,
118                      const PropertyInput& sizeProperty,
119                      const PropertyInput& parentSizeProperty)
120   {
121     return FillXYKeepAspectRatio( parentSizeProperty.GetVector3(), sizeProperty.GetVector3() );
122   }
123 };
124
125 /**
126  * @brief EqualToConstraint
127  *
128  * f(current, property) = property
129  */
130 struct EqualToConstraint
131 {
132   /**
133    * @brief Constructor.
134    */
135   EqualToConstraint()
136   { }
137
138   /**
139    * @brief override functor for float properties
140    *
141    * @param[in] current The current property value
142    * @param[in] property The property to copy
143    * @return The copy of the input property
144    */
145   float operator()(const float current, const PropertyInput& property)
146   {
147     return property.GetFloat();
148   }
149
150   /**
151    * @brief override functor for float properties
152    *
153    * @param[in] current The current property value
154    * @param[in] property The property to copy
155    * @return The copy of the input property
156    */
157   Vector3 operator()(const Vector3& current, const PropertyInput& property)
158   {
159     return property.GetVector3();
160   }
161
162   /**
163    * @brief override functor for float properties
164    *
165    * @param[in] current The current property value
166    * @param[in] property The property to copy
167    * @return The copy of the input property
168    */
169   Vector4 operator()(const Vector4& current, const PropertyInput& property)
170   {
171     return property.GetVector4();
172   }
173
174   /**
175    * @brief override functor for float properties
176    *
177    * @param[in] current The current property value
178    * @param[in] property The property to copy
179    * @return The copy of the input property
180    */
181   Quaternion operator()(const Quaternion& current, const PropertyInput& property)
182   {
183     return property.GetQuaternion();
184   }
185
186   /**
187    * @brief override functor for float properties
188    *
189    * @param[in] current The current property value
190    * @param[in] property The property to copy
191    * @return The copy of the input property
192    */
193   Matrix3 operator()(const Matrix3& current, const PropertyInput& property)
194   {
195     return property.GetMatrix3();
196   }
197
198   /**
199    * @brief override functor for float properties
200    *
201    * @param[in] current The current property value
202    * @param[in] property The property to copy
203    * @return The copy of the input property
204    */
205   Matrix operator()(const Matrix& current, const PropertyInput& property)
206   {
207     return property.GetMatrix();
208   }
209
210 };
211
212 /**
213  * @brief RelativeToConstraint for Vector3 properties
214  *
215  * f(current, property, scale) = property * scale
216  */
217 struct RelativeToConstraint
218 {
219   /**
220    * @brief Constructor.
221    */
222   RelativeToConstraint( float scale )
223   : mScale( scale, scale, scale ) { }
224
225   /**
226    * @brief Constructor.
227    */
228   RelativeToConstraint( const Vector3& scale )
229   : mScale( scale ) { }
230
231   /**
232    * @brief Functor.
233    */
234   Vector3 operator()(const Vector3& current, const PropertyInput& property)
235   {
236     return property.GetVector3() * mScale;
237   }
238
239   Vector3 mScale; ///< Component-wise scale factor
240 };
241
242 /**
243  * @brief RelativeToConstraint for float properties
244  */
245 struct RelativeToConstraintFloat
246 {
247   /**
248    * @brief Constructor.
249    */
250   RelativeToConstraintFloat( float scale )
251   : mScale( scale ) { }
252
253   /**
254    * @brief Functor.
255    */
256   float operator()(const float current, const PropertyInput& property)
257   {
258     return property.GetFloat() * mScale;
259   }
260
261   float mScale; ///< Scale factor
262 };
263
264 /**
265  * @brief Constraint which sets width to be another actor's width,
266  * and the height to a fixed height.
267  */
268 struct SourceWidthFixedHeight
269 {
270   /**
271    * @brief Constructor.
272    */
273   SourceWidthFixedHeight( float height )
274   : mFixedHeight( height ) { }
275
276   /**
277    * @brief Functor.
278    */
279   Vector3 operator()(const Vector3& current,
280                      const PropertyInput& sourceSize)
281   {
282     return Vector3( sourceSize.GetVector3().width, mFixedHeight, current.depth );
283   }
284
285   float mFixedHeight; ///< the fixed height
286 };
287
288 /**
289  * @brief Constraint function to aim a camera at a target.
290  *
291  * Constraint which sets camera's rotation given camera world position
292  * and a target world position.  Uses target's up vector to orient the
293  * constrained actor along the vector between camera position and
294  * target position.
295  *
296  * @param[in] current The current rotation property value
297  * @param[in] targetPosition World position of target
298  * @param[in] cameraPosition World position of camera
299  * @param[in] targetRotation World rotation of the target
300  * @return The orientation of the camera
301  */
302 inline Quaternion LookAt( const Quaternion& current,
303                           const PropertyInput& targetPosition,
304                           const PropertyInput& cameraPosition,
305                           const PropertyInput& targetRotation )
306 {
307   Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
308   vForward.Normalize();
309
310   const Quaternion& targetRotationQ = targetRotation.GetQuaternion();
311
312   Vector3 targetY(targetRotationQ.Rotate(Vector3::YAXIS));
313   targetY.Normalize();
314
315   // Camera Right vector is perpendicular to forward & target up
316   Vector3 vX = targetY.Cross(vForward);
317   vX.Normalize();
318
319   // Camera Up vector is perpendicular to forward and right
320   Vector3 vY = vForward.Cross(vX);
321   vY.Normalize();
322
323   return Quaternion( vX, vY, vForward );
324 }
325
326 } // namespace Dali
327
328 #endif // __DALI_CONSTRAINTS_H__