Keyframe 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* -*-c++-*-
  2. * Copyright (C) 2008 Cedric Pinson <mornifle@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. #ifndef OSGANIMATION_KEYFRAME_H
  15. #define OSGANIMATION_KEYFRAME_H
  16. #include <string>
  17. #include <osg/Referenced>
  18. #include <osg/MixinVector>
  19. #include <osgAnimation/Vec3Packed>
  20. #include <osgAnimation/CubicBezier>
  21. #include <osg/Quat>
  22. #include <osg/Vec4>
  23. #include <osg/Vec3>
  24. #include <osg/Vec2>
  25. #include <osg/Vec3us>
  26. #include <osg/Matrixf>
  27. namespace osgAnimation
  28. {
  29. class Keyframe
  30. {
  31. public:
  32. double getTime() const { return _time; }
  33. void setTime(double time) { _time = time; }
  34. protected:
  35. double _time;
  36. };
  37. template <class T>
  38. class TemplateKeyframe : public Keyframe
  39. {
  40. protected:
  41. T _value;
  42. public:
  43. typedef T value_type;
  44. TemplateKeyframe () {}
  45. ~TemplateKeyframe () {}
  46. TemplateKeyframe (double time, const T& value)
  47. {
  48. _time = time;
  49. _value = value;
  50. }
  51. void setValue(const T& value) { _value = value;}
  52. const T& getValue() const { return _value;}
  53. };
  54. class KeyframeContainer : public osg::Referenced
  55. {
  56. public:
  57. KeyframeContainer() {}
  58. virtual unsigned int size() const = 0;
  59. virtual unsigned int linearInterpolationDeduplicate() = 0;
  60. protected:
  61. ~KeyframeContainer() {}
  62. std::string _name;
  63. };
  64. template <class T>
  65. class TemplateKeyframeContainer : public osg::MixinVector<TemplateKeyframe<T> >, public KeyframeContainer
  66. {
  67. public:
  68. // const char* getKeyframeType() { return #T ;}
  69. TemplateKeyframeContainer() {}
  70. typedef TemplateKeyframe<T> KeyType;
  71. typedef typename osg::MixinVector< TemplateKeyframe<T> > VectorType;
  72. virtual unsigned int size() const { return (unsigned int)osg::MixinVector<TemplateKeyframe<T> >::size(); }
  73. virtual unsigned int linearInterpolationDeduplicate() {
  74. if(size() <= 1) {
  75. return 0;
  76. }
  77. typename VectorType::iterator keyframe = VectorType::begin(),
  78. previous = VectorType::begin();
  79. // 1. find number of consecutives identical keyframes
  80. std::vector<unsigned int> intervalSizes;
  81. unsigned int intervalSize = 1;
  82. for(++ keyframe ; keyframe != VectorType::end() ; ++ keyframe, ++ previous, ++ intervalSize) {
  83. if(!(previous->getValue() == keyframe->getValue())) {
  84. intervalSizes.push_back(intervalSize);
  85. intervalSize = 0;
  86. }
  87. }
  88. intervalSizes.push_back(intervalSize);
  89. // 2. build deduplicated list of keyframes
  90. unsigned int cumul = 0;
  91. VectorType deduplicated;
  92. for(std::vector<unsigned int>::iterator iterator = intervalSizes.begin() ; iterator != intervalSizes.end() ; ++ iterator) {
  93. deduplicated.push_back((*this)[cumul]);
  94. if(*iterator > 1) {
  95. deduplicated.push_back((*this)[cumul + (*iterator) - 1]);
  96. }
  97. cumul += *iterator;
  98. }
  99. unsigned int count = size() - deduplicated.size();
  100. this->swap(deduplicated);
  101. return count;
  102. }
  103. };
  104. template <>
  105. class TemplateKeyframeContainer<Vec3Packed> : public osg::MixinVector<TemplateKeyframe<Vec3Packed> >, public KeyframeContainer
  106. {
  107. public:
  108. typedef TemplateKeyframe<Vec3Packed> KeyType;
  109. TemplateKeyframeContainer() {}
  110. const char* getKeyframeType() { return "Vec3Packed" ;}
  111. void init(const osg::Vec3f& min, const osg::Vec3f& scale) { _min = min; _scale = scale; }
  112. osg::Vec3f _min;
  113. osg::Vec3f _scale;
  114. };
  115. typedef TemplateKeyframe<float> FloatKeyframe;
  116. typedef TemplateKeyframeContainer<float> FloatKeyframeContainer;
  117. typedef TemplateKeyframe<double> DoubleKeyframe;
  118. typedef TemplateKeyframeContainer<double> DoubleKeyframeContainer;
  119. typedef TemplateKeyframe<osg::Vec2> Vec2Keyframe;
  120. typedef TemplateKeyframeContainer<osg::Vec2> Vec2KeyframeContainer;
  121. typedef TemplateKeyframe<osg::Vec3> Vec3Keyframe;
  122. typedef TemplateKeyframeContainer<osg::Vec3> Vec3KeyframeContainer;
  123. typedef TemplateKeyframe<osg::Vec3us> Vec3usKeyframe;
  124. typedef TemplateKeyframeContainer<osg::Vec3us> Vec3usKeyframeContainer;
  125. typedef TemplateKeyframe<osg::Vec4> Vec4Keyframe;
  126. typedef TemplateKeyframeContainer<osg::Vec4> Vec4KeyframeContainer;
  127. typedef TemplateKeyframe<osg::Quat> QuatKeyframe;
  128. typedef TemplateKeyframeContainer<osg::Quat> QuatKeyframeContainer;
  129. typedef TemplateKeyframe<osg::Vec3us> Vec3usKeyframe;
  130. typedef TemplateKeyframeContainer<osg::Vec3us> Vec3usKeyframeContainer;
  131. typedef TemplateKeyframe<osg::Matrixf> MatrixKeyframe;
  132. typedef TemplateKeyframeContainer<osg::Matrixf> MatrixKeyframeContainer;
  133. typedef TemplateKeyframe<Vec3Packed> Vec3PackedKeyframe;
  134. typedef TemplateKeyframeContainer<Vec3Packed> Vec3PackedKeyframeContainer;
  135. typedef TemplateKeyframe<FloatCubicBezier> FloatCubicBezierKeyframe;
  136. typedef TemplateKeyframeContainer<FloatCubicBezier> FloatCubicBezierKeyframeContainer;
  137. typedef TemplateKeyframe<DoubleCubicBezier> DoubleCubicBezierKeyframe;
  138. typedef TemplateKeyframeContainer<DoubleCubicBezier> DoubleCubicBezierKeyframeContainer;
  139. typedef TemplateKeyframe<Vec2CubicBezier> Vec2CubicBezierKeyframe;
  140. typedef TemplateKeyframeContainer<Vec2CubicBezier> Vec2CubicBezierKeyframeContainer;
  141. typedef TemplateKeyframe<Vec3CubicBezier> Vec3CubicBezierKeyframe;
  142. typedef TemplateKeyframeContainer<Vec3CubicBezier> Vec3CubicBezierKeyframeContainer;
  143. typedef TemplateKeyframe<Vec4CubicBezier> Vec4CubicBezierKeyframe;
  144. typedef TemplateKeyframeContainer<Vec4CubicBezier> Vec4CubicBezierKeyframeContainer;
  145. }
  146. #endif