PageRenderTime 59ms CodeModel.GetById 18ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/llinterp.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 426 lines | 304 code | 69 blank | 53 comment | 9 complexity | 42d0bad19c4eae5cf33457d6896fd00b MD5 | raw file
  1/** 
  2 * @file llinterp.h
  3 *
  4 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2010, Linden Research, Inc.
  7 * 
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation;
 11 * version 2.1 of the License only.
 12 * 
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * Lesser General Public License for more details.
 17 * 
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 21 * 
 22 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 23 * $/LicenseInfo$
 24 */
 25
 26#ifndef LL_LLINTERP_H
 27#define LL_LLINTERP_H
 28
 29#if defined(LL_WINDOWS)
 30// macro definitions for common math constants (e.g. M_PI) are declared under the _USE_MATH_DEFINES
 31// on Windows system.
 32// So, let's define _USE_MATH_DEFINES before including math.h
 33	#define _USE_MATH_DEFINES
 34#endif
 35
 36#include "math.h"
 37
 38// Class from which different types of interpolators can be derived
 39
 40class LLInterpVal
 41{
 42public:
 43	virtual ~LLInterpVal() {}
 44	virtual void interp(LLInterpVal &target, const F32 frac); // Linear interpolation for each type
 45};
 46
 47template <typename Type>
 48class LLInterp
 49{
 50public:
 51        LLInterp();
 52	virtual ~LLInterp() {}
 53
 54	virtual void start();
 55	void update(const F32 time);
 56	const Type &getCurVal() const;
 57
 58	void setStartVal(const Type &start_val);
 59	const Type &getStartVal() const;
 60
 61	void setEndVal(const Type &target_val);
 62	const Type &getEndVal() const;
 63
 64	void setStartTime(const F32 time);
 65	F32 getStartTime() const;
 66
 67	void setEndTime(const F32 time);
 68	F32 getEndTime() const;
 69
 70	BOOL isActive() const;
 71	BOOL isDone() const;
 72	
 73protected:
 74	F32 mStartTime;
 75	F32 mEndTime;
 76	F32 mDuration;
 77	BOOL mActive;
 78	BOOL mDone;
 79
 80	Type mStartVal;
 81	Type mEndVal;
 82
 83	F32 mCurTime;
 84	Type mCurVal;
 85};
 86
 87template <typename Type>
 88class LLInterpLinear : public LLInterp<Type>
 89{
 90public:
 91	/*virtual*/ void start();
 92	void update(const F32 time);
 93	F32 getCurFrac() const;
 94protected:
 95	F32 mCurFrac;
 96};
 97
 98template <typename Type>
 99class LLInterpExp : public LLInterpLinear<Type>
100{
101public:
102	void update(const F32 time);
103protected:
104};
105
106template <typename Type>
107class LLInterpAttractor : public LLInterp<Type>
108{
109public:
110	LLInterpAttractor();
111	/*virtual*/ void start();
112	void setStartVel(const Type &vel);
113	void setForce(const F32 force);
114	void update(const F32 time);
115protected:
116	F32 mForce;
117	Type mStartVel;
118	Type mVelocity;
119};
120
121template <typename Type>
122class LLInterpFunc : public LLInterp<Type>
123{
124public:
125	LLInterpFunc();
126	void update(const F32 time);
127
128	void setFunc(Type (*)(const F32, void *data), void *data);
129protected:
130	Type (*mFunc)(const F32 time, void *data);
131	void *mData;
132};
133
134
135///////////////////////////////////
136//
137// Implementation
138//
139//
140
141/////////////////////////////////
142//
143// LLInterp base class implementation
144//
145
146template <typename Type>
147LLInterp<Type>::LLInterp()
148: mStartVal(Type()), mEndVal(Type()), mCurVal(Type())
149{
150	mStartTime = 0.f;
151	mEndTime = 1.f;
152	mDuration = 1.f;
153	mCurTime = 0.f;
154	mDone = FALSE;
155	mActive = FALSE;
156}
157
158template <class Type>
159void LLInterp<Type>::setStartVal(const Type &start_val)
160{
161	mStartVal = start_val;
162}
163
164template <class Type>
165void LLInterp<Type>::start()
166{
167	mCurVal = mStartVal;
168	mCurTime = mStartTime;
169	mDone = FALSE;
170	mActive = FALSE;
171}
172
173template <class Type>
174const Type &LLInterp<Type>::getStartVal() const
175{
176	return mStartVal;
177}
178
179template <class Type>
180void LLInterp<Type>::setEndVal(const Type &end_val)
181{
182	mEndVal = end_val;
183}
184
185template <class Type>
186const Type &LLInterp<Type>::getEndVal() const
187{
188	return mEndVal;
189}
190
191template <class Type>
192const Type &LLInterp<Type>::getCurVal() const
193{
194	return mCurVal;
195}
196
197
198template <class Type>
199void LLInterp<Type>::setStartTime(const F32 start_time)
200{
201	mStartTime = start_time;
202	mDuration = mEndTime - mStartTime;
203}
204
205template <class Type>
206F32 LLInterp<Type>::getStartTime() const
207{
208	return mStartTime;
209}
210
211
212template <class Type>
213void LLInterp<Type>::setEndTime(const F32 end_time)
214{
215	mEndTime = end_time;
216	mDuration = mEndTime - mStartTime;
217}
218
219
220template <class Type>
221F32 LLInterp<Type>::getEndTime() const
222{
223	return mEndTime;
224}
225
226
227template <class Type>
228BOOL LLInterp<Type>::isDone() const
229{
230	return mDone;
231}
232
233template <class Type>
234BOOL LLInterp<Type>::isActive() const
235{
236	return mActive;
237}
238
239//////////////////////////////
240//
241// LLInterpLinear derived class implementation.
242//
243template <typename Type>
244void LLInterpLinear<Type>::start()
245{
246	LLInterp<Type>::start();
247	mCurFrac = 0.f;
248}
249
250template <typename Type>
251void LLInterpLinear<Type>::update(const F32 time)
252{
253	F32 target_frac = (time - this->mStartTime) / this->mDuration;
254	F32 dfrac = target_frac - this->mCurFrac;
255	if (target_frac >= 0.f)
256	{
257		this->mActive = TRUE;
258	}
259	
260	if (target_frac > 1.f)
261	{
262		this->mCurVal = this->mEndVal;
263		this->mCurFrac = 1.f;
264		this->mCurTime = time;
265		this->mDone = TRUE;
266		return;
267	}
268
269	target_frac = llmin(1.f, target_frac);
270	target_frac = llmax(0.f, target_frac);
271
272	if (dfrac >= 0.f)
273	{
274		F32 total_frac = 1.f - this->mCurFrac;
275		F32 inc_frac = dfrac / total_frac;
276		this->mCurVal = inc_frac * this->mEndVal + (1.f - inc_frac) * this->mCurVal;
277		this->mCurTime = time;
278	}
279	else
280	{
281		F32 total_frac = this->mCurFrac - 1.f;
282		F32 inc_frac = dfrac / total_frac;
283		this->mCurVal = inc_frac * this->mStartVal + (1.f - inc_frac) * this->mCurVal;
284		this->mCurTime = time;
285	}
286	mCurFrac = target_frac;
287}
288
289template <class Type>
290F32 LLInterpLinear<Type>::getCurFrac() const
291{
292	return mCurFrac;
293}
294
295
296//////////////////////////////
297//
298// LLInterpAttractor derived class implementation.
299//
300
301
302template <class Type>
303LLInterpAttractor<Type>::LLInterpAttractor() : LLInterp<Type>()
304{
305	mForce = 0.1f;
306	mVelocity *= 0.f;
307	mStartVel *= 0.f;
308}
309
310template <class Type>
311void LLInterpAttractor<Type>::start()
312{
313	LLInterp<Type>::start();
314	mVelocity = mStartVel;
315}
316
317
318template <class Type>
319void LLInterpAttractor<Type>::setStartVel(const Type &vel)
320{
321	mStartVel = vel;
322}
323
324template <class Type>
325void LLInterpAttractor<Type>::setForce(const F32 force)
326{
327	mForce = force;
328}
329
330template <class Type>
331void LLInterpAttractor<Type>::update(const F32 time)
332{
333	if (time > this->mStartTime)
334	{
335		this->mActive = TRUE;
336	}
337	else
338	{
339		return;
340	}
341	if (time > this->mEndTime)
342	{
343		this->mDone = TRUE;
344		return;
345	}
346
347	F32 dt = time - this->mCurTime;
348	Type dist_val = this->mEndVal - this->mCurVal;
349	Type dv = 0.5*dt*dt*this->mForce*dist_val;
350	this->mVelocity += dv;
351	this->mCurVal += this->mVelocity * dt;
352	this->mCurTime = time;
353}
354
355
356//////////////////////////////
357//
358// LLInterpFucn derived class implementation.
359//
360
361
362template <class Type>
363LLInterpFunc<Type>::LLInterpFunc() : LLInterp<Type>()
364{
365	mFunc = NULL;
366	mData = NULL;
367}
368
369template <class Type>
370void LLInterpFunc<Type>::setFunc(Type (*func)(const F32, void *data), void *data)
371{
372	mFunc = func;
373	mData = data;
374}
375
376template <class Type>
377void LLInterpFunc<Type>::update(const F32 time)
378{
379	if (time > this->mStartTime)
380	{
381		this->mActive = TRUE;
382	}
383	else
384	{
385		return;
386	}
387	if (time > this->mEndTime)
388	{
389		this->mDone = TRUE;
390		return;
391	}
392
393	this->mCurVal = (*mFunc)(time - this->mStartTime, mData);
394	this->mCurTime = time;
395}
396
397//////////////////////////////
398//
399// LLInterpExp derived class implementation.
400//
401
402template <class Type>
403void LLInterpExp<Type>::update(const F32 time)
404{
405	F32 target_frac = (time - this->mStartTime) / this->mDuration;
406	if (target_frac >= 0.f)
407	{
408		this->mActive = TRUE;
409	}
410	
411	if (target_frac > 1.f)
412	{
413		this->mCurVal = this->mEndVal;
414		this->mCurFrac = 1.f;
415		this->mCurTime = time;
416		this->mDone = TRUE;
417		return;
418	}
419
420	this->mCurFrac = 1.f - (F32)(exp(-2.f*target_frac));
421	this->mCurVal = this->mStartVal + this->mCurFrac * (this->mEndVal - this->mStartVal);
422	this->mCurTime = time;
423}
424
425#endif // LL_LLINTERP_H
426