knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 1.2 · I Geometry

Curves

What it's about: What a curve in knotflow is, where it comes from, and how to describe smooth curves, including exact circles, with a few numbers: Bézier curves, B-splines, and weights.

In short: Every curve in knotflow is a NURBS curve, whether it comes from circle, from points, from a font, or from an SVG drawing. A single representation is sufficient for lines, circles, and free forms, and everything that follows (surfaces, walls, holes) only needs to understand this one type of curve.

1. Where curves come from

How to use curves in a model is shown in the handbook (chapters 4 and 5). Here the question is what they make. A small model with the most common methods:

// A bowl: a profile made of pieces, corner rounded, then revolved around the axis
let floor = line((0, 0), (30, 0))
let side  = interpolate([(30, 0), (42, 12), (44, 30)])
let outside = chain([floor, side]) |> fillet(4)
export revolve(thicken(outside, 2))

The bowl: a floor, a wall through three points, the corner between them rounded, 2 mm thick and revolved

Source Result exact?
line, polyline, rectangle, polygon Degree 1 curve: the control points are the corners yes
circle, arc, arc_between Degree 2 circular arcs with weights (section 5) yes
nurbs, bezier the control points, as specified yes
interpolate a degree 3 curve through the points; its control points are solved via a system of equations yes
chain the pieces one after another, all raised to the highest degree yes
segment a piece of a curve as its own curve yes
fillet corners replaced by circular arcs touching both sides yes
offset, thicken the parallel curve: lines and arcs are exact, otherwise approximated to 0.0001 mm
lettering the outlines of the letters: Bézier pieces of degree 2 (TrueType) or 3 (OpenType) yes
svg the filled areas of a drawing: lines, Bézier pieces, elliptical arcs yes

"Exact" means: The curve is the same shape that was intended, not an approximation made of many short pieces. Only when it is cut into triangles at the very end does it become straight pieces (Lesson 1.3). The parallel curve is the one exception: the parallel of a free form is mathematically no longer a NURBS curve, so it is approximated with cubic pieces until no point is further than 0.0001 mm away (Lesson 3.3).

Every curve runs from t = 0 (start) to t = 1 (end). This parameter is used to query it: at(c, t) is the point, tangent(c, t) the direction, segment(c, a, b) the piece in between. What lies behind t and why equal steps in t are not equal in length is shown in the following sections.

2. Control points: describing a curve with a few numbers

A smooth curve has infinitely many points. It can be described with a few numbers that you can grab and move: the control points. The curve usually does not pass through them, but is pulled towards them as if by magnets. If you connect the control points in sequence, you get the control polygon (dashed in the pictures).

This makes curves handy: if you move, rotate, or mirror a NURBS curve, it is sufficient to move, rotate, or mirror the control points. The curve follows exactly.

3. Bézier curves: repeated averaging

The simplest idea: You take a parameter t between 0 and 1 and mix the points. lerp(A, B, t) is the point that has travelled the fraction t of the path from A to B; at t = 0.5 this is the middle.

For three control points P0, P1, P2, you mix twice (de Casteljau's algorithm):

Round 1:  Q0 = lerp(P0, P1, t)    Q1 = lerp(P1, P2, t)
Round 2:  C(t) = lerp(Q0, Q1, t)

With P0 = (0, 0), P1 = (1, 2), P2 = (2, 0) and t = 0.5, Q0 = (0.5, 1), Q1 = (1.5, 1) and C = (1, 1). If you let t run from 0 to 1, a parabola is created. With n points, you need n − 1 rounds; the curve then has degree n − 1. This is bezier(points).

The catch: With many control points, the degree becomes high, and every point influences the entire curve. Move one, and everything wobbles (on the right in the picture):

B-spline against Bézier

4. B-splines: many small pieces, smoothly connected

A B-spline assembles the curve from pieces of low degree, usually degree 2 or 3, which transition smoothly into each other at the joints. Each piece depends only on degree + 1 neighbouring control points. That is why only a part of the curve moves in the picture on the left.

The degree determines how smooth the curve is. Degree 1 is the control polygon itself (this is how polylines are created); higher degrees are smoother but follow the points less closely:

Same polygon, different degrees

A list of parameter values, the knot vector, determines where one piece ends and the next begins. If the joints are unevenly distributed, the spline is called "non-uniform"; this is the "NU" in NURBS. This also explains why equal steps in t are different lengths on the curve: t counts parameters, not millimetres. Equal distances are provided by divide(curve, n).

In interpolate, the joints depend on the distances between the points: If two points are far apart, the piece between them gets a large portion of t. With spacing: "even", each piece gets the same amount. This helps when many curves through the same number of points later meet in a loft: they then share their joints, and the surface stays light (Lesson 1.3).

5. Weights and exact circles

Now the "R", for rational. Each control point gets a weight w. A large weight pulls the curve more strongly towards this point, a small weight lets it go:

Weights of the middle point

A polynomial cannot describe an exact circle: without weights (all w = 1), three points become a parabola, not a circular arc. With the weight cos(45°) ≈ 0.707 at the corner point, it becomes exactly a quarter circle. This can be verified in the language:

// A quarter circle with radius 20: three control points, the corner with weight cos(45°)
let quarter = nurbs([(20, 0), (20, 20), (0, 20)], degree: 2, weights: [1, cos(45), 1])
let radii = [distance(at(quarter, t), (0, 0)) | t <- linspace(0, 1, 5)]
export extrude(thicken(quarter, 2), 5)    // as a 2 mm wide band, 5 mm high

kf render shows radii = [20, 20, 20, 20, 20]. A full circle consists of four such quarters; this is how circle builds it:

Exact circle and arc

A polygon with 64 corners, as used by programs that cut into triangles right away, is off the radius by 0.12 % in the middle of each edge.

With this, the name is complete: Non-Uniform Rational B-Spline.

How it is checked

Tests check circles at a thousand places (distance to the centre within 10⁻¹²), check that interpolate goes through all points and segment does not change the shape, and compare four profiles of a cachepot modelled in OpenSCAD at 21 places each with the values OpenSCAD computes itself: two independent implementations arrive at the same points.

Try it yourself

Compute every model in the browser or with kf render model.kf -o out/x.stl; the values are in the output.

  1. In the quarter circle, change the weight cos(45) to 0.1, 1 and 20. What does the band look like, and what does radii show?
  2. nurbs(points, degree: 5) with exactly six points: compare it with bezier(points). How many pieces does the curve have then?
  3. Make interpolate(points) and nurbs(points) from the same points, export both as a band (extrude(thicken(c, 0.5), 2)) and small cylinders at the points. Which curve hits the points, which one is smoother?
  4. Offset a rectangle with offset(rectangle(40, 20), 3) and with -3. Which corners remain sharp, which become rounded – and why?

In depth: the knot vector and de Boor's algorithm

The knot vector. For 6 control points and degree 3, it looks like this, for example:

[0, 0, 0, 0,  1/3, 2/3,  1, 1, 1, 1]
 └ 4× 0 ┘     └Joints┘    └ 4× 1 ┘

De Boor. Curves are evaluated with de Boor's algorithm. It works like de Casteljau, repeatedly lerp between neighbours, only the mixing factor is not simply t, but comes from the knots: a = (t − knots[i]) / (knots[i + degree + 1 − r] − knots[i]). In the code, this is de_boor in crates/geom/src/bspline.rs, about a dozen lines.

Homogeneous coordinates. For the weights, the point (x, y, z) with weight w becomes the point (w·x, w·y, w·z, w) in four dimensions. A plain B-spline is computed with these points, and at the end the result is divided by its last component. This division makes the curve rational: a fraction of two polynomials.

Knot insertion. An additional knot does not change the shape, only the number of control points. This is the basis for segment (inserting two knots up to full multiplicity, the piece in between is its own curve) and chain (raising the degree so that all pieces have the same degree).

File Content
crates/geom/src/knots.rs make a knot vector (clamped_uniform), find span (find_span)
crates/geom/src/bspline.rs de_boor, knot insertion
crates/geom/src/curve.rs NurbsCurve: point (point_at), derivative (derivative_at), segment, transformed
crates/geom/src/primitives.rs line, polyline, bezier, arc, circle, polygon
crates/geom/src/construct.rs interpolate (system of equations for the control points)
crates/geom/src/offset.rs chain, offset
crates/geom/src/fillet.rs fillet, corners
crates/geom/src/font.rs, svg.rs curves from fonts and SVG drawings

The pictures of this lesson are made by kf svg --demo <name> -o out/<name>.svg (kf svg --list shows all).