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