Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / inc / renderer / math / FUiEffects_RendererMathMatrix2Traits.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_RendererMathMatrix2Traits.h
20  * @brief      The Matrix2Traits class
21  *
22  */
23
24 #ifndef _FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX2_TRAITS_H_
25 #define _FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX2_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, 2> :
36         public MatrixTraitsBase<T, 2>
37 {
38 public:
39         typedef Matrix<T, 2> MatrixType;
40         typedef Vector<T, 2> VectorType;
41         typedef MatrixTraitsBase<T, 2> 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 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);
55
56         inline static MatrixType& GetIdentity(void);
57
58         inline VectorType& ApplyTransform(void) const;
59         inline VectorType GetAppliedTransform(void) const;
60
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);
65
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;
70
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);
75 };
76
77 template<typename T>
78 typename MatrixTraits<T, 2>::VectorType& MatrixTraits<T, 2>::I(void)
79 {
80         return data[0];
81 }
82
83 template<typename T>
84 const typename MatrixTraits<T, 2>::VectorType& MatrixTraits<T, 2>::I(void) const
85 {
86         return data[0];
87 }
88
89 template<typename T>
90 typename MatrixTraits<T, 2>::VectorType& MatrixTraits<T, 2>::J(void)
91 {
92         return data[1];
93 }
94
95 template<typename T>
96 const typename MatrixTraits<T, 2>::VectorType& MatrixTraits<T, 2>::J(void) const
97 {
98         return data[1];
99 }
100
101 template<typename T>
102 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Set(const VectorType& i, const VectorType& j)
103 {
104         data[0] = i;
105         data[1] = j;
106         return *(static_cast<MatrixType*>(this));
107 }
108
109 template<typename T>
110 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Set(const TypeReference a11, const TypeReference a12,
111                                                                                                                                  const TypeReference a21, const TypeReference a22)
112 {
113         data[0].Set(a11, a12);
114         data[1].Set(a21, a22);
115         return *(static_cast<MatrixType*>(this));
116 }
117
118 template<typename T>
119 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::GetIdentity(void)
120 {
121         static MatrixType identityMatrix = MatrixType(T(1.0f), T(0.0f),
122                                                                                                   T(0.0f), T(1.0f));
123         return identityMatrix;
124 }
125
126 template<typename T>
127 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Slerp(const MatrixType& to, const TypeReference coeff)
128 {
129         MakeRotation(effects_lerp(GetRotationAngle(), to.GetRotationAngle(), coeff));
130         return *(static_cast<MatrixType*>(this));
131 }
132
133 template<typename T>
134 typename MatrixTraits<T, 2>::MatrixType MatrixTraits<T, 2>::GetSlerped(const MatrixType& to, const TypeReference coeff) const
135 {
136         MatrixType result;
137         result.MakeRotation(effects_lerp(GetRotationAngle(), to.GetRotationAngle(), coeff));
138         return result;
139 }
140
141 template<typename T>
142 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::MakeSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff)
143 {
144         MakeRotation(effects_lerp(from.GetRotationAngle(), to.GetRotationAngle(), coeff));
145         return *(static_cast<MatrixType*>(this));
146 }
147
148 template<typename T>
149 typename MatrixTraits<T, 2>::MatrixType MatrixTraits<T, 2>::CreateSlerped(const MatrixType& from, const MatrixType& to, const TypeReference coeff)
150 {
151         return MatrixType().MakeSlerped(from, to, coeff);
152 }
153
154 template<typename T>
155 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::MakeRotation(const TypeReference angleRad)
156 {
157         const T s = effects_sin(angleRad);
158         const T c = effects_cos(angleRad);
159         data[0].data[0] = c;
160         data[0].data[1] = -s;
161         data[1].data[0] = s;
162         data[1].data[1] = c;
163         return *(static_cast<MatrixType*>(this));
164 }
165
166 template<typename T>
167 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Rotate(const TypeReference angleRad)
168 {
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));
180 }
181
182 template<typename T>
183 typename MatrixTraits<T, 2>::MatrixType MatrixTraits<T, 2>::CreateRotation(const TypeReference angleRad)
184 {
185         return MatrixType().MakeRotation(angleRad);
186 }
187
188 template<typename T>
189 typename MatrixTraits<T, 2>::TypeReference MatrixTraits<T, 2>::GetRotationAngle(void) const
190 {
191         static const T t0(0.0f);
192         static const T t1(1.0f);
193
194         static const VectorType orig_vector = VectorType(t1, t0);
195         static const VectorType orig_vector_cross = VectorType(t0, t1);
196
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();
199
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);
202
203         if (angleCosCross > t0)
204         {
205                 return effects_acos(angleCos);
206         }
207
208         return -effects_acos(angleCos);
209 }
210
211 template<typename T>
212 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::MakeScale(const TypeReference x, const TypeReference y)
213 {
214         data[0].data[0] = x;
215         data[1].data[1] = y;
216         data[0].data[1] = T(0.0f);
217         data[1].data[0] = T(0.0f);
218         return *(static_cast<MatrixType*>(this));
219 }
220
221 template<typename T>
222 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::SetScale(const TypeReference x, const TypeReference y)
223 {
224         data[0].data[0] = x;
225         data[1].data[1] = y;
226         data[0].data[1] = T(0.0f);
227         data[1].data[0] = T(0.0f);
228         return *this;
229 }
230
231 template<typename T>
232 typename MatrixTraits<T, 2>::MatrixType  MatrixTraits<T, 2>::CreateScale(const TypeReference x, const TypeReference y)
233 {
234         return MatrixType().MakeScale(x,  y);
235 }
236
237 template<typename T>
238 typename MatrixTraits<T, 2>::MatrixType& MatrixTraits<T, 2>::Scale(const TypeReference x, const TypeReference y)
239 {
240         data[0].data[0] *= x;
241         data[0].data[1] *= x;
242         data[1].data[0] *= y;
243         data[1].data[1] *= y;
244
245         return *(static_cast<MatrixType*>(this));
246 }
247
248 }}}}} //Tizen::Ui::Effects::_Renderer::Math
249
250
251 #endif //_FUI_EFFECTS_INTERNAL_RENDERER_MATH_MATRIX2_TRAITS_H_