Merge "Doxygen grouping" into devel/master
[platform/core/uifw/dali-core.git] / dali / public-api / math / radian.h
1 #ifndef __DALI_RADIAN_H__
2 #define __DALI_RADIAN_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 // INTERNAL INCLUDES
22 #include <dali/public-api/common/constants.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/public-api/math/math-utils.h>
25 #include <dali/public-api/math/degree.h>
26
27 namespace Dali
28 {
29 /**
30  * @addtogroup dali-core-math
31  * @{
32  */
33
34 /**
35  * @brief An angle in radians.
36  *
37  * This reduces ambiguity when using methods which accept angles in degrees or radians.
38  */
39 struct Radian
40 {
41   /**
42    * @brief default constructor, initialises to 0.
43    */
44   Radian()
45   : radian( 0.f )
46   { }
47
48   /**
49    * @brief Create an angle in radians.
50    *
51    * @param[in] value The initial value in radians.
52    */
53   explicit Radian( float value )
54   : radian( value )
55   { }
56
57   /**
58    * @brief Create an angle in radians from an angle in degrees.
59    *
60    * @param[in] degree The initial value in degrees.
61    */
62   Radian( Degree degree )
63   : radian( degree.degree * Math::PI_OVER_180 )
64   { }
65
66   /**
67    * @brief Assign an angle from a float value.
68    *
69    * @param[in] value Float value in radians
70    * @return a reference to this object
71    */
72   Radian& operator=( float value )
73   {
74     radian = value;
75     return *this;
76   }
77
78   /**
79    * @brief Assign an angle from a Degree value.
80    *
81    * @param[in] degree The value in degrees.
82    * @return a reference to this object
83    */
84   Radian& operator=( Degree degree )
85   {
86     radian = degree.degree * Math::PI_OVER_180;
87     return *this;
88   }
89
90   /**
91    * @brief Conversion to float
92    * @return the float value of this Radian
93    */
94   operator float() const
95   {
96     return radian;
97   }
98
99 public:
100
101   // member data
102   float radian; ///< The value in radians
103
104 };
105
106 // compiler generated destructor, copy constructor and assignment operators are ok as this class is POD
107
108 // useful constant angles
109 static const Radian ANGLE_360 = Radian( Math::PI * 2.f     ); ///< 360 degree turn in radians
110 static const Radian ANGLE_315 = Radian( Math::PI * 1.75f   ); ///< 315 degree turn in radians
111 static const Radian ANGLE_270 = Radian( Math::PI * 1.50f   ); ///< 270 degree turn in radians
112 static const Radian ANGLE_225 = Radian( Math::PI * 1.25f   ); ///< 225 degree turn in radians
113 static const Radian ANGLE_180 = Radian( Math::PI           ); ///< 180 degree turn in radians
114 static const Radian ANGLE_135 = Radian( Math::PI * 0.75f   ); ///< 135 degree turn in radians
115 static const Radian ANGLE_120 = Radian( Math::PI * 2.f/3.f ); ///< 120 degree turn in radians
116 static const Radian ANGLE_90  = Radian( Math::PI_2         ); ///< 90 degree turn in radians
117 static const Radian ANGLE_45  = Radian( Math::PI_4         ); ///< 45 degree turn in radians
118 static const Radian ANGLE_60  = Radian( Math::PI / 3.f     ); ///< 60 degree turn in radians
119 static const Radian ANGLE_30  = Radian( Math::PI / 6.f     ); ///< 30 degree turn in radians
120 static const Radian ANGLE_0   = Radian( 0.0f               ); ///< 0 degree turn in radians
121
122 /**
123  * @brief Compare equality between two radians.
124  *
125  * @param[in] lhs Radian to compare
126  * @param[in] rhs Radian to compare to
127  * @return true if the values are identical
128  */
129 inline bool operator==( Radian lhs, Radian rhs )
130 {
131   return fabsf( lhs.radian - rhs.radian ) < Math::MACHINE_EPSILON_10; // expect Radian angles to be between 0 and 10 (multiplies of Math::PI)
132 }
133
134 /**
135  * @brief Compare inequality between two radians.
136  *
137  * @param[in] lhs Radian to compare
138  * @param[in] rhs Radian to compare to
139  * @return true if the values are not identical
140  */
141 inline bool operator!=( Radian lhs, Radian rhs )
142 {
143   return !( operator==( lhs, rhs ) );
144 }
145
146 /**
147  * @brief Compare equality between a radian and degree.
148  *
149  * @param[in] lhs Radian to compare
150  * @param[in] rhs Degree to compare to
151  * @return true if the values are identical
152  */
153 inline bool operator==( Radian lhs, Degree rhs )
154 {
155   return fabsf( lhs.radian - Radian( rhs ).radian ) < Math::MACHINE_EPSILON_100; // expect Degree angles to be between 0 and 999
156 }
157
158 /**
159  * @brief Compare inequality between a radian and a degree.
160  *
161  * @param[in] lhs Radian to compare
162  * @param[in] rhs Degree to compare to
163  * @return true if the values are not identical
164  */
165 inline bool operator!=( Radian lhs, Degree rhs )
166 {
167   return !( operator==( lhs, rhs ) );
168 }
169
170 /**
171  * @brief Compare equality between a degree and a radian.
172  *
173  * @param[in] lhs Degree to compare
174  * @param[in] rhs Radian to compare to
175  * @return true if the values are identical
176  */
177 inline bool operator==( Degree lhs, Radian rhs )
178 {
179   return fabsf( Radian( lhs ).radian - rhs.radian ) < Math::MACHINE_EPSILON_100; // expect Degree angles to be between 0 and 999
180 }
181
182 /**
183  * @brief Compare inequality between a degree and a radian.
184  *
185  * @param[in] lhs Degree to compare
186  * @param[in] rhs Radian to compare to
187  * @return true if the values are not identical
188  */
189 inline bool operator!=( Degree lhs, Radian rhs )
190 {
191   return !( operator==( lhs, rhs ) );
192 }
193
194 /**
195  * @brief Compare greater than between two radians
196  *
197  * @param[in] lhs Radian to compare
198  * @param[in] rhs Radian to compare to
199  * @return true if lhs is greater than rhs
200  */
201 inline bool operator>( Radian lhs, Radian rhs )
202 {
203   return lhs.radian > rhs.radian;
204 }
205
206 /**
207  * @brief Compare greater than between a radian and a degree.
208  *
209  * @param[in] lhs Radian to compare
210  * @param[in] rhs Degree to compare to
211  * @return true if lhs is greater than rhs
212  */
213 inline bool operator>( Radian lhs, Degree rhs )
214 {
215   return lhs.radian > Radian(rhs).radian;
216 }
217
218 /**
219  * @brief Compare greater than between a radian and a degree.
220  *
221  * @param[in] lhs Radian to compare
222  * @param[in] rhs Degree to compare to
223  * @return true if lhs is greater than rhs
224  */
225 inline bool operator>( Degree lhs, Radian rhs )
226 {
227   return Radian(lhs).radian > rhs.radian;
228 }
229
230 /**
231  * @brief Compare less than between two radians.
232  *
233  * @param[in] lhs Radian to compare
234  * @param[in] rhs Radian to compare to
235  * @return true if lhs is less than rhs
236  */
237 inline bool operator<( Radian lhs, Radian rhs )
238 {
239   return lhs.radian < rhs.radian;
240 }
241
242 /**
243  * @brief Compare less than between a radian and a degree.
244  *
245  * @param[in] lhs Radian to compare
246  * @param[in] rhs Degree to compare to
247  * @return true if lhs is less than rhs
248  */
249 inline bool operator<( Radian lhs, Degree rhs )
250 {
251   return lhs.radian < Radian(rhs).radian;
252 }
253
254 /**
255  * @brief Compare less than between a degree and a radian.
256  *
257  * @param[in] lhs Degree to compare
258  * @param[in] rhs Radian to compare to
259  * @return true if lhs is less than rhs
260  */
261 inline bool operator<( Degree lhs, Radian rhs )
262 {
263   return Radian(lhs).radian < rhs.radian;
264 }
265
266 /**
267  * @brief Multiply Radian with a float
268  *
269  * @param[in] lhs Radian to multiply
270  * @param[in] rhs float to multiply
271  * @return result of the multiplication
272  */
273 inline Radian operator*( Radian lhs, float rhs )
274 {
275   return Radian( lhs.radian * rhs );
276 }
277
278 /**
279  * @brief Negate the radian
280  * @return The negative angle
281  */
282 inline Radian operator-( Radian in )
283 {
284    return Radian( -in.radian );
285 }
286
287 /**
288  * @brief Clamp a radian value
289  * @param angle to clamp
290  * @param min value
291  * @param max value
292  * @return the resulting radian
293  */
294 inline Radian Clamp( Radian angle, float min, float max )
295 {
296   return Radian( Clamp<float>( angle.radian, min, max ) );
297 }
298
299 /**
300  * @}
301  */
302 } // namespace Dali
303
304 #endif // __DALI_RADIAN_H__