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