1 #ifndef DALI_CONSTRAINTS_H
2 #define DALI_CONSTRAINTS_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
33 * @addtogroup dali_core_animation
38 * @brief The constraint function that updates the target property with the value of the first source.
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.
44 struct EqualToConstraint
55 * @brief Overrides functor for float properties.
58 * @param[in,out] current The current property value, the constrained value is set
59 * @param[in] inputs Contains the property to copy
61 void operator()(float& current, const PropertyInputContainer& inputs)
63 current = inputs[0]->GetFloat();
67 * @brief Overrides functor for Vector2 properties.
70 * @param[in,out] current The current property value, the constrained value is set
71 * @param[in] inputs Contains the property to copy
73 void operator()(Vector2& current, const PropertyInputContainer& inputs)
75 current = inputs[0]->GetVector2();
79 * @brief Overrides functor for Vector3 properties.
82 * @param[in,out] current The current property value, the constrained value is set
83 * @param[in] inputs Contains the property to copy
85 void operator()(Vector3& current, const PropertyInputContainer& inputs)
87 current = inputs[0]->GetVector3();
91 * @brief Overrides functor for Vector4 properties.
94 * @param[in,out] current The current property value, the constrained value is set
95 * @param[in] inputs Contains the property to copy
97 void operator()(Vector4& current, const PropertyInputContainer& inputs)
99 current = inputs[0]->GetVector4();
103 * @brief Overrides functor for Quaternion properties.
106 * @param[in,out] current The current property value, the constrained value is set
107 * @param[in] inputs Contains the property to copy
109 void operator()(Quaternion& current, const PropertyInputContainer& inputs)
111 current = inputs[0]->GetQuaternion();
115 * @brief Overrides functor for Matrix3 properties.
118 * @param[in,out] current The current property value
119 * @param[in] inputs Contains the property to copy
121 void operator()(Matrix3& current, const PropertyInputContainer& inputs)
123 current = inputs[0]->GetMatrix3();
127 * @brief Overrides functor for Matrix properties.
130 * @param[in,out] current The current property value, the constrained value is set
131 * @param[in] inputs Contains the property to copy
133 void operator()(Matrix& current, const PropertyInputContainer& inputs)
135 current = inputs[0]->GetMatrix();
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).
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.
148 struct RelativeToConstraint
151 * @brief Constructor.
153 * @param[in] scale Scale factor
155 RelativeToConstraint(float scale)
156 : mScale(scale, scale, scale)
161 * @brief Constructor.
163 * @param[in] scale Scale factor
165 RelativeToConstraint(const Vector3& scale)
173 * @param[in,out] current The current property value (vector3 property * scale factor)
174 * @param[in] inputs Property container for current property calculation
176 void operator()(Vector3& current, const PropertyInputContainer& inputs)
178 current = inputs[0]->GetVector3() * mScale;
181 Vector3 mScale; ///< Component-wise scale factor
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).
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.
192 struct RelativeToConstraintFloat
195 * @brief Constructor.
197 * @param[in] scale Scale factor
199 RelativeToConstraintFloat(float scale)
207 * @param[in,out] current The current property value (float property * scale factor)
208 * @param[in] inputs Property container for current property calculation
210 void operator()(float& current, const PropertyInputContainer& inputs)
212 current = inputs[0]->GetFloat() * mScale;
215 float mScale; ///< Scale factor
219 * @brief Constraint function to aim a camera at a target.
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
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
230 inline void LookAt(Dali::Quaternion& current, const Dali::PropertyInputContainer& inputs)
232 const PropertyInput& targetPosition(*inputs[0]);
233 const PropertyInput& cameraPosition(*inputs[1]);
234 const PropertyInput& targetOrientation(*inputs[2]);
236 Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
237 vForward.Normalize();
239 const Quaternion& targetOrientationQ = targetOrientation.GetQuaternion();
241 Vector3 targetY(targetOrientationQ.Rotate(Vector3::YAXIS));
244 // Camera Right vector is perpendicular to forward & target up
245 Vector3 vX = targetY.Cross(vForward);
248 // Camera Up vector is perpendicular to forward and right
249 Vector3 vY = vForward.Cross(vX);
252 current = Quaternion(vX, vY, vForward);
260 #endif // DALI_CONSTRAINTS_H