X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Fpublic-api%2Fmath%2Frect.h;h=de43c9125b0d042c77a7ea2ae4eeacbad12fe185;hb=8a73b1bfdc359a52a3cf1b104b4e42902866705e;hp=9d14f59debfcce9bc4058795016a517ad87c661b;hpb=eb6f5959d8f8e03bf8bfba6c3091bdb962ec0830;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/public-api/math/rect.h b/dali/public-api/math/rect.h index 9d14f59..de43c91 100644 --- a/dali/public-api/math/rect.h +++ b/dali/public-api/math/rect.h @@ -1,8 +1,8 @@ -#ifndef __DALI_RECT_H__ -#define __DALI_RECT_H__ +#ifndef DALI_RECT_H +#define DALI_RECT_H /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ namespace Dali { /** - * @addtogroup dali_core_object + * @addtogroup dali_core_math * @{ */ @@ -36,11 +36,10 @@ namespace Dali * @brief Template class to create and operate on rectangles. * @SINCE_1_0.0 */ - -template< typename T = float > +template struct Rect { -// Methods + // Methods /** * @brief Constructor. @@ -72,35 +71,66 @@ struct Rect } /** - * @brief Copy constructor. + * @brief Conversion constructor from Vector4. * - * @SINCE_1_0.0 - * @param[in] rhs The original object + * @SINCE_1_9.14 + * @param[in] vec4 Vector4 to convert from */ - Rect(const Rect& rhs) + Rect(const Vector4& vec4) + : x(vec4.x), + y(vec4.y), + width(vec4.z), + height(vec4.w) { - x = rhs.x; - y = rhs.y; - width = rhs.width; - height = rhs.height; } /** - * @brief Assignment operator. + * @brief Default copy constructor. * - * @SINCE_1_0.0 + * @SINCE_1_9.27 + * @param[in] rhs The original object + */ + Rect(const Rect& rhs) = default; + + /** + * @brief Default copy assignment operator. + * + * @SINCE_1_9.27 * @param[in] rhs The original object * @return Reference to this */ - Rect& operator= (const Rect& rhs) + Rect& operator=(const Rect& rhs) = default; + + /** + * @brief Default move constructor. + * + * @SINCE_1_9.27 + * @param[in] rhs The original object + */ + Rect(Rect&& rhs) noexcept = default; + + /** + * @brief Default move assignment operator. + * + * @SINCE_1_9.27 + * @param[in] rhs The original object + * @return Reference to this + */ + Rect& operator=(Rect&& rhs) noexcept = default; + + /** + * @brief Assignment operator. + * + * @SINCE_1_9.14 + * @param[in] vec4 The Vector4 to assign + * @return Reference to this + */ + Rect& operator=(const Vector4& vec4) { - if (this != &rhs) - { - x = rhs.x; - y = rhs.y; - width = rhs.width; - height = rhs.height; - } + x = vec4.x; + y = vec4.y; + width = vec4.z; + height = vec4.w; return *this; } @@ -116,9 +146,9 @@ struct Rect */ void Set(T newX, T newY, T newWidth, T newHeight) { - x = newX; - y = newY; - width = newWidth; + x = newX; + y = newY; + width = newWidth; height = newHeight; } @@ -130,8 +160,19 @@ struct Rect */ bool IsEmpty() const { - return width == 0 || - height == 0; + return width == 0 || + height == 0; + } + + /** + * @brief Determines whether or not this Rectangle is valid. + * + * @SINCE_1_9.18 + * @return True if width and height are not negative + */ + bool IsValid() const + { + return !(width < 0 || height < 0); } /** @@ -197,10 +238,74 @@ struct Rect */ bool Intersects(const Rect& other) const { - return (other.x + other.width) > x && - other.x < (x + width) && - (other.y + other.height) > y && - other.y < (y + height); + return (other.x + other.width) > x && other.x < (x + width) && + (other.y + other.height) > y && other.y < (y + height); + } + + /** + * @brief Intersects this rectangle and the specified rectangle. + * The result of the intersection is stored in this rectangle. + * + * @SINCE_1_9.18 + * @param[in] rect The other rectangle to intersect with + * @return True if the rectangles intersect + */ + bool Intersect(const Rect& rect) + { + const int left = std::max(rect.x, x); + const int top = std::max(rect.y, y); + const int right = std::min(rect.x + rect.width, x + width); + const int bottom = std::min(rect.y + rect.height, y + height); + + const int width = right - left; + const int height = bottom - top; + if(!(width < 0 || height < 0)) + { + x = left; + y = top; + this->width = width; + this->height = height; + return true; + } + + return false; + } + + /** + * @brief Merges this rectangle and the specified rectangle. + * The result of the merge is stored in this rectangle. + * + * @SINCE_1_9.18 + * @param[in] rect The other rectangle to merge with + */ + void Merge(const Rect& rect) + { + const int left = std::min(rect.x, x); + const int top = std::min(rect.y, y); + const int right = std::max(rect.x + rect.width, x + width); + const int bottom = std::max(rect.y + rect.height, y + height); + x = left; + y = top; + width = right - left; + height = bottom - top; + } + + /** + * @brief Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards. + * If dx is negative, then the sides are moved outwards. + * The result of the inset is stored in this rectangle. + * @SINCE_1_9.18 + */ + void Inset(T dx, T dy) + { + const int left = x - dx; + const int top = y - dy; + const int right = x + width + dx; + const int bottom = y + height + dy; + x = left; + y = top; + width = right - left; + height = bottom - top; } /** @@ -212,24 +317,21 @@ struct Rect */ bool Contains(const Rect& other) const { - return other.x >= x && - (other.x + other.width) <= (x + width) && - other.y >= y && - (other.y + other.height) <= (y + height); + return other.x >= x && (other.x + other.width) <= (x + width) && + other.y >= y && (other.y + other.height) <= (y + height); } -public: // Data - +public: // Data union { - T x; ///< X position of the rectangle - T left; ///< The left value + T x; ///< X position of the rectangle + T left; ///< The left value }; union { - T y; ///< Y position of the rectangle - T right; ///< The right value + T y; ///< Y position of the rectangle + T right; ///< The right value }; union @@ -253,13 +355,13 @@ public: // Data * @param[in] rhs Second operand * @return True if boxes are exactly same */ -template< typename T > -inline bool operator==( const Rect& lhs, const Rect& rhs ) +template +inline bool operator==(const Rect& lhs, const Rect& rhs) { - return ( lhs.x == rhs.x )&& - ( lhs.y == rhs.y )&& - ( lhs.width == rhs.width )&& - ( lhs.height == rhs.height ); + return (lhs.x == rhs.x) && + (lhs.y == rhs.y) && + (lhs.width == rhs.width) && + (lhs.height == rhs.height); } /** @@ -270,8 +372,8 @@ inline bool operator==( const Rect& lhs, const Rect& rhs ) * @param[in] rhs The second rectangle * @return True if rectangles are not identical */ -template< typename T > -inline bool operator!=( const Rect& lhs, const Rect& rhs ) +template +inline bool operator!=(const Rect& lhs, const Rect& rhs) { return !(lhs == rhs); } @@ -285,12 +387,12 @@ inline bool operator!=( const Rect& lhs, const Rect& rhs ) * @return True if rectangles are exactly same */ template<> -inline bool operator==( const Rect& lhs, const Rect& rhs ) +inline bool operator==(const Rect& lhs, const Rect& rhs) { - return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&& - ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&& - ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&& - ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) ); + return (fabsf(lhs.x - rhs.x) < GetRangedEpsilon(lhs.x, rhs.x)) && + (fabsf(lhs.y - rhs.y) < GetRangedEpsilon(lhs.y, rhs.y)) && + (fabsf(lhs.width - rhs.width) < GetRangedEpsilon(lhs.width, rhs.width)) && + (fabsf(lhs.height - rhs.height) < GetRangedEpsilon(lhs.height, rhs.height)); } /** @@ -302,8 +404,7 @@ inline bool operator==( const Rect& lhs, const Rect& rhs ) template<> inline bool Rect::IsEmpty() const { - return (fabsf(width) <= GetRangedEpsilon(width, width) - || + return (fabsf(width) <= GetRangedEpsilon(width, width) || fabsf(height) <= GetRangedEpsilon(height, height)); } @@ -315,8 +416,8 @@ inline bool Rect::IsEmpty() const * @param[in] rectangle the rectangle to output * @return The output stream operator */ -template< typename T > -inline std::ostream& operator<< (std::ostream& stream, const Rect& rectangle) +template +inline std::ostream& operator<<(std::ostream& stream, const Rect& rectangle) { return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]"; } @@ -326,4 +427,4 @@ inline std::ostream& operator<< (std::ostream& stream, const Rect& rectangle) */ } // namespace Dali -#endif // __DALI_RECT_H__ +#endif // DALI_RECT_H