Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  tbezier.h: contains useful function to manipulate bezier polynomious
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
#ifndef TBEZIER_H
Toshihiro Shimizu 890ddd
#define TBEZIER_H
Toshihiro Shimizu 890ddd
#include <iterator></iterator>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Compute the forward differences.  
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void forwardDifferences(
Toshihiro Shimizu 890ddd
	const vector<t> &coeff,</t>
Toshihiro Shimizu 890ddd
	vector<t> &diffs)</t>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	diffs.clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::copy(coeff.begin(), coeff.end(), std::back_inserter(diffs));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	typename std::vector<t>::iterator vectIt;</t>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const UINT degree = coeff.size();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (UINT r = 0; r < degree; ++r) {
Toshihiro Shimizu 890ddd
		vectIt = diffs.begin();
Toshihiro Shimizu 890ddd
		std::advance(vectIt, r);
Toshihiro Shimizu 890ddd
		std::adjacent_difference(vectIt, diffs.end(), vectIt);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Converts Bezier form to power (polynomial) form. 
Toshihiro Shimizu 890ddd
  \par  bez coefficients in Bezier form.
Toshihiro Shimizu 890ddd
  \ret  coeff coefficients of power form.
Toshihiro Shimizu 890ddd
  \note coeffs are ordered from smaller b[0] to greater b[size-1] .
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void bezier2poly(const vector<t> &bez,</t>
Toshihiro Shimizu 890ddd
				 vector<t> &coeff)</t>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	forwardDifferences(bez, coeff); // compute forward differences
Toshihiro Shimizu 890ddd
									// and store them in coeff.
Toshihiro Shimizu 890ddd
	int
Toshihiro Shimizu 890ddd
		degree = bez.size() - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double
Toshihiro Shimizu 890ddd
		i_factorial = 1,
Toshihiro Shimizu 890ddd
		n_r = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	coeff[0] = bez[0];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 1; i < degree; i++) {
Toshihiro Shimizu 890ddd
		i_factorial = i_factorial * i;
Toshihiro Shimizu 890ddd
		n_r = n_r * (degree - i + 1);
Toshihiro Shimizu 890ddd
		i_factorial = 1.0 / i_factorial;
Toshihiro Shimizu 890ddd
		coeff[i] = (n_r * i_factorial) * coeff[i];
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Converts power (polynomial) form to Bezier form. 
Toshihiro Shimizu 890ddd
  \par  coeff coefficients of power form.
Toshihiro Shimizu 890ddd
  \ret  bez coefficients in Bezier form.
Toshihiro Shimizu 890ddd
  \note coeffs are ordered from smaller b[0] to greater b[size-1].
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void poly2bezier(const vector<t> &poly,</t>
Toshihiro Shimizu 890ddd
				 vector<t> &bez)</t>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT n = poly.size();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bez.clear();
Toshihiro Shimizu 890ddd
	std::copy(poly.begin(), poly.end(), std::back_inserter(bez));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double c, d, e;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (UINT j = 1; j < n; j++) {
Toshihiro Shimizu 890ddd
		c = 1.0 / (n - j);
Toshihiro Shimizu 890ddd
		d = 1;
Toshihiro Shimizu 890ddd
		e = c;
Toshihiro Shimizu 890ddd
		for (UINT i = n - 1; i >= j; i--) {
Toshihiro Shimizu 890ddd
			bez[i] = d * bez[i] + e * bez[i - 1];
Toshihiro Shimizu 890ddd
			d -= c;
Toshihiro Shimizu 890ddd
			e += c;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // TBEZIER_H
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------