diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmath/llinterp.h | |
parent | README.txt (diff) | |
download | meta-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.h | 419 |
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 | |||
34 | class LLInterpVal | ||
35 | { | ||
36 | public: | ||
37 | virtual ~LLInterpVal() {} | ||
38 | virtual void interp(LLInterpVal &target, const F32 frac); // Linear interpolation for each type | ||
39 | }; | ||
40 | |||
41 | template <typename Type> | ||
42 | class LLInterp | ||
43 | { | ||
44 | public: | ||
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 | |||
67 | protected: | ||
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 | |||
81 | template <typename Type> | ||
82 | class LLInterpLinear : public LLInterp<Type> | ||
83 | { | ||
84 | public: | ||
85 | /*virtual*/ void start(); | ||
86 | void update(const F32 time); | ||
87 | F32 getCurFrac() const; | ||
88 | protected: | ||
89 | F32 mCurFrac; | ||
90 | }; | ||
91 | |||
92 | template <typename Type> | ||
93 | class LLInterpExp : public LLInterpLinear<Type> | ||
94 | { | ||
95 | public: | ||
96 | void update(const F32 time); | ||
97 | protected: | ||
98 | }; | ||
99 | |||
100 | template <typename Type> | ||
101 | class LLInterpAttractor : public LLInterp<Type> | ||
102 | { | ||
103 | public: | ||
104 | LLInterpAttractor(); | ||
105 | /*virtual*/ void start(); | ||
106 | void setStartVel(const Type &vel); | ||
107 | void setForce(const F32 force); | ||
108 | void update(const F32 time); | ||
109 | protected: | ||
110 | F32 mForce; | ||
111 | Type mStartVel; | ||
112 | Type mVelocity; | ||
113 | }; | ||
114 | |||
115 | template <typename Type> | ||
116 | class LLInterpFunc : public LLInterp<Type> | ||
117 | { | ||
118 | public: | ||
119 | LLInterpFunc(); | ||
120 | void update(const F32 time); | ||
121 | |||
122 | void setFunc(Type (*)(const F32, void *data), void *data); | ||
123 | protected: | ||
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 | |||
140 | template <typename Type> | ||
141 | LLInterp<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 | |||
151 | template <class Type> | ||
152 | void LLInterp<Type>::setStartVal(const Type &start_val) | ||
153 | { | ||
154 | mStartVal = start_val; | ||
155 | } | ||
156 | |||
157 | template <class Type> | ||
158 | void LLInterp<Type>::start() | ||
159 | { | ||
160 | mCurVal = mStartVal; | ||
161 | mCurTime = mStartTime; | ||
162 | mDone = FALSE; | ||
163 | mActive = FALSE; | ||
164 | } | ||
165 | |||
166 | template <class Type> | ||
167 | const Type &LLInterp<Type>::getStartVal() const | ||
168 | { | ||
169 | return mStartVal; | ||
170 | } | ||
171 | |||
172 | template <class Type> | ||
173 | void LLInterp<Type>::setEndVal(const Type &end_val) | ||
174 | { | ||
175 | mEndVal = end_val; | ||
176 | } | ||
177 | |||
178 | template <class Type> | ||
179 | const Type &LLInterp<Type>::getEndVal() const | ||
180 | { | ||
181 | return mEndVal; | ||
182 | } | ||
183 | |||
184 | template <class Type> | ||
185 | const Type &LLInterp<Type>::getCurVal() const | ||
186 | { | ||
187 | return mCurVal; | ||
188 | } | ||
189 | |||
190 | |||
191 | template <class Type> | ||
192 | void LLInterp<Type>::setStartTime(const F32 start_time) | ||
193 | { | ||
194 | mStartTime = start_time; | ||
195 | mDuration = mEndTime - mStartTime; | ||
196 | } | ||
197 | |||
198 | template <class Type> | ||
199 | F32 LLInterp<Type>::getStartTime() const | ||
200 | { | ||
201 | return mStartTime; | ||
202 | } | ||
203 | |||
204 | |||
205 | template <class Type> | ||
206 | void LLInterp<Type>::setEndTime(const F32 end_time) | ||
207 | { | ||
208 | mEndTime = end_time; | ||
209 | mDuration = mEndTime - mStartTime; | ||
210 | } | ||
211 | |||
212 | |||
213 | template <class Type> | ||
214 | F32 LLInterp<Type>::getEndTime() const | ||
215 | { | ||
216 | return mEndTime; | ||
217 | } | ||
218 | |||
219 | |||
220 | template <class Type> | ||
221 | BOOL LLInterp<Type>::isDone() const | ||
222 | { | ||
223 | return mDone; | ||
224 | } | ||
225 | |||
226 | template <class Type> | ||
227 | BOOL LLInterp<Type>::isActive() const | ||
228 | { | ||
229 | return mActive; | ||
230 | } | ||
231 | |||
232 | ////////////////////////////// | ||
233 | // | ||
234 | // LLInterpLinear derived class implementation. | ||
235 | // | ||
236 | template <typename Type> | ||
237 | void LLInterpLinear<Type>::start() | ||
238 | { | ||
239 | LLInterp<Type>::start(); | ||
240 | mCurFrac = 0.f; | ||
241 | } | ||
242 | |||
243 | template <typename Type> | ||
244 | void 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 | |||
282 | template <class Type> | ||
283 | F32 LLInterpLinear<Type>::getCurFrac() const | ||
284 | { | ||
285 | return mCurFrac; | ||
286 | } | ||
287 | |||
288 | |||
289 | ////////////////////////////// | ||
290 | // | ||
291 | // LLInterpAttractor derived class implementation. | ||
292 | // | ||
293 | |||
294 | |||
295 | template <class Type> | ||
296 | LLInterpAttractor<Type>::LLInterpAttractor() : LLInterp<Type>() | ||
297 | { | ||
298 | mForce = 0.1f; | ||
299 | mVelocity *= 0.f; | ||
300 | mStartVel *= 0.f; | ||
301 | } | ||
302 | |||
303 | template <class Type> | ||
304 | void LLInterpAttractor<Type>::start() | ||
305 | { | ||
306 | LLInterp<Type>::start(); | ||
307 | mVelocity = mStartVel; | ||
308 | } | ||
309 | |||
310 | |||
311 | template <class Type> | ||
312 | void LLInterpAttractor<Type>::setStartVel(const Type &vel) | ||
313 | { | ||
314 | mStartVel = vel; | ||
315 | } | ||
316 | |||
317 | template <class Type> | ||
318 | void LLInterpAttractor<Type>::setForce(const F32 force) | ||
319 | { | ||
320 | mForce = force; | ||
321 | } | ||
322 | |||
323 | template <class Type> | ||
324 | void 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 | |||
355 | template <class Type> | ||
356 | LLInterpFunc<Type>::LLInterpFunc() : LLInterp<Type>() | ||
357 | { | ||
358 | mFunc = NULL; | ||
359 | mData = NULL; | ||
360 | } | ||
361 | |||
362 | template <class Type> | ||
363 | void LLInterpFunc<Type>::setFunc(Type (*func)(const F32, void *data), void *data) | ||
364 | { | ||
365 | mFunc = func; | ||
366 | mData = data; | ||
367 | } | ||
368 | |||
369 | template <class Type> | ||
370 | void 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 | |||
395 | template <class Type> | ||
396 | void 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 | |||