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_RendererMathMatrix3Traits.h
20 * @brief The Matrix3Traits class
24 #ifndef _FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX3_TRAITS_H_
25 #define _FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX3_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, 3> :
36 public MatrixTraitsBase<T, 3>
39 typedef Matrix<T, 3> MatrixType;
40 typedef Vector<T, 3> VectorType;
41 typedef MatrixTraitsBase<T, 3> 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 VectorType& K(void);
53 inline const VectorType& K(void) const;
55 inline MatrixType& Set(const VectorType& i, const VectorType& j, const VectorType& k);
57 inline MatrixType& Set(const TypeReference a11, const TypeReference a12, const TypeReference a13,
58 const TypeReference a21, const TypeReference a22, const TypeReference a23,
59 const TypeReference a31, const TypeReference a32, const TypeReference a33);
61 inline static MatrixType& GetIdentity(void);
63 inline MatrixType& Slerp(const MatrixType& to, const TypeReference coeff);
64 inline MatrixType GetSlerped(const MatrixType& to, const TypeReference coeff) const;
65 inline MatrixType& MakeSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff);
66 static inline MatrixType CreateSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff);
68 inline VectorType& ApplyRotation(VectorType& vector) const;
69 inline VectorType GetAppliedRotation(const VectorType& vector) const;
70 inline VectorType& ApplyTranslate(VectorType& vector) const;
71 inline VectorType GetAppliedTranslate(const VectorType& vector) const;
73 inline Vector<T, 2>& ApplyTransform(Vector<T, 2>& vector) const;
74 inline Vector<T, 2> GetAppliedTransform(const Vector<T, 2>& vector) const;
75 inline Vector<T, 2>& ApplyRotation(Vector<T, 2>& vector) const;
76 inline Vector<T, 2> GetAppliedRotation(const Vector<T, 2>& vector) const;
77 inline Vector<T, 2>& ApplyTranslate(Vector<T, 2>& vector) const;
78 inline Vector<T, 2> GetAppliedTranslate(const Vector<T, 2>& vector) const;
80 inline MatrixType& SetRotation(const TypeReference angleRad);
81 inline MatrixType& SetRotation(const Matrix<T, 2>& rotation);
82 inline MatrixType& SetRotation(const MatrixType& rotation);
83 inline MatrixType& MakeRotation(const TypeReference angleRad);
84 inline MatrixType& MakeRotation(const Matrix<T, 2>& rotation);
85 inline MatrixType& MakeRotation(const MatrixType& rotation);
86 inline MatrixType& Rotate(const TypeReference angleRad);
87 inline MatrixType& Rotate(const Matrix<T, 2>& rotation);
88 inline MatrixType& Rotate(const MatrixType& rotation);
89 static inline MatrixType CreateRotation(const TypeReference angleRad);
90 static inline MatrixType CreateRotation(const Matrix<T, 2>& rotation);
91 static inline MatrixType CreateRotation(const MatrixType& rotation);
92 inline Matrix<T, 2> GetRotation(void) const;
93 inline TypeReference GetRotationAngle(void) const;
95 inline MatrixType& MakeScale(const TypeReference x, const TypeReference y);
96 inline MatrixType& SetScale(const TypeReference x, const TypeReference y);
97 static inline MatrixType CreateScale(const TypeReference x, const TypeReference y);
98 inline MatrixType& Scale(const TypeReference x, const TypeReference y);
100 inline MatrixType& SetTranslation(const TypeReference x, const TypeReference y);
101 inline MatrixType& SetTranslation(const Vector<T, 2>& tran);
102 inline MatrixType& MakeTranslation(const TypeReference x, const TypeReference y);
103 inline MatrixType& MakeTranslation(const Vector<T, 2>& tran);
104 inline MatrixType& Translate(const TypeReference x, const TypeReference y);
105 inline MatrixType& Translate(const Vector<T, 2>& tran);
106 static inline MatrixType CreateTranslation(const TypeReference x, const TypeReference y);
107 static inline MatrixType CreateTranslation(const Vector<T, 2>& tran);
108 inline Vector<T, 2> GetTranslation(void) const;
109 inline VectorType GetFullTranslation(void) const;
113 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::I(void)
119 const typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::I(void) const
125 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::J(void)
131 const typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::J(void) const
137 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::K(void)
143 const typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::K(void) const
149 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Set(const VectorType& i, const VectorType& j, const VectorType& k)
154 return *(static_cast<MatrixType*>(this));
158 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Set(const TypeReference a11, const TypeReference a12, const TypeReference a13,
159 const TypeReference a21, const TypeReference a22, const TypeReference a23,
160 const TypeReference a31, const TypeReference a32, const TypeReference a33)
162 data[0].Set(a11, a12, a13);
163 data[1].Set(a21, a22, a23);
164 data[2].Set(a31, a32, a33);
165 return *(static_cast<MatrixType*>(this));
169 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::GetIdentity(void)
171 static MatrixType identityMatrix = MatrixType(T(1.0f), T(0.0f), T(0.0f),
172 T(0.0f), T(1.0f), T(0.0f),
173 T(0.0f), T(0.0f), T(1.0f));
174 return identityMatrix;
178 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Slerp(const MatrixType& to, const TypeReference coeff)
180 SetRotation(effects_lerp(GetRotationAngle(), to.GetRotationAngle(), coeff));
181 VectorType column = VectorType(data[0].data[2], data[1].data[2], data[2].data[2]);
182 column.lerp(VectorType(to.data[0].data[2], to.data[1].data[2], to.data[2].data[2]), coeff);
183 data[0].data[2] = column.data[0];
184 data[1].data[2] = column.data[1];
185 data[2].data[2] = column.data[2];
186 return *(static_cast<MatrixType*>(this));
190 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::GetSlerped(const MatrixType& to, const TypeReference coeff) const
193 result.MakeRotation(effects_lerp(GetRotationAngle(), to.GetRotationAngle(), coeff));
194 VectorType column = VectorType(data[0].data[2], data[1].data[2], data[2].data[2]);
195 column.lerp(VectorType(to.data[0].data[2], to.data[1].data[2], to.data[2].data[2]), coeff);
196 result.data[0].data[2] = column.data[0];
197 result.data[1].data[2] = column.data[1];
198 result.data[2].data[2] = column.data[2];
203 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff)
207 return *(static_cast<MatrixType*>(this));
211 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff)
213 return MatrixType().MakeSlerped(from, to, coeff);
217 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::ApplyRotation(VectorType& vector) const
219 return vector.Set(data[0].data[0] * vector.data[0] + data[0].data[1] * vector.data[1],
220 data[1].data[0] * vector.data[0] + data[1].data[1] * vector.data[1],
225 typename MatrixTraits<T, 3>::VectorType MatrixTraits<T, 3>::GetAppliedRotation(const VectorType& vector) const
227 return VectorType(data[0].data[0] * vector.data[0] + data[0].data[1] * vector.data[1],
228 data[1].data[0] * vector.data[0] + data[1].data[1] * vector.data[1],
233 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::ApplyTranslate(VectorType& vector) const
235 return vector.Set(vector.data[0] + data[0].data[2] * vector.data[2],
236 vector.data[1] + data[1].data[2] * vector.data[2],
241 typename MatrixTraits<T, 3>::VectorType MatrixTraits<T, 3>::GetAppliedTranslate(const VectorType& vector) const
243 return VectorType(vector.data[0] + data[0].data[2] * vector.data[2],
244 vector.data[1] + data[1].data[2] * vector.data[2],
249 Vector<T, 2>& MatrixTraits<T, 3>::ApplyTransform(Vector<T, 2>& vector) const
251 return vector.Set(data[0].data[0] * vector.data[0] + data[0].data[1] * vector.data[1] + data[0].data[2],
252 data[1].data[0] * vector.data[0] + data[1].data[1] * vector.data[1] + data[1].data[2]);
256 Vector<T, 2> MatrixTraits<T, 3>::GetAppliedTransform(const Vector<T, 2>& vector) const
258 return Vector<T, 2>(data[0].data[0] * vector.data[0] + data[0].data[1] * vector.data[1] + data[0].data[2],
259 data[1].data[0] * vector.data[0] + data[1].data[1] * vector.data[1] + data[1].data[2]);
263 Vector<T, 2>& MatrixTraits<T, 3>::ApplyRotation(Vector<T, 2>& vector) const
265 return vector.Set(data[0].data[0] * vector.data[0] + data[0].data[1] * vector.data[1],
266 data[1].data[0] * vector.data[0] + data[1].data[1] * vector.data[1]);
270 Vector<T, 2> MatrixTraits<T, 3>::GetAppliedRotation(const Vector<T, 2>& vector) const
272 return Vector<T, 2>(data[0].data[0] * vector.data[0] + data[0].data[1] * vector.data[1],
273 data[1].data[0] * vector.data[0] + data[1].data[1] * vector.data[1]);
277 Vector<T, 2>& MatrixTraits<T, 3>::ApplyTranslate(Vector<T, 2>& vector) const
279 return vector.Set(vector.data[0] + data[0].data[2],
280 vector.data[1] + data[1].data[2]);
284 Vector<T, 2> MatrixTraits<T, 3>::GetAppliedTranslate(const Vector<T, 2>& vector) const
286 return Vector<T, 2>(vector.data[0] + data[0].data[2],
287 vector.data[1] + data[1].data[2]);
291 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetRotation(const TypeReference angleRad)
293 const T s = effects_sin(angleRad);
294 const T c = effects_cos(angleRad);
296 data[0].data[1] = -s;
299 return *(static_cast<MatrixType*>(this));
303 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetRotation(const Matrix<T, 2>& rotation)
305 data[0].data[0] = rotation.data[0].data[0];
306 data[0].data[1] = rotation.data[0].data[1];
307 data[1].data[0] = rotation.data[1].data[0];
308 data[1].data[1] = rotation.data[1].data[1];
309 return *(static_cast<MatrixType*>(this));
313 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetRotation(const MatrixType& rotation)
315 data[0].data[0] = rotation.data[0].data[0];
316 data[0].data[1] = rotation.data[0].data[1];
317 data[1].data[0] = rotation.data[1].data[0];
318 data[1].data[1] = rotation.data[1].data[1];
319 return *(static_cast<MatrixType*>(this));
323 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeRotation(const TypeReference angleRad)
325 *this = GetIdentity();
326 return SetRotation(angleRad);
330 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeRotation(const Matrix<T, 2>& rotation)
332 *this = GetIdentity();
333 return SetRotation(rotation);
337 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeRotation(const MatrixType& rotation)
339 *this = GetIdentity();
340 return SetRotation(rotation);
344 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Rotate(const TypeReference angleRad)
346 const T s = effects_sin(angleRad);
347 const T c = effects_cos(angleRad);
348 const T ix = data[0].data[0] * c - data[1].data[0] * s;
349 const T iy = data[0].data[1] * c - data[1].data[1] * s;
350 const T jx = data[0].data[0] * s + data[1].data[0] * c;
351 const T jy = data[0].data[1] * s + data[1].data[1] * c;
352 data[0].data[0] = ix;
353 data[0].data[1] = iy;
354 data[1].data[0] = jx;
355 data[1].data[1] = jy;
356 return *(static_cast<MatrixType*>(this));
360 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Rotate(const Matrix<T, 2>& rotation)
362 const T ix = data[0].data[0] * rotation.data[0].data[0] + data[1].data[0] * rotation.data[0].data[1];
363 const T iy = data[0].data[1] * rotation.data[0].data[0] + data[1].data[1] * rotation.data[0].data[1];
364 const T jx = data[0].data[0] * rotation.data[1].data[0] + data[1].data[0] * rotation.data[1].data[1];
365 const T jy = data[0].data[1] * rotation.data[1].data[0] + data[1].data[1] * rotation.data[1].data[1];
366 data[0].data[0] = ix;
367 data[0].data[1] = iy;
368 data[1].data[0] = jx;
369 data[1].data[1] = jy;
370 return *(static_cast<MatrixType*>(this));
374 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Rotate(const MatrixType& rotation)
376 const T ix = data[0].data[0] * rotation.data[0].data[0] + data[1].data[0] * rotation.data[0].data[1];
377 const T iy = data[0].data[1] * rotation.data[0].data[0] + data[1].data[1] * rotation.data[0].data[1];
378 const T jx = data[0].data[0] * rotation.data[1].data[0] + data[1].data[0] * rotation.data[1].data[1];
379 const T jy = data[0].data[1] * rotation.data[1].data[0] + data[1].data[1] * rotation.data[1].data[1];
380 data[0].data[0] = ix;
381 data[0].data[1] = iy;
382 data[1].data[0] = jx;
383 data[1].data[1] = jy;
384 return *(static_cast<MatrixType*>(this));
388 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateRotation(const TypeReference angleRad)
390 return MatrixType().MakeRotation(angleRad);
394 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateRotation(const Matrix<T, 2>& rotation)
396 return MatrixType().MakeRotation(rotation);
400 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateRotation(const MatrixType& rotation)
402 return MatrixType().MakeRotation(rotation);
406 Matrix<T, 2> MatrixTraits<T, 3>::GetRotation() const
408 return Matrix<T, 2>(data[0].data[0], data[0].data[1], data[1].data[0], data[1].data[1]);
412 typename MatrixTraits<T, 3>::TypeReference MatrixTraits<T, 3>::GetRotationAngle() const
414 static const T t0(0.0f);
415 static const T t1(1.0f);
417 static const Vector<T, 2> orig_vector = Vector<T, 2>(t1, t0);
418 static const Vector<T, 2> orig_vector_cross = Vector<T, 2>(t0, t1);
420 Vector<T, 2> rotated(GetAppliedRotation(orig_vector).normalize());
421 const T angleCos = effects_clamp(rotated.dot(orig_vector), -t1, t1);
422 const T angleCosCross = effects_clamp(rotated.dot(orig_vector_cross), -t1, t1);
424 if (angleCosCross > t0)
426 return effects_acos(angleCos);
428 return -effects_acos(angleCos);
432 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeScale(const TypeReference x, const TypeReference y)
434 *this = GetIdentity();
435 return SetScale(x, y);
439 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetScale(const TypeReference x, const TypeReference y)
444 data[0].data[1] = data[1].data[0] = T(0.0f);
445 return *(static_cast<MatrixType*>(this));
449 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateScale(const TypeReference x, const TypeReference y)
451 return MatrixType().MakeScale(x, y);
455 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Scale(const TypeReference x, const TypeReference y)
457 data[0].data[0] *= x;
458 data[0].data[1] *= x;
459 data[1].data[0] *= y;
460 data[1].data[1] *= y;
462 return *(static_cast<MatrixType*>(this));
466 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetTranslation(const TypeReference x, const TypeReference y)
470 return *(static_cast<MatrixType*>(this));
474 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetTranslation(const Vector<T, 2>& tran)
476 return SetTranslation(tran.data[0], tran.data[1]);
480 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeTranslation(const TypeReference x, const TypeReference y)
482 *this = GetIdentity();
483 return SetTranslation(x, y);
487 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeTranslation(const Vector<T, 2>& tran)
489 return MakeTranslation(tran.data[0], tran.data[1]);
493 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Translate(const TypeReference x, const TypeReference y)
495 data[0].data[2] += x;
496 data[1].data[2] += y;
497 return *(static_cast<MatrixType*>(this));
501 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Translate(const Vector<T, 2>& tran)
503 return Translate(tran.data[0], tran.data[1]);
507 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateTranslation(const TypeReference x, const TypeReference y)
509 return MatrixType().MakeTranslation(x, y);
513 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateTranslation(const Vector<T, 2>& tran)
515 return MatrixType().MakeTranslation(tran);
519 Vector<T, 2> MatrixTraits<T, 3>::GetTranslation(void) const
521 return Vector<T, 2>().Set(data[0].data[2], data[1].data[2]);
525 typename MatrixTraits<T, 3>::VectorType MatrixTraits<T, 3>::GetFullTranslation(void) const
527 return VectorType().Set(data[0].data[2], data[1].data[2], data[2].data[2]);
530 }}}}} //Tizen::Ui::Effects::_Renderer::Math
533 #endif //_FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX3_TRAITS_H_