TargetAnimation.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, assimp 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 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 "TargetAnimation.h"
  34. #include <algorithm>
  35. #include <assimp/ai_assert.h>
  36. using namespace Assimp;
  37. // ------------------------------------------------------------------------------------------------
  38. KeyIterator::KeyIterator(const std::vector<aiVectorKey>* _objPos,
  39. const std::vector<aiVectorKey>* _targetObjPos,
  40. const aiVector3D* defaultObjectPos /*= NULL*/,
  41. const aiVector3D* defaultTargetPos /*= NULL*/)
  42. : reachedEnd (false)
  43. , curTime (-1.)
  44. , objPos (_objPos)
  45. , targetObjPos (_targetObjPos)
  46. , nextObjPos (0)
  47. , nextTargetObjPos(0)
  48. {
  49. // Generate default transformation tracks if necessary
  50. if (!objPos || objPos->empty())
  51. {
  52. defaultObjPos.resize(1);
  53. defaultObjPos.front().mTime = 10e10;
  54. if (defaultObjectPos)
  55. defaultObjPos.front().mValue = *defaultObjectPos;
  56. objPos = & defaultObjPos;
  57. }
  58. if (!targetObjPos || targetObjPos->empty())
  59. {
  60. defaultTargetObjPos.resize(1);
  61. defaultTargetObjPos.front().mTime = 10e10;
  62. if (defaultTargetPos)
  63. defaultTargetObjPos.front().mValue = *defaultTargetPos;
  64. targetObjPos = & defaultTargetObjPos;
  65. }
  66. }
  67. // ------------------------------------------------------------------------------------------------
  68. template <class T>
  69. inline T Interpolate(const T& one, const T& two, ai_real val)
  70. {
  71. return one + (two-one)*val;
  72. }
  73. // ------------------------------------------------------------------------------------------------
  74. void KeyIterator::operator ++()
  75. {
  76. // If we are already at the end of all keyframes, return
  77. if (reachedEnd) {
  78. return;
  79. }
  80. // Now search in all arrays for the time value closest
  81. // to our current position on the time line
  82. double d0,d1;
  83. d0 = objPos->at ( std::min ( nextObjPos, static_cast<unsigned int>(objPos->size()-1)) ).mTime;
  84. d1 = targetObjPos->at( std::min ( nextTargetObjPos, static_cast<unsigned int>(targetObjPos->size()-1)) ).mTime;
  85. // Easiest case - all are identical. In this
  86. // case we don't need to interpolate so we can
  87. // return earlier
  88. if ( d0 == d1 )
  89. {
  90. curTime = d0;
  91. curPosition = objPos->at(nextObjPos).mValue;
  92. curTargetPosition = targetObjPos->at(nextTargetObjPos).mValue;
  93. // increment counters
  94. if (objPos->size() != nextObjPos-1)
  95. ++nextObjPos;
  96. if (targetObjPos->size() != nextTargetObjPos-1)
  97. ++nextTargetObjPos;
  98. }
  99. // An object position key is closest to us
  100. else if (d0 < d1)
  101. {
  102. curTime = d0;
  103. // interpolate the other
  104. if (1 == targetObjPos->size() || !nextTargetObjPos) {
  105. curTargetPosition = targetObjPos->at(0).mValue;
  106. }
  107. else
  108. {
  109. const aiVectorKey& last = targetObjPos->at(nextTargetObjPos);
  110. const aiVectorKey& first = targetObjPos->at(nextTargetObjPos-1);
  111. curTargetPosition = Interpolate(first.mValue, last.mValue, (ai_real) (
  112. (curTime-first.mTime) / (last.mTime-first.mTime) ));
  113. }
  114. if (objPos->size() != nextObjPos-1)
  115. ++nextObjPos;
  116. }
  117. // A target position key is closest to us
  118. else
  119. {
  120. curTime = d1;
  121. // interpolate the other
  122. if (1 == objPos->size() || !nextObjPos) {
  123. curPosition = objPos->at(0).mValue;
  124. }
  125. else
  126. {
  127. const aiVectorKey& last = objPos->at(nextObjPos);
  128. const aiVectorKey& first = objPos->at(nextObjPos-1);
  129. curPosition = Interpolate(first.mValue, last.mValue, (ai_real) (
  130. (curTime-first.mTime) / (last.mTime-first.mTime)));
  131. }
  132. if (targetObjPos->size() != nextTargetObjPos-1)
  133. ++nextTargetObjPos;
  134. }
  135. if (nextObjPos >= objPos->size()-1 &&
  136. nextTargetObjPos >= targetObjPos->size()-1)
  137. {
  138. // We reached the very last keyframe
  139. reachedEnd = true;
  140. }
  141. }
  142. // ------------------------------------------------------------------------------------------------
  143. void TargetAnimationHelper::SetTargetAnimationChannel (
  144. const std::vector<aiVectorKey>* _targetPositions)
  145. {
  146. ai_assert(NULL != _targetPositions);
  147. targetPositions = _targetPositions;
  148. }
  149. // ------------------------------------------------------------------------------------------------
  150. void TargetAnimationHelper::SetMainAnimationChannel (
  151. const std::vector<aiVectorKey>* _objectPositions)
  152. {
  153. ai_assert(NULL != _objectPositions);
  154. objectPositions = _objectPositions;
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. void TargetAnimationHelper::SetFixedMainAnimationChannel(
  158. const aiVector3D& fixed)
  159. {
  160. objectPositions = NULL; // just to avoid confusion
  161. fixedMain = fixed;
  162. }
  163. // ------------------------------------------------------------------------------------------------
  164. void TargetAnimationHelper::Process(std::vector<aiVectorKey>* distanceTrack)
  165. {
  166. ai_assert(NULL != targetPositions && NULL != distanceTrack);
  167. // TODO: in most cases we won't need the extra array
  168. std::vector<aiVectorKey> real;
  169. std::vector<aiVectorKey>* fill = (distanceTrack == objectPositions ? &real : distanceTrack);
  170. fill->reserve(std::max( objectPositions->size(), targetPositions->size() ));
  171. // Iterate through all object keys and interpolate their values if necessary.
  172. // Then get the corresponding target position, compute the difference
  173. // vector between object and target position. Then compute a rotation matrix
  174. // that rotates the base vector of the object coordinate system at that time
  175. // to match the diff vector.
  176. KeyIterator iter(objectPositions,targetPositions,&fixedMain);
  177. for (;!iter.Finished();++iter)
  178. {
  179. const aiVector3D& position = iter.GetCurPosition();
  180. const aiVector3D& tposition = iter.GetCurTargetPosition();
  181. // diff vector
  182. aiVector3D diff = tposition - position;
  183. ai_real f = diff.Length();
  184. // output distance vector
  185. if (f)
  186. {
  187. fill->push_back(aiVectorKey());
  188. aiVectorKey& v = fill->back();
  189. v.mTime = iter.GetCurTime();
  190. v.mValue = diff;
  191. diff /= f;
  192. }
  193. else
  194. {
  195. // FIXME: handle this
  196. }
  197. // diff is now the vector in which our camera is pointing
  198. }
  199. if (real.size()) {
  200. *distanceTrack = real;
  201. }
  202. }