[dali_1.9.14] Merge branch 'devel/master'
[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 Gets the left of the rectangle.
170    *
171    * @SINCE_1_0.0
172    * @return The left edge of the rectangle
173    */
174   T Left() const
175   {
176     return x;
177   }
178   /**
179    * @brief Gets the right of the rectangle.
180    *
181    * @SINCE_1_0.0
182    * @return The right edge of the rectangle
183    */
184   T Right() const
185   {
186     return x + width;
187   }
188
189   /**
190    * @brief Gets the top of the rectangle.
191    *
192    * @SINCE_1_0.0
193    * @return The top of the rectangle
194    */
195   T Top() const
196   {
197     return y;
198   }
199
200   /**
201    * @brief Gets the bottom of the rectangle.
202    *
203    * @SINCE_1_0.0
204    * @return The bottom of the rectangle
205    */
206   T Bottom() const
207   {
208     return y + height;
209   }
210
211   /**
212    * @brief Gets the area of the rectangle.
213    *
214    * @SINCE_1_0.0
215    * @return The area of the rectangle
216    */
217   T Area() const
218   {
219     return width * height;
220   }
221
222   /**
223    * @brief Determines whether or not this rectangle and the specified rectangle intersect.
224    *
225    * @SINCE_1_0.0
226    * @param[in] other The other rectangle to test against this rectangle
227    * @return True if the rectangles intersect
228    */
229   bool Intersects(const Rect<T>& other) const
230   {
231     return (other.x + other.width)  > x           &&
232       other.x                 < (x + width) &&
233                                 (other.y + other.height) > y           &&
234       other.y                 < (y + height);
235   }
236
237   /**
238    * @brief Determines whether or not this Rectangle contains the specified rectangle.
239    *
240    * @SINCE_1_0.0
241    * @param[in] other The other rectangle to test against this rectangle
242    * @return True if the specified rectangle is contained
243    */
244   bool Contains(const Rect<T>& other) const
245   {
246     return other.x                  >= x           &&
247       (other.x + other.width)  <= (x + width) &&
248       other.y                  >= y           &&
249       (other.y + other.height) <= (y + height);
250   }
251
252 public:   // Data
253
254   union
255   {
256     T x;      ///< X position of the rectangle
257     T left;   ///< The left value
258   };
259
260   union
261   {
262     T y;      ///< Y position of the rectangle
263     T right;  ///< The right value
264   };
265
266   union
267   {
268     T width;  ///< width of the rectangle
269     T bottom; ///< The bottom value
270   };
271
272   union
273   {
274     T height; ///< height of the rectangle
275     T top;    ///< The top value
276   };
277 };
278
279 /**
280  * @brief Equality operator.
281  *
282  * @SINCE_1_0.0
283  * @param[in] lhs First operand
284  * @param[in] rhs Second operand
285  * @return True if boxes are exactly same
286  */
287 template< typename T >
288 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
289 {
290   return ( lhs.x == rhs.x )&&
291     ( lhs.y == rhs.y )&&
292     ( lhs.width == rhs.width )&&
293     ( lhs.height == rhs.height );
294 }
295
296 /**
297  * @brief Inequality operator.
298  *
299  * @SINCE_1_0.0
300  * @param[in] lhs The first rectangle
301  * @param[in] rhs The second rectangle
302  * @return True if rectangles are not identical
303  */
304 template< typename T >
305 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
306 {
307   return !(lhs == rhs);
308 }
309
310 /**
311  * @brief Equality operator specialization for float.
312  *
313  * @SINCE_1_0.0
314  * @param[in] lhs The first rectangle
315  * @param[in] rhs The second rectangle
316  * @return True if rectangles are exactly same
317  */
318 template<>
319 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
320 {
321   return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
322     ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
323     ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
324     ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
325 }
326
327 /**
328  * @brief IsEmpty specialization for float.
329  *
330  * @SINCE_1_0.0
331  * @return True if the rectangle has zero size
332  */
333 template<>
334 inline bool Rect<float>::IsEmpty() const
335 {
336   return (fabsf(width)  <= GetRangedEpsilon(width, width)
337           ||
338           fabsf(height) <= GetRangedEpsilon(height, height));
339 }
340
341 /**
342  * @brief Converts the value of the rectangle into a string and insert in to an output stream.
343  *
344  * @SINCE_1_0.0
345  * @param[in] stream The output stream operator
346  * @param[in] rectangle the rectangle to output
347  * @return The output stream operator
348  */
349 template< typename T >
350 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
351 {
352   return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
353 }
354
355 /**
356  * @}
357  */
358 } // namespace Dali
359
360 #endif // DALI_RECT_H