X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Fbezier.cpp;h=da6533656847bf97eca50bba9212898a24754762;hp=f0e1f4d88721a693d25c94a2e2ce982534441fa4;hb=e35bf651e7ebfe4932e877780bb00397c41a7ec2;hpb=b3c2d3472c3b3d77eae0f66731a32b852dce11f0 diff --git a/src/bezier.cpp b/src/bezier.cpp index f0e1f4d..da65336 100644 --- a/src/bezier.cpp +++ b/src/bezier.cpp @@ -2,6 +2,7 @@ #include #include +#include using namespace std; @@ -45,4 +46,95 @@ Real Bernstein(int k, int n, const Real & u) return Real(BinomialCoeff(n, k)) * Power(u, k) * Power(Real(1.0) - u, n-k); } + +/** + * Returns the parametric parameter at the turning point(s) + * In one coordinate direction + */ + +static pair BezierTurningPoints(const Real & p0, const Real & p1, const Real & p2, const Real & p3) +{ + // straight line + if (p1 == p2 && p2 == p3) + { + return pair(0, 1); + } + Real a = (3*(p1-p2) + p3 - p0); + Real b = 2*(p2 - 2*p1 + p0); + Real c = (p1-p0); + if (a == 0) + { + if (b == 0) + return pair(0,1); + Real t = -c/b; + if (t > 1) t = 1; + if (t < 0) t = 0; + return pair(t, t); + } + Debug("a, b, c are %f, %f, %f", Float(a), Float(b), Float(c)); + if (b*b - 4*a*c < 0) + { + Debug("No real roots"); + return pair(0,1); + } + pair tsols = SolveQuadratic(a, b, c); + if (tsols.first > 1) tsols.first = 1; + if (tsols.first < 0) tsols.first = 0; + if (tsols.second > 1) tsols.second = 1; + if (tsols.second < 0) tsols.second = 0; + return tsols; } + +inline bool CompRealByPtr(const Real * a, const Real * b) +{ + return (*a) < (*b); +} + +/** + * Get Bounds Rectangle of Bezier + */ +Rect Bezier::SolveBounds() const +{ + Rect result; + pair tsols = BezierTurningPoints(x0, x1, x2, x3); + + Real tp0; Real tp1; Real o; + Evaluate(tp0, o, tsols.first); + Evaluate(tp1, o, tsols.second); + + Debug("x: tp0 is %f tp1 is %f", Float(tp0), Float(tp1)); + + vector v(4); + v[0] = &x0; + v[1] = &x3; + v[2] = &tp0; + v[3] = &tp1; + + // Not using a lambda to keep this compiling on cabellera + sort(v.begin(), v.end(), CompRealByPtr); + + result.x = *(v[0]); + result.w = *(v[3]) - result.x; + + // Do the same thing for y component (wow this is a mess) + tsols = BezierTurningPoints(y0, y1, y2, y3); + Evaluate(o, tp0, tsols.first); + Evaluate(o, tp1, tsols.second); + + + Debug("y: tp0 is %f tp1 is %f", Float(tp0), Float(tp1)); + + v[0] = &y0; + v[1] = &y3; + v[2] = &tp0; + v[3] = &tp1; + sort(v.begin(), v.end(), CompRealByPtr); + + result.y = *(v[0]); + result.h = *(v[3]) - result.y; + + Debug("Solved Bezier %s bounds as %s", Str().c_str(), result.Str().c_str()); + return result; +} + +} // end namespace