anim.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2017, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /**
  35. * @file anim.h
  36. * @brief Defines the data structures in which the imported animations
  37. * are returned.
  38. */
  39. #pragma once
  40. #ifndef AI_ANIM_H_INC
  41. #define AI_ANIM_H_INC
  42. #include <assimp/types.h>
  43. #include <assimp/quaternion.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. // ---------------------------------------------------------------------------
  48. /** A time-value pair specifying a certain 3D vector for the given time. */
  49. struct aiVectorKey
  50. {
  51. /** The time of this key */
  52. double mTime;
  53. /** The value of this key */
  54. C_STRUCT aiVector3D mValue;
  55. #ifdef __cplusplus
  56. /// @brief The default constructor.
  57. aiVectorKey()
  58. : mTime( 0.0 )
  59. , mValue() {
  60. // empty
  61. }
  62. /// @brief Construction from a given time and key value.
  63. aiVectorKey(double time, const aiVector3D& value)
  64. : mTime (time)
  65. , mValue (value)
  66. {}
  67. typedef aiVector3D elem_type;
  68. // Comparison operators. For use with std::find();
  69. bool operator == (const aiVectorKey& o) const {
  70. return o.mValue == this->mValue;
  71. }
  72. bool operator != (const aiVectorKey& o) const {
  73. return o.mValue != this->mValue;
  74. }
  75. // Relational operators. For use with std::sort();
  76. bool operator < (const aiVectorKey& o) const {
  77. return mTime < o.mTime;
  78. }
  79. bool operator > (const aiVectorKey& o) const {
  80. return mTime > o.mTime;
  81. }
  82. #endif // __cplusplus
  83. };
  84. // ---------------------------------------------------------------------------
  85. /** A time-value pair specifying a rotation for the given time.
  86. * Rotations are expressed with quaternions. */
  87. struct aiQuatKey
  88. {
  89. /** The time of this key */
  90. double mTime;
  91. /** The value of this key */
  92. C_STRUCT aiQuaternion mValue;
  93. #ifdef __cplusplus
  94. aiQuatKey()
  95. : mTime( 0.0 )
  96. , mValue() {
  97. // empty
  98. }
  99. /** Construction from a given time and key value */
  100. aiQuatKey(double time, const aiQuaternion& value)
  101. : mTime (time)
  102. , mValue (value)
  103. {}
  104. typedef aiQuaternion elem_type;
  105. // Comparison operators. For use with std::find();
  106. bool operator == (const aiQuatKey& o) const {
  107. return o.mValue == this->mValue;
  108. }
  109. bool operator != (const aiQuatKey& o) const {
  110. return o.mValue != this->mValue;
  111. }
  112. // Relational operators. For use with std::sort();
  113. bool operator < (const aiQuatKey& o) const {
  114. return mTime < o.mTime;
  115. }
  116. bool operator > (const aiQuatKey& o) const {
  117. return mTime > o.mTime;
  118. }
  119. #endif
  120. };
  121. // ---------------------------------------------------------------------------
  122. /** Binds a anim mesh to a specific point in time. */
  123. struct aiMeshKey
  124. {
  125. /** The time of this key */
  126. double mTime;
  127. /** Index into the aiMesh::mAnimMeshes array of the
  128. * mesh corresponding to the #aiMeshAnim hosting this
  129. * key frame. The referenced anim mesh is evaluated
  130. * according to the rules defined in the docs for #aiAnimMesh.*/
  131. unsigned int mValue;
  132. #ifdef __cplusplus
  133. aiMeshKey() {
  134. }
  135. /** Construction from a given time and key value */
  136. aiMeshKey(double time, const unsigned int value)
  137. : mTime (time)
  138. , mValue (value)
  139. {}
  140. typedef unsigned int elem_type;
  141. // Comparison operators. For use with std::find();
  142. bool operator == (const aiMeshKey& o) const {
  143. return o.mValue == this->mValue;
  144. }
  145. bool operator != (const aiMeshKey& o) const {
  146. return o.mValue != this->mValue;
  147. }
  148. // Relational operators. For use with std::sort();
  149. bool operator < (const aiMeshKey& o) const {
  150. return mTime < o.mTime;
  151. }
  152. bool operator > (const aiMeshKey& o) const {
  153. return mTime > o.mTime;
  154. }
  155. #endif
  156. };
  157. // ---------------------------------------------------------------------------
  158. /** Binds a morph anim mesh to a specific point in time. */
  159. struct aiMeshMorphKey
  160. {
  161. /** The time of this key */
  162. double mTime;
  163. /** The values and weights at the time of this key */
  164. unsigned int *mValues;
  165. double *mWeights;
  166. /** The number of values and weights */
  167. unsigned int mNumValuesAndWeights;
  168. #ifdef __cplusplus
  169. aiMeshMorphKey()
  170. : mTime(0.0)
  171. , mValues(NULL)
  172. , mWeights(NULL)
  173. , mNumValuesAndWeights(0)
  174. {
  175. }
  176. ~aiMeshMorphKey()
  177. {
  178. if (mNumValuesAndWeights && mValues && mWeights) {
  179. delete [] mValues;
  180. delete [] mWeights;
  181. }
  182. }
  183. #endif
  184. };
  185. // ---------------------------------------------------------------------------
  186. /** Defines how an animation channel behaves outside the defined time
  187. * range. This corresponds to aiNodeAnim::mPreState and
  188. * aiNodeAnim::mPostState.*/
  189. enum aiAnimBehaviour
  190. {
  191. /** The value from the default node transformation is taken*/
  192. aiAnimBehaviour_DEFAULT = 0x0,
  193. /** The nearest key value is used without interpolation */
  194. aiAnimBehaviour_CONSTANT = 0x1,
  195. /** The value of the nearest two keys is linearly
  196. * extrapolated for the current time value.*/
  197. aiAnimBehaviour_LINEAR = 0x2,
  198. /** The animation is repeated.
  199. *
  200. * If the animation key go from n to m and the current
  201. * time is t, use the value at (t-n) % (|m-n|).*/
  202. aiAnimBehaviour_REPEAT = 0x3,
  203. /** This value is not used, it is just here to force the
  204. * the compiler to map this enum to a 32 Bit integer */
  205. #ifndef SWIG
  206. _aiAnimBehaviour_Force32Bit = INT_MAX
  207. #endif
  208. };
  209. // ---------------------------------------------------------------------------
  210. /** Describes the animation of a single node. The name specifies the
  211. * bone/node which is affected by this animation channel. The keyframes
  212. * are given in three separate series of values, one each for position,
  213. * rotation and scaling. The transformation matrix computed from these
  214. * values replaces the node's original transformation matrix at a
  215. * specific time.
  216. * This means all keys are absolute and not relative to the bone default pose.
  217. * The order in which the transformations are applied is
  218. * - as usual - scaling, rotation, translation.
  219. *
  220. * @note All keys are returned in their correct, chronological order.
  221. * Duplicate keys don't pass the validation step. Most likely there
  222. * will be no negative time values, but they are not forbidden also ( so
  223. * implementations need to cope with them! ) */
  224. struct aiNodeAnim {
  225. /** The name of the node affected by this animation. The node
  226. * must exist and it must be unique.*/
  227. C_STRUCT aiString mNodeName;
  228. /** The number of position keys */
  229. unsigned int mNumPositionKeys;
  230. /** The position keys of this animation channel. Positions are
  231. * specified as 3D vector. The array is mNumPositionKeys in size.
  232. *
  233. * If there are position keys, there will also be at least one
  234. * scaling and one rotation key.*/
  235. C_STRUCT aiVectorKey* mPositionKeys;
  236. /** The number of rotation keys */
  237. unsigned int mNumRotationKeys;
  238. /** The rotation keys of this animation channel. Rotations are
  239. * given as quaternions, which are 4D vectors. The array is
  240. * mNumRotationKeys in size.
  241. *
  242. * If there are rotation keys, there will also be at least one
  243. * scaling and one position key. */
  244. C_STRUCT aiQuatKey* mRotationKeys;
  245. /** The number of scaling keys */
  246. unsigned int mNumScalingKeys;
  247. /** The scaling keys of this animation channel. Scalings are
  248. * specified as 3D vector. The array is mNumScalingKeys in size.
  249. *
  250. * If there are scaling keys, there will also be at least one
  251. * position and one rotation key.*/
  252. C_STRUCT aiVectorKey* mScalingKeys;
  253. /** Defines how the animation behaves before the first
  254. * key is encountered.
  255. *
  256. * The default value is aiAnimBehaviour_DEFAULT (the original
  257. * transformation matrix of the affected node is used).*/
  258. C_ENUM aiAnimBehaviour mPreState;
  259. /** Defines how the animation behaves after the last
  260. * key was processed.
  261. *
  262. * The default value is aiAnimBehaviour_DEFAULT (the original
  263. * transformation matrix of the affected node is taken).*/
  264. C_ENUM aiAnimBehaviour mPostState;
  265. #ifdef __cplusplus
  266. aiNodeAnim()
  267. : mNumPositionKeys( 0 )
  268. , mPositionKeys( NULL )
  269. , mNumRotationKeys( 0 )
  270. , mRotationKeys( NULL )
  271. , mNumScalingKeys( 0 )
  272. , mScalingKeys( NULL )
  273. , mPreState( aiAnimBehaviour_DEFAULT )
  274. , mPostState( aiAnimBehaviour_DEFAULT ) {
  275. // empty
  276. }
  277. ~aiNodeAnim() {
  278. delete [] mPositionKeys;
  279. delete [] mRotationKeys;
  280. delete [] mScalingKeys;
  281. }
  282. #endif // __cplusplus
  283. };
  284. // ---------------------------------------------------------------------------
  285. /** Describes vertex-based animations for a single mesh or a group of
  286. * meshes. Meshes carry the animation data for each frame in their
  287. * aiMesh::mAnimMeshes array. The purpose of aiMeshAnim is to
  288. * define keyframes linking each mesh attachment to a particular
  289. * point in time. */
  290. struct aiMeshAnim
  291. {
  292. /** Name of the mesh to be animated. An empty string is not allowed,
  293. * animated meshes need to be named (not necessarily uniquely,
  294. * the name can basically serve as wild-card to select a group
  295. * of meshes with similar animation setup)*/
  296. C_STRUCT aiString mName;
  297. /** Size of the #mKeys array. Must be 1, at least. */
  298. unsigned int mNumKeys;
  299. /** Key frames of the animation. May not be NULL. */
  300. C_STRUCT aiMeshKey* mKeys;
  301. #ifdef __cplusplus
  302. aiMeshAnim()
  303. : mNumKeys()
  304. , mKeys()
  305. {}
  306. ~aiMeshAnim()
  307. {
  308. delete[] mKeys;
  309. }
  310. #endif
  311. };
  312. // ---------------------------------------------------------------------------
  313. /** Describes a morphing animation of a given mesh. */
  314. struct aiMeshMorphAnim
  315. {
  316. /** Name of the mesh to be animated. An empty string is not allowed,
  317. * animated meshes need to be named (not necessarily uniquely,
  318. * the name can basically serve as wildcard to select a group
  319. * of meshes with similar animation setup)*/
  320. C_STRUCT aiString mName;
  321. /** Size of the #mKeys array. Must be 1, at least. */
  322. unsigned int mNumKeys;
  323. /** Key frames of the animation. May not be NULL. */
  324. C_STRUCT aiMeshMorphKey* mKeys;
  325. #ifdef __cplusplus
  326. aiMeshMorphAnim()
  327. : mNumKeys()
  328. , mKeys()
  329. {}
  330. ~aiMeshMorphAnim()
  331. {
  332. delete[] mKeys;
  333. }
  334. #endif
  335. };
  336. // ---------------------------------------------------------------------------
  337. /** An animation consists of key-frame data for a number of nodes. For
  338. * each node affected by the animation a separate series of data is given.*/
  339. struct aiAnimation {
  340. /** The name of the animation. If the modeling package this data was
  341. * exported from does support only a single animation channel, this
  342. * name is usually empty (length is zero). */
  343. C_STRUCT aiString mName;
  344. /** Duration of the animation in ticks. */
  345. double mDuration;
  346. /** Ticks per second. 0 if not specified in the imported file */
  347. double mTicksPerSecond;
  348. /** The number of bone animation channels. Each channel affects
  349. * a single node. */
  350. unsigned int mNumChannels;
  351. /** The node animation channels. Each channel affects a single node.
  352. * The array is mNumChannels in size. */
  353. C_STRUCT aiNodeAnim** mChannels;
  354. /** The number of mesh animation channels. Each channel affects
  355. * a single mesh and defines vertex-based animation. */
  356. unsigned int mNumMeshChannels;
  357. /** The mesh animation channels. Each channel affects a single mesh.
  358. * The array is mNumMeshChannels in size. */
  359. C_STRUCT aiMeshAnim** mMeshChannels;
  360. /** The number of mesh animation channels. Each channel affects
  361. * a single mesh and defines morphing animation. */
  362. unsigned int mNumMorphMeshChannels;
  363. /** The morph mesh animation channels. Each channel affects a single mesh.
  364. * The array is mNumMorphMeshChannels in size. */
  365. C_STRUCT aiMeshMorphAnim **mMorphMeshChannels;
  366. #ifdef __cplusplus
  367. aiAnimation()
  368. : mDuration(-1.)
  369. , mTicksPerSecond(0.)
  370. , mNumChannels(0)
  371. , mChannels(NULL)
  372. , mNumMeshChannels(0)
  373. , mMeshChannels(NULL)
  374. , mNumMorphMeshChannels(0)
  375. , mMorphMeshChannels(NULL) {
  376. // empty
  377. }
  378. ~aiAnimation() {
  379. // DO NOT REMOVE THIS ADDITIONAL CHECK
  380. if ( mNumChannels && mChannels ) {
  381. for( unsigned int a = 0; a < mNumChannels; a++) {
  382. delete mChannels[ a ];
  383. }
  384. delete [] mChannels;
  385. }
  386. if (mNumMeshChannels && mMeshChannels) {
  387. for( unsigned int a = 0; a < mNumMeshChannels; a++) {
  388. delete mMeshChannels[a];
  389. }
  390. delete [] mMeshChannels;
  391. }
  392. if (mNumMorphMeshChannels && mMorphMeshChannels) {
  393. for( unsigned int a = 0; a < mNumMorphMeshChannels; a++) {
  394. delete mMorphMeshChannels[a];
  395. }
  396. delete [] mMorphMeshChannels;
  397. }
  398. }
  399. #endif // __cplusplus
  400. };
  401. #ifdef __cplusplus
  402. }
  403. /// @brief Some C++ utilities for inter- and extrapolation
  404. namespace Assimp {
  405. // ---------------------------------------------------------------------------
  406. /**
  407. * @brief CPP-API: Utility class to simplify interpolations of various data types.
  408. *
  409. * The type of interpolation is chosen automatically depending on the
  410. * types of the arguments.
  411. */
  412. template <typename T>
  413. struct Interpolator
  414. {
  415. // ------------------------------------------------------------------
  416. /** @brief Get the result of the interpolation between a,b.
  417. *
  418. * The interpolation algorithm depends on the type of the operands.
  419. * aiQuaternion's and aiQuatKey's SLERP, the rest does a simple
  420. * linear interpolation. */
  421. void operator () (T& out,const T& a, const T& b, ai_real d) const {
  422. out = a + (b-a)*d;
  423. }
  424. }; // ! Interpolator <T>
  425. //! @cond Never
  426. template <>
  427. struct Interpolator <aiQuaternion> {
  428. void operator () (aiQuaternion& out,const aiQuaternion& a,
  429. const aiQuaternion& b, ai_real d) const
  430. {
  431. aiQuaternion::Interpolate(out,a,b,d);
  432. }
  433. }; // ! Interpolator <aiQuaternion>
  434. template <>
  435. struct Interpolator <unsigned int> {
  436. void operator () (unsigned int& out,unsigned int a,
  437. unsigned int b, ai_real d) const
  438. {
  439. out = d>0.5f ? b : a;
  440. }
  441. }; // ! Interpolator <aiQuaternion>
  442. template <>
  443. struct Interpolator<aiVectorKey> {
  444. void operator () (aiVector3D& out,const aiVectorKey& a,
  445. const aiVectorKey& b, ai_real d) const
  446. {
  447. Interpolator<aiVector3D> ipl;
  448. ipl(out,a.mValue,b.mValue,d);
  449. }
  450. }; // ! Interpolator <aiVectorKey>
  451. template <>
  452. struct Interpolator<aiQuatKey> {
  453. void operator () (aiQuaternion& out, const aiQuatKey& a,
  454. const aiQuatKey& b, ai_real d) const
  455. {
  456. Interpolator<aiQuaternion> ipl;
  457. ipl(out,a.mValue,b.mValue,d);
  458. }
  459. }; // ! Interpolator <aiQuatKey>
  460. template <>
  461. struct Interpolator<aiMeshKey> {
  462. void operator () (unsigned int& out, const aiMeshKey& a,
  463. const aiMeshKey& b, ai_real d) const
  464. {
  465. Interpolator<unsigned int> ipl;
  466. ipl(out,a.mValue,b.mValue,d);
  467. }
  468. }; // ! Interpolator <aiQuatKey>
  469. //! @endcond
  470. } // ! end namespace Assimp
  471. #endif // __cplusplus
  472. #endif // AI_ANIM_H_INC