2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FUiEffects_RendererMathMatrix2Traits.h
20 * @brief The Matrix2Traits class
24 #ifndef _FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX2_TRAITS_H_
25 #define _FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX2_TRAITS_H_
27 #include <renderer/math/FUiEffects_RendererMathMatrixTraits.h>
28 #include <renderer/math/FUiEffects_RendererMathCommon.h>
29 #include <renderer/math/FUiEffects_RendererMathAdapterFunctions.h>
31 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer { namespace Math
35 class MatrixTraits<T, 2> :
36 public MatrixTraitsBase<T, 2>
39 typedef Matrix<T, 2> MatrixType;
40 typedef Vector<T, 2> VectorType;
41 typedef MatrixTraitsBase<T, 2> MatrixTraitsBaseType;
42 typedef typename MatrixTraitsBaseType::TypeReference TypeReference;
44 using MatrixTraitsBaseType::data;
46 inline VectorType& I(void);
47 inline const VectorType& I(void) const;
49 inline VectorType& J(void);
50 inline const VectorType& J(void) const;
52 inline MatrixType& Set(const VectorType& i, const VectorType& j);
53 inline MatrixType& Set(const TypeReference a11, const TypeReference a12,
54 const TypeReference a21, const TypeReference a22);
56 inline static MatrixType& GetIdentity(void);
58 inline VectorType& ApplyTransform(void) const;
59 inline VectorType GetAppliedTransform(void) const;
61 inline MatrixType& Slerp(const MatrixType& to, const TypeReference coeff);
62 inline MatrixType GetSlerped(const MatrixType& to, const TypeReference coeff) const;
63 inline MatrixType& MakeSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff);
64 static inline MatrixType CreateSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff);
66 inline MatrixType& MakeRotation(const TypeReference angleRad);
67 inline MatrixType& Rotate(const TypeReference angleRad);
68 static inline MatrixType CreateRotation(const TypeReference angleRad);
69 inline TypeReference GetRotationAngle(void) const;
71 inline MatrixType& MakeScale(const TypeReference x, const TypeReference y);
72 inline MatrixType& SetScale(const TypeReference x, const TypeReference y);
73 static inline MatrixType CreateScale(const TypeReference x, const TypeReference y);
74 inline MatrixType& Scale(const TypeReference x, const TypeReference y);
78 typename MatrixTraits<T, 2>::VectorType& MatrixTraits<T, 2>::I(void)
84 const typename MatrixTraits<T, 2>::VectorType& MatrixTraits<T, 2>::I(void) const
90 typename MatrixTraits<T, 2>::VectorType& MatrixTraits<T, 2>::J(void)
96 const typename MatrixTraits<T, 2>::VectorType& MatrixTraits<T, 2>::J(void) const
102 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Set(const VectorType& i, const VectorType& j)
106 return *(static_cast<MatrixType*>(this));
110 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Set(const TypeReference a11, const TypeReference a12,
111 const TypeReference a21, const TypeReference a22)
113 data[0].Set(a11, a12);
114 data[1].Set(a21, a22);
115 return *(static_cast<MatrixType*>(this));
119 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::GetIdentity(void)
121 static MatrixType identityMatrix = MatrixType(T(1.0f), T(0.0f),
123 return identityMatrix;
127 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Slerp(const MatrixType& to, const TypeReference coeff)
129 MakeRotation(effects_lerp(GetRotationAngle(), to.GetRotationAngle(), coeff));
130 return *(static_cast<MatrixType*>(this));
134 typename MatrixTraits<T, 2>::MatrixType MatrixTraits<T, 2>::GetSlerped(const MatrixType& to, const TypeReference coeff) const
137 result.MakeRotation(effects_lerp(GetRotationAngle(), to.GetRotationAngle(), coeff));
142 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::MakeSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff)
144 MakeRotation(effects_lerp(from.GetRotationAngle(), to.GetRotationAngle(), coeff));
145 return *(static_cast<MatrixType*>(this));
149 typename MatrixTraits<T, 2>::MatrixType MatrixTraits<T, 2>::CreateSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff)
151 return MatrixType().MakeSlerped(from, to, coeff);
155 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::MakeRotation(const TypeReference angleRad)
157 const T s = effects_sin(angleRad);
158 const T c = effects_cos(angleRad);
160 data[0].data[1] = -s;
163 return *(static_cast<MatrixType*>(this));
167 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Rotate(const TypeReference angleRad)
169 const T s = effects_sin(angleRad);
170 const T c = effects_cos(angleRad);
171 const T ix = data[0].data[0] * c - data[1].data[0] * s;
172 const T iy = data[0].data[1] * c - data[1].data[1] * s;
173 const T jx = data[0].data[0] * s + data[1].data[0] * c;
174 const T jy = data[0].data[1] * s + data[1].data[1] * c;
175 data[0].data[0] = ix;
176 data[0].data[1] = iy;
177 data[1].data[0] = jx;
178 data[1].data[1] = jy;
179 return *(static_cast<MatrixType*>(this));
183 typename MatrixTraits<T, 2>::MatrixType MatrixTraits<T, 2>::CreateRotation(const TypeReference angleRad)
185 return MatrixType().MakeRotation(angleRad);
189 typename MatrixTraits<T, 2>::TypeReference MatrixTraits<T, 2>::GetRotationAngle(void) const
191 static const T t0(0.0f);
192 static const T t1(1.0f);
194 static const VectorType orig_vector = VectorType(t1, t0);
195 static const VectorType orig_vector_cross = VectorType(t0, t1);
197 VectorType rotated = VectorType(orig_vector.data[0] * data[0].data[0] + orig_vector.data[1] * data[0].data[1],
198 orig_vector.data[0] * data[1].data[0] + orig_vector.data[1] * data[1].data[1]).normalize();
200 const T angleCos = effects_clamp(rotated.dot(orig_vector), -t1, t1);
201 const T angleCosCross = effects_clamp(rotated.dot(orig_vector_cross), -t1, t1);
203 if (angleCosCross > t0)
205 return effects_acos(angleCos);
208 return -effects_acos(angleCos);
212 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::MakeScale(const TypeReference x, const TypeReference y)
216 data[0].data[1] = T(0.0f);
217 data[1].data[0] = T(0.0f);
218 return *(static_cast<MatrixType*>(this));
222 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::SetScale(const TypeReference x, const TypeReference y)
226 data[0].data[1] = T(0.0f);
227 data[1].data[0] = T(0.0f);
232 typename MatrixTraits<T, 2>::MatrixType MatrixTraits<T, 2>::CreateScale(const TypeReference x, const TypeReference y)
234 return MatrixType().MakeScale(x, y);
238 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Scale(const TypeReference x, const TypeReference y)
240 data[0].data[0] *= x;
241 data[0].data[1] *= x;
242 data[1].data[0] *= y;
243 data[1].data[1] *= y;
245 return *(static_cast<MatrixType*>(this));
248 }}}}} //Tizen::Ui::Effects::_Renderer::Math
251 #endif //_FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX2_TRAITS_H_