1 #ifndef __DALI_VECTOR_4_H__
2 #define __DALI_VECTOR_4_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/type-traits.h>
31 * @addtogroup dali_core_math
39 * @brief A four dimensional vector.
41 * Components can be used as position or offset (x,y,z,w); color (r,g,b,a) or texture coords(s,t,p,q)
44 struct DALI_IMPORT_API Vector4
47 // xrs, ygt, zbp and waq must be consecutive in memory.
48 // No other data must be added before xrs member.
49 // No virtual methods must be added to this struct.
54 * @brief Default constructor, initializes the vector to 0.
66 * @brief Conversion constructor from four floats.
69 * @param [in] x (or r/s) component
70 * @param [in] y (or g/t) component
71 * @param [in] z (or b/p) component
72 * @param [in] w (or a/q) component
74 explicit Vector4(float x, float y, float z, float w)
84 * @brief Conversion constructor from an array of four floats.
87 * @param [in] array of either xyzw/rgba/stpq
89 explicit Vector4(const float* array)
98 * @brief Conversion constructor from Vector2.
101 * @param [in] vec2 to copy from, z and w are initialized to 0
103 explicit Vector4( const Vector2& vec2 );
106 * @brief Conversion constructor from Vector3.
109 * @param [in] vec3 to copy from, w is initialized to 0
111 explicit Vector4( const Vector3& vec3 );
114 static const Vector4 ONE; ///< (1.0f,1.0f,1.0f,1.0f)
115 static const Vector4 XAXIS; ///< (1.0f,0.0f,0.0f,0.0f)
116 static const Vector4 YAXIS; ///< (0.0f,1.0f,0.0f,0.0f)
117 static const Vector4 ZAXIS; ///< (0.0f,0.0f,1.0f,0.0f)
118 static const Vector4 ZERO; ///< (0.0f, 0.0f, 0.0f, 0.0f)
123 * @brief Assignment operator.
126 * @param [in] array of floats
129 Vector4& operator=(const float* array)
140 * @brief Assignment operator.
142 * Only sets x and y. z and w are left as they were
144 * @param [in] vec2 to assign from.
147 Vector4& operator=(const Vector2& vec2 );
150 * @brief Assignment operator.
152 * Only sets x and y and z. w is left as it was
154 * @param [in] vec3 to assign from
157 Vector4& operator=(const Vector3& vec3 );
160 * @brief Addition operator.
163 * @param[in] rhs vector to add.
164 * @return A vector containing the result of the addition
166 Vector4 operator+(const Vector4 & rhs) const
174 * @brief Addition assignment operator.
177 * @param[in] rhs vector to add.
180 Vector4& operator+=(const Vector4& rhs)
191 * @brief Subtraction operator.
194 * @param[in] rhs the vector to subtract
195 * @return A vector containing the result of the subtraction
197 Vector4 operator-(const Vector4& rhs) const
207 * @brief Subtraction assignment operator.
210 * @param[in] rhs the vector to subtract
213 Vector4& operator-=(const Vector4& rhs)
224 * @brief Multiplication operator.
227 * @param[in] rhs the vector to multiply
228 * @return A vector containing the result of the multiplication
230 Vector4 operator*(const Vector4& rhs) const
238 * @brief Multiplication operator.
241 * @param[in] rhs the float value to scale the vector
242 * @return A vector containing the result of the scaling
244 Vector4 operator*(float rhs) const
246 return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
250 * @brief Multiplication assignment operator.
253 * @param[in] rhs the vector to multiply
256 Vector4& operator*=(const Vector4& rhs)
267 * @brief Multiplication assignment operator.
270 * @param[in] rhs the float value to scale the vector
273 Vector4& operator*=(float rhs)
284 * @brief Division operator.
287 * @param[in] rhs the vector to divide
288 * @return A vector containing the result of the division
290 Vector4 operator/(const Vector4 & rhs) const
298 * @brief Division operator.
301 * @param[in] rhs The float value to scale the vector by
302 * @return A vector containing the result of the scaling
304 Vector4 operator/(float rhs) const
306 float oneOver = 1.0f / rhs;
307 return Vector4(x * oneOver, y * oneOver, z * oneOver, w * oneOver);
311 * @brief Division assignment operator.
314 * @param[in] rhs the vector to divide
317 Vector4& operator/=(const Vector4& rhs)
328 * @brief Division assignment operator.
331 * @param[in] rhs the float value to scale the vector by
334 Vector4& operator/=(float rhs)
336 const float oneOver = 1.0f / rhs;
346 * @brief Unary negation operator.
349 * @return the negative value
351 Vector4 operator-() const
353 Vector4 temp(-x, -y, -z, -w);
359 * @brief Equality operator.
361 * Utilises appropriate machine epsilon values.
364 * @param[in] rhs The vector to test against
365 * @return true if the vectors are equal
367 bool operator==(const Vector4 &rhs) const;
370 * @brief Inequality operator.
372 * Utilises appropriate machine epsilon values.
375 * @param[in] rhs The vector to test against
376 * @return true if the vectors are not equal
378 bool operator!=(const Vector4 &rhs) const
380 return !(*this == rhs);
384 * @brief Const array subscript operator overload.
386 * Asserts if index is out of range. Should be 0, 1, 2 or 3
388 * @param[in] index Subscript
389 * @return The float at the given index
391 const float& operator[](const unsigned int index) const
393 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
395 return AsFloat()[index];
399 * @brief Mutable array subscript operator overload.
401 * Asserts if index is out of range. Should be 0, 1, 2 or 3
403 * @param[in] index Subscript index
404 * @return The float at the given index
406 float& operator[](const unsigned int index)
408 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
410 return AsFloat()[index];
414 * @brief Returns the dot product of this vector (4d) and another vector (3d).
416 * The dot product is the length of one vector in the direction of another vector.
417 * This is great for lighting, threshold testing the angle between two unit vectors,
418 * calculating the distance between two points in a particular direction.
420 * @param [in] other the other vector
421 * @return the dot product
423 float Dot(const Vector3& other) const;
426 * @brief Returns the dot product of this vector and another vector.
428 * The dot product is the length of one vector in the direction of another vector.
429 * This is great for lighting, threshold testing the angle between two unit vectors,
430 * calculating the distance between two points in a particular direction.
432 * @param [in] other the other vector
433 * @return the dot product
435 float Dot(const Vector4& other) const;
438 * @brief Returns the 4d dot product of this vector and another vector.
441 * @param [in] other the other vector
442 * @return the dot product
444 float Dot4(const Vector4& other) const;
447 * @brief Returns the cross produce of this vector and another vector.
449 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
450 * two vectors. This is great for calculating normals and making matrices orthogonal.
453 * @param [in] other the other vector
454 * @return A vector containing the cross product
456 Vector4 Cross(const Vector4& other) const;
459 * @brief Returns the length of the vector.
462 * @return the length.
464 float Length() const;
467 * @brief Returns the length of the vector squared.
469 * This is faster than using Length() when performing
470 * threshold checks as it avoids use of the square root.
472 * @return the length of the vector squared.
474 float LengthSquared() const;
477 * @brief Normalizes the vector.
479 * Sets the vector to unit length whilst maintaining its direction.
485 * @brief Clamps the vector between minimum and maximum vectors.
488 * @param [in] min the minimum vector
489 * @param [in] max the maximum vector
491 void Clamp( const Vector4& min, const Vector4& max );
494 * @brief Returns the contents of the vector as an array of 4 floats.
496 * The order of the values in this array are as follows:
502 * @return the vector contents as an array of 4 floats.
503 * @note inlined for performance reasons (generates less code than a function call)
505 const float* AsFloat() const {return &x;}
508 * @brief Returns the contents of the vector as an array of 4 floats.
510 * The order of the values in this array are as follows:
516 * @return the vector contents as an array of 4 floats.
517 * @note inlined for performance reasons (generates less code than a function call)
519 float* AsFloat() {return &x;}
524 // xrs, ygt, zbp and waq must be consecutive in memory.
525 // No other data must be added before xrs member.
526 // No virtual methods must be added to this struct.
530 float x; ///< x component
531 float r; ///< red component
532 float s; ///< s component
536 float y; ///< y component
537 float g; ///< green component
538 float t; ///< t component
542 float z; ///< z component
543 float b; ///< blue component
544 float p; ///< p component
548 float w; ///< w component
549 float a; ///< alpha component
550 float q; ///< q component
555 * @brief Print a Vector4.
558 * @param [in] o The output stream operator.
559 * @param [in] vector The vector to print.
560 * @return The output stream operator.
562 DALI_IMPORT_API std::ostream& operator<<(std::ostream& o, const Vector4& vector);
565 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
567 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 0,0,1,2
569 * @param [in] a a vector
570 * @param [in] b a vector
571 * @return a vector containing the minimum of each component from a and b
573 inline Vector4 Min( const Vector4& a, const Vector4& b )
575 return Vector4( a.x < b.x ? a.x : b.x,
576 a.y < b.y ? a.y : b.y,
577 a.z < b.z ? a.z : b.z,
578 a.w < b.w ? a.w : b.w );
582 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
584 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 4,1,2,3
586 * @param [in] a a vector
587 * @param [in] b a vector
588 * @return a vector containing the maximum of each component from a and b
590 inline Vector4 Max( const Vector4& a, const Vector4& b )
592 return Vector4( a.x > b.x ? a.x : b.x,
593 a.y > b.y ? a.y : b.y,
594 a.z > b.z ? a.z : b.z,
595 a.w > b.w ? a.w : b.w );
599 * @brief Clamps each of vector v's components between minimum and maximum values.
602 * @param [in] v a vector
603 * @param [in] min the minimum value
604 * @param [in] max the maximum value
605 * @return a vector containing the clamped components of v
607 DALI_IMPORT_API Vector4 Clamp( const Vector4& v, const float& min, const float& max );
609 // Allow Vector4 to be treated as a POD type
610 template <> struct TypeTraits< Vector4 > : public BasicTypes< Vector4 > { enum { IS_TRIVIAL_TYPE = true }; };
617 #endif // __DALI_VECTOR_4_H__