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