2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __MATH_POLYNOMIAL_H__
|
|
|
|
#define __MATH_POLYNOMIAL_H__
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================================================
|
|
|
|
|
|
|
|
Polynomial of arbitrary degree with real coefficients.
|
|
|
|
|
|
|
|
===============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
class idPolynomial
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
public:
|
2012-11-28 15:47:07 +00:00
|
|
|
idPolynomial();
|
|
|
|
explicit idPolynomial( int d );
|
|
|
|
explicit idPolynomial( float a, float b );
|
|
|
|
explicit idPolynomial( float a, float b, float c );
|
|
|
|
explicit idPolynomial( float a, float b, float c, float d );
|
|
|
|
explicit idPolynomial( float a, float b, float c, float d, float e );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
float operator[]( int index ) const;
|
2012-11-28 15:47:07 +00:00
|
|
|
float& operator[]( int index );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idPolynomial operator-() const;
|
2012-11-28 15:47:07 +00:00
|
|
|
idPolynomial& operator=( const idPolynomial& p );
|
|
|
|
|
|
|
|
idPolynomial operator+( const idPolynomial& p ) const;
|
|
|
|
idPolynomial operator-( const idPolynomial& p ) const;
|
2012-11-26 18:58:24 +00:00
|
|
|
idPolynomial operator*( const float s ) const;
|
|
|
|
idPolynomial operator/( const float s ) const;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
idPolynomial& operator+=( const idPolynomial& p );
|
|
|
|
idPolynomial& operator-=( const idPolynomial& p );
|
|
|
|
idPolynomial& operator*=( const float s );
|
|
|
|
idPolynomial& operator/=( const float s );
|
|
|
|
|
|
|
|
bool Compare( const idPolynomial& p ) const; // exact compare, no epsilon
|
|
|
|
bool Compare( const idPolynomial& p, const float epsilon ) const;// compare with epsilon
|
|
|
|
bool operator==( const idPolynomial& p ) const; // exact compare, no epsilon
|
|
|
|
bool operator!=( const idPolynomial& p ) const; // exact compare, no epsilon
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
void Zero();
|
|
|
|
void Zero( int d );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int GetDimension() const; // get the degree of the polynomial
|
|
|
|
int GetDegree() const; // get the degree of the polynomial
|
|
|
|
float GetValue( const float x ) const; // evaluate the polynomial with the given real value
|
2012-11-28 15:47:07 +00:00
|
|
|
idComplex GetValue( const idComplex& x ) const; // evaluate the polynomial with the given complex value
|
2012-11-26 18:58:24 +00:00
|
|
|
idPolynomial GetDerivative() const; // get the first derivative of the polynomial
|
|
|
|
idPolynomial GetAntiDerivative() const; // get the anti derivative of the polynomial
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
int GetRoots( idComplex* roots ) const; // get all roots
|
|
|
|
int GetRoots( float* roots ) const; // get the real roots
|
|
|
|
|
|
|
|
static int GetRoots1( float a, float b, float* roots );
|
|
|
|
static int GetRoots2( float a, float b, float c, float* roots );
|
|
|
|
static int GetRoots3( float a, float b, float c, float d, float* roots );
|
|
|
|
static int GetRoots4( float a, float b, float c, float d, float e, float* roots );
|
|
|
|
|
|
|
|
const float* ToFloatPtr() const;
|
|
|
|
float* ToFloatPtr();
|
|
|
|
const char* ToString( int precision = 2 ) const;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
static void Test();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
private:
|
|
|
|
int degree;
|
|
|
|
int allocated;
|
2012-11-28 15:47:07 +00:00
|
|
|
float* coefficient;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
void Resize( int d, bool keep );
|
2012-11-28 15:47:07 +00:00
|
|
|
int Laguer( const idComplex* coef, const int degree, idComplex& r ) const;
|
2012-11-26 18:58:24 +00:00
|
|
|
};
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial::idPolynomial()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = -1;
|
|
|
|
allocated = 0;
|
|
|
|
coefficient = NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial::idPolynomial( int d )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = -1;
|
|
|
|
allocated = 0;
|
|
|
|
coefficient = NULL;
|
|
|
|
Resize( d, false );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial::idPolynomial( float a, float b )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = -1;
|
|
|
|
allocated = 0;
|
|
|
|
coefficient = NULL;
|
|
|
|
Resize( 1, false );
|
|
|
|
coefficient[0] = b;
|
|
|
|
coefficient[1] = a;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial::idPolynomial( float a, float b, float c )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = -1;
|
|
|
|
allocated = 0;
|
|
|
|
coefficient = NULL;
|
|
|
|
Resize( 2, false );
|
|
|
|
coefficient[0] = c;
|
|
|
|
coefficient[1] = b;
|
|
|
|
coefficient[2] = a;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial::idPolynomial( float a, float b, float c, float d )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = -1;
|
|
|
|
allocated = 0;
|
|
|
|
coefficient = NULL;
|
|
|
|
Resize( 3, false );
|
|
|
|
coefficient[0] = d;
|
|
|
|
coefficient[1] = c;
|
|
|
|
coefficient[2] = b;
|
|
|
|
coefficient[3] = a;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial::idPolynomial( float a, float b, float c, float d, float e )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = -1;
|
|
|
|
allocated = 0;
|
|
|
|
coefficient = NULL;
|
|
|
|
Resize( 4, false );
|
|
|
|
coefficient[0] = e;
|
|
|
|
coefficient[1] = d;
|
|
|
|
coefficient[2] = c;
|
|
|
|
coefficient[3] = b;
|
|
|
|
coefficient[4] = a;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE float idPolynomial::operator[]( int index ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( index >= 0 && index <= degree );
|
|
|
|
return coefficient[ index ];
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE float& idPolynomial::operator[]( int index )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( index >= 0 && index <= degree );
|
|
|
|
return coefficient[ index ];
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial idPolynomial::operator-() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
|
|
|
idPolynomial n;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
n = *this;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n[i] = -n[i];
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial& idPolynomial::operator=( const idPolynomial& p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Resize( p.degree, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] = p.coefficient[i];
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial idPolynomial::operator+( const idPolynomial& p ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
|
|
|
idPolynomial n;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( degree > p.degree )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.Resize( degree, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= p.degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i] + p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i];
|
|
|
|
}
|
|
|
|
n.degree = degree;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( p.degree > degree )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.Resize( p.degree, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i] + p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i <= p.degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = p.coefficient[i];
|
|
|
|
}
|
|
|
|
n.degree = p.degree;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.Resize( degree, false );
|
|
|
|
n.degree = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i] + p.coefficient[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( n.coefficient[i] != 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.degree = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial idPolynomial::operator-( const idPolynomial& p ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
|
|
|
idPolynomial n;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( degree > p.degree )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.Resize( degree, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= p.degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i] - p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i];
|
|
|
|
}
|
|
|
|
n.degree = degree;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( p.degree >= degree )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.Resize( p.degree, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i] - p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i <= p.degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = - p.coefficient[i];
|
|
|
|
}
|
|
|
|
n.degree = p.degree;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.Resize( degree, false );
|
|
|
|
n.degree = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i] - p.coefficient[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( n.coefficient[i] != 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.degree = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial idPolynomial::operator*( const float s ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idPolynomial n;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( s == 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.degree = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.Resize( degree, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i] * s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial idPolynomial::operator/( const float s ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float invs;
|
|
|
|
idPolynomial n;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( s != 0.0f );
|
|
|
|
n.Resize( degree, false );
|
|
|
|
invs = 1.0f / s;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
n.coefficient[i] = coefficient[i] * invs;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial& idPolynomial::operator+=( const idPolynomial& p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( degree > p.degree )
|
|
|
|
{
|
|
|
|
for( i = 0; i <= p.degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] += p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( p.degree > degree )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Resize( p.degree, true );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] += p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i <= p.degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] = p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] += p.coefficient[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( coefficient[i] != 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial& idPolynomial::operator-=( const idPolynomial& p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( degree > p.degree )
|
|
|
|
{
|
|
|
|
for( i = 0; i <= p.degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] -= p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( p.degree > degree )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Resize( p.degree, true );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] -= p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i <= p.degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] = - p.coefficient[i];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] -= p.coefficient[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( coefficient[i] != 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial& idPolynomial::operator*=( const float s )
|
|
|
|
{
|
|
|
|
if( s == 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] *= s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial& idPolynomial::operator/=( const float s )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float invs;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( s != 0.0f );
|
|
|
|
invs = 1.0f / s;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] = invs;
|
|
|
|
}
|
|
|
|
return *this;;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idPolynomial::Compare( const idPolynomial& p ) const
|
|
|
|
{
|
|
|
|
if( degree != p.degree )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
|
|
|
if( coefficient[i] != p.coefficient[i] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idPolynomial::Compare( const idPolynomial& p, const float epsilon ) const
|
|
|
|
{
|
|
|
|
if( degree != p.degree )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
|
|
|
if( idMath::Fabs( coefficient[i] - p.coefficient[i] ) > epsilon )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idPolynomial::operator==( const idPolynomial& p ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return Compare( p );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE bool idPolynomial::operator!=( const idPolynomial& p ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return !Compare( p );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idPolynomial::Zero()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
degree = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idPolynomial::Zero( int d )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Resize( d, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
coefficient[i] = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE int idPolynomial::GetDimension() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return degree;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE int idPolynomial::GetDegree() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return degree;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE float idPolynomial::GetValue( const float x ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float y, z;
|
|
|
|
y = coefficient[0];
|
|
|
|
z = x;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 1; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
y += coefficient[i] * z;
|
|
|
|
z *= x;
|
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idComplex idPolynomial::GetValue( const idComplex& x ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idComplex y, z;
|
|
|
|
y.Set( coefficient[0], 0.0f );
|
|
|
|
z = x;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 1; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
y += coefficient[i] * z;
|
|
|
|
z *= x;
|
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial idPolynomial::GetDerivative() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idPolynomial n;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( degree == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
n.Resize( degree - 1, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 1; i <= degree; i++ )
|
|
|
|
{
|
|
|
|
n.coefficient[i - 1] = i * coefficient[i];
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idPolynomial idPolynomial::GetAntiDerivative() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idPolynomial n;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( degree == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
n.Resize( degree + 1, false );
|
|
|
|
n.coefficient[0] = 0.0f;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
|
|
|
n.coefficient[i + 1] = coefficient[i] / ( i + 1 );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE int idPolynomial::GetRoots1( float a, float b, float* roots )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( a != 0.0f );
|
|
|
|
roots[0] = - b / a;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE int idPolynomial::GetRoots2( float a, float b, float c, float* roots )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float inva, ds;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( a != 1.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( a != 0.0f );
|
|
|
|
inva = 1.0f / a;
|
|
|
|
c *= inva;
|
|
|
|
b *= inva;
|
|
|
|
}
|
|
|
|
ds = b * b - 4.0f * c;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ds < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( ds > 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ds = idMath::Sqrt( ds );
|
|
|
|
roots[0] = 0.5f * ( -b - ds );
|
|
|
|
roots[1] = 0.5f * ( -b + ds );
|
|
|
|
return 2;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
roots[0] = 0.5f * -b;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE int idPolynomial::GetRoots3( float a, float b, float c, float d, float* roots )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
float inva, f, g, halfg, ofs, ds, dist, angle, cs, ss, t;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( a != 1.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( a != 0.0f );
|
|
|
|
inva = 1.0f / a;
|
|
|
|
d *= inva;
|
|
|
|
c *= inva;
|
|
|
|
b *= inva;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
f = ( 1.0f / 3.0f ) * ( 3.0f * c - b * b );
|
|
|
|
g = ( 1.0f / 27.0f ) * ( 2.0f * b * b * b - 9.0f * c * b + 27.0f * d );
|
|
|
|
halfg = 0.5f * g;
|
|
|
|
ofs = ( 1.0f / 3.0f ) * b;
|
|
|
|
ds = 0.25f * g * g + ( 1.0f / 27.0f ) * f * f * f;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ds < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
dist = idMath::Sqrt( ( -1.0f / 3.0f ) * f );
|
|
|
|
angle = ( 1.0f / 3.0f ) * idMath::ATan( idMath::Sqrt( -ds ), -halfg );
|
|
|
|
cs = idMath::Cos( angle );
|
|
|
|
ss = idMath::Sin( angle );
|
|
|
|
roots[0] = 2.0f * dist * cs - ofs;
|
|
|
|
roots[1] = -dist * ( cs + idMath::SQRT_THREE * ss ) - ofs;
|
|
|
|
roots[2] = -dist * ( cs - idMath::SQRT_THREE * ss ) - ofs;
|
|
|
|
return 3;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( ds > 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ds = idMath::Sqrt( ds );
|
|
|
|
t = -halfg + ds;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( t >= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
roots[0] = idMath::Pow( t, ( 1.0f / 3.0f ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
roots[0] = -idMath::Pow( -t, ( 1.0f / 3.0f ) );
|
|
|
|
}
|
|
|
|
t = -halfg - ds;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( t >= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
roots[0] += idMath::Pow( t, ( 1.0f / 3.0f ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
roots[0] -= idMath::Pow( -t, ( 1.0f / 3.0f ) );
|
|
|
|
}
|
|
|
|
roots[0] -= ofs;
|
|
|
|
return 1;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( halfg >= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
t = -idMath::Pow( halfg, ( 1.0f / 3.0f ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
t = idMath::Pow( -halfg, ( 1.0f / 3.0f ) );
|
|
|
|
}
|
|
|
|
roots[0] = 2.0f * t - ofs;
|
|
|
|
roots[1] = -t - ofs;
|
|
|
|
roots[2] = roots[1];
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE int idPolynomial::GetRoots4( float a, float b, float c, float d, float e, float* roots )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int count;
|
|
|
|
float inva, y, ds, r, s1, s2, t1, t2, tp, tm;
|
|
|
|
float roots3[3];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( a != 1.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( a != 0.0f );
|
|
|
|
inva = 1.0f / a;
|
|
|
|
e *= inva;
|
|
|
|
d *= inva;
|
|
|
|
c *= inva;
|
|
|
|
b *= inva;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
count = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
GetRoots3( 1.0f, -c, b * d - 4.0f * e, -b * b * e + 4.0f * c * e - d * d, roots3 );
|
|
|
|
y = roots3[0];
|
|
|
|
ds = 0.25f * b * b - c + y;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ds < 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( ds > 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
r = idMath::Sqrt( ds );
|
|
|
|
t1 = 0.75f * b * b - r * r - 2.0f * c;
|
|
|
|
t2 = ( 4.0f * b * c - 8.0f * d - b * b * b ) / ( 4.0f * r );
|
|
|
|
tp = t1 + t2;
|
|
|
|
tm = t1 - t2;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( tp >= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
s1 = idMath::Sqrt( tp );
|
|
|
|
roots[count++] = -0.25f * b + 0.5f * ( r + s1 );
|
|
|
|
roots[count++] = -0.25f * b + 0.5f * ( r - s1 );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tm >= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
s2 = idMath::Sqrt( tm );
|
|
|
|
roots[count++] = -0.25f * b + 0.5f * ( s2 - r );
|
|
|
|
roots[count++] = -0.25f * b - 0.5f * ( s2 + r );
|
|
|
|
}
|
|
|
|
return count;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
t2 = y * y - 4.0f * e;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( t2 >= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
t2 = 2.0f * idMath::Sqrt( t2 );
|
|
|
|
t1 = 0.75f * b * b - 2.0f * c;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( t1 + t2 >= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
s1 = idMath::Sqrt( t1 + t2 );
|
|
|
|
roots[count++] = -0.25f * b + 0.5f * s1;
|
|
|
|
roots[count++] = -0.25f * b - 0.5f * s1;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( t1 - t2 >= 0.0f )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
s2 = idMath::Sqrt( t1 - t2 );
|
|
|
|
roots[count++] = -0.25f * b + 0.5f * s2;
|
|
|
|
roots[count++] = -0.25f * b - 0.5f * s2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE const float* idPolynomial::ToFloatPtr() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return coefficient;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE float* idPolynomial::ToFloatPtr()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return coefficient;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idPolynomial::Resize( int d, bool keep )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int alloc = ( d + 1 + 3 ) & ~3;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( alloc > allocated )
|
|
|
|
{
|
|
|
|
float* ptr = ( float* ) Mem_Alloc16( alloc * sizeof( float ), TAG_MATH );
|
|
|
|
if( coefficient != NULL )
|
|
|
|
{
|
|
|
|
if( keep )
|
|
|
|
{
|
|
|
|
for( int i = 0; i <= degree; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ptr[i] = coefficient[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Mem_Free16( coefficient );
|
|
|
|
}
|
|
|
|
allocated = alloc;
|
|
|
|
coefficient = ptr;
|
|
|
|
}
|
|
|
|
degree = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !__MATH_POLYNOMIAL_H__ */
|