+ BRect SolveBounds() const;
+
+ std::pair<BReal,BReal> GetTop() const;
+ std::pair<BReal,BReal> GetBottom() const;
+ std::pair<BReal,BReal> GetLeft() const;
+ std::pair<BReal,BReal> GetRight() const;
+
+ Bezier ToAbsolute(const BRect & bounds) const
+ {
+ return Bezier(*this, bounds);
+ }
+
+ /** Convert absolute control points to control points relative to bounds
+ * (This basically does the opposite of the Copy constructor)
+ * ie: If this is absolute, the returned Bezier will be relative to the bounds rectangle
+ */
+ Bezier ToRelative(const BRect & bounds) const
+ {
+ // x' <- (x - x0)/w etc
+ // special cases when w or h = 0
+ // (So can't just use the Copy constructor on the inverse of bounds)
+ // BRect inverse = {-bounds.x/bounds.w, -bounds.y/bounds.h, BReal(1)/bounds.w, BReal(1)/bounds.h};
+ Bezier result;
+ if (bounds.w == BReal(0))
+ {
+ result.x0 = 0;
+ result.x1 = 0;
+ result.x2 = 0;
+ result.x3 = 0;
+ }
+ else
+ {
+ result.x0 = (x0 - bounds.x)/bounds.w;
+ result.x1 = (x1 - bounds.x)/bounds.w;
+ result.x2 = (x2 - bounds.x)/bounds.w;
+ result.x3 = (x3 - bounds.x)/bounds.w;
+ }
+
+ if (bounds.h == BReal(0))
+ {
+ result.y0 = 0;
+ result.y1 = 0;
+ result.y2 = 0;
+ result.y3 = 0;
+ }
+ else
+ {
+ result.y0 = (y0 - bounds.y)/bounds.h;
+ result.y1 = (y1 - bounds.y)/bounds.h;
+ result.y2 = (y2 - bounds.y)/bounds.h;
+ result.y3 = (y3 - bounds.y)/bounds.h;
+ }
+ return result;
+ }
+
+ // Performs one round of De Casteljau subdivision and returns the [t,1] part.
+ Bezier DeCasteljauSubdivideLeft(const BReal& t)
+ {
+ BReal one_minus_t = BReal(1) - t;
+
+ // X Coordinates
+ BReal x01 = x1*t + x0*one_minus_t;
+ BReal x12 = x2*t + x1*one_minus_t;
+ BReal x23 = x3*t + x2*one_minus_t;
+
+ BReal x012 = x12*t + x01*one_minus_t;
+ BReal x123 = x23*t + x12*one_minus_t;
+
+ BReal x0123 = x123*t + x012*one_minus_t;
+
+ // Y Coordinates
+ BReal y01 = y1*t + y0*one_minus_t;
+ BReal y12 = y2*t + y1*one_minus_t;
+ BReal y23 = y3*t + y2*one_minus_t;
+
+ BReal y012 = y12*t + y01*one_minus_t;
+ BReal y123 = y23*t + y12*one_minus_t;
+
+ BReal y0123 = y123*t + y012*one_minus_t;
+
+ return Bezier(x0, y0, x01, y01, x012, y012, x0123, y0123);
+ }
+ // Performs one round of De Casteljau subdivision and returns the [t,1] part.
+ Bezier DeCasteljauSubdivideRight(const BReal& t)
+ {
+ BReal one_minus_t = BReal(1) - t;
+
+ // X Coordinates
+ BReal x01 = x1*t + x0*one_minus_t;
+ BReal x12 = x2*t + x1*one_minus_t;
+ BReal x23 = x3*t + x2*one_minus_t;
+
+ BReal x012 = x12*t + x01*one_minus_t;
+ BReal x123 = x23*t + x12*one_minus_t;
+
+ BReal x0123 = x123*t + x012*one_minus_t;
+
+ // Y Coordinates
+ BReal y01 = y1*t + y0*one_minus_t;
+ BReal y12 = y2*t + y1*one_minus_t;
+ BReal y23 = y3*t + y2*one_minus_t;
+
+ BReal y012 = y12*t + y01*one_minus_t;
+ BReal y123 = y23*t + y12*one_minus_t;
+
+ BReal y0123 = y123*t + y012*one_minus_t;
+
+ return Bezier(x0123, y0123, x123, y123, x23, y23, x3, y3);
+ }
+
+ Bezier ReParametrise(const BReal& t0, const BReal& t1)
+ {
+ //Debug("Reparametrise: %f -> %f",Double(t0),Double(t1));
+ Bezier new_bezier;
+ // Subdivide to get from [0,t1]
+ new_bezier = DeCasteljauSubdivideLeft(t1);
+ // Convert t0 from [0,1] range to [0, t1]
+ BReal new_t0 = t0 / t1;
+ //Debug("New t0 = %f", Double(new_t0));
+ new_bezier = new_bezier.DeCasteljauSubdivideRight(new_t0);
+
+ //Debug("%s becomes %s", this->Str().c_str(), new_bezier.Str().c_str());
+ return new_bezier;
+ }
+
+ std::vector<Bezier> ClipToRectangle(const BRect & r)
+ {
+ // Find points of intersection with the rectangle.
+ Debug("Clipping Bezier to BRect %s", r.Str().c_str());
+
+ bool isVerticalLine = false;//(x0 == x1 && x1 == x2 && x2 == x3);
+ bool isHorizontalLine = false;//(y0 == y1 && y1 == y2 && y2 == y3);
+
+ // Find its roots.
+
+ std::vector<BReal> intersection;
+
+ if (!isVerticalLine)
+ {
+ std::vector<BReal> x_intersection = SolveXParam(r.x);
+ intersection.insert(intersection.end(), x_intersection.begin(), x_intersection.end());
+ Debug("Number of top intersections: %d", x_intersection.size());
+
+ // And for the other side.
+
+ std::vector<BReal> x_intersection_pt2 = SolveXParam(r.x + r.w);
+ intersection.insert(intersection.end(), x_intersection_pt2.begin(), x_intersection_pt2.end());
+ Debug("Number of bottom intersections: %d", x_intersection_pt2.size());
+ }
+
+ // Find its roots.
+ if (!isHorizontalLine)
+ {
+ std::vector<BReal> y_intersection = SolveYParam(r.y);
+ intersection.insert(intersection.end(), y_intersection.begin(), y_intersection.end());
+ Debug("Number of left intersections: %d", y_intersection.size());
+
+ std::vector<BReal> y_intersection_pt2 = SolveYParam(r.y+r.h);
+ intersection.insert(intersection.end(), y_intersection_pt2.begin(), y_intersection_pt2.end());
+ Debug("Number of right intersections: %d", y_intersection_pt2.size());
+ }
+
+ // Merge and sort.
+ intersection.push_back(BReal(0));
+ intersection.push_back(BReal(1));
+ std::sort(intersection.begin(), intersection.end());
+ Debug("Number of intersections: %d", intersection.size());
+
+ std::vector<Bezier> all_beziers;
+ if (intersection.size() <= 2)
+ {
+ all_beziers.push_back(*this);
+ return all_beziers;
+ }
+ BReal t0 = *(intersection.begin());
+ for (auto it = intersection.begin()+1; it != intersection.end(); ++it)
+ {
+ BReal t1 = *it;
+ if (t1 == t0) continue;
+ //Debug(" -- t0: %f to t1: %f: %f", Double(t0), Double(t1), Double((t1 + t0)/BReal(2)));
+ BReal ptx, pty;
+ Evaluate(ptx, pty, ((t1 + t0) / BReal(2)));
+ if (r.PointIn(ptx, pty))
+ {
+ //Debug("Adding segment: (point at %f, %f)", Double(ptx), Double(pty));
+ all_beziers.push_back(this->ReParametrise(t0, t1));
+ }
+ else
+ {
+ //Debug("Segment removed (point at %f, %f)", Double(ptx), Double(pty));
+ }
+ t0 = t1;
+ }
+ return all_beziers;
+ }