1 #ifndef DALI_VECTOR_2_H
2 #define DALI_VECTOR_2_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>
34 * @addtogroup dali_core_math
42 * @brief A two dimensional vector.
45 struct DALI_CORE_API Vector2
47 // (x width) and (y height) must be consecutive in memory.
48 // No other data must be added before (x width) member.
49 // No virtual methods must be added to this struct.
66 * @param[in] x x or width component
67 * @param[in] y y or height component
69 explicit constexpr Vector2(float x, float y)
76 * @brief Conversion constructor from an array of two floats.
79 * @param[in] array Array of xy
81 explicit Vector2(const float* array)
91 * @param[in] vec3 Vector3 to create this vector from
93 explicit Vector2(const Vector3& vec3);
99 * @param[in] vec4 Vector4 to create this vector from
101 explicit Vector2(const Vector4& vec4);
104 * @brief Conversion constructor from a Uint16Pair.
108 * @param[in] pair The Uint16Pair to create this vector from
110 Vector2(const Uint16Pair& pair);
114 static const Vector2 ONE; ///< (1.0f,1.0f)
115 static const Vector2 XAXIS; ///< Vector representing the X axis
116 static const Vector2 YAXIS; ///< Vector representing the Y axis
117 static const Vector2 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
118 static const Vector2 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
119 static const Vector2 ZERO; ///< (0.0f, 0.0f)
124 * @brief Assignment operator.
127 * @param[in] array Array of floats
130 Vector2& operator=(const float* array)
139 * @brief Assignment operator.
142 * @param[in] rhs Vector to assign
145 Vector2& operator=(const Vector3& rhs);
148 * @brief Assignment operator.
151 * @param[in] rhs Vector to assign
154 Vector2& operator=(const Vector4& rhs);
157 * @brief Addition operator.
160 * @param[in] rhs Vector to add
161 * @return A vector containing the result of the addition
163 Vector2 operator+(const Vector2& rhs) const
171 * @brief Addition assignment operator.
174 * @param[in] rhs Vector to add
177 Vector2& operator+=(const Vector2& rhs)
186 * @brief Subtraction operator.
189 * @param[in] rhs The vector to subtract
190 * @return A vector containing the result of the subtraction
192 Vector2 operator-(const Vector2& rhs) const
200 * @brief Subtraction assignment operator.
203 * @param[in] rhs The vector to subtract
206 Vector2& operator-=(const Vector2& rhs)
215 * @brief Multiplication operator.
218 * @param[in] rhs The vector to multiply
219 * @return A vector containing the result of the multiplication
221 Vector2 operator*(const Vector2& rhs) const
223 return Vector2(x * rhs.x, y * rhs.y);
227 * @brief Multiplication operator.
230 * @param[in] rhs The float value to scale the vector
231 * @return A vector containing the result of the scaling
233 Vector2 operator*(float rhs) const
235 return Vector2(x * rhs, y * rhs);
239 * @brief Multiplication assignment operator.
242 * @param[in] rhs The vector to multiply
245 Vector2& operator*=(const Vector2& rhs)
254 * @brief Multiplication assignment operator.
257 * @param[in] rhs The float value to scale the vector
260 Vector2& operator*=(float rhs)
269 * @brief Division operator.
272 * @param[in] rhs The vector to divide
273 * @return A vector containing the result of the division
275 Vector2 operator/(const Vector2& rhs) const
277 return Vector2(x / rhs.x, y / rhs.y);
281 * @brief Division operator.
284 * @param[in] rhs The float value to scale the vector by
285 * @return A vector containing the result of the scaling
287 Vector2 operator/(float rhs) const
289 return Vector2(x / rhs, y / rhs);
293 * @brief Division assignment operator.
296 * @param[in] rhs The vector to divide
299 Vector2& operator/=(const Vector2& rhs)
308 * @brief Division assignment operator.
311 * @param[in] rhs The float value to scale the vector by
314 Vector2& operator/=(float rhs)
323 * @brief Unary negation operator.
326 * @return A vector containing the negation
328 Vector2 operator-() const
330 Vector2 temp(-x, -y);
336 * @brief Equality operator.
338 * Utilizes appropriate machine epsilon values.
341 * @param[in] rhs The vector to test against
342 * @return true if the vectors are equal
344 bool operator==(const Vector2& rhs) const;
347 * @brief Inequality operator.
349 * Utilizes appropriate machine epsilon values.
352 * @param[in] rhs The vector to test against
353 * @return true if the vectors are not equal
355 bool operator!=(const Vector2& rhs) const
357 return !(*this == rhs);
361 * @brief Const array subscript operator overload.
363 * Asserts if index is out of range. Should be 0 or 1.
365 * @param[in] index Subscript index
366 * @return The float at the given index
368 const float& operator[](const uint32_t index) const
370 DALI_ASSERT_ALWAYS(index < 2 && "Vector element index out of bounds");
372 return AsFloat()[index];
376 * @brief Mutable array subscript operator overload.
378 * Asserts if index is out of range. Should be 0 or 1.
380 * @param[in] index Subscript index
381 * @return The float at the given index
383 float& operator[](const uint32_t index)
385 DALI_ASSERT_ALWAYS(index < 2 && "Vector element index out of bounds");
387 return AsFloat()[index];
391 * @brief Returns the length of the vector.
394 * @return The length of the vector
396 float Length() const;
399 * @brief Returns the length of the vector squared.
401 * This is more efficient than Length() for threshold
402 * testing as it avoids the use of a square root.
404 * @return The length of the vector squared
406 float LengthSquared() const;
409 * @brief Sets the vector to be unit length, whilst maintaining its direction.
416 * @brief Clamps the vector between minimum and maximum vectors.
419 * @param[in] min The minimum vector
420 * @param[in] max The maximum vector
422 void Clamp(const Vector2& min, const Vector2& max);
425 * @brief Returns the contents of the vector as an array of 2 floats.
427 * The order of the values in this array are as follows:
431 * @return The vector contents as an array of 2 floats
432 * @note inlined for performance reasons (generates less code than a function call)
434 const float* AsFloat() const
440 * @brief Returns the contents of the vector as an array of 2 floats.
442 * The order of the values in this array are as follows:
446 * @return The vector contents as an array of 2 floats
447 * @note inlined for performance reasons (generates less code than a function call)
455 Vector2(const Vector2&) = default; ///< Default copy constructor
456 Vector2(Vector2&&) noexcept = default; ///< Default move constructor
457 Vector2& operator=(const Vector2&) = default; ///< Default copy assignment operator
458 Vector2& operator=(Vector2&&) noexcept = default; ///< Default move assignment operator
462 // (x width) and (y height) must be consecutive in memory.
463 // No other data must be added before (x width) member.
464 // No virtual methods must be added to this struct.
467 float x; ///< x component
468 float width; ///< width
473 float y; ///< y component
474 float height; ///< height
479 * @brief Size is an alias of Dali::Vector2.
482 using Size = Vector2;
485 * @brief Print a Vector2.
488 * @param[in] o The output stream operator
489 * @param[in] vector The vector to print
490 * @return The output stream operator
492 DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Vector2& vector);
495 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
497 * If a=0,1 and b=1,0 returns a vector of 0,0.
499 * @param[in] a A vector
500 * @param[in] b A vector
501 * @return A vector containing the minimum of each component from a and b
503 inline Vector2 Min(const Vector2& a, const Vector2& b)
505 return Vector2(a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y);
509 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
511 * If a=0,1 and b=1,0 returns a vector of 1,1.
513 * @param[in] a A vector
514 * @param[in] b A vector
515 * @return A vector containing the maximum of each component from a and b
517 inline Vector2 Max(const Vector2& a, const Vector2& b)
519 return Vector2(a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y);
523 * @brief Clamps each of vector v's components between minimum and maximum values.
526 * @param[in] v A vector
527 * @param[in] min The minimum value
528 * @param[in] max The maximum value
529 * @return A vector containing the clamped components of v
531 DALI_CORE_API Vector2 Clamp(const Vector2& v, const float& min, const float& max);
533 // Allow Vector2 to be treated as a POD type
535 struct TypeTraits<Vector2> : public BasicTypes<Vector2>
539 IS_TRIVIAL_TYPE = true
548 #endif // DALI_VECTOR_2_H