anim.h 18 KB

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