123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- /*
- ===========================================================================
- Doom 3 BFG Edition GPL Source Code
- Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
- This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
- Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
- 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.
- 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.
- ===========================================================================
- */
- #pragma hdrstop
- #include "../precompiled.h"
- //===============================================================
- //
- // idODE_Euler
- //
- //===============================================================
- /*
- =============
- idODE_Euler::idODE_Euler
- =============
- */
- idODE_Euler::idODE_Euler( const int dim, deriveFunction_t dr, const void *ud ) {
- dimension = dim;
- derivatives = new (TAG_MATH) float[dim];
- derive = dr;
- userData = ud;
- }
- /*
- =============
- idODE_Euler::~idODE_Euler
- =============
- */
- idODE_Euler::~idODE_Euler() {
- delete[] derivatives;
- }
- /*
- =============
- idODE_Euler::Evaluate
- =============
- */
- float idODE_Euler::Evaluate( const float *state, float *newState, float t0, float t1 ) {
- float delta;
- int i;
- derive( t0, userData, state, derivatives );
- delta = t1 - t0;
- for ( i = 0; i < dimension; i++ ) {
- newState[i] = state[i] + delta * derivatives[i];
- }
- return delta;
- }
- //===============================================================
- //
- // idODE_Midpoint
- //
- //===============================================================
- /*
- =============
- idODE_Midpoint::idODE_Midpoint
- =============
- */
- idODE_Midpoint::idODE_Midpoint( const int dim, deriveFunction_t dr, const void *ud ) {
- dimension = dim;
- tmpState = new (TAG_MATH) float[dim];
- derivatives = new (TAG_MATH) float[dim];
- derive = dr;
- userData = ud;
- }
- /*
- =============
- idODE_Midpoint::~idODE_Midpoint
- =============
- */
- idODE_Midpoint::~idODE_Midpoint() {
- delete tmpState;
- delete derivatives;
- }
- /*
- =============
- idODE_Midpoint::~Evaluate
- =============
- */
- float idODE_Midpoint::Evaluate( const float *state, float *newState, float t0, float t1 ) {
- double delta, halfDelta;
- int i;
- delta = t1 - t0;
- halfDelta = delta * 0.5;
- // first step
- derive( t0, userData, state, derivatives );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + halfDelta * derivatives[i];
- }
- // second step
- derive( t0 + halfDelta, userData, tmpState, derivatives );
- for ( i = 0; i < dimension; i++ ) {
- newState[i] = state[i] + delta * derivatives[i];
- }
- return delta;
- }
- //===============================================================
- //
- // idODE_RK4
- //
- //===============================================================
- /*
- =============
- idODE_RK4::idODE_RK4
- =============
- */
- idODE_RK4::idODE_RK4( const int dim, deriveFunction_t dr, const void *ud ) {
- dimension = dim;
- derive = dr;
- userData = ud;
- tmpState = new (TAG_MATH) float[dim];
- d1 = new (TAG_MATH) float[dim];
- d2 = new (TAG_MATH) float[dim];
- d3 = new (TAG_MATH) float[dim];
- d4 = new (TAG_MATH) float[dim];
- }
- /*
- =============
- idODE_RK4::~idODE_RK4
- =============
- */
- idODE_RK4::~idODE_RK4() {
- delete tmpState;
- delete d1;
- delete d2;
- delete d3;
- delete d4;
- }
- /*
- =============
- idODE_RK4::Evaluate
- =============
- */
- float idODE_RK4::Evaluate( const float *state, float *newState, float t0, float t1 ) {
- double delta, halfDelta, sixthDelta;
- int i;
- delta = t1 - t0;
- halfDelta = delta * 0.5;
- // first step
- derive( t0, userData, state, d1 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + halfDelta * d1[i];
- }
- // second step
- derive( t0 + halfDelta, userData, tmpState, d2 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + halfDelta * d2[i];
- }
- // third step
- derive( t0 + halfDelta, userData, tmpState, d3 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + delta * d3[i];
- }
- // fourth step
- derive( t0 + delta, userData, tmpState, d4 );
- sixthDelta = delta * (1.0/6.0);
- for ( i = 0; i < dimension; i++ ) {
- newState[i] = state[i] + sixthDelta * (d1[i] + 2.0 * (d2[i] + d3[i]) + d4[i]);
- }
- return delta;
- }
- //===============================================================
- //
- // idODE_RK4Adaptive
- //
- //===============================================================
- /*
- =============
- idODE_RK4Adaptive::idODE_RK4Adaptive
- =============
- */
- idODE_RK4Adaptive::idODE_RK4Adaptive( const int dim, deriveFunction_t dr, const void *ud ) {
- dimension = dim;
- derive = dr;
- userData = ud;
- maxError = 0.01f;
- tmpState = new (TAG_MATH) float[dim];
- d1 = new (TAG_MATH) float[dim];
- d1half = new (TAG_MATH) float [dim];
- d2 = new (TAG_MATH) float[dim];
- d3 = new (TAG_MATH) float[dim];
- d4 = new (TAG_MATH) float[dim];
- }
- /*
- =============
- idODE_RK4Adaptive::~idODE_RK4Adaptive
- =============
- */
- idODE_RK4Adaptive::~idODE_RK4Adaptive() {
- delete tmpState;
- delete d1;
- delete d1half;
- delete d2;
- delete d3;
- delete d4;
- }
- /*
- =============
- idODE_RK4Adaptive::SetMaxError
- =============
- */
- void idODE_RK4Adaptive::SetMaxError( const float err ) {
- if ( err > 0.0f ) {
- maxError = err;
- }
- }
- /*
- =============
- idODE_RK4Adaptive::Evaluate
- =============
- */
- float idODE_RK4Adaptive::Evaluate( const float *state, float *newState, float t0, float t1 ) {
- double delta, halfDelta, fourthDelta, sixthDelta;
- double error, max;
- int i, n;
- delta = t1 - t0;
- for ( n = 0; n < 4; n++ ) {
- halfDelta = delta * 0.5;
- fourthDelta = delta * 0.25;
- // first step of first half delta
- derive( t0, userData, state, d1 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + fourthDelta * d1[i];
- }
- // second step of first half delta
- derive( t0 + fourthDelta, userData, tmpState, d2 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + fourthDelta * d2[i];
- }
- // third step of first half delta
- derive( t0 + fourthDelta, userData, tmpState, d3 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + halfDelta * d3[i];
- }
- // fourth step of first half delta
- derive( t0 + halfDelta, userData, tmpState, d4 );
- sixthDelta = halfDelta * (1.0/6.0);
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + sixthDelta * (d1[i] + 2.0 * (d2[i] + d3[i]) + d4[i]);
- }
- // first step of second half delta
- derive( t0 + halfDelta, userData, tmpState, d1half );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + fourthDelta * d1half[i];
- }
- // second step of second half delta
- derive( t0 + halfDelta + fourthDelta, userData, tmpState, d2 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + fourthDelta * d2[i];
- }
- // third step of second half delta
- derive( t0 + halfDelta + fourthDelta, userData, tmpState, d3 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + halfDelta * d3[i];
- }
- // fourth step of second half delta
- derive( t0 + delta, userData, tmpState, d4 );
- sixthDelta = halfDelta * (1.0/6.0);
- for ( i = 0; i < dimension; i++ ) {
- newState[i] = state[i] + sixthDelta * (d1[i] + 2.0 * (d2[i] + d3[i]) + d4[i]);
- }
- // first step of full delta
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + halfDelta * d1[i];
- }
- // second step of full delta
- derive( t0 + halfDelta, userData, tmpState, d2 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + halfDelta * d2[i];
- }
- // third step of full delta
- derive( t0 + halfDelta, userData, tmpState, d3 );
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + delta * d3[i];
- }
- // fourth step of full delta
- derive( t0 + delta, userData, tmpState, d4 );
- sixthDelta = delta * (1.0/6.0);
- for ( i = 0; i < dimension; i++ ) {
- tmpState[i] = state[i] + sixthDelta * (d1[i] + 2.0 * (d2[i] + d3[i]) + d4[i]);
- }
- // get max estimated error
- max = 0.0;
- for ( i = 0; i < dimension; i++ ) {
- error = idMath::Fabs( (newState[i] - tmpState[i]) / (delta * d1[i] + 1e-10) );
- if ( error > max ) {
- max = error;
- }
- }
- error = max / maxError;
- if ( error <= 1.0f ) {
- return delta * 4.0;
- }
- if ( delta <= 1e-7 ) {
- return delta;
- }
- delta *= 0.25;
- }
- return delta;
- }
|