#ifndef Magnum_Math_Functions_h #define Magnum_Math_Functions_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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 /* std::div() */ #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 IsBoolVectorOrScalar: std::false_type {}; template<> struct IsBoolVectorOrScalar: std::true_type {}; template struct IsBoolVectorOrScalar>: 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 inline 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 inline T exp(T exponent) { return std::exp(exponent); } /** @brief Integer division with remainder Example usage: @snippet MagnumMath.cpp div Equivalent to the following, but possibly done in a single CPU instruction: @snippet MagnumMath.cpp div-equivalent */ template inline std::pair div(Integral x, Integral y) { static_assert(IsIntegral::value && IsScalar::value, "scalar integral type expected"); const auto result = std::div(x, y); return {result.quot, result.rem}; } /** @brief If given number is a positive or negative infinity @see @ref isNan(), @ref Constants::inf() */ template inline typename std::enable_if::value, bool>::type isInf(T value) { return std::isinf(UnderlyingTypeOf(value)); } /** @overload */ template inline BoolVector isInf(const Vector& value) { BoolVector out; for(std::size_t i = 0; i != size; ++i) out.set(i, Math::isInf(value[i])); return out; } /** @brief If given number is a NaN Equivalent to @cpp value != value @ce. @see @ref isInf(), @ref Constants::nan() */ template inline typename std::enable_if::value, bool>::type isNan(T value) { return std::isnan(UnderlyingTypeOf(value)); } /** @overload */ template inline BoolVector isNan(const Vector& value) { BoolVector out; for(std::size_t i = 0; i != size; ++i) out.set(i, Math::isNan(value[i])); return out; } /** @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, including @ref Deg and @ref Rad. 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. Works only on types that satisfy @ref IsUnitless. @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) { static_assert(IsUnitless::value, "expected an unitless type"); return Implementation::Pow::pow(base); } template inline Vector pow(const Vector& base) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::pow(base[i]); return out; } #endif /** @brief Power Returns power of @p base to the @p exponent. Works only on types that satisfy @ref IsUnitless. @see @ref pow(T), @ref exp() */ #ifdef DOXYGEN_GENERATING_OUTPUT template T pow(T base, T exponent); #else template inline typename std::enable_if::value, T>::type pow(T base, T exponent) { static_assert(IsUnitless::value, "expected an unitless type"); 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] = Math::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 min(Corrade::Containers::ArrayView), @ref Vector::min() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T min(T value, T min); #else /* min() for scalars defined in Vector.h */ template inline Vector min(const Vector& value, const Vector& min) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::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] = Math::min(value[i], min); return out; } /** @brief Maximum NaNs passed in @p value parameter are propagated. @see @ref min(), @ref minmax(), @ref clamp(), @ref max(Corrade::Containers::ArrayView), @ref Vector::max() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T max(T value, T max); #else /* max() for scalars defined in Vector.h */ template Vector max(const Vector& value, const Vector& max) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::max(value[i], max[i]); return out; } #endif /** @overload */ template inline Vector max(const Vector& value, T max) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::max(value[i], max); return out; } /** @brief Minimum and maximum of two values @see @ref min(), @ref max(), @ref clamp(), @ref minmax(Corrade::Containers::ArrayView), @ref Vector::minmax(), @ref Range::Range(const std::pair&) */ #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 inline 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: @snippet MagnumMath.cpp clamp 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 Math::min(Math::max(value, min), max); } template inline 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] = Math::clamp(value[i], min[i], max[i]); return out; } #endif /** @overload */ template inline Vector clamp(const Vector& value, T min, T max) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::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 inline Vector sign(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::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 T(std::abs(UnderlyingTypeOf(a))); } template inline Vector abs(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::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 T(std::floor(UnderlyingTypeOf(a))); } template inline Vector floor(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::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) { return T(std::round(UnderlyingTypeOf(a))); } template inline Vector round(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::round(a[i]); 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 T(std::ceil(UnderlyingTypeOf(a))); } template inline Vector ceil(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::ceil(a[i]); return out; } #endif /** @brief Square root Works only on types that satisfy @ref IsUnitless. @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) { static_assert(IsUnitless::value, "expecting an unitless type"); return std::sqrt(a); } template inline Vector sqrt(const Vector& a) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::sqrt(a[i]); return out; } #endif /** @brief Inverse square root Works only on types that satisfy @ref IsUnitless. @see @ref sqrt(), @ref Vector::lengthInverted() @m_keyword{inversesqrt(),GLSL inversesqrt(),} */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T sqrtInverted(const T& a); #else template inline typename std::enable_if::value, T>::type sqrtInverted(T a) { static_assert(IsUnitless::value, "expecting an unitless type"); return T(1)/std::sqrt(a); } template inline Vector sqrtInverted(const Vector& a) { return Vector(T(1))/Math::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 select() for constant interpolation using the same API and @ref splerp() for spline interpolation. @see @ref lerpInverted(), @ref lerp(const Complex&, const Complex&, T), @ref lerp(const Quaternion&, const Quaternion&, T), @ref lerp(const CubicHermite&, const CubicHermite&, U), @ref lerp(const CubicHermiteComplex&, const CubicHermiteComplex&, T), @ref lerp(const CubicHermiteQuaternion&, const CubicHermiteQuaternion&, T) @m_keyword{mix(),GLSL mix(),} */ template inline #ifndef DOXYGEN_GENERATING_OUTPUT typename std::enable_if<(IsVector::value || IsScalar::value) && !Implementation::IsBoolVectorOrScalar::value, T>::type #else T #endif lerp(const T& a, const T& b, U t) { return Implementation::lerp(a, b, t); } /** @overload @m_keyword{mix(),GLSL mix(),} */ template inline T lerp(const T& a, const T& b, bool t) { return t ? b : a; } /** @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. @m_keyword{mix(),GLSL mix(),} */ 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 @m_keyword{mix(),GLSL mix(),} */ 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(), @ref select() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T lerpInverted(const T& a, const T& b, const T& lerp); #else template inline UnderlyingTypeOf::value, T>::type> 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 Constant interpolation of two values @param a First value @param b Second value @param t Interpolation phase A constant interpolation counterpart to @ref lerp(): @f[ \boldsymbol{v}_i = \begin{cases} \boldsymbol{v_A}_i, & t_i < 1 \\ \boldsymbol{v_B}_i, & t_i \ge 1 \end{cases} @f] Equivalent to calling @cpp Math::lerp(a, b, t >= U(1)) @ce. */ template constexpr T select(const T& a, const T& b, U t) { return lerp(a, b, t >= U(1)); } /** @brief Fused multiply-add Computes and returns @f$ ab + c @f$. On some architectures might be faster than doing the computation manually. Works only on types that satisfy @ref IsUnitless. */ #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) { static_assert(IsUnitless::value, "expecting an unitless type"); /* 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. */ #ifndef 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) { static_assert(IsUnitless::value, "expecting an unitless type"); return a*b + c; } #endif /*@}*/ }} #ifdef MAGNUM_BUILD_DEPRECATED #include "Magnum/Math/FunctionsBatch.h" /** @todo remove once compat is dropped */ #endif #endif