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