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.
19 * @file FUiEffects_UtilsVector2.h
20 * @brief The _Vector2 class
24 #ifndef _FUI_EFFECTS_INTERNAL_UTILS_VECTOR2_H_
25 #define _FUI_EFFECTS_INTERNAL_UTILS_VECTOR2_H_
27 #include "FUiEffects_UtilsAdapterFunctions.h"
29 namespace Tizen { namespace Ui { namespace Effects { namespace _Utils
31 template<class T> class _Vector3;
32 template<class T> class Vector4;
34 template<class T> class _Vector2
37 typedef _Vector2<T> SelfType;
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);
49 inline SelfType& operator=(const SelfType& aRhv);
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);
56 inline SelfType operator-() const;
57 inline SelfType& inverse();
58 inline SelfType getInversed() const;
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);
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;
79 inline T* getPointer();
80 inline const T* getPointer() const;
82 inline SelfType& normalize();
83 inline SelfType getNormalized() const;
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&);
90 inline T dot(const SelfType& aOther) const;
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);
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;
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;
107 typedef _Vector2<float> Vec2f;
108 typedef _Vector2<float> Vector2; //for use in lua
109 typedef _Vector2<double> Vec2d;
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);
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) {}
127 template<class T> _Vector2<T>& _Vector2<T>::operator=(const _Vector2<T>& aRhv)
137 template<class T> _Vector2<T>& _Vector2<T>::set(const _Vector2<T>& aOther)
139 return *this = aOther;
141 template<class T> _Vector2<T>& _Vector2<T>::set(const T& aX, const T& aY)
147 template<class T> _Vector2<T>& _Vector2<T>::set(const T& aValue)
153 template<class T> _Vector2<T>& _Vector2<T>::set(const T* aValue)
160 template<class T> _Vector2<T> _Vector2<T>::operator-() const
162 return getInversed();
164 template<class T> _Vector2<T>& _Vector2<T>::inverse()
170 template<class T> _Vector2<T> _Vector2<T>::getInversed() const
172 return _Vector2<T>(-x, -y);
175 template<class T> _Vector2<T>& _Vector2<T>::operator+=(const _Vector2<T>& aRhv)
181 template<class T> _Vector2<T>& _Vector2<T>::operator+=(const T& aRhv)
187 template<class T> _Vector2<T>& _Vector2<T>::operator-=(const _Vector2<T>& aRhv)
193 template<class T> _Vector2<T>& _Vector2<T>::operator-=(const T& aRhv)
199 template<class T> _Vector2<T>& _Vector2<T>::operator*=(const _Vector2<T>& aRhv)
205 template<class T> _Vector2<T>& _Vector2<T>::operator*=(const T& aRhv)
211 template<class T> _Vector2<T>& _Vector2<T>::operator/=(const _Vector2<T>& aRhv)
217 template<class T> _Vector2<T>& _Vector2<T>::operator/=(const T& aRhv)
224 template<class T> T* _Vector2<T>::getPointer()
228 template<class T> const T* _Vector2<T>::getPointer() const
233 template<class T> _Vector2<T>& _Vector2<T>::normalize()
235 const static T t0(0.0f);
237 const T dist = length();
238 if(EffectsEqual(t0, dist))
241 return operator/=(dist);
243 template<class T> _Vector2<T> _Vector2<T>::getNormalized() const
245 const static T t0(0.0f);
247 const T dist = length();
248 if(EffectsEqual(t0, dist))
254 template<class T> _Vector2<T>& _Vector2<T>::lerp(const _Vector2<T>& aTo, const T& aCoeff)
256 x += (aTo.x - x) * aCoeff;
257 y += (aTo.y - y) * aCoeff;
260 template<class T> _Vector2<T> _Vector2<T>::getLerped(const _Vector2<T>& aTo, const T& aCoeff) const
263 x + (aTo.x - x) * aCoeff,
264 y + (aTo.y - y) * aCoeff);
266 template<class T> _Vector2<T>& _Vector2<T>::makeLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff)
269 aFrom.x + (aTo.x - aFrom.x) * aCoeff,
270 aFrom.y + (aTo.y - aFrom.y) * aCoeff);
272 template<class T> _Vector2<T> _Vector2<T>::createLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff)
274 return _Vector2<T>().makeLerped(aFrom, aTo, aCoeff);
277 template<class T> T _Vector2<T>::dot(const _Vector2<T>& aOther) const
279 return x * aOther.x + y * aOther.y;
282 template<class T> _Vector2<T>& _Vector2<T>::cross(const _Vector2<T>& aOther)
288 template<class T> _Vector2<T> _Vector2<T>::getCrossed(const _Vector2<T>& aOther) const
294 template<class T> _Vector2<T>& _Vector2<T>::makeCrossed(const SelfType& aFirst, const SelfType& aSecond) const
297 aFirst.y - aSecond.y,
298 aSecond.x - aFirst.x);
300 template<class T> _Vector2<T> _Vector2<T>::createCrossed(const SelfType& aFirst, const SelfType& aSecond)
302 return _Vector2<T>().makeCrossed(aFirst, aSecond);
305 template<class T> T _Vector2<T>::lengthSqr() const
307 return x * x + y * y;
309 template<class T> T _Vector2<T>::length() const
311 return effects_sqrt(lengthSqr());
313 template<class T> T _Vector2<T>::distanceSqr(const SelfType& aOther) const
315 const T _x = aOther.x - x;
316 const T _y = aOther.y - y;
317 return _x * _x + _y * _y;
319 template<class T> T _Vector2<T>::distance(const SelfType& aOther) const
321 return effects_sqrt(distanceSqr(aOther));
324 template<class T> _Vector2<T> _Vector2<T>::swizzle(unsigned int aX, unsigned int aY) const
326 const T* pointer = getPointer();
327 return _Vector2<T>(pointer[aX], pointer[aY]);
330 template<class T> _Vector3<T> _Vector2<T>::swizzle(unsigned int aX, unsigned int aY, unsigned int aZ) const
332 const T* pointer = getPointer();
333 return _Vector3<T>(pointer[aX], pointer[aY], pointer[aZ]);
336 template<class T> Vector4<T> _Vector2<T>::swizzle(unsigned int aX, unsigned int aY, unsigned int aZ, unsigned int aW) const
338 const T* pointer = getPointer();
339 return Vector4<T>(pointer[aX], pointer[aY], pointer[aZ], pointer[aW]);
343 template<class T> inline bool isEqual(const _Vector2<T>& aLhv, const _Vector2<T>& aRhv, const T& aEpsilon = effects_epsilon<T>::epsilon())
345 return EffectsEqual(aLhv.x, aRhv.x, aEpsilon) && EffectsEqual(aLhv.y, aRhv.y, aEpsilon);
348 template<class T> _Vector2<T> _Vector2<T>::operator+(const _Vector2<T>& aRhv) const
350 return _Vector2<T>(x + aRhv.x, y + aRhv.y);
352 template<class T> _Vector2<T> _Vector2<T>::operator+(const T& aRhv) const
354 return _Vector2<T>(x + aRhv, y + aRhv);
357 template<class T> inline _Vector2<T> operator+(const T& aLhv, const _Vector2<T>& aRhv)
359 return _Vector2<T>(aLhv + aRhv.x, aLhv + aRhv.y);
361 template<class T> _Vector2<T> _Vector2<T>::operator-(const _Vector2<T>& aRhv) const
363 return _Vector2<T>(x - aRhv.x, y - aRhv.y);
365 template<class T> _Vector2<T> _Vector2<T>::operator-(const T& aRhv) const
367 return _Vector2<T>(x - aRhv, y - aRhv);
370 template<class T> inline _Vector2<T> operator-(const T& aLhv, const _Vector2<T>& aRhv)
372 return _Vector2<T>(aLhv - aRhv.x, aLhv - aRhv.y);
374 template<class T> _Vector2<T> _Vector2<T>::operator*(const _Vector2<T>& aRhv) const
376 return _Vector2<T>(x * aRhv.x, y * aRhv.y);
378 template<class T> _Vector2<T> _Vector2<T>::operator*(const T& aRhv) const
380 return _Vector2<T>(x * aRhv, y * aRhv);
383 template<class T> inline _Vector2<T> operator*(const T& aLhv, const _Vector2<T>& aRhv)
385 return _Vector2<T>(aLhv * aRhv.x, aLhv * aRhv.y);
387 template<class T> _Vector2<T> _Vector2<T>::operator/(const _Vector2<T>& aRhv) const
389 if (EffectsEqual(aRhv.x, 0) || EffectsEqual(aRhv.y, 0))
391 return _Vector2<T>(0, 0);
393 return _Vector2<T>(x / aRhv.x, y / aRhv.y);
395 template<class T> _Vector2<T> _Vector2<T>::operator/(const T& aRhv) const
397 if (EffectsEqual(aRhv, 0))
399 return _Vector2<T>(0, 0);
401 return _Vector2<T>(x / aRhv, y / aRhv);
404 template<class T> inline _Vector2<T> operator/(const T& aLhv, const _Vector2<T>& aRhv)
406 return _Vector2<T>(aLhv / aRhv.x, aLhv / aRhv.y);
408 template<class T> bool _Vector2<T>::operator==(const _Vector2<T>& aRhv) const
410 return EffectsEqual(x, aRhv.x) && EffectsEqual(y, aRhv.y);
413 template<class T> inline bool operator==(const _Vector2<T>& aLhv, const T& aRhv)
415 return EffectsEqual(aLhv.x, aRhv) && EffectsEqual(aLhv.y, aRhv);
417 template<class T> inline bool operator==(const T& aLhv, const _Vector2<T>& aRhv)
419 return EffectsEqual(aLhv, aRhv.x) && EffectsEqual(aLhv, aRhv.x);
421 template<class T> inline bool operator!=(const _Vector2<T>& aLhv, const _Vector2<T>& aRhv)
423 return !(aLhv == aRhv);
425 template<class T> inline bool operator!=(const _Vector2<T>& aLhv, const T& aRhv)
427 return !(aLhv == aRhv);
429 template<class T> inline bool operator!=(const T& aLhv, const _Vector2<T>& aRhv)
431 return !(aLhv == aRhv);
433 } } } } //Tizen::Ui::Effects::_Utils
435 #endif //_FUI_EFFECTS_INTERNAL_UTILS_VECTOR2_H_