Sampler 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* -*-c++-*-
  2. * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
  3. *
  4. * This library is open source and may be redistributed and/or modified under
  5. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  6. * (at your option) any later version. The full license is in LICENSE file
  7. * included with this distribution, and on the openscenegraph.org website.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * OpenSceneGraph Public License for more details.
  13. *
  14. * Authors:
  15. * Cedric Pinson <cedric.pinson@plopbyte.net>
  16. * Michael Platings <mplatings@pixelpower.com>
  17. */
  18. #ifndef OSGANIMATION_SAMPLER
  19. #define OSGANIMATION_SAMPLER 1
  20. #include <vector>
  21. #include <iostream>
  22. #include <osg/Referenced>
  23. #include <osg/ref_ptr>
  24. #include <osgAnimation/Keyframe>
  25. #include <osgAnimation/Interpolator>
  26. namespace osgAnimation
  27. {
  28. class Sampler : public osg::Referenced
  29. {
  30. public:
  31. virtual KeyframeContainer* getKeyframeContainer() = 0;
  32. virtual const KeyframeContainer* getKeyframeContainer() const = 0;
  33. protected:
  34. };
  35. // Sampler generic
  36. template <class F>
  37. class TemplateSampler : public Sampler
  38. {
  39. public:
  40. typedef typename F::KeyframeType KeyframeType;
  41. typedef TemplateKeyframeContainer<KeyframeType> KeyframeContainerType;
  42. typedef typename F::UsingType UsingType;
  43. typedef F FunctorType;
  44. TemplateSampler() {}
  45. ~TemplateSampler() {}
  46. void getValueAt(double time, UsingType& result) const { _functor.getValue(*_keyframes, time, result);}
  47. void setKeyframeContainer(KeyframeContainerType* kf) { _keyframes = kf;}
  48. virtual KeyframeContainer* getKeyframeContainer() { return _keyframes.get(); }
  49. virtual const KeyframeContainer* getKeyframeContainer() const { return _keyframes.get();}
  50. KeyframeContainerType* getKeyframeContainerTyped() { return _keyframes.get();}
  51. const KeyframeContainerType* getKeyframeContainerTyped() const { return _keyframes.get();}
  52. KeyframeContainerType* getOrCreateKeyframeContainer()
  53. {
  54. if (_keyframes != 0)
  55. return _keyframes.get();
  56. _keyframes = new KeyframeContainerType;
  57. return _keyframes.get();
  58. }
  59. double getStartTime() const
  60. {
  61. if (!_keyframes || _keyframes->empty())
  62. return 0.0;
  63. return _keyframes->front().getTime();
  64. }
  65. double getEndTime() const
  66. {
  67. if (!_keyframes || _keyframes->empty())
  68. return 0.0;
  69. return _keyframes->back().getTime();
  70. }
  71. protected:
  72. FunctorType _functor;
  73. osg::ref_ptr<KeyframeContainerType> _keyframes;
  74. };
  75. template<typename VALUESAMPLERTYPE, typename TIMESAMPLERTYPE>
  76. class TemplateCompositeSampler : public osg::Referenced
  77. {
  78. VALUESAMPLERTYPE& _value;
  79. TIMESAMPLERTYPE& _time;
  80. public:
  81. typedef typename VALUESAMPLERTYPE::FunctorType::UsingType UsingType;
  82. typedef typename VALUESAMPLERTYPE::FunctorType::KeyframeType KeyframeType;
  83. TemplateCompositeSampler(VALUESAMPLERTYPE& value, TIMESAMPLERTYPE& time) : _value(value), _time(time)
  84. {
  85. }
  86. void getValueAt(double time, typename VALUESAMPLERTYPE::FunctorType::UsingType& result)
  87. {
  88. double newtime;
  89. _time.getValueAt(time, newtime);
  90. _value.getValueAt(newtime, result);
  91. }
  92. float getStartTime() const {return _time.getStartTime(); }
  93. float getEndTime() const {return _time.getEndTime();}
  94. };
  95. typedef TemplateSampler<DoubleStepInterpolator> DoubleStepSampler;
  96. typedef TemplateSampler<FloatStepInterpolator> FloatStepSampler;
  97. typedef TemplateSampler<Vec2StepInterpolator> Vec2StepSampler;
  98. typedef TemplateSampler<Vec3StepInterpolator> Vec3StepSampler;
  99. typedef TemplateSampler<Vec4StepInterpolator> Vec4StepSampler;
  100. typedef TemplateSampler<QuatStepInterpolator> QuatStepSampler;
  101. typedef TemplateSampler<DoubleLinearInterpolator> DoubleLinearSampler;
  102. typedef TemplateSampler<FloatLinearInterpolator> FloatLinearSampler;
  103. typedef TemplateSampler<Vec2LinearInterpolator> Vec2LinearSampler;
  104. typedef TemplateSampler<Vec3LinearInterpolator> Vec3LinearSampler;
  105. typedef TemplateSampler<Vec4LinearInterpolator> Vec4LinearSampler;
  106. typedef TemplateSampler<QuatSphericalLinearInterpolator> QuatSphericalLinearSampler;
  107. typedef TemplateSampler<MatrixLinearInterpolator> MatrixLinearSampler;
  108. typedef TemplateSampler<FloatCubicBezierInterpolator> FloatCubicBezierSampler;
  109. typedef TemplateSampler<DoubleCubicBezierInterpolator> DoubleCubicBezierSampler;
  110. typedef TemplateSampler<Vec2CubicBezierInterpolator> Vec2CubicBezierSampler;
  111. typedef TemplateSampler<Vec3CubicBezierInterpolator> Vec3CubicBezierSampler;
  112. typedef TemplateSampler<Vec4CubicBezierInterpolator> Vec4CubicBezierSampler;
  113. }
  114. #endif