1 #ifndef __DALI_VECTOR_2_H__
2 #define __DALI_VECTOR_2_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 two dimensional vector.
43 struct DALI_CORE_API Vector2
45 // (x width) and (y height) must be consecutive in memory.
46 // No other data must be added before (x width) member.
47 // No virtual methods must be added to this struct.
65 * @param[in] x x or width component
66 * @param[in] y y or height component
68 explicit Vector2(float x, float y)
74 * @brief Conversion constructor from an array of two floats.
77 * @param[in] array Array of xy
79 explicit Vector2(const float* array)
89 * @param[in] vec3 Vector3 to create this vector from
91 explicit Vector2(const Vector3& vec3);
97 * @param[in] vec4 Vector4 to create this vector from
99 explicit Vector2(const Vector4& vec4);
103 static const Vector2 ONE; ///< (1.0f,1.0f)
104 static const Vector2 XAXIS; ///< Vector representing the X axis
105 static const Vector2 YAXIS; ///< Vector representing the Y axis
106 static const Vector2 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
107 static const Vector2 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
108 static const Vector2 ZERO; ///< (0.0f, 0.0f)
113 * @brief Assignment operator.
116 * @param[in] array Array of floats
119 Vector2& operator=(const float* array)
128 * @brief Assignment operator.
131 * @param[in] rhs Vector to assign
134 Vector2& operator=(const Vector3& rhs);
137 * @brief Assignment operator.
140 * @param[in] rhs Vector to assign
143 Vector2& operator=(const Vector4& rhs);
146 * @brief Addition operator.
149 * @param[in] rhs Vector to add
150 * @return A vector containing the result of the addition
152 Vector2 operator+(const Vector2& rhs) const
160 * @brief Addition assignment operator.
163 * @param[in] rhs Vector to add
166 Vector2& operator+=(const Vector2& rhs)
175 * @brief Subtraction operator.
178 * @param[in] rhs The vector to subtract
179 * @return A vector containing the result of the subtraction
181 Vector2 operator-(const Vector2& rhs) const
189 * @brief Subtraction assignment operator.
192 * @param[in] rhs The vector to subtract
195 Vector2& operator-=(const Vector2& rhs)
204 * @brief Multiplication operator.
207 * @param[in] rhs The vector to multiply
208 * @return A vector containing the result of the multiplication
210 Vector2 operator*(const Vector2& rhs) const
212 return Vector2(x * rhs.x, y * rhs.y);
216 * @brief Multiplication operator.
219 * @param[in] rhs The float value to scale the vector
220 * @return A vector containing the result of the scaling
222 Vector2 operator*(float rhs) const
224 return Vector2(x * rhs, y * rhs);
228 * @brief Multiplication assignment operator.
231 * @param[in] rhs The vector to multiply
234 Vector2& operator*=(const Vector2& rhs)
243 * @brief Multiplication assignment operator.
246 * @param[in] rhs The float value to scale the vector
249 Vector2& operator*=(float rhs)
258 * @brief Division operator.
261 * @param[in] rhs The vector to divide
262 * @return A vector containing the result of the division
264 Vector2 operator/(const Vector2& rhs) const
266 return Vector2(x / rhs.x, y / rhs.y);
270 * @brief Division operator.
273 * @param[in] rhs The float value to scale the vector by
274 * @return A vector containing the result of the scaling
276 Vector2 operator/(float rhs) const
278 return Vector2(x / rhs, y / rhs);
283 * @brief Division assignment operator.
286 * @param[in] rhs The vector to divide
289 Vector2& operator/=(const Vector2& rhs)
298 * @brief Division assignment operator.
301 * @param[in] rhs The float value to scale the vector by
304 Vector2& operator/=(float rhs)
313 * @brief Unary negation operator.
316 * @return A vector containing the negation
318 Vector2 operator-() const
320 Vector2 temp(-x, -y);
326 * @brief Equality operator.
328 * Utilizes appropriate machine epsilon values.
331 * @param[in] rhs The vector to test against
332 * @return true if the vectors are equal
334 bool operator==(const Vector2& rhs) const;
337 * @brief Inequality operator.
339 * Utilizes appropriate machine epsilon values.
342 * @param[in] rhs The vector to test against
343 * @return true if the vectors are not equal
345 bool operator!=(const Vector2& rhs) const
347 return !(*this == rhs);
351 * @brief Const array subscript operator overload.
353 * Asserts if index is out of range. Should be 0 or 1.
355 * @param[in] index Subscript index
356 * @return The float at the given index
358 const float& operator[](const uint32_t index) const
360 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
362 return AsFloat()[index];
366 * @brief Mutable array subscript operator overload.
368 * Asserts if index is out of range. Should be 0 or 1.
370 * @param[in] index Subscript index
371 * @return The float at the given index
373 float& operator[](const uint32_t index)
375 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
377 return AsFloat()[index];
381 * @brief Returns the length of the vector.
384 * @return The length of the vector
386 float Length() const;
389 * @brief Returns the length of the vector squared.
391 * This is more efficient than Length() for threshold
392 * testing as it avoids the use of a square root.
394 * @return The length of the vector squared
396 float LengthSquared() const;
399 * @brief Sets the vector to be unit length, whilst maintaining its direction.
406 * @brief Clamps the vector between minimum and maximum vectors.
409 * @param[in] min The minimum vector
410 * @param[in] max The maximum vector
412 void Clamp( const Vector2& min, const Vector2& max );
415 * @brief Returns the contents of the vector as an array of 2 floats.
417 * The order of the values in this array are as follows:
421 * @return The vector contents as an array of 2 floats
422 * @note inlined for performance reasons (generates less code than a function call)
424 const float* AsFloat() const {return &x;}
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 float* AsFloat() {return &x;}
441 // (x width) and (y height) must be consecutive in memory.
442 // No other data must be added before (x width) member.
443 // No virtual methods must be added to this struct.
446 float x; ///< x component
447 float width; ///< width
452 float y; ///< y component
453 float height; ///< height
459 * @brief Size is an alias of Dali::Vector2.
462 typedef Vector2 Size;
465 * @brief Print a Vector2.
468 * @param[in] o The output stream operator
469 * @param[in] vector The vector to print
470 * @return The output stream operator
472 DALI_CORE_API std::ostream& operator<< (std::ostream& o, const Vector2& vector);
475 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
477 * If a=0,1 and b=1,0 returns a vector of 0,0.
479 * @param[in] a A vector
480 * @param[in] b A vector
481 * @return A vector containing the minimum of each component from a and b
483 inline Vector2 Min( const Vector2& a, const Vector2& b )
485 return Vector2( a.x < b.x ? a.x : b.x , a.y < b.y ? a.y : b.y );
489 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
491 * If a=0,1 and b=1,0 returns a vector of 1,1.
493 * @param[in] a A vector
494 * @param[in] b A vector
495 * @return A vector containing the maximum of each component from a and b
497 inline Vector2 Max( const Vector2& a, const Vector2& b )
499 return Vector2( a.x > b.x ? a.x : b.x , a.y > b.y ? a.y : b.y );
503 * @brief Clamps each of vector v's components between minimum and maximum values.
506 * @param[in] v A vector
507 * @param[in] min The minimum value
508 * @param[in] max The maximum value
509 * @return A vector containing the clamped components of v
511 DALI_CORE_API Vector2 Clamp( const Vector2& v, const float& min, const float& max );
513 // Allow Vector2 to be treated as a POD type
514 template <> struct TypeTraits< Vector2 > : public BasicTypes< Vector2 > { enum { IS_TRIVIAL_TYPE = true }; };
521 #endif // __DALI_VECTOR_2_H__