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.
41 struct DALI_IMPORT_API Vector2
43 // (x width) and (y height) must be consecutive in memory.
44 // No other data must be added before (x width) member.
45 // No virtual methods must be added to this struct.
61 * @param[in] x x or width component
62 * @param[in] y y or height component
64 explicit Vector2(float x, float y)
70 * @brief Conversion constructor from an array of two floats.
72 * @param [in] array of xy
74 explicit Vector2(const float* array)
83 * @param [in] vec3 Vector3 to create this vector from
85 explicit Vector2(const Vector3& vec3);
90 * @param [in] vec4 Vector4 to create this vector from
92 explicit Vector2(const Vector4& vec4);
96 static const Vector2 ONE; ///< (1.0f,1.0f)
97 static const Vector2 XAXIS; ///< Vector representing the X axis
98 static const Vector2 YAXIS; ///< Vector representing the Y axis
99 static const Vector2 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
100 static const Vector2 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
101 static const Vector2 ZERO; ///< (0.0f, 0.0f)
106 * @brief Assignment operator.
108 * @param[in] array of floats
111 Vector2& operator=(const float* array)
120 * @brief Assignment operator.
122 * @param[in] rhs vector to assign.
125 Vector2& operator=(const Vector3& rhs);
128 * @brief Assignment operator.
130 * @param[in] rhs vector to assign.
133 Vector2& operator=(const Vector4& rhs);
136 * @brief Addition operator.
138 * @param[in] rhs vector to add.
139 * @return A vector containing the result of the addition
141 Vector2 operator+(const Vector2& rhs) const
149 * @brief Addition assignment operator.
151 * @param[in] rhs vector to add.
154 Vector2& operator+=(const Vector2& rhs)
163 * @brief Subtraction operator.
165 * @param[in] rhs the vector to subtract
166 * @return A vector containing the result of the subtraction
168 Vector2 operator-(const Vector2& rhs) const
176 * @brief Subtraction assignment operator.
178 * @param[in] rhs the vector to subtract
181 Vector2& operator-=(const Vector2& rhs)
190 * @brief Multiplication operator.
192 * @param[in] rhs the vector to multiply
193 * @return A vector containing the result of the multiplication
195 Vector2 operator*(const Vector2& rhs) const
197 return Vector2(x * rhs.x, y * rhs.y);
201 * @brief Multiplication operator.
203 * @param[in] rhs the float value to scale the vector
204 * @return A vector containing the result of the scaling
206 Vector2 operator*(float rhs) const
208 return Vector2(x * rhs, y * rhs);
212 * @brief Multiplication assignment operator.
214 * @param[in] rhs the vector to multiply
217 Vector2& operator*=(const Vector2& rhs)
226 * @brief Multiplication assignment operator.
228 * @param[in] rhs the float value to scale the vector
231 Vector2& operator*=(float rhs)
240 * @brief Division operator.
242 * @param[in] rhs the vector to divide
243 * @return A vector containing the result of the division
245 Vector2 operator/(const Vector2& rhs) const
247 return Vector2(x / rhs.x, y / rhs.y);
251 * @brief Division operator.
253 * @param[in] rhs The float value to scale the vector by
254 * @return A vector containing the result of the scaling
256 Vector2 operator/(float rhs) const
258 return Vector2(x / rhs, y / rhs);
263 * @brief Division assignment operator.
265 * @param[in] rhs the vector to divide
268 Vector2& operator/=(const Vector2& rhs)
277 * @brief Division assignment operator.
279 * @param[in] rhs the float value to scale the vector by
282 Vector2& operator/=(float rhs)
291 * @brief Unary negation operator.
293 * @return A vector containg the negation
295 Vector2 operator-() const
297 Vector2 temp(-x, -y);
303 * @brief Equality operator.
305 * utilises appropriate machine epsilon values;
307 * @param[in] rhs The vector to test against
308 * @return true if the vectors are equal
310 bool operator==(const Vector2& rhs) const;
313 * @brief Inequality operator.
315 * utilises appropriate machine epsilon values;
317 * @param[in] rhs The vector to test against
318 * @return true if the vectors are not equal
320 bool operator!=(const Vector2& rhs) const
322 return !(*this == rhs);
326 * @brief Const array subscript operator overload.
328 * Asserts if index is out of range. Should be 0 or 1
329 * @param[in] index Subscript
330 * @return The float at the given index
332 const float& operator[](const unsigned int index) const
334 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
336 return AsFloat()[index];
340 * @brief Mutable array subscript operator overload.
342 * Asserts if index is out of range. Should be 0 or 1
343 * @param[in] index Subscript index
344 * @return The float at the given index.
346 float& operator[](const unsigned int index)
348 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
350 return AsFloat()[index];
354 * @brief Returns the length of the vector.
356 * @return the length of the vector
358 float Length() const;
361 * @brief Returns the length of the vector squared.
363 * This is more efficient than Length() for threshold
364 * testing as it avoids the use of a square root.
365 * @return the length of the vector squared.
367 float LengthSquared() const;
370 * @brief Sets the vector to be unit length, whilst maintaining its direction.
376 * @brief Clamps the vector between minimum and maximum vectors.
378 * @param [in] min the minimum vector
379 * @param [in] max the maximum vector
381 void Clamp( const Vector2& min, const Vector2& max );
384 * @brief Returns the contents of the vector as an array of 2 floats.
386 * The order of the values in this array are as follows:
389 * @note inlined for performance reasons (generates less code than a function call)
390 * @return the vector contents as an array of 2 floats.
392 const float* AsFloat() const {return &x;}
395 * @brief Returns the contents of the vector as an array of 2 floats.
397 * The order of the values in this array are as follows:
400 * @note inlined for performance reasons (generates less code than a function call)
401 * @return the vector contents as an array of 2 floats.
403 float* AsFloat() {return &x;}
408 // (x width) and (y height) must be consecutive in memory.
409 // No other data must be added before (x width) member.
410 // No virtual methods must be added to this struct.
413 float x; ///< x component
414 float width; ///< width
419 float y; ///< y component
420 float height; ///< height
426 * @brief Size is an alias of Dali::Vector2
428 typedef Vector2 Size;
431 * @brief Print a Vector2.
433 * @param [in] o The output stream operator.
434 * @param [in] vector The vector to print.
435 * @return The output stream operator.
437 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector2& vector);
440 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
442 * If a=0,1 and b=1,0 returns a vector of 0,0.
443 * @param [in] a a vector
444 * @param [in] b a vector
445 * @return a vector containing the minimum of each component from a and b
447 inline Vector2 Min( const Vector2& a, const Vector2& b )
449 return Vector2( a.x < b.x ? a.x : b.x , a.y < b.y ? a.y : b.y );
453 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
455 * If a=0,1 and b=1,0 returns a vector of 1,1
456 * @param [in] a a vector
457 * @param [in] b a vector
458 * @return a vector containing the maximum of each component from a and b
460 inline Vector2 Max( const Vector2& a, const Vector2& b )
462 return Vector2( a.x > b.x ? a.x : b.x , a.y > b.y ? a.y : b.y );
466 * @brief Clamps each of vector v's components between minimum and maximum values.
468 * @param [in] v a vector
469 * @param [in] min the minimum value
470 * @param [in] max the maximum value
471 * @return a vector containing the clamped components of v
473 DALI_IMPORT_API Vector2 Clamp( const Vector2& v, const float& min, const float& max );
475 // Allow Vector2 to be treated as a POD type
476 template <> struct TypeTraits< Vector2 > : public BasicTypes< Vector2 > { enum { IS_TRIVIAL_TYPE = true }; };
483 #endif // __DALI_VECTOR_2_H__