Return a pair of SplineCurves from SplineCurve::Split rather than mutating the arguments, as it doesn't actually work in-place

Originally committed to SVN as r6100.
This commit is contained in:
Thomas Goyne 2011-12-22 21:25:08 +00:00
parent 400b8252e9
commit 5e9339611b
3 changed files with 15 additions and 12 deletions

View file

@ -51,10 +51,12 @@ SplineCurve::SplineCurve(Vector2D p1, Vector2D p2, Vector2D p3, Vector2D p4)
{
}
void SplineCurve::Split(SplineCurve &c1, SplineCurve &c2, float t) {
std::pair<SplineCurve, SplineCurve> SplineCurve::Split(float t) {
if (type == LINE) {
c1 = SplineCurve(p1, p1 * (1 - t) + p2 * t);
c2 = SplineCurve(c1.p2, p2);
Vector2D m = p1 * (1 - t) + p2 * t;
return std::make_pair(
SplineCurve(p1, m),
SplineCurve(m, p2));
}
else if (type == BICUBIC) {
float u = 1 - t;
@ -65,9 +67,11 @@ void SplineCurve::Split(SplineCurve &c1, SplineCurve &c2, float t) {
Vector2D p234 = p23 * u + p34 * t;
Vector2D p1234 = p123 * u + p234 * t;
c1 = SplineCurve(p1, p12, p123, p1234);
c2 = SplineCurve(p1234, p234, p34, p4);
return std::make_pair(
SplineCurve(p1, p12, p123, p1234),
SplineCurve(p1234, p234, p34, p4));
}
return std::make_pair(SplineCurve(p1), SplineCurve(p1));
}
void SplineCurve::Smooth(Vector2D p0, Vector2D p3, float smooth) {

View file

@ -67,10 +67,9 @@ public:
SplineCurve(Vector2D p1, Vector2D p2, Vector2D p3, Vector2D p4);
/// @brief Split a curve in two using the de Casteljau algorithm
/// @param[out] c1 Curve before split point
/// @param[out] c2 Curve after split point
/// @param t Split point
void Split(SplineCurve &c1, SplineCurve &c2, float t = 0.5f);
/// @param t Split point from 0-1
/// @return Curve before and after the split point
std::pair<SplineCurve, SplineCurve> Split(float t = 0.5f);
/// @brief Smooths the curve
/// @note Based on http://antigrain.com/research/bezier_interpolation/index.html

View file

@ -289,9 +289,9 @@ bool VisualToolVectorClip::InitializeHold() {
spline.push_back(ct);
}
else {
SplineCurve c2;
curve->Split(*curve, c2, t);
spline.insert(++curve, c2);
std::pair<SplineCurve, SplineCurve> split = curve->Split(t);
*curve = split.first;
spline.insert(++curve, split.second);
}
}