Partial update implementation, first phase.
[platform/core/uifw/dali-core.git] / dali / public-api / math / rect.h
1 #ifndef DALI_RECT_H
2 #define DALI_RECT_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <math.h>
23 #include <ostream>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/math/math-utils.h>
27
28 namespace Dali
29 {
30 /**
31  * @addtogroup dali_core_math
32  * @{
33  */
34
35 /**
36  * @brief Template class to create and operate on rectangles.
37  * @SINCE_1_0.0
38  */
39
40 template< typename T = float >
41 struct Rect
42 {
43 // Methods
44
45   /**
46    * @brief Constructor.
47    * @SINCE_1_0.0
48    */
49   Rect()
50   : x(0),
51     y(0),
52     width(0),
53     height(0)
54   {
55   }
56
57   /**
58    * @brief Constructor.
59    *
60    * @SINCE_1_0.0
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)
65    */
66   Rect(T x, T y, T width, T height)
67   : x(x),
68     y(y),
69     width(width),
70     height(height)
71   {
72   }
73
74   /**
75    * @brief Conversion constructor from Vector4.
76    *
77    * @SINCE_1_9.14
78    * @param[in] vec4 Vector4 to convert from
79    */
80   Rect( const Vector4& vec4 )
81   : x(vec4.x),
82     y(vec4.y),
83     width(vec4.z),
84     height(vec4.w)
85   {
86   }
87
88   /**
89    * @brief Copy constructor.
90    *
91    * @SINCE_1_0.0
92    * @param[in] rhs The original object
93    */
94   Rect(const Rect<T>& rhs)
95   {
96     x = rhs.x;
97     y = rhs.y;
98     width = rhs.width;
99     height = rhs.height;
100   }
101
102   /**
103    * @brief Assignment operator.
104    *
105    * @SINCE_1_0.0
106    * @param[in] rhs The original object
107    * @return Reference to this
108    */
109   Rect<T>& operator= (const Rect<T>& rhs)
110   {
111     if (this != &rhs)
112     {
113       x = rhs.x;
114       y = rhs.y;
115       width = rhs.width;
116       height = rhs.height;
117     }
118
119     return *this;
120   }
121
122   /**
123    * @brief Assignment operator.
124    *
125    * @SINCE_1_9.14
126    * @param[in] vec4 The Vector4 to assign
127    * @return Reference to this
128    */
129   Rect<T>& operator= (const Vector4& vec4)
130   {
131     x = vec4.x;
132     y = vec4.y;
133     width = vec4.z;
134     height = vec4.w;
135
136     return *this;
137   }
138
139   /**
140    * @brief Assignment from individual values.
141    *
142    * @SINCE_1_0.0
143    * @param[in] newX      X coordinate
144    * @param[in] newY      Y coordinate
145    * @param[in] newWidth  Width
146    * @param[in] newHeight Height
147    */
148   void Set(T newX, T newY, T newWidth, T newHeight)
149   {
150     x = newX;
151     y = newY;
152     width = newWidth;
153     height = newHeight;
154   }
155
156   /**
157    * @brief Determines whether or not this Rectangle is empty.
158    *
159    * @SINCE_1_0.0
160    * @return True if width or height are zero
161    */
162   bool IsEmpty() const
163   {
164     return width  == 0 ||
165       height == 0;
166   }
167
168   /**
169    * @brief Determines whether or not this Rectangle is valid.
170    *
171    * @return True if width and height are not negative
172    */
173   bool IsValid() const
174   {
175     return !(width < 0 || height < 0);
176   }
177
178   /**
179    * @brief Gets the left of the rectangle.
180    *
181    * @SINCE_1_0.0
182    * @return The left edge of the rectangle
183    */
184   T Left() const
185   {
186     return x;
187   }
188   /**
189    * @brief Gets the right of the rectangle.
190    *
191    * @SINCE_1_0.0
192    * @return The right edge of the rectangle
193    */
194   T Right() const
195   {
196     return x + width;
197   }
198
199   /**
200    * @brief Gets the top of the rectangle.
201    *
202    * @SINCE_1_0.0
203    * @return The top of the rectangle
204    */
205   T Top() const
206   {
207     return y;
208   }
209
210   /**
211    * @brief Gets the bottom of the rectangle.
212    *
213    * @SINCE_1_0.0
214    * @return The bottom of the rectangle
215    */
216   T Bottom() const
217   {
218     return y + height;
219   }
220
221   /**
222    * @brief Gets the area of the rectangle.
223    *
224    * @SINCE_1_0.0
225    * @return The area of the rectangle
226    */
227   T Area() const
228   {
229     return width * height;
230   }
231
232   /**
233    * @brief Determines whether or not this rectangle and the specified rectangle intersect.
234    *
235    * @SINCE_1_0.0
236    * @param[in] other The other rectangle to test against this rectangle
237    * @return True if the rectangles intersect
238    */
239   bool Intersects(const Rect<T>& other) const
240   {
241     return (other.x + other.width) > x && other.x < (x + width) &&
242       (other.y + other.height) > y && other.y < (y + height);
243   }
244
245   /**
246    * @brief Intersects this rectangle and the specified rectangle.
247    * The result of the intersection is stored in this rectangle.
248    *
249    * @param[in] rect The other rectangle to intersect with
250    */
251   bool Intersect(const Rect<T>& rect)
252   {
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);
257
258     const int width = right - left;
259     const int height = bottom - top;
260     if (!(width < 0 || height < 0))
261     {
262       x = left;
263       y = top;
264       this->width = width;
265       this->height = height;
266       return true;
267     }
268
269     return false;
270   }
271
272   /**
273    * @brief Merges this rectangle and the specified rectangle.
274    * The result of the merge is stored in this rectangle.
275    *
276    * @param[in] rect The other rectangle to merge with
277    */
278   void Merge(const Rect<T>& rect)
279   {
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);
284     x = left;
285     y = top;
286     width = right - left;
287     height = bottom - top;
288   }
289
290   /**
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.
294    */
295   void Inset(T dx, T dy)
296   {
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;
301     x = left;
302     y = top;
303     width = right - left;
304     height = bottom - top;
305   }
306
307   /**
308    * @brief Determines whether or not this Rectangle contains the specified rectangle.
309    *
310    * @SINCE_1_0.0
311    * @param[in] other The other rectangle to test against this rectangle
312    * @return True if the specified rectangle is contained
313    */
314   bool Contains(const Rect<T>& other) const
315   {
316     return other.x >= x && (other.x + other.width)  <= (x + width) &&
317       other.y >= y && (other.y + other.height) <= (y + height);
318   }
319
320 public:   // Data
321
322   union
323   {
324     T x;      ///< X position of the rectangle
325     T left;   ///< The left value
326   };
327
328   union
329   {
330     T y;      ///< Y position of the rectangle
331     T right;  ///< The right value
332   };
333
334   union
335   {
336     T width;  ///< width of the rectangle
337     T bottom; ///< The bottom value
338   };
339
340   union
341   {
342     T height; ///< height of the rectangle
343     T top;    ///< The top value
344   };
345 };
346
347 /**
348  * @brief Equality operator.
349  *
350  * @SINCE_1_0.0
351  * @param[in] lhs First operand
352  * @param[in] rhs Second operand
353  * @return True if boxes are exactly same
354  */
355 template< typename T >
356 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
357 {
358   return ( lhs.x == rhs.x )&&
359     ( lhs.y == rhs.y )&&
360     ( lhs.width == rhs.width )&&
361     ( lhs.height == rhs.height );
362 }
363
364 /**
365  * @brief Inequality operator.
366  *
367  * @SINCE_1_0.0
368  * @param[in] lhs The first rectangle
369  * @param[in] rhs The second rectangle
370  * @return True if rectangles are not identical
371  */
372 template< typename T >
373 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
374 {
375   return !(lhs == rhs);
376 }
377
378 /**
379  * @brief Equality operator specialization for float.
380  *
381  * @SINCE_1_0.0
382  * @param[in] lhs The first rectangle
383  * @param[in] rhs The second rectangle
384  * @return True if rectangles are exactly same
385  */
386 template<>
387 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
388 {
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) );
393 }
394
395 /**
396  * @brief IsEmpty specialization for float.
397  *
398  * @SINCE_1_0.0
399  * @return True if the rectangle has zero size
400  */
401 template<>
402 inline bool Rect<float>::IsEmpty() const
403 {
404   return (fabsf(width)  <= GetRangedEpsilon(width, width)
405           ||
406           fabsf(height) <= GetRangedEpsilon(height, height));
407 }
408
409 /**
410  * @brief Converts the value of the rectangle into a string and insert in to an output stream.
411  *
412  * @SINCE_1_0.0
413  * @param[in] stream The output stream operator
414  * @param[in] rectangle the rectangle to output
415  * @return The output stream operator
416  */
417 template< typename T >
418 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
419 {
420   return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
421 }
422
423 /**
424  * @}
425  */
426 } // namespace Dali
427
428 #endif // DALI_RECT_H