aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/llinterp.h
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmath/llinterp.h
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llmath/llinterp.h')
-rw-r--r--linden/indra/llmath/llinterp.h419
1 files changed, 419 insertions, 0 deletions
diff --git a/linden/indra/llmath/llinterp.h b/linden/indra/llmath/llinterp.h
new file mode 100644
index 0000000..3cc0b18
--- /dev/null
+++ b/linden/indra/llmath/llinterp.h
@@ -0,0 +1,419 @@
1/**
2 * @file llinterp.h
3 *
4 * Copyright (c) 2001-2007, Linden Research, Inc.
5 *
6 * The source code in this file ("Source Code") is provided by Linden Lab
7 * to you under the terms of the GNU General Public License, version 2.0
8 * ("GPL"), unless you have obtained a separate licensing agreement
9 * ("Other License"), formally executed by you and Linden Lab. Terms of
10 * the GPL can be found in doc/GPL-license.txt in this distribution, or
11 * online at http://secondlife.com/developers/opensource/gplv2
12 *
13 * There are special exceptions to the terms and conditions of the GPL as
14 * it is applied to this Source Code. View the full text of the exception
15 * in the file doc/FLOSS-exception.txt in this software distribution, or
16 * online at http://secondlife.com/developers/opensource/flossexception
17 *
18 * By copying, modifying or distributing this software, you acknowledge
19 * that you have read and understood your obligations described above,
20 * and agree to abide by those obligations.
21 *
22 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
23 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
24 * COMPLETENESS OR PERFORMANCE.
25 */
26
27#ifndef LL_LLINTERP_H
28#define LL_LLINTERP_H
29
30#include "math.h"
31
32// Class from which different types of interpolators can be derived
33
34class LLInterpVal
35{
36public:
37 virtual ~LLInterpVal() {}
38 virtual void interp(LLInterpVal &target, const F32 frac); // Linear interpolation for each type
39};
40
41template <typename Type>
42class LLInterp
43{
44public:
45 LLInterp();
46 virtual ~LLInterp() {}
47
48 virtual void start();
49 void update(const F32 time);
50 const Type &getCurVal() const;
51
52 void setStartVal(const Type &start_val);
53 const Type &getStartVal() const;
54
55 void setEndVal(const Type &target_val);
56 const Type &getEndVal() const;
57
58 void setStartTime(const F32 time);
59 F32 getStartTime() const;
60
61 void setEndTime(const F32 time);
62 F32 getEndTime() const;
63
64 BOOL isActive() const;
65 BOOL isDone() const;
66
67protected:
68 F32 mStartTime;
69 F32 mEndTime;
70 F32 mDuration;
71 BOOL mActive;
72 BOOL mDone;
73
74 Type mStartVal;
75 Type mEndVal;
76
77 F32 mCurTime;
78 Type mCurVal;
79};
80
81template <typename Type>
82class LLInterpLinear : public LLInterp<Type>
83{
84public:
85 /*virtual*/ void start();
86 void update(const F32 time);
87 F32 getCurFrac() const;
88protected:
89 F32 mCurFrac;
90};
91
92template <typename Type>
93class LLInterpExp : public LLInterpLinear<Type>
94{
95public:
96 void update(const F32 time);
97protected:
98};
99
100template <typename Type>
101class LLInterpAttractor : public LLInterp<Type>
102{
103public:
104 LLInterpAttractor();
105 /*virtual*/ void start();
106 void setStartVel(const Type &vel);
107 void setForce(const F32 force);
108 void update(const F32 time);
109protected:
110 F32 mForce;
111 Type mStartVel;
112 Type mVelocity;
113};
114
115template <typename Type>
116class LLInterpFunc : public LLInterp<Type>
117{
118public:
119 LLInterpFunc();
120 void update(const F32 time);
121
122 void setFunc(Type (*)(const F32, void *data), void *data);
123protected:
124 Type (*mFunc)(const F32 time, void *data);
125 void *mData;
126};
127
128
129///////////////////////////////////
130//
131// Implementation
132//
133//
134
135/////////////////////////////////
136//
137// LLInterp base class implementation
138//
139
140template <typename Type>
141LLInterp<Type>::LLInterp()
142{
143 mStartTime = 0.f;
144 mEndTime = 1.f;
145 mDuration = 1.f;
146 mCurTime = 0.f;
147 mDone = FALSE;
148 mActive = FALSE;
149}
150
151template <class Type>
152void LLInterp<Type>::setStartVal(const Type &start_val)
153{
154 mStartVal = start_val;
155}
156
157template <class Type>
158void LLInterp<Type>::start()
159{
160 mCurVal = mStartVal;
161 mCurTime = mStartTime;
162 mDone = FALSE;
163 mActive = FALSE;
164}
165
166template <class Type>
167const Type &LLInterp<Type>::getStartVal() const
168{
169 return mStartVal;
170}
171
172template <class Type>
173void LLInterp<Type>::setEndVal(const Type &end_val)
174{
175 mEndVal = end_val;
176}
177
178template <class Type>
179const Type &LLInterp<Type>::getEndVal() const
180{
181 return mEndVal;
182}
183
184template <class Type>
185const Type &LLInterp<Type>::getCurVal() const
186{
187 return mCurVal;
188}
189
190
191template <class Type>
192void LLInterp<Type>::setStartTime(const F32 start_time)
193{
194 mStartTime = start_time;
195 mDuration = mEndTime - mStartTime;
196}
197
198template <class Type>
199F32 LLInterp<Type>::getStartTime() const
200{
201 return mStartTime;
202}
203
204
205template <class Type>
206void LLInterp<Type>::setEndTime(const F32 end_time)
207{
208 mEndTime = end_time;
209 mDuration = mEndTime - mStartTime;
210}
211
212
213template <class Type>
214F32 LLInterp<Type>::getEndTime() const
215{
216 return mEndTime;
217}
218
219
220template <class Type>
221BOOL LLInterp<Type>::isDone() const
222{
223 return mDone;
224}
225
226template <class Type>
227BOOL LLInterp<Type>::isActive() const
228{
229 return mActive;
230}
231
232//////////////////////////////
233//
234// LLInterpLinear derived class implementation.
235//
236template <typename Type>
237void LLInterpLinear<Type>::start()
238{
239 LLInterp<Type>::start();
240 mCurFrac = 0.f;
241}
242
243template <typename Type>
244void LLInterpLinear<Type>::update(const F32 time)
245{
246 F32 target_frac = (time - this->mStartTime) / this->mDuration;
247 F32 dfrac = target_frac - this->mCurFrac;
248 if (target_frac >= 0.f)
249 {
250 this->mActive = TRUE;
251 }
252
253 if (target_frac > 1.f)
254 {
255 this->mCurVal = this->mEndVal;
256 this->mCurFrac = 1.f;
257 this->mCurTime = time;
258 this->mDone = TRUE;
259 return;
260 }
261
262 target_frac = llmin(1.f, target_frac);
263 target_frac = llmax(0.f, target_frac);
264
265 if (dfrac >= 0.f)
266 {
267 F32 total_frac = 1.f - this->mCurFrac;
268 F32 inc_frac = dfrac / total_frac;
269 this->mCurVal = inc_frac * this->mEndVal + (1.f - inc_frac) * this->mCurVal;
270 this->mCurTime = time;
271 }
272 else
273 {
274 F32 total_frac = this->mCurFrac - 1.f;
275 F32 inc_frac = dfrac / total_frac;
276 this->mCurVal = inc_frac * this->mStartVal + (1.f - inc_frac) * this->mCurVal;
277 this->mCurTime = time;
278 }
279 mCurFrac = target_frac;
280}
281
282template <class Type>
283F32 LLInterpLinear<Type>::getCurFrac() const
284{
285 return mCurFrac;
286}
287
288
289//////////////////////////////
290//
291// LLInterpAttractor derived class implementation.
292//
293
294
295template <class Type>
296LLInterpAttractor<Type>::LLInterpAttractor() : LLInterp<Type>()
297{
298 mForce = 0.1f;
299 mVelocity *= 0.f;
300 mStartVel *= 0.f;
301}
302
303template <class Type>
304void LLInterpAttractor<Type>::start()
305{
306 LLInterp<Type>::start();
307 mVelocity = mStartVel;
308}
309
310
311template <class Type>
312void LLInterpAttractor<Type>::setStartVel(const Type &vel)
313{
314 mStartVel = vel;
315}
316
317template <class Type>
318void LLInterpAttractor<Type>::setForce(const F32 force)
319{
320 mForce = force;
321}
322
323template <class Type>
324void LLInterpAttractor<Type>::update(const F32 time)
325{
326 if (time > this->mStartTime)
327 {
328 this->mActive = TRUE;
329 }
330 else
331 {
332 return;
333 }
334 if (time > this->mEndTime)
335 {
336 this->mDone = TRUE;
337 return;
338 }
339
340 F32 dt = time - this->mCurTime;
341 Type dist_val = this->mEndVal - this->mCurVal;
342 Type dv = 0.5*dt*dt*this->mForce*dist_val;
343 this->mVelocity += dv;
344 this->mCurVal += this->mVelocity * dt;
345 this->mCurTime = time;
346}
347
348
349//////////////////////////////
350//
351// LLInterpFucn derived class implementation.
352//
353
354
355template <class Type>
356LLInterpFunc<Type>::LLInterpFunc() : LLInterp<Type>()
357{
358 mFunc = NULL;
359 mData = NULL;
360}
361
362template <class Type>
363void LLInterpFunc<Type>::setFunc(Type (*func)(const F32, void *data), void *data)
364{
365 mFunc = func;
366 mData = data;
367}
368
369template <class Type>
370void LLInterpFunc<Type>::update(const F32 time)
371{
372 if (time > this->mStartTime)
373 {
374 this->mActive = TRUE;
375 }
376 else
377 {
378 return;
379 }
380 if (time > this->mEndTime)
381 {
382 this->mDone = TRUE;
383 return;
384 }
385
386 this->mCurVal = (*mFunc)(time - this->mStartTime, mData);
387 this->mCurTime = time;
388}
389
390//////////////////////////////
391//
392// LLInterpExp derived class implementation.
393//
394
395template <class Type>
396void LLInterpExp<Type>::update(const F32 time)
397{
398 F32 target_frac = (time - this->mStartTime) / this->mDuration;
399 if (target_frac >= 0.f)
400 {
401 this->mActive = TRUE;
402 }
403
404 if (target_frac > 1.f)
405 {
406 this->mCurVal = this->mEndVal;
407 this->mCurFrac = 1.f;
408 this->mCurTime = time;
409 this->mDone = TRUE;
410 return;
411 }
412
413 this->mCurFrac = 1.f - (F32)(exp(-2.f*target_frac));
414 this->mCurVal = this->mStartVal + this->mCurFrac * (this->mEndVal - this->mStartVal);
415 this->mCurTime = time;
416}
417
418#endif // LL_LLINTERP_H
419