Interpolator 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. //osgParticle - Copyright (C) 2002 Marco Jez
  14. #ifndef OSGPARTICLE_INTERPOLATOR
  15. #define OSGPARTICLE_INTERPOLATOR
  16. #include <osgParticle/range>
  17. #include <osg/CopyOp>
  18. #include <osg/Object>
  19. #include <osg/Vec2>
  20. #include <osg/Vec3>
  21. #include <osg/Vec4>
  22. namespace osgParticle
  23. {
  24. /// An abstract base class for implementing interpolators.
  25. class Interpolator : public osg::Object {
  26. public:
  27. Interpolator()
  28. : osg::Object() {}
  29. Interpolator(const Interpolator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
  30. : osg::Object(copy, copyop) {}
  31. virtual const char* libraryName() const { return "osgParticle"; }
  32. virtual const char* className() const { return "Interpolator"; }
  33. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Interpolator* >(obj) != 0; }
  34. /// Interpolate between floats. Must be overridden in descendant classes.
  35. virtual float interpolate(float t, float y1, float y2) const = 0;
  36. /// Interpolate between 2-dimensional vectors. Default behavior is to interpolate each component separately.
  37. virtual osg::Vec2 interpolate(float t, const osg::Vec2& y1, const osg::Vec2& y2) const
  38. {
  39. return osg::Vec2(
  40. interpolate(t, y1.x(), y2.x()),
  41. interpolate(t, y1.y(), y2.y())
  42. );
  43. }
  44. /// Interpolate between 3-dimensional vectors. Default behavior is to interpolate each component separately.
  45. virtual osg::Vec3 interpolate(float t, const osg::Vec3& y1, const osg::Vec3& y2) const
  46. {
  47. return osg::Vec3(
  48. interpolate(t, y1.x(), y2.x()),
  49. interpolate(t, y1.y(), y2.y()),
  50. interpolate(t, y1.z(), y2.z())
  51. );
  52. }
  53. /// Interpolate between 4-dimensional vectors. Default behavior is to interpolate each component separately.
  54. virtual osg::Vec4 interpolate(float t, const osg::Vec4& y1, const osg::Vec4& y2) const
  55. {
  56. return osg::Vec4(
  57. interpolate(t, y1.x(), y2.x()),
  58. interpolate(t, y1.y(), y2.y()),
  59. interpolate(t, y1.z(), y2.z()),
  60. interpolate(t, y1.w(), y2.w())
  61. );
  62. }
  63. template<class ValueType>
  64. ValueType interpolate(float t, const range<ValueType>& r) const
  65. {
  66. return interpolate(t, r.minimum, r.maximum);
  67. }
  68. protected:
  69. virtual ~Interpolator() {}
  70. };
  71. }
  72. #endif