Merge branch 'devel/master (1.1.2 ~ 1.1.7)' into tizen
[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 /**
109  * @brief Compare equality between two radians.
110  *
111  * @param[in] lhs Radian to compare
112  * @param[in] rhs Radian to compare to
113  * @return true if the values are identical
114  */
115 inline bool operator==( Radian lhs, Radian rhs )
116 {
117   return fabsf( lhs.radian - rhs.radian ) < Math::MACHINE_EPSILON_10; // expect Radian angles to be between 0 and 10 (multiplies of Math::PI)
118 }
119
120 /**
121  * @brief Compare inequality between two radians.
122  *
123  * @param[in] lhs Radian to compare
124  * @param[in] rhs Radian to compare to
125  * @return true if the values are not identical
126  */
127 inline bool operator!=( Radian lhs, Radian rhs )
128 {
129   return !( operator==( lhs, rhs ) );
130 }
131
132 /**
133  * @brief Compare equality between a radian and degree.
134  *
135  * @param[in] lhs Radian to compare
136  * @param[in] rhs Degree to compare to
137  * @return true if the values are identical
138  */
139 inline bool operator==( Radian lhs, Degree rhs )
140 {
141   return fabsf( lhs.radian - Radian( rhs ).radian ) < Math::MACHINE_EPSILON_100; // expect Degree angles to be between 0 and 999
142 }
143
144 /**
145  * @brief Compare inequality between a radian and a degree.
146  *
147  * @param[in] lhs Radian to compare
148  * @param[in] rhs Degree to compare to
149  * @return true if the values are not identical
150  */
151 inline bool operator!=( Radian lhs, Degree rhs )
152 {
153   return !( operator==( lhs, rhs ) );
154 }
155
156 /**
157  * @brief Compare equality between a degree and a radian.
158  *
159  * @param[in] lhs Degree to compare
160  * @param[in] rhs Radian to compare to
161  * @return true if the values are identical
162  */
163 inline bool operator==( Degree lhs, Radian rhs )
164 {
165   return fabsf( Radian( lhs ).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 degree and a radian.
170  *
171  * @param[in] lhs Degree to compare
172  * @param[in] rhs Radian to compare to
173  * @return true if the values are not identical
174  */
175 inline bool operator!=( Degree lhs, Radian rhs )
176 {
177   return !( operator==( lhs, rhs ) );
178 }
179
180 /**
181  * @brief Compare greater than between two radians
182  *
183  * @param[in] lhs Radian to compare
184  * @param[in] rhs Radian to compare to
185  * @return true if lhs is greater than rhs
186  */
187 inline bool operator>( Radian lhs, Radian rhs )
188 {
189   return lhs.radian > rhs.radian;
190 }
191
192 /**
193  * @brief Compare greater than between a radian and a degree.
194  *
195  * @param[in] lhs Radian to compare
196  * @param[in] rhs Degree to compare to
197  * @return true if lhs is greater than rhs
198  */
199 inline bool operator>( Radian lhs, Degree rhs )
200 {
201   return lhs.radian > Radian(rhs).radian;
202 }
203
204 /**
205  * @brief Compare greater than between a radian and a degree.
206  *
207  * @param[in] lhs Radian to compare
208  * @param[in] rhs Degree to compare to
209  * @return true if lhs is greater than rhs
210  */
211 inline bool operator>( Degree lhs, Radian rhs )
212 {
213   return Radian(lhs).radian > rhs.radian;
214 }
215
216 /**
217  * @brief Compare less than between two radians.
218  *
219  * @param[in] lhs Radian to compare
220  * @param[in] rhs Radian to compare to
221  * @return true if lhs is less than rhs
222  */
223 inline bool operator<( Radian lhs, Radian rhs )
224 {
225   return lhs.radian < rhs.radian;
226 }
227
228 /**
229  * @brief Compare less than between a radian and a degree.
230  *
231  * @param[in] lhs Radian to compare
232  * @param[in] rhs Degree to compare to
233  * @return true if lhs is less than rhs
234  */
235 inline bool operator<( Radian lhs, Degree rhs )
236 {
237   return lhs.radian < Radian(rhs).radian;
238 }
239
240 /**
241  * @brief Compare less than between a degree and a radian.
242  *
243  * @param[in] lhs Degree to compare
244  * @param[in] rhs Radian to compare to
245  * @return true if lhs is less than rhs
246  */
247 inline bool operator<( Degree lhs, Radian rhs )
248 {
249   return Radian(lhs).radian < rhs.radian;
250 }
251
252 /**
253  * @brief Multiply Radian with a float
254  *
255  * @param[in] lhs Radian to multiply
256  * @param[in] rhs float to multiply
257  * @return result of the multiplication
258  */
259 inline Radian operator*( Radian lhs, float rhs )
260 {
261   return Radian( lhs.radian * rhs );
262 }
263
264 /**
265  * @brief Negate the radian
266  * @return The negative angle
267  */
268 inline Radian operator-( Radian in )
269 {
270    return Radian( -in.radian );
271 }
272
273 /**
274  * @brief Clamp a radian value
275  * @param angle to clamp
276  * @param min value
277  * @param max value
278  * @return the resulting radian
279  */
280 inline Radian Clamp( Radian angle, float min, float max )
281 {
282   return Radian( Clamp<float>( angle.radian, min, max ) );
283 }
284
285 /**
286  * @}
287  */
288 } // namespace Dali
289
290 #endif // __DALI_RADIAN_H__