3 #include <unordered_map>
14 * Use dynamic programming / recursion
18 static unordered_map<int, int> dp;
19 static bool init = false;
28 int result = n*Factorial(n-1);
34 * Binomial coefficients
36 int BinomialCoeff(int n, int k)
38 return Factorial(n) / (Factorial(k) * Factorial(n-k));
42 * Bernstein Basis Polynomial
44 Real Bernstein(int k, int n, const Real & u)
46 return Real(BinomialCoeff(n, k)) * Power(u, k) * Power(Real(1.0) - u, n-k);
51 * Returns the parametric parameter at the turning point(s)
52 * In one coordinate direction
55 static pair<Real, Real> BezierTurningPoints(const Real & p0, const Real & p1, const Real & p2, const Real & p3)
58 if (p1 == p2 && p2 == p3)
60 return pair<Real,Real>(0, 1);
62 Real a = (3*(p1-p2) + p3 - p0);
63 Real b = 2*(p2 - 2*p1 + p0);
68 return pair<Real, Real>(0,1);
72 return pair<Real, Real>(t, t);
74 //Debug("a, b, c are %f, %f, %f", Float(a), Float(b), Float(c));
77 //Debug("No real roots");
78 return pair<Real, Real>(0,1);
80 pair<Real, Real> tsols = SolveQuadratic(a, b, c);
81 if (tsols.first > 1) tsols.first = 1;
82 if (tsols.first < 0) tsols.first = 0;
83 if (tsols.second > 1) tsols.second = 1;
84 if (tsols.second < 0) tsols.second = 0;
88 inline bool CompRealByPtr(const Real * a, const Real * b)
94 * Get Bounds Rectangle of Bezier
96 Rect Bezier::SolveBounds() const
99 pair<Real, Real> tsols = BezierTurningPoints(x0, x1, x2, x3);
101 Real tp0; Real tp1; Real o;
102 Evaluate(tp0, o, tsols.first);
103 Evaluate(tp1, o, tsols.second);
105 //Debug("x: tp0 is %f tp1 is %f", Float(tp0), Float(tp1));
107 vector<const Real*> v(4);
113 // Not using a lambda to keep this compiling on cabellera
114 sort(v.begin(), v.end(), CompRealByPtr);
117 result.w = *(v[3]) - result.x;
119 // Do the same thing for y component (wow this is a mess)
120 tsols = BezierTurningPoints(y0, y1, y2, y3);
121 Evaluate(o, tp0, tsols.first);
122 Evaluate(o, tp1, tsols.second);
125 //Debug("y: tp0 is %f tp1 is %f", Float(tp0), Float(tp1));
131 sort(v.begin(), v.end(), CompRealByPtr);
134 result.h = *(v[3]) - result.y;
136 //Debug("Solved Bezier %s bounds as %s", Str().c_str(), result.Str().c_str());