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.
171 * @return True if width and height are not negative
175 return !(width < 0 || height < 0);
179 * @brief Gets the left of the rectangle.
182 * @return The left edge of the rectangle
189 * @brief Gets the right of the rectangle.
192 * @return The right edge of the rectangle
200 * @brief Gets the top of the rectangle.
203 * @return The top of the rectangle
211 * @brief Gets the bottom of the rectangle.
214 * @return The bottom of the rectangle
222 * @brief Gets the area of the rectangle.
225 * @return The area of the rectangle
229 return width * height;
233 * @brief Determines whether or not this rectangle and the specified rectangle intersect.
236 * @param[in] other The other rectangle to test against this rectangle
237 * @return True if the rectangles intersect
239 bool Intersects(const Rect<T>& other) const
241 return (other.x + other.width) > x && other.x < (x + width) &&
242 (other.y + other.height) > y && other.y < (y + height);
246 * @brief Intersects this rectangle and the specified rectangle.
247 * The result of the intersection is stored in this rectangle.
249 * @param[in] rect The other rectangle to intersect with
251 bool Intersect(const Rect<T>& rect)
253 const int left = std::max(rect.x, x);
254 const int top = std::max(rect.y, y);
255 const int right = std::min(rect.x + rect.width, x + width);
256 const int bottom = std::min(rect.y + rect.height, y + height);
258 const int width = right - left;
259 const int height = bottom - top;
260 if (!(width < 0 || height < 0))
265 this->height = height;
273 * @brief Merges this rectangle and the specified rectangle.
274 * The result of the merge is stored in this rectangle.
276 * @param[in] rect The other rectangle to merge with
278 void Merge(const Rect<T>& rect)
280 const int left = std::min(rect.x, x);
281 const int top = std::min(rect.y, y);
282 const int right = std::max(rect.x + rect.width, x + width);
283 const int bottom = std::max(rect.y + rect.height, y + height);
286 width = right - left;
287 height = bottom - top;
291 * @brief Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards.
292 * If dx is negative, then the sides are moved outwards.
293 * The result of the inset is stored in this rectangle.
295 void Inset(T dx, T dy)
297 const int left = x - dx;
298 const int top = y - dy;
299 const int right = x + width + dx;
300 const int bottom = y + height + dy;
303 width = right - left;
304 height = bottom - top;
308 * @brief Determines whether or not this Rectangle contains the specified rectangle.
311 * @param[in] other The other rectangle to test against this rectangle
312 * @return True if the specified rectangle is contained
314 bool Contains(const Rect<T>& other) const
316 return other.x >= x && (other.x + other.width) <= (x + width) &&
317 other.y >= y && (other.y + other.height) <= (y + height);
324 T x; ///< X position of the rectangle
325 T left; ///< The left value
330 T y; ///< Y position of the rectangle
331 T right; ///< The right value
336 T width; ///< width of the rectangle
337 T bottom; ///< The bottom value
342 T height; ///< height of the rectangle
343 T top; ///< The top value
348 * @brief Equality operator.
351 * @param[in] lhs First operand
352 * @param[in] rhs Second operand
353 * @return True if boxes are exactly same
355 template< typename T >
356 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
358 return ( lhs.x == rhs.x )&&
360 ( lhs.width == rhs.width )&&
361 ( lhs.height == rhs.height );
365 * @brief Inequality operator.
368 * @param[in] lhs The first rectangle
369 * @param[in] rhs The second rectangle
370 * @return True if rectangles are not identical
372 template< typename T >
373 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
375 return !(lhs == rhs);
379 * @brief Equality operator specialization for float.
382 * @param[in] lhs The first rectangle
383 * @param[in] rhs The second rectangle
384 * @return True if rectangles are exactly same
387 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
389 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
390 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
391 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
392 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
396 * @brief IsEmpty specialization for float.
399 * @return True if the rectangle has zero size
402 inline bool Rect<float>::IsEmpty() const
404 return (fabsf(width) <= GetRangedEpsilon(width, width)
406 fabsf(height) <= GetRangedEpsilon(height, height));
410 * @brief Converts the value of the rectangle into a string and insert in to an output stream.
413 * @param[in] stream The output stream operator
414 * @param[in] rectangle the rectangle to output
415 * @return The output stream operator
417 template< typename T >
418 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
420 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
428 #endif // DALI_RECT_H