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>
34 * @brief A four dimensional vector.
36 * Components can be used as position or offset (x,y,z,w); color (r,g,b,a) or texture coords(s,t,p,q)
38 struct DALI_IMPORT_API Vector4
41 // xrs, ygt, zbp and waq must be consecutive in memory.
42 // No other data must be added before xrs member.
43 // No virtual methods must be added to this struct.
48 * @brief Default constructor, initializes the vector to 0.
59 * @brief Conversion constructor from four floats.
61 * @param [in] x (or r/s) component
62 * @param [in] y (or g/t) component
63 * @param [in] z (or b/p) component
64 * @param [in] w (or a/q) component
66 explicit Vector4(float x, float y, float z, float w)
76 * @brief Conversion constructor from an array of four floats.
78 * @param [in] array of either xyzw/rgba/stpq
80 explicit Vector4(const float* array)
89 * @brief Conversion constructor from Vector2.
91 * @param [in] vec2 to copy from, z and w are initialized to 0
93 explicit Vector4( const Vector2& vec2 );
96 * @brief Conversion constructor from Vector3.
98 * @param [in] vec3 to copy from, w is initialized to 0
100 explicit Vector4( const Vector3& vec3 );
103 static const Vector4 ONE; ///< (1.0f,1.0f,1.0f,1.0f)
104 static const Vector4 XAXIS; ///< (1.0f,0.0f,0.0f,0.0f)
105 static const Vector4 YAXIS; ///< (0.0f,1.0f,0.0f,0.0f)
106 static const Vector4 ZAXIS; ///< (0.0f,0.0f,1.0f,0.0f)
107 static const Vector4 ZERO; ///< (0.0f, 0.0f, 0.0f, 0.0f)
112 * @brief Assignment operator.
114 * @param [in] array of floats
117 Vector4& operator=(const float* array)
128 * @brief Assignment operator.
130 * Only sets x and y. z and w are left as they were
131 * @param [in] vec2 to assign from.
134 Vector4& operator=(const Vector2& vec2 );
137 * @brief Assignment operator.
139 * Only sets x and y and z. w is left as it was
140 * @param [in] vec3 to assign from
143 Vector4& operator=(const Vector3& vec3 );
146 * @brief Addition operator.
148 * @param[in] rhs vector to add.
149 * @return A vector containing the result of the addition
151 Vector4 operator+(const Vector4 & rhs) const
159 * @brief Addition assignment operator.
161 * @param[in] rhs vector to add.
164 Vector4& operator+=(const Vector4& rhs)
175 * @brief Subtraction operator.
177 * @param[in] rhs the vector to subtract
178 * @return A vector containing the result of the subtraction
180 Vector4 operator-(const Vector4& rhs) const
190 * @brief Subtraction assignment operator.
192 * @param[in] rhs the vector to subtract
195 Vector4& operator-=(const Vector4& rhs)
206 * @brief Multiplication operator.
208 * @param[in] rhs the vector to multiply
209 * @return A vector containing the result of the multiplication
211 Vector4 operator*(const Vector4& rhs) const
219 * @brief Multiplication operator.
221 * @param[in] rhs the float value to scale the vector
222 * @return A vector containing the result of the scaling
224 Vector4 operator*(float rhs) const
226 return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
230 * @brief Multiplication assignment operator.
232 * @param[in] rhs the vector to multiply
235 Vector4& operator*=(const Vector4& rhs)
246 * @brief Multiplication assignment operator.
248 * @param[in] rhs the float value to scale the vector
251 Vector4& operator*=(float rhs)
262 * @brief Division operator.
264 * @param[in] rhs the vector to divide
265 * @return A vector containing the result of the division
267 Vector4 operator/(const Vector4 & rhs) const
275 * @brief Division operator.
277 * @param[in] rhs The float value to scale the vector by
278 * @return A vector containing the result of the scaling
280 Vector4 operator/(float rhs) const
282 float oneOver = 1.0f / rhs;
283 return Vector4(x * oneOver, y * oneOver, z * oneOver, w * oneOver);
287 * @brief Division assignment operator.
289 * @param[in] rhs the vector to divide
292 Vector4& operator/=(const Vector4& rhs)
303 * @brief Division assignment operator.
305 * @param[in] rhs the float value to scale the vector by
308 Vector4& operator/=(float rhs)
310 const float oneOver = 1.0f / rhs;
320 * @brief Unary negation operator.
322 * @return the negative value
324 Vector4 operator-() const
326 Vector4 temp(-x, -y, -z, -w);
332 * @brief Equality operator.
334 * Utilises appropriate machine epsilon values.
336 * @param[in] rhs The vector to test against
337 * @return true if the vectors are equal
339 bool operator==(const Vector4 &rhs) const;
342 * @brief Inequality operator.
344 * Utilises appropriate machine epsilon values.
346 * @param[in] rhs The vector to test against
347 * @return true if the vectors are not equal
349 bool operator!=(const Vector4 &rhs) const
351 return !(*this == rhs);
355 * @brief Const array subscript operator overload.
357 * Asserts if index is out of range. Should be 0, 1, 2 or 3
358 * @param[in] index Subscript
359 * @return The float at the given index
361 const float& operator[](const unsigned int index) const
363 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
365 return AsFloat()[index];
369 * @brief Mutable array subscript operator overload.
371 * Asserts if index is out of range. Should be 0, 1, 2 or 3
372 * @param[in] index Subscript index
373 * @return The float at the given index
375 float& operator[](const unsigned int index)
377 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
379 return AsFloat()[index];
383 * @brief Returns the dot product of this vector (4d) and another vector (3d).
385 * The dot product is the length of one vector in the direction of another vector.
386 * This is great for lighting, threshold testing the angle between two unit vectors,
387 * calculating the distance between two points in a particular direction.
388 * @param [in] other the other vector
389 * @return the dot product
391 float Dot(const Vector3& other) const;
394 * @brief Returns the dot product of this vector and another vector.
396 * The dot product is the length of one vector in the direction of another vector.
397 * This is great for lighting, threshold testing the angle between two unit vectors,
398 * calculating the distance between two points in a particular direction.
399 * @param [in] other the other vector
400 * @return the dot product
402 float Dot(const Vector4& other) const;
405 * @brief Returns the 4d dot product of this vector and another vector.
407 * @param [in] other the other vector
408 * @return the dot product
410 float Dot4(const Vector4& other) const;
413 * @brief Returns the cross produce of this vector and another vector.
415 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
416 * two vectors. This is great for calculating normals and making matrices orthogonal.
418 * @param [in] other the other vector
419 * @return A vector containing the cross product
421 Vector4 Cross(const Vector4& other) const;
424 * @brief Returns the length of the vector.
426 * @return the length.
428 float Length() const;
431 * @brief Returns the length of the vector squared.
433 * This is faster than using Length() when performing
434 * threshold checks as it avoids use of the square root.
435 * @return the length of the vector squared.
437 float LengthSquared() const;
440 * @brief Normalizes the vector.
442 * Sets the vector to unit length whilst maintaining its direction.
447 * @brief Clamps the vector between minimum and maximum vectors.
449 * @param [in] min the minimum vector
450 * @param [in] max the maximum vector
452 void Clamp( const Vector4& min, const Vector4& max );
455 * @brief Returns the contents of the vector as an array of 4 floats.
457 * The order of the values in this array are as follows:
462 * @note inlined for performance reasons (generates less code than a function call)
463 * @return the vector contents as an array of 4 floats.
465 const float* AsFloat() const {return &x;}
468 * @brief Returns the contents of the vector as an array of 4 floats.
470 * The order of the values in this array are as follows:
475 * @note inlined for performance reasons (generates less code than a function call)
476 * @return the vector contents as an array of 4 floats.
478 float* AsFloat() {return &x;}
483 // xrs, ygt, zbp and waq must be consecutive in memory.
484 // No other data must be added before xrs member.
485 // No virtual methods must be added to this struct.
489 float x; ///< x component
490 float r; ///< red component
491 float s; ///< s component
495 float y; ///< y component
496 float g; ///< green component
497 float t; ///< t component
501 float z; ///< z component
502 float b; ///< blue component
503 float p; ///< p component
507 float w; ///< w component
508 float a; ///< alpha component
509 float q; ///< q component
514 * @brief Print a Vector4.
516 * @param [in] o The output stream operator.
517 * @param [in] vector The vector to print.
518 * @return The output stream operator.
520 DALI_IMPORT_API std::ostream& operator<<(std::ostream& o, const Vector4& vector);
523 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
525 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 0,0,1,2
526 * @param [in] a a vector
527 * @param [in] b a vector
528 * @return a vector containing the minimum of each component from a and b
530 inline Vector4 Min( const Vector4& a, const Vector4& b )
532 return Vector4( a.x < b.x ? a.x : b.x,
533 a.y < b.y ? a.y : b.y,
534 a.z < b.z ? a.z : b.z,
535 a.w < b.w ? a.w : b.w );
539 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
541 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 4,1,2,3
542 * @param [in] a a vector
543 * @param [in] b a vector
544 * @return a vector containing the maximum of each component from a and b
546 inline Vector4 Max( const Vector4& a, const Vector4& b )
548 return Vector4( a.x > b.x ? a.x : b.x,
549 a.y > b.y ? a.y : b.y,
550 a.z > b.z ? a.z : b.z,
551 a.w > b.w ? a.w : b.w );
555 * @brief Clamps each of vector v's components between minimum and maximum values.
557 * @param [in] v a vector
558 * @param [in] min the minimum value
559 * @param [in] max the maximum value
560 * @return a vector containing the clamped components of v
562 DALI_IMPORT_API Vector4 Clamp( const Vector4& v, const float& min, const float& max );
564 // Allow Vector4 to be treated as a POD type
565 template <> struct TypeTraits< Vector4 > : public BasicTypes< Vector4 > { enum { IS_TRIVIAL_TYPE = true }; };
569 #endif // __DALI_VECTOR_4_H__