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.
25 #include <dali/public-api/common/dali-common.h>
34 * @brief A two dimensional vector.
36 struct DALI_IMPORT_API Vector2
38 // (x width) and (y height) must be consecutive in memory.
39 // No other data must be added before (x width) member.
40 // No virtual methods must be added to this struct.
56 * @param[in] x x or width component
57 * @param[in] y y or height component
59 explicit Vector2(float x, float y)
65 * @brief Conversion constructor from an array of two floats.
67 * @param [in] array of xy
69 explicit Vector2(const float* array)
78 * @param [in] vec3 Vector3 to create this vector from
80 explicit Vector2(const Vector3& vec3);
85 * @param [in] vec4 Vector4 to create this vector from
87 explicit Vector2(const Vector4& vec4);
91 static const Vector2 ONE; ///< (1.0f,1.0f)
92 static const Vector2 XAXIS; ///< Vector representing the X axis
93 static const Vector2 YAXIS; ///< Vector representing the Y axis
94 static const Vector2 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
95 static const Vector2 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
96 static const Vector2 ZERO; ///< (0.0f, 0.0f)
101 * @brief Assignment operator.
103 * @param[in] array of floats
106 Vector2& operator=(const float* array)
115 * @brief Assignment operator.
117 * @param[in] rhs vector to assign.
120 Vector2& operator=(const Vector3& rhs);
123 * @brief Assignment operator.
125 * @param[in] rhs vector to assign.
128 Vector2& operator=(const Vector4& rhs);
131 * @brief Addition operator.
133 * @param[in] rhs vector to add.
134 * @return A vector containing the result of the addition
136 Vector2 operator+(const Vector2& rhs) const
144 * @brief Addition assignment operator.
146 * @param[in] rhs vector to add.
149 Vector2& operator+=(const Vector2& rhs)
158 * @brief Subtraction operator.
160 * @param[in] rhs the vector to subtract
161 * @return A vector containing the result of the subtraction
163 Vector2 operator-(const Vector2& rhs) const
171 * @brief Subtraction assignment operator.
173 * @param[in] rhs the vector to subtract
176 Vector2& operator-=(const Vector2& rhs)
185 * @brief Multiplication operator.
187 * @param[in] rhs the vector to multiply
188 * @return A vector containing the result of the multiplication
190 Vector2 operator*(const Vector2& rhs) const
192 return Vector2(x * rhs.x, y * rhs.y);
196 * @brief Multiplication operator.
198 * @param[in] rhs the float value to scale the vector
199 * @return A vector containing the result of the scaling
201 Vector2 operator*(float rhs) const
203 return Vector2(x * rhs, y * rhs);
207 * @brief Multiplication assignment operator.
209 * @param[in] rhs the vector to multiply
212 Vector2& operator*=(const Vector2& rhs)
221 * @brief Multiplication assignment operator.
223 * @param[in] rhs the float value to scale the vector
226 Vector2& operator*=(float rhs)
235 * @brief Division operator.
237 * @param[in] rhs the vector to divide
238 * @return A vector containing the result of the division
240 Vector2 operator/(const Vector2& rhs) const
242 return Vector2(x / rhs.x, y / rhs.y);
246 * @brief Division operator.
248 * @param[in] rhs The float value to scale the vector by
249 * @return A vector containing the result of the scaling
251 Vector2 operator/(float rhs) const
253 return Vector2(x / rhs, y / rhs);
258 * @brief Division assignment operator.
260 * @param[in] rhs the vector to divide
263 Vector2& operator/=(const Vector2& rhs)
272 * @brief Division assignment operator.
274 * @param[in] rhs the float value to scale the vector by
277 Vector2& operator/=(float rhs)
286 * @brief Unary negation operator.
288 * @return A vector containg the negation
290 Vector2 operator-() const
292 Vector2 temp(-x, -y);
298 * @brief Equality operator.
300 * utilises appropriate machine epsilon values;
302 * @param[in] rhs The vector to test against
303 * @return true if the vectors are equal
305 bool operator==(const Vector2& rhs) const;
308 * @brief Inequality operator.
310 * utilises appropriate machine epsilon values;
312 * @param[in] rhs The vector to test against
313 * @return true if the vectors are not equal
315 bool operator!=(const Vector2& rhs) const
317 return !(*this == rhs);
321 * @brief Const array subscript operator overload.
323 * Asserts if index is out of range. Should be 0 or 1
324 * @param[in] index Subscript
325 * @return The float at the given index
327 const float& operator[](const unsigned int index) const
329 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
331 return AsFloat()[index];
335 * @brief Mutable array subscript operator overload.
337 * Asserts if index is out of range. Should be 0 or 1
338 * @param[in] index Subscript index
339 * @return The float at the given index.
341 float& operator[](const unsigned int index)
343 DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
345 return AsFloat()[index];
349 * @brief Returns the length of the vector.
351 * @return the length of the vector
353 float Length() const;
356 * @brief Returns the length of the vector squared.
358 * This is more efficient than Length() for threshold
359 * testing as it avoids the use of a square root.
360 * @return the length of the vector squared.
362 float LengthSquared() const;
365 * @brief Sets the vector to be unit length, whilst maintaining its direction.
371 * @brief Clamps the vector between minimum and maximum vectors.
373 * @param [in] min the minimum vector
374 * @param [in] max the maximum vector
376 void Clamp( const Vector2& min, const Vector2& max );
379 * @brief Returns the contents of the vector as an array of 2 floats.
381 * The order of the values in this array are as follows:
384 * @note inlined for performance reasons (generates less code than a function call)
385 * @return the vector contents as an array of 2 floats.
387 const float* AsFloat() const {return &x;}
390 * @brief Returns the contents of the vector as an array of 2 floats.
392 * The order of the values in this array are as follows:
395 * @note inlined for performance reasons (generates less code than a function call)
396 * @return the vector contents as an array of 2 floats.
398 float* AsFloat() {return &x;}
403 // (x width) and (y height) must be consecutive in memory.
404 // No other data must be added before (x width) member.
405 // No virtual methods must be added to this struct.
408 float x; ///< x component
409 float width; ///< width
414 float y; ///< y component
415 float height; ///< height
421 * @brief Size is an alias of Dali::Vector2
423 typedef Vector2 Size;
426 * @brief Print a Vector2.
428 * @param [in] o The output stream operator.
429 * @param [in] vector The vector to print.
430 * @return The output stream operator.
432 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector2& vector);
435 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
437 * If a=0,1 and b=1,0 returns a vector of 0,0.
438 * @param [in] a a vector
439 * @param [in] b a vector
440 * @return a vector containing the minimum of each component from a and b
442 inline Vector2 Min( const Vector2& a, const Vector2& b )
444 return Vector2( a.x < b.x ? a.x : b.x , a.y < b.y ? a.y : b.y );
448 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
450 * If a=0,1 and b=1,0 returns a vector of 1,1
451 * @param [in] a a vector
452 * @param [in] b a vector
453 * @return a vector containing the maximum of each component from a and b
455 inline Vector2 Max( const Vector2& a, const Vector2& b )
457 return Vector2( a.x > b.x ? a.x : b.x , a.y > b.y ? a.y : b.y );
461 * @brief Clamps each of vector v's components between minimum and maximum values.
463 * @param [in] v a vector
464 * @param [in] min the minimum value
465 * @param [in] max the maximum value
466 * @return a vector containing the clamped components of v
468 DALI_IMPORT_API Vector2 Clamp( const Vector2& v, const float& min, const float& max );
471 * @brief Fits source size inside the target size maintaining aspect ratio.
473 * @pre source width and height > 0
474 * @param [in] target size
475 * @param [in] source size
476 * @return target scaled inside source
478 DALI_IMPORT_API Size FitInside( const Size& target, const Size& source );
481 * @brief Fits or scales to fill.
483 * a) If target width and height are non-zero
484 * Fits source size into target aspect ratio
485 * If source is bigger, simply returns target.
486 * Does not scale larger than source
487 * b) If target width or height is zero
488 * maintains the aspect ratio of source (as target has no aspect ratio)
489 * returns target width and scaled height or target height and scaled width
490 * This algorithm is usefull when you want for example a square thumbnail of a rectangular image data
491 * @param [in] target size
492 * @param [in] source size
493 * @return target scaled inside source
495 DALI_IMPORT_API Size FitScaleToFill( const Size& target, const Size& source );
498 * @brief Shrinks source size inside the target size maintaining aspect ratio of source.
500 * If source is smaller than target it returns source.
501 * @pre source width and height > 0
502 * @param [in] target size
503 * @param [in] source size
504 * @return target scaled inside source
506 DALI_IMPORT_API Size ShrinkInside( const Size& target, const Size& source );
510 #endif // __DALI_VECTOR_2_H__