TargetAnimation.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #include "TargetAnimation.h"
  35. using namespace Assimp;
  36. // ------------------------------------------------------------------------------------------------
  37. KeyIterator::KeyIterator(const std::vector<aiVectorKey>* _objPos,
  38. const std::vector<aiVectorKey>* _targetObjPos,
  39. const aiVector3D* defaultObjectPos /*= NULL*/,
  40. const aiVector3D* defaultTargetPos /*= NULL*/)
  41. : reachedEnd (false)
  42. , curTime (-1.)
  43. , objPos (_objPos)
  44. , targetObjPos (_targetObjPos)
  45. , nextObjPos (0)
  46. , nextTargetObjPos(0)
  47. {
  48. // Generate default transformation tracks if necessary
  49. if (!objPos || objPos->empty())
  50. {
  51. defaultObjPos.resize(1);
  52. defaultObjPos.front().mTime = 10e10;
  53. if (defaultObjectPos)
  54. defaultObjPos.front().mValue = *defaultObjectPos;
  55. objPos = & defaultObjPos;
  56. }
  57. if (!targetObjPos || targetObjPos->empty())
  58. {
  59. defaultTargetObjPos.resize(1);
  60. defaultTargetObjPos.front().mTime = 10e10;
  61. if (defaultTargetPos)
  62. defaultTargetObjPos.front().mValue = *defaultTargetPos;
  63. targetObjPos = & defaultTargetObjPos;
  64. }
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. template <class T>
  68. inline T Interpolate(const T& one, const T& two, float val)
  69. {
  70. return one + (two-one)*val;
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. void KeyIterator::operator ++()
  74. {
  75. // If we are already at the end of all keyframes, return
  76. if (reachedEnd)return;
  77. // Now search in all arrays for the time value closest
  78. // to our current position on the time line
  79. double d0,d1;
  80. d0 = objPos->at ( std::min ( nextObjPos, objPos->size()-1) ).mTime;
  81. d1 = targetObjPos->at( std::min ( nextTargetObjPos, targetObjPos->size()-1) ).mTime;
  82. // Easiest case - all are identical. In this
  83. // case we don't need to interpolate so we can
  84. // return earlier
  85. if ( d0 == d1 )
  86. {
  87. curTime = d0;
  88. curPosition = objPos->at(nextObjPos).mValue;
  89. curTargetPosition = targetObjPos->at(nextTargetObjPos).mValue;
  90. // increment counters
  91. if (objPos->size() != nextObjPos-1)
  92. ++nextObjPos;
  93. if (targetObjPos->size() != nextTargetObjPos-1)
  94. ++nextTargetObjPos;
  95. }
  96. // An object position key is closest to us
  97. else if (d0 < d1)
  98. {
  99. curTime = d0;
  100. // interpolate the other
  101. if (1 == targetObjPos->size() || !nextTargetObjPos)
  102. {
  103. curTargetPosition = targetObjPos->at(0).mValue;
  104. }
  105. else
  106. {
  107. const aiVectorKey& last = targetObjPos->at(nextTargetObjPos);
  108. const aiVectorKey& first = targetObjPos->at(nextTargetObjPos-1);
  109. curTargetPosition = Interpolate(first.mValue, last.mValue, (float) (
  110. (curTime-first.mTime) / (last.mTime-first.mTime) ));
  111. }
  112. if (objPos->size() != nextObjPos-1)
  113. ++nextObjPos;
  114. }
  115. // A target position key is closest to us
  116. else
  117. {
  118. curTime = d1;
  119. // interpolate the other
  120. if (1 == objPos->size() || !nextObjPos)
  121. {
  122. curPosition = objPos->at(0).mValue;
  123. }
  124. else
  125. {
  126. const aiVectorKey& last = objPos->at(nextObjPos);
  127. const aiVectorKey& first = objPos->at(nextObjPos-1);
  128. curPosition = Interpolate(first.mValue, last.mValue, (float) (
  129. (curTime-first.mTime) / (last.mTime-first.mTime)));
  130. }
  131. if (targetObjPos->size() != nextTargetObjPos-1)
  132. ++nextTargetObjPos;
  133. }
  134. if (nextObjPos >= objPos->size()-1 &&
  135. nextTargetObjPos >= targetObjPos->size()-1)
  136. {
  137. // We reached the very last keyframe
  138. reachedEnd = true;
  139. }
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. void TargetAnimationHelper::SetTargetAnimationChannel (
  143. const std::vector<aiVectorKey>* _targetPositions)
  144. {
  145. ai_assert(NULL != _targetPositions);
  146. targetPositions = _targetPositions;
  147. }
  148. // ------------------------------------------------------------------------------------------------
  149. void TargetAnimationHelper::SetMainAnimationChannel (
  150. const std::vector<aiVectorKey>* _objectPositions)
  151. {
  152. ai_assert(NULL != _objectPositions);
  153. objectPositions = _objectPositions;
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. void TargetAnimationHelper::SetFixedMainAnimationChannel(
  157. const aiVector3D& fixed)
  158. {
  159. objectPositions = NULL; // just to avoid confusion
  160. fixedMain = fixed;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. void TargetAnimationHelper::Process(std::vector<aiVectorKey>* distanceTrack)
  164. {
  165. ai_assert(NULL != targetPositions);
  166. // TODO: in most cases we won't need the extra array
  167. std::vector<aiVectorKey>* fill = NULL;
  168. std::vector<aiVectorKey> real;
  169. if (distanceTrack)
  170. {
  171. fill = (distanceTrack == objectPositions ? &real : distanceTrack);
  172. }
  173. fill->reserve(std::max( objectPositions->size(), targetPositions->size() ));
  174. // Iterate through all object keys and interpolate their values if necessary.
  175. // Then get the corresponding target position, compute the difference
  176. // vector between object and target position. Then compute a rotation matrix
  177. // that rotates the base vector of the object coordinate system at that time
  178. // to match the diff vector.
  179. KeyIterator iter(objectPositions,targetPositions,&fixedMain);
  180. for (;!iter.Finished();++iter)
  181. {
  182. const aiVector3D& position = iter.GetCurPosition();
  183. const aiVector3D& tposition = iter.GetCurTargetPosition();
  184. // diff vector
  185. aiVector3D diff = tposition - position;
  186. float f = diff.Length();
  187. // output distance vector
  188. if (fill)
  189. {
  190. fill->push_back(aiVectorKey());
  191. aiVectorKey& v = fill->back();
  192. v.mTime = iter.GetCurTime();
  193. v.mValue = aiVector3D (0.f,0.f,f);
  194. }
  195. diff /= f;
  196. // diff is now the vector in which our camera is pointing
  197. }
  198. if (real.size())
  199. *distanceTrack = real;
  200. }