1 #ifndef __DALI_VECTOR_4_H__
2 #define __DALI_VECTOR_4_H__
5 * Copyright (c) 2018 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.
22 #include <cstdint> // uint32_t
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/type-traits.h>
32 * @addtogroup dali_core_math
40 * @brief A four dimensional vector.
42 * Components can be used as position or offset (x,y,z,w); color (r,g,b,a) or texture coords(s,t,p,q).
45 struct DALI_CORE_API Vector4
48 // xrs, ygt, zbp and waq must be consecutive in memory.
49 // No other data must be added before xrs member.
50 // No virtual methods must be added to this struct.
55 * @brief Default constructor, initializes the vector to 0.
67 * @brief Conversion constructor from four floats.
70 * @param[in] x x (or r/s) component
71 * @param[in] y y (or g/t) component
72 * @param[in] z z (or b/p) component
73 * @param[in] w w (or a/q) component
75 explicit Vector4(float x, float y, float z, float w)
85 * @brief Conversion constructor from an array of four floats.
88 * @param[in] array Array of either xyzw/rgba/stpq
90 explicit Vector4(const float* array)
99 * @brief Conversion constructor from Vector2.
102 * @param[in] vec2 Vector2 to copy from, z and w are initialized to 0
104 explicit Vector4( const Vector2& vec2 );
107 * @brief Conversion constructor from Vector3.
110 * @param[in] vec3 Vector3 to copy from, w is initialized to 0
112 explicit Vector4( const Vector3& vec3 );
115 static const Vector4 ONE; ///< (1.0f,1.0f,1.0f,1.0f)
116 static const Vector4 XAXIS; ///< (1.0f,0.0f,0.0f,0.0f)
117 static const Vector4 YAXIS; ///< (0.0f,1.0f,0.0f,0.0f)
118 static const Vector4 ZAXIS; ///< (0.0f,0.0f,1.0f,0.0f)
119 static const Vector4 ZERO; ///< (0.0f, 0.0f, 0.0f, 0.0f)
124 * @brief Assignment operator.
127 * @param[in] array Array of floats
130 Vector4& operator=(const float* array)
141 * @brief Assignment operator.
143 * Only sets x and y. z and w are left as they were.
145 * @param[in] vec2 A reference to assign from
148 Vector4& operator=(const Vector2& vec2 );
151 * @brief Assignment operator.
153 * Only sets x and y and z. w is left as it was.
155 * @param[in] vec3 A reference to assign from
158 Vector4& operator=(const Vector3& vec3 );
161 * @brief Addition operator.
164 * @param[in] rhs Vector to add
165 * @return A vector containing the result of the addition
167 Vector4 operator+(const Vector4 & rhs) const
175 * @brief Addition assignment operator.
178 * @param[in] rhs Vector to add
181 Vector4& operator+=(const Vector4& rhs)
192 * @brief Subtraction operator.
195 * @param[in] rhs The vector to subtract
196 * @return A vector containing the result of the subtraction
198 Vector4 operator-(const Vector4& rhs) const
208 * @brief Subtraction assignment operator.
211 * @param[in] rhs The vector to subtract
214 Vector4& operator-=(const Vector4& rhs)
225 * @brief Multiplication operator.
228 * @param[in] rhs The vector to multiply
229 * @return A vector containing the result of the multiplication
231 Vector4 operator*(const Vector4& rhs) const
239 * @brief Multiplication operator.
242 * @param[in] rhs The float value to scale the vector
243 * @return A vector containing the result of the scaling
245 Vector4 operator*(float rhs) const
247 return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
251 * @brief Multiplication assignment operator.
254 * @param[in] rhs The vector to multiply
257 Vector4& operator*=(const Vector4& rhs)
268 * @brief Multiplication assignment operator.
271 * @param[in] rhs The float value to scale the vector
274 Vector4& operator*=(float rhs)
285 * @brief Division operator.
288 * @param[in] rhs The vector to divide
289 * @return A vector containing the result of the division
291 Vector4 operator/(const Vector4 & rhs) const
299 * @brief Division operator.
302 * @param[in] rhs The float value to scale the vector by
303 * @return A vector containing the result of the scaling
305 Vector4 operator/(float rhs) const
307 float oneOver = 1.0f / rhs;
308 return Vector4(x * oneOver, y * oneOver, z * oneOver, w * oneOver);
312 * @brief Division assignment operator.
315 * @param[in] rhs The vector to divide
318 Vector4& operator/=(const Vector4& rhs)
329 * @brief Division assignment operator.
332 * @param[in] rhs The float value to scale the vector by
335 Vector4& operator/=(float rhs)
337 const float oneOver = 1.0f / rhs;
347 * @brief Unary negation operator.
350 * @return The negative value
352 Vector4 operator-() const
354 Vector4 temp(-x, -y, -z, -w);
360 * @brief Equality operator.
362 * Utilizes appropriate machine epsilon values.
365 * @param[in] rhs The vector to test against
366 * @return True if the vectors are equal
368 bool operator==(const Vector4 &rhs) const;
371 * @brief Inequality operator.
373 * Utilizes appropriate machine epsilon values.
376 * @param[in] rhs The vector to test against
377 * @return True if the vectors are not equal
379 bool operator!=(const Vector4 &rhs) const
381 return !(*this == rhs);
385 * @brief Const array subscript operator overload.
387 * Asserts if index is out of range. Should be 0, 1, 2 or 3.
389 * @param[in] index Subscript index
390 * @return The float at the given index
392 const float& operator[](const uint32_t index) const
394 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
396 return AsFloat()[index];
400 * @brief Mutable array subscript operator overload.
402 * Asserts if index is out of range. Should be 0, 1, 2 or 3.
404 * @param[in] index Subscript index
405 * @return The float at the given index
407 float& operator[](const uint32_t index)
409 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
411 return AsFloat()[index];
415 * @brief Returns the dot product of this vector (4d) and another vector (3d).
417 * The dot product is the length of one vector in the direction of another vector.
418 * This is great for lighting, threshold testing the angle between two unit vectors,
419 * calculating the distance between two points in a particular direction.
421 * @param[in] other The other vector
422 * @return The dot product
424 float Dot(const Vector3& other) const;
427 * @brief Returns the dot product of this vector and another vector.
429 * The dot product is the length of one vector in the direction of another vector.
430 * This is great for lighting, threshold testing the angle between two unit vectors,
431 * calculating the distance between two points in a particular direction.
433 * @param[in] other The other vector
434 * @return The dot product
436 float Dot(const Vector4& other) const;
439 * @brief Returns the 4d dot product of this vector and another vector.
442 * @param[in] other The other vector
443 * @return The dot product
445 float Dot4(const Vector4& other) const;
448 * @brief Returns the cross produce of this vector and another vector.
450 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
451 * two vectors. This is great for calculating normals and making matrices orthogonal.
454 * @param[in] other The other vector
455 * @return A vector containing the cross product
457 Vector4 Cross(const Vector4& other) const;
460 * @brief Returns the length of the vector.
465 float Length() const;
468 * @brief Returns the length of the vector squared.
470 * This is faster than using Length() when performing
471 * threshold checks as it avoids use of the square root.
473 * @return The length of the vector squared
475 float LengthSquared() const;
478 * @brief Normalizes the vector.
480 * Sets the vector to unit length whilst maintaining its direction.
486 * @brief Clamps the vector between minimum and maximum vectors.
489 * @param[in] min The minimum vector
490 * @param[in] max The maximum vector
492 void Clamp( const Vector4& min, const Vector4& max );
495 * @brief Returns the contents of the vector as an array of 4 floats.
499 * The order of the values in this array are as follows:
508 * @return The vector contents as an array of 4 floats
509 * @note inlined for performance reasons (generates less code than a function call)
511 const float* AsFloat() const {return &x;}
514 * @brief Returns the contents of the vector as an array of 4 floats.
518 * The order of the values in this array are as follows:
527 * @return The vector contents as an array of 4 floats
528 * @note inlined for performance reasons (generates less code than a function call)
530 float* AsFloat() {return &x;}
535 // xrs, ygt, zbp and waq must be consecutive in memory.
536 // No other data must be added before xrs member.
537 // No virtual methods must be added to this struct.
541 float x; ///< x component
542 float r; ///< red component
543 float s; ///< s component
547 float y; ///< y component
548 float g; ///< green component
549 float t; ///< t component
553 float z; ///< z component
554 float b; ///< blue component
555 float p; ///< p component
559 float w; ///< w component
560 float a; ///< alpha component
561 float q; ///< q component
566 * @brief Print a Vector4.
569 * @param[in] o The output stream operator
570 * @param[in] vector The vector to print
571 * @return The output stream operator
573 DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Vector4& vector);
576 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
578 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 0,0,1,2.
580 * @param[in] a A vector
581 * @param[in] b A vector
582 * @return A vector containing the minimum of each component from a and b
584 inline Vector4 Min( const Vector4& a, const Vector4& b )
586 return Vector4( a.x < b.x ? a.x : b.x,
587 a.y < b.y ? a.y : b.y,
588 a.z < b.z ? a.z : b.z,
589 a.w < b.w ? a.w : b.w );
593 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
595 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 4,1,2,3.
597 * @param[in] a A vector
598 * @param[in] b A vector
599 * @return A vector containing the maximum of each component from a and b
601 inline Vector4 Max( const Vector4& a, const Vector4& b )
603 return Vector4( a.x > b.x ? a.x : b.x,
604 a.y > b.y ? a.y : b.y,
605 a.z > b.z ? a.z : b.z,
606 a.w > b.w ? a.w : b.w );
610 * @brief Clamps each of vector v's components between minimum and maximum values.
613 * @param[in] v A vector
614 * @param[in] min The minimum value
615 * @param[in] max The maximum value
616 * @return A vector containing the clamped components of v
618 DALI_CORE_API Vector4 Clamp( const Vector4& v, const float& min, const float& max );
620 // Allow Vector4 to be treated as a POD type
621 template <> struct TypeTraits< Vector4 > : public BasicTypes< Vector4 > { enum { IS_TRIVIAL_TYPE = true }; };
628 #endif // __DALI_VECTOR_4_H__