5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
30 * @addtogroup dali_core_math
35 * @brief An angle in radians.
37 * This reduces ambiguity when using methods which accept angles in degrees or radians.
43 * @brief Default constructor, initializes to 0.
52 * @brief Creates an angle in radians.
55 * @param[in] value The initial value in radians
57 explicit constexpr Radian(float value)
63 * @brief Creates an angle in radians from an angle in degrees.
66 * @param[in] degree The initial value in degrees
69 : radian(degree.degree * Math::PI_OVER_180)
74 * @brief Assigns an angle from a float value.
77 * @param[in] value Float value in radians
78 * @return A reference to this
80 Radian& operator=(float value)
87 * @brief Assigns an angle from a Degree value.
90 * @param[in] degree The value in degrees
91 * @return A reference to this
93 Radian& operator=(Degree degree)
95 radian = degree.degree * Math::PI_OVER_180;
100 * @brief Conversion to float.
102 * @return The float value of this Radian
104 operator float() const
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
117 float radian; ///< The value in radians
120 // compiler generated destructor, copy constructor and assignment operators are ok as this class is POD
123 * @brief Compares equality between two radians.
126 * @param[in] lhs Radian to compare
127 * @param[in] rhs Radian to compare to
128 * @return True if the values are identical
130 inline bool operator==(Radian lhs, Radian rhs)
132 return fabsf(lhs.radian - rhs.radian) < Math::MACHINE_EPSILON_10; // expect Radian angles to be between 0 and 10 (multiplies of Math::PI)
136 * @brief Compares inequality between two radians.
139 * @param[in] lhs Radian to compare
140 * @param[in] rhs Radian to compare to
141 * @return True if the values are not identical
143 inline bool operator!=(Radian lhs, Radian rhs)
145 return !(operator==(lhs, rhs));
149 * @brief Compares equality between a radian and degree.
152 * @param[in] lhs Radian to compare
153 * @param[in] rhs Degree to compare to
154 * @return True if the values are identical
156 inline bool operator==(Radian lhs, Degree rhs)
158 return fabsf(lhs.radian - Radian(rhs).radian) < Math::MACHINE_EPSILON_100; // expect Degree angles to be between 0 and 999
162 * @brief Compares inequality between a radian and a degree.
165 * @param[in] lhs Radian to compare
166 * @param[in] rhs Degree to compare to
167 * @return True if the values are not identical
169 inline bool operator!=(Radian lhs, Degree rhs)
171 return !(operator==(lhs, rhs));
175 * @brief Compares equality between a degree and a radian.
178 * @param[in] lhs Degree to compare
179 * @param[in] rhs Radian to compare to
180 * @return True if the values are identical
182 inline bool operator==(Degree lhs, Radian rhs)
184 return fabsf(Radian(lhs).radian - rhs.radian) < Math::MACHINE_EPSILON_100; // expect Degree angles to be between 0 and 999
188 * @brief Compares inequality between a degree and a radian.
191 * @param[in] lhs Degree to compare
192 * @param[in] rhs Radian to compare to
193 * @return True if the values are not identical
195 inline bool operator!=(Degree lhs, Radian rhs)
197 return !(operator==(lhs, rhs));
201 * @brief Compares greater than between two radians
204 * @param[in] lhs Radian to compare
205 * @param[in] rhs Radian to compare to
206 * @return True if lhs is greater than rhs
208 inline bool operator>(Radian lhs, Radian rhs)
210 return lhs.radian > rhs.radian;
214 * @brief Compares greater than between a radian and a degree.
217 * @param[in] lhs Radian to compare
218 * @param[in] rhs Degree to compare to
219 * @return True if lhs is greater than rhs
221 inline bool operator>(Radian lhs, Degree rhs)
223 return lhs.radian > Radian(rhs).radian;
227 * @brief Compares greater than between a radian and a degree.
230 * @param[in] lhs Radian to compare
231 * @param[in] rhs Degree to compare to
232 * @return True if lhs is greater than rhs
234 inline bool operator>(Degree lhs, Radian rhs)
236 return Radian(lhs).radian > rhs.radian;
240 * @brief Compares less than between two radians.
243 * @param[in] lhs Radian to compare
244 * @param[in] rhs Radian to compare to
245 * @return True if lhs is less than rhs
247 inline bool operator<(Radian lhs, Radian rhs)
249 return lhs.radian < rhs.radian;
253 * @brief Compares less than between a radian and a degree.
256 * @param[in] lhs Radian to compare
257 * @param[in] rhs Degree to compare to
258 * @return True if lhs is less than rhs
260 inline bool operator<(Radian lhs, Degree rhs)
262 return lhs.radian < Radian(rhs).radian;
266 * @brief Compares less than between a degree and a radian.
269 * @param[in] lhs Degree to compare
270 * @param[in] rhs Radian to compare to
271 * @return True if lhs is less than rhs
273 inline bool operator<(Degree lhs, Radian rhs)
275 return Radian(lhs).radian < rhs.radian;
279 * @brief Multiplies Radian with a float.
282 * @param[in] lhs Radian to multiply
283 * @param[in] rhs float to multiply
284 * @return Result of the multiplication
286 inline Radian operator*(Radian lhs, float rhs)
288 return Radian(lhs.radian * rhs);
292 * @brief Negates the radian.
294 * @param[in] in Radian to negate
295 * @return The negative angle
297 inline Radian operator-(Radian in)
299 return Radian(-in.radian);
303 * @brief Clamps a radian value.
305 * @param angle to clamp
308 * @return The resulting radian
310 inline Radian Clamp(Radian angle, float min, float max)
312 return Radian(Clamp<float>(angle.radian, min, max));
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);
334 #endif // DALI_RADIAN_H