I was critically damp once. It was okay.
This commit is contained in:
@@ -33,63 +33,105 @@
|
||||
#include "linden_common.h"
|
||||
|
||||
#include "llcriticaldamp.h"
|
||||
#include <algorithm>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// static members
|
||||
//-----------------------------------------------------------------------------
|
||||
LLFrameTimer LLCriticalDamp::sInternalTimer;
|
||||
std::map<F32, F32> LLCriticalDamp::sInterpolants;
|
||||
F32 LLCriticalDamp::sTimeDelta;
|
||||
LLFrameTimer LLSmoothInterpolation::sInternalTimer;
|
||||
std::vector<LLSmoothInterpolation::Interpolant> LLSmoothInterpolation::sInterpolants;
|
||||
F32 LLSmoothInterpolation::sTimeDelta;
|
||||
std::pair<F32, F32> sCachedEntry;
|
||||
|
||||
// helper functors
|
||||
struct LLSmoothInterpolation::CompareTimeConstants
|
||||
{
|
||||
bool operator()(const F32& a, const LLSmoothInterpolation::Interpolant& b) const
|
||||
{
|
||||
return a < b.mTimeScale;
|
||||
}
|
||||
|
||||
bool operator()(const LLSmoothInterpolation::Interpolant& a, const F32& b) const
|
||||
{
|
||||
return a.mTimeScale < b; // bottom of a is higher than bottom of b
|
||||
}
|
||||
|
||||
bool operator()(const LLSmoothInterpolation::Interpolant& a, const LLSmoothInterpolation::Interpolant& b) const
|
||||
{
|
||||
return a.mTimeScale < b.mTimeScale; // bottom of a is higher than bottom of b
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// LLCriticalDamp()
|
||||
// LLSmoothInterpolation()
|
||||
//-----------------------------------------------------------------------------
|
||||
LLCriticalDamp::LLCriticalDamp()
|
||||
LLSmoothInterpolation::LLSmoothInterpolation()
|
||||
{
|
||||
sTimeDelta = 0.f;
|
||||
sCachedEntry = std::pair<F32, F32>(-1.f, -1.f);
|
||||
}
|
||||
|
||||
// static
|
||||
//-----------------------------------------------------------------------------
|
||||
// updateInterpolants()
|
||||
//-----------------------------------------------------------------------------
|
||||
void LLCriticalDamp::updateInterpolants()
|
||||
void LLSmoothInterpolation::updateInterpolants()
|
||||
{
|
||||
sTimeDelta = sInternalTimer.getElapsedTimeAndResetF32();
|
||||
|
||||
F32 time_constant;
|
||||
|
||||
for (std::map<F32, F32>::iterator iter = sInterpolants.begin();
|
||||
iter != sInterpolants.end(); iter++)
|
||||
for (size_t i = 0; i < sInterpolants.size(); i++)
|
||||
{
|
||||
time_constant = iter->first;
|
||||
F32 new_interpolant = 1.f - pow(2.f, -sTimeDelta / time_constant);
|
||||
new_interpolant = llclamp(new_interpolant, 0.f, 1.f);
|
||||
sInterpolants[time_constant] = new_interpolant;
|
||||
Interpolant& interp = sInterpolants[i];
|
||||
interp.mInterpolant = calcInterpolant(interp.mTimeScale);
|
||||
if (sCachedEntry.first == interp.mTimeScale)
|
||||
{
|
||||
sCachedEntry.second = interp.mInterpolant;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// getInterpolant()
|
||||
//-----------------------------------------------------------------------------
|
||||
F32 LLCriticalDamp::getInterpolant(const F32 time_constant, BOOL use_cache)
|
||||
F32 LLSmoothInterpolation::getInterpolant(F32SecondsImplicit time_constant, bool use_cache)
|
||||
{
|
||||
if (time_constant == 0.f)
|
||||
{
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
if (use_cache && sInterpolants.count(time_constant))
|
||||
{
|
||||
return sInterpolants[time_constant];
|
||||
}
|
||||
|
||||
F32 interpolant = 1.f - pow(2.f, -sTimeDelta / time_constant);
|
||||
interpolant = llclamp(interpolant, 0.f, 1.f);
|
||||
if (use_cache)
|
||||
{
|
||||
sInterpolants[time_constant] = interpolant;
|
||||
if (sCachedEntry.first == time_constant)
|
||||
{
|
||||
return sCachedEntry.second;
|
||||
}
|
||||
interpolant_vec_t::iterator find_it = std::lower_bound(sInterpolants.begin(), sInterpolants.end(), time_constant.value(), CompareTimeConstants());
|
||||
if (find_it != sInterpolants.end() && find_it->mTimeScale == time_constant)
|
||||
{
|
||||
sCachedEntry = std::make_pair(time_constant.value(), find_it->mInterpolant);
|
||||
return find_it->mInterpolant;
|
||||
}
|
||||
else
|
||||
{
|
||||
Interpolant interp;
|
||||
interp.mTimeScale = time_constant.value();
|
||||
interp.mInterpolant = calcInterpolant(time_constant.value());
|
||||
sInterpolants.insert(find_it, interp);
|
||||
sCachedEntry = std::make_pair(time_constant.value(), interp.mInterpolant);
|
||||
return interp.mInterpolant;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return calcInterpolant(time_constant.value());
|
||||
}
|
||||
|
||||
return interpolant;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// calcInterpolant()
|
||||
//-----------------------------------------------------------------------------
|
||||
F32 LLSmoothInterpolation::calcInterpolant(F32 time_constant)
|
||||
{
|
||||
return llclamp(1.f - powf(2.f, -sTimeDelta / time_constant), 0.f, 1.f);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user