AnimationChannel.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "Base.h"
  2. #include "AnimationChannel.h"
  3. #include "Transform.h"
  4. namespace gameplay
  5. {
  6. AnimationChannel::AnimationChannel(void) :
  7. _targetAttrib(0)
  8. {
  9. }
  10. AnimationChannel::~AnimationChannel(void)
  11. {
  12. }
  13. unsigned int AnimationChannel::getTypeId(void) const
  14. {
  15. return ANIMATIONCHANNEL_ID;
  16. }
  17. const char* AnimationChannel::getElementName(void) const
  18. {
  19. return "AnimationChannel";
  20. }
  21. void AnimationChannel::writeBinary(FILE* file)
  22. {
  23. Object::writeBinary(file);
  24. write(_targetId, file);
  25. write(_targetAttrib, file);
  26. write((unsigned int)_keytimes.size(), file);
  27. for (std::vector<float>::const_iterator i = _keytimes.begin(); i != _keytimes.end(); ++i)
  28. {
  29. write((unsigned int)*i, file);
  30. }
  31. write(_keyValues, file);
  32. write(_tangentsIn, file);
  33. write(_tangentsOut, file);
  34. write(_interpolations, file);
  35. }
  36. void AnimationChannel::writeText(FILE* file)
  37. {
  38. fprintElementStart(file);
  39. fprintfElement(file, "targetId", _targetId);
  40. fprintf(file, "<%s>%u %s</%s>\n", "targetAttrib", _targetAttrib, Transform::getPropertyString(_targetAttrib), "targetAttrib");
  41. fprintfElement(file, "%f ", "keytimes", _keytimes);
  42. fprintfElement(file, "%f ", "values", _keyValues);
  43. fprintfElement(file, "%f ", "tangentsIn", _tangentsIn);
  44. fprintfElement(file, "%f ", "tangentsOut", _tangentsOut);
  45. fprintfElement(file, "%u ", "interpolations", _interpolations);
  46. fprintElementEnd(file);
  47. }
  48. void AnimationChannel::setInterpolation(unsigned int interpolation)
  49. {
  50. _interpolations.clear();
  51. _interpolations.push_back(interpolation);
  52. }
  53. const std::string& AnimationChannel::getTargetId() const
  54. {
  55. return _targetId;
  56. }
  57. unsigned int AnimationChannel::getTargetAttribute() const
  58. {
  59. return _targetAttrib;
  60. }
  61. std::vector<float>& AnimationChannel::getKeyValues()
  62. {
  63. return _keyValues;
  64. }
  65. std::vector<float>& AnimationChannel::getKeyTimes()
  66. {
  67. return _keytimes;
  68. }
  69. std::vector<float>& AnimationChannel::getTangentsIn()
  70. {
  71. return _tangentsIn;
  72. }
  73. std::vector<float>& AnimationChannel::getTangentsOut()
  74. {
  75. return _tangentsOut;
  76. }
  77. std::vector<unsigned int>& AnimationChannel::getInterpolationTypes()
  78. {
  79. return _interpolations;
  80. }
  81. void AnimationChannel::setTargetId(const std::string& str)
  82. {
  83. _targetId = str;
  84. }
  85. void AnimationChannel::setTargetAttribute(unsigned int attrib)
  86. {
  87. _targetAttrib = attrib;
  88. }
  89. void AnimationChannel::setKeyTimes(const std::vector<float>& values)
  90. {
  91. _keytimes = values;
  92. }
  93. void AnimationChannel::setKeyValues(const std::vector<float>& values)
  94. {
  95. _keyValues = values;
  96. }
  97. void AnimationChannel::setTangentsIn(const std::vector<float>& values)
  98. {
  99. _tangentsIn = values;
  100. }
  101. void AnimationChannel::setTangentsOut(const std::vector<float>& values)
  102. {
  103. _tangentsOut = values;
  104. }
  105. void AnimationChannel::setInterpolations(const std::vector<unsigned int>& values)
  106. {
  107. _interpolations = values;
  108. }
  109. void AnimationChannel::removeDuplicates()
  110. {
  111. LOG(3, " Removing duplicates for channel with target attribute: %u.\n", _targetAttrib);
  112. int startCount = _keytimes.size();
  113. size_t propSize = Transform::getPropertySize(_targetAttrib);
  114. if (propSize > 1 && !_interpolations.empty() && _interpolations[0] == LINEAR)
  115. {
  116. size_t prevIndex = 0;
  117. std::vector<float>::iterator prevStart = _keyValues.begin();
  118. std::vector<float>::iterator prevEnd = prevStart + propSize - 1;
  119. size_t i = 1;
  120. for (i = 1; i < _keytimes.size(); ++i)
  121. {
  122. std::vector<float>::iterator start = _keyValues.begin() + i * propSize;
  123. std::vector<float>::iterator end = start + propSize - 1;
  124. if (!equal(prevStart, prevEnd, start) || i == _keytimes.size() - 1)
  125. {
  126. if (i - prevIndex > 2)
  127. {
  128. deleteRange(prevIndex+1, i, propSize);
  129. i = prevIndex;
  130. prevStart = _keyValues.begin() + i * propSize;
  131. prevEnd = prevStart + propSize - 1;
  132. }
  133. else
  134. {
  135. prevStart = start;
  136. prevEnd = end;
  137. prevIndex = i;
  138. }
  139. }
  140. }
  141. if (i - 1 - prevIndex >= 2)
  142. {
  143. deleteRange(prevIndex+1, i, propSize);
  144. }
  145. }
  146. LOG(3, " Removed %d duplicate keyframes from channel.\n", startCount- _keytimes.size());
  147. }
  148. unsigned int AnimationChannel::getInterpolationType(const char* str)
  149. {
  150. unsigned int value = 0;
  151. switch (str[0])
  152. {
  153. case 'L':
  154. if (strcmp(str, "LINEAR") == 0)
  155. {
  156. value = AnimationChannel::LINEAR;
  157. }
  158. break;
  159. case 'B':
  160. if (strcmp(str, "BEZIER") == 0)
  161. {
  162. value = AnimationChannel::BEZIER;
  163. }
  164. else if (strcmp(str, "BSPLINE") == 0)
  165. {
  166. value = AnimationChannel::BSPLINE;
  167. }
  168. break;
  169. case 'C':
  170. if (strcmp(str, "CARDINAL") == 0)
  171. {
  172. value = AnimationChannel::CARDINAL;
  173. }
  174. break;
  175. case 'H':
  176. if (strcmp(str, "HERMITE") == 0)
  177. {
  178. value = AnimationChannel::HERMITE;
  179. }
  180. break;
  181. case 'S':
  182. if (strcmp(str, "STEP") == 0)
  183. {
  184. value = AnimationChannel::STEP;
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. return value;
  191. }
  192. void AnimationChannel::deleteRange(size_t begin, size_t end, size_t propSize)
  193. {
  194. assert(end > begin);
  195. // delete range
  196. LOG(4, " delete %lu to %lu\n", begin, end - 1);
  197. std::vector<float>::iterator a = _keyValues.begin() + begin * propSize;
  198. std::vector<float>::iterator b = _keyValues.begin() + end * propSize;
  199. _keyValues.erase(a, b);
  200. a = _keytimes.begin() + begin;
  201. b = _keytimes.begin() + end;
  202. _keytimes.erase(a, b);
  203. if (_interpolations.size() > 1)
  204. {
  205. std::vector<unsigned int>::iterator a = _interpolations.begin() + begin;
  206. std::vector<unsigned int>::iterator b = _interpolations.begin() + end * propSize;
  207. _interpolations.erase(a, b);
  208. }
  209. // TODO: also remove key frames from _tangentsIn and _tangentsOut once other curve types are supported.
  210. }
  211. }