5 * Copyright (c) 2020 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/math/math-utils.h>
31 * @addtogroup dali_core_math
36 * @brief Template class to create and operate on rectangles.
40 template< typename T = float >
61 * @param[in] x X coordinate (or left)
62 * @param[in] y Y coordinate (or right)
63 * @param[in] width Width (or bottom)
64 * @param[in] height Height (or top)
66 Rect(T x, T y, T width, T height)
75 * @brief Conversion constructor from Vector4.
78 * @param[in] vec4 Vector4 to convert from
80 Rect( const Vector4& vec4 )
89 * @brief Copy constructor.
92 * @param[in] rhs The original object
94 Rect(const Rect<T>& rhs)
103 * @brief Assignment operator.
106 * @param[in] rhs The original object
107 * @return Reference to this
109 Rect<T>& operator= (const Rect<T>& rhs)
123 * @brief Assignment operator.
126 * @param[in] vec4 The Vector4 to assign
127 * @return Reference to this
129 Rect<T>& operator= (const Vector4& vec4)
140 * @brief Assignment from individual values.
143 * @param[in] newX X coordinate
144 * @param[in] newY Y coordinate
145 * @param[in] newWidth Width
146 * @param[in] newHeight Height
148 void Set(T newX, T newY, T newWidth, T newHeight)
157 * @brief Determines whether or not this Rectangle is empty.
160 * @return True if width or height are zero
169 * @brief Determines whether or not this Rectangle is valid.
172 * @return True if width and height are not negative
176 return !(width < 0 || height < 0);
180 * @brief Gets the left of the rectangle.
183 * @return The left edge of the rectangle
190 * @brief Gets the right of the rectangle.
193 * @return The right edge of the rectangle
201 * @brief Gets the top of the rectangle.
204 * @return The top of the rectangle
212 * @brief Gets the bottom of the rectangle.
215 * @return The bottom of the rectangle
223 * @brief Gets the area of the rectangle.
226 * @return The area of the rectangle
230 return width * height;
234 * @brief Determines whether or not this rectangle and the specified rectangle intersect.
237 * @param[in] other The other rectangle to test against this rectangle
238 * @return True if the rectangles intersect
240 bool Intersects(const Rect<T>& other) const
242 return (other.x + other.width) > x && other.x < (x + width) &&
243 (other.y + other.height) > y && other.y < (y + height);
247 * @brief Intersects this rectangle and the specified rectangle.
248 * The result of the intersection is stored in this rectangle.
251 * @param[in] rect The other rectangle to intersect with
252 * @return True if the rectangles intersect
254 bool Intersect(const Rect<T>& rect)
256 const int left = std::max(rect.x, x);
257 const int top = std::max(rect.y, y);
258 const int right = std::min(rect.x + rect.width, x + width);
259 const int bottom = std::min(rect.y + rect.height, y + height);
261 const int width = right - left;
262 const int height = bottom - top;
263 if (!(width < 0 || height < 0))
268 this->height = height;
276 * @brief Merges this rectangle and the specified rectangle.
277 * The result of the merge is stored in this rectangle.
280 * @param[in] rect The other rectangle to merge with
282 void Merge(const Rect<T>& rect)
284 const int left = std::min(rect.x, x);
285 const int top = std::min(rect.y, y);
286 const int right = std::max(rect.x + rect.width, x + width);
287 const int bottom = std::max(rect.y + rect.height, y + height);
290 width = right - left;
291 height = bottom - top;
295 * @brief Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards.
296 * If dx is negative, then the sides are moved outwards.
297 * The result of the inset is stored in this rectangle.
300 void Inset(T dx, T dy)
302 const int left = x - dx;
303 const int top = y - dy;
304 const int right = x + width + dx;
305 const int bottom = y + height + dy;
308 width = right - left;
309 height = bottom - top;
313 * @brief Determines whether or not this Rectangle contains the specified rectangle.
316 * @param[in] other The other rectangle to test against this rectangle
317 * @return True if the specified rectangle is contained
319 bool Contains(const Rect<T>& other) const
321 return other.x >= x && (other.x + other.width) <= (x + width) &&
322 other.y >= y && (other.y + other.height) <= (y + height);
329 T x; ///< X position of the rectangle
330 T left; ///< The left value
335 T y; ///< Y position of the rectangle
336 T right; ///< The right value
341 T width; ///< width of the rectangle
342 T bottom; ///< The bottom value
347 T height; ///< height of the rectangle
348 T top; ///< The top value
353 * @brief Equality operator.
356 * @param[in] lhs First operand
357 * @param[in] rhs Second operand
358 * @return True if boxes are exactly same
360 template< typename T >
361 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
363 return ( lhs.x == rhs.x )&&
365 ( lhs.width == rhs.width )&&
366 ( lhs.height == rhs.height );
370 * @brief Inequality operator.
373 * @param[in] lhs The first rectangle
374 * @param[in] rhs The second rectangle
375 * @return True if rectangles are not identical
377 template< typename T >
378 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
380 return !(lhs == rhs);
384 * @brief Equality operator specialization for float.
387 * @param[in] lhs The first rectangle
388 * @param[in] rhs The second rectangle
389 * @return True if rectangles are exactly same
392 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
394 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
395 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
396 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
397 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
401 * @brief IsEmpty specialization for float.
404 * @return True if the rectangle has zero size
407 inline bool Rect<float>::IsEmpty() const
409 return (fabsf(width) <= GetRangedEpsilon(width, width)
411 fabsf(height) <= GetRangedEpsilon(height, height));
415 * @brief Converts the value of the rectangle into a string and insert in to an output stream.
418 * @param[in] stream The output stream operator
419 * @param[in] rectangle the rectangle to output
420 * @return The output stream operator
422 template< typename T >
423 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
425 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
433 #endif // DALI_RECT_H