afxAnimCurve.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "afx/util/afxAnimCurve.h"
  26. afxAnimCurve::afxAnimCurve() : usable( false ), final_value( 0.0f ), start_value( 0.0f )
  27. {
  28. evaluator = new afxHermiteEval();
  29. }
  30. afxAnimCurve::~afxAnimCurve()
  31. {
  32. delete evaluator;
  33. }
  34. void afxAnimCurve::addKey( Point2F &v )
  35. {
  36. Key k;
  37. k.time = v.x;
  38. k.value = v.y;
  39. keys.push_back( k );
  40. usable = false;
  41. }
  42. void afxAnimCurve::addKey( F32 time, F32 value )
  43. {
  44. Key k;
  45. k.time = time;
  46. k.value = value;
  47. keys.push_back( k );
  48. usable = false;
  49. }
  50. void afxAnimCurve::setKeyTime( int index, F32 t )
  51. {
  52. if( ( index < 0 ) || ( index >= keys.size() ) )
  53. return;
  54. Key &k = keys[index];
  55. k.time = t;
  56. usable = false;
  57. }
  58. void afxAnimCurve::setKeyValue( int index, F32 v )
  59. {
  60. if( ( index < 0 ) || ( index >= keys.size() ) )
  61. return;
  62. Key &k = keys[index];
  63. k.value = v;
  64. if( index == 0 )
  65. start_value = v;
  66. else if( index == keys.size()-1 )
  67. final_value = v;
  68. }
  69. //bool afxAnimCurve::compare_Key( const afxAnimCurve::Key &a, const afxAnimCurve::Key &b )
  70. //{
  71. // return a.time < b.time;
  72. //}
  73. S32 QSORT_CALLBACK afxAnimCurve::compare_Key( const void* a, const void* b )
  74. {
  75. const Key *key_a = (Key *)a;
  76. const Key *key_b = (Key *)b;
  77. //Con::printf( "*** %f %f", key_a->time, key_b->time );
  78. //return key_a->time < key_b->time;
  79. if (key_a->time > key_b->time)
  80. return 1;
  81. else if (key_a->time < key_b->time)
  82. return -1;
  83. else
  84. return 0;
  85. }
  86. void afxAnimCurve::sort( )
  87. {
  88. if( keys.size() == 0 )
  89. return;
  90. //std::sort( keys.begin(), keys.end(), afxAnimCurve::compare_Key );
  91. dQsort( keys.address(), keys.size(), sizeof(Key), afxAnimCurve::compare_Key );
  92. start_value = keys[0].value;
  93. final_value = keys[keys.size()-1].value;
  94. start_time = keys[0].time;
  95. final_time = keys[keys.size()-1].time;
  96. usable = true;
  97. }
  98. int afxAnimCurve::numKeys()
  99. {
  100. return keys.size();
  101. }
  102. F32 afxAnimCurve::getKeyTime( int index )
  103. {
  104. if( ( index < 0 ) || ( index >= keys.size() ) )
  105. return 0.0f;
  106. Key &k = keys[index];
  107. return k.time;
  108. }
  109. F32 afxAnimCurve::getKeyValue( int index )
  110. {
  111. if( ( index < 0 ) || ( index >= keys.size() ) )
  112. return 0.0f;
  113. Key &k = keys[index];
  114. return k.value;
  115. }
  116. Point2F afxAnimCurve::getSegment( F32 time )
  117. {
  118. Point2F segment( 0, 0 );
  119. if( keys.size() == 0 )
  120. return segment;
  121. int start_index = 0;
  122. for( ; start_index < keys.size()-1; start_index++ )
  123. {
  124. if( time < keys[start_index+1].time )
  125. break;
  126. }
  127. int end_index = start_index+1;
  128. segment.x = (F32)start_index;
  129. segment.y = (F32)end_index;
  130. return segment;
  131. }
  132. F32 afxAnimCurve::evaluate( F32 time )
  133. {
  134. if( !usable )
  135. return 0.0f;
  136. if( time <= start_time )
  137. return start_value;
  138. if( time >= final_time )
  139. return final_value;
  140. if( keys.size() == 1 )
  141. return start_value;
  142. int start_index = 0;
  143. for( ; start_index < keys.size()-1; start_index++ )
  144. {
  145. if( time < keys[start_index+1].time )
  146. break;
  147. }
  148. int end_index = start_index+1;
  149. Key k0 = keys[start_index];
  150. Key k1 = keys[end_index];
  151. Point2F v0( (F32) k0.time, k0.value );
  152. Point2F v1( (F32) k1.time, k1.value );
  153. // Compute tangents
  154. Point2F tan0 = computeTangentK0( v0, v1, start_index );
  155. Point2F tan1 = computeTangentK1( v0, v1, end_index );
  156. F32 time_perc = (F32)( time - k0.time ) / (F32)( k1.time - k0.time );
  157. Point2F vnew = evaluator->evaluateCurve( v0,
  158. v1,
  159. tan0,
  160. tan1,
  161. time_perc );
  162. return vnew.y;
  163. }
  164. Point2F afxAnimCurve::computeTangentK0( Point2F &k0, Point2F &k1, int start_index )
  165. {
  166. Point2F tan0;
  167. Point2F k_prev;
  168. Point2F k_next;
  169. // tangent for k0
  170. if( start_index == 0 )
  171. {
  172. k_prev = k0; // Setting previous point to k0, creating a hidden point in
  173. // the same spot
  174. k_next = k1;
  175. }
  176. else
  177. {
  178. Key &k = keys[start_index-1];
  179. k_prev.set( k.time, k.value );
  180. k_next = k1;
  181. }
  182. tan0 = k_next-k_prev; //k_next.subtract( k_prev );
  183. tan0 *= .5f;
  184. return tan0;
  185. }
  186. Point2F afxAnimCurve::computeTangentK1( Point2F &k0, Point2F &k1, int end_index )
  187. {
  188. Point2F tan1;
  189. Point2F k_prev;
  190. Point2F k_next;
  191. // tangent for k1
  192. if( end_index == keys.size()-1 )
  193. {
  194. k_prev = k0;
  195. k_next = k1; // Setting next point to k1, creating a hidden point in
  196. // the same spot
  197. }
  198. else
  199. {
  200. k_prev = k0;
  201. Key &k = keys[end_index+1];
  202. k_next.set( k.time, k.value );
  203. }
  204. tan1 = k_next-k_prev; //k_next.subtract( k_prev );
  205. tan1 *= .5f;
  206. return tan1;
  207. }
  208. void afxAnimCurve::print()
  209. {
  210. Con::printf( "afxAnimCurve -------------------------" );
  211. for( int i = 0; i < keys.size(); i++ )
  212. {
  213. Key &k = keys[i];
  214. Con::printf( "%f: %f", k.time, k.value );
  215. }
  216. Con::printf( "-----------------------------------" );
  217. }
  218. void afxAnimCurve::printKey( int index )
  219. {
  220. Key &k = keys[index];
  221. Con::printf( "%f: %f", k.time, k.value );
  222. }