521d266160aa3f5abf327b063bf92706c5675eda
[platform/core/uifw/dali-core.git] / dali / public-api / math / math-utils.h
1 #ifndef __DALI_MATH_UTILS_H__
2 #define __DALI_MATH_UTILS_H__
3
4 /*
5  * Copyright (c) 2018 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 <cstdint> // uint32_t
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/constants.h>
27
28 namespace Dali
29 {
30 /**
31  * @addtogroup dali_core_math
32  * @{
33  */
34
35 /**
36  * @brief Returns the next power of two.
37  *
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.
40  * @SINCE_1_0.0
41  * @param[in] i Input number
42  * @return The next power of two or i itself in case it's a power of two
43  */
44 inline uint32_t NextPowerOfTwo( uint32_t i )
45 {
46   DALI_ASSERT_ALWAYS(i <= 1u << (sizeof(uint32_t) * 8 - 1) && "Return type cannot represent the next power of two greater than the argument.");
47   if(i==0u)
48   {
49     return 1u;
50   }
51
52   i--;
53   i |= i >> 1;
54   i |= i >> 2;
55   i |= i >> 4;
56   i |= i >> 8;
57   i |= i >> 16;
58   i++;
59   return i;
60 }
61
62 /**
63  * @brief Whether a number is power of two.
64  *
65  * @SINCE_1_0.0
66  * @param[in] i Input number
67  * @return    True if i is power of two.
68  */
69 inline bool IsPowerOfTwo( uint32_t i )
70 {
71   return (i != 0u) && ((i & (i - 1u)) == 0u);
72 }
73
74 /**
75  * @brief Clamp a value.
76  *
77  * @SINCE_1_0.0
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
82  */
83 template< typename T >
84 inline const T& Clamp( const T& value, const T& min, const T& max )
85 {
86   const T& constrainedUpper = value < max ? value : max;
87   const T& constrainedUpperAndLower = constrainedUpper > min ? constrainedUpper : min;
88   return  constrainedUpperAndLower;
89 }
90
91 /**
92  * @brief Clamp a value directly.
93  *
94  * @SINCE_1_0.0
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.
98  */
99 template< typename T >
100 inline void ClampInPlace( T& value, const T& min, const T& max )
101 {
102   const T& constrainedUpper = value < max ? value : max;
103   const T& constrainedUpperAndLower = constrainedUpper > min ? constrainedUpper : min;
104   value = constrainedUpperAndLower;
105 }
106
107
108 /**
109  * @brief Linear interpolation between two values.
110  *
111  * @SINCE_1_0.0
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.
117  */
118 template< typename T >
119 inline const T Lerp( const float offset, const T& low, const T& high )
120 {
121   return low + ((high - low) * Clamp(offset, 0.0f, 1.0f));
122 }
123
124 /**
125  * @brief Get an epsilon that is valid for the given range.
126  *
127  * @SINCE_1_0.0
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
131  */
132 inline float GetRangedEpsilon( float a, float b )
133 {
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
138
139   float epsilon = Math::MACHINE_EPSILON_10000;
140   if (absF < 0.1f)
141   {
142     return Math::MACHINE_EPSILON_0;
143   }
144   else if (absI < 2)
145   {
146     return Math::MACHINE_EPSILON_1;
147   }
148   else if (absI < 20)
149   {
150     return Math::MACHINE_EPSILON_10;
151   }
152   else if (absI < 200)
153   {
154     return Math::MACHINE_EPSILON_100;
155   }
156   else if (absI < 2000)
157   {
158     return Math::MACHINE_EPSILON_1000;
159   }
160   return epsilon;
161 }
162
163 /**
164  * @brief Helper function to compare equality of a floating point value with zero.
165  *
166  * @SINCE_1_0.0
167  * @param[in] value the value to compare
168  * @return true if the value is equal to zero
169  */
170 #pragma GCC diagnostic push
171 #pragma GCC diagnostic ignored "-Wfloat-equal"
172 inline bool EqualsZero( float value )
173 {
174   return value == 0.0f;
175 }
176 #pragma GCC diagnostic pop
177
178 /**
179  * @brief Helper function to compare equality of two floating point values.
180  *
181  * @SINCE_1_0.0
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
185  */
186 inline bool Equals( float a, float b )
187 {
188   return ( fabsf( a - b ) <= GetRangedEpsilon( a, b ) );
189 }
190
191 /**
192  * @brief Helper function to compare equality of two floating point values.
193  *
194  * @SINCE_1_0.0
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
199  */
200 inline bool Equals( float a, float b, float epsilon )
201 {
202   return ( fabsf( a - b ) <= epsilon );
203 }
204
205 /**
206  * @brief Get an float that is rounded at specified place of decimals.
207  *
208  * @SINCE_1_0.0
209  * @param[in] value float value
210  * @param[in] pos decimal place
211  * @return a rounded float
212  */
213 inline float Round( float value, int32_t pos )
214 {
215   float temp;
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 ) );
219   return temp;
220 }
221
222 /**
223  * @brief Wrap x in domain (start) to (end).
224  *
225  * This works like a floating point version
226  * of the % modulo operation. But with an offset (start).
227  *
228  * For instance a domain is specified as:
229  * start: 2
230  * end: 8
231  *
232  * @code
233  *   2                         8
234  * (\ / start)               (\ / end)
235  *   |----x                    |
236  * @endcode
237  *
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
241  * wrapped to 2.1.
242  *
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.
246  *
247  * @SINCE_1_0.0
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
251  *
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.
255  *
256  */
257 inline float WrapInDomain(float x, float start, float end)
258 {
259   float domain = end - start;
260   x -= start;
261
262   if(fabsf(domain) > Math::MACHINE_EPSILON_1)
263   {
264     return start + (x - floorf(x / domain) * domain);
265   }
266
267   return start;
268 }
269
270
271 /**
272  * @brief Find the shortest distance (magnitude) and direction (sign)
273  * from (a) to (b) in domain (start) to (end).
274  *
275  * @code
276  *  (\ / start)               (\ / end)
277  *    |-a                 b<----|
278  * @endcode
279  *
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).
286  *
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).
291  *
292  * @SINCE_1_0.0
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
299  * (start) to (end).
300  *
301  */
302 inline float ShortestDistanceInDomain( float a, float b, float start, float end )
303 {
304   //  (a-start + end-b)
305   float size = end-start;
306   float vect = b-a;
307
308   if(vect > 0)
309   {
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 )
314     {
315       return b-aRight;
316     }
317   }
318   else
319   {
320     // -ve vector, let's try perspective 1 domain to the left,
321     // and see if closer.
322     float aLeft = a-size;
323     if( aLeft-b > vect )
324     {
325       return b-aLeft;
326     }
327   }
328
329   return vect;
330 }
331
332 /**
333  * @brief Extracts the sign of a number
334  *
335  * @SINCE_1_0.0
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
338  */
339 template <typename T>
340 int32_t Sign( T value )
341 {
342   return ( T(0) < value ) - ( value < T(0) );
343 }
344
345 /**
346  * @}
347  */
348 } // namespace Dali
349
350 #endif // __DALI_MATH_UTILS_H__