1 #ifndef DALI_VECTOR_4_H
2 #define DALI_VECTOR_4_H
5 * Copyright (c) 2022 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 constexpr Vector4(float x, float y, float z, float w)
84 * @brief Conversion constructor from an array of four floats.
87 * @param[in] array Array of either xyzw/rgba/stpq
89 explicit Vector4(const float* array)
98 * @brief Conversion constructor from Vector2.
101 * @param[in] vec2 Vector2 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 Vector3 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 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 A reference 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 A reference 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 * Utilizes 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 * Utilizes 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 index
389 * @return The float at the given index
391 const float& operator[](const uint32_t 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 uint32_t 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.
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.
498 * The order of the values in this array are as follows:
507 * @return The vector contents as an array of 4 floats
508 * @note inlined for performance reasons (generates less code than a function call)
510 const float* AsFloat() const
516 * @brief Returns the contents of the vector as an array of 4 floats.
520 * The order of the values in this array are as follows:
529 * @return The vector contents as an array of 4 floats
530 * @note inlined for performance reasons (generates less code than a function call)
540 // xrs, ygt, zbp and waq must be consecutive in memory.
541 // No other data must be added before xrs member.
542 // No virtual methods must be added to this struct.
546 float x; ///< x component
547 float r; ///< red component
548 float s; ///< s component
552 float y; ///< y component
553 float g; ///< green component
554 float t; ///< t component
558 float z; ///< z component
559 float b; ///< blue component
560 float p; ///< p component
564 float w; ///< w component
565 float a; ///< alpha component
566 float q; ///< q component
570 Vector4(const Vector4&) = default; ///< Default copy constructor
571 Vector4(Vector4&&) noexcept = default; ///< Default move constructor
572 Vector4& operator=(const Vector4&) = default; ///< Default copy assignment operator
573 Vector4& operator=(Vector4&&) noexcept = default; ///< Default move assignment operator
577 * @brief Print a Vector4.
580 * @param[in] o The output stream operator
581 * @param[in] vector The vector to print
582 * @return The output stream operator
584 DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Vector4& vector);
587 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
589 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 0,0,1,2.
591 * @param[in] a A vector
592 * @param[in] b A vector
593 * @return A vector containing the minimum of each component from a and b
595 inline Vector4 Min(const Vector4& a, const Vector4& b)
597 return Vector4(a.x < b.x ? a.x : b.x,
598 a.y < b.y ? a.y : b.y,
599 a.z < b.z ? a.z : b.z,
600 a.w < b.w ? a.w : b.w);
604 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
606 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 4,1,2,3.
608 * @param[in] a A vector
609 * @param[in] b A vector
610 * @return A vector containing the maximum of each component from a and b
612 inline Vector4 Max(const Vector4& a, const Vector4& b)
614 return Vector4(a.x > b.x ? a.x : b.x,
615 a.y > b.y ? a.y : b.y,
616 a.z > b.z ? a.z : b.z,
617 a.w > b.w ? a.w : b.w);
621 * @brief Clamps each of vector v's components between minimum and maximum values.
624 * @param[in] v A vector
625 * @param[in] min The minimum value
626 * @param[in] max The maximum value
627 * @return A vector containing the clamped components of v
629 DALI_CORE_API Vector4 Clamp(const Vector4& v, const float& min, const float& max);
631 // Allow Vector4 to be treated as a POD type
633 struct TypeTraits<Vector4> : public BasicTypes<Vector4>
637 IS_TRIVIAL_TYPE = true
646 #endif // DALI_VECTOR_4_H