1 #ifndef __DALI_MATH_UTILS_H__
2 #define __DALI_MATH_UTILS_H__
5 * Copyright (c) 2018 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 <cstdint> // uint32_t
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/constants.h>
31 * @addtogroup dali_core_math
36 * @brief Returns the next power of two.
38 * In case of numbers which are already a power of two this function returns the original number.
39 * If i is zero returns 1.
41 * @param[in] i Input number
42 * @return The next power of two or i itself in case it's a power of two
44 inline uint32_t NextPowerOfTwo( uint32_t i )
46 DALI_ASSERT_ALWAYS(i <= 1u << (sizeof(uint32_t) * 8 - 1) && "Return type cannot represent the next power of two greater than the argument.");
63 * @brief Whether a number is power of two.
66 * @param[in] i Input number
67 * @return True if i is power of two.
69 inline bool IsPowerOfTwo( uint32_t i )
71 return (i != 0u) && ((i & (i - 1u)) == 0u);
75 * @brief Clamp a value.
78 * @param[in] value The value to clamp.
79 * @param[in] min The minimum allowed value.
80 * @param[in] max The maximum allowed value.
81 * @return T the clamped value
83 template< typename T >
84 inline const T& Clamp( const T& value, const T& min, const T& max )
86 const T& constrainedUpper = value < max ? value : max;
87 const T& constrainedUpperAndLower = constrainedUpper > min ? constrainedUpper : min;
88 return constrainedUpperAndLower;
92 * @brief Clamp a value directly.
95 * @param[in,out] value The value that will be clamped.
96 * @param[in] min The minimum allowed value.
97 * @param[in] max The maximum allowed value.
99 template< typename T >
100 inline void ClampInPlace( T& value, const T& min, const T& max )
102 const T& constrainedUpper = value < max ? value : max;
103 const T& constrainedUpperAndLower = constrainedUpper > min ? constrainedUpper : min;
104 value = constrainedUpperAndLower;
109 * @brief Linear interpolation between two values.
112 * @param[in] offset The offset through the range @p low to @p high.
113 * This value is clamped between 0 and 1.
114 * @param[in] low Lowest value in range
115 * @param[in] high Highest value in range
116 * @return A value between low and high.
118 template< typename T >
119 inline const T Lerp( const float offset, const T& low, const T& high )
121 return low + ((high - low) * Clamp(offset, 0.0f, 1.0f));
125 * @brief Get an epsilon that is valid for the given range.
128 * @param[in] a the first value in the range
129 * @param[in] b the second value in the range.
130 * @return a suitable epsilon
132 inline float GetRangedEpsilon( float a, float b )
134 const float absA = fabsf( a );
135 const float absB = fabsf( b );
136 const float absF = absA > absB ? absA : absB;
137 const int32_t absI = static_cast<int32_t>( absF ); // truncated
139 float epsilon = Math::MACHINE_EPSILON_10000;
142 return Math::MACHINE_EPSILON_0;
146 return Math::MACHINE_EPSILON_1;
150 return Math::MACHINE_EPSILON_10;
154 return Math::MACHINE_EPSILON_100;
156 else if (absI < 2000)
158 return Math::MACHINE_EPSILON_1000;
164 * @brief Helper function to compare equality of a floating point value with zero.
167 * @param[in] value the value to compare
168 * @return true if the value is equal to zero
170 #pragma GCC diagnostic push
171 #pragma GCC diagnostic ignored "-Wfloat-equal"
172 inline bool EqualsZero( float value )
174 return value == 0.0f;
176 #pragma GCC diagnostic pop
179 * @brief Helper function to compare equality of two floating point values.
182 * @param[in] a the first value to compare
183 * @param[in] b the second value to compare
184 * @return true if the values are equal within a minimal epsilon for their values
186 inline bool Equals( float a, float b )
188 return ( fabsf( a - b ) <= GetRangedEpsilon( a, b ) );
192 * @brief Helper function to compare equality of two floating point values.
195 * @param[in] a the first value to compare
196 * @param[in] b the second value to compare
197 * @param[in] epsilon the minimum epsilon value that will be used to consider the values different
198 * @return true if the difference between the values is less than the epsilon
200 inline bool Equals( float a, float b, float epsilon )
202 return ( fabsf( a - b ) <= epsilon );
206 * @brief Get an float that is rounded at specified place of decimals.
209 * @param[in] value float value
210 * @param[in] pos decimal place
211 * @return a rounded float
213 inline float Round( float value, int32_t pos )
216 temp = value * powf( 10.f, static_cast<float>( pos ) );
217 temp = floorf( temp + 0.5f );
218 temp *= powf( 10.f, static_cast<float>( -pos ) );
223 * @brief Wrap x in domain (start) to (end).
225 * This works like a floating point version
226 * of the % modulo operation. But with an offset (start).
228 * For instance a domain is specified as:
234 * (\ / start) (\ / end)
238 * The value x will be confined to this domain.
239 * If x is below 2 e.g. 0, then it is wrapped to 6.
240 * If x is above or equal to 8 e.g. 8.1 then it is
243 * Domain wrapping is useful for various problems from
244 * calculating positions in a space that repeats, to
245 * computing angles that range from 0 to 360.
248 * @param[in] x the point to be wrapped within the domain
249 * @param[in] start The start of the domain
250 * @param[in] end The end of the domain
252 * @return the wrapped value over the domain (start) (end)
253 * @note If start = end (i.e. size of domain 0), then wrapping will not occur
254 * and result will always be equal to start.
257 inline float WrapInDomain(float x, float start, float end)
259 float domain = end - start;
262 if(fabsf(domain) > Math::MACHINE_EPSILON_1)
264 return start + (x - floorf(x / domain) * domain);
272 * @brief Find the shortest distance (magnitude) and direction (sign)
273 * from (a) to (b) in domain (start) to (end).
276 * (\ / start) (\ / end)
280 * Knowing the shortest distance is useful with wrapped domains
281 * to solve problems such as determing the closest object to
282 * a given point, or determing whether turning left or turning
283 * right is the shortest route to get from angle 10 degrees
284 * to angle 350 degrees (clearly in a 0-360 degree domain, turning
285 * left 20 degrees is quicker than turning right 340 degrees).
287 * The value returned holds the distance and the direction from
288 * value a to value b. For instance in the above example it would
289 * return -20. i.e. subtract 20 from current value (10) to reach
290 * target wrapped value (350).
293 * @param a the current value
294 * @param b the target value
295 * @param start the start of the domain
296 * @param end the end of the domain
297 * @return the shortest direction (the sign) and distance (the magnitude)
298 * @note Assumes both (a) and (b) are already within the domain
302 inline float ShortestDistanceInDomain( float a, float b, float start, float end )
305 float size = end-start;
310 // +ve vector, let's try perspective 1 domain to the right,
311 // and see if closer.
312 float aRight = a+size;
313 if( aRight-b < vect )
320 // -ve vector, let's try perspective 1 domain to the left,
321 // and see if closer.
322 float aLeft = a-size;
333 * @brief Extracts the sign of a number
336 * @param[in] value The value we want to extract the sign
337 * @return -1 for negative values, +1 for positive values and 0 if value is 0
339 template <typename T>
340 int32_t Sign( T value )
342 return ( T(0) < value ) - ( value < T(0) );
350 #endif // __DALI_MATH_UTILS_H__