Merge "Added RenderTask WorldToViewport coordinates" into devel/master
[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) 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 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/common/constants.h>
24
25 namespace Dali
26 {
27 /**
28  * @addtogroup dali_core_math
29  * @{
30  */
31
32 /**
33  * @brief Returns the next power of two.
34  *
35  * In case of numbers which are already a power of two this function returns the original number.
36  * If i is zero returns 1
37  * @param[in] i input number
38  * @return    next power of two or i itself in case it's a power of two
39  */
40 inline unsigned int NextPowerOfTwo( unsigned int i )
41 {
42   DALI_ASSERT_ALWAYS(i <= 1u << (sizeof(unsigned) * 8 - 1) && "Return type cannot represent the next power of two greater than the argument.");
43   if(i==0u)
44   {
45     return 1u;
46   }
47
48   i--;
49   i |= i >> 1;
50   i |= i >> 2;
51   i |= i >> 4;
52   i |= i >> 8;
53   i |= i >> 16;
54   i++;
55   return i;
56 }
57
58 /**
59  * @brief Whether a number is power of two.
60  *
61  * @param[in] i input number
62  * @return    true if i is power of two
63  */
64 inline bool IsPowerOfTwo( unsigned int i )
65 {
66   return (i != 0u) && ((i & (i - 1u)) == 0u);
67 }
68
69 /**
70  * @brief Clamp a value.
71  *
72  * @param[in] value The value to clamp.
73  * @param[in] min The minimum allowed value.
74  * @param[in] max The maximum allowed value.
75  * @return T the clamped value
76  */
77 template< typename T >
78 inline const T& Clamp( const T& value, const T& min, const T& max )
79 {
80   const T& constrainedUpper = value < max ? value : max;
81   const T& constrainedUpperAndLower = constrainedUpper > min ? constrainedUpper : min;
82   return  constrainedUpperAndLower;
83 }
84
85 /**
86  * @brief Clamp a value directly.
87  *
88  * @param[in,out] value The value that will be clamped.
89  * @param[in] min The minimum allowed value.
90  * @param[in] max The maximum allowed value.
91  */
92 template< typename T >
93 inline void ClampInPlace( T& value, const T& min, const T& max )
94 {
95   const T& constrainedUpper = value < max ? value : max;
96   const T& constrainedUpperAndLower = constrainedUpper > min ? constrainedUpper : min;
97   value = constrainedUpperAndLower;
98 }
99
100
101 /**
102  * @brief Linear interpolation between two values.
103  *
104  * @param[in] offset The offset through the range @p low to @p high.
105  *                   This value is clamped between 0 and 1
106  * @param[in] low    Lowest value in range
107  * @param[in] high   Highest value in range
108  * @return A value between low and high.
109  */
110 template< typename T >
111 inline const T Lerp( const float offset, const T& low, const T& high )
112 {
113   return low + ((high - low) * Clamp(offset, 0.0f, 1.0f));
114 }
115
116 /**
117  * @brief Get an epsilon that is valid for the given range.
118  *
119  * @param[in] a the first value in the range
120  * @param[in] b the second value in the range.
121  * @return a suitable epsilon
122  */
123 inline float GetRangedEpsilon( float a, float b )
124 {
125   const float absA = fabsf( a );
126   const float absB = fabsf( b );
127   const float absF = absA > absB ? absA : absB;
128   const int absI = absF;
129
130   float epsilon = Math::MACHINE_EPSILON_10000;
131   if (absF < 0.1f)
132   {
133     return Math::MACHINE_EPSILON_0;
134   }
135   else if (absI < 2)
136   {
137     return Math::MACHINE_EPSILON_1;
138   }
139   else if (absI < 20)
140   {
141     return Math::MACHINE_EPSILON_10;
142   }
143   else if (absI < 200)
144   {
145     return Math::MACHINE_EPSILON_100;
146   }
147   else if (absI < 2000)
148   {
149     return Math::MACHINE_EPSILON_1000;
150   }
151   return epsilon;
152 }
153
154 /**
155  * @brief Helper function to compare equality of a floating point value with zero.
156  *
157  * @param[in] value the value to compare
158  * @return true if the value is equal to zero
159  */
160 #pragma GCC diagnostic push
161 #pragma GCC diagnostic ignored "-Wfloat-equal"
162 inline bool EqualsZero( float value )
163 {
164   return value == 0.0f;
165 }
166 #pragma GCC diagnostic pop
167
168 /**
169  * @brief Helper function to compare equality of two floating point values.
170  *
171  * @param[in] a the first value to compare
172  * @param[in] b the second value to compare
173  * @return true if the values are equal within a minimal epsilon for their values
174  */
175 inline bool Equals( float a, float b )
176 {
177   return ( fabsf( a - b ) <= GetRangedEpsilon( a, b ) );
178 }
179
180 /**
181  * @brief Helper function to compare equality of two floating point values.
182  *
183  * @param[in] a the first value to compare
184  * @param[in] b the second value to compare
185  * @param[in] epsilon the minimum epsilon value that will be used to consider the values different
186  * @return true if the difference between the values is less than the epsilon
187  */
188 inline bool Equals( float a, float b, float epsilon )
189 {
190   return ( fabsf( a - b ) <= epsilon );
191 }
192
193 /**
194  * @brief Get an float that is rounded at specified place of decimals.
195  *
196  * @param[in] value float value
197  * @param[in] pos decimal place
198  * @return a rounded float
199  */
200 inline float Round(float value, int pos)
201 {
202   float temp;
203   temp = value * powf( 10, pos );
204   temp = floorf( temp + 0.5 );
205   temp *= powf( 10, -pos );
206   return temp;
207 }
208
209 /**
210  * @brief Wrap x in domain (start) to (end).
211  *
212  * This works like a floating point version
213  * of the % modulo operation. But with an offset (start).
214  *
215  * For instance a domain is specified as:
216  * start: 2
217  * end: 8
218  *
219  *   2                         8
220  * (\ / start)               (\ / end)
221  *   |----x                    |
222  *
223  * The value x will be confined to this domain.
224  * If x is below 2 e.g. 0, then it is wraped to 6.
225  * If x is above or equal to 8 e.g. 8.1 then it is
226  * wrapped to 2.1
227  *
228  * Domain wrapping is useful for various problems from
229  * calculating positions in a space that repeats, to
230  * computing angles that range from 0 to 360.
231  *
232  * @param[in] x the point to be wrapped within the domain
233  * @param[in] start The start of the domain
234  * @param[in] end The end of the domain
235  *
236  * @note if start = end (i.e. size of domain 0), then wrapping will not occur
237  * and result will always be equal to start.
238  *
239  * @return the wrapped value over the domain (start) (end)
240  */
241 inline float WrapInDomain(float x, float start, float end)
242 {
243   float domain = end - start;
244   x -= start;
245
246   if(fabsf(domain) > Math::MACHINE_EPSILON_1)
247   {
248     return start + (x - floorf(x / domain) * domain);
249   }
250
251   return start;
252 }
253
254
255 /**
256  * @brief Find the shortest distance (magnitude) and direction (sign)
257  * from (a) to (b) in domain (start) to (end).
258  *
259  * (\ / start)               (\ / end)
260  *   |-a                 b<----|
261  *
262  * Knowing the shortest distance is useful with wrapped domains
263  * to solve problems such as determing the closest object to
264  * a given point, or determing whether turning left or turning
265  * right is the shortest route to get from angle 10 degrees
266  * to angle 350 degrees (clearly in a 0-360 degree domain, turning
267  * left 20 degrees is quicker than turning right 340 degrees).
268  *
269  * The value returned holds the distance and the direction from
270  * value a to value b. For instance in the above example it would
271  * return -20. i.e. subtract 20 from current value (10) to reach
272  * target wrapped value (350).
273  *
274  * @note assumes both (a) and (b) are already within the domain
275  * (start) to (end)
276  *
277  * @param a the current value
278  * @param b the target value
279  * @param start the start of the domain
280  * @param end the end of the domain
281  * @return the shortest direction (the sign) and distance (the magnitude)
282  */
283 inline float ShortestDistanceInDomain( float a, float b, float start, float end )
284 {
285   //  (a-start + end-b)
286   float size = end-start;
287   float vect = b-a;
288
289   if(vect > 0)
290   {
291     // +ve vector, let's try perspective 1 domain to the right,
292     // and see if closer.
293     float aRight = a+size;
294     if( aRight-b < vect )
295     {
296       return b-aRight;
297     }
298   }
299   else
300   {
301     // -ve vector, let's try perspective 1 domain to the left,
302     // and see if closer.
303     float aLeft = a-size;
304     if( aLeft-b > vect )
305     {
306       return b-aLeft;
307     }
308   }
309
310   return vect;
311 }
312
313 /**
314  * @brief Extracts the sign of a number
315  *
316  * @param[in] value The value we want to extract the sign
317  * @return -1 for negative values, +1 for positive values and 0 if value is 0
318  */
319 template <typename T>
320 int Sign( T value )
321 {
322   return ( T(0) < value ) - ( value < T(0) );
323 }
324
325 /**
326  * @}
327  */
328 } // namespace Dali
329
330 #endif // __DALI_MATH_UTILS_H__