1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "BsLookupTable.h"
4#include "Math/BsMath.h"
5
6namespace bs
7{
8 LookupTable::LookupTable(Vector<float> values, float startTime, float endTime, uint32_t sampleSize)
9 : mValues(std::move(values))
10 , mSampleSize(std::max(sampleSize, 1U))
11 , mNumSamples((uint32_t)mValues.size() / mSampleSize)
12 , mTimeStart(startTime)
13 {
14 if(endTime < startTime)
15 endTime = startTime;
16
17 float timeInterval;
18 if(mNumSamples > 1)
19 timeInterval = (endTime - startTime) / (mNumSamples - 1);
20 else
21 timeInterval = 0.0f;
22
23 mTimeScale = 1.0f / timeInterval;
24 }
25
26 void LookupTable::evaluate(float t, const float*& left, const float*& right, float& fraction) const
27 {
28 t -= mTimeStart;
29 t *= mTimeScale;
30
31 const auto index = (uint32_t)t;
32 fraction = Math::frac(t);
33
34 const uint32_t leftIdx = std::min(index, mNumSamples - 1);
35 const uint32_t rightIdx = std::min(index + 1, mNumSamples - 1);
36
37 left = &mValues[leftIdx * mSampleSize];
38 right = &mValues[rightIdx * mSampleSize];
39 }
40
41 const float* LookupTable::getSample(uint32_t idx) const
42 {
43 if(mNumSamples == 0)
44 return nullptr;
45
46 idx = std::min(idx, mNumSamples - 1);
47 return &mValues[idx * mSampleSize];
48 }
49}