Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / ui / effects / inc / utils / FUiEffects_UtilsVector4.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_UtilsVector4.h
20  * @brief      The Vector4 class
21  *
22  */
23
24 #ifndef _FUI_EFFECTS_INTERNAL_UTILS_VECTOR4_H_
25 #define _FUI_EFFECTS_INTERNAL_UTILS_VECTOR4_H_
26
27 #include "FUiEffects_UtilsAdapterFunctions.h"
28
29 namespace Tizen { namespace Ui { namespace Effects { namespace _Utils
30 {
31         template<class T> class _Vector2;
32         template<class T> class _Vector3;
33
34         template<class T> class Vector4
35         {
36             private:
37                 typedef Vector4<T> SelfType;
38
39             public:
40                 T x;
41                 T y;
42                 T z;
43                 T w;
44
45                 inline Vector4();
46                 inline explicit Vector4(const T& aValue);
47                 inline explicit Vector4(const T* aValue);
48                 inline Vector4(const T& aX, const T& aY, const T& aZ, const T& aW);
49                 inline Vector4(const _Vector2<T>& aXY, const T& aZ, const T& aW);
50                 inline Vector4(const T& aX, const _Vector2<T>& aYZ, const T& aW);
51                 inline Vector4(const T& aX, const T& aY, const _Vector2<T>& aZW);
52                 inline Vector4(const _Vector3<T>& aXYZ, const T& aW);
53                 inline Vector4(const T& aX, const _Vector3<T>& aYZW);
54                 inline Vector4(const _Vector2<T>& aXY, const _Vector2<T>& aZW);
55                 inline Vector4(const SelfType& aOther);
56
57                 inline SelfType& operator=(const SelfType& aRhv);
58
59                 inline SelfType& set(const SelfType& aOther);
60                 inline SelfType& set(const T& aX, const T& aY, const T& aZ, const T& aW);
61                 inline SelfType& set(const T& aValue);
62                 inline SelfType& set(const T* aValue);
63                 inline SelfType& set(const _Vector2<T>& aXY, const T& aZ, const T& aW);
64                 inline SelfType& set(const T& aX, const _Vector2<T>& aYZ, const T& aW);
65                 inline SelfType& set(const T& aX, const T& aY, const _Vector2<T>& aZW);
66                 inline SelfType& set(const _Vector3<T>& aXYZ, const T& aW);
67                 inline SelfType& set(const T& aX, const _Vector3<T>& aYZW);
68                 inline SelfType& set(const _Vector2<T>& aXY, const _Vector2<T>& aZW);
69
70                 inline SelfType operator-() const;
71                 inline SelfType& inverse();
72                 inline SelfType getInversed() const;
73
74                 inline SelfType& operator+=(const SelfType& aRhv);
75                 inline SelfType& operator+=(const T& aRhv);
76                 inline SelfType& operator-=(const SelfType& aRhv);
77                 inline SelfType& operator-=(const T& aRhv);
78                 inline SelfType& operator*=(const SelfType& aRhv);
79                 inline SelfType& operator*=(const T& aRhv);
80                 inline SelfType& operator/=(const SelfType& aRhv);
81                 inline SelfType& operator/=(const T& aRhv);
82
83                 inline T* getPointer();
84                 inline const T* getPointer() const;
85
86                 inline SelfType& normalize();
87                 inline SelfType getNormalized() const;
88
89                 inline SelfType& lerp(const SelfType& aTo, const T& aCoeff);
90                 inline SelfType getLerped(const SelfType& aTo, const T& aCoeff) const;
91                 inline SelfType& makeLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff);
92                 static inline SelfType createLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff);
93
94                 inline T dot(const SelfType& aOther) const;
95
96                 inline T lengthSqr() const;
97                 inline T length() const;
98                 inline T distanceSqr(const SelfType& aOther) const;
99                 inline T distance(const SelfType& aOther) const;
100
101                 inline _Vector2<T> swizzle(unsigned int aX, unsigned int aY) const;
102                 inline _Vector3<T> swizzle(unsigned int aX, unsigned int aY, unsigned int aZ) const;
103                 inline Vector4<T> swizzle(unsigned int aX, unsigned int aY, unsigned int aZ, unsigned int aW) const;
104         };
105
106         typedef Vector4<float> Vec4f;
107         typedef Vector4<double> Vec4d;
108
109         template<class T> inline Vector4<T> operator+(const Vector4<T>& aLhv, const Vector4<T>& aRhv);
110         template<class T> inline Vector4<T> operator+(const Vector4<T>& aLhv, const T& aRhv);
111         template<class T> inline Vector4<T> operator+(const T& aLhv, const Vector4<T>& aRhv);
112         template<class T> inline Vector4<T> operator-(const Vector4<T>& aLhv, const Vector4<T>& aRhv);
113         template<class T> inline Vector4<T> operator-(const Vector4<T>& aLhv, const T& aRhv);
114         template<class T> inline Vector4<T> operator-(const T& aLhv, const Vector4<T>& aRhv);
115         template<class T> inline Vector4<T> operator*(const Vector4<T>& aLhv, const Vector4<T>& aRhv);
116         template<class T> inline Vector4<T> operator*(const Vector4<T>& aLhv, const T& aRhv);
117         template<class T> inline Vector4<T> operator*(const T& aLhv, const Vector4<T>& aRhv);
118         template<class T> inline Vector4<T> operator/(const Vector4<T>& aLhv, const Vector4<T>& aRhv);
119         template<class T> inline Vector4<T> operator/(const Vector4<T>& aLhv, const T& aRhv);
120         template<class T> inline Vector4<T> operator/(const T& aLhv, const Vector4<T>& aRhv);
121         template<class T> inline bool operator==(const Vector4<T>& aLhv, const Vector4<T>& aRhv);
122         template<class T> inline bool operator==(const Vector4<T>& aLhv, const T& aRhv);
123         template<class T> inline bool operator==(const T& aLhv, const Vector4<T>& aRhv);
124         template<class T> inline bool operator!=(const Vector4<T>& aLhv, const Vector4<T>& aRhv);
125         template<class T> inline bool operator!=(const Vector4<T>& aLhv, const T& aRhv);
126         template<class T> inline bool operator!=(const T& aLhv, const Vector4<T>& aRhv);
127
128         template<class T> Vector4<T>::Vector4() {}
129         template<class T> Vector4<T>::Vector4(const T& aValue): x(aValue), y(aValue), z(aValue), w(aValue) {}
130         template<class T> Vector4<T>::Vector4(const T* aValue): x(aValue[0]), y(aValue[1]), z(aValue[2]), w(aValue[3]) {}
131         template<class T> Vector4<T>::Vector4(const T& aX, const T& aY, const T& aZ, const T& aW): x(aX), y(aY), z(aZ), w(aW) {}
132         template<class T> Vector4<T>::Vector4(const Vector4<T>& aOther): x(aOther.x), y(aOther.y), z(aOther.z), w(aOther.w) {}
133         template<class T> Vector4<T>::Vector4(const _Vector2<T>& aXY, const T& aZ, const T& aW): x(aXY.x), y(aXY.y), z(aZ), w(aW) {}
134         template<class T> Vector4<T>::Vector4(const T& aX, const _Vector2<T>& aYZ, const T& aW): x(aX), y(aYZ.x), z(aYZ.y), w(aW) {}
135         template<class T> Vector4<T>::Vector4(const T& aX, const T& aY, const _Vector2<T>& aZW): x(aX), y(aY), z(aZW.x), w(aZW.y) {}
136         template<class T> Vector4<T>::Vector4(const _Vector3<T>& aXYZ, const T& aW): x(aXYZ.x), y(aXYZ.y), z(aXYZ.z), w(aW) {}
137         template<class T> Vector4<T>::Vector4(const T& aX, const _Vector3<T>& aYZW): x(aX), y(aYZW.x), z(aYZW.y), w(aYZW.z) {}
138         template<class T> Vector4<T>::Vector4(const _Vector2<T>& aXY, const _Vector2<T>& aZW): x(aXY.x), y(aXY.y), z(aZW.x), w(aZW.y) {}
139
140         template<class T> Vector4<T>& Vector4<T>::operator=(const Vector4<T>& aRhv)
141         {
142             x = aRhv.x;
143             y = aRhv.y;
144             z = aRhv.z;
145             w = aRhv.w;
146             return *this;
147         }
148
149         template<class T> Vector4<T>& Vector4<T>::set(const Vector4<T>& aOther)
150         {
151             return *this = aOther;
152         }
153         template<class T> Vector4<T>& Vector4<T>::set(const T& aX, const T& aY, const T& aZ, const T& aW)
154         {
155             x = aX;
156             y = aY;
157             z = aZ;
158             w = aW;
159             return *this;
160         }
161         template<class T> Vector4<T>& Vector4<T>::set(const T& aValue)
162         {
163             x = aValue;
164             y = aValue;
165             z = aValue;
166             w = aValue;
167             return *this;
168         }
169         template<class T> Vector4<T>& Vector4<T>::set(const T* aValue)
170         {
171             x = aValue[0];
172             y = aValue[1];
173             z = aValue[2];
174             w = aValue[3];
175             return *this;
176         }
177         template<class T> Vector4<T>& Vector4<T>::set(const _Vector2<T>& aXY, const T& aZ, const T& aW)
178         {
179             x = aXY.x;
180             y = aXY.y;
181             z = aZ;
182             w = aW;
183             return *this;
184         }
185         template<class T> Vector4<T>& Vector4<T>::set(const T& aX, const _Vector2<T>& aYZ, const T& aW)
186         {
187             x = aX;
188             y = aYZ.x;
189             z = aYZ.y;
190             w = aW;
191             return *this;
192         }
193         template<class T> Vector4<T>& Vector4<T>::set(const T& aX, const T& aY, const _Vector2<T>& aZW)
194         {
195             x = aX;
196             y = aY;
197             z = aZW.x;
198             w = aZW.y;
199             return *this;
200         }
201         template<class T> Vector4<T>& Vector4<T>::set(const _Vector3<T>& aXYZ, const T& aW)
202         {
203             x = aXYZ.x;
204             y = aXYZ.y;
205             z = aXYZ.z;
206             w = aW;
207             return *this;
208         }
209         template<class T> Vector4<T>& Vector4<T>::set(const T& aX, const _Vector3<T>& aYZW)
210         {
211             x = aX;
212             y = aYZW.x;
213             z = aYZW.y;
214             w = aYZW.z;
215             return *this;
216         }
217         template<class T> Vector4<T>& Vector4<T>::set(const _Vector2<T>& aXY, const _Vector2<T>& aZW)
218         {
219             x = aXY.x;
220             y = aXY.y;
221             z = aZW.x;
222             w = aZW.y;
223             return *this;
224         }
225
226         template<class T> Vector4<T> Vector4<T>::operator-() const
227         {
228             return getInversed();
229         }
230         template<class T> Vector4<T>& Vector4<T>::inverse()
231         {
232             x = -x;
233             y = -y;
234             z = -z;
235             w = -w;
236             return *this;
237         }
238         template<class T> Vector4<T> Vector4<T>::getInversed() const
239         {
240             return Vector4<T>(-x, -y, -z, -w);
241         }
242
243         template<class T> Vector4<T>& Vector4<T>::operator+=(const Vector4<T>& aRhv)
244         {
245             x += aRhv.x;
246             y += aRhv.y;
247             z += aRhv.z;
248             w += aRhv.w;
249             return *this;
250         }
251         template<class T> Vector4<T>& Vector4<T>::operator+=(const T& aRhv)
252         {
253             x += aRhv;
254             y += aRhv;
255             z += aRhv;
256             w += aRhv;
257             return *this;
258         }
259         template<class T> Vector4<T>& Vector4<T>::operator-=(const Vector4<T>& aRhv)
260         {
261             x -= aRhv.x;
262             y -= aRhv.y;
263             z -= aRhv.z;
264             w -= aRhv.w;
265             return *this;
266         }
267         template<class T> Vector4<T>& Vector4<T>::operator-=(const T& aRhv)
268         {
269             x -= aRhv;
270             y -= aRhv;
271             z -= aRhv;
272             w -= aRhv;
273             return *this;
274         }
275         template<class T> Vector4<T>& Vector4<T>::operator*=(const Vector4<T>& aRhv)
276         {
277             x *= aRhv.x;
278             y *= aRhv.y;
279             z *= aRhv.z;
280             w *= aRhv.w;
281             return *this;
282         }
283         template<class T> Vector4<T>& Vector4<T>::operator*=(const T& aRhv)
284         {
285             x *= aRhv;
286             y *= aRhv;
287             z *= aRhv;
288             w *= aRhv;
289             return *this;
290         }
291         template<class T> Vector4<T>& Vector4<T>::operator/=(const Vector4<T>& aRhv)
292         {
293             x /= aRhv.x;
294             y /= aRhv.y;
295             z /= aRhv.z;
296             w /= aRhv.w;
297             return *this;
298         }
299         template<class T> Vector4<T>& Vector4<T>::operator/=(const T& aRhv)
300         {
301             x /= aRhv;
302             y /= aRhv;
303             z /= aRhv;
304             w /= aRhv;
305             return *this;
306         }
307
308         template<class T> T* Vector4<T>::getPointer()
309         {
310             return &x;
311         }
312         template<class T> const T* Vector4<T>::getPointer() const
313         {
314             return &x;
315         }
316
317         template<class T> Vector4<T>& Vector4<T>::normalize()
318         {
319             const static T t0(0.0f);
320
321             const T dist = length();
322             if(EffectsEqual(t0, dist))
323                 return *this;
324
325             return operator/=(dist);
326         }
327         template<class T> Vector4<T> Vector4<T>::getNormalized() const
328         {
329             const static T t0(0.0f);
330
331             const T dist = length();
332             if(EffectsEqual(t0, dist))
333                 return *this;
334
335             return *this / dist;
336         }
337
338         template<class T> Vector4<T>& Vector4<T>::lerp(const Vector4<T>& aTo, const T& aCoeff)
339         {
340             x += (aTo.x - x) * aCoeff;
341             y += (aTo.y - y) * aCoeff;
342             z += (aTo.z - z) * aCoeff;
343             w += (aTo.w - w) * aCoeff;
344             return *this;
345         }
346         template<class T> Vector4<T> Vector4<T>::getLerped(const Vector4<T>& aTo, const T& aCoeff) const
347         {
348             return Vector4<T>(
349                 x + (aTo.x - x) * aCoeff,
350                 y + (aTo.y - y) * aCoeff,
351                 z + (aTo.z - z) * aCoeff,
352                 w + (aTo.w - w) * aCoeff);
353         }
354         template<class T> Vector4<T>& Vector4<T>::makeLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff)
355         {
356             return set(
357                 aFrom.x + (aTo.x - aFrom.x) * aCoeff,
358                 aFrom.y + (aTo.y - aFrom.y) * aCoeff,
359                 aFrom.z + (aTo.z - aFrom.z) * aCoeff,
360                 aFrom.w + (aTo.w - aFrom.w) * aCoeff);
361         }
362         template<class T> Vector4<T> Vector4<T>::createLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff)
363         {
364             return Vector4<T>().makeLerped(aFrom, aTo, aCoeff);
365         }
366
367         template<class T> T Vector4<T>::dot(const Vector4<T>& aOther) const
368         {
369             return x * aOther.x + y * aOther.y + z * aOther.z + w * aOther.w;
370         }
371
372         template<class T> T Vector4<T>::lengthSqr() const
373         {
374             return x * x + y * y + z * z + w * w;
375         }
376         template<class T> T Vector4<T>::length() const
377         {
378             return effects_sqrt(lengthSqr());
379         }
380         template<class T> T Vector4<T>::distanceSqr(const SelfType& aOther) const
381         {
382             T _x = aOther.x - x;
383             T _y = aOther.y - y;
384             T _z = aOther.z - z;
385             T _w = aOther.w - w;
386             return _x * _x + _y * _y + _z * _z + _w * _w;
387         }
388         template<class T> T Vector4<T>::distance(const SelfType& aOther) const
389         {
390             return effects_sqrt(distanceSqr(aOther));
391         }
392
393         template<class T> _Vector2<T> Vector4<T>::swizzle(unsigned int aX, unsigned int aY) const
394         {
395             const T* pointer = getPointer();
396             return _Vector2<T>(pointer[aX], pointer[aY]);
397         }
398         template<class T> _Vector3<T> Vector4<T>::swizzle(unsigned int aX, unsigned int aY, unsigned int aZ) const
399         {
400             const T* pointer = getPointer();
401             return _Vector3<T>(pointer[aX], pointer[aY], pointer[aZ]);
402         }
403         template<class T> Vector4<T> Vector4<T>::swizzle(unsigned int aX, unsigned int aY, unsigned int aZ, unsigned int aW) const
404         {
405             const T* pointer = getPointer();
406             return Vector4<T>(pointer[aX], pointer[aY], pointer[aZ], pointer[aW]);
407         }
408
409         template<class T> inline bool isEqual(const Vector4<T>& aLhv, const Vector4<T>& aRhv, const T& aEpsilon = effects_epsilon<T>::epsilon())
410         {
411             return EffectsEqual(aLhv.x, aRhv.x, aEpsilon) && EffectsEqual(aLhv.y, aRhv.y, aEpsilon) && EffectsEqual(aLhv.z, aRhv.z, aEpsilon) && EffectsEqual(aLhv.w, aRhv.w, aEpsilon);
412         }
413         template<class T> inline Vector4<T> operator+(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
414         {
415             return Vector4<T>(aLhv.x + aRhv.x, aLhv.y + aRhv.y, aLhv.z + aRhv.z, aLhv.w + aRhv.w);
416         }
417         template<class T> inline Vector4<T> operator+(const Vector4<T>& aLhv, const T& aRhv)
418         {
419             return Vector4<T>(aLhv.x + aRhv, aLhv.y + aRhv, aLhv.z + aRhv, aLhv.w + aRhv);
420         }
421         template<class T> inline Vector4<T> operator+(const T& aLhv, const Vector4<T>& aRhv)
422         {
423             return Vector4<T>(aLhv + aRhv.x, aLhv + aRhv.y, aLhv + aRhv.z, aLhv + aRhv.w);
424         }
425         template<class T> inline Vector4<T> operator-(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
426         {
427             return Vector4<T>(aLhv.x - aRhv.x, aLhv.y - aRhv.y, aLhv.z - aRhv.z, aLhv.w - aRhv.w);
428         }
429         template<class T> inline Vector4<T> operator-(const Vector4<T>& aLhv, const T& aRhv)
430         {
431             return Vector4<T>(aLhv.x - aRhv, aLhv.y - aRhv, aLhv.z - aRhv, aLhv.w - aRhv);
432         }
433         template<class T> inline Vector4<T> operator-(const T& aLhv, const Vector4<T>& aRhv)
434         {
435             return Vector4<T>(aLhv - aRhv.x, aLhv - aRhv.y, aLhv - aRhv.z, aLhv - aRhv.w);
436         }
437         template<class T> inline Vector4<T> operator*(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
438         {
439             return Vector4<T>(aLhv.x * aRhv.x, aLhv.y * aRhv.y, aLhv.z * aRhv.z, aLhv.w * aRhv.w);
440         }
441         template<class T> inline Vector4<T> operator*(const Vector4<T>& aLhv, const T& aRhv)
442         {
443             return Vector4<T>(aLhv.x * aRhv, aLhv.y * aRhv, aLhv.z * aRhv, aLhv.w * aRhv);
444         }
445         template<class T> inline Vector4<T> operator*(const T& aLhv, const Vector4<T>& aRhv)
446         {
447             return Vector4<T>(aLhv * aRhv.x, aLhv * aRhv.y, aLhv * aRhv.z, aLhv * aRhv.w);
448         }
449         template<class T> inline Vector4<T> operator/(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
450         {
451             return Vector4<T>(aLhv.x / aRhv.x, aLhv.y / aRhv.y, aLhv.z / aRhv.z, aLhv.w / aRhv.w);
452         }
453         template<class T> inline Vector4<T> operator/(const Vector4<T>& aLhv, const T& aRhv)
454         {
455             return Vector4<T>(aLhv.x / aRhv, aLhv.y / aRhv, aLhv.z / aRhv, aLhv.w / aRhv);
456         }
457         template<class T> inline Vector4<T> operator/(const T& aLhv, const Vector4<T>& aRhv)
458         {
459             return Vector4<T>(aLhv / aRhv.x, aLhv / aRhv.y, aLhv / aRhv.z, aLhv / aRhv.w);
460         }
461         template<class T> inline bool operator==(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
462         {
463             return EffectsEqual(aLhv.x, aRhv.x) && EffectsEqual(aLhv.y, aRhv.y) && EffectsEqual(aLhv.z, aRhv.z) && EffectsEqual(aLhv.w, aRhv.w);
464         }
465         template<class T> inline bool operator==(const Vector4<T>& aLhv, const T& aRhv)
466         {
467             return EffectsEqual(aLhv.x, aRhv) && EffectsEqual(aLhv.y, aRhv) && EffectsEqual(aLhv.z, aRhv) && EffectsEqual(aLhv.w, aRhv);
468         }
469         template<class T> inline bool operator==(const T& aLhv, const Vector4<T>& aRhv)
470         {
471             return EffectsEqual(aLhv, aRhv.x) && EffectsEqual(aLhv, aRhv.x) && EffectsEqual(aLhv, aRhv.z) && EffectsEqual(aLhv, aRhv.w);
472         }
473         template<class T> inline bool operator!=(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
474         {
475             return !(aLhv == aRhv);
476         }
477         template<class T> inline bool operator!=(const Vector4<T>& aLhv, const T& aRhv)
478         {
479             return !(aLhv == aRhv);
480         }
481         template<class T> inline bool operator!=(const T& aLhv, const Vector4<T>& aRhv)
482         {
483             return !(aLhv == aRhv);
484         }
485 } } } } //Tizen::Ui::Effects::_Utils
486
487 #endif //_FUI_EFFECTS_INTERNAL_UTILS_VECTOR4_H_