diff --git a/src/Math/Functions.h b/src/Math/Functions.h index e06f5dd36..a78e5d06c 100644 --- a/src/Math/Functions.h +++ b/src/Math/Functions.h @@ -75,14 +75,18 @@ std::uint32_t MAGNUM_EXPORT log2(std::uint32_t number); std::uint32_t MAGNUM_EXPORT log(std::uint32_t base, std::uint32_t number); /** -@brief Normalize floating-point value +@brief Normalize integral value -Converts integral value from full range of given (signed/unsigned) integral -type to value in range @f$ [0, 1] @f$. +Converts integral value from full range of given *unsigned* integral type to +value in range @f$ [0, 1] @f$ or from *signed* integral to range @f$ [-1, 1] @f$. + +@note For best precision, `FloatingPoint` type should be always larger that + resulting `Integral` type (e.g. `double` to `std::int32_t`, `long double` + to `std::int64_t`). @attention To ensure the integral type is correctly detected when using -literals, this function should be called with both template parameters -explicit, e.g.: + literals, this function should be called with both template parameters + explicit, e.g.: @code // Even if this is character literal, integral type is 32bit, thus a != 1.0f float a = normalize('\127'); @@ -91,32 +95,43 @@ float a = normalize('\127'); float b = normalize('\127'); @endcode -@todo Signed normalization to [-1.0, 1.0] like in OpenGL? +@see denormalize() */ -template inline constexpr typename std::enable_if::value && std::is_integral::value, FloatingPoint>::type normalize(Integral value) { - return (FloatingPoint(value)-FloatingPoint(std::numeric_limits::min()))/ - (FloatingPoint(std::numeric_limits::max()) - FloatingPoint(std::numeric_limits::min())); +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline constexpr FloatingPoint normalize(Integral value); +#else +template inline constexpr typename std::enable_if::value && std::is_integral::value && std::is_unsigned::value, FloatingPoint>::type normalize(Integral value) { + return value/FloatingPoint(std::numeric_limits::max()); } +template inline constexpr typename std::enable_if::value && std::is_integral::value && std::is_signed::value, FloatingPoint>::type normalize(Integral value) { + return std::max(value/FloatingPoint(std::numeric_limits::max()), FloatingPoint(-1)); +} +#endif + /** @brief Denormalize floating-point value Converts floating-point value in range @f$ [0, 1] @f$ to full range of given +unsigned integral type or range @f$ [-1, 1] @f$ to full range of given signed integral type. @note For best precision, `FloatingPoint` type should be always larger that -resulting `Integral` type (e.g. `double` to `std::int32_t`, `long double` to -`std::int64_t`). + resulting `Integral` type (e.g. `double` to `std::int32_t`, `long double` + to `std::int64_t`). -@todo Signed normalization to [-1.0, 1.0] like in OpenGL? -@todo Stable behavior (working/broken) for long double and long long - (currently fails in Debug builds, but passes in Release on GCC 4.7) +@see normalize() */ -template inline constexpr typename std::enable_if::value && std::is_integral::value, Integral>::type denormalize(FloatingPoint value) { - return std::numeric_limits::min() + - round(FloatingPoint(value*std::numeric_limits::max()) - - FloatingPoint(value*std::numeric_limits::min())); +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline constexpr typename Integral denormalize(FloatingPoint value); +#else +template inline constexpr typename std::enable_if::value && std::is_integral::value && std::is_unsigned::value, Integral>::type denormalize(FloatingPoint value) { + return value*std::numeric_limits::max(); +} +template inline constexpr typename std::enable_if::value && std::is_integral::value && std::is_signed::value, Integral>::type denormalize(FloatingPoint value) { + return value*std::numeric_limits::max(); } +#endif /** @brief Clamp value */ template inline T clamp(T value, T min, T max) { diff --git a/src/Math/Test/FunctionsTest.cpp b/src/Math/Test/FunctionsTest.cpp index e379674dd..9b55ae7e2 100644 --- a/src/Math/Test/FunctionsTest.cpp +++ b/src/Math/Test/FunctionsTest.cpp @@ -23,8 +23,12 @@ class FunctionsTest: public Corrade::TestSuite::Tester { public: FunctionsTest(); - void normalize(); - void denormalize(); + void normalizeUnsigned(); + void normalizeSigned(); + void denormalizeUnsigned(); + void denormalizeSigned(); + void renormalizeUnsinged(); + void renormalizeSinged(); void clamp(); void pow(); void log(); @@ -32,62 +36,124 @@ class FunctionsTest: public Corrade::TestSuite::Tester { }; FunctionsTest::FunctionsTest() { - addTests(&FunctionsTest::normalize, - &FunctionsTest::denormalize, + addTests(&FunctionsTest::normalizeUnsigned, + &FunctionsTest::normalizeSigned, + &FunctionsTest::denormalizeUnsigned, + &FunctionsTest::denormalizeSigned, + &FunctionsTest::renormalizeUnsinged, + &FunctionsTest::renormalizeSinged, &FunctionsTest::clamp, &FunctionsTest::pow, &FunctionsTest::log, &FunctionsTest::log2); } -void FunctionsTest::normalize() { - /* Range for signed and unsigned */ - CORRADE_COMPARE((Math::normalize(-128)), 0.0f); - CORRADE_COMPARE((Math::normalize(127)), 1.0f); +void FunctionsTest::normalizeUnsigned() { CORRADE_COMPARE((Math::normalize(0)), 0.0f); CORRADE_COMPARE((Math::normalize(255)), 1.0f); - /* Between */ - CORRADE_COMPARE((Math::normalize(16384)), 0.750011f); - CORRADE_COMPARE((Math::normalize(-16384)), 0.250004f); + CORRADE_COMPARE((Math::normalize(0)), 0.0); + CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0); + + CORRADE_COMPARE((Math::normalize(0)), 0.0); + CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0); - /* Test overflow for large types */ - CORRADE_COMPARE((Math::normalize(std::numeric_limits::min())), 0.0f); - CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0f); - CORRADE_COMPARE((Math::normalize(0)), 0.0f); - CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0f); + CORRADE_COMPARE((Math::normalize(0)), 0.0f); + CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0f); - CORRADE_COMPARE((Math::normalize(std::numeric_limits::min())), 0.0); - CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0); - CORRADE_COMPARE((Math::normalize(0)), 0.0); - CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0); + CORRADE_COMPARE((Math::normalize(8192)), 0.125002f); + CORRADE_COMPARE((Math::normalize(49152)), 0.750011f); } -void FunctionsTest::denormalize() { - /* Range for signed and unsigned */ - CORRADE_COMPARE(Math::denormalize(0.0f), -128); - CORRADE_COMPARE(Math::denormalize(1.0f), 127); +void FunctionsTest::normalizeSigned() { + CORRADE_COMPARE((Math::normalize(127)), 1.0f); + CORRADE_COMPARE((Math::normalize(0)), 0.0f); + CORRADE_COMPARE((Math::normalize(-128)), -1.0f); + + CORRADE_COMPARE((Math::normalize(std::numeric_limits::min())), -1.0f); + CORRADE_COMPARE((Math::normalize(0)), 0.0f); + CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0f); + + CORRADE_COMPARE((Math::normalize(std::numeric_limits::min())), -1.0); + CORRADE_COMPARE((Math::normalize(0)), 0.0); + CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0); + + CORRADE_COMPARE((Math::normalize(std::numeric_limits::min())), -1.0); + CORRADE_COMPARE((Math::normalize(0)), 0.0); + CORRADE_COMPARE((Math::normalize(std::numeric_limits::max())), 1.0); + + CORRADE_COMPARE((Math::normalize(16384)), 0.500015f); + CORRADE_COMPARE((Math::normalize(-16384)), -0.500015f); +} + +void FunctionsTest::denormalizeUnsigned() { CORRADE_COMPARE(Math::denormalize(0.0f), 0); CORRADE_COMPARE(Math::denormalize(1.0f), 255); - /* Between */ - CORRADE_COMPARE(Math::denormalize(0.33f), -11141); - CORRADE_COMPARE(Math::denormalize(0.66f), 10485); + CORRADE_COMPARE(Math::denormalize(0.0f), 0); + CORRADE_COMPARE(Math::denormalize(1.0f), std::numeric_limits::max()); + + CORRADE_COMPARE(Math::denormalize(0.0), 0); + CORRADE_COMPARE(Math::denormalize(1.0), std::numeric_limits::max()); - /* Test overflow for large types */ - CORRADE_COMPARE(Math::denormalize(0.0f), std::numeric_limits::min()); - CORRADE_COMPARE(Math::denormalize(0.0f), 0); - CORRADE_COMPARE(Math::denormalize(0.0), std::numeric_limits::min()); CORRADE_COMPARE(Math::denormalize(0.0), 0); + CORRADE_COMPARE(Math::denormalize(1.0), std::numeric_limits::max()); + CORRADE_COMPARE(Math::denormalize(0.33f), 21626); + CORRADE_COMPARE(Math::denormalize(0.66f), 43253); +} + +void FunctionsTest::denormalizeSigned() { + CORRADE_COMPARE(Math::denormalize(-1.0f), -127); + CORRADE_COMPARE(Math::denormalize(0.0f), 0); + CORRADE_COMPARE(Math::denormalize(1.0f), 127); + + CORRADE_COMPARE(Math::denormalize(-1.0f), std::numeric_limits::min()+1); + CORRADE_COMPARE(Math::denormalize(0.0f), 0); + CORRADE_COMPARE(Math::denormalize(1.0f), std::numeric_limits::max()); + + CORRADE_COMPARE(Math::denormalize(-1.0), std::numeric_limits::min()+1); + CORRADE_COMPARE(Math::denormalize(0.0), 0); CORRADE_COMPARE(Math::denormalize(1.0), std::numeric_limits::max()); - CORRADE_COMPARE(Math::denormalize(1.0), std::numeric_limits::max()); -// { -// CORRADE_EXPECT_FAIL("Denormalize doesn't work for large types well"); -// CORRADE_COMPARE((Math::denormalize(1.0)), numeric_limits::max()); -// CORRADE_COMPARE((Math::denormalize(1.0)), numeric_limits::max()); -// } + CORRADE_COMPARE(Math::denormalize(-1.0l), std::numeric_limits::min()+1); + CORRADE_COMPARE(Math::denormalize(0.0l), 0); + CORRADE_COMPARE(Math::denormalize(1.0l), std::numeric_limits::max()); + + CORRADE_COMPARE(Math::denormalize(-0.33f), -10813); + CORRADE_COMPARE(Math::denormalize(0.66f), 21626); +} + +void FunctionsTest::renormalizeUnsinged() { + CORRADE_COMPARE(Math::normalize(Math::denormalize(0.0f)), 0.0f); + CORRADE_COMPARE(Math::normalize(Math::denormalize(1.0f)), 1.0f); + + CORRADE_COMPARE(Math::normalize(Math::denormalize(0.0f)), 0.0f); + CORRADE_COMPARE(Math::normalize(Math::denormalize(1.0f)), 1.0f); + + CORRADE_COMPARE(Math::normalize(Math::denormalize(0.0)), 0.0); + CORRADE_COMPARE(Math::normalize(Math::denormalize(1.0)), 1.0); + + CORRADE_COMPARE(Math::normalize(Math::denormalize(0.0l)), 0.0l); + CORRADE_COMPARE(Math::normalize(Math::denormalize(1.0l)), 1.0l); +} + +void FunctionsTest::renormalizeSinged() { + CORRADE_COMPARE(Math::normalize(Math::denormalize(-1.0f)), -1.0f); + CORRADE_COMPARE(Math::normalize(Math::denormalize(0.0f)), 0.0f); + CORRADE_COMPARE(Math::normalize(Math::denormalize(1.0f)), 1.0f); + + CORRADE_COMPARE(Math::normalize(Math::denormalize(-1.0f)), -1.0f); + CORRADE_COMPARE(Math::normalize(Math::denormalize(0.0f)), 0.0f); + CORRADE_COMPARE(Math::normalize(Math::denormalize(1.0f)), 1.0f); + + CORRADE_COMPARE(Math::normalize(Math::denormalize(-1.0)), -1.0); + CORRADE_COMPARE(Math::normalize(Math::denormalize(0.0)), 0.0); + CORRADE_COMPARE(Math::normalize(Math::denormalize(1.0)), 1.0); + + CORRADE_COMPARE(Math::normalize(Math::denormalize(-1.0l)), -1.0l); + CORRADE_COMPARE(Math::normalize(Math::denormalize(0.0l)), 0.0l); + CORRADE_COMPARE(Math::normalize(Math::denormalize(1.0l)), 1.0l); } void FunctionsTest::clamp() { diff --git a/src/Test/ColorTest.cpp b/src/Test/ColorTest.cpp index d7ae54931..fd6aca832 100644 --- a/src/Test/ColorTest.cpp +++ b/src/Test/ColorTest.cpp @@ -104,16 +104,16 @@ void ColorTest::fromDenormalized() { } void ColorTest::fromNormalized() { - CORRADE_COMPARE(Color3::fromNormalized(Color3f(0.294118, 0.45098, 0.878431)), Color3(75, 115, 224)); + CORRADE_COMPARE(Color3::fromNormalized(Color3f(0.294118, 0.45098, 0.878431)), Color3(75, 114, 223)); } void ColorTest::fromHue() { - CORRADE_COMPARE(Color3::fromHSV(27.0f, 1.0f, 1.0f), Color3(255, 115, 0)); - CORRADE_COMPARE(Color3::fromHSV(86.0f, 1.0f, 1.0f), Color3(145, 255, 0)); - CORRADE_COMPARE(Color3::fromHSV(134.0f, 1.0f, 1.0f), Color3(0, 255, 60)); + CORRADE_COMPARE(Color3::fromHSV(27.0f, 1.0f, 1.0f), Color3(255, 114, 0)); + CORRADE_COMPARE(Color3::fromHSV(86.0f, 1.0f, 1.0f), Color3(144, 255, 0)); + CORRADE_COMPARE(Color3::fromHSV(134.0f, 1.0f, 1.0f), Color3(0, 255, 59)); CORRADE_COMPARE(Color3::fromHSV(191.0f, 1.0f, 1.0f), Color3(0, 208, 255)); CORRADE_COMPARE(Color3::fromHSV(269.0f, 1.0f, 1.0f), Color3(123, 0, 255)); - CORRADE_COMPARE(Color3::fromHSV(317.0f, 1.0f, 1.0f), Color3(255, 0, 183)); + CORRADE_COMPARE(Color3::fromHSV(317.0f, 1.0f, 1.0f), Color3(255, 0, 182)); } void ColorTest::hue() { @@ -126,7 +126,7 @@ void ColorTest::hue() { } void ColorTest::fromSaturation() { - CORRADE_COMPARE(Color3::fromHSV(0.0f, 0.702f, 1.0f), Color3(255, 76, 76)); + CORRADE_COMPARE(Color3::fromHSV(0.0f, 0.702f, 1.0f), Color3(255, 75, 75)); } void ColorTest::saturation() { @@ -143,7 +143,7 @@ void ColorTest::value() { } void ColorTest::hsv() { - CORRADE_COMPARE(Color3::fromHSV(230.0f, 0.749f, 0.427f), Color3(27, 41, 109)); + CORRADE_COMPARE(Color3::fromHSV(230.0f, 0.749f, 0.427f), Color3(27, 40, 108)); float hue, saturation, value; std::tie(hue, saturation, value) = Color3(27, 41, 109).toHSV(); @@ -153,24 +153,24 @@ void ColorTest::hsv() { } void ColorTest::hsvOverflow() { - CORRADE_COMPARE(Color3::fromHSV(27.0f-360.0f, 1.0f, 1.0f), Color3(255, 115, 0)); - CORRADE_COMPARE(Color3::fromHSV(86.0f-360.0f, 1.0f, 1.0f), Color3(145, 255, 0)); - CORRADE_COMPARE(Color3::fromHSV(134.0f-360.0f, 1.0f, 1.0f), Color3(0, 255, 60)); + CORRADE_COMPARE(Color3::fromHSV(27.0f-360.0f, 1.0f, 1.0f), Color3(255, 114, 0)); + CORRADE_COMPARE(Color3::fromHSV(86.0f-360.0f, 1.0f, 1.0f), Color3(144, 255, 0)); + CORRADE_COMPARE(Color3::fromHSV(134.0f-360.0f, 1.0f, 1.0f), Color3(0, 255, 59)); CORRADE_COMPARE(Color3::fromHSV(191.0f-360.0f, 1.0f, 1.0f), Color3(0, 208, 255)); CORRADE_COMPARE(Color3::fromHSV(269.0f-360.0f, 1.0f, 1.0f), Color3(123, 0, 255)); - CORRADE_COMPARE(Color3::fromHSV(317.0f-360.0f, 1.0f, 1.0f), Color3(255, 0, 183)); + CORRADE_COMPARE(Color3::fromHSV(317.0f-360.0f, 1.0f, 1.0f), Color3(255, 0, 182)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+27.0f, 1.0f, 1.0f), Color3(255, 115, 0)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+86.0f, 1.0f, 1.0f), Color3(145, 255, 0)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+134.0f, 1.0f, 1.0f), Color3(0, 255, 60)); + CORRADE_COMPARE(Color3::fromHSV(360.0f+27.0f, 1.0f, 1.0f), Color3(255, 114, 0)); + CORRADE_COMPARE(Color3::fromHSV(360.0f+86.0f, 1.0f, 1.0f), Color3(144, 255, 0)); + CORRADE_COMPARE(Color3::fromHSV(360.0f+134.0f, 1.0f, 1.0f), Color3(0, 255, 59)); CORRADE_COMPARE(Color3::fromHSV(360.0f+191.0f, 1.0f, 1.0f), Color3(0, 208, 255)); CORRADE_COMPARE(Color3::fromHSV(360.0f+269.0f, 1.0f, 1.0f), Color3(123, 0, 255)); - CORRADE_COMPARE(Color3::fromHSV(360.0f+317.0f, 1.0f, 1.0f), Color3(255, 0, 183)); + CORRADE_COMPARE(Color3::fromHSV(360.0f+317.0f, 1.0f, 1.0f), Color3(255, 0, 182)); } void ColorTest::hsvAlpha() { - CORRADE_COMPARE(Color4::fromHSV(std::make_tuple(230.0f, 0.749f, 0.427f), 23), Color4(27, 41, 109, 23)); - CORRADE_COMPARE(Color4::fromHSV(230.0f, 0.749f, 0.427f, 23), Color4(27, 41, 109, 23)); + CORRADE_COMPARE(Color4::fromHSV(std::make_tuple(230.0f, 0.749f, 0.427f), 23), Color4(27, 40, 108, 23)); + CORRADE_COMPARE(Color4::fromHSV(230.0f, 0.749f, 0.427f, 23), Color4(27, 40, 108, 23)); } void ColorTest::debug() {