1 #ifndef __DALI_CONSTRAINTS_H__
2 #define __DALI_CONSTRAINTS_H__
5 * Copyright (c) 2015 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/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>
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
54 * @brief Overrides functor for float properties.
57 * @param[in,out] current The current property value, the constrained value is set
58 * @param[in] inputs Contains the property to copy
60 void operator()( float& current, const PropertyInputContainer& inputs )
62 current = inputs[0]->GetFloat();
66 * @brief Overrides functor for Vector2 properties.
69 * @param[in,out] current The current property value, the constrained value is set
70 * @param[in] inputs Contains the property to copy
72 void operator()( Vector2& current, const PropertyInputContainer& inputs )
74 current = inputs[0]->GetVector2();
78 * @brief Overrides functor for Vector3 properties.
81 * @param[in,out] current The current property value, the constrained value is set
82 * @param[in] inputs Contains the property to copy
84 void operator()( Vector3& current, const PropertyInputContainer& inputs )
86 current = inputs[0]->GetVector3();
90 * @brief Overrides functor for Vector4 properties.
93 * @param[in,out] current The current property value, the constrained value is set
94 * @param[in] inputs Contains the property to copy
96 void operator()( Vector4& current, const PropertyInputContainer& inputs )
98 current = inputs[0]->GetVector4();
102 * @brief Overrides functor for Quaternion properties.
105 * @param[in,out] current The current property value, the constrained value is set
106 * @param[in] inputs Contains the property to copy
108 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
110 current = inputs[0]->GetQuaternion();
114 * @brief Overrides functor for Matrix3 properties.
117 * @param[in,out] current The current property value
118 * @param[in] inputs Contains the property to copy
120 void operator()( Matrix3& current, const PropertyInputContainer& inputs )
122 current = inputs[0]->GetMatrix3();
126 * @brief Overrides functor for Matrix properties.
129 * @param[in,out] current The current property value, the constrained value is set
130 * @param[in] inputs Contains the property to copy
132 void operator()( Matrix& current, const PropertyInputContainer& inputs )
134 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 ) { }
159 * @brief Constructor.
161 * @param[in] scale Scale factor
163 RelativeToConstraint( const Vector3& scale )
164 : mScale( scale ) { }
169 * @param[in,out] current The current property value (vector3 property * scale factor)
170 * @param[in] inputs Property container for current property calculation
172 void operator()( Vector3& current, const PropertyInputContainer& inputs )
174 current = inputs[0]->GetVector3() * mScale;
177 Vector3 mScale; ///< Component-wise scale factor
181 * @brief The constraint function that updates the target property with the value of the first source
182 * multiplied by scale parameter (for float properties).
184 * @e current = <em>input[0]</em> * @e scale. @e current, <em>input[0]</em>, and @e scale
185 * indicates the target property, the first constraint source, and the scale parameter, respectively.
188 struct RelativeToConstraintFloat
191 * @brief Constructor.
193 * @param[in] scale Scale factor
195 RelativeToConstraintFloat( float scale )
196 : mScale( scale ) { }
201 * @param[in,out] current The current property value (float property * scale factor)
202 * @param[in] inputs Property container for current property calculation
204 void operator()( float& current, const PropertyInputContainer& inputs )
206 current = inputs[0]->GetFloat() * mScale;
209 float mScale; ///< Scale factor
213 * @brief Constraint function to aim a camera at a target.
215 * Constraint which sets camera's orientation given camera world position
216 * and a target world position. Uses target's up vector to orient the
217 * constrained actor along the vector between camera position and
221 * @param[in,out] current The current orientation property value, the constrained value is set
222 * @param[in] inputs Contains the world position of the target, the world position of the camera, and the world orientation of the target
224 inline void LookAt( Dali::Quaternion& current, const Dali::PropertyInputContainer& inputs )
226 const PropertyInput& targetPosition( *inputs[0] );
227 const PropertyInput& cameraPosition( *inputs[1] );
228 const PropertyInput& targetOrientation( *inputs[2] );
230 Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
231 vForward.Normalize();
233 const Quaternion& targetOrientationQ = targetOrientation.GetQuaternion();
235 Vector3 targetY(targetOrientationQ.Rotate(Vector3::YAXIS));
238 // Camera Right vector is perpendicular to forward & target up
239 Vector3 vX = targetY.Cross(vForward);
242 // Camera Up vector is perpendicular to forward and right
243 Vector3 vY = vForward.Cross(vX);
246 current = Quaternion( vX, vY, vForward );
254 #endif // __DALI_CONSTRAINTS_H__