Remove unnecessary exports
[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) 2014 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 <algorithm> // std::min & max
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26
27 namespace Dali
28 {
29
30 /**
31  * @brief Returns the next power of two.
32  *
33  * In case of numbers which are already a power of two this function returns the original number.
34  * If i is zero returns 1
35  * @param[in] i input number
36  * @return    next power of two or i itself in case it's a power of two
37  */
38 DALI_IMPORT_API unsigned int NextPowerOfTwo( unsigned int i );
39
40 /**
41  * @brief Whether a number is power of two.
42  *
43  * @param[in] i input number
44  * @return    true if i is power of two
45  */
46 DALI_IMPORT_API bool IsPowerOfTwo( unsigned int i );
47
48 /**
49  * @brief Clamp a value.
50  *
51  * @param[in] value The value to clamp.
52  * @param[in] min The minimum allowed value.
53  * @param[in] max The maximum allowed value.
54  * @return T the clamped value
55  */
56 template< typename T >
57 const T& Clamp( const T& value, const T& min, const T& max )
58 {
59   return std::max( std::min( value, max ), min );
60 }
61
62 /**
63  * @brief Clamp a value directly.
64  *
65  * @param[in,out] value The value that will be clamped.
66  * @param[in] min The minimum allowed value.
67  * @param[in] max The maximum allowed value.
68  */
69 template< typename T >
70 void ClampInPlace( T& value, const T& min, const T& max )
71 {
72   value =  std::max( std::min( value, max ), min );
73 }
74
75
76 /**
77  * @brief Linear interpolation between two values.
78  *
79  * @param[in] offset The offset through the range @p low to @p high.
80  *                   This value is clamped between 0 and 1
81  * @param[in] low    Lowest value in range
82  * @param[in] high   Highest value in range
83  * @return A value between low and high.
84  */
85 template< typename T >
86 inline const T Lerp( const float offset, const T& low, const T& high )
87 {
88   return low + ((high - low) * Clamp(offset, 0.0f, 1.0f));
89 }
90
91 /**
92  * @brief Get an epsilon that is valid for the given range.
93  *
94  * @param[in] a the first value in the range
95  * @param[in] b the second value in the range.
96  * @return a suitable epsilon
97  */
98 DALI_IMPORT_API float GetRangedEpsilon(float a, float b);
99
100 /**
101  * @brief Helper function to compare equality of a floating point value with zero.
102  *
103  * @param[in] value the value to compare
104  * @return true if the value is equal to zero
105  */
106 DALI_IMPORT_API bool EqualsZero( float value );
107
108 /**
109  * @brief Helper function to compare equality of two floating point values.
110  *
111  * @param[in] a the first value to compare
112  * @param[in] b the second value to compare
113  * @return true if the values are equal within a minimal epsilon for their values
114  */
115 DALI_IMPORT_API bool Equals( float a, float b );
116
117 /**
118  * @brief Helper function to compare equality of two floating point values.
119  *
120  * @param[in] a the first value to compare
121  * @param[in] b the second value to compare
122  * @param[in] epsilon the minimum epsilon value that will be used to consider the values different
123  * @return true if the difference between the values is less than the epsilon
124  */
125 DALI_IMPORT_API bool Equals( float a, float b, float epsilon );
126
127 /**
128  * @brief Get an float that is rounded at specified place of decimals.
129  *
130  * @param[in] value float value
131  * @param[in] pos decimal place
132  * @return a rounded float
133  */
134 DALI_IMPORT_API float Round( float value, int pos );
135
136 /**
137  * @brief Wrap x in domain (start) to (end).
138  *
139  * This works like a floating point version
140  * of the % modulo operation. But with an offset (start).
141  *
142  * For instance a domain is specified as:
143  * start: 2
144  * end: 8
145  *
146  *   2                         8
147  * (\ / start)               (\ / end)
148  *   |----x                    |
149  *
150  * The value x will be confined to this domain.
151  * If x is below 2 e.g. 0, then it is wraped to 6.
152  * If x is above or equal to 8 e.g. 8.1 then it is
153  * wrapped to 2.1
154  *
155  * Domain wrapping is useful for various problems from
156  * calculating positions in a space that repeats, to
157  * computing angles that range from 0 to 360.
158  *
159  * @param[in] x the point to be wrapped within the domain
160  * @param[in] start The start of the domain
161  * @param[in] end The end of the domain
162  *
163  * @note if start = end (i.e. size of domain 0), then wrapping will not occur
164  * and result will always be equal to start.
165  *
166  * @return the wrapped value over the domain (start) (end)
167  */
168 DALI_IMPORT_API float WrapInDomain(float x, float start, float end);
169
170 /**
171  * @brief Find the shortest distance (magnitude) and direction (sign)
172  * from (a) to (b) in domain (start) to (end).
173  *
174  * (\ / start)               (\ / end)
175  *   |-a                 b<----|
176  *
177  * Knowing the shortest distance is useful with wrapped domains
178  * to solve problems such as determing the closest object to
179  * a given point, or determing whether turning left or turning
180  * right is the shortest route to get from angle 10 degrees
181  * to angle 350 degrees (clearly in a 0-360 degree domain, turning
182  * left 20 degrees is quicker than turning right 340 degrees).
183  *
184  * The value returned holds the distance and the direction from
185  * value a to value b. For instance in the above example it would
186  * return -20. i.e. subtract 20 from current value (10) to reach
187  * target wrapped value (350).
188  *
189  * @note assumes both (a) and (b) are already within the domain
190  * (start) to (end)
191  *
192  * @param a the current value
193  * @param b the target value
194  * @param start the start of the domain
195  * @param end the end of the domain
196  * @return the shortest direction (the sign) and distance (the magnitude)
197  */
198 DALI_IMPORT_API float ShortestDistanceInDomain(float a, float b, float start, float end);
199
200 } // namespace Dali
201
202 #endif // __DALI_MATH_UTILS_H__