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
42 struct EqualToConstraint
51 * @brief override functor for float properties
53 * @param[in, out] current The current property value, the constrained value is set
54 * @param[in] inputs Contains the property to copy
55 * @return The copy of the input property
57 void operator()( float& current, const PropertyInputContainer& inputs )
59 current = inputs[0]->GetFloat();
63 * @brief override functor for float properties
65 * @param[in] current The current property value, the constrained value is set
66 * @param[in] inputs Contains the property to copy
67 * @return The copy of the input property
69 void operator()( Vector2& current, const PropertyInputContainer& inputs )
71 current = inputs[0]->GetVector2();
75 * @brief override functor for float properties
77 * @param[in,out] current The current property value, the constrained value is set
78 * @param[in] inputs Contains the property to copy
79 * @return The copy of the input property
81 void operator()( Vector3& current, const PropertyInputContainer& inputs )
83 current = inputs[0]->GetVector3();
87 * @brief override functor for float properties
89 * @param[in,out] current The current property value, the constrained value is set
90 * @param[in] inputs Contains the property to copy
91 * @return The copy of the input property
93 void operator()( Vector4& current, const PropertyInputContainer& inputs )
95 current = inputs[0]->GetVector4();
99 * @brief override functor for float properties
101 * @param[in,out] current The current property value, the constrained value is set
102 * @param[in] inputs Contains the property to copy
103 * @return The copy of the input property
105 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
107 current = inputs[0]->GetQuaternion();
111 * @brief override functor for float properties
113 * @param[in,out] current The current property value
114 * @param[in] inputs Contains the property to copy
115 * @return The copy of the input property
117 void operator()( Matrix3& current, const PropertyInputContainer& inputs )
119 current = inputs[0]->GetMatrix3();
123 * @brief override functor for float properties
125 * @param[in,out] current The current property value, the constrained value is set
126 * @param[in] inputs Contains the property to copy
127 * @return The copy of the input property
129 void operator()( Matrix& current, const PropertyInputContainer& inputs )
131 current = inputs[0]->GetMatrix();
137 * @brief RelativeToConstraint for Vector3 properties
139 * current = property * scale
141 struct RelativeToConstraint
144 * @brief Constructor.
146 RelativeToConstraint( float scale )
147 : mScale( scale, scale, scale ) { }
150 * @brief Constructor.
152 RelativeToConstraint( const Vector3& scale )
153 : mScale( scale ) { }
158 void operator()( Vector3& current, const PropertyInputContainer& inputs )
160 current = inputs[0]->GetVector3() * mScale;
163 Vector3 mScale; ///< Component-wise scale factor
167 * @brief RelativeToConstraint for float properties
169 struct RelativeToConstraintFloat
172 * @brief Constructor.
174 RelativeToConstraintFloat( float scale )
175 : mScale( scale ) { }
180 void operator()( float& current, const PropertyInputContainer& inputs )
182 current = inputs[0]->GetFloat() * mScale;
185 float mScale; ///< Scale factor
189 * @brief Constraint function to aim a camera at a target.
191 * Constraint which sets camera's orientation given camera world position
192 * and a target world position. Uses target's up vector to orient the
193 * constrained actor along the vector between camera position and
196 * @param[in,out] current The current orientation property value, the constrained value is set.
197 * @param[in] inputs Contains the World position of the target, the World position of the camera, and the world orientation of the target
198 * @return The orientation of the camera
200 inline void LookAt( Quaternion& current, const PropertyInputContainer& inputs )
202 const PropertyInput& targetPosition( *inputs[0] );
203 const PropertyInput& cameraPosition( *inputs[1] );
204 const PropertyInput& targetOrientation( *inputs[2] );
206 Vector3 vForward = targetPosition.GetVector3() - cameraPosition.GetVector3();
207 vForward.Normalize();
209 const Quaternion& targetOrientationQ = targetOrientation.GetQuaternion();
211 Vector3 targetY(targetOrientationQ.Rotate(Vector3::YAXIS));
214 // Camera Right vector is perpendicular to forward & target up
215 Vector3 vX = targetY.Cross(vForward);
218 // Camera Up vector is perpendicular to forward and right
219 Vector3 vY = vForward.Cross(vX);
222 current = Quaternion( vX, vY, vForward );
230 #endif // __DALI_CONSTRAINTS_H__