Browse Source

Math: add castInto() overloads for Float and Double.

euler-xxx
Vladimír Vondruš 5 years ago
parent
commit
4dace92581
  1. 3
      doc/changelog.dox
  2. 8
      src/Magnum/Math/PackingBatch.cpp
  3. 37
      src/Magnum/Math/PackingBatch.h
  4. 43
      src/Magnum/Math/Test/PackingBatchTest.cpp

3
doc/changelog.dox

@ -224,7 +224,8 @@ See also:
@subsubsection changelog-latest-changes-math Math library
- Added @ref Math::castInto() overloads for casting between @ref UnsignedByte
and @ref UnsignedShort or @ref Byte and @ref Short
and @ref UnsignedShort or @ref Byte and @ref Short, and for casting between
@ref Float and @ref Double
@subsubsection changelog-latest-changes-meshtools MeshTools library

8
src/Magnum/Math/PackingBatch.cpp

@ -288,6 +288,14 @@ void castInto(const Corrade::Containers::StridedArrayView2D<const Short>& src, c
castIntoImplementation(src, dst);
}
void castInto(const Corrade::Containers::StridedArrayView2D<const Float>& src, const Corrade::Containers::StridedArrayView2D<Double>& dst) {
castIntoImplementation(src, dst);
}
void castInto(const Corrade::Containers::StridedArrayView2D<const Double>& src, const Corrade::Containers::StridedArrayView2D<Float>& dst) {
castIntoImplementation(src, dst);
}
static_assert(sizeof(HalfMantissaTable) + sizeof(HalfOffsetTable) + sizeof(HalfExponentTable) == 8576,
"improper size of half->float conversion tables");

37
src/Magnum/Math/PackingBatch.h

@ -348,6 +348,43 @@ MAGNUM_EXPORT void castInto(const Corrade::Containers::StridedArrayView2D<const
*/
MAGNUM_EXPORT void castInto(const Corrade::Containers::StridedArrayView2D<const Short>& src, const Corrade::Containers::StridedArrayView2D<Byte>& dst);
/**
@brief Cast 32-bit floating-point values into a 64-bit floating-point representation
@param[in] src Source float values
@param[out] dst Destination double values
@m_since_latest
This function performs an equivalent of @cpp Double(a) @ce over the range, so
e.g. @cpp 135.0f @ce becomes @cpp 135.0 @ce. Second dimension is meant to
contain vector/matrix components, or have a size of 1 for scalars. Expects that
@p src and @p dst have the same size and that the second dimension in both is
contiguous.
@see @ref castInto(const Corrade::Containers::StridedArrayView2D<const Double>&, const Corrade::Containers::StridedArrayView2D<Float>&),
@ref Corrade::Containers::StridedArrayView::isContiguous()
*/
MAGNUM_EXPORT void castInto(const Corrade::Containers::StridedArrayView2D<const Float>& src, const Corrade::Containers::StridedArrayView2D<Double>& dst);
/**
@brief Cast 64-bit floating-point values into a 32-bit floating-point representation
@param[in] src Source double values
@param[out] dst Destination float values
@m_since_latest
This function performs an equivalent of @cpp Float(a) @ce over the range, so
e.g. @cpp 135.0 @ce becomes @cpp 135.0f @ce. Second dimension is meant to
contain vector/matrix components, or have a size of 1 for scalars. Expects that
@p src and @p dst have the same size and that the second dimension in both is
contiguous.
@attention Numbers with more than 23 bits of precision will not be represented
accurately when cast into a @ref Magnum::Float "Float".
@see @ref castInto(const Corrade::Containers::StridedArrayView2D<const Float>&, const Corrade::Containers::StridedArrayView2D<Double>&),
@ref Corrade::Containers::StridedArrayView::isContiguous()
*/
MAGNUM_EXPORT void castInto(const Corrade::Containers::StridedArrayView2D<const Double>& src, const Corrade::Containers::StridedArrayView2D<Float>& dst);
/* Since 1.8.17, the original short-hand group closing doesn't work anymore.
FFS. */
/**

43
src/Magnum/Math/Test/PackingBatchTest.cpp

@ -57,6 +57,8 @@ struct PackingBatchTest: Corrade::TestSuite::Tester {
template<class T, class U> void castUnsignedInteger();
template<class T, class U> void castSignedInteger();
void castFloatDouble();
template<class T> void assertionsPackUnpack();
void assertionsPackUnpackHalf();
template<class U, class T> void assertionsCast();
@ -89,6 +91,8 @@ PackingBatchTest::PackingBatchTest() {
&PackingBatchTest::castSignedInteger<Short, Int>,
&PackingBatchTest::castSignedInteger<Byte, Short>,
&PackingBatchTest::castFloatDouble,
&PackingBatchTest::assertionsPackUnpack<UnsignedByte>,
&PackingBatchTest::assertionsPackUnpack<Byte>,
&PackingBatchTest::assertionsPackUnpack<UnsignedShort>,
@ -105,7 +109,8 @@ PackingBatchTest::PackingBatchTest() {
&PackingBatchTest::assertionsCast<UnsignedShort, UnsignedByte>,
&PackingBatchTest::assertionsCast<Int, Byte>,
&PackingBatchTest::assertionsCast<Int, Short>,
&PackingBatchTest::assertionsCast<Short, Byte>});
&PackingBatchTest::assertionsCast<Short, Byte>,
&PackingBatchTest::assertionsCast<Double, Float>});
}
typedef Math::Constants<Float> Constants;
@ -573,6 +578,42 @@ template<class T, class U> void PackingBatchTest::castSignedInteger() {
Corrade::TestSuite::Compare::Container);
}
void PackingBatchTest::castFloatDouble() {
struct Data {
Math::Vector2<Float> src;
Math::Vector2<Double> dst;
} data[]{
{{0.25f, -89.5f}, {}},
{{-119.0f, 22.75f}, {}},
{{13.0f, 127.5f}, {}}
};
constexpr Math::Vector2<Double> expectedTargetType[] {
{0.25, -89.5},
{-119.0, 22.75},
{13.0, 127.5}
};
constexpr Math::Vector2<Float> expectedOriginalType[] {
{0.25f, -89.5f},
{-119.0f, 22.75f},
{13.0f, 127.5f}
};
Corrade::Containers::StridedArrayView1D<Math::Vector2<Float>> src{data, &data[0].src, 3, sizeof(Data)};
Corrade::Containers::StridedArrayView1D<Math::Vector2<Double>> dst{data, &data[0].dst, 3, sizeof(Data)};
castInto(Corrade::Containers::arrayCast<2, Float>(src),
Corrade::Containers::arrayCast<2, Double>(dst));
CORRADE_COMPARE_AS(dst, Corrade::Containers::stridedArrayView(expectedTargetType),
Corrade::TestSuite::Compare::Container);
/* Test the other way around as well */
castInto(Corrade::Containers::arrayCast<2, Double>(dst),
Corrade::Containers::arrayCast<2, Float>(src));
CORRADE_COMPARE_AS(src, Corrade::Containers::stridedArrayView(expectedOriginalType),
Corrade::TestSuite::Compare::Container);
}
template<class T> void PackingBatchTest::assertionsPackUnpack() {
#ifdef CORRADE_NO_ASSERT
CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions");

Loading…
Cancel
Save