PageRenderTime 18ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llcriticaldamp.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 89 lines | 40 code | 11 blank | 38 comment | 7 complexity | bf1e1dd61b97b999e2c547d638e2f122 MD5 | raw file
 1/** 
 2 * @file llcriticaldamp.cpp
 3 * @brief Implementation of the critical damping functionality.
 4 *
 5 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 6 * Second Life Viewer Source Code
 7 * Copyright (C) 2010, Linden Research, Inc.
 8 * 
 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License only.
13 * 
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 * 
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 * 
23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
24 * $/LicenseInfo$
25 */
26
27#include "linden_common.h"
28
29#include "llcriticaldamp.h"
30
31//-----------------------------------------------------------------------------
32// static members
33//-----------------------------------------------------------------------------
34LLFrameTimer LLCriticalDamp::sInternalTimer;
35std::map<F32, F32> LLCriticalDamp::sInterpolants;
36F32 LLCriticalDamp::sTimeDelta;
37
38//-----------------------------------------------------------------------------
39// LLCriticalDamp()
40//-----------------------------------------------------------------------------
41LLCriticalDamp::LLCriticalDamp()
42{
43	sTimeDelta = 0.f;
44}
45
46// static
47//-----------------------------------------------------------------------------
48// updateInterpolants()
49//-----------------------------------------------------------------------------
50void LLCriticalDamp::updateInterpolants()
51{
52	sTimeDelta = sInternalTimer.getElapsedTimeAndResetF32();
53
54	F32 time_constant;
55
56	for (std::map<F32, F32>::iterator iter = sInterpolants.begin();
57		 iter != sInterpolants.end(); iter++)
58	{
59		time_constant = iter->first;
60		F32 new_interpolant = 1.f - pow(2.f, -sTimeDelta / time_constant);
61		new_interpolant = llclamp(new_interpolant, 0.f, 1.f);
62		sInterpolants[time_constant] = new_interpolant;
63	}
64} 
65
66//-----------------------------------------------------------------------------
67// getInterpolant()
68//-----------------------------------------------------------------------------
69F32 LLCriticalDamp::getInterpolant(const F32 time_constant, BOOL use_cache)
70{
71	if (time_constant == 0.f)
72	{
73		return 1.f;
74	}
75
76	if (use_cache && sInterpolants.count(time_constant))
77	{
78		return sInterpolants[time_constant];
79	}
80	
81	F32 interpolant = 1.f - pow(2.f, -sTimeDelta / time_constant);
82	interpolant = llclamp(interpolant, 0.f, 1.f);
83	if (use_cache)
84	{
85		sInterpolants[time_constant] = interpolant;
86	}
87
88	return interpolant;
89}