Cad Cam Development



Bernstein polynomial basis Evaluation

Bernstein polynomial basis plots.

Bernstein polynomials are fundamental in CAD/CAM design due to their perfect numerical properties. They are used in Bezier Curves drawing. It is not so difficult to compute them on computer. Please see the code below to see how I managed it. I have computed the first six basis (n=0,..,6) in preprocessing so that further calculations for instance in DeCasteljau algorithm could be faster.

Short struct to store Bernstein polynomial. Note that only coefficients are important.


public class BernsteinPolynomial
 {
 public int M { get; set; }
 internal decimal[] BernsteinCoefficients;

 public BernsteinPolynomial(decimal[] coefficients)
 {
 this.M = coefficients.GetLength( 0 );
 this.BernsteinCoefficients = coefficients;
 }

}

 


class BernsteinBasisEvaluator : IPolynomialValue
 {
 BernsteinPolynomial polynomial;
 double[,] basisCoefficients;
 public static SortedDictionary<int, double[,]> Basis { get; set; }

 public BernsteinBasisEvaluator(BernsteinPolynomial polynomial)
 {
 this.polynomial = polynomial;
 if (BernsteinBasisEvaluator.Basis.ContainsKey( polynomial.M ))
 {
 this.basisCoefficients = new double[polynomial.M, polynomial.M];

 for (int i = 0; i < this.basisCoefficients.GetLength( 0 ); i++)
 {
 for (int j = 0; j < this.basisCoefficients.GetLength( 1 ); j++)
 {
 this.basisCoefficients[i, j] = BernsteinBasisEvaluator.Basis[polynomial.M][i, j];
 }
 }
 }
 else
 {
 BernsteinBasisEvaluator.CalculateBernsteinBaseCoefficients( polynomial.M, out this.basisCoefficients );
 }
 }

static BernsteinBasisEvaluator()
 {
 Basis = new SortedDictionary<int, double[,]>();
 for (int i = 2; i < 6; i++)
 {
 double[,] b;
 CalculateBernsteinBaseCoefficients(i,out b);
 Basis.Add( i, b );
 }
 }

 private static void CalculateBernsteinBaseCoefficients(int m, out double[,] baseCoeffcients)
 {
 baseCoeffcients = new double[m, m];
 for (int i = 0; i < m; i++)
 {
 for (int j = i; j < m; j++)
 {
 NewtonSymbolValue ns1 = new NewtonSymbolValue( m - 1, j );
 NewtonSymbolValue ns2 = new NewtonSymbolValue( j, i );
 baseCoeffcients[j, i] = Math.Pow( -1.0, j - i ) * ns1.GetValue() * ns2.GetValue();
 }
 }
 }

}

De Casteljau algorithm:


public decimal GetValue(int i, decimal t)
 {
 decimal[,] resultTable = new decimal[this.polynomial.M, this.polynomial.M];
 QuickCopy( resultTable, this.basisCoefficients );

 decimal u = 1m - t;
 if (u < 0)
 u = 0;

 for (int k = 1; k < this.basisCoefficients.GetLength( 0 ); k++)
 {
 for (int l = 0; l <= i; l++)
 {
 if (l == 0)
 {
 resultTable[k, l] = resultTable[k - 1, l] * u;
 }
 else
 {
 resultTable[k, l] = resultTable[k - 1, l] * u + resultTable[k - 1, l - 1] * t;
 }
 }
 }

 return resultTable[this.polynomial.M - 1, i];

 }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s