DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Ode.h (4556B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition 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 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 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.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #ifndef __MATH_ODE_H__
     30 #define __MATH_ODE_H__
     31 
     32 /*
     33 ===============================================================================
     34 
     35 	Numerical solvers for ordinary differential equations.
     36 
     37 ===============================================================================
     38 */
     39 
     40 
     41 //===============================================================
     42 //
     43 //	idODE
     44 //
     45 //===============================================================
     46 
     47 typedef void (*deriveFunction_t)( const float t, const void *userData, const float *state, float *derivatives );
     48 
     49 class idODE {
     50 
     51 public:
     52 	virtual				~idODE() {}
     53 
     54 	virtual float		Evaluate( const float *state, float *newState, float t0, float t1 ) = 0;
     55 
     56 protected:
     57 	int					dimension;		// dimension in floats allocated for
     58 	deriveFunction_t	derive;			// derive function
     59 	const void *		userData;		// client data
     60 };
     61 
     62 //===============================================================
     63 //
     64 //	idODE_Euler
     65 //
     66 //===============================================================
     67 
     68 class idODE_Euler : public idODE {
     69 
     70 public:
     71 						idODE_Euler( const int dim, const deriveFunction_t dr, const void *ud );
     72 	virtual				~idODE_Euler();
     73 
     74 	virtual float		Evaluate( const float *state, float *newState, float t0, float t1 );
     75 
     76 protected:
     77 	float *				derivatives;	// space to store derivatives
     78 };
     79 
     80 //===============================================================
     81 //
     82 //	idODE_Midpoint
     83 //
     84 //===============================================================
     85 
     86 class idODE_Midpoint : public idODE {
     87 
     88 public:
     89 						idODE_Midpoint( const int dim, const deriveFunction_t dr, const void *ud );
     90 	virtual				~idODE_Midpoint();
     91 
     92 	virtual float		Evaluate( const float *state, float *newState, float t0, float t1 );
     93 
     94 protected:
     95 	float *				tmpState;
     96 	float *				derivatives;	// space to store derivatives
     97 };
     98 
     99 //===============================================================
    100 //
    101 //	idODE_RK4
    102 //
    103 //===============================================================
    104 
    105 class idODE_RK4 : public idODE {
    106 
    107 public:
    108 						idODE_RK4( const int dim, const deriveFunction_t dr, const void *ud );
    109 	virtual				~idODE_RK4();
    110 
    111 	virtual float		Evaluate( const float *state, float *newState, float t0, float t1 );
    112 
    113 protected:
    114 	float *				tmpState;
    115 	float *				d1;				// derivatives
    116 	float *				d2;
    117 	float *				d3;
    118 	float *				d4;
    119 };
    120 
    121 //===============================================================
    122 //
    123 //	idODE_RK4Adaptive
    124 //
    125 //===============================================================
    126 
    127 class idODE_RK4Adaptive : public idODE {
    128 
    129 public:
    130 						idODE_RK4Adaptive( const int dim, const deriveFunction_t dr, const void *ud );
    131 	virtual				~idODE_RK4Adaptive();
    132 
    133 	virtual float		Evaluate( const float *state, float *newState, float t0, float t1 );
    134 	void				SetMaxError( const float err );
    135 
    136 protected:
    137 	float				maxError;		// maximum allowed error
    138 	float *				tmpState;
    139 	float *				d1;				// derivatives
    140 	float *				d1half;
    141 	float *				d2;
    142 	float *				d3;
    143 	float *				d4;
    144 };
    145 
    146 #endif /* !__MATH_ODE_H__ */