mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.9 KiB
129 lines
3.9 KiB
|
14 years ago
|
#ifndef Magnum_Math_Functions_h
|
||
|
|
#define Magnum_Math_Functions_h
|
||
|
16 years ago
|
/*
|
||
|
15 years ago
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
16 years ago
|
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
|
Magnum is free software: you can redistribute it and/or modify
|
||
|
|
it under the terms of the GNU Lesser General Public License version 3
|
||
|
|
only, as published by the Free Software Foundation.
|
||
|
|
|
||
|
|
Magnum is distributed in the hope that it will be useful,
|
||
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
|
GNU Lesser General Public License version 3 for more details.
|
||
|
|
*/
|
||
|
|
|
||
|
14 years ago
|
#include <cmath>
|
||
|
14 years ago
|
#include <type_traits>
|
||
|
|
#include <limits>
|
||
|
14 years ago
|
#include <algorithm>
|
||
|
15 years ago
|
|
||
|
14 years ago
|
#include "magnumVisibility.h"
|
||
|
14 years ago
|
|
||
|
16 years ago
|
/** @file
|
||
|
14 years ago
|
* @brief Functions usable with scalar and vector types
|
||
|
16 years ago
|
*/
|
||
|
|
|
||
|
14 years ago
|
namespace Magnum { namespace Math {
|
||
|
15 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
@todo Vector/scalar overloaded functions (...SIMD):
|
||
|
|
- min(), max() (component-wise)
|
||
|
|
- abs()
|
||
|
|
- sin(), cos()...
|
||
|
|
- clamp(), lerp()
|
||
|
|
*/
|
||
|
|
|
||
|
14 years ago
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
||
|
14 years ago
|
namespace Implementation {
|
||
|
14 years ago
|
template<std::uint32_t exponent> struct Pow {
|
||
|
14 years ago
|
template<class T> inline constexpr T operator()(T base) const {
|
||
|
|
return base*Pow<exponent-1>()(base);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
template<> struct Pow<0> {
|
||
|
14 years ago
|
template<class T> inline constexpr T operator()(T) const { return 1; }
|
||
|
14 years ago
|
};
|
||
|
|
}
|
||
|
14 years ago
|
#endif
|
||
|
16 years ago
|
|
||
|
15 years ago
|
/**
|
||
|
|
* @brief Integral power
|
||
|
|
*
|
||
|
|
* Returns integral power of base to the exponent.
|
||
|
|
*/
|
||
|
14 years ago
|
template<std::uint32_t exponent, class T> inline constexpr T pow(T base) {
|
||
|
14 years ago
|
return Implementation::Pow<exponent>()(base);
|
||
|
15 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Base-2 integral logarithm
|
||
|
|
*
|
||
|
|
* Returns integral logarithm of given number with base `2`.
|
||
|
|
* @see log()
|
||
|
|
*/
|
||
|
|
std::uint32_t MAGNUM_EXPORT log2(std::uint32_t number);
|
||
|
|
|
||
|
15 years ago
|
/**
|
||
|
|
* @brief Integral logarithm
|
||
|
|
*
|
||
|
|
* Returns integral logarithm of given number with given base.
|
||
|
14 years ago
|
* @see log2()
|
||
|
15 years ago
|
*/
|
||
|
14 years ago
|
std::uint32_t MAGNUM_EXPORT log(std::uint32_t base, std::uint32_t number);
|
||
|
15 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
@brief Normalize floating-point value
|
||
|
|
|
||
|
|
Converts integral value from full range of given (signed/unsigned) integral
|
||
|
|
type to value in range @f$ [0, 1] @f$.
|
||
|
|
|
||
|
|
@attention To ensure the integral type is correctly detected when using
|
||
|
|
literals, this function should be called with both template parameters
|
||
|
|
explicit, e.g.:
|
||
|
|
@code
|
||
|
14 years ago
|
// Even if this is character literal, integral type is 32bit, thus a != 1.0f
|
||
|
14 years ago
|
float a = normalize<float>('\127');
|
||
|
|
|
||
|
|
// b = 1.0f
|
||
|
14 years ago
|
float b = normalize<float, std::int8_t>('\127');
|
||
|
14 years ago
|
@endcode
|
||
|
14 years ago
|
|
||
|
|
@todo Signed normalization to [-1.0, 1.0] like in OpenGL?
|
||
|
14 years ago
|
*/
|
||
|
|
template<class FloatingPoint, class Integral> inline constexpr typename std::enable_if<std::is_floating_point<FloatingPoint>::value && std::is_integral<Integral>::value, FloatingPoint>::type normalize(Integral value) {
|
||
|
|
return (FloatingPoint(value)-FloatingPoint(std::numeric_limits<Integral>::min()))/
|
||
|
|
(FloatingPoint(std::numeric_limits<Integral>::max()) - FloatingPoint(std::numeric_limits<Integral>::min()));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
@brief Denormalize floating-point value
|
||
|
|
|
||
|
|
Converts floating-point value in range @f$ [0, 1] @f$ to full range of given
|
||
|
|
integral type.
|
||
|
|
|
||
|
|
@note For best precision, `FloatingPoint` type should be always larger that
|
||
|
14 years ago
|
resulting `Integral` type (e.g. `double` to `std::int32_t`, `long double` to
|
||
|
|
`std::int64_t`).
|
||
|
14 years ago
|
|
||
|
|
@todo Signed normalization to [-1.0, 1.0] like in OpenGL?
|
||
|
14 years ago
|
@todo Stable behavior (working/broken) for long double and long long
|
||
|
|
(currently fails in Debug builds, but passes in Release on GCC 4.7)
|
||
|
14 years ago
|
*/
|
||
|
|
template<class Integral, class FloatingPoint> inline constexpr typename std::enable_if<std::is_floating_point<FloatingPoint>::value && std::is_integral<Integral>::value, Integral>::type denormalize(FloatingPoint value) {
|
||
|
|
return std::numeric_limits<Integral>::min() +
|
||
|
14 years ago
|
round(FloatingPoint(value*std::numeric_limits<Integral>::max()) -
|
||
|
|
FloatingPoint(value*std::numeric_limits<Integral>::min()));
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
/** @brief Clamp value */
|
||
|
|
template<class T> inline T clamp(T value, T min, T max) {
|
||
|
|
return std::min(std::max(value, min), max);
|
||
|
|
}
|
||
|
|
|
||
|
16 years ago
|
}}
|
||
|
16 years ago
|
|
||
|
|
#endif
|