2
0

TargetAnimation.cpp 8.0 KB

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