1 #ifndef __DALI_VECTOR_2_H__
2 #define __DALI_VECTOR_2_H__
5 * Copyright (c) 2014 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.
26 #include <dali/public-api/common/dali-common.h>
28 namespace Dali DALI_IMPORT_API
35 * @brief A two dimensional vector.
37 struct DALI_IMPORT_API Vector2
39 // (x width) and (y height) must be consecutive in memory.
40 // No other data must be added before (x width) member.
41 // No virtual methods must be added to this struct.
57 * @param[in] x x or width component
58 * @param[in] y y or height component
60 explicit Vector2(float x, float y)
66 * @brief Conversion constructor from an array of two floats.
68 * @param [in] array of xy
70 explicit Vector2(const float* array)
79 * @param [in] vec3 Vector3 to create this vector from
81 explicit Vector2(const Vector3& vec3);
86 * @param [in] vec4 Vector4 to create this vector from
88 explicit Vector2(const Vector4& vec4);
92 static const Vector2 ONE; ///< (1.0f,1.0f)
93 static const Vector2 XAXIS; ///< Vector representing the X axis
94 static const Vector2 YAXIS; ///< Vector representing the Y axis
95 static const Vector2 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
96 static const Vector2 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
97 static const Vector2 ZERO; ///< (0.0f, 0.0f)
102 * @brief Assignment operator.
104 * @param[in] array of floats
107 Vector2& operator=(const float* array)
116 * @brief Assignment operator.
118 * @param[in] rhs vector to assign.
121 Vector2& operator=(const Vector3& rhs);
124 * @brief Assignment operator.
126 * @param[in] rhs vector to assign.
129 Vector2& operator=(const Vector4& rhs);
132 * @brief Addition operator.
134 * @param[in] rhs vector to add.
135 * @return A vector containing the result of the addition
137 Vector2 operator+(const Vector2& rhs) const
145 * @brief Addition assignment operator.
147 * @param[in] rhs vector to add.
150 Vector2& operator+=(const Vector2& rhs)
159 * @brief Subtraction operator.
161 * @param[in] rhs the vector to subtract
162 * @return A vector containing the result of the subtraction
164 Vector2 operator-(const Vector2& rhs) const
172 * @brief Subtraction assignment operator.
174 * @param[in] rhs the vector to subtract
177 Vector2& operator-=(const Vector2& rhs)
186 * @brief Multiplication operator.
188 * @param[in] rhs the vector to multiply
189 * @return A vector containing the result of the multiplication
191 Vector2 operator*(const Vector2& rhs) const
193 return Vector2(x * rhs.x, y * rhs.y);
197 * @brief Multiplication operator.
199 * @param[in] rhs the float value to scale the vector
200 * @return A vector containing the result of the scaling
202 Vector2 operator*(float rhs) const
204 return Vector2(x * rhs, y * rhs);
208 * @brief Multiplication assignment operator.
210 * @param[in] rhs the vector to multiply
213 Vector2& operator*=(const Vector2& rhs)
222 * @brief Multiplication assignment operator.
224 * @param[in] rhs the float value to scale the vector
227 Vector2& operator*=(float rhs)
236 * @brief Division operator.
238 * @param[in] rhs the vector to divide
239 * @return A vector containing the result of the division
241 Vector2 operator/(const Vector2& rhs) const
243 return Vector2(x / rhs.x, y / rhs.y);
247 * @brief Division operator.
249 * @param[in] rhs The float value to scale the vector by
250 * @return A vector containing the result of the scaling
252 Vector2 operator/(float rhs) const
254 return Vector2(x / rhs, y / rhs);
259 * @brief Division assignment operator.
261 * @param[in] rhs the vector to divide
264 Vector2& operator/=(const Vector2& rhs)
273 * @brief Division assignment operator.
275 * @param[in] rhs the float value to scale the vector by
278 Vector2& operator/=(float rhs)
287 * @brief Unary negation operator.
289 * @return A vector containg the negation
291 Vector2 operator-() const
293 Vector2 temp(-x, -y);
299 * @brief Equality operator.
301 * utilises appropriate machine epsilon values;
303 * @param[in] rhs The vector to test against
304 * @return true if the vectors are equal
306 bool operator==(const Vector2& rhs) const;
309 * @brief Inequality operator.
311 * utilises appropriate machine epsilon values;
313 * @param[in] rhs The vector to test against
314 * @return true if the vectors are not equal
316 bool operator!=(const Vector2& rhs) const
318 return !(*this == rhs);
322 * @brief Const array subscript operator overload.
324 * Asserts if index is out of range. Should be 0 or 1
325 * @param[in] index Subscript
326 * @return The float at the given index
328 const float& operator[](const unsigned int index) const
330 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
332 return AsFloat()[index];
336 * @brief Mutable array subscript operator overload.
338 * Asserts if index is out of range. Should be 0 or 1
339 * @param[in] index Subscript index
340 * @return The float at the given index.
342 float& operator[](const unsigned int index)
344 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
346 return AsFloat()[index];
350 * @brief Returns the length of the vector.
352 * @return the length of the vector
354 float Length() const;
357 * @brief Returns the length of the vector squared.
359 * This is more efficient than Length() for threshold
360 * testing as it avoids the use of a square root.
361 * @return the length of the vector squared.
363 float LengthSquared() const;
366 * @brief Sets the vector to be unit length, whilst maintaining its direction.
372 * @brief Clamps the vector between minimum and maximum vectors.
374 * @param [in] min the minimum vector
375 * @param [in] max the maximum vector
377 void Clamp( const Vector2& min, const Vector2& max );
380 * @brief Returns the contents of the vector as an array of 2 floats.
382 * The order of the values in this array are as follows:
385 * @note inlined for performance reasons (generates less code than a function call)
386 * @return the vector contents as an array of 2 floats.
388 const float* AsFloat() const {return &x;}
391 * @brief Returns the contents of the vector as an array of 2 floats.
393 * The order of the values in this array are as follows:
396 * @note inlined for performance reasons (generates less code than a function call)
397 * @return the vector contents as an array of 2 floats.
399 float* AsFloat() {return &x;}
404 // (x width) and (y height) must be consecutive in memory.
405 // No other data must be added before (x width) member.
406 // No virtual methods must be added to this struct.
409 float x; ///< x component
410 float width; ///< width
415 float y; ///< y component
416 float height; ///< height
422 * @brief Size is an alias of Dali::Vector2
424 typedef Vector2 Size;
427 * @brief Print a Vector2.
429 * @param [in] o The output stream operator.
430 * @param [in] vector The vector to print.
431 * @return The output stream operator.
433 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector2& vector);
436 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
438 * If a=0,1 and b=1,0 returns a vector of 0,0.
439 * @param [in] a a vector
440 * @param [in] b a vector
441 * @return a vector containing the minimum of each component from a and b
443 inline Vector2 Min( const Vector2& a, const Vector2& b )
445 return Vector2( std::min(a.x,b.x), std::min(a.y,b.y) );
449 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
451 * If a=0,1 and b=1,0 returns a vector of 1,1
452 * @param [in] a a vector
453 * @param [in] b a vector
454 * @return a vector containing the maximum of each component from a and b
456 inline Vector2 Max( const Vector2& a, const Vector2& b )
458 return Vector2( std::max(a.x,b.x), std::max(a.y,b.y) );
462 * @brief Clamps each of vector v's components between minimum and maximum values.
464 * @param [in] v a vector
465 * @param [in] min the minimum value
466 * @param [in] max the maximum value
467 * @return a vector containing the clamped components of v
469 DALI_IMPORT_API Vector2 Clamp( const Vector2& v, const float& min, const float& max );
472 * @brief Fits source size inside the target size maintaining aspect ratio.
474 * @pre source width and height > 0
475 * @param [in] target size
476 * @param [in] source size
477 * @return target scaled inside source
479 DALI_IMPORT_API Size FitInside( const Size& target, const Size& source );
482 * @brief Fits or scales to fill.
484 * a) If target width and height are non-zero
485 * Fits source size into target aspect ratio
486 * If source is bigger, simply returns target.
487 * Does not scale larger than source
488 * b) If target width or height is zero
489 * maintains the aspect ratio of source (as target has no aspect ratio)
490 * returns target width and scaled height or target height and scaled width
491 * This algorithm is usefull when you want for example a square thumbnail of a rectangular image data
492 * @param [in] target size
493 * @param [in] source size
494 * @return target scaled inside source
496 DALI_IMPORT_API Size FitScaleToFill( const Size& target, const Size& source );
499 * @brief Shrinks source size inside the target size maintaining aspect ratio of source.
501 * If source is smaller than target it returns source.
502 * @pre source width and height > 0
503 * @param [in] target size
504 * @param [in] source size
505 * @return target scaled inside source
507 DALI_IMPORT_API Size ShrinkInside( const Size& target, const Size& source );
511 #endif // __DALI_VECTOR_2_H__