Ode.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __MATH_ODE_H__
  21. #define __MATH_ODE_H__
  22. /*
  23. ===============================================================================
  24. Numerical solvers for ordinary differential equations.
  25. ===============================================================================
  26. */
  27. //===============================================================
  28. //
  29. // idODE
  30. //
  31. //===============================================================
  32. typedef void (*deriveFunction_t)( const float t, const void *userData, const float *state, float *derivatives );
  33. class idODE {
  34. public:
  35. virtual ~idODE( void ) {}
  36. virtual float Evaluate( const float *state, float *newState, float t0, float t1 ) = 0;
  37. protected:
  38. int dimension; // dimension in floats allocated for
  39. deriveFunction_t derive; // derive function
  40. const void * userData; // client data
  41. };
  42. //===============================================================
  43. //
  44. // idODE_Euler
  45. //
  46. //===============================================================
  47. class idODE_Euler : public idODE {
  48. public:
  49. idODE_Euler( const int dim, const deriveFunction_t dr, const void *ud );
  50. virtual ~idODE_Euler( void );
  51. virtual float Evaluate( const float *state, float *newState, float t0, float t1 );
  52. protected:
  53. float * derivatives; // space to store derivatives
  54. };
  55. //===============================================================
  56. //
  57. // idODE_Midpoint
  58. //
  59. //===============================================================
  60. class idODE_Midpoint : public idODE {
  61. public:
  62. idODE_Midpoint( const int dim, const deriveFunction_t dr, const void *ud );
  63. virtual ~idODE_Midpoint( void );
  64. virtual float Evaluate( const float *state, float *newState, float t0, float t1 );
  65. protected:
  66. float * tmpState;
  67. float * derivatives; // space to store derivatives
  68. };
  69. //===============================================================
  70. //
  71. // idODE_RK4
  72. //
  73. //===============================================================
  74. class idODE_RK4 : public idODE {
  75. public:
  76. idODE_RK4( const int dim, const deriveFunction_t dr, const void *ud );
  77. virtual ~idODE_RK4( void );
  78. virtual float Evaluate( const float *state, float *newState, float t0, float t1 );
  79. protected:
  80. float * tmpState;
  81. float * d1; // derivatives
  82. float * d2;
  83. float * d3;
  84. float * d4;
  85. };
  86. //===============================================================
  87. //
  88. // idODE_RK4Adaptive
  89. //
  90. //===============================================================
  91. class idODE_RK4Adaptive : public idODE {
  92. public:
  93. idODE_RK4Adaptive( const int dim, const deriveFunction_t dr, const void *ud );
  94. virtual ~idODE_RK4Adaptive( void );
  95. virtual float Evaluate( const float *state, float *newState, float t0, float t1 );
  96. void SetMaxError( const float err );
  97. protected:
  98. float maxError; // maximum allowed error
  99. float * tmpState;
  100. float * d1; // derivatives
  101. float * d1half;
  102. float * d2;
  103. float * d3;
  104. float * d4;
  105. };
  106. #endif /* !__MATH_ODE_H__ */