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