Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / inc / renderer / math / FUiEffects_RendererMathMatrix3Traits.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17
18 /**
19  * @file       FUiEffects_RendererMathMatrix3Traits.h
20  * @brief      The Matrix3Traits class
21  *
22  */
23
24 #ifndef _FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX3_TRAITS_H_
25 #define _FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX3_TRAITS_H_
26
27 #include <renderer/math/FUiEffects_RendererMathMatrixTraits.h>
28 #include <renderer/math/FUiEffects_RendererMathCommon.h>
29 #include <renderer/math/FUiEffects_RendererMathAdapterFunctions.h>
30
31 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer { namespace Math
32 {
33
34 template<typename T>
35 class MatrixTraits<T, 3> :
36         public MatrixTraitsBase<T, 3>
37 {
38 public:
39         typedef Matrix<T, 3> MatrixType;
40         typedef Vector<T, 3> VectorType;
41         typedef MatrixTraitsBase<T, 3> MatrixTraitsBaseType;
42         typedef typename MatrixTraitsBaseType::TypeReference TypeReference;
43
44         using MatrixTraitsBaseType::data;
45
46         inline VectorType& I(void);
47         inline const VectorType& I(void) const;
48
49         inline VectorType& J(void);
50         inline const VectorType& J(void) const;
51
52         inline VectorType& K(void);
53         inline const VectorType& K(void) const;
54
55         inline MatrixType& Set(const VectorType& i, const VectorType& j, const VectorType& k);
56
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);
60
61         inline static MatrixType& GetIdentity(void);
62
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);
67
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;
72
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;
79
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;
94
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);
99
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;
110 };
111
112 template<typename T>
113 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::I(void)
114 {
115         return data[0];
116 }
117
118 template<typename T>
119 const typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::I(void) const
120 {
121         return data[0];
122 }
123
124 template<typename T>
125 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::J(void)
126 {
127         return data[1];
128 }
129
130 template<typename T>
131 const typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::J(void) const
132 {
133         return data[1];
134 }
135
136 template<typename T>
137 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::K(void)
138 {
139         return data[2];
140 }
141
142 template<typename T>
143 const typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::K(void) const
144 {
145         return data[2];
146 }
147
148 template<typename T>
149 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Set(const VectorType& i, const VectorType& j, const VectorType& k)
150 {
151         data[0] = i;
152         data[1] = j;
153         data[2] = k;
154         return *(static_cast<MatrixType*>(this));
155 }
156
157 template<typename T>
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)
161 {
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));
166 }
167
168 template<typename T>
169 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::GetIdentity(void)
170 {
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;
175 }
176
177 template<typename T>
178 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Slerp(const MatrixType& to, const TypeReference coeff)
179 {
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));
187 }
188
189 template<typename T>
190 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::GetSlerped(const MatrixType& to, const TypeReference coeff) const
191 {
192         MatrixType result;
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];
199         return result;
200 }
201
202 template<typename T>
203 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff)
204 {
205         *this = from;
206         Slerp(to, coeff);
207         return *(static_cast<MatrixType*>(this));
208 }
209
210 template<typename T>
211 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff)
212 {
213         return MatrixType().MakeSlerped(from, to, coeff);
214 }
215
216 template<typename T>
217 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::ApplyRotation(VectorType& vector) const
218 {
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],
221                                            vector.data[2]);
222 }
223
224 template<typename T>
225 typename MatrixTraits<T, 3>::VectorType MatrixTraits<T, 3>::GetAppliedRotation(const VectorType& vector) const
226 {
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],
229                 vector.data[2]);
230 }
231
232 template<typename T>
233 typename MatrixTraits<T, 3>::VectorType& MatrixTraits<T, 3>::ApplyTranslate(VectorType& vector) const
234 {
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],
237                                            vector.data[2]);
238 }
239
240 template<typename T>
241 typename MatrixTraits<T, 3>::VectorType MatrixTraits<T, 3>::GetAppliedTranslate(const VectorType& vector) const
242 {
243         return VectorType(vector.data[0] + data[0].data[2] * vector.data[2],
244                                                           vector.data[1] + data[1].data[2] * vector.data[2],
245                                                           vector.data[2]);
246 }
247
248 template<typename T>
249 Vector<T, 2>& MatrixTraits<T, 3>::ApplyTransform(Vector<T, 2>& vector) const
250 {
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]);
253 }
254
255 template<typename T>
256 Vector<T, 2> MatrixTraits<T, 3>::GetAppliedTransform(const Vector<T, 2>& vector) const
257 {
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]);
260 }
261
262 template<typename T>
263 Vector<T, 2>& MatrixTraits<T, 3>::ApplyRotation(Vector<T, 2>& vector) const
264 {
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]);
267 }
268
269 template<typename T>
270 Vector<T, 2> MatrixTraits<T, 3>::GetAppliedRotation(const Vector<T, 2>& vector) const
271 {
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]);
274 }
275
276 template<typename T>
277 Vector<T, 2>& MatrixTraits<T, 3>::ApplyTranslate(Vector<T, 2>& vector) const
278 {
279         return vector.Set(vector.data[0] + data[0].data[2],
280                                            vector.data[1] + data[1].data[2]);
281 }
282
283 template<typename T>
284 Vector<T, 2> MatrixTraits<T, 3>::GetAppliedTranslate(const Vector<T, 2>& vector) const
285 {
286         return Vector<T, 2>(vector.data[0] + data[0].data[2],
287                                            vector.data[1] + data[1].data[2]);
288 }
289
290 template<typename T>
291 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetRotation(const TypeReference angleRad)
292 {
293         const T s = effects_sin(angleRad);
294         const T c = effects_cos(angleRad);
295         data[0].data[0] = c;
296         data[0].data[1] = -s;
297         data[1].data[0] = s;
298         data[1].data[1] = c;
299         return *(static_cast<MatrixType*>(this));
300 }
301
302 template<typename T>
303 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetRotation(const Matrix<T, 2>& rotation)
304 {
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));
310 }
311
312 template<typename T>
313 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetRotation(const MatrixType& rotation)
314 {
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));
320 }
321
322 template<typename T>
323 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeRotation(const TypeReference angleRad)
324 {
325         *this = GetIdentity();
326         return SetRotation(angleRad);
327 }
328
329 template<typename T>
330 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeRotation(const Matrix<T, 2>& rotation)
331 {
332         *this = GetIdentity();
333         return SetRotation(rotation);
334 }
335
336 template<typename T>
337 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeRotation(const MatrixType& rotation)
338 {
339         *this = GetIdentity();
340         return SetRotation(rotation);
341 }
342
343 template<typename T>
344 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Rotate(const TypeReference angleRad)
345 {
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));
357 }
358
359 template<typename T>
360 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Rotate(const Matrix<T, 2>& rotation)
361 {
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));
371 }
372
373 template<typename T>
374 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Rotate(const MatrixType& rotation)
375 {
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));
385 }
386
387 template<typename T>
388 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateRotation(const TypeReference angleRad)
389 {
390         return MatrixType().MakeRotation(angleRad);
391 }
392
393 template<typename T>
394 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateRotation(const Matrix<T, 2>& rotation)
395 {
396         return MatrixType().MakeRotation(rotation);
397 }
398
399 template<typename T>
400 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateRotation(const MatrixType& rotation)
401 {
402         return MatrixType().MakeRotation(rotation);
403 }
404
405 template<typename T>
406 Matrix<T, 2> MatrixTraits<T, 3>::GetRotation() const
407 {
408         return Matrix<T, 2>(data[0].data[0], data[0].data[1], data[1].data[0], data[1].data[1]);
409 }
410
411 template<typename T>
412 typename MatrixTraits<T, 3>::TypeReference MatrixTraits<T, 3>::GetRotationAngle() const
413 {
414         static const T t0(0.0f);
415         static const T t1(1.0f);
416
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);
419
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);
423
424         if (angleCosCross > t0)
425         {
426                 return effects_acos(angleCos);
427         }
428         return -effects_acos(angleCos);
429 }
430
431 template<typename T>
432 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeScale(const TypeReference x, const TypeReference y)
433 {
434         *this = GetIdentity();
435         return SetScale(x, y);
436 }
437
438 template<typename T>
439 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetScale(const TypeReference x, const TypeReference y)
440 {
441         data[0].data[0] = x;
442         data[1].data[1] = y;
443
444         data[0].data[1] = data[1].data[0] = T(0.0f);
445         return *(static_cast<MatrixType*>(this));
446 }
447
448 template<typename T>
449 typename MatrixTraits<T, 3>::MatrixType  MatrixTraits<T, 3>::CreateScale(const TypeReference x, const TypeReference y)
450 {
451         return MatrixType().MakeScale(x, y);
452 }
453
454 template<typename T>
455 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Scale(const TypeReference x, const TypeReference y)
456 {
457         data[0].data[0] *= x;
458         data[0].data[1] *= x;
459         data[1].data[0] *= y;
460         data[1].data[1] *= y;
461
462         return *(static_cast<MatrixType*>(this));
463 }
464
465 template<typename T>
466 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetTranslation(const TypeReference x, const TypeReference y)
467 {
468         data[0].data[2] = x;
469         data[1].data[2] = y;
470         return *(static_cast<MatrixType*>(this));
471 }
472
473 template<typename T>
474 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::SetTranslation(const Vector<T, 2>& tran)
475 {
476         return SetTranslation(tran.data[0], tran.data[1]);
477 }
478
479 template<typename T>
480 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeTranslation(const TypeReference x, const TypeReference y)
481 {
482         *this = GetIdentity();
483         return SetTranslation(x, y);
484 }
485
486 template<typename T>
487 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::MakeTranslation(const Vector<T, 2>& tran)
488 {
489         return MakeTranslation(tran.data[0], tran.data[1]);
490 }
491
492 template<typename T>
493 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Translate(const TypeReference x, const TypeReference y)
494 {
495         data[0].data[2] += x;
496         data[1].data[2] += y;
497         return *(static_cast<MatrixType*>(this));
498 }
499
500 template<typename T>
501 typename MatrixTraits<T, 3>::MatrixType& MatrixTraits<T, 3>::Translate(const Vector<T, 2>& tran)
502 {
503         return Translate(tran.data[0], tran.data[1]);
504 }
505
506 template<typename T>
507 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateTranslation(const TypeReference x, const TypeReference y)
508 {
509         return MatrixType().MakeTranslation(x, y);
510 }
511
512 template<typename T>
513 typename MatrixTraits<T, 3>::MatrixType MatrixTraits<T, 3>::CreateTranslation(const Vector<T, 2>& tran)
514 {
515         return MatrixType().MakeTranslation(tran);
516 }
517
518 template<typename T>
519 Vector<T, 2> MatrixTraits<T, 3>::GetTranslation(void) const
520 {
521         return Vector<T, 2>().Set(data[0].data[2], data[1].data[2]);
522 }
523
524 template<typename T>
525 typename MatrixTraits<T, 3>::VectorType MatrixTraits<T, 3>::GetFullTranslation(void) const
526 {
527         return VectorType().Set(data[0].data[2], data[1].data[2], data[2].data[2]);
528 };
529
530 }}}}} //Tizen::Ui::Effects::_Renderer::Math
531
532
533 #endif //_FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX3_TRAITS_H_