# [cairo] Rendering B-Splines with Cairo

Gerald Hansford gerhans at gmail.com
Sun Apr 5 11:50:27 PDT 2009

```Hi - thanks as always to the tremendous team responsible for Cairo - it's an
excellent library.

Does anyone have any experience rendering open, cubic uniform B-Splines
using Cairo? I know that it does not have explicit support for B-Splines,
but my understanding is that one can derive the series of Bézier splines
represented implicity in a B-Spline. In fact I have code that does this for
the open & periodic cases for quadratic b-splines, as well as the periodic
case for cubic B-Splines. For that, my code looks about like this:

Vec2f q0, q1, q2, q3;
q0 = ( spline.getControlPoint( 0 ) + spline.getControlPoint( 1 ) * 4.0f +
spline.getControlPoint( 2 ) ) / 6.0f;
moveTo( q0 );
int lastPt = ( spline.isLoop() ) ? numPoints : numPoints - 3;
for( int i = 0; i < lastPt; ++i ) {
Vec2f p1 = spline.getControlPoint( ( i + 1 ) % numPoints );
Vec2f p2 = spline.getControlPoint( ( i + 2 ) % numPoints );
Vec2f p3 = spline.getControlPoint( ( i + 3 ) % numPoints );

q1 = p1 * ( 4.0f / 6.0f ) + p2 * ( 2.0f / 6.0f );
q2 = p1 * ( 2.0f / 6.0f ) + p2 * ( 4.0f / 6.0f );
q3 = p1 * ( 1.0f / 6.0f ) + p2 * ( 4.0f / 6.0f ) + p3 * ( 1.0f / 6.0f );
curveTo( q1, q2, q3 );
}

The trouble comes when I try to address the open cubic case (where the curve
passes through the endpoints of the b-spline). All of the literature on the
topic which I can actually understand seems to be about the periodic case.

I know that in the open case, a b-spline with 4 control points exactly maps
to the bezier curve with the same control points. Also through
experimentation I can tell that the non-end segments of an open b-spline
with >= 6 points correspond to the periodic case. However I am still at a
loss as to how one would glue all of this information together, and I
haven't managed to get deep enough in the relevant math to follow
discussions of how knot insertion or blossoming helps me solve this problem.

Any help from someone experienced in all of this would be *very* much
appreciated.