[3.0] Update doxygen comments
[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) 2015 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_object
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 Copy constructor.
76    *
77    * @SINCE_1_0.0
78    * @param[in] rhs The original object
79    */
80   Rect(const Rect<T>& rhs)
81   {
82     x = rhs.x;
83     y = rhs.y;
84     width = rhs.width;
85     height = rhs.height;
86   }
87
88   /**
89    * @brief Assignment operator.
90    *
91    * @SINCE_1_0.0
92    * @param[in] rhs The original object
93    * @return Reference to this
94    */
95   Rect<T>& operator= (const Rect<T>& rhs)
96   {
97     if (this != &rhs)
98     {
99       x = rhs.x;
100       y = rhs.y;
101       width = rhs.width;
102       height = rhs.height;
103     }
104
105     return *this;
106   }
107
108   /**
109    * @brief Assignment from individual values.
110    *
111    * @SINCE_1_0.0
112    * @param[in] newX      X coordinate
113    * @param[in] newY      Y coordinate
114    * @param[in] newWidth  Width
115    * @param[in] newHeight Height
116    */
117   void Set(T newX, T newY, T newWidth, T newHeight)
118   {
119     x = newX;
120     y = newY;
121     width = newWidth;
122     height = newHeight;
123   }
124
125   /**
126    * @brief Determines whether or not this Rectangle is empty.
127    *
128    * @SINCE_1_0.0
129    * @return True if width or height are zero
130    */
131   bool IsEmpty() const
132   {
133     return width  == 0 ||
134       height == 0;
135   }
136
137   /**
138    * @brief Gets the left of the rectangle.
139    *
140    * @SINCE_1_0.0
141    * @return The left edge of the rectangle
142    */
143   T Left() const
144   {
145     return x;
146   }
147   /**
148    * @brief Gets the right of the rectangle.
149    *
150    * @SINCE_1_0.0
151    * @return The right edge of the rectangle
152    */
153   T Right() const
154   {
155     return x + width;
156   }
157
158   /**
159    * @brief Gets the top of the rectangle.
160    *
161    * @SINCE_1_0.0
162    * @return The top of the rectangle
163    */
164   T Top() const
165   {
166     return y;
167   }
168
169   /**
170    * @brief Gets the bottom of the rectangle.
171    *
172    * @SINCE_1_0.0
173    * @return The bottom of the rectangle
174    */
175   T Bottom() const
176   {
177     return y + height;
178   }
179
180   /**
181    * @brief Gets the area of the rectangle.
182    *
183    * @SINCE_1_0.0
184    * @return The area of the rectangle
185    */
186   T Area() const
187   {
188     return width * height;
189   }
190
191   /**
192    * @brief Determines whether or not this rectangle and the specified rectangle intersect.
193    *
194    * @SINCE_1_0.0
195    * @param[in] other The other rectangle to test against this rectangle
196    * @return True if the rectangles intersect
197    */
198   bool Intersects(const Rect<T>& other) const
199   {
200     return (other.x + other.width)  > x           &&
201       other.x                 < (x + width) &&
202                                 (other.y + other.height) > y           &&
203       other.y                 < (y + height);
204   }
205
206   /**
207    * @brief Determines whether or not this Rectangle contains the specified rectangle.
208    *
209    * @SINCE_1_0.0
210    * @param[in] other The other rectangle to test against this rectangle
211    * @return True if the specified rectangle is contained
212    */
213   bool Contains(const Rect<T>& other) const
214   {
215     return other.x                  >= x           &&
216       (other.x + other.width)  <= (x + width) &&
217       other.y                  >= y           &&
218       (other.y + other.height) <= (y + height);
219   }
220
221 public:   // Data
222
223   union
224   {
225     T x;      ///< X position of the rectangle
226     T left;   ///< The left value
227   };
228
229   union
230   {
231     T y;      ///< Y position of the rectangle
232     T right;  ///< The right value
233   };
234
235   union
236   {
237     T width;  ///< width of the rectangle
238     T bottom; ///< The bottom value
239   };
240
241   union
242   {
243     T height; ///< height of the rectangle
244     T top;    ///< The top value
245   };
246 };
247
248 /**
249  * @brief Equality operator.
250  *
251  * @SINCE_1_0.0
252  * @param[in] lhs First operand
253  * @param[in] rhs Second operand
254  * @return True if boxes are exactly same
255  */
256 template< typename T >
257 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
258 {
259   return ( lhs.x == rhs.x )&&
260     ( lhs.y == rhs.y )&&
261     ( lhs.width == rhs.width )&&
262     ( lhs.height == rhs.height );
263 }
264
265 /**
266  * @brief Inequality operator.
267  *
268  * @SINCE_1_0.0
269  * @param[in] lhs The first rectangle
270  * @param[in] rhs The second rectangle
271  * @return True if rectangles are not identical
272  */
273 template< typename T >
274 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
275 {
276   return !(lhs == rhs);
277 }
278
279 /**
280  * @brief Equality operator specialization for float.
281  *
282  * @SINCE_1_0.0
283  * @param[in] lhs The first rectangle
284  * @param[in] rhs The second rectangle
285  * @return True if rectangles are exactly same
286  */
287 template<>
288 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
289 {
290   return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
291     ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
292     ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
293     ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
294 }
295
296 /**
297  * @brief IsEmpty specialization for float.
298  *
299  * @SINCE_1_0.0
300  * @return True if the rectangle has zero size
301  */
302 template<>
303 inline bool Rect<float>::IsEmpty() const
304 {
305   return (fabsf(width)  <= GetRangedEpsilon(width, width)
306           ||
307           fabsf(height) <= GetRangedEpsilon(height, height));
308 }
309
310 /**
311  * @brief Converts the value of the rectangle into a string and insert in to an output stream.
312  *
313  * @SINCE_1_0.0
314  * @param[in] stream The output stream operator
315  * @param[in] rectangle the rectangle to output
316  * @return The output stream operator
317  */
318 template< typename T >
319 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
320 {
321   return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
322 }
323
324 /**
325  * @}
326  */
327 } // namespace Dali
328
329 #endif // __DALI_RECT_H__