From 7b0f1e99d68920e1635a895bb0c103541f4070ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 10 Jul 2018 18:44:09 +0200 Subject: [PATCH] Animation: add a typeless storage base for TrackView. --- src/Magnum/Animation/Animation.h | 1 + src/Magnum/Animation/Test/TrackViewTest.cpp | 26 +++++++++- src/Magnum/Animation/Track.h | 54 ++++++++++++++------- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/Magnum/Animation/Animation.h b/src/Magnum/Animation/Animation.h index bb9d7e60b..4852f55ca 100644 --- a/src/Magnum/Animation/Animation.h +++ b/src/Magnum/Animation/Animation.h @@ -44,6 +44,7 @@ enum class Interpolation: UnsignedByte; enum class Extrapolation: UnsignedByte; template> class Track; +class TrackViewStorage; template> class TrackView; }} diff --git a/src/Magnum/Animation/Test/TrackViewTest.cpp b/src/Magnum/Animation/Test/TrackViewTest.cpp index 4706cc284..9c2cf602b 100644 --- a/src/Magnum/Animation/Test/TrackViewTest.cpp +++ b/src/Magnum/Animation/Test/TrackViewTest.cpp @@ -39,6 +39,8 @@ struct TrackViewTest: TestSuite::Tester { void constructSingleArray(); void constructSingleArrayDefaults(); + void constructCopyStorage(); + void at(); void atStrict(); void atDifferentResultType(); @@ -88,7 +90,9 @@ TrackViewTest::TrackViewTest() { addTests({&TrackViewTest::construct, &TrackViewTest::constructDefaults, &TrackViewTest::constructSingleArray, - &TrackViewTest::constructSingleArrayDefaults}); + &TrackViewTest::constructSingleArrayDefaults, + + &TrackViewTest::constructCopyStorage}); addInstancedTests({&TrackViewTest::at, &TrackViewTest::atStrict}, Containers::arraySize(AtData)); @@ -161,6 +165,26 @@ void TrackViewTest::constructSingleArrayDefaults() { CORRADE_COMPARE(a[1], (std::pair{5.0f, {0.3f, 0.6f, 1.0f}})); } +void TrackViewTest::constructCopyStorage() { + const std::pair data[]{ + {0.0f, {3.0f, 1.0f, 0.1f}}, + {5.0f, {0.3f, 0.6f, 1.0f}}}; + + const TrackView a{data, Math::lerp, + Extrapolation::Extrapolated, Extrapolation::DefaultConstructed}; + + const TrackViewStorage b = a; + + auto& bv = *static_cast*>(&b); + + CORRADE_COMPARE(bv.interpolator(), Math::lerp); + CORRADE_COMPARE(bv.before(), Extrapolation::Extrapolated); + CORRADE_COMPARE(bv.after(), Extrapolation::DefaultConstructed); + CORRADE_COMPARE(bv.keys().size(), 2); + CORRADE_COMPARE(bv.values().size(), 2); + CORRADE_COMPARE(bv[1], (std::pair{5.0f, {0.3f, 0.6f, 1.0f}})); +} + namespace { const std::pair Keyframes[]{ {0.0f, 3.0f}, diff --git a/src/Magnum/Animation/Track.h b/src/Magnum/Animation/Track.h index a7cb88023..f05a04802 100644 --- a/src/Magnum/Animation/Track.h +++ b/src/Magnum/Animation/Track.h @@ -26,7 +26,7 @@ */ /** @file - * @brief Class @ref Magnum::Animation::Track, @ref Magnum::Animation::TrackView + * @brief Class @ref Magnum::Animation::Track, @ref Magnum::Animation::TrackViewStorage, @ref Magnum::Animation::TrackView */ #include @@ -252,6 +252,24 @@ template friend class TrackView; + + template explicit TrackViewStorage(const Containers::StridedArrayView& keys, const Containers::StridedArrayView& values, R(*interpolator)(const V&, const V&, Float), Extrapolation before, Extrapolation after) noexcept: _keys{reinterpret_cast&>(keys)}, _values{reinterpret_cast&>(values)}, _interpolator{reinterpret_cast(interpolator)}, _before{before}, _after{after} {} + + Containers::StridedArrayView _keys; + Containers::StridedArrayView _values; + void(*_interpolator)(void); + Extrapolation _before, _after; +}; + /** @brief Animation track view @tparam K Key type @@ -266,7 +284,7 @@ template #endif -> class TrackView { +> class TrackView: public TrackViewStorage { public: /** @brief Key type */ typedef K KeyType; @@ -291,13 +309,13 @@ template& keys, const Containers::StridedArrayView& values, Interpolator interpolator, Extrapolation before, Extrapolation after) noexcept: _keys{keys}, _values{values}, _interpolator{interpolator}, _before{before}, _after{after} {} + explicit TrackView(const Containers::StridedArrayView& keys, const Containers::StridedArrayView& values, Interpolator interpolator, Extrapolation before, Extrapolation after) noexcept: TrackViewStorage{keys, values, interpolator, before, after} {} /** @overload * Equivalent to calling @ref TrackView(const Containers::StridedArrayView&, const Containers::StridedArrayView&, Interpolator, Extrapolation, Extrapolation) * with both @p before and @p after set to @p extrapolation. */ - constexpr explicit TrackView(const Containers::StridedArrayView& keys, const Containers::StridedArrayView& values, Interpolator interpolator, Extrapolation extrapolation = Extrapolation::Extrapolated) noexcept: TrackView{keys, values, interpolator, extrapolation, extrapolation} {} + explicit TrackView(const Containers::StridedArrayView& keys, const Containers::StridedArrayView& values, Interpolator interpolator, Extrapolation extrapolation = Extrapolation::Extrapolated) noexcept: TrackView{keys, values, interpolator, extrapolation, extrapolation} {} /** * @brief Construct from an interleaved array @@ -309,13 +327,13 @@ template&, const Containers::StridedArrayView&, Interpolator, Extrapolation, Extrapolation). */ - constexpr explicit TrackView(Containers::ArrayView> data, Interpolator interpolator, Extrapolation before, Extrapolation after) noexcept: _keys{data ? &data[0].first : nullptr, data.size(), sizeof(std::pair)}, _values{data ? &data[0].second : nullptr, data.size(), sizeof(std::pair)}, _interpolator{interpolator}, _before{before}, _after{after} {} + explicit TrackView(Containers::ArrayView> data, Interpolator interpolator, Extrapolation before, Extrapolation after) noexcept: TrackViewStorage{Containers::StridedArrayView{data ? &data[0].first : nullptr, data.size(), sizeof(std::pair)}, Containers::StridedArrayView{data ? &data[0].second : nullptr, data.size(), sizeof(std::pair)}, interpolator, before, after} {} /** @overload * Equivalent to calling @ref TrackView(Containers::ArrayView>, Interpolator, Extrapolation, Extrapolation) * with both @p before and @p after set to @p extrapolation. */ - constexpr explicit TrackView(Containers::ArrayView> data, Interpolator interpolator, Extrapolation extrapolation = Extrapolation::Extrapolated) noexcept: TrackView{data, interpolator, extrapolation, extrapolation} {} + explicit TrackView(Containers::ArrayView> data, Interpolator interpolator, Extrapolation extrapolation = Extrapolation::Extrapolated) noexcept: TrackView{data, interpolator, extrapolation, extrapolation} {} /** * @brief Extrapolation behavior before first keyframe @@ -332,25 +350,31 @@ template(_interpolator); + } /** * @brief Key data * * @see @ref values(), @ref operator[]() */ - Containers::StridedArrayView keys() const { return _keys; } + Containers::StridedArrayView keys() const { + return reinterpret_cast&>(_keys); + } /** * @brief Value data * * @see @ref keys(), @ref operator[]() */ - Containers::StridedArrayView values() const { return _values; } + Containers::StridedArrayView values() const { + return reinterpret_cast&>(_values); + } /** @brief Keyframe access */ std::pair operator[](std::size_t i) const { - return {_keys[i], _values[i]}; + return {keys()[i], values()[i]}; } /** @@ -374,7 +398,7 @@ template _keys; - Containers::StridedArrayView _values; - Interpolator _interpolator; - Extrapolation _before, _after; }; }}