diff options
author | ExMix <rahuba.youri@mapswithme.com> | 2015-06-09 12:37:50 +0300 |
---|---|---|
committer | r.kuznetsov <r.kuznetsov@corp.mail.ru> | 2015-11-30 16:08:33 +0300 |
commit | ccdb358857ca68add99ee9abd462a6bd7cda3064 (patch) | |
tree | 75379811f8194e973cdcd453f42abe5c64b65f18 /drape_frontend/animation | |
parent | eba6b4900534aab61e520ee15f99f7a4a8d1b8a5 (diff) |
[drape] helper class for create chain of animations
Diffstat (limited to 'drape_frontend/animation')
-rw-r--r-- | drape_frontend/animation/value_mapping.hpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/drape_frontend/animation/value_mapping.hpp b/drape_frontend/animation/value_mapping.hpp new file mode 100644 index 0000000000..5c8cbe2360 --- /dev/null +++ b/drape_frontend/animation/value_mapping.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include "base/buffer_vector.hpp" +#include "base/logging.hpp" + +namespace df +{ + +template <typename TValue> +class ValueMapping +{ + /// double = interpolation point [0.0, 1.0] + /// TValue = output value + using TRangePoint = pair<double, TValue>; + using TRangeVector = buffer_vector<TRangePoint, 8>; + using TRangeIter = typename TRangeVector::const_iterator; +public: + ValueMapping() = default; + + void AddRangePoint(double t, TValue const & v) + { +#ifdef DEBUG + if (!m_ranges.empty()) + ASSERT(m_ranges.back().first < t, ()); +#endif + m_ranges.emplace_back(t, v); + } + + TValue GetValue(double t) + { + TRangePoint startPoint, endPoint; + GetRangePoints(t, startPoint, endPoint); + + double normT = t - startPoint.first; + double rangeT = endPoint.first - startPoint.first; + double rangeV = endPoint.second - startPoint.second; + + return startPoint.second + rangeV * normT / rangeT; + } + +private: + void GetRangePoints(double t, TRangePoint & startPoint, TRangePoint & endPoint) + { + ASSERT(t >= 0.0 && t <= 1.0, ()); + ASSERT(m_ranges.size() > 1, ()); + + TRangeIter startIter = m_ranges.begin(); + if (t < startIter->first) + { + startPoint.first = 0.0; + startPoint.second = TValue(); + endPoint = *startIter; + return; + } + + TRangeIter endIter = startIter + 1; + while (t > endIter->first) + { + endIter++; + ASSERT(m_ranges.end() != endIter, ()); + } + + ASSERT(startIter->first <= t && t <= endIter->first, ()); + startPoint = *startIter; + endPoint = *endIter; + } + +private: + TRangeVector m_ranges; +}; + +} |