Browse Source

Math: add scalar and vector fract().

Want to use it with TextureTools::sample*().
pull/690/head
Vladimír Vondruš 3 months ago
parent
commit
f3a4ce7d1d
  1. 1
      doc/changelog.dox
  2. 28
      src/Magnum/Math/Functions.h
  3. 14
      src/Magnum/Math/Test/FunctionsTest.cpp
  4. 3
      src/Magnum/TextureTools/Sample.h

1
doc/changelog.dox

@ -263,6 +263,7 @@ See also:
@subsubsection changelog-latest-new-math Math library @subsubsection changelog-latest-new-math Math library
- Added @ref Math::fmod() (see [mosra/magnum#454](https://github.com/mosra/magnum/pull/454)) - Added @ref Math::fmod() (see [mosra/magnum#454](https://github.com/mosra/magnum/pull/454))
- Added @ref Math::fract()
- Added @ref Math::binomialCoefficient() (see [mosra/magnum#461](https://github.com/mosra/magnum/pull/461)) - Added @ref Math::binomialCoefficient() (see [mosra/magnum#461](https://github.com/mosra/magnum/pull/461))
- Added @ref Math::popcount() - Added @ref Math::popcount()
- Added @ref Math::Color3::fromLinearRgbInt(), - Added @ref Math::Color3::fromLinearRgbInt(),

28
src/Magnum/Math/Functions.h

@ -524,6 +524,7 @@ Calculates the remainder @f$ r @f$ of a floating point division: @f[
numerator, whereas @glsl mod() @ce keeps the denumerator's sign. numerator, whereas @glsl mod() @ce keeps the denumerator's sign.
@m_keyword{mod(),GLSL mod(),} @m_keyword{mod(),GLSL mod(),}
@see @ref fract()
*/ */
template<class T template<class T
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
@ -555,6 +556,33 @@ template<std::size_t size, class T> inline Vector<size, T> fmod(const Vector<siz
return out; return out;
} }
/**
@brief Fractional part of a floating point number
@m_since_latest
Equivalent to @cpp a - floor(a) @ce. In other words, the result is always
non-negative.
@see @ref fmod()
*/
template<class T
#ifndef DOXYGEN_GENERATING_OUTPUT
, typename std::enable_if<IsScalar<T>::value, int>::type = 0
#endif
> inline T fract(T a) {
return T(UnderlyingTypeOf<T>(a) - std::floor(UnderlyingTypeOf<T>(a)));
}
/**
@overload
@m_since_latest
*/
template<std::size_t size, class T> inline Vector<size, T> fract(const Vector<size, T>& a) {
Vector<size, T> out{Magnum::NoInit};
for(std::size_t i = 0; i != size; ++i)
out[i] = Math::fract(a[i]);
return out;
}
/** /**
@brief Linear interpolation of two values @brief Linear interpolation of two values
@param a First value @param a First value

14
src/Magnum/Math/Test/FunctionsTest.cpp

@ -55,6 +55,7 @@ struct FunctionsTest: TestSuite::Tester {
void round(); void round();
void ceil(); void ceil();
void fmod(); void fmod();
void fract();
void binomialCoefficient(); void binomialCoefficient();
void binomialCoefficientInvalidInput(); void binomialCoefficientInvalidInput();
@ -122,6 +123,7 @@ FunctionsTest::FunctionsTest() {
&FunctionsTest::round, &FunctionsTest::round,
&FunctionsTest::ceil, &FunctionsTest::ceil,
&FunctionsTest::fmod, &FunctionsTest::fmod,
&FunctionsTest::fract,
&FunctionsTest::binomialCoefficient, &FunctionsTest::binomialCoefficient,
&FunctionsTest::binomialCoefficientInvalidInput, &FunctionsTest::binomialCoefficientInvalidInput,
@ -369,6 +371,18 @@ void FunctionsTest::fmod() {
CORRADE_COMPARE(Math::fmod(Seconds{2.7_sec}, Seconds{1.3_sec}), Seconds{0.1_sec}); CORRADE_COMPARE(Math::fmod(Seconds{2.7_sec}, Seconds{1.3_sec}), Seconds{0.1_sec});
} }
void FunctionsTest::fract() {
CORRADE_COMPARE(Math::fract(5.132f), 0.132f);
/* It works the other way for negative numbers */
CORRADE_COMPARE(Math::fract(-5.132f), 0.868f);
CORRADE_COMPARE(Math::fract(Vector3{5.132f, 4.0f, -2.474f}), (Vector3{0.132f, 0.0f, 0.526f}));
/* Wrapped types */
CORRADE_COMPARE(Math::fract(2.7_degf), 0.7_degf);
/* Nanoseconds are an integer type */
CORRADE_COMPARE(Math::fract(Seconds{2.7_sec}), Seconds{0.7_sec});
}
void FunctionsTest::sqrt() { void FunctionsTest::sqrt() {
CORRADE_COMPARE(Math::sqrt(16), 4); CORRADE_COMPARE(Math::sqrt(16), 4);
CORRADE_COMPARE(Math::sqrt(Vector3i(256, 1, 0)), Vector3i(16, 1, 0)); CORRADE_COMPARE(Math::sqrt(Vector3i(256, 1, 0)), Vector3i(16, 1, 0));

3
src/Magnum/TextureTools/Sample.h

@ -48,7 +48,8 @@ floating-point type.
Note that the @p texture is interpreted as having linear colors. Use Note that the @p texture is interpreted as having linear colors. Use
@ref sampleSrgb() if you want to perform conversion from sRGB values instead. @ref sampleSrgb() if you want to perform conversion from sRGB values instead.
@see @ref Math::lerp(const T&, const T&, U), @ref Math::unpack(const Integral&) @see @ref Math::lerp(const T&, const T&, U),
@ref Math::unpack(const Integral&), @ref Math::fract()
*/ */
MAGNUM_TEXTURETOOLS_EXPORT Color3 sampleLinear(const Containers::StridedArrayView1D<const Vector3ub>& texture, Float factor); MAGNUM_TEXTURETOOLS_EXPORT Color3 sampleLinear(const Containers::StridedArrayView1D<const Vector3ub>& texture, Float factor);

Loading…
Cancel
Save