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