00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00021
00022
00023 #ifndef H_SPK_INTERPOLATOR
00024 #define H_SPK_INTERPOLATOR
00025
00026 #include "Core/SPK_DEF.h"
00027
00028 namespace SPK
00029 {
00030 class Particle;
00031
00037 enum InterpolationType
00038 {
00039 INTERPOLATOR_LIFETIME,
00040 INTERPOLATOR_AGE,
00041 INTERPOLATOR_PARAM,
00042 INTERPOLATOR_VELOCITY,
00043 };
00044
00052 struct InterpolatorEntry
00053 {
00054 float x;
00055 float y0;
00056 float y1;
00059 InterpolatorEntry() : x(0.0f),y0(0.0f),y1(0.0f) {}
00060
00066 InterpolatorEntry(float x,float y) : x(x),y0(y),y1(y) {}
00067
00074 InterpolatorEntry(float x,float y0,float y1) : x(x),y0(y0),y1(y1) {}
00075
00076
00077 InterpolatorEntry(float x) : x(x) {}
00078 };
00079
00080
00081 bool operator<(const InterpolatorEntry& entry0,const InterpolatorEntry& entry1);
00082
00130 class SPK_PREFIX Interpolator
00131 {
00132 friend class Particle;
00133 friend class Model;
00134
00135 public :
00136
00138
00140
00150 void setType(InterpolationType type,ModelParam param = PARAM_SIZE);
00151
00162 void enableLooping(bool loop);
00163
00171 void setScaleXVariation(float scaleXVariation);
00172
00180 void setOffsetXVariation(float offsetXVariation);
00181
00183
00185
00190 InterpolationType getType() const;
00191
00199 ModelParam getInterpolatorParam() const;
00200
00205 bool isLoopingEnabled() const;
00206
00211 float getScaleXVariation() const;
00212
00217 float getOffsetXVariation() const;
00218
00223 std::set<InterpolatorEntry>& getGraph();
00224
00229 const std::set<InterpolatorEntry>& getGraph() const;
00230
00232
00234
00240 bool addEntry(const InterpolatorEntry& entry);
00241
00248 bool addEntry(float x,float y);
00249
00257 bool addEntry(float x,float y0,float y1);
00258
00260 void clearGraph();
00261
00267 void generateSinCurve(float period,float amplitudeMin,float amplitudeMax,float offsetX,float offsetY,float startX,unsigned int length,unsigned int nbSamples);
00268
00274 void generatePolyCurve(float constant,float linear,float quadratic,float cubic,float startX,float endX,unsigned int nbSamples);
00275
00276 private :
00277
00278 std::set<InterpolatorEntry> graph;
00279
00280 InterpolationType type;
00281 ModelParam param;
00282 bool loopingEnabled;
00283
00284 float scaleXVariation;
00285 float offsetXVariation;
00286
00287 float interpolate(const Particle& particle,ModelParam interpolatedParam,float ratioY,float offsetX,float scaleX);
00288 float interpolateY(const InterpolatorEntry& entry,float ratio);
00289
00290
00291 typedef float (Interpolator::*computeXFn)(const Particle&) const;
00292 static computeXFn COMPUTE_X_FN[4];
00293
00294 float computeXLifeTime(const Particle& particle) const;
00295 float computeXAge(const Particle& particle) const;
00296 float computeXParam(const Particle& particle) const;
00297 float computeXVelocity(const Particle& particle) const;
00298
00299
00300 Interpolator();
00301 ~Interpolator() {};
00302 };
00303
00304
00305 inline void Interpolator::setType(InterpolationType type,ModelParam param)
00306 {
00307 this->type = type;
00308 this->param = param;
00309 }
00310
00311 inline void Interpolator::enableLooping(bool loop)
00312 {
00313 loopingEnabled = loop;
00314 }
00315
00316 inline void Interpolator::setScaleXVariation(float scaleXVariation)
00317 {
00318 this->scaleXVariation = scaleXVariation;
00319 }
00320
00321 inline void Interpolator::setOffsetXVariation(float offsetXVariation)
00322 {
00323 this->offsetXVariation = offsetXVariation;
00324 }
00325
00326 inline InterpolationType Interpolator::getType() const
00327 {
00328 return type;
00329 }
00330
00331 inline ModelParam Interpolator::getInterpolatorParam() const
00332 {
00333 return param;
00334 }
00335
00336 inline bool Interpolator::isLoopingEnabled() const
00337 {
00338 return loopingEnabled;
00339 }
00340
00341 inline float Interpolator::getScaleXVariation() const
00342 {
00343 return scaleXVariation;
00344 }
00345
00346 inline float Interpolator::getOffsetXVariation() const
00347 {
00348 return offsetXVariation;
00349 }
00350
00351 inline std::set<InterpolatorEntry>& Interpolator::getGraph()
00352 {
00353 return graph;
00354 }
00355
00356 inline const std::set<InterpolatorEntry>& Interpolator::getGraph() const
00357 {
00358 return graph;
00359 }
00360
00361 inline bool Interpolator::addEntry(const InterpolatorEntry& entry)
00362 {
00363 return graph.insert(entry).second;
00364 }
00365
00366 inline bool Interpolator::addEntry(float x,float y)
00367 {
00368 return addEntry(InterpolatorEntry(x,y));
00369 }
00370
00371 inline bool Interpolator::addEntry(float x,float y0,float y1)
00372 {
00373 return addEntry(InterpolatorEntry(x,y0,y1));
00374 }
00375
00376 inline void Interpolator::clearGraph()
00377 {
00378 graph.clear();
00379 }
00380
00381 inline float Interpolator::interpolateY(const InterpolatorEntry& entry,float ratio)
00382 {
00383 return entry.y0 + (entry.y1 - entry.y0) * ratio;
00384 }
00385
00387
00389
00390 inline bool operator<(const InterpolatorEntry& entry0,const InterpolatorEntry& entry1)
00391 {
00392 return entry0.x < entry1.x;
00393 }
00394
00395 inline bool operator==(const InterpolatorEntry& entry0,const InterpolatorEntry& entry1)
00396 {
00397 return entry0.x == entry1.x;
00398 }
00399
00400 }
00401
00402 #endif
00403