1 #ifndef __DALI_MATH_UTILS_H__
2 #define __DALI_MATH_UTILS_H__
5 * Copyright (c) 2014 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 <algorithm> // std::min & max
25 #include <dali/public-api/common/dali-common.h>
31 * @brief Returns the next power of two.
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
38 DALI_IMPORT_API unsigned int NextPowerOfTwo( unsigned int i );
41 * @brief Whether a number is power of two.
43 * @param[in] i input number
44 * @return true if i is power of two
46 DALI_IMPORT_API bool IsPowerOfTwo( unsigned int i );
49 * @brief Clamp a value.
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
56 template< typename T >
57 const T& Clamp( const T& value, const T& min, const T& max )
59 return std::max( std::min( value, max ), min );
63 * @brief Clamp a value directly.
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.
69 template< typename T >
70 void ClampInPlace( T& value, const T& min, const T& max )
72 value = std::max( std::min( value, max ), min );
77 * @brief Linear interpolation between two values.
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.
85 template< typename T >
86 inline const T Lerp( const float offset, const T& low, const T& high )
88 return low + ((high - low) * Clamp(offset, 0.0f, 1.0f));
92 * @brief Get an epsilon that is valid for the given range.
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
98 DALI_IMPORT_API float GetRangedEpsilon(float a, float b);
101 * @brief Helper function to compare equality of a floating point value with zero.
103 * @param[in] value the value to compare
104 * @return true if the value is equal to zero
106 DALI_IMPORT_API bool EqualsZero( float value );
109 * @brief Helper function to compare equality of two floating point values.
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
115 DALI_IMPORT_API bool Equals( float a, float b );
118 * @brief Helper function to compare equality of two floating point values.
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
125 DALI_IMPORT_API bool Equals( float a, float b, float epsilon );
128 * @brief Get an float that is rounded at specified place of decimals.
130 * @param[in] value float value
131 * @param[in] pos decimal place
132 * @return a rounded float
134 DALI_IMPORT_API float Round( float value, int pos );
137 * @brief Wrap x in domain (start) to (end).
139 * This works like a floating point version
140 * of the % modulo operation. But with an offset (start).
142 * For instance a domain is specified as:
147 * (\ / start) (\ / end)
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
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.
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
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.
166 * @return the wrapped value over the domain (start) (end)
168 DALI_IMPORT_API float WrapInDomain(float x, float start, float end);
171 * @brief Find the shortest distance (magnitude) and direction (sign)
172 * from (a) to (b) in domain (start) to (end).
174 * (\ / start) (\ / end)
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).
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).
189 * @note assumes both (a) and (b) are already within the domain
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)
198 DALI_IMPORT_API float ShortestDistanceInDomain(float a, float b, float start, float end);
202 #endif // __DALI_MATH_UTILS_H__