noexcept move for BaseHandle/InstrusivePtr/Math
[platform/core/uifw/dali-core.git] / dali / public-api / math / vector3.h
index 75c716f..2ea7012 100644 (file)
@@ -1,8 +1,8 @@
-#ifndef __DALI_VECTOR_3_H__
-#define __DALI_VECTOR_3_H__
+#ifndef DALI_VECTOR_3_H
+#define DALI_VECTOR_3_H
 
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 // EXTERNAL INCLUDES
-#include <algorithm>
-#include <iostream>
+#include <cstdint> // uint32_t
+#include <iosfwd>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/common/type-traits.h>
 
-namespace Dali DALI_IMPORT_API
+namespace Dali
 {
+/**
+ * @addtogroup dali_core_math
+ * @{
+ */
 
 struct Vector2;
 struct Vector4;
@@ -34,13 +39,15 @@ class Quaternion;
 
 /**
  * @brief A three dimensional vector.
+ * @SINCE_1_0.0
  */
-struct DALI_IMPORT_API Vector3
+struct DALI_CORE_API Vector3
 {
-// Construction
+  // Construction
 
   /**
    * @brief Constructor.
+   * @SINCE_1_0.0
    */
   // NOTE
   // (x width r), (y height g), (z depth b) must be consecutive in memory.
@@ -56,11 +63,12 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Constructor.
    *
-   * @param [in] x (or width) component
-   * @param [in] y (or height) component
-   * @param [in] z (or depth) component
+   * @SINCE_1_0.0
+   * @param[in] x (or width) component
+   * @param[in] y (or height) component
+   * @param[in] z (or depth) component
    */
-  explicit Vector3(float x, float y, float z)
+  explicit constexpr Vector3(float x, float y, float z)
   : x(x),
     y(y),
     z(z)
@@ -70,7 +78,8 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Conversion constructor from an array of three floats.
    *
-   * @param [in] array of xyz
+   * @SINCE_1_0.0
+   * @param[in] array Array of xyz
    */
   explicit Vector3(const float* array)
   : x(array[0]),
@@ -82,35 +91,38 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Constructor.
    *
-   * @param [in] vec2 Vector2 to create this vector from
+   * @SINCE_1_0.0
+   * @param[in] vec2 Vector2 to create this vector from
    */
-  explicit Vector3( const Vector2& vec2 );
+  explicit Vector3(const Vector2& vec2);
 
   /**
    * @brief Constructor.
    *
-   * @param [in] vec4 Vector4 to create this vector from
+   * @SINCE_1_0.0
+   * @param[in] vec4 Vector4 to create this vector from
    */
-  explicit Vector3( const Vector4& vec4 );
+  explicit Vector3(const Vector4& vec4);
 
-// Constants
+  // Constants
 
-  static const Vector3 ONE;               ///< (1.0f,1.0f,1.0f)
-  static const Vector3 XAXIS;             ///< Vector representing the X axis
-  static const Vector3 YAXIS;             ///< Vector representing the Y axis
-  static const Vector3 ZAXIS;             ///< Vector representing the Z axis
-  static const Vector3 NEGATIVE_XAXIS;    ///< Vector representing the negative X axis
-  static const Vector3 NEGATIVE_YAXIS;    ///< Vector representing the negative Y axis
-  static const Vector3 NEGATIVE_ZAXIS;    ///< Vector representing the negative Z axis
-  static const Vector3 ZERO;              ///< (0.0f, 0.0f, 0.0f)
+  static const Vector3 ONE;            ///< (1.0f,1.0f,1.0f)
+  static const Vector3 XAXIS;          ///< Vector representing the X axis
+  static const Vector3 YAXIS;          ///< Vector representing the Y axis
+  static const Vector3 ZAXIS;          ///< Vector representing the Z axis
+  static const Vector3 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
+  static const Vector3 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
+  static const Vector3 NEGATIVE_ZAXIS; ///< Vector representing the negative Z axis
+  static const Vector3 ZERO;           ///< (0.0f, 0.0f, 0.0f)
 
-// API
+  // API
 
   /**
    * @brief Assignment operator.
    *
-   * @param[in] array of floats
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] array Array of floats
+   * @return Itself
    */
   Vector3& operator=(const float* array)
   {
@@ -124,23 +136,26 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Assignment operator.
    *
-   * @param[in] rhs vector to assign.
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs Vector to assign
+   * @return Itself
    */
   Vector3& operator=(const Vector2& rhs);
 
   /**
    * @brief Assignment operator.
    *
-   * @param[in] rhs vector to assign.
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs Vector to assign
+   * @return Itself
    */
   Vector3& operator=(const Vector4& rhs);
 
   /**
    * @brief Addition operator.
    *
-   * @param[in] rhs vector to add.
+   * @SINCE_1_0.0
+   * @param[in] rhs Vector to add
    * @return A vector containing the result of the addition
    */
   Vector3 operator+(const Vector3& rhs) const
@@ -153,8 +168,9 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Addition assignment operator.
    *
-   * @param[in] rhs vector to add.
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs Vector to add
+   * @return Itself
    */
   Vector3& operator+=(const Vector3& rhs)
   {
@@ -168,7 +184,8 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Subtraction operator.
    *
-   * @param[in] rhs  the vector to subtract
+   * @SINCE_1_0.0
+   * @param[in] rhs The vector to subtract
    * @return A vector containing the result of the subtraction
    */
   Vector3 operator-(const Vector3& rhs) const
@@ -181,8 +198,9 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Subtraction assignment operator.
    *
-   * @param[in] rhs the vector to subtract
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs The vector to subtract
+   * @return Itself
    */
   Vector3& operator-=(const Vector3& rhs)
   {
@@ -196,7 +214,8 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Multiplication operator.
    *
-   * @param[in] rhs the vector to multiply
+   * @SINCE_1_0.0
+   * @param[in] rhs The vector to multiply
    * @return A vector containing the result of the multiplication
    */
   Vector3 operator*(const Vector3& rhs) const
@@ -209,7 +228,8 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Multiplication operator.
    *
-   * @param[in] rhs the float value to scale the vector
+   * @SINCE_1_0.0
+   * @param[in] rhs The float value to scale the vector
    * @return A vector containing the result of the scaling
    */
   Vector3 operator*(float rhs) const
@@ -220,8 +240,9 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Multiplication assignment operator.
    *
-   * @param[in] rhs the vector to multiply
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs The vector to multiply
+   * @return Itself
    */
   Vector3& operator*=(const Vector3& rhs)
   {
@@ -235,8 +256,9 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Multiplication assignment operator.
    *
-   * @param[in] rhs the float value to scale the vector
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs The float value to scale the vector
+   * @return Itself
    */
   Vector3& operator*=(float rhs)
   {
@@ -250,15 +272,17 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Multiplication assignment operator.
    *
-   * @param[in] rhs the Quaternion value to multiply the vector by
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs The Quaternion value to multiply the vector by
+   * @return Itself
    */
   Vector3& operator*=(const Quaternion& rhs);
 
   /**
    * @brief Division operator.
    *
-   * @param[in] rhs the vector to divide
+   * @SINCE_1_0.0
+   * @param[in] rhs The vector to divide
    * @return A vector containing the result of the division
    */
   Vector3 operator/(const Vector3& rhs) const
@@ -271,6 +295,7 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Division operator.
    *
+   * @SINCE_1_0.0
    * @param[in] rhs The float value to scale the vector by
    * @return A vector containing the result of the scaling
    */
@@ -282,8 +307,9 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Division assignment operator.
    *
-   * @param[in] rhs the vector to divide
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs The vector to divide
+   * @return Itself
    */
   Vector3& operator/=(const Vector3& rhs)
   {
@@ -297,8 +323,9 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Division assignment operator.
    *
-   * @param[in] rhs the float value to scale the vector by
-   * @return itself
+   * @SINCE_1_0.0
+   * @param[in] rhs The float value to scale the vector by
+   * @return Itself
    */
   Vector3& operator/=(float rhs)
   {
@@ -313,7 +340,8 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Unary negation operator.
    *
-   * @return A vector containg the negation
+   * @SINCE_1_0.0
+   * @return A vector containing the negation
    */
   Vector3 operator-() const
   {
@@ -325,20 +353,22 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Equality operator.
    *
-   * utilises appropriate machine epsilon values;
+   * Utilizes appropriate machine epsilon values.
    *
+   * @SINCE_1_0.0
    * @param[in] rhs The vector to test against
-   * @return true if the vectors are equal
+   * @return True if the vectors are equal
    */
   bool operator==(const Vector3& rhs) const;
 
   /**
    * @brief Inequality operator.
    *
-   * utilises appropriate machine epsilon values;
+   * Utilizes appropriate machine epsilon values.
    *
+   * @SINCE_1_0.0
    * @param[in] rhs The vector to test against
-   * @return true if the vectors are not equal
+   * @return True if the vectors are not equal
    */
   bool operator!=(const Vector3& rhs) const
   {
@@ -348,13 +378,14 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Const array subscript operator overload.
    *
-   * Asserts if index is out of range. Should be 0, 1 or 2
-   * @param[in] index Subscript
-   * @return    The float at the given index.
+   * Asserts if index is out of range. Should be 0, 1 or 2.
+   * @SINCE_1_0.0
+   * @param[in] index Subscript index
+   * @return The float at the given index
    */
-  const float& operator[](const unsigned int index) const
+  const float& operator[](const uint32_t index) const
   {
-    DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
+    DALI_ASSERT_ALWAYS(index < 3 && "Vector element index out of bounds");
 
     return AsFloat()[index];
   }
@@ -362,13 +393,14 @@ struct DALI_IMPORT_API Vector3
   /**
    * @brief Mutable array subscript operator overload.
    *
-   * Asserts if index is out of range. Should be 0, 1 or 2
+   * Asserts if index is out of range. Should be 0, 1 or 2.
+   * @SINCE_1_0.0
    * @param[in] index Subscript index
-   * @return    The float at the given index.
+   * @return The float at the given index
    */
-  float& operator[](const unsigned int index)
+  float& operator[](const uint32_t index)
   {
-    DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
+    DALI_ASSERT_ALWAYS(index < 3 && "Vector element index out of bounds");
 
     return AsFloat()[index];
   }
@@ -379,8 +411,9 @@ struct DALI_IMPORT_API Vector3
    * The dot product is the length of one vector in the direction of another vector.
    * This is great for lighting, threshold testing the angle between two unit vectors,
    * calculating the distance between two points in a particular direction.
-   * @param [in]  other     the other vector
-   * @return          the dot product
+   * @SINCE_1_0.0
+   * @param[in] other The other vector
+   * @return The dot product
    */
   float Dot(const Vector3& other) const;
 
@@ -390,15 +423,17 @@ struct DALI_IMPORT_API Vector3
    * The cross produce of two vectors is a vector which is perpendicular to the plane of the
    * two vectors. This is great for calculating normals and making matrices orthogonal.
    *
-   * @param [in] other    the other vector
-   * @return         the cross product
+   * @SINCE_1_0.0
+   * @param[in] other The other vector
+   * @return The cross product
    */
   Vector3 Cross(const Vector3& other) const;
 
   /**
    * @brief Returns the length of the vector.
    *
-   * @return the length of the vector
+   * @SINCE_1_0.0
+   * @return The length of the vector
    */
   float Length() const;
 
@@ -407,23 +442,26 @@ struct DALI_IMPORT_API Vector3
    *
    * This is more efficient than Length() for threshold
    * testing as it avoids the use of a square root.
-   * @return the length of the vector squared.
+   * @SINCE_1_0.0
+   * @return The length of the vector squared
    */
   float LengthSquared() const;
 
   /**
    * @brief Sets the vector to be unit length, whilst maintaining its direction.
    *
+   * @SINCE_1_0.0
    */
   void Normalize();
 
   /**
    * @brief Clamps the vector between minimum and maximum vectors.
    *
-   * @param [in] min the minimum vector
-   * @param [in] max the maximum vector
+   * @SINCE_1_0.0
+   * @param[in] min The minimum vector
+   * @param[in] max The maximum vector
    */
-  void Clamp( const Vector3& min, const Vector3& max );
+  void Clamp(const Vector3& min, const Vector3& max);
 
   /**
    * @brief Returns the contents of the vector as an array of 3 floats.
@@ -432,10 +470,14 @@ struct DALI_IMPORT_API Vector3
    * 0: x (or width, or r)
    * 1: y (or height, or g)
    * 2: z (or depth, or b)
+   * @SINCE_1_0.0
+   * @return The vector contents as an array of 3 floats
    * @note inlined for performance reasons (generates less code than a function call)
-   * @return the vector contents as an array of 3 floats.
    */
-  const float* AsFloat() const {return &x;}
+  const float* AsFloat() const
+  {
+    return &x;
+  }
 
   /**
    * @brief Returns the contents of the vector as an array of 3 floats.
@@ -444,44 +486,64 @@ struct DALI_IMPORT_API Vector3
    * 0: x (or width, or r)
    * 1: y (or height, or g)
    * 2: z (or depth, or b)
+   * @SINCE_1_0.0
+   * @return The vector contents as an array of 3 floats
    * @note inlined for performance reasons (generates less code than a function call)
-   * @return the vector contents as an array of 3 floats.
    */
-  float* AsFloat() {return &x;}
+  float* AsFloat()
+  {
+    return &x;
+  }
 
   /**
    * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
    *
+   * @SINCE_1_0.0
+   * @return The partial vector contents as Vector2 (x,y)
    * @note inlined for performance reasons (generates less code than a function call)
-   * @return the partial vector contents as Vector2 (x,y)
    */
-  const Vector2& GetVectorXY() const {return reinterpret_cast<const Vector2&>(x);}
+  const Vector2& GetVectorXY() const
+  {
+    return reinterpret_cast<const Vector2&>(x);
+  }
 
   /**
    * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
    *
+   * @SINCE_1_0.0
+   * @return The partial vector contents as Vector2 (x,y)
    * @note inlined for performance reasons (generates less code than a function call)
-   * @return the partial vector contents as Vector2 (x,y)
    */
-  Vector2& GetVectorXY() {return reinterpret_cast<Vector2&>(x);}
+  Vector2& GetVectorXY()
+  {
+    return reinterpret_cast<Vector2&>(x);
+  }
 
   /**
    * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
    *
+   * @SINCE_1_0.0
+   * @return The partial vector contents as Vector2 (y,z)
    * @note inlined for performance reasons (generates less code than a function call)
-   * @return the partial vector contents as Vector2 (y,z)
    */
-  const Vector2& GetVectorYZ() const {return reinterpret_cast<const Vector2&>(y);}
+  const Vector2& GetVectorYZ() const
+  {
+    return reinterpret_cast<const Vector2&>(y);
+  }
 
   /**
    * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
    *
+   * @SINCE_1_0.0
+   * @return The partial vector contents as Vector2 (y,z)
    * @note inlined for performance reasons (generates less code than a function call)
-   * @return the partial vector contents as Vector2 (y,z)
    */
-  Vector2& GetVectorYZ() {return reinterpret_cast<Vector2&>(y);}
+  Vector2& GetVectorYZ()
+  {
+    return reinterpret_cast<Vector2&>(y);
+  }
 
-// Data
+  // Data
 
   // NOTE
   // (x width r), (y height g), (z depth b) must be consecutive in memory.
@@ -489,9 +551,9 @@ struct DALI_IMPORT_API Vector3
   // No virtual methods must be added to this struct.
   union
   {
-    float x;      ///< x component
-    float width;  ///< width component
-    float r;      ///< red component
+    float x;     ///< x component
+    float width; ///< width component
+    float r;     ///< red component
   };
   union
   {
@@ -501,105 +563,84 @@ struct DALI_IMPORT_API Vector3
   };
   union
   {
-    float z;      ///< z component
-    float depth;  ///< depth component
-    float b;      ///< blue component
+    float z;     ///< z component
+    float depth; ///< depth component
+    float b;     ///< blue component
   };
+
+public:
+  Vector3(const Vector3&)     = default;            ///< Default copy constructor
+  Vector3(Vector3&&) noexcept = default;            ///< Default move constructor
+  Vector3& operator=(const Vector3&) = default;     ///< Default copy assignment operator
+  Vector3& operator=(Vector3&&) noexcept = default; ///< Default move assignment operator
 };
 
 /**
- * @brief Print a Vector3.
+ * @brief Prints a Vector3.
  *
- * @param [in] o The output stream operator.
- * @param [in] vector The vector to print.
- * @return The output stream operator.
+ * @SINCE_1_0.0
+ * @param[in] o The output stream operator
+ * @param[in] vector The vector to print
+ * @return The output stream operator
  */
-DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector3& vector);
+DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Vector3& vector);
 
 /**
  * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
  *
  * If a=0,1,2 and b=2,1,0  returns a vector of 2,1,2.
- * @param [in] a     a vector
- * @param [in] b     a vector
- * @return      a vector containing the minimum of each component from a and b
+ * @SINCE_1_0.0
+ * @param[in] a A vector
+ * @param[in] b A vector
+ * @return A vector containing the minimum of each component from a and b
  */
-inline Vector3 Min( const Vector3& a, const Vector3& b )
+inline Vector3 Min(const Vector3& a, const Vector3& b)
 {
-  return Vector3( std::min(a.x,b.x), std::min(a.y,b.y), std::min(a.z,b.z) );
+  return Vector3(a.x < b.x ? a.x : b.x,
+                 a.y < b.y ? a.y : b.y,
+                 a.z < b.z ? a.z : b.z);
 }
 
 /**
  * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
  *
- * If a=0,1 and b=1,0  returns a vector of 1,1
- * @param [in] a     a vector
- * @param [in] b     a vector
- * @return      a vector containing the maximum of each component from a and b
+ * If a=0,1 and b=1,0  returns a vector of 1,1.
+ * @SINCE_1_0.0
+ * @param[in] a A vector
+ * @param[in] b A vector
+ * @return A vector containing the maximum of each component from a and b
  */
-inline Vector3 Max( const Vector3& a, const Vector3& b )
+inline Vector3 Max(const Vector3& a, const Vector3& b)
 {
-  return Vector3( std::max(a.x,b.x), std::max(a.y,b.y), std::max(a.z,b.z) );
+  return Vector3(a.x > b.x ? a.x : b.x,
+                 a.y > b.y ? a.y : b.y,
+                 a.z > b.z ? a.z : b.z);
 }
 
 /**
  * @brief Clamps each of vector v's components between minimum and maximum values.
  *
- * @param [in] v     a vector
- * @param [in] min the minimum value
- * @param [in] max the maximum value
- * @return     a vector containing the clamped components of v
- */
-DALI_IMPORT_API Vector3 Clamp( const Vector3& v, const float& min, const float& max );
-
-/**
- * @brief Scales an Actor, such that it fits within its Parent's Size Keeping the aspect ratio.
- *
- * f(target, source) = Vector3( min( target.X / source.X, min( target.Y / source.Y, target.Z / source.Z ) )
- * If any of the source dimensions is zero it will be ignored in the calculation
- *
- * @param [in] target size
- * @param [in] source size
- * @return target scaled inside source
- */
-DALI_IMPORT_API Vector3 FitKeepAspectRatio( const Vector3& target, const Vector3& source );
-
-/**
- * @brief Scales an Actor, such that it fill its Parent's Size Keeping the aspect ratio.
- *
- * f(target, source) = Vector3( max( target.X / source.X, max( target.Y / source.Y, target.Z / source.Z ) )
- * If any of the source dimensions is zero it will be ignored in the calculation
- *
- * @param [in] target size
- * @param [in] source size
- * @return target scaled inside source
+ * @SINCE_1_0.0
+ * @param[in] v A vector
+ * @param[in] min The minimum value
+ * @param[in] max The maximum value
+ * @return A vector containing the clamped components of v
  */
-DALI_IMPORT_API Vector3 FillKeepAspectRatio( const Vector3& target, const Vector3& source );
+DALI_CORE_API Vector3 Clamp(const Vector3& v, const float& min, const float& max);
 
-/**
- * @brief Scales an Actor, such that it fill its Parent's Size in the X and Y coordinates Keeping the aspect ratio.
- *
- * f(target, source) = Vector3( max( target.X / sizeX, target.Y / sizeY ) )
- * If any of the source dimensions is zero it will be ignored in the calculation
- *
- * @param [in] target size
- * @param [in] source size
- * @return target scaled inside source
- */
-DALI_IMPORT_API Vector3 FillXYKeepAspectRatio( const Vector3& target, const Vector3& source );
+// Allow Vector3 to be treated as a POD type
+template<>
+struct TypeTraits<Vector3> : public BasicTypes<Vector3>
+{
+  enum
+  {
+    IS_TRIVIAL_TYPE = true
+  };
+};
 
 /**
- * @brief Shrinks source size inside the target size maintaining aspect ratio of source.
- *
- * If source is smaller than target it returns source
- * @pre source width and height > 0
- * @param [in] target size
- * @param [in] source size
- * @return target scaled inside source
+ * @}
  */
-Vector3 ShrinkInsideKeepAspectRatio( const Vector3& target, const Vector3& source );
-
-
 } // namespace Dali
 
-#endif // __DALI_VECTOR_3_H__
+#endif // DALI_VECTOR_3_H