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.
18 * @file FUiEffects_UtilsLuaMat4.h
19 * @brief This file contains a definition of LuaMat4 class template
23 #ifndef _FUI_EFFECTS_INTERNAL_UTILS_LUA_MAT4_H_
24 #define _FUI_EFFECTS_INTERNAL_UTILS_LUA_MAT4_H_
26 namespace Tizen { namespace Ui { namespace Effects { namespace _Utils
29 template<class T> class Matrix4;
32 * @template class LuaMat4
33 * @brief This class is needed for model matrix manipulations
34 * from scripts such as rotation, scale, translate and set identity matrix
42 * @brief LuaMat4 class default constructor
47 * @brief LuaMat4 class copy constructor
49 inline LuaMat4(const LuaMat4<T>& rhs);
52 * @brief LuaMat4 class constructor by elements
54 inline LuaMat4(T m0, T m1, T m2, T m3,
55 T m4, T m5, T m6, T m7,
56 T m8, T m9, T m10, T m11,
57 T m12, T m13, T m14, T m15);
60 * @brief LuaMat4 class constructor by one element
65 * @brief LuaMat4 class adaptive constructor from class Matrix4
67 inline LuaMat4(const Matrix4<T> &mat4);
70 * @brief Sets identity matrix
72 inline void SetIdentity(void);
75 * @brief Translates model matrix by values X0, Y0 and Z0 relative to the axes
77 inline void Translate(T x0, T y0, T z0);
80 * @brief Scales model matrix by the factors ax, ay, az relative to the axes
81 * @remark The coordinates of point (0,0,0) do not change
83 inline bool Scale(T ax, T ay, T az);
86 * @brief Scales model matrix by the factors ax, ay, az relative to the axes
87 * @remark The coordinates of point (x0, y0, z0) do not change
89 inline bool Scale(T ax, T ay, T az, T x0, T y0, T z0);
92 * @brief Rotates model matrix by the Angle around axis Z
93 * drawn from the point (0,0,0) in global coordinates
95 inline void RotateAroundAxisZ(T angle);
98 * @brief Rotates model matrix by the Angle around axis Z
99 * drawn from the point (x0, y0, z0) in global coordinates
101 inline void RotateAroundAxisZ(T angle, T x0, T y0, T z0);
104 * @brief Rotates model matrix by the Angle around axis X
105 * drawn from the point (0, 0, 0) in global coordinates
107 inline void RotateAroundAxisX(T angle);
110 * @brief Rotates model matrix by the Angle around axis X
111 * drawn from the point (x0, y0, z0) in global coordinates
113 inline void RotateAroundAxisX(T angle, T x0, T y0, T z0);
116 * @brief Rotates model matrix by the Angle around axis Y
117 * drawn from the point (0, 0, 0) in global coordinates
119 inline void RotateAroundAxisY(T angle);
122 * @brief Rotates model matrix by the Angle around axis Y
123 * drawn from the point (x0,y0,z0) in global coordinates
125 inline void RotateAroundAxisY(T angle, T x0, T y0, T z0);
128 * @brief Rotates model matrix by the Angle around axis with coordinates Ax, Ay, Az
129 * drawn from the point (0,0,0) in global coordinates
131 inline bool RotateAroundAxisArbitrary(T angle, T ax, T ay, T az);
134 * @brief Rotates model matrix by the Angle around axis with coordinates Ax, Ay, Az
135 * drawn from the point (x0, y0, z0) in global coordinates
137 inline bool RotateAroundAxisArbitrary(T angle, T ax, T ay, T az, T x0, T y0, T z0);
140 * @brief Overloading multiplication operator of two matrix as class LuaMat4 instances
142 inline LuaMat4<T> operator*(const LuaMat4<T> &mul) const;
145 * @brief Overloading multiplication operator of matrix and number
147 inline LuaMat4<T> operator*(T mul) const;
150 * @brief Overloading addition operator of two matrix
152 inline LuaMat4<T> operator+(const LuaMat4<T> &add) const;
155 * @brief Overloading subtraction operator of two matrix
157 inline LuaMat4<T> operator-(const LuaMat4<T> &sub) const;
160 * @brief Overloading the unary minus operator
162 inline LuaMat4<T> operator-(void)const;
165 * @brief Overloading the assignment operator
167 inline LuaMat4<T> &operator=(const LuaMat4<T> &orig);
170 * @brief Casts LuaMat4 class instance to type Matrix4
172 inline operator Matrix4<T>(void)const;
185 T __m[16]; /**< the matrix 4*4 as one dimensional array of type T */
189 typedef LuaMat4<float> LuaMatrix4;
191 } } } } // Tizen::Ui::Effects::_Utils
197 namespace Tizen { namespace Ui { namespace Effects { namespace _Utils
201 template<class T> inline
202 LuaMat4<T>::LuaMat4(void)
207 template<class T> inline
208 LuaMat4<T>::LuaMat4(T m)
210 for (int i = 0; i < 16; ++i)
216 template<class T> inline
217 LuaMat4<T>::LuaMat4(const LuaMat4<T> &rhs)
219 memcpy(this->__m, rhs.__m, 16 * sizeof(T));
222 template<class T> inline
223 LuaMat4<T>::LuaMat4(const Matrix4<T> &mat4)
225 memcpy( __m, &mat4.i, 4 * sizeof(T));
226 memcpy(&__m[4], &mat4.j, 4 * sizeof(T));
227 memcpy(&__m[8], &mat4.k, 4 * sizeof(T));
228 memcpy(&__m[12], &mat4.c, 4 * sizeof(T));
231 template<class T> inline
232 LuaMat4<T>::LuaMat4(T m0, T m1, T m2, T m3,
233 T m4, T m5, T m6, T m7,
234 T m8, T m9, T m10, T m11,
235 T m12, T m13, T m14, T m15)
256 template<class T> inline void
257 LuaMat4<T>::SetIdentity(void)
259 memset(__m, 0, 16 * sizeof(T));
261 T one = static_cast<T>(1);
270 template<class T> inline void
271 LuaMat4<T>::Translate(T x0, T y0, T z0)
274 T* pnt3Init = &(__m[12]);
277 const T* pArg[3] = {&x0, &y0, &z0};
279 for (short i = 0, j = 0; i < 12; ++i, ++j)
287 *pnt0 += *(pnt3++) * (**p);
294 template<class T> inline bool
295 LuaMat4<T>::Scale(T aX, T aY, T aZ)
297 T epsilon = std::numeric_limits<T>::epsilon();
298 if (aX <= -epsilon || aY <= -epsilon || aZ <= -epsilon)
304 const T* pArg[3] = {&aX, &aY, &aZ};
306 for (short i = 0, j = 0; i < 12; ++i, ++j)
320 template<class T> inline bool
321 LuaMat4<T>::Scale(T aX, T aY, T aZ, T x0, T y0, T z0)
323 Translate(-x0, -y0, -z0);
324 bool res = Scale(aX, aY, aZ);
325 Translate(x0, y0, z0);
330 template<class T> inline void
331 LuaMat4<T>::RotateAroundAxisZ(T angle)
333 // ---- for case T==float ----------
334 float coi = cosf(angle);
335 float sii = sinf(angle);
336 //----------------------------------
341 T* pnt2Init = &(__m[4]);
346 for (short i = 0, j = 0; i < 8; ++i, ++j)
356 *pMTemp = (*(pnt1++) * co + *(pnt2++) * si);
359 memcpy(__m, mTemp, 8 * sizeof(T));
364 template<class T> inline void
365 LuaMat4<T>::RotateAroundAxisZ(T angle, T x0, T y0, T z0)
367 Translate(-x0, -y0, -z0);
368 RotateAroundAxisZ(angle);
369 Translate(x0, y0, z0);
374 template<class T> inline void
375 LuaMat4<T>::RotateAroundAxisX(T angle)
377 // ---- for case T==float ----------
378 float coi = cosf(angle);
379 float sii = sinf(angle);
380 //----------------------------------
384 T* pnt1Init = &(__m[4]);
385 T* pnt2Init = &(__m[8]);
390 for (short i = 0, j = 0; i < 8; ++i, ++j)
400 *pMTemp = (*(pnt1++) * co + *(pnt2++) * si);
403 memcpy(&(__m[4]), mTemp, 8 * sizeof(T));
408 template<class T> inline void
409 LuaMat4<T>::RotateAroundAxisX(T angle, T x0, T y0, T z0)
411 Translate(-x0, -y0, -z0);
412 RotateAroundAxisX(angle);
413 Translate(x0, y0, z0);
418 template<class T> inline void
419 LuaMat4<T>::RotateAroundAxisY(T angle)
421 // ---- for case T==float ----------
422 float coi = cosf(angle);
423 float sii = sinf(angle);
424 //----------------------------------
429 T* pnt2Init = &(__m[8]);
434 for (short i = 0, j = 0; i < 8; ++i, ++j)
444 *pMTemp = (*(pnt1++) * co + *(pnt2++) * si);
447 memcpy(__m, mTemp, 4 * sizeof(T));
448 memcpy(&(__m[8]), &(mTemp[4]), 4 * sizeof(T));
453 template<class T> inline void
454 LuaMat4<T>::RotateAroundAxisY(T angle, T x0, T y0, T z0)
456 Translate(-x0, -y0, -z0);
457 RotateAroundAxisY(angle);
458 Translate(x0, y0, z0);
463 template<class T> inline bool
464 LuaMat4<T>::RotateAroundAxisArbitrary(T angle, T ax, T ay, T az)
466 // ---- for case T==float -------------------------------------
467 float c = cosf(angle);
468 float s = sinf(angle);
470 T len = sqrtf(ax * ax + ay * ay + az * az);
471 if (fabsf(len) <= std::numeric_limits<T>::epsilon())
475 //-------------------------------------------------------------
480 LuaMat4<T> res(c + (1 - c) * axn * axn, (1 - c) * axn * ayn - s * azn, (1 - c) * axn * azn + s * ayn, 0,
481 (1 - c) * ayn * axn + s * azn, c + (1 - c) * ayn * ayn, (1 - c) * ayn * azn - s * axn, 0,
482 (1 - c) * azn * axn - s * ayn, (1 - c) * azn * ayn + s * axn, c + (1 - c) * azn * azn, 0,
485 *this = res * (*this);
489 template<class T> inline bool
490 LuaMat4<T>::RotateAroundAxisArbitrary(T angle, T ax, T ay, T az, T x0, T y0, T z0)
492 Translate(-x0, -y0, -z0);
493 bool res = RotateAroundAxisArbitrary(angle, ax, ay, az);
494 Translate(x0, y0, z0);
499 template<class T> inline LuaMat4<T>
500 LuaMat4<T>::operator*(const LuaMat4<T> &rhs) const
503 memset(res.__m, 0, 16 * sizeof(T));
504 const T* pThis = __m;
506 const T* pMul = rhs.__m;
510 for (k = 0; k < 4; ++k)
512 for (j = 0; j < 4; ++j)
516 for (i = 0; i < 4; ++i)
518 *pRes += *(pThisC++) * (*pMulC);
531 template<class T> inline LuaMat4<T>
532 LuaMat4<T>::operator*(T rhs)const
534 LuaMat4<T> res(*this);
537 for (short i = 0; i < 15; ++i, ++pRes)
545 template<class T> inline LuaMat4<T>
546 LuaMat4<T>::operator+(const LuaMat4<T> &rhs)const
549 memset(res.__m, 0, 16*sizeof(T));
551 const T* pAdd1 = __m;
552 const T* pAdd2 = rhs.__m;
554 for (short i = 0; i < 15; ++i, ++pRes)
556 *pRes = *(pAdd1++) + *(pAdd2++);
562 template<class T> inline LuaMat4<T>
563 LuaMat4<T>::operator-(void)const
565 LuaMat4<T> res(*this);
568 for (short i = 0; i < 15; ++i, ++pRes)
576 template<class T> inline LuaMat4<T>
577 LuaMat4<T>::operator-(const LuaMat4<T> &rhs)const
579 return *this + (-rhs);
582 template<class T> inline LuaMat4<T> &
583 LuaMat4<T>::operator=(const LuaMat4<T> &rhs)
585 memcpy(__m, rhs.__m, 16 * sizeof(T));
589 template<class T> inline
590 LuaMat4<T>::operator Matrix4<T>(void)const
592 Matrix4<T> res(__m[0], __m[1], __m[2], __m[3],
593 __m[4], __m[5], __m[6], __m[7],
594 __m[8], __m[9], __m[10], __m[11],
595 __m[12], __m[13], __m[14], __m[15]);
601 } } } } // Tizen::Ui::Effects::_Utils
603 #endif //_FUI_EFFECTS_INTERNAL_UTILS_LUA_MAT4_H_