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