tResponseCurve.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Notice:
  23. // Some of this code originates from an article in AI Game Programming Wisdom
  24. // by Dave Mark.
  25. #ifndef _TRESPONSECURVE_H_
  26. #define _TRESPONSECURVE_H_
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _MMATHFN_H_
  31. #include "math/mMathFn.h"
  32. #endif
  33. #ifndef _SIMOBJECT_H_
  34. #include "console/simObject.h"
  35. #endif
  36. // -------------------------------
  37. // Represents a sigmoid function
  38. // Note: not used by ResponseCurve
  39. // -------------------------------
  40. class Sigmoid
  41. {
  42. public:
  43. Sigmoid( F32 s, F32 m) { _s = s; _m = m; }
  44. ~Sigmoid() {};
  45. inline F32 get( F32 x )
  46. {
  47. F32 pow = -2.0f * ( ( x - _m ) / _s );
  48. F32 y = 1.0f / mPow( 1 + M_CONST_E_F, pow );
  49. return y;
  50. }
  51. F32 _s;
  52. F32 _m;
  53. };
  54. // -----------------------------------------------------------------------------
  55. // Represents a response curve, can query values
  56. // -----------------------------------------------------------------------------
  57. template< class T >
  58. class ResponseCurve
  59. {
  60. public:
  61. struct Sample
  62. {
  63. Sample() :mF(0) { dMemset(&mVal, 0, sizeof(mVal)); }
  64. Sample( F32 f, const T &val ) : mF(f), mVal(val) {}
  65. F32 mF;
  66. T mVal;
  67. };
  68. typedef Vector< Sample > SampleList;
  69. SampleList mSamples;
  70. const SampleList& getSamples() { return mSamples; }
  71. ResponseCurve() {}
  72. ResponseCurve( U32 numSamples ) { mSamples.reserve(numSamples); }
  73. void clear() { mSamples.clear(); }
  74. void addPoint( F32 f, const T &val );
  75. //void addPoints( U32 count, F32 f[], const T &val[] );
  76. T getVal( F32 f ) const;
  77. S32 setPoint( S32 idx, F32 f, const T &val );
  78. void removePoint( S32 idx );
  79. S32 getSampleCount() const { return mSamples.size(); }
  80. };
  81. //-----------------------------------------------------------------------------
  82. // Adds a new value to the Response Curve, at the position f
  83. //-----------------------------------------------------------------------------
  84. template< class T >
  85. inline void ResponseCurve<T>::addPoint( F32 f, const T &val )
  86. {
  87. typename SampleList::iterator iter = mSamples.begin();
  88. for ( ; iter != mSamples.end(); iter++ )
  89. {
  90. if ( iter->mF == f )
  91. {
  92. Con::warnf( "Warn: ResponseCurve<T>::AddPoint, Duplicate values are not allowed." );
  93. return;
  94. }
  95. if ( iter->mF > f )
  96. break;
  97. }
  98. mSamples.insert( iter, Sample( f, val ) );
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Finds the right value at position f, interpolating between the previous
  102. // and the following values
  103. //-----------------------------------------------------------------------------
  104. template< class T >
  105. inline T ResponseCurve<T>::getVal( F32 f ) const
  106. {
  107. T retVal;
  108. if ( mSamples.empty() )
  109. {
  110. retVal = T();
  111. }
  112. else
  113. {
  114. U32 nSamples = mSamples.size();
  115. if ( nSamples == 1 || f <= mSamples[0].mF )
  116. {
  117. retVal = mSamples[0].mVal;
  118. }
  119. else if ( f >= mSamples[nSamples-1].mF )
  120. {
  121. retVal = mSamples[nSamples-1].mVal;
  122. }
  123. else
  124. {
  125. U32 i = 1;
  126. while ( i < (nSamples-1) && mSamples[i].mF < f )
  127. ++i;
  128. // Interpolate between m_Samples[i-1] and m_Samples[i]
  129. F32 fSampleMin = mSamples[i-1].mF;
  130. F32 fSampleMax = mSamples[i].mF;
  131. AssertWarn(fSampleMin != fSampleMax, "fSampleMin should not equal fSampleMax" );
  132. F32 t = (f - fSampleMin) / (fSampleMax - fSampleMin);
  133. retVal = mSamples[i-1].mVal + ( mSamples[i].mVal - mSamples[i-1].mVal) * t;
  134. }
  135. }
  136. return retVal;
  137. }
  138. template< class T >
  139. inline S32 ResponseCurve< T >::setPoint( S32 idx, F32 f, const T &val )
  140. {
  141. mSamples.erase( idx );
  142. typename SampleList::iterator iter = mSamples.begin();
  143. for ( ; iter != mSamples.end(); iter++ )
  144. {
  145. if ( iter->mF == f )
  146. {
  147. Con::warnf( "Warn: ResponseCurve<T>::AddPoint, Duplicate values are not allowed." );
  148. return -1;
  149. }
  150. if ( iter->mF > f )
  151. break;
  152. }
  153. mSamples.insert( iter, Sample( f, val ) );
  154. return (S32)( iter - mSamples.begin() );
  155. }
  156. class FloatCurve : public ResponseCurve<F32>
  157. {
  158. public:
  159. FloatCurve() {}
  160. };
  161. // -----------------------------------------------
  162. // A ResponseCurve<F32> wrapped as a SimObject
  163. // -----------------------------------------------
  164. class SimResponseCurve : public SimObject
  165. {
  166. typedef SimObject Parent;
  167. public:
  168. SimResponseCurve();
  169. //~SimResponseCurve();
  170. DECLARE_CONOBJECT( SimResponseCurve );
  171. virtual bool onAdd();
  172. virtual void onRemove();
  173. void addPoint( F32 value, F32 time );
  174. F32 getValue( F32 time );
  175. void clear();
  176. ResponseCurve<F32> mCurve;
  177. };
  178. // A networked-datablock version of ResponseCurve
  179. /*
  180. class ResponseCurveData : public SimDataBlock
  181. {
  182. typedef SimDataBlock Parent;
  183. public:
  184. ResponseCurveData();
  185. //~ResponseCurveData();
  186. DECLARE_CONOBJECT( ResponseCurveData );
  187. virtual bool onAdd();
  188. virtual void onRemove();
  189. void addPoint( F32 value, F32 time );
  190. F32 getValue( F32 time );
  191. void clear();
  192. ResponseCurve<F32> mCurve;
  193. };
  194. */
  195. #endif