1 #ifndef DALI_VECTOR_2_H
2 #define DALI_VECTOR_2_H
5 * Copyright (c) 2020 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>
35 * @addtogroup dali_core_math
43 * @brief A two dimensional vector.
46 struct DALI_CORE_API Vector2
48 // (x width) and (y height) must be consecutive in memory.
49 // No other data must be added before (x width) member.
50 // No virtual methods must be added to this struct.
68 * @param[in] x x or width component
69 * @param[in] y y or height component
71 explicit constexpr Vector2(float x, float y)
77 * @brief Conversion constructor from an array of two floats.
80 * @param[in] array Array of xy
82 explicit Vector2(const float* array)
92 * @param[in] vec3 Vector3 to create this vector from
94 explicit Vector2(const Vector3& vec3);
100 * @param[in] vec4 Vector4 to create this vector from
102 explicit Vector2(const Vector4& vec4);
105 * @brief Conversion constructor from a Uint16Pair.
109 * @param[in] pair The Uint16Pair to create this vector from
111 Vector2(const Uint16Pair& pair);
115 static const Vector2 ONE; ///< (1.0f,1.0f)
116 static const Vector2 XAXIS; ///< Vector representing the X axis
117 static const Vector2 YAXIS; ///< Vector representing the Y axis
118 static const Vector2 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
119 static const Vector2 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
120 static const Vector2 ZERO; ///< (0.0f, 0.0f)
125 * @brief Assignment operator.
128 * @param[in] array Array of floats
131 Vector2& operator=(const float* array)
140 * @brief Assignment operator.
143 * @param[in] rhs Vector to assign
146 Vector2& operator=(const Vector3& rhs);
149 * @brief Assignment operator.
152 * @param[in] rhs Vector to assign
155 Vector2& operator=(const Vector4& rhs);
158 * @brief Addition operator.
161 * @param[in] rhs Vector to add
162 * @return A vector containing the result of the addition
164 Vector2 operator+(const Vector2& rhs) const
172 * @brief Addition assignment operator.
175 * @param[in] rhs Vector to add
178 Vector2& operator+=(const Vector2& rhs)
187 * @brief Subtraction operator.
190 * @param[in] rhs The vector to subtract
191 * @return A vector containing the result of the subtraction
193 Vector2 operator-(const Vector2& rhs) const
201 * @brief Subtraction assignment operator.
204 * @param[in] rhs The vector to subtract
207 Vector2& operator-=(const Vector2& rhs)
216 * @brief Multiplication operator.
219 * @param[in] rhs The vector to multiply
220 * @return A vector containing the result of the multiplication
222 Vector2 operator*(const Vector2& rhs) const
224 return Vector2(x * rhs.x, y * rhs.y);
228 * @brief Multiplication operator.
231 * @param[in] rhs The float value to scale the vector
232 * @return A vector containing the result of the scaling
234 Vector2 operator*(float rhs) const
236 return Vector2(x * rhs, y * rhs);
240 * @brief Multiplication assignment operator.
243 * @param[in] rhs The vector to multiply
246 Vector2& operator*=(const Vector2& rhs)
255 * @brief Multiplication assignment operator.
258 * @param[in] rhs The float value to scale the vector
261 Vector2& operator*=(float rhs)
270 * @brief Division operator.
273 * @param[in] rhs The vector to divide
274 * @return A vector containing the result of the division
276 Vector2 operator/(const Vector2& rhs) const
278 return Vector2(x / rhs.x, y / rhs.y);
282 * @brief Division operator.
285 * @param[in] rhs The float value to scale the vector by
286 * @return A vector containing the result of the scaling
288 Vector2 operator/(float rhs) const
290 return Vector2(x / rhs, y / rhs);
295 * @brief Division assignment operator.
298 * @param[in] rhs The vector to divide
301 Vector2& operator/=(const Vector2& rhs)
310 * @brief Division assignment operator.
313 * @param[in] rhs The float value to scale the vector by
316 Vector2& operator/=(float rhs)
325 * @brief Unary negation operator.
328 * @return A vector containing the negation
330 Vector2 operator-() const
332 Vector2 temp(-x, -y);
338 * @brief Equality operator.
340 * Utilizes appropriate machine epsilon values.
343 * @param[in] rhs The vector to test against
344 * @return true if the vectors are equal
346 bool operator==(const Vector2& rhs) const;
349 * @brief Inequality operator.
351 * Utilizes appropriate machine epsilon values.
354 * @param[in] rhs The vector to test against
355 * @return true if the vectors are not equal
357 bool operator!=(const Vector2& rhs) const
359 return !(*this == rhs);
363 * @brief Const array subscript operator overload.
365 * Asserts if index is out of range. Should be 0 or 1.
367 * @param[in] index Subscript index
368 * @return The float at the given index
370 const float& operator[](const uint32_t index) const
372 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
374 return AsFloat()[index];
378 * @brief Mutable array subscript operator overload.
380 * Asserts if index is out of range. Should be 0 or 1.
382 * @param[in] index Subscript index
383 * @return The float at the given index
385 float& operator[](const uint32_t index)
387 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
389 return AsFloat()[index];
393 * @brief Returns the length of the vector.
396 * @return The length of the vector
398 float Length() const;
401 * @brief Returns the length of the vector squared.
403 * This is more efficient than Length() for threshold
404 * testing as it avoids the use of a square root.
406 * @return The length of the vector squared
408 float LengthSquared() const;
411 * @brief Sets the vector to be unit length, whilst maintaining its direction.
418 * @brief Clamps the vector between minimum and maximum vectors.
421 * @param[in] min The minimum vector
422 * @param[in] max The maximum vector
424 void Clamp( const Vector2& min, const Vector2& max );
427 * @brief Returns the contents of the vector as an array of 2 floats.
429 * The order of the values in this array are as follows:
433 * @return The vector contents as an array of 2 floats
434 * @note inlined for performance reasons (generates less code than a function call)
436 const float* AsFloat() const {return &x;}
439 * @brief Returns the contents of the vector as an array of 2 floats.
441 * The order of the values in this array are as follows:
445 * @return The vector contents as an array of 2 floats
446 * @note inlined for performance reasons (generates less code than a function call)
448 float* AsFloat() {return &x;}
452 Vector2( const Vector2& ) = default; ///< Default copy constructor
453 Vector2( Vector2&& ) = default; ///< Default move constructor
454 Vector2& operator=( const Vector2& ) = default; ///< Default copy assignment operator
455 Vector2& operator=( Vector2&& ) = default; ///< Default move assignment operator
460 // (x width) and (y height) must be consecutive in memory.
461 // No other data must be added before (x width) member.
462 // No virtual methods must be added to this struct.
465 float x; ///< x component
466 float width; ///< width
471 float y; ///< y component
472 float height; ///< height
478 * @brief Size is an alias of Dali::Vector2.
481 using Size = Vector2;
484 * @brief Print a Vector2.
487 * @param[in] o The output stream operator
488 * @param[in] vector The vector to print
489 * @return The output stream operator
491 DALI_CORE_API std::ostream& operator<< (std::ostream& o, const Vector2& vector);
494 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
496 * If a=0,1 and b=1,0 returns a vector of 0,0.
498 * @param[in] a A vector
499 * @param[in] b A vector
500 * @return A vector containing the minimum of each component from a and b
502 inline Vector2 Min( const Vector2& a, const Vector2& b )
504 return Vector2( a.x < b.x ? a.x : b.x , a.y < b.y ? a.y : b.y );
508 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
510 * If a=0,1 and b=1,0 returns a vector of 1,1.
512 * @param[in] a A vector
513 * @param[in] b A vector
514 * @return A vector containing the maximum of each component from a and b
516 inline Vector2 Max( const Vector2& a, const Vector2& b )
518 return Vector2( a.x > b.x ? a.x : b.x , a.y > b.y ? a.y : b.y );
522 * @brief Clamps each of vector v's components between minimum and maximum values.
525 * @param[in] v A vector
526 * @param[in] min The minimum value
527 * @param[in] max The maximum value
528 * @return A vector containing the clamped components of v
530 DALI_CORE_API Vector2 Clamp( const Vector2& v, const float& min, const float& max );
532 // Allow Vector2 to be treated as a POD type
533 template <> struct TypeTraits< Vector2 > : public BasicTypes< Vector2 > { enum { IS_TRIVIAL_TYPE = true }; };
540 #endif // DALI_VECTOR_2_H