#ifndef Magnum_Math_Functions_h #define Magnum_Math_Functions_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file * @brief Functions usable with scalar and vector types */ #include #include #include #include #include "Magnum/visibility.h" #include "Magnum/Math/Vector.h" namespace Magnum { namespace Math { namespace Implementation { template struct Pow { Pow() = delete; template constexpr static T pow(T base) { return base*Pow::pow(base); } }; template<> struct Pow<0> { Pow() = delete; template constexpr static T pow(T) { return T(1); } }; template struct IsBoolVector: std::false_type {}; template struct IsBoolVector>: std::true_type {}; } /** @brief Integral logarithm Returns integral logarithm of given number with given base. @see @ref log2(), @ref log(T) */ UnsignedInt MAGNUM_EXPORT log(UnsignedInt base, UnsignedInt number); /** @brief Base-2 integral logarithm Returns integral logarithm of given number with base `2`. @see @ref log(UnsignedInt, UnsignedInt), @ref log(T) */ UnsignedInt MAGNUM_EXPORT log2(UnsignedInt number); /** @brief Natural logarithm Returns natural (base @f$ e @f$) logarithm of given number. @see @ref Constants::e(), @ref log(UnsignedInt, UnsignedInt), @ref log2() */ template T log(T number) { return std::log(number); } /** @brief Natural exponential Returns @f$ e^x @f$. @see @ref Constants::e(), @ref pow(T, T) */ template T exp(T exponent) { return std::exp(exponent); } /** @brief Integer division with remainder Example usage: @code Int quotient, remainder; std::tie(quotient, remainder) = Math::div(57, 6); // {9, 3} @endcode Equivalent to the following, but possibly done in a single CPU instruction: @code Int quotient = 57/6; Int remainder = 57%6; @endcode */ template std::pair div(Integral x, Integral y) { static_assert(std::is_integral{}, "Math::div(): not an integral type"); const auto result = std::div(x, y); return {result.quot, result.rem}; } /** @todo Can't trigonometric functions be done with only one overload? */ /* The functions accept Unit instead of Rad to make them working with operator products (e.g. 2*35.0_degf, which is of type Unit) */ /** @brief Sine @see @ref sincos() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T sin(Rad angle); #else template inline T sin(Unit angle) { return std::sin(T(angle)); } template inline T sin(Unit angle) { return sin(Rad(angle)); } #endif /** @brief Cosine @see @ref sincos() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T cos(Rad angle); #else template inline T cos(Unit angle) { return std::cos(T(angle)); } template inline T cos(Unit angle) { return cos(Rad(angle)); } #endif /** @brief Sine and cosine On some architectures might be faster than doing both computations separately. @see @ref sin(), @ref cos(), @ref sincos(const Dual>&) */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline std::pair sincos(Rad angle); #else template inline std::pair sincos(Unit angle) { return {std::sin(T(angle)) ,std::cos(T(angle))}; } template inline std::pair sincos(Unit angle) { return sincos(Rad(angle)); } #endif /** @brief Tangent */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T tan(Rad angle); #else template inline T tan(Unit angle) { return std::tan(T(angle)); } template inline T tan(Unit angle) { return tan(Rad(angle)); } #endif /** @brief Arc sine */ template inline Rad asin(T value) { return Rad(std::asin(value)); } /** @brief Arc cosine */ template inline Rad acos(T value) { return Rad(std::acos(value)); } /** @brief Arc tangent */ template inline Rad atan(T value) { return Rad(std::atan(value)); } /** @{ @name Scalar/vector functions These functions are overloaded for both scalar and vector types. Scalar versions function exactly as their possible STL equivalents, vector overloads perform the operations component-wise. */ /** @brief Integral power Returns integral power of base to the exponent. @see @ref pow(T, T) */ #ifdef DOXYGEN_GENERATING_OUTPUT template constexpr T pow(T base); #else template constexpr typename std::enable_if::value, T>::type pow(T base) { return Implementation::Pow::pow(base); } template Vector pow(const Vector& base) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Implementation::Pow::pow(base[i]); return out; } #endif /** @brief Power Returns power of @p base to the @p exponent. @see @ref pow(T), @ref exp() */ #ifdef DOXYGEN_GENERATING_OUTPUT template T pow(T base, T exponent); #else template typename std::enable_if::value, T>::type pow(T base, T exponent) { return std::pow(base, exponent); } template inline Vector pow(const Vector& base, T exponent) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = std::pow(base[i], exponent); return out; } #endif /** @brief Minimum NaNs passed in @p value parameter are propagated. @see @ref max(), @ref minmax(), @ref clamp(), @ref Vector::min() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T min(T value, T min); #else template inline typename std::enable_if::value, T>::type min(T value, T min) { return std::min(value, min); } template inline Vector min(const Vector& value, const Vector& min) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = std::min(value[i], min[i]); return out; } #endif /** @overload */ template inline Vector min(const Vector& value, T min) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = std::min(value[i], min); return out; } /** @brief Minimum of a range */ template T min(Corrade::Containers::ArrayView range) { T out(range[0]); for(std::size_t i = 1; i != range.size(); ++i) out = min(out, range[i]); return out; } /** @overload */ template inline T min(std::initializer_list list) { return min(Corrade::Containers::ArrayView{list.begin(), list.size()}); } /** @brief Maximum NaNs passed in @p value parameter are propagated. @see @ref min(), @ref minmax(), @ref clamp(), @ref Vector::max() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T max(T value, T max); #else template inline typename std::enable_if::value, T>::type max(T value, T max) { return std::max(value, max); } template Vector max(const Vector& value, const Vector& max) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = std::max(value[i], max[i]); return out; } #endif /** @overload */ template Vector max(const Vector& value, T max) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = std::max(value[i], max); return out; } /** @brief Maximum of a range */ template T max(Corrade::Containers::ArrayView range) { T out(range[0]); for(std::size_t i = 1; i != range.size(); ++i) out = max(out, range[i]); return out; } /** @overload */ template inline T max(std::initializer_list list) { return max(Corrade::Containers::ArrayView{list.begin(), list.size()}); } /** @brief Minimum and maximum of two values @see @ref min(), @ref max(), @ref clamp(), @ref Vector2::minmax() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline std::pair minmax(const T& a, const T& b); #else template inline typename std::enable_if::value, std::pair>::type minmax(T a, T b) { return a < b ? std::make_pair(a, b) : std::make_pair(b, a); } template std::pair, Vector> minmax(const Vector& a, const Vector& b) { using std::swap; std::pair, Vector> out{a, b}; for(std::size_t i = 0; i != size; ++i) if(out.first[i] > out.second[i]) swap(out.first[i], out.second[i]); return out; } #endif /** @brief Clamp value Values smaller than @p min are set to @p min, values larger than @p max are set to @p max. Equivalent to: @code Math::min(Math::max(value, min), max) @endcode NaNs passed in @p value parameter are propagated. @see @ref min(), @ref max() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T clamp(const T& value, const T& min, const T& max); #else template inline typename std::enable_if::value, T>::type clamp(T value, T min, T max) { return std::min(std::max(value, min), max); } template Vector clamp(const Vector& value, const Vector& min, const Vector& max) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = clamp(value[i], min[i], max[i]); return out; } #endif /** @overload */ template Vector clamp(const Vector& value, T min, T max) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = clamp(value[i], min, max); return out; } /** @brief Sign Returns `1` if @p x > 0, `0` if @p x = 0 and `-1` if @p x < 0. */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T sign(const T scalar); #else template inline typename std::enable_if::value, T>::type sign(const T& scalar) { if(scalar > T(0)) return T(1); if(scalar < T(0)) return T(-1); return T(0); } template Vector sign(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = sign(a[i]); return out; } #endif /** @brief Absolute value */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T abs(const T& a); #else template inline typename std::enable_if::value, T>::type abs(T a) { return std::abs(a); } template Vector abs(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = std::abs(a[i]); return out; } #endif /** @brief Nearest not larger integer */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T floor(const T& a); #else template inline typename std::enable_if::value, T>::type floor(T a) { return std::floor(a); } template Vector floor(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = std::floor(a[i]); return out; } #endif /** @brief Round value to nearest integer */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T round(const T& a); #else template inline typename std::enable_if::value, T>::type round(T a) { /** @todo Remove when newlib has this fixed */ #if !defined(CORRADE_TARGET_NACL_NEWLIB) && !defined(CORRADE_TARGET_ANDROID) return std::round(a); #else return (a > T(0)) ? std::floor(a + T(0.5)) : std::ceil(a - T(0.5)); #endif } template Vector round(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) { #if !defined(CORRADE_TARGET_NACL_NEWLIB) && !defined(CORRADE_TARGET_ANDROID) out[i] = std::round(a[i]); #else out[i] = round(a[i]); #endif } return out; } #endif /** @brief Nearest not smaller integer */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T ceil(const T& a); #else template inline typename std::enable_if::value, T>::type ceil(T a) { return std::ceil(a); } template Vector ceil(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = std::ceil(a[i]); return out; } #endif /** @brief Square root @see @ref sqrtInverted(), @ref Vector::length(), @ref sqrt(const Dual&) */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T sqrt(const T& a); #else template inline typename std::enable_if::value, T>::type sqrt(T a) { return T(std::sqrt(a)); } template Vector sqrt(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = T(std::sqrt(a[i])); return out; } #endif /** @brief Inverse square root @see @ref sqrt(), @ref Vector::lengthInverted() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T sqrtInverted(const T& a); #else template inline typename std::enable_if::value, T>::type sqrtInverted(T a) { return T(1)/std::sqrt(a); } template Vector sqrtInverted(const Vector& a) { return Vector(T(1))/sqrt(a); } #endif /** @brief Linear interpolation of two values @param a First value @param b Second value @param t Interpolation phase (from range @f$ [0; 1] @f$) The interpolation for vectors is done as in following, similarly for scalars: @f[ \boldsymbol v_{LERP} = (1 - t) \boldsymbol v_A + t \boldsymbol v_B @f] @see @ref lerpInverted(), @ref lerp(const Quaternion&, const Quaternion&, T) */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T lerp(const T& a, const T& b, U t); #else template inline typename std::enable_if::value, T>::type lerp(T a, T b, U t) { return T(Implementation::lerp(a, b, t)); } template inline typename std::enable_if::value, Vector>::type lerp(const Vector& a, const Vector& b, U t) { return Implementation::lerp(a, b, t); } #endif /** @overload Similar to the above, but instead of multiplication and addition it just does component-wise selection from either @p a or @p b based on values in @p t. */ template inline Vector lerp(const Vector& a, const Vector& b, const BoolVector& t) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = t[i] ? b[i] : a[i]; return out; } /** @overload */ template inline BoolVector lerp(const BoolVector& a, const BoolVector& b, const BoolVector& t) { /* Not using NoInit because it causes some compilers to report unitialized value */ BoolVector out; for(std::size_t i = 0; i != size; ++i) out.set(i, t[i] ? b[i] : a[i]); return out; } /** @brief Inverse linear interpolation of two values @param a First value @param b Second value @param lerp Interpolated value Returns interpolation phase *t*: @f[ t = \frac{\boldsymbol v_{LERP} - \boldsymbol v_A}{\boldsymbol v_B - \boldsymbol v_A} @f] @see @ref lerp() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T lerpInverted(const T& a, const T& b, const T& lerp); #else template inline T lerpInverted(T a, T b, T lerp) { return (lerp - a)/(b - a); } template inline Vector lerpInverted(const Vector& a, const Vector& b, const Vector& lerp) { return (lerp - a)/(b - a); } #endif /** @brief Fused multiply-add Computes and returns @f$ ab + c @f$. On some architectures might be faster than doing the computation manually. */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T fma(const T& a, const T& b, const T& c); #else template inline typename std::enable_if::value, T>::type fma(T a, T b, T c) { /** @todo Remove when newlib has this fixed */ /* On Emscripten it works with -O2 but not with -O1 (function not defined). I guess that's only because -O2 optimizes it out, so disabling it there also */ #if !defined(CORRADE_TARGET_NACL_NEWLIB) && !defined(CORRADE_TARGET_ANDROID) && !defined(CORRADE_TARGET_EMSCRIPTEN) return std::fma(a, b, c); #else return a*b + c; #endif } template inline Vector fma(const Vector& a, const Vector& b, const Vector& c) { return a*b + c; } #endif /*@}*/ }} #ifdef MAGNUM_BUILD_DEPRECATED /* In order to make the deprecated normalize() / denormalize() functions available in the original header. The Packing.h header depends on this file so it needs to be included after it. */ #include "Magnum/Math/Packing.h" #endif #endif