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 EqualToConstraint
40 * f(current, property) = property
43 struct EqualToConstraint
53 * @brief override functor for float properties
56 * @param[in, out] current The current property value, the constrained value is set
57 * @param[in] inputs Contains the property to copy
58 * @return The copy of the input property
60 void operator()( float& current, const PropertyInputContainer& inputs )
62 current = inputs[0]->GetFloat();
66 * @brief override functor for float properties
69 * @param[in] current The current property value, the constrained value is set
70 * @param[in] inputs Contains the property to copy
71 * @return The copy of the input property
73 void operator()( Vector2& current, const PropertyInputContainer& inputs )
75 current = inputs[0]->GetVector2();
79 * @brief override functor for float properties
82 * @param[in,out] current The current property value, the constrained value is set
83 * @param[in] inputs Contains the property to copy
84 * @return The copy of the input property
86 void operator()( Vector3& current, const PropertyInputContainer& inputs )
88 current = inputs[0]->GetVector3();
92 * @brief override functor for float properties
95 * @param[in,out] current The current property value, the constrained value is set
96 * @param[in] inputs Contains the property to copy
97 * @return The copy of the input property
99 void operator()( Vector4& current, const PropertyInputContainer& inputs )
101 current = inputs[0]->GetVector4();
105 * @brief override functor for float properties
108 * @param[in,out] current The current property value, the constrained value is set
109 * @param[in] inputs Contains the property to copy
110 * @return The copy of the input property
112 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
114 current = inputs[0]->GetQuaternion();
118 * @brief override functor for float properties
121 * @param[in,out] current The current property value
122 * @param[in] inputs Contains the property to copy
123 * @return The copy of the input property
125 void operator()( Matrix3& current, const PropertyInputContainer& inputs )
127 current = inputs[0]->GetMatrix3();
131 * @brief override functor for float properties
134 * @param[in,out] current The current property value, the constrained value is set
135 * @param[in] inputs Contains the property to copy
136 * @return The copy of the input property
138 void operator()( Matrix& current, const PropertyInputContainer& inputs )
140 current = inputs[0]->GetMatrix();
146 * @brief RelativeToConstraint for Vector3 properties
148 * current = property * scale
151 struct RelativeToConstraint
154 * @brief Constructor.
157 RelativeToConstraint( float scale )
158 : mScale( scale, scale, scale ) { }
161 * @brief Constructor.
164 RelativeToConstraint( const Vector3& scale )
165 : mScale( scale ) { }
171 void operator()( Vector3& current, const PropertyInputContainer& inputs )
173 current = inputs[0]->GetVector3() * mScale;
176 Vector3 mScale; ///< Component-wise scale factor
180 * @brief RelativeToConstraint for float properties
183 struct RelativeToConstraintFloat
186 * @brief Constructor.
189 RelativeToConstraintFloat( float scale )
190 : mScale( scale ) { }
196 void operator()( float& current, const PropertyInputContainer& inputs )
198 current = inputs[0]->GetFloat() * mScale;
201 float mScale; ///< Scale factor
205 * @brief Constraint function to aim a camera at a target.
207 * Constraint which sets camera's orientation given camera world position
208 * and a target world position. Uses target's up vector to orient the
209 * constrained actor along the vector between camera position and
213 * @param[in,out] current The current orientation property value, the constrained value is set.
214 * @param[in] inputs Contains the World position of the target, the World position of the camera, and the world orientation of the target
215 * @return The orientation of the camera
217 inline void LookAt( Quaternion& current, const PropertyInputContainer& inputs )
219 const PropertyInput& targetPosition( *inputs[0] );
220 const PropertyInput& cameraPosition( *inputs[1] );
221 const PropertyInput& targetOrientation( *inputs[2] );
223 Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
224 vForward.Normalize();
226 const Quaternion& targetOrientationQ = targetOrientation.GetQuaternion();
228 Vector3 targetY(targetOrientationQ.Rotate(Vector3::YAXIS));
231 // Camera Right vector is perpendicular to forward & target up
232 Vector3 vX = targetY.Cross(vForward);
235 // Camera Up vector is perpendicular to forward and right
236 Vector3 vY = vForward.Cross(vX);
239 current = Quaternion( vX, vY, vForward );
247 #endif // __DALI_CONSTRAINTS_H__