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_UtilsVector4.h
20 * @brief The Vector4 class
24 #ifndef _FUI_EFFECTS_INTERNAL_UTILS_VECTOR4_H_
25 #define _FUI_EFFECTS_INTERNAL_UTILS_VECTOR4_H_
27 #include "FUiEffects_UtilsAdapterFunctions.h"
29 namespace Tizen { namespace Ui { namespace Effects { namespace _Utils
31 template<class T> class _Vector2;
32 template<class T> class _Vector3;
34 template<class T> class Vector4
37 typedef Vector4<T> SelfType;
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);
57 inline SelfType& operator=(const SelfType& aRhv);
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);
70 inline SelfType operator-() const;
71 inline SelfType& inverse();
72 inline SelfType getInversed() const;
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);
83 inline T* getPointer();
84 inline const T* getPointer() const;
86 inline SelfType& normalize();
87 inline SelfType getNormalized() const;
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);
94 inline T dot(const SelfType& aOther) const;
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;
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;
106 typedef Vector4<float> Vec4f;
107 typedef Vector4<double> Vec4d;
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);
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) {}
140 template<class T> Vector4<T>& Vector4<T>::operator=(const Vector4<T>& aRhv)
149 template<class T> Vector4<T>& Vector4<T>::set(const Vector4<T>& aOther)
151 return *this = aOther;
153 template<class T> Vector4<T>& Vector4<T>::set(const T& aX, const T& aY, const T& aZ, const T& aW)
161 template<class T> Vector4<T>& Vector4<T>::set(const T& aValue)
169 template<class T> Vector4<T>& Vector4<T>::set(const T* aValue)
177 template<class T> Vector4<T>& Vector4<T>::set(const _Vector2<T>& aXY, const T& aZ, const T& aW)
185 template<class T> Vector4<T>& Vector4<T>::set(const T& aX, const _Vector2<T>& aYZ, const T& aW)
193 template<class T> Vector4<T>& Vector4<T>::set(const T& aX, const T& aY, const _Vector2<T>& aZW)
201 template<class T> Vector4<T>& Vector4<T>::set(const _Vector3<T>& aXYZ, const T& aW)
209 template<class T> Vector4<T>& Vector4<T>::set(const T& aX, const _Vector3<T>& aYZW)
217 template<class T> Vector4<T>& Vector4<T>::set(const _Vector2<T>& aXY, const _Vector2<T>& aZW)
226 template<class T> Vector4<T> Vector4<T>::operator-() const
228 return getInversed();
230 template<class T> Vector4<T>& Vector4<T>::inverse()
238 template<class T> Vector4<T> Vector4<T>::getInversed() const
240 return Vector4<T>(-x, -y, -z, -w);
243 template<class T> Vector4<T>& Vector4<T>::operator+=(const Vector4<T>& aRhv)
251 template<class T> Vector4<T>& Vector4<T>::operator+=(const T& aRhv)
259 template<class T> Vector4<T>& Vector4<T>::operator-=(const Vector4<T>& aRhv)
267 template<class T> Vector4<T>& Vector4<T>::operator-=(const T& aRhv)
275 template<class T> Vector4<T>& Vector4<T>::operator*=(const Vector4<T>& aRhv)
283 template<class T> Vector4<T>& Vector4<T>::operator*=(const T& aRhv)
291 template<class T> Vector4<T>& Vector4<T>::operator/=(const Vector4<T>& aRhv)
299 template<class T> Vector4<T>& Vector4<T>::operator/=(const T& aRhv)
308 template<class T> T* Vector4<T>::getPointer()
312 template<class T> const T* Vector4<T>::getPointer() const
317 template<class T> Vector4<T>& Vector4<T>::normalize()
319 const static T t0(0.0f);
321 const T dist = length();
322 if(EffectsEqual(t0, dist))
325 return operator/=(dist);
327 template<class T> Vector4<T> Vector4<T>::getNormalized() const
329 const static T t0(0.0f);
331 const T dist = length();
332 if(EffectsEqual(t0, dist))
338 template<class T> Vector4<T>& Vector4<T>::lerp(const Vector4<T>& aTo, const T& aCoeff)
340 x += (aTo.x - x) * aCoeff;
341 y += (aTo.y - y) * aCoeff;
342 z += (aTo.z - z) * aCoeff;
343 w += (aTo.w - w) * aCoeff;
346 template<class T> Vector4<T> Vector4<T>::getLerped(const Vector4<T>& aTo, const T& aCoeff) const
349 x + (aTo.x - x) * aCoeff,
350 y + (aTo.y - y) * aCoeff,
351 z + (aTo.z - z) * aCoeff,
352 w + (aTo.w - w) * aCoeff);
354 template<class T> Vector4<T>& Vector4<T>::makeLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff)
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);
362 template<class T> Vector4<T> Vector4<T>::createLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff)
364 return Vector4<T>().makeLerped(aFrom, aTo, aCoeff);
367 template<class T> T Vector4<T>::dot(const Vector4<T>& aOther) const
369 return x * aOther.x + y * aOther.y + z * aOther.z + w * aOther.w;
372 template<class T> T Vector4<T>::lengthSqr() const
374 return x * x + y * y + z * z + w * w;
376 template<class T> T Vector4<T>::length() const
378 return effects_sqrt(lengthSqr());
380 template<class T> T Vector4<T>::distanceSqr(const SelfType& aOther) const
386 return _x * _x + _y * _y + _z * _z + _w * _w;
388 template<class T> T Vector4<T>::distance(const SelfType& aOther) const
390 return effects_sqrt(distanceSqr(aOther));
393 template<class T> _Vector2<T> Vector4<T>::swizzle(unsigned int aX, unsigned int aY) const
395 const T* pointer = getPointer();
396 return _Vector2<T>(pointer[aX], pointer[aY]);
398 template<class T> _Vector3<T> Vector4<T>::swizzle(unsigned int aX, unsigned int aY, unsigned int aZ) const
400 const T* pointer = getPointer();
401 return _Vector3<T>(pointer[aX], pointer[aY], pointer[aZ]);
403 template<class T> Vector4<T> Vector4<T>::swizzle(unsigned int aX, unsigned int aY, unsigned int aZ, unsigned int aW) const
405 const T* pointer = getPointer();
406 return Vector4<T>(pointer[aX], pointer[aY], pointer[aZ], pointer[aW]);
409 template<class T> inline bool isEqual(const Vector4<T>& aLhv, const Vector4<T>& aRhv, const T& aEpsilon = effects_epsilon<T>::epsilon())
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);
413 template<class T> inline Vector4<T> operator+(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
415 return Vector4<T>(aLhv.x + aRhv.x, aLhv.y + aRhv.y, aLhv.z + aRhv.z, aLhv.w + aRhv.w);
417 template<class T> inline Vector4<T> operator+(const Vector4<T>& aLhv, const T& aRhv)
419 return Vector4<T>(aLhv.x + aRhv, aLhv.y + aRhv, aLhv.z + aRhv, aLhv.w + aRhv);
421 template<class T> inline Vector4<T> operator+(const T& aLhv, const Vector4<T>& aRhv)
423 return Vector4<T>(aLhv + aRhv.x, aLhv + aRhv.y, aLhv + aRhv.z, aLhv + aRhv.w);
425 template<class T> inline Vector4<T> operator-(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
427 return Vector4<T>(aLhv.x - aRhv.x, aLhv.y - aRhv.y, aLhv.z - aRhv.z, aLhv.w - aRhv.w);
429 template<class T> inline Vector4<T> operator-(const Vector4<T>& aLhv, const T& aRhv)
431 return Vector4<T>(aLhv.x - aRhv, aLhv.y - aRhv, aLhv.z - aRhv, aLhv.w - aRhv);
433 template<class T> inline Vector4<T> operator-(const T& aLhv, const Vector4<T>& aRhv)
435 return Vector4<T>(aLhv - aRhv.x, aLhv - aRhv.y, aLhv - aRhv.z, aLhv - aRhv.w);
437 template<class T> inline Vector4<T> operator*(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
439 return Vector4<T>(aLhv.x * aRhv.x, aLhv.y * aRhv.y, aLhv.z * aRhv.z, aLhv.w * aRhv.w);
441 template<class T> inline Vector4<T> operator*(const Vector4<T>& aLhv, const T& aRhv)
443 return Vector4<T>(aLhv.x * aRhv, aLhv.y * aRhv, aLhv.z * aRhv, aLhv.w * aRhv);
445 template<class T> inline Vector4<T> operator*(const T& aLhv, const Vector4<T>& aRhv)
447 return Vector4<T>(aLhv * aRhv.x, aLhv * aRhv.y, aLhv * aRhv.z, aLhv * aRhv.w);
449 template<class T> inline Vector4<T> operator/(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
451 return Vector4<T>(aLhv.x / aRhv.x, aLhv.y / aRhv.y, aLhv.z / aRhv.z, aLhv.w / aRhv.w);
453 template<class T> inline Vector4<T> operator/(const Vector4<T>& aLhv, const T& aRhv)
455 return Vector4<T>(aLhv.x / aRhv, aLhv.y / aRhv, aLhv.z / aRhv, aLhv.w / aRhv);
457 template<class T> inline Vector4<T> operator/(const T& aLhv, const Vector4<T>& aRhv)
459 return Vector4<T>(aLhv / aRhv.x, aLhv / aRhv.y, aLhv / aRhv.z, aLhv / aRhv.w);
461 template<class T> inline bool operator==(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
463 return EffectsEqual(aLhv.x, aRhv.x) && EffectsEqual(aLhv.y, aRhv.y) && EffectsEqual(aLhv.z, aRhv.z) && EffectsEqual(aLhv.w, aRhv.w);
465 template<class T> inline bool operator==(const Vector4<T>& aLhv, const T& aRhv)
467 return EffectsEqual(aLhv.x, aRhv) && EffectsEqual(aLhv.y, aRhv) && EffectsEqual(aLhv.z, aRhv) && EffectsEqual(aLhv.w, aRhv);
469 template<class T> inline bool operator==(const T& aLhv, const Vector4<T>& aRhv)
471 return EffectsEqual(aLhv, aRhv.x) && EffectsEqual(aLhv, aRhv.x) && EffectsEqual(aLhv, aRhv.z) && EffectsEqual(aLhv, aRhv.w);
473 template<class T> inline bool operator!=(const Vector4<T>& aLhv, const Vector4<T>& aRhv)
475 return !(aLhv == aRhv);
477 template<class T> inline bool operator!=(const Vector4<T>& aLhv, const T& aRhv)
479 return !(aLhv == aRhv);
481 template<class T> inline bool operator!=(const T& aLhv, const Vector4<T>& aRhv)
483 return !(aLhv == aRhv);
485 } } } } //Tizen::Ui::Effects::_Utils
487 #endif //_FUI_EFFECTS_INTERNAL_UTILS_VECTOR4_H_