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_UtilsVector3.h
20 * @brief The _Vector3 class
24 #ifndef _FUI_EFFECTS_INTERNAL_UTILS_VECTOR3_H_
25 #define _FUI_EFFECTS_INTERNAL_UTILS_VECTOR3_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 Vector4;
34 template<class T> class _Vector3
37 typedef _Vector3<T> SelfType;
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);
52 inline SelfType& operator=(const SelfType& aRhv);
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);
61 inline SelfType operator-() const;
62 inline SelfType& inverse();
63 inline SelfType getInversed() const;
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);
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;
84 inline T* getPointer();
85 inline const T* getPointer() const;
87 inline SelfType& normalize();
88 inline SelfType& GetNormalizedVector3(); // for using in lua
89 inline SelfType getNormalized() const;
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);
96 inline T dot(const SelfType& aOther) const;
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);
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;
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;
114 typedef _Vector3<float> Vec3f;
115 typedef _Vector3<float> Vector3; //for use in lua
116 typedef _Vector3<double> Vec3d;
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);
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) {}
136 template<class T> _Vector3<T>& _Vector3<T>::operator=(const _Vector3<T>& aRhv)
147 template<class T> _Vector3<T>& _Vector3<T>::set(const _Vector3<T>& aOther)
149 return *this = aOther;
151 template<class T> _Vector3<T>& _Vector3<T>::set(const T& aX, const T& aY, const T& aZ)
158 template<class T> _Vector3<T>& _Vector3<T>::set(const _Vector2<T>& aXY, const T& aZ)
165 template<class T> _Vector3<T>& _Vector3<T>::set(const T& aX, const _Vector2<T>& aYZ)
172 template<class T> _Vector3<T>& _Vector3<T>::set(const T& aValue)
179 template<class T> _Vector3<T>& _Vector3<T>::set(const T* aValue)
187 template<class T> _Vector3<T> _Vector3<T>::operator-() const
189 return getInversed();
191 template<class T> _Vector3<T>& _Vector3<T>::inverse()
198 template<class T> _Vector3<T> _Vector3<T>::getInversed() const
200 return _Vector3<T>(-x, -y, -z);
203 template<class T> _Vector3<T>& _Vector3<T>::operator+=(const _Vector3<T>& aRhv)
210 template<class T> _Vector3<T>& _Vector3<T>::operator+=(const T& aRhv)
217 template<class T> _Vector3<T>& _Vector3<T>::operator-=(const _Vector3<T>& aRhv)
224 template<class T> _Vector3<T>& _Vector3<T>::operator-=(const T& aRhv)
231 template<class T> _Vector3<T>& _Vector3<T>::operator*=(const _Vector3<T>& aRhv)
238 template<class T> _Vector3<T>& _Vector3<T>::operator*=(const T& aRhv)
245 template<class T> _Vector3<T>& _Vector3<T>::operator/=(const _Vector3<T>& aRhv)
252 template<class T> _Vector3<T>& _Vector3<T>::operator/=(const T& aRhv)
260 template<class T> T* _Vector3<T>::getPointer()
264 template<class T> const T* _Vector3<T>::getPointer() const
269 template<class T> _Vector3<T>& _Vector3<T>::normalize()
271 const static T t0(0.0f);
273 const T dist = length();
274 if(EffectsEqual(t0, dist))
277 return operator/=(dist);
279 //--- for using in lua ---------------------------------------------------------
280 template<class T> _Vector3<T>& _Vector3<T>::GetNormalizedVector3()
284 //------------------------------------------------------------------------------
285 template<class T> _Vector3<T> _Vector3<T>::getNormalized() const
287 const static T t0(0.0f);
289 const T dist = length();
290 if(EffectsEqual(t0, dist))
296 template<class T> _Vector3<T>& _Vector3<T>::lerp(const _Vector3<T>& aTo, const T& aCoeff)
298 x += (aTo.x - x) * aCoeff;
299 y += (aTo.y - y) * aCoeff;
300 z += (aTo.z - z) * aCoeff;
303 template<class T> _Vector3<T> _Vector3<T>::getLerped(const _Vector3<T>& aTo, const T& aCoeff) const
306 x + (aTo.x - x) * aCoeff,
307 y + (aTo.y - y) * aCoeff,
308 z + (aTo.z - z) * aCoeff);
310 template<class T> _Vector3<T>& _Vector3<T>::makeLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff)
313 aFrom.x + (aTo.x - aFrom.x) * aCoeff,
314 aFrom.y + (aTo.y - aFrom.y) * aCoeff,
315 aFrom.z + (aTo.z - aFrom.z) * aCoeff);
317 template<class T> _Vector3<T> _Vector3<T>::createLerped(const SelfType& aFrom, const SelfType& aTo, const T& aCoeff)
319 return _Vector3<T>().makeLerped(aFrom, aTo, aCoeff);
322 template<class T> T _Vector3<T>::dot(const _Vector3<T>& aOther) const
324 return x * aOther.x + y * aOther.y + z * aOther.z;
327 template<class T> _Vector3<T>& _Vector3<T>::cross(const _Vector3<T>& aOther)
330 (y * aOther.z - z * aOther.y),
331 (z * aOther.x - x * aOther.z),
332 (x * aOther.y - y * aOther.x));
334 //--- for using in lua code -----------------------------------------------------------
335 template<class T> _Vector3<T>& _Vector3<T>::GetCrossVector3(const _Vector3<T>& aOther)
337 return cross(aOther);
339 //--------------------------------------------------------------------------------------
340 template<class T> _Vector3<T> _Vector3<T>::getCrossed(const _Vector3<T>& aOther) const
343 (y * aOther.z - z * aOther.y),
344 (z * aOther.x - x * aOther.z),
345 (x * aOther.y - y * aOther.x));
347 template<class T> _Vector3<T>& _Vector3<T>::makeCrossed(const SelfType& aFirst, const SelfType& aSecond)
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));
354 template<class T> _Vector3<T> _Vector3<T>::createCrossed(const SelfType& aFirst, const SelfType& aSecond)
356 return _Vector3<T>().makeCrossed(aFirst, aSecond);
359 template<class T> T _Vector3<T>::lengthSqr() const
361 return x * x + y * y + z * z;
363 template<class T> T _Vector3<T>::length() const
365 return effects_sqrt(lengthSqr());
367 template<class T> T _Vector3<T>::distanceSqr(const SelfType& aOther) const
372 return _x * _x + _y * _y + _z * _z;
374 template<class T> T _Vector3<T>::distance(const SelfType& aOther) const
376 return effects_sqrt(distanceSqr(aOther));
379 template<class T> _Vector2<T> _Vector3<T>::swizzle(unsigned int aX, unsigned int aY) const
381 const T* pointer = getPointer();
382 return _Vector2<T>(pointer[aX], pointer[aY]);
384 template<class T> _Vector3<T> _Vector3<T>::swizzle(unsigned int aX, unsigned int aY, unsigned int aZ) const
386 const T* pointer = getPointer();
387 return _Vector3<T>(pointer[aX], pointer[aY], pointer[aZ]);
389 template<class T> Vector4<T> _Vector3<T>::swizzle(unsigned int aX, unsigned int aY, unsigned int aZ, unsigned int aW) const
391 const T* pointer = getPointer();
392 return Vector4<T>(pointer[aX], pointer[aY], pointer[aZ], pointer[aW]);
395 template<class T> inline bool isEqual(const _Vector3<T>& aLhv, const _Vector3<T>& aRhv, const T& aEpsilon = effects_epsilon<T>::epsilon())
397 return EffectsEqual(aLhv.x, aRhv.x, aEpsilon) && EffectsEqual(aLhv.y, aRhv.y, aEpsilon) && EffectsEqual(aLhv.z, aRhv.z, aEpsilon);
399 template<class T> _Vector3<T> _Vector3<T>::operator+(const _Vector3<T>& aRhv) const
401 return _Vector3<T>(x + aRhv.x, y + aRhv.y, z + aRhv.z);
403 template<class T> _Vector3<T> _Vector3<T>::operator+(const T& aRhv) const
405 return _Vector3<T>(x + aRhv, y + aRhv, z + aRhv);
408 template<class T> inline _Vector3<T> operator+(const T& aLhv, const _Vector3<T>& aRhv)
410 return _Vector3<T>(aLhv + aRhv.x, aLhv + aRhv.y, aLhv + aRhv.z);
412 template<class T> _Vector3<T> _Vector3<T>::operator-(const _Vector3<T>& aRhv) const
414 return _Vector3<T>(x - aRhv.x, y - aRhv.y, z - aRhv.z);
416 template<class T> _Vector3<T> _Vector3<T>::operator-(const T& aRhv) const
418 return _Vector3<T>(x - aRhv, y - aRhv, z - aRhv);
421 template<class T> inline _Vector3<T> operator-(const T& aLhv, const _Vector3<T>& aRhv)
423 return _Vector3<T>(aLhv - aRhv.x, aLhv - aRhv.y, aLhv - aRhv.z);
425 template<class T> _Vector3<T> _Vector3<T>::operator*(const _Vector3<T>& aRhv) const
427 return _Vector3<T>(x * aRhv.x, y * aRhv.y, z * aRhv.z);
429 template<class T> _Vector3<T> _Vector3<T>::operator*(const T& aRhv) const
431 return _Vector3<T>(x * aRhv, y * aRhv, z * aRhv);
434 template<class T> inline _Vector3<T> operator*(const T& aLhv, const _Vector3<T>& aRhv)
436 return _Vector3<T>(aLhv * aRhv.x, aLhv * aRhv.y, aLhv * aRhv.z);
438 template<class T> _Vector3<T> _Vector3<T>::operator/(const _Vector3<T>& aRhv) const
440 if (EffectsEqual(aRhv.x, 0) || EffectsEqual(aRhv.y, 0) || EffectsEqual(aRhv.z, 0))
442 return _Vector3<T>(0, 0, 0);
444 return _Vector3<T>(x / aRhv.x, y / aRhv.y, z / aRhv.z);
446 template<class T> _Vector3<T> _Vector3<T>::operator/(const T& aRhv) const
448 if (EffectsEqual(aRhv, 0))
450 return _Vector3<T>(0, 0, 0);
452 return _Vector3<T>(x / aRhv, y / aRhv, z / aRhv);
455 template<class T> inline _Vector3<T> operator/(const T& aLhv, const _Vector3<T>& aRhv)
457 return _Vector3<T>(aLhv / aRhv.x, aLhv / aRhv.y, aLhv / aRhv.z);
459 template<class T> bool _Vector3<T>::operator==(const _Vector3<T>& aRhv) const
461 return EffectsEqual(x, aRhv.x) && EffectsEqual(y, aRhv.y) && EffectsEqual(z, aRhv.z);
464 template<class T> inline bool operator==(const _Vector3<T>& aLhv, const T& aRhv)
466 return EffectsEqual(aLhv.x, aRhv) && EffectsEqual(aLhv.y, aRhv) && EffectsEqual(aLhv.z, aRhv);
468 template<class T> inline bool operator==(const T& aLhv, const _Vector3<T>& aRhv)
470 return EffectsEqual(aLhv, aRhv.x) && EffectsEqual(aLhv, aRhv.x) && EffectsEqual(aLhv, aRhv.z);
472 template<class T> inline bool operator!=(const _Vector3<T>& aLhv, const _Vector3<T>& aRhv)
474 return !(aLhv == aRhv);
476 template<class T> inline bool operator!=(const _Vector3<T>& aLhv, const T& aRhv)
478 return !(aLhv == aRhv);
480 template<class T> inline bool operator!=(const T& aLhv, const _Vector3<T>& aRhv)
482 return !(aLhv == aRhv);
484 } } } } //Tizen::Ui::Effects::_Utils
486 #endif //_FUI_EFFECTS_INTERNAL_UTILS_VECTOR3_H_