You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

595 regels
18 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. /** @file LWOAnimation.cpp
  34. * @brief LWOAnimationResolver utility class
  35. *
  36. * It's a very generic implementation of LightWave's system of
  37. * componentwise-animated stuff. The one and only fully free
  38. * implementation of LightWave envelopes of which I know.
  39. */
  40. #include "AssimpPCH.h"
  41. #if (!defined ASSIMP_BUILD_NO_LWO_IMPORTER) && (!defined ASSIMP_BUILD_NO_LWS_IMPORTER)
  42. #include <functional>
  43. // internal headers
  44. #include "LWOFileData.h"
  45. using namespace Assimp;
  46. using namespace Assimp::LWO;
  47. // ------------------------------------------------------------------------------------------------
  48. // Construct an animation resolver from a given list of envelopes
  49. AnimResolver::AnimResolver(std::list<Envelope>& _envelopes,double tick)
  50. : envelopes (_envelopes)
  51. , sample_rate (0.)
  52. {
  53. trans_x = trans_y = trans_z = NULL;
  54. rotat_x = rotat_y = rotat_z = NULL;
  55. scale_x = scale_y = scale_z = NULL;
  56. first = last = 150392.;
  57. // find transformation envelopes
  58. for (std::list<LWO::Envelope>::iterator it = envelopes.begin(); it != envelopes.end(); ++it) {
  59. (*it).old_first = 0;
  60. (*it).old_last = (*it).keys.size()-1;
  61. if ((*it).keys.empty()) continue;
  62. switch ((*it).type) {
  63. // translation
  64. case LWO::EnvelopeType_Position_X:
  65. trans_x = &*it;break;
  66. case LWO::EnvelopeType_Position_Y:
  67. trans_y = &*it;break;
  68. case LWO::EnvelopeType_Position_Z:
  69. trans_z = &*it;break;
  70. // rotation
  71. case LWO::EnvelopeType_Rotation_Heading:
  72. rotat_x = &*it;break;
  73. case LWO::EnvelopeType_Rotation_Pitch:
  74. rotat_y = &*it;break;
  75. case LWO::EnvelopeType_Rotation_Bank:
  76. rotat_z = &*it;break;
  77. // scaling
  78. case LWO::EnvelopeType_Scaling_X:
  79. scale_x = &*it;break;
  80. case LWO::EnvelopeType_Scaling_Y:
  81. scale_y = &*it;break;
  82. case LWO::EnvelopeType_Scaling_Z:
  83. scale_z = &*it;break;
  84. default:
  85. continue;
  86. };
  87. // convert from seconds to ticks
  88. for (std::vector<LWO::Key>::iterator d = (*it).keys.begin(); d != (*it).keys.end(); ++d)
  89. (*d).time *= tick;
  90. // set default animation range (minimum and maximum time value for which we have a keyframe)
  91. first = std::min(first, (*it).keys.front().time );
  92. last = std::max(last, (*it).keys.back().time );
  93. }
  94. // deferred setup of animation range to increase performance.
  95. // typically the application will want to specify its own.
  96. need_to_setup = true;
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. // Reset all envelopes to their original contents
  100. void AnimResolver::ClearAnimRangeSetup()
  101. {
  102. for (std::list<LWO::Envelope>::iterator it = envelopes.begin(); it != envelopes.end(); ++it) {
  103. (*it).keys.erase((*it).keys.begin(),(*it).keys.begin()+(*it).old_first);
  104. (*it).keys.erase((*it).keys.begin()+(*it).old_last+1,(*it).keys.end());
  105. }
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. // Insert additional keys to match LWO's pre& post behaviours.
  109. void AnimResolver::UpdateAnimRangeSetup()
  110. {
  111. // XXX doesn't work yet (hangs if more than one envelope channels needs to be interpolated)
  112. for (std::list<LWO::Envelope>::iterator it = envelopes.begin(); it != envelopes.end(); ++it) {
  113. if ((*it).keys.empty()) continue;
  114. const double my_first = (*it).keys.front().time;
  115. const double my_last = (*it).keys.back().time;
  116. const double delta = my_last-my_first;
  117. const size_t old_size = (*it).keys.size();
  118. const float value_delta = (*it).keys.back().value - (*it).keys.front().value;
  119. // NOTE: We won't handle reset, linear and constant here.
  120. // See DoInterpolation() for their implementation.
  121. // process pre behaviour
  122. switch ((*it).pre) {
  123. case LWO::PrePostBehaviour_OffsetRepeat:
  124. case LWO::PrePostBehaviour_Repeat:
  125. case LWO::PrePostBehaviour_Oscillate:
  126. {
  127. const double start_time = delta - fmod(my_first-first,delta);
  128. std::vector<LWO::Key>::iterator n = std::find_if((*it).keys.begin(),(*it).keys.end(),
  129. std::bind1st(std::greater<double>(),start_time)),m;
  130. size_t ofs = 0;
  131. if (n != (*it).keys.end()) {
  132. // copy from here - don't use iterators, insert() would invalidate them
  133. ofs = (*it).keys.end()-n;
  134. (*it).keys.insert((*it).keys.begin(),ofs,LWO::Key());
  135. std::copy((*it).keys.end()-ofs,(*it).keys.end(),(*it).keys.begin());
  136. }
  137. // do full copies. again, no iterators
  138. const unsigned int num = (unsigned int)((my_first-first) / delta);
  139. (*it).keys.resize((*it).keys.size() + num*old_size);
  140. n = (*it).keys.begin()+ofs;
  141. bool reverse = false;
  142. for (unsigned int i = 0; i < num; ++i) {
  143. m = n+old_size*(i+1);
  144. std::copy(n,n+old_size,m);
  145. if ((*it).pre == LWO::PrePostBehaviour_Oscillate && (reverse = !reverse))
  146. std::reverse(m,m+old_size-1);
  147. }
  148. // update time values
  149. n = (*it).keys.end() - (old_size+1);
  150. double cur_minus = delta;
  151. unsigned int tt = 1;
  152. for (const double tmp = delta*(num+1);cur_minus <= tmp;cur_minus += delta,++tt) {
  153. m = (delta == tmp ? (*it).keys.begin() : n - (old_size+1));
  154. for (;m != n; --n) {
  155. (*n).time -= cur_minus;
  156. // offset repeat? add delta offset to key value
  157. if ((*it).pre == LWO::PrePostBehaviour_OffsetRepeat) {
  158. (*n).value += tt * value_delta;
  159. }
  160. }
  161. }
  162. break;
  163. }
  164. default:
  165. // silence compiler warning
  166. break;
  167. }
  168. // process post behaviour
  169. switch ((*it).post) {
  170. case LWO::PrePostBehaviour_OffsetRepeat:
  171. case LWO::PrePostBehaviour_Repeat:
  172. case LWO::PrePostBehaviour_Oscillate:
  173. break;
  174. default:
  175. // silence compiler warning
  176. break;
  177. }
  178. }
  179. }
  180. // ------------------------------------------------------------------------------------------------
  181. // Extract bind pose matrix
  182. void AnimResolver::ExtractBindPose(aiMatrix4x4& out)
  183. {
  184. // If we have no envelopes, return identity
  185. if (envelopes.empty()) {
  186. out = aiMatrix4x4();
  187. return;
  188. }
  189. aiVector3D angles, scaling(1.f,1.f,1.f), translation;
  190. if (trans_x) translation.x = trans_x->keys[0].value;
  191. if (trans_y) translation.y = trans_y->keys[0].value;
  192. if (trans_z) translation.z = trans_z->keys[0].value;
  193. if (rotat_x) angles.x = rotat_x->keys[0].value;
  194. if (rotat_y) angles.y = rotat_y->keys[0].value;
  195. if (rotat_z) angles.z = rotat_z->keys[0].value;
  196. if (scale_x) scaling.x = scale_x->keys[0].value;
  197. if (scale_y) scaling.y = scale_y->keys[0].value;
  198. if (scale_z) scaling.z = scale_z->keys[0].value;
  199. // build the final matrix
  200. aiMatrix4x4 s,rx,ry,rz,t;
  201. aiMatrix4x4::RotationZ(angles.z, rz);
  202. aiMatrix4x4::RotationX(angles.y, rx);
  203. aiMatrix4x4::RotationY(angles.x, ry);
  204. aiMatrix4x4::Translation(translation,t);
  205. aiMatrix4x4::Scaling(scaling,s);
  206. out = t*ry*rx*rz*s;
  207. }
  208. // ------------------------------------------------------------------------------------------------
  209. // Do a single interpolation on a channel
  210. void AnimResolver::DoInterpolation(std::vector<LWO::Key>::const_iterator cur,
  211. LWO::Envelope* envl,double time, float& fill)
  212. {
  213. if (envl->keys.size() == 1) {
  214. fill = envl->keys[0].value;
  215. return;
  216. }
  217. // check whether we're at the beginning of the animation track
  218. if (cur == envl->keys.begin()) {
  219. // ok ... this depends on pre behaviour now
  220. // we don't need to handle repeat&offset repeat&oszillate here, see UpdateAnimRangeSetup()
  221. switch (envl->pre)
  222. {
  223. case LWO::PrePostBehaviour_Linear:
  224. DoInterpolation2(cur,cur+1,time,fill);
  225. return;
  226. case LWO::PrePostBehaviour_Reset:
  227. fill = 0.f;
  228. return;
  229. default : //case LWO::PrePostBehaviour_Constant:
  230. fill = (*cur).value;
  231. return;
  232. }
  233. }
  234. // check whether we're at the end of the animation track
  235. else if (cur == envl->keys.end()-1 && time > envl->keys.rbegin()->time) {
  236. // ok ... this depends on post behaviour now
  237. switch (envl->post)
  238. {
  239. case LWO::PrePostBehaviour_Linear:
  240. DoInterpolation2(cur,cur-1,time,fill);
  241. return;
  242. case LWO::PrePostBehaviour_Reset:
  243. fill = 0.f;
  244. return;
  245. default : //case LWO::PrePostBehaviour_Constant:
  246. fill = (*cur).value;
  247. return;
  248. }
  249. }
  250. // Otherwise do a simple interpolation
  251. DoInterpolation2(cur-1,cur,time,fill);
  252. }
  253. // ------------------------------------------------------------------------------------------------
  254. // Almost the same, except we won't handle pre/post conditions here
  255. void AnimResolver::DoInterpolation2(std::vector<LWO::Key>::const_iterator beg,
  256. std::vector<LWO::Key>::const_iterator end,double time, float& fill)
  257. {
  258. switch ((*end).inter) {
  259. case LWO::IT_STEP:
  260. // no interpolation at all - take the value of the last key
  261. fill = (*beg).value;
  262. return;
  263. default:
  264. // silence compiler warning
  265. break;
  266. }
  267. // linear interpolation - default
  268. fill = (*beg).value + ((*end).value - (*beg).value)*(float)(((time - (*beg).time) / ((*end).time - (*beg).time)));
  269. }
  270. // ------------------------------------------------------------------------------------------------
  271. // Subsample animation track by given key values
  272. void AnimResolver::SubsampleAnimTrack(std::vector<aiVectorKey>& /*out*/,
  273. double /*time*/ ,double /*sample_delta*/ )
  274. {
  275. //ai_assert(out.empty() && sample_delta);
  276. //const double time_start = out.back().mTime;
  277. // for ()
  278. }
  279. // ------------------------------------------------------------------------------------------------
  280. // Track interpolation
  281. void AnimResolver::InterpolateTrack(std::vector<aiVectorKey>& out,aiVectorKey& fill,double time)
  282. {
  283. // subsample animation track?
  284. if (flags & AI_LWO_ANIM_FLAG_SAMPLE_ANIMS) {
  285. SubsampleAnimTrack(out,time, sample_delta);
  286. }
  287. fill.mTime = time;
  288. // get x
  289. if ((*cur_x).time == time) {
  290. fill.mValue.x = (*cur_x).value;
  291. if (cur_x != envl_x->keys.end()-1) /* increment x */
  292. ++cur_x;
  293. else end_x = true;
  294. }
  295. else DoInterpolation(cur_x,envl_x,time,(float&)fill.mValue.x);
  296. // get y
  297. if ((*cur_y).time == time) {
  298. fill.mValue.y = (*cur_y).value;
  299. if (cur_y != envl_y->keys.end()-1) /* increment y */
  300. ++cur_y;
  301. else end_y = true;
  302. }
  303. else DoInterpolation(cur_y,envl_y,time,(float&)fill.mValue.y);
  304. // get z
  305. if ((*cur_z).time == time) {
  306. fill.mValue.z = (*cur_z).value;
  307. if (cur_z != envl_z->keys.end()-1) /* increment z */
  308. ++cur_z;
  309. else end_x = true;
  310. }
  311. else DoInterpolation(cur_z,envl_z,time,(float&)fill.mValue.z);
  312. }
  313. // ------------------------------------------------------------------------------------------------
  314. // Build linearly subsampled keys from three single envelopes, one for each component (x,y,z)
  315. void AnimResolver::GetKeys(std::vector<aiVectorKey>& out,
  316. LWO::Envelope* _envl_x,
  317. LWO::Envelope* _envl_y,
  318. LWO::Envelope* _envl_z,
  319. unsigned int _flags)
  320. {
  321. envl_x = _envl_x;
  322. envl_y = _envl_y;
  323. envl_z = _envl_z;
  324. flags = _flags;
  325. // generate default channels if none are given
  326. LWO::Envelope def_x, def_y, def_z;
  327. LWO::Key key_dummy;
  328. key_dummy.time = 0.f;
  329. if ((envl_x && envl_x->type == LWO::EnvelopeType_Scaling_X) ||
  330. (envl_y && envl_y->type == LWO::EnvelopeType_Scaling_Y) ||
  331. (envl_z && envl_z->type == LWO::EnvelopeType_Scaling_Z)) {
  332. key_dummy.value = 1.f;
  333. }
  334. else key_dummy.value = 0.f;
  335. if (!envl_x) {
  336. envl_x = &def_x;
  337. envl_x->keys.push_back(key_dummy);
  338. }
  339. if (!envl_y) {
  340. envl_y = &def_y;
  341. envl_y->keys.push_back(key_dummy);
  342. }
  343. if (!envl_z) {
  344. envl_z = &def_z;
  345. envl_z->keys.push_back(key_dummy);
  346. }
  347. // guess how many keys we'll get
  348. size_t reserve;
  349. double sr = 1.;
  350. if (flags & AI_LWO_ANIM_FLAG_SAMPLE_ANIMS) {
  351. if (!sample_rate)
  352. sr = 100.f;
  353. else sr = sample_rate;
  354. sample_delta = 1.f / sr;
  355. reserve = (size_t)(
  356. std::max( envl_x->keys.rbegin()->time,
  357. std::max( envl_y->keys.rbegin()->time, envl_z->keys.rbegin()->time )) * sr);
  358. }
  359. else reserve = std::max(envl_x->keys.size(),std::max(envl_x->keys.size(),envl_z->keys.size()));
  360. out.reserve(reserve+(reserve>>1));
  361. // Iterate through all three arrays at once - it's tricky, but
  362. // rather interesting to implement.
  363. double lasttime = std::min(envl_x->keys[0].time,std::min(envl_y->keys[0].time,envl_z->keys[0].time));
  364. cur_x = envl_x->keys.begin();
  365. cur_y = envl_y->keys.begin();
  366. cur_z = envl_z->keys.begin();
  367. end_x = end_y = end_z = false;
  368. while (1) {
  369. aiVectorKey fill;
  370. if ((*cur_x).time == (*cur_y).time && (*cur_x).time == (*cur_z).time ) {
  371. // we have a keyframe for all of them defined .. this means
  372. // we don't need to interpolate here.
  373. fill.mTime = (*cur_x).time;
  374. fill.mValue.x = (*cur_x).value;
  375. fill.mValue.y = (*cur_y).value;
  376. fill.mValue.z = (*cur_z).value;
  377. // subsample animation track
  378. if (flags & AI_LWO_ANIM_FLAG_SAMPLE_ANIMS) {
  379. //SubsampleAnimTrack(out,cur_x, cur_y, cur_z, d, sample_delta);
  380. }
  381. }
  382. // Find key with lowest time value
  383. else if ((*cur_x).time <= (*cur_y).time && !end_x) {
  384. if ((*cur_z).time <= (*cur_x).time && !end_z) {
  385. InterpolateTrack(out,fill,(*cur_z).time);
  386. }
  387. else {
  388. InterpolateTrack(out,fill,(*cur_x).time);
  389. }
  390. }
  391. else if ((*cur_z).time <= (*cur_y).time && !end_y) {
  392. InterpolateTrack(out,fill,(*cur_y).time);
  393. }
  394. else if (!end_y) {
  395. // welcome on the server, y
  396. InterpolateTrack(out,fill,(*cur_y).time);
  397. }
  398. else {
  399. // we have reached the end of at least 2 channels,
  400. // only one is remaining. Extrapolate the 2.
  401. if (end_y) {
  402. InterpolateTrack(out,fill,(end_x ? (*cur_z) : (*cur_x)).time);
  403. }
  404. else if (end_x) {
  405. InterpolateTrack(out,fill,(end_z ? (*cur_y) : (*cur_z)).time);
  406. }
  407. else { // if (end_z)
  408. InterpolateTrack(out,fill,(end_y ? (*cur_x) : (*cur_y)).time);
  409. }
  410. }
  411. lasttime = fill.mTime;
  412. out.push_back(fill);
  413. if (lasttime >= (*cur_x).time) {
  414. if (cur_x != envl_x->keys.end()-1)
  415. ++cur_x;
  416. else end_x = true;
  417. }
  418. if (lasttime >= (*cur_y).time) {
  419. if (cur_y != envl_y->keys.end()-1)
  420. ++cur_y;
  421. else end_y = true;
  422. }
  423. if (lasttime >= (*cur_z).time) {
  424. if (cur_z != envl_z->keys.end()-1)
  425. ++cur_z;
  426. else end_z = true;
  427. }
  428. if( end_x && end_y && end_z ) /* finished? */
  429. break;
  430. }
  431. if (flags & AI_LWO_ANIM_FLAG_START_AT_ZERO) {
  432. for (std::vector<aiVectorKey>::iterator it = out.begin(); it != out.end(); ++it)
  433. (*it).mTime -= first;
  434. }
  435. }
  436. // ------------------------------------------------------------------------------------------------
  437. // Extract animation channel
  438. void AnimResolver::ExtractAnimChannel(aiNodeAnim** out, unsigned int flags /*= 0*/)
  439. {
  440. *out = NULL;
  441. //FIXME: crashes if more than one component is animated at different timings, to be resolved.
  442. // If we have no envelopes, return NULL
  443. if (envelopes.empty()) {
  444. return;
  445. }
  446. // We won't spawn an animation channel if we don't have at least one envelope with more than one keyframe defined.
  447. const bool trans = ((trans_x && trans_x->keys.size() > 1) || (trans_y && trans_y->keys.size() > 1) || (trans_z && trans_z->keys.size() > 1));
  448. const bool rotat = ((rotat_x && rotat_x->keys.size() > 1) || (rotat_y && rotat_y->keys.size() > 1) || (rotat_z && rotat_z->keys.size() > 1));
  449. const bool scale = ((scale_x && scale_x->keys.size() > 1) || (scale_y && scale_y->keys.size() > 1) || (scale_z && scale_z->keys.size() > 1));
  450. if (!trans && !rotat && !scale)
  451. return;
  452. // Allocate the output animation
  453. aiNodeAnim* anim = *out = new aiNodeAnim();
  454. // Setup default animation setup if necessary
  455. if (need_to_setup) {
  456. UpdateAnimRangeSetup();
  457. need_to_setup = false;
  458. }
  459. // copy translation keys
  460. if (trans) {
  461. std::vector<aiVectorKey> keys;
  462. GetKeys(keys,trans_x,trans_y,trans_z,flags);
  463. anim->mPositionKeys = new aiVectorKey[ anim->mNumPositionKeys = keys.size() ];
  464. std::copy(keys.begin(),keys.end(),anim->mPositionKeys);
  465. }
  466. // copy rotation keys
  467. if (rotat) {
  468. std::vector<aiVectorKey> keys;
  469. GetKeys(keys,rotat_x,rotat_y,rotat_z,flags);
  470. anim->mRotationKeys = new aiQuatKey[ anim->mNumRotationKeys = keys.size() ];
  471. // convert heading, pitch, bank to quaternion
  472. // mValue.x=Heading=Rot(Y), mValue.y=Pitch=Rot(X), mValue.z=Bank=Rot(Z)
  473. // Lightwave's rotation order is ZXY
  474. aiVector3D X(1.0,0.0,0.0);
  475. aiVector3D Y(0.0,1.0,0.0);
  476. aiVector3D Z(0.0,0.0,1.0);
  477. for (unsigned int i = 0; i < anim->mNumRotationKeys; ++i) {
  478. aiQuatKey& qk = anim->mRotationKeys[i];
  479. qk.mTime = keys[i].mTime;
  480. qk.mValue = aiQuaternion(Y,keys[i].mValue.x)*aiQuaternion(X,keys[i].mValue.y)*aiQuaternion(Z,keys[i].mValue.z);
  481. }
  482. }
  483. // copy scaling keys
  484. if (scale) {
  485. std::vector<aiVectorKey> keys;
  486. GetKeys(keys,scale_x,scale_y,scale_z,flags);
  487. anim->mScalingKeys = new aiVectorKey[ anim->mNumScalingKeys = keys.size() ];
  488. std::copy(keys.begin(),keys.end(),anim->mScalingKeys);
  489. }
  490. }
  491. #endif // no lwo or no lws