LWOAnimation.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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.h
  34. * @brief LWOAnimationResolver utility class
  35. *
  36. * This is for all lightwave-related file format, not only LWO.
  37. * LWS isthe main purpose.
  38. */
  39. #ifndef AI_LWO_ANIMATION_INCLUDED
  40. #define AI_LWO_ANIMATION_INCLUDED
  41. //
  42. #include <vector>
  43. #include <list>
  44. struct aiNodeAnim;
  45. struct aiVectorKey;
  46. namespace Assimp {
  47. namespace LWO {
  48. // ---------------------------------------------------------------------------
  49. /** \brief List of recognized LWO envelopes
  50. */
  51. enum EnvelopeType
  52. {
  53. EnvelopeType_Position_X = 0x1,
  54. EnvelopeType_Position_Y = 0x2,
  55. EnvelopeType_Position_Z = 0x3,
  56. EnvelopeType_Rotation_Heading = 0x4,
  57. EnvelopeType_Rotation_Pitch = 0x5,
  58. EnvelopeType_Rotation_Bank = 0x6,
  59. EnvelopeType_Scaling_X = 0x7,
  60. EnvelopeType_Scaling_Y = 0x8,
  61. EnvelopeType_Scaling_Z = 0x9,
  62. // -- currently not yet handled
  63. EnvelopeType_Color_R = 0xa,
  64. EnvelopeType_Color_G = 0xb,
  65. EnvelopeType_Color_B = 0xc,
  66. EnvelopeType_Falloff_X = 0xd,
  67. EnvelopeType_Falloff_Y = 0xe,
  68. EnvelopeType_Falloff_Z = 0xf,
  69. EnvelopeType_Unknown
  70. };
  71. // ---------------------------------------------------------------------------
  72. /** \brief List of recognized LWO interpolation modes
  73. */
  74. enum InterpolationType
  75. {
  76. IT_STEP, IT_LINE, IT_TCB, IT_HERM, IT_BEZI, IT_BEZ2
  77. };
  78. // ---------------------------------------------------------------------------
  79. /** \brief List of recognized LWO pre or post range behaviours
  80. */
  81. enum PrePostBehaviour
  82. {
  83. PrePostBehaviour_Reset = 0x0,
  84. PrePostBehaviour_Constant = 0x1,
  85. PrePostBehaviour_Repeat = 0x2,
  86. PrePostBehaviour_Oscillate = 0x3,
  87. PrePostBehaviour_OffsetRepeat = 0x4,
  88. PrePostBehaviour_Linear = 0x5
  89. };
  90. // ---------------------------------------------------------------------------
  91. /** \brief Data structure for a LWO animation keyframe
  92. */
  93. struct Key {
  94. Key() AI_NO_EXCEPT
  95. : time()
  96. , value()
  97. , inter(IT_LINE)
  98. , params() {
  99. // empty
  100. }
  101. //! Current time
  102. double time;
  103. //! Current value
  104. float value;
  105. //! How to interpolate this key with previous key?
  106. InterpolationType inter;
  107. //! Interpolation parameters
  108. float params[5];
  109. // for std::find()
  110. operator double () {
  111. return time;
  112. }
  113. };
  114. // ---------------------------------------------------------------------------
  115. /** \brief Data structure for a LWO animation envelope
  116. */
  117. struct Envelope {
  118. Envelope() AI_NO_EXCEPT
  119. : index()
  120. , type(EnvelopeType_Unknown)
  121. , pre(PrePostBehaviour_Constant)
  122. , post(PrePostBehaviour_Constant)
  123. , old_first(0)
  124. , old_last(0) {
  125. // empty
  126. }
  127. //! Index of this envelope
  128. unsigned int index;
  129. //! Type of envelope
  130. EnvelopeType type;
  131. //! Pre- and post-behavior
  132. PrePostBehaviour pre,post;
  133. //! Keyframes for this envelope
  134. std::vector<Key> keys;
  135. // temporary data for AnimResolver
  136. size_t old_first,old_last;
  137. };
  138. // ---------------------------------------------------------------------------
  139. //! @def AI_LWO_ANIM_FLAG_SAMPLE_ANIMS
  140. //! Flag for AnimResolver, subsamples the input data with the rate specified
  141. //! by AnimResolver::SetSampleRate().
  142. #define AI_LWO_ANIM_FLAG_SAMPLE_ANIMS 0x1
  143. // ---------------------------------------------------------------------------
  144. //! @def AI_LWO_ANIM_FLAG_START_AT_ZERO
  145. //! Flag for AnimResolver, ensures that the animations starts at zero.
  146. #define AI_LWO_ANIM_FLAG_START_AT_ZERO 0x2
  147. // ---------------------------------------------------------------------------
  148. /** @brief Utility class to build Assimp animations from LWO envelopes.
  149. *
  150. * Used for both LWO and LWS (MOT also).
  151. */
  152. class AnimResolver
  153. {
  154. public:
  155. // ------------------------------------------------------------------
  156. /** @brief Construct an AnimResolver from a given list of envelopes
  157. * @param envelopes Input envelopes. May be empty.
  158. * @param Output tick rate, per second
  159. * @note The input envelopes are possibly modified.
  160. */
  161. AnimResolver(std::list<Envelope>& envelopes, double tick);
  162. public:
  163. // ------------------------------------------------------------------
  164. /** @brief Extract the bind-pose transformation matrix.
  165. * @param out Receives bind-pose transformation matrix
  166. */
  167. void ExtractBindPose(aiMatrix4x4& out);
  168. // ------------------------------------------------------------------
  169. /** @brief Extract a node animation channel
  170. * @param out Receives a pointer to a newly allocated node anim.
  171. * If there's just one keyframe defined, *out is set to nullptr and
  172. * no animation channel is computed.
  173. * @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags.
  174. */
  175. void ExtractAnimChannel(aiNodeAnim** out, unsigned int flags = 0);
  176. // ------------------------------------------------------------------
  177. /** @brief Set the sampling rate for ExtractAnimChannel().
  178. *
  179. * Non-linear interpolations are subsampled with this rate (keys
  180. * per second). Closer sampling positions, if existent, are kept.
  181. * The sampling rate defaults to 0, if this value is not changed and
  182. * AI_LWO_ANIM_FLAG_SAMPLE_ANIMS is specified for ExtractAnimChannel(),
  183. * the class finds a suitable sample rate by itself.
  184. */
  185. void SetSampleRate(double sr) {
  186. sample_rate = sr;
  187. }
  188. // ------------------------------------------------------------------
  189. /** @brief Getter for SetSampleRate()
  190. */
  191. double GetSampleRate() const {
  192. return sample_rate;
  193. }
  194. // ------------------------------------------------------------------
  195. /** @brief Set the animation time range
  196. *
  197. * @param first Time where the animation starts, in ticks
  198. * @param last Time where the animation ends, in ticks
  199. */
  200. void SetAnimationRange(double _first, double _last) {
  201. first = _first;
  202. last = _last;
  203. ClearAnimRangeSetup();
  204. UpdateAnimRangeSetup();
  205. }
  206. protected:
  207. // ------------------------------------------------------------------
  208. /** @brief Build linearly subsampled keys from 3 single envelopes
  209. * @param out Receives output keys
  210. * @param envl_x X-component envelope
  211. * @param envl_y Y-component envelope
  212. * @param envl_z Z-component envelope
  213. * @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags.
  214. * @note Up to two input envelopes may be nullptr
  215. */
  216. void GetKeys(std::vector<aiVectorKey>& out,
  217. LWO::Envelope* envl_x,
  218. LWO::Envelope* envl_y,
  219. LWO::Envelope* envl_z,
  220. unsigned int flags);
  221. // ------------------------------------------------------------------
  222. /** @brief Resolve a single animation key by applying the right
  223. * interpolation to it.
  224. * @param cur Current key
  225. * @param envl Envelope working on
  226. * @param time time to be interpolated
  227. * @param fill Receives the interpolated output value.
  228. */
  229. void DoInterpolation(std::vector<LWO::Key>::const_iterator cur,
  230. LWO::Envelope* envl,double time, float& fill);
  231. // ------------------------------------------------------------------
  232. /** @brief Almost the same, except we won't handle pre/post
  233. * conditions here.
  234. * @see DoInterpolation
  235. */
  236. void DoInterpolation2(std::vector<LWO::Key>::const_iterator beg,
  237. std::vector<LWO::Key>::const_iterator end,double time, float& fill);
  238. // ------------------------------------------------------------------
  239. /** @brief Interpolate 2 tracks if one is given
  240. *
  241. * @param out Receives extra output keys
  242. * @param key_out Primary output key
  243. * @param time Time to interpolate for
  244. */
  245. void InterpolateTrack(std::vector<aiVectorKey>& out,
  246. aiVectorKey& key_out,double time);
  247. // ------------------------------------------------------------------
  248. /** @brief Subsample an animation track by a given sampling rate
  249. *
  250. * @param out Receives output keys. Last key at input defines the
  251. * time where subsampling starts.
  252. * @param time Time to end subsampling at
  253. * @param sample_delta Time delta between two samples
  254. */
  255. void SubsampleAnimTrack(std::vector<aiVectorKey>& out,
  256. double time,double sample_delta);
  257. // ------------------------------------------------------------------
  258. /** @brief Delete all keys which we inserted to match anim setup
  259. */
  260. void ClearAnimRangeSetup();
  261. // ------------------------------------------------------------------
  262. /** @brief Insert extra keys to match LWO's pre and post behaviours
  263. * in a given time range [first...last]
  264. */
  265. void UpdateAnimRangeSetup();
  266. private:
  267. std::list<Envelope>& envelopes;
  268. double sample_rate;
  269. LWO::Envelope* trans_x, *trans_y, *trans_z;
  270. LWO::Envelope* rotat_x, *rotat_y, *rotat_z;
  271. LWO::Envelope* scale_x, *scale_y, *scale_z;
  272. double first, last;
  273. bool need_to_setup;
  274. // temporary storage
  275. LWO::Envelope* envl_x, * envl_y, * envl_z;
  276. std::vector<LWO::Key>::const_iterator cur_x,cur_y,cur_z;
  277. bool end_x, end_y, end_z;
  278. unsigned int flags;
  279. double sample_delta;
  280. };
  281. } // end namespace LWO
  282. } // end namespace Assimp
  283. #endif // !! AI_LWO_ANIMATION_INCLUDED