Merge "Clean up the code to build successfully on macOS" into devel/master
[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) 2020 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/matrix.h>
24 #include <dali/public-api/math/matrix3.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <dali/public-api/math/vector3.h>
27 #include <dali/public-api/math/vector4.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 The constraint function that updates the target property with the value of the first source.
39  *
40  * @e current = <em>input[0]</em>. @e current and <em>input[0]</em> indicate the target property
41  * and the first constraint source (the one added by the first Constraint::AddSource call), respectively.
42  * @SINCE_1_0.0
43  */
44 struct EqualToConstraint
45 {
46   /**
47    * @brief Constructor.
48    * @SINCE_1_0.0
49    */
50   EqualToConstraint() = default;
51
52   /**
53    * @brief Overrides functor for float properties.
54    *
55    * @SINCE_1_0.0
56    * @param[in,out] current The current property value, the constrained value is set
57    * @param[in] inputs Contains the property to copy
58    */
59   void operator()(float& current, const PropertyInputContainer& inputs)
60   {
61     current = inputs[0]->GetFloat();
62   }
63
64   /**
65    * @brief Overrides functor for Vector2 properties.
66    *
67    * @SINCE_1_0.0
68    * @param[in,out] current The current property value, the constrained value is set
69    * @param[in] inputs Contains the property to copy
70    */
71   void operator()(Vector2& current, const PropertyInputContainer& inputs)
72   {
73     current = inputs[0]->GetVector2();
74   }
75
76   /**
77    * @brief Overrides functor for Vector3 properties.
78    *
79    * @SINCE_1_0.0
80    * @param[in,out] current The current property value, the constrained value is set
81    * @param[in] inputs Contains the property to copy
82    */
83   void operator()(Vector3& current, const PropertyInputContainer& inputs)
84   {
85     current = inputs[0]->GetVector3();
86   }
87
88   /**
89    * @brief Overrides functor for Vector4 properties.
90    *
91    * @SINCE_1_0.0
92    * @param[in,out] current The current property value, the constrained value is set
93    * @param[in] inputs Contains the property to copy
94    */
95   void operator()(Vector4& current, const PropertyInputContainer& inputs)
96   {
97     current = inputs[0]->GetVector4();
98   }
99
100   /**
101    * @brief Overrides functor for Quaternion properties.
102    *
103    * @SINCE_1_0.0
104    * @param[in,out] current The current property value, the constrained value is set
105    * @param[in] inputs Contains the property to copy
106    */
107   void operator()(Quaternion& current, const PropertyInputContainer& inputs)
108   {
109     current = inputs[0]->GetQuaternion();
110   }
111
112   /**
113    * @brief Overrides functor for Matrix3 properties.
114    *
115    * @SINCE_1_0.0
116    * @param[in,out] current The current property value
117    * @param[in] inputs Contains the property to copy
118    */
119   void operator()(Matrix3& current, const PropertyInputContainer& inputs)
120   {
121     current = inputs[0]->GetMatrix3();
122   }
123
124   /**
125    * @brief Overrides functor for Matrix properties.
126    *
127    * @SINCE_1_0.0
128    * @param[in,out] current The current property value, the constrained value is set
129    * @param[in] inputs Contains the property to copy
130    */
131   void operator()(Matrix& current, const PropertyInputContainer& inputs)
132   {
133     current = inputs[0]->GetMatrix();
134   }
135 };
136
137 /**
138  * @brief The constraint function that updates the target property with the value of the first source
139  * multiplied by scale parameter (for Vector3 properties).
140  *
141  * @e current = <em>input[0]</em> * @e scale. @e current, <em>input[0]</em>, and @e scale
142  * indicates the target property, the first constraint source, and the scale parameter, respectively.
143  * * implies element-wise multiplication.
144  * @SINCE_1_0.0
145  */
146 struct RelativeToConstraint
147 {
148   /**
149    * @brief Constructor.
150    * @SINCE_1_0.0
151    * @param[in] scale Scale factor
152    */
153   RelativeToConstraint(float scale)
154   : mScale(scale, scale, scale)
155   {
156   }
157
158   /**
159    * @brief Constructor.
160    * @SINCE_1_0.0
161    * @param[in] scale Scale factor
162    */
163   RelativeToConstraint(const Vector3& scale)
164   : mScale(scale)
165   {
166   }
167
168   /**
169    * @brief Functor.
170    * @SINCE_1_0.0
171    * @param[in,out] current The current property value (vector3 property * scale factor)
172    * @param[in] inputs Property container for current property calculation
173    */
174   void operator()(Vector3& current, const PropertyInputContainer& inputs)
175   {
176     current = inputs[0]->GetVector3() * mScale;
177   }
178
179   Vector3 mScale; ///< Component-wise scale factor
180 };
181
182 /**
183  * @brief The constraint function that updates the target property with the value of the first source
184  * multiplied by scale parameter (for float properties).
185  *
186  * @e current = <em>input[0]</em> * @e scale. @e current, <em>input[0]</em>, and @e scale
187  * indicates the target property, the first constraint source, and the scale parameter, respectively.
188  * @SINCE_1_0.0
189  */
190 struct RelativeToConstraintFloat
191 {
192   /**
193    * @brief Constructor.
194    * @SINCE_1_0.0
195    * @param[in] scale Scale factor
196    */
197   RelativeToConstraintFloat(float scale)
198   : mScale(scale)
199   {
200   }
201
202   /**
203    * @brief Functor.
204    * @SINCE_1_0.0
205    * @param[in,out] current The current property value (float property * scale factor)
206    * @param[in] inputs Property container for current property calculation
207    */
208   void operator()(float& current, const PropertyInputContainer& inputs)
209   {
210     current = inputs[0]->GetFloat() * mScale;
211   }
212
213   float mScale; ///< Scale factor
214 };
215
216 /**
217  * @brief Constraint function to aim a camera at a target.
218  *
219  * Constraint which sets camera's orientation given camera world position
220  * and a target world position. Uses target's up vector to orient the
221  * constrained actor along the vector between camera position and
222  * target position.
223  *
224  * @SINCE_1_0.0
225  * @param[in,out] current The current orientation property value, the constrained value is set
226  * @param[in] inputs Contains the world position of the target, the world position of the camera, and the world orientation of the target
227  */
228 inline void LookAt(Dali::Quaternion& current, const Dali::PropertyInputContainer& inputs)
229 {
230   const PropertyInput& targetPosition(*inputs[0]);
231   const PropertyInput& cameraPosition(*inputs[1]);
232   const PropertyInput& targetOrientation(*inputs[2]);
233
234   Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
235   vForward.Normalize();
236
237   const Quaternion& targetOrientationQ = targetOrientation.GetQuaternion();
238
239   Vector3 targetY(targetOrientationQ.Rotate(Vector3::YAXIS));
240   targetY.Normalize();
241
242   // Camera Right vector is perpendicular to forward & target up
243   Vector3 vX = targetY.Cross(vForward);
244   vX.Normalize();
245
246   // Camera Up vector is perpendicular to forward and right
247   Vector3 vY = vForward.Cross(vX);
248   vY.Normalize();
249
250   current = Quaternion(vX, vY, vForward);
251 }
252
253 /**
254  * @}
255  */
256 } // namespace Dali
257
258 #endif // DALI_CONSTRAINTS_H