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