1 #ifndef __DALI_VECTOR_2_H__
2 #define __DALI_VECTOR_2_H__
5 * Copyright (c) 2015 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.
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/type-traits.h>
31 * @addtogroup dali_core_math
39 * @brief A two dimensional vector.
42 struct DALI_IMPORT_API Vector2
44 // (x width) and (y height) must be consecutive in memory.
45 // No other data must be added before (x width) member.
46 // No virtual methods must be added to this struct.
64 * @param[in] x x or width component
65 * @param[in] y y or height component
67 explicit Vector2(float x, float y)
73 * @brief Conversion constructor from an array of two floats.
76 * @param [in] array Array of xy
78 explicit Vector2(const float* array)
88 * @param [in] vec3 Vector3 to create this vector from
90 explicit Vector2(const Vector3& vec3);
96 * @param [in] vec4 Vector4 to create this vector from
98 explicit Vector2(const Vector4& vec4);
102 static const Vector2 ONE; ///< (1.0f,1.0f)
103 static const Vector2 XAXIS; ///< Vector representing the X axis
104 static const Vector2 YAXIS; ///< Vector representing the Y axis
105 static const Vector2 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
106 static const Vector2 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
107 static const Vector2 ZERO; ///< (0.0f, 0.0f)
112 * @brief Assignment operator.
115 * @param[in] array Array of floats
118 Vector2& operator=(const float* array)
127 * @brief Assignment operator.
130 * @param[in] rhs Vector to assign.
133 Vector2& operator=(const Vector3& rhs);
136 * @brief Assignment operator.
139 * @param[in] rhs Vector to assign.
142 Vector2& operator=(const Vector4& rhs);
145 * @brief Addition operator.
148 * @param[in] rhs Vector to add.
149 * @return A vector containing the result of the addition
151 Vector2 operator+(const Vector2& rhs) const
159 * @brief Addition assignment operator.
162 * @param[in] rhs Vector to add.
165 Vector2& operator+=(const Vector2& rhs)
174 * @brief Subtraction operator.
177 * @param[in] rhs The vector to subtract
178 * @return A vector containing the result of the subtraction
180 Vector2 operator-(const Vector2& rhs) const
188 * @brief Subtraction assignment operator.
191 * @param[in] rhs The vector to subtract
194 Vector2& operator-=(const Vector2& rhs)
203 * @brief Multiplication operator.
206 * @param[in] rhs The vector to multiply
207 * @return A vector containing the result of the multiplication
209 Vector2 operator*(const Vector2& rhs) const
211 return Vector2(x * rhs.x, y * rhs.y);
215 * @brief Multiplication operator.
218 * @param[in] rhs The float value to scale the vector
219 * @return A vector containing the result of the scaling
221 Vector2 operator*(float rhs) const
223 return Vector2(x * rhs, y * rhs);
227 * @brief Multiplication assignment operator.
230 * @param[in] rhs The vector to multiply
233 Vector2& operator*=(const Vector2& rhs)
242 * @brief Multiplication assignment operator.
245 * @param[in] rhs The float value to scale the vector
248 Vector2& operator*=(float rhs)
257 * @brief Division operator.
260 * @param[in] rhs The vector to divide
261 * @return A vector containing the result of the division
263 Vector2 operator/(const Vector2& rhs) const
265 return Vector2(x / rhs.x, y / rhs.y);
269 * @brief Division operator.
272 * @param[in] rhs The float value to scale the vector by
273 * @return A vector containing the result of the scaling
275 Vector2 operator/(float rhs) const
277 return Vector2(x / rhs, y / rhs);
282 * @brief Division assignment operator.
285 * @param[in] rhs The vector to divide
288 Vector2& operator/=(const Vector2& rhs)
297 * @brief Division assignment operator.
300 * @param[in] rhs The float value to scale the vector by
303 Vector2& operator/=(float rhs)
312 * @brief Unary negation operator.
315 * @return A vector containg the negation
317 Vector2 operator-() const
319 Vector2 temp(-x, -y);
325 * @brief Equality operator.
327 * utilises appropriate machine epsilon values;
330 * @param[in] rhs The vector to test against
331 * @return true if the vectors are equal
333 bool operator==(const Vector2& rhs) const;
336 * @brief Inequality operator.
338 * utilises appropriate machine epsilon values;
341 * @param[in] rhs The vector to test against
342 * @return true if the vectors are not equal
344 bool operator!=(const Vector2& rhs) const
346 return !(*this == rhs);
350 * @brief Const array subscript operator overload.
352 * Asserts if index is out of range. Should be 0 or 1
354 * @param[in] index Subscript index
355 * @return The float at the given index
357 const float& operator[](const unsigned int index) const
359 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
361 return AsFloat()[index];
365 * @brief Mutable array subscript operator overload.
367 * Asserts if index is out of range. Should be 0 or 1
369 * @param[in] index Subscript index
370 * @return The float at the given index.
372 float& operator[](const unsigned int index)
374 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
376 return AsFloat()[index];
380 * @brief Returns the length of the vector.
383 * @return the length of the vector
385 float Length() const;
388 * @brief Returns the length of the vector squared.
390 * This is more efficient than Length() for threshold
391 * testing as it avoids the use of a square root.
393 * @return the length of the vector squared.
395 float LengthSquared() const;
398 * @brief Sets the vector to be unit length, whilst maintaining its direction.
405 * @brief Clamps the vector between minimum and maximum vectors.
408 * @param [in] min The minimum vector
409 * @param [in] max The maximum vector
411 void Clamp( const Vector2& min, const Vector2& max );
414 * @brief Returns the contents of the vector as an array of 2 floats.
416 * The order of the values in this array are as follows:
420 * @return the vector contents as an array of 2 floats.
421 * @note inlined for performance reasons (generates less code than a function call)
423 const float* AsFloat() const {return &x;}
426 * @brief Returns the contents of the vector as an array of 2 floats.
428 * The order of the values in this array are as follows:
432 * @return the vector contents as an array of 2 floats.
433 * @note inlined for performance reasons (generates less code than a function call)
435 float* AsFloat() {return &x;}
440 // (x width) and (y height) must be consecutive in memory.
441 // No other data must be added before (x width) member.
442 // No virtual methods must be added to this struct.
445 float x; ///< x component
446 float width; ///< width
451 float y; ///< y component
452 float height; ///< height
458 * @brief Size is an alias of Dali::Vector2
461 typedef Vector2 Size;
464 * @brief Print a Vector2.
467 * @param [in] o The output stream operator.
468 * @param [in] vector The vector to print.
469 * @return The output stream operator.
471 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector2& vector);
474 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
476 * If a=0,1 and b=1,0 returns a vector of 0,0.
478 * @param [in] a a vector
479 * @param [in] b a vector
480 * @return a vector containing the minimum of each component from a and b
482 inline Vector2 Min( const Vector2& a, const Vector2& b )
484 return Vector2( a.x < b.x ? a.x : b.x , a.y < b.y ? a.y : b.y );
488 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
490 * If a=0,1 and b=1,0 returns a vector of 1,1
492 * @param [in] a a vector
493 * @param [in] b a vector
494 * @return a vector containing the maximum of each component from a and b
496 inline Vector2 Max( const Vector2& a, const Vector2& b )
498 return Vector2( a.x > b.x ? a.x : b.x , a.y > b.y ? a.y : b.y );
502 * @brief Clamps each of vector v's components between minimum and maximum values.
505 * @param [in] v A vector
506 * @param [in] min The minimum value
507 * @param [in] max The maximum value
508 * @return a vector containing the clamped components of v
510 DALI_IMPORT_API Vector2 Clamp( const Vector2& v, const float& min, const float& max );
512 // Allow Vector2 to be treated as a POD type
513 template <> struct TypeTraits< Vector2 > : public BasicTypes< Vector2 > { enum { IS_TRIVIAL_TYPE = true }; };
520 #endif // __DALI_VECTOR_2_H__