Shinya Kitaoka 810553
#pragma once
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
/*!
Shinya Kitaoka 120a6e
  Compute the forward differences.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void forwardDifferences(const std::vector<t> &coeff, std::vector<t> &diffs) {</t></t>
Shinya Kitaoka 120a6e
  diffs.clear();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::copy(coeff.begin(), coeff.end(), std::back_inserter(diffs));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  typename std::vector<t>::iterator vectIt;</t>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const UINT degree = coeff.size();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (UINT r = 0; r < degree; ++r) {
Shinya Kitaoka 120a6e
    vectIt = diffs.begin();
Shinya Kitaoka 120a6e
    std::advance(vectIt, r);
Shinya Kitaoka 120a6e
    std::adjacent_difference(vectIt, diffs.end(), vectIt);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Shinya Kitaoka 120a6e
  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>
Shinya Kitaoka 120a6e
void bezier2poly(const std::vector<t> &bez, std::vector<t> &coeff) {</t></t>
Shinya Kitaoka 120a6e
  forwardDifferences(bez, coeff);  // compute forward differences
Shinya Kitaoka 120a6e
                                   // and store them in coeff.
Shinya Kitaoka 120a6e
  int degree = bez.size() - 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double i_factorial = 1, n_r = 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  coeff[0] = bez[0];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 1; i < degree; i++) {
Shinya Kitaoka 120a6e
    i_factorial = i_factorial * i;
Shinya Kitaoka 120a6e
    n_r         = n_r * (degree - i + 1);
Shinya Kitaoka 120a6e
    i_factorial = 1.0 / i_factorial;
Shinya Kitaoka 120a6e
    coeff[i]    = (n_r * i_factorial) * coeff[i];
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Shinya Kitaoka 120a6e
  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>
Shinya Kitaoka 120a6e
void poly2bezier(const std::vector<t> &poly, std::vector<t> &bez) {</t></t>
Shinya Kitaoka 120a6e
  UINT n = poly.size();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bez.clear();
Shinya Kitaoka 120a6e
  std::copy(poly.begin(), poly.end(), std::back_inserter(bez));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double c, d, e;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (UINT j = 1; j < n; j++) {
Shinya Kitaoka 120a6e
    c = 1.0 / (n - j);
Shinya Kitaoka 120a6e
    d = 1;
Shinya Kitaoka 120a6e
    e = c;
Shinya Kitaoka 120a6e
    for (UINT i = n - 1; i >= j; i--) {
Shinya Kitaoka 120a6e
      bez[i] = d * bez[i] + e * bez[i - 1];
Shinya Kitaoka 120a6e
      d -= c;
Shinya Kitaoka 120a6e
      e += c;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TBEZIER_H
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------