anim.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2015, 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. /** @file anim.h
  35. * @brief Defines the data structures in which the imported animations
  36. * are returned.
  37. */
  38. #ifndef AI_ANIM_H_INC
  39. #define AI_ANIM_H_INC
  40. #include "types.h"
  41. #include "quaternion.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. // ---------------------------------------------------------------------------
  46. /** A time-value pair specifying a certain 3D vector for the given time. */
  47. struct aiVectorKey
  48. {
  49. /** The time of this key */
  50. double mTime;
  51. /** The value of this key */
  52. C_STRUCT aiVector3D mValue;
  53. #ifdef __cplusplus
  54. //! Default constructor
  55. aiVectorKey(){}
  56. //! Construction from a given time and key value
  57. aiVectorKey(double time, const aiVector3D& value)
  58. : mTime (time)
  59. , mValue (value)
  60. {}
  61. typedef aiVector3D elem_type;
  62. // Comparison operators. For use with std::find();
  63. bool operator == (const aiVectorKey& o) const {
  64. return o.mValue == this->mValue;
  65. }
  66. bool operator != (const aiVectorKey& o) const {
  67. return o.mValue != this->mValue;
  68. }
  69. // Relational operators. For use with std::sort();
  70. bool operator < (const aiVectorKey& o) const {
  71. return mTime < o.mTime;
  72. }
  73. bool operator > (const aiVectorKey& o) const {
  74. return mTime > o.mTime;
  75. }
  76. #endif
  77. };
  78. // ---------------------------------------------------------------------------
  79. /** A time-value pair specifying a rotation for the given time.
  80. * Rotations are expressed with quaternions. */
  81. struct aiQuatKey
  82. {
  83. /** The time of this key */
  84. double mTime;
  85. /** The value of this key */
  86. C_STRUCT aiQuaternion mValue;
  87. #ifdef __cplusplus
  88. aiQuatKey(){
  89. }
  90. /** Construction from a given time and key value */
  91. aiQuatKey(double time, const aiQuaternion& value)
  92. : mTime (time)
  93. , mValue (value)
  94. {}
  95. typedef aiQuaternion elem_type;
  96. // Comparison operators. For use with std::find();
  97. bool operator == (const aiQuatKey& o) const {
  98. return o.mValue == this->mValue;
  99. }
  100. bool operator != (const aiQuatKey& o) const {
  101. return o.mValue != this->mValue;
  102. }
  103. // Relational operators. For use with std::sort();
  104. bool operator < (const aiQuatKey& o) const {
  105. return mTime < o.mTime;
  106. }
  107. bool operator > (const aiQuatKey& o) const {
  108. return mTime > o.mTime;
  109. }
  110. #endif
  111. };
  112. // ---------------------------------------------------------------------------
  113. /** Binds a anim mesh to a specific point in time. */
  114. struct aiMeshKey
  115. {
  116. /** The time of this key */
  117. double mTime;
  118. /** Index into the aiMesh::mAnimMeshes array of the
  119. * mesh coresponding to the #aiMeshAnim hosting this
  120. * key frame. The referenced anim mesh is evaluated
  121. * according to the rules defined in the docs for #aiAnimMesh.*/
  122. unsigned int mValue;
  123. #ifdef __cplusplus
  124. aiMeshKey() {
  125. }
  126. /** Construction from a given time and key value */
  127. aiMeshKey(double time, const unsigned int value)
  128. : mTime (time)
  129. , mValue (value)
  130. {}
  131. typedef unsigned int elem_type;
  132. // Comparison operators. For use with std::find();
  133. bool operator == (const aiMeshKey& o) const {
  134. return o.mValue == this->mValue;
  135. }
  136. bool operator != (const aiMeshKey& o) const {
  137. return o.mValue != this->mValue;
  138. }
  139. // Relational operators. For use with std::sort();
  140. bool operator < (const aiMeshKey& o) const {
  141. return mTime < o.mTime;
  142. }
  143. bool operator > (const aiMeshKey& o) const {
  144. return mTime > o.mTime;
  145. }
  146. #endif
  147. };
  148. // ---------------------------------------------------------------------------
  149. /** Defines how an animation channel behaves outside the defined time
  150. * range. This corresponds to aiNodeAnim::mPreState and
  151. * aiNodeAnim::mPostState.*/
  152. enum aiAnimBehaviour
  153. {
  154. /** The value from the default node transformation is taken*/
  155. aiAnimBehaviour_DEFAULT = 0x0,
  156. /** The nearest key value is used without interpolation */
  157. aiAnimBehaviour_CONSTANT = 0x1,
  158. /** The value of the nearest two keys is linearly
  159. * extrapolated for the current time value.*/
  160. aiAnimBehaviour_LINEAR = 0x2,
  161. /** The animation is repeated.
  162. *
  163. * If the animation key go from n to m and the current
  164. * time is t, use the value at (t-n) % (|m-n|).*/
  165. aiAnimBehaviour_REPEAT = 0x3,
  166. /** This value is not used, it is just here to force the
  167. * the compiler to map this enum to a 32 Bit integer */
  168. #ifndef SWIG
  169. _aiAnimBehaviour_Force32Bit = INT_MAX
  170. #endif
  171. };
  172. // ---------------------------------------------------------------------------
  173. /** Describes the animation of a single node. The name specifies the
  174. * bone/node which is affected by this animation channel. The keyframes
  175. * are given in three separate series of values, one each for position,
  176. * rotation and scaling. The transformation matrix computed from these
  177. * values replaces the node's original transformation matrix at a
  178. * specific time.
  179. * This means all keys are absolute and not relative to the bone default pose.
  180. * The order in which the transformations are applied is
  181. * - as usual - scaling, rotation, translation.
  182. *
  183. * @note All keys are returned in their correct, chronological order.
  184. * Duplicate keys don't pass the validation step. Most likely there
  185. * will be no negative time values, but they are not forbidden also ( so
  186. * implementations need to cope with them! ) */
  187. struct aiNodeAnim
  188. {
  189. /** The name of the node affected by this animation. The node
  190. * must exist and it must be unique.*/
  191. C_STRUCT aiString mNodeName;
  192. /** The number of position keys */
  193. unsigned int mNumPositionKeys;
  194. /** The position keys of this animation channel. Positions are
  195. * specified as 3D vector. The array is mNumPositionKeys in size.
  196. *
  197. * If there are position keys, there will also be at least one
  198. * scaling and one rotation key.*/
  199. C_STRUCT aiVectorKey* mPositionKeys;
  200. /** The number of rotation keys */
  201. unsigned int mNumRotationKeys;
  202. /** The rotation keys of this animation channel. Rotations are
  203. * given as quaternions, which are 4D vectors. The array is
  204. * mNumRotationKeys in size.
  205. *
  206. * If there are rotation keys, there will also be at least one
  207. * scaling and one position key. */
  208. C_STRUCT aiQuatKey* mRotationKeys;
  209. /** The number of scaling keys */
  210. unsigned int mNumScalingKeys;
  211. /** The scaling keys of this animation channel. Scalings are
  212. * specified as 3D vector. The array is mNumScalingKeys in size.
  213. *
  214. * If there are scaling keys, there will also be at least one
  215. * position and one rotation key.*/
  216. C_STRUCT aiVectorKey* mScalingKeys;
  217. /** Defines how the animation behaves before the first
  218. * key is encountered.
  219. *
  220. * The default value is aiAnimBehaviour_DEFAULT (the original
  221. * transformation matrix of the affected node is used).*/
  222. C_ENUM aiAnimBehaviour mPreState;
  223. /** Defines how the animation behaves after the last
  224. * key was processed.
  225. *
  226. * The default value is aiAnimBehaviour_DEFAULT (the original
  227. * transformation matrix of the affected node is taken).*/
  228. C_ENUM aiAnimBehaviour mPostState;
  229. #ifdef __cplusplus
  230. aiNodeAnim()
  231. {
  232. mNumPositionKeys = 0; mPositionKeys = NULL;
  233. mNumRotationKeys = 0; mRotationKeys = NULL;
  234. mNumScalingKeys = 0; mScalingKeys = NULL;
  235. mPreState = mPostState = aiAnimBehaviour_DEFAULT;
  236. }
  237. ~aiNodeAnim()
  238. {
  239. delete [] mPositionKeys;
  240. delete [] mRotationKeys;
  241. delete [] mScalingKeys;
  242. }
  243. #endif // __cplusplus
  244. };
  245. // ---------------------------------------------------------------------------
  246. /** Describes vertex-based animations for a single mesh or a group of
  247. * meshes. Meshes carry the animation data for each frame in their
  248. * aiMesh::mAnimMeshes array. The purpose of aiMeshAnim is to
  249. * define keyframes linking each mesh attachment to a particular
  250. * point in time. */
  251. struct aiMeshAnim
  252. {
  253. /** Name of the mesh to be animated. An empty string is not allowed,
  254. * animated meshes need to be named (not necessarily uniquely,
  255. * the name can basically serve as wildcard to select a group
  256. * of meshes with similar animation setup)*/
  257. C_STRUCT aiString mName;
  258. /** Size of the #mKeys array. Must be 1, at least. */
  259. unsigned int mNumKeys;
  260. /** Key frames of the animation. May not be NULL. */
  261. C_STRUCT aiMeshKey* mKeys;
  262. #ifdef __cplusplus
  263. aiMeshAnim()
  264. : mNumKeys()
  265. , mKeys()
  266. {}
  267. ~aiMeshAnim()
  268. {
  269. delete[] mKeys;
  270. }
  271. #endif
  272. };
  273. // ---------------------------------------------------------------------------
  274. /** An animation consists of keyframe data for a number of nodes. For
  275. * each node affected by the animation a separate series of data is given.*/
  276. struct aiAnimation
  277. {
  278. /** The name of the animation. If the modeling package this data was
  279. * exported from does support only a single animation channel, this
  280. * name is usually empty (length is zero). */
  281. C_STRUCT aiString mName;
  282. /** Duration of the animation in ticks. */
  283. double mDuration;
  284. /** Ticks per second. 0 if not specified in the imported file */
  285. double mTicksPerSecond;
  286. /** The number of bone animation channels. Each channel affects
  287. * a single node. */
  288. unsigned int mNumChannels;
  289. /** The node animation channels. Each channel affects a single node.
  290. * The array is mNumChannels in size. */
  291. C_STRUCT aiNodeAnim** mChannels;
  292. /** The number of mesh animation channels. Each channel affects
  293. * a single mesh and defines vertex-based animation. */
  294. unsigned int mNumMeshChannels;
  295. /** The mesh animation channels. Each channel affects a single mesh.
  296. * The array is mNumMeshChannels in size. */
  297. C_STRUCT aiMeshAnim** mMeshChannels;
  298. #ifdef __cplusplus
  299. aiAnimation()
  300. : mDuration(-1.)
  301. , mTicksPerSecond()
  302. , mNumChannels()
  303. , mChannels()
  304. , mNumMeshChannels()
  305. , mMeshChannels()
  306. {
  307. }
  308. ~aiAnimation()
  309. {
  310. // DO NOT REMOVE THIS ADDITIONAL CHECK
  311. if (mNumChannels && mChannels) {
  312. for( unsigned int a = 0; a < mNumChannels; a++) {
  313. delete mChannels[a];
  314. }
  315. delete [] mChannels;
  316. }
  317. if (mNumMeshChannels && mMeshChannels) {
  318. for( unsigned int a = 0; a < mNumMeshChannels; a++) {
  319. delete mMeshChannels[a];
  320. }
  321. delete [] mMeshChannels;
  322. }
  323. }
  324. #endif // __cplusplus
  325. };
  326. #ifdef __cplusplus
  327. }
  328. // some C++ utilities for inter- and extrapolation
  329. namespace Assimp {
  330. // ---------------------------------------------------------------------------
  331. /** @brief CPP-API: Utility class to simplify interpolations of various data types.
  332. *
  333. * The type of interpolation is choosen automatically depending on the
  334. * types of the arguments. */
  335. template <typename T>
  336. struct Interpolator
  337. {
  338. // ------------------------------------------------------------------
  339. /** @brief Get the result of the interpolation between a,b.
  340. *
  341. * The interpolation algorithm depends on the type of the operands.
  342. * aiQuaternion's and aiQuatKey's SLERP, the rest does a simple
  343. * linear interpolation. */
  344. void operator () (T& out,const T& a, const T& b, float d) const {
  345. out = a + (b-a)*d;
  346. }
  347. }; // ! Interpolator <T>
  348. //! @cond Never
  349. template <>
  350. struct Interpolator <aiQuaternion> {
  351. void operator () (aiQuaternion& out,const aiQuaternion& a,
  352. const aiQuaternion& b, float d) const
  353. {
  354. aiQuaternion::Interpolate(out,a,b,d);
  355. }
  356. }; // ! Interpolator <aiQuaternion>
  357. template <>
  358. struct Interpolator <unsigned int> {
  359. void operator () (unsigned int& out,unsigned int a,
  360. unsigned int b, float d) const
  361. {
  362. out = d>0.5f ? b : a;
  363. }
  364. }; // ! Interpolator <aiQuaternion>
  365. template <>
  366. struct Interpolator <aiVectorKey> {
  367. void operator () (aiVector3D& out,const aiVectorKey& a,
  368. const aiVectorKey& b, float d) const
  369. {
  370. Interpolator<aiVector3D> ipl;
  371. ipl(out,a.mValue,b.mValue,d);
  372. }
  373. }; // ! Interpolator <aiVectorKey>
  374. template <>
  375. struct Interpolator <aiQuatKey> {
  376. void operator () (aiQuaternion& out, const aiQuatKey& a,
  377. const aiQuatKey& b, float d) const
  378. {
  379. Interpolator<aiQuaternion> ipl;
  380. ipl(out,a.mValue,b.mValue,d);
  381. }
  382. }; // ! Interpolator <aiQuatKey>
  383. template <>
  384. struct Interpolator <aiMeshKey> {
  385. void operator () (unsigned int& out, const aiMeshKey& a,
  386. const aiMeshKey& b, float d) const
  387. {
  388. Interpolator<unsigned int> ipl;
  389. ipl(out,a.mValue,b.mValue,d);
  390. }
  391. }; // ! Interpolator <aiQuatKey>
  392. //! @endcond
  393. } // ! end namespace Assimp
  394. #endif // __cplusplus
  395. #endif // AI_ANIM_H_INC