Algorithm Search⌘K

Algorithmcore

Gordon surface

Also called Gordon-Coons surface, curve network surface.

Builds one surface through a compatible network of curves in two directions, as the sum of the two lofts minus the tensor product interpolant of the crossing points.

Time
O(N^3) worst
Space
Θ(N^2) extra
Where
N = the total of the numbers of curves and of control points per curve, after the knot merge
Properties
deterministicofflineiterative
Origin
William J. Gordon, 1969

A Gordon surface is a single surface that passes through every curve of a network of curves in two directions: n+1n + 1 curves along which vv varies and m+1m + 1 curves along which uu varies, which cross each other in a grid of points. It is the sum of two lofted surfaces, one through each family, minus the tensor product surface through the crossing points. The construction is the Coons patch with interpolation through many curves in place of the linear blend between two, and Gordon published it in 1969. With B-spline curves and spline interpolation the result is one B-spline surface, which is how CAD systems build it.

In plain words

A designer draws a grid of curves, like the frames and the long stringers of a ship hull. The task is to find one smooth skin that contains every curve. A first try is to skin only the frames: stretch a surface across them from front to back. This surface holds the frames, but between them it takes its own shape, and it misses the stringers. A second skin across the stringers holds the stringers and misses the frames. Adding the two skins counts the shape twice, so subtract a third skin that only knows the points where the curves cross. On a frame, the second and third skins both see the same crossing points, so they cancel, and what remains is the first skin, which is exactly the frame. The same happens on a stringer. So the sum holds every curve of the grid.

The problem

Input. The v-curves f0(v),…,fn(v)f_0(v), \dots, f_n(v), B-spline curves of one degree qq with clamped knot vectors on [v0,vm][v_0, v_m], and the u-curves g0(u),…,gm(u)g_0(u), \dots, g_m(u), B-spline curves of one degree pp on [u0,un][u_0, u_n]. Increasing parameters u0<⋯<unu_0 < \dots < u_n and v0<⋯<vmv_0 < \dots < v_m, with n≥pn \ge p and m≥qm \ge q. The curve fif_i is meant to lie on the surface at u=uiu = u_i, and gjg_j at v=vjv = v_j.

Compatibility. The network is compatible if the curves meet where they should:

fi(vj)=gj(ui)=:pijfor all i,j.f_i(v_j) = g_j(u_i) =: p_{ij} \qquad \text{for all } i, j.

Output. A B-spline surface S(u,v)S(u, v) of degree (p,q)(p, q) with

S(ui,v)=fi(v) for all v and i,S(u,vj)=gj(u) for all u and j.S(u_i, v) = f_i(v) \ \text{for all } v \text{ and } i, \qquad S(u, v_j) = g_j(u) \ \text{for all } u \text{ and } j.

No surface can do this for an incompatible network, since S(ui,vj)S(u_i, v_j) would have two values. The network must also be rectangular: each u-curve crosses every v-curve once, at the same parameters uiu_i for all jj. Curves of one family with different degrees must first be raised to one degree, which does not change them.

The algorithm

Write Lu\mathcal{L}_u for the operator that interpolates data given at u0,…,unu_0, \dots, u_n by a B-spline curve of degree pp with averaged knots, as on the curve interpolation page, and Lv\mathcal{L}_v for the same operator in vv with degree qq. Applied to points that are themselves control points of curves, the operator works entry by entry.

  1. GORDON(f_0..f_n, g_0..g_m, u_0..u_n, v_0..v_m)
  2. 1refine all f_i to one knot vector V_c by knot insertion; F_i ← the control points of f_i on V_c
  3. 2refine all g_j to one knot vector U_c by knot insertion; G_j ← the control points of g_j on U_c
  4. 3p_ij ← f_i(v_j); if p_ij ≠ g_j(u_i) for some i, j then report "incompatible network"
  5. 4S1 ← ℒ_u(F_0, .., F_n): for each index l, interpolate F_0[l], .., F_n[l] at u_0, .., u_n ▷ knots U_1 × V_c
  6. 5S2 ← ℒ_v(G_0, .., G_m): for each index k, interpolate G_0[k], .., G_m[k] at v_0, .., v_m ▷ knots U_c × V_2
  7. 6T ← ℒ_v ℒ_u(p_ij): interpolate each column p_0j, .., p_nj in u, then each row of the result in v ▷ knots U_1 × V_2
  8. 7U ← U_1 merged with U_c; V ← V_c merged with V_2; refine S1, S2 and T to U × V by knot insertion
  9. 8return the surface of degree (p, q) on U × V with control net P1 + P2 − P3

Lines L1 and L2 put each family on one knot vector, so that the control points of all curves of a family refer to the same basis. Merging takes each knot value with its largest multiplicity in the family. Lines L4 to L6 are three interpolations that share the same two operators: S1S1 and TT use the same knots U1U_1 and the same parameters in uu, and S2S2 and TT use the same knots V2V_2 in vv. This is what makes the terms cancel on the curves. Line L7 changes only the representation of the three surfaces, so that in line L8 their control nets refer to one basis and can be added.

As functions, the three surfaces are

S1=∑i=0nϕi(u)fi(v),S2=∑j=0mψj(v)gj(u),T=∑i=0n∑j=0mϕi(u)ψj(v)pij,S_1 = \sum_{i=0}^{n} \phi_i(u) f_i(v), \qquad S_2 = \sum_{j=0}^{m} \psi_j(v) g_j(u), \qquad T = \sum_{i=0}^{n} \sum_{j=0}^{m} \phi_i(u) \psi_j(v) p_{ij},

where ϕi\phi_i is the spline on U1U_1 with ϕi(uk)=δik\phi_i(u_k) = \delta_{ik} and ψj\psi_j is the spline on V2V_2 with ψj(vl)=δjl\psi_j(v_l) = \delta_{jl}. In the language of Gordon 1971, let PF(u,v)=∑iϕi(u)F(ui,v)\mathcal{P} F(u, v) = \sum_i \phi_i(u) F(u_i, v) and QF(u,v)=∑jψj(v)F(u,vj)\mathcal{Q} F(u, v) = \sum_j \psi_j(v) F(u, v_j) for a surface FF. These are projectors, and they commute. For any FF that contains the network, S=(P⊕Q)F=(P+Q−PQ)FS = (\mathcal{P} \oplus \mathcal{Q}) F = (\mathcal{P} + \mathcal{Q} - \mathcal{P}\mathcal{Q}) F, the Boolean sum, and it uses only the values of FF on the network. With n=m=1n = m = 1 and linear ϕ\phi and ψ\psi it is the bilinearly blended Coons patch.

Example

Take three u-curves at v=0,12,1v = 0, \frac12, 1 and three v-curves at u=0,12,1u = 0, \frac12, 1, all of degree 22 (p=q=2p = q = 2), in the height field form (x,y,z)(x, y, z). The outer curves are straight lines at height 00 on the edges of the unit square, Bezier curves with knots (0,0,0,1,1,1)(0, 0, 0, 1, 1, 1). The two middle curves are the same leaning bump hh: a quadratic B-spline on the knots H=(0,0,0,12,1,1,1)H = (0, 0, 0, \frac12, 1, 1, 1) with heights 0,2,0,00, 2, 0, 0, so h(0)=h(1)=0h(0) = h(1) = 0, h(18)=1316h(\frac18) = \frac{13}{16}, h(14)=54h(\frac14) = \frac54 and h(12)=1h(\frac12) = 1. The middle u-curve is g1(u)=(u,12,h(u))g_1(u) = (u, \frac12, h(u)) and the middle v-curve is f1(v)=(12,v,h(v))f_1(v) = (\frac12, v, h(v)). They cross at the centre at height h(12)=1h(\frac12) = 1, and all other crossings are at height 00, so the network is compatible.

  • L1, L2. The merged knot vector of each family is HH. The outer lines get four control points by the insertion of the knot 12\frac12.
  • L4. Three curves at u=0,12,1u = 0, \frac12, 1 with degree 22 give the averaged knots U1=(0,0,0,1,1,1)U_1 = (0, 0, 0, 1, 1, 1), so ϕ1(u)=4u(1−u)\phi_1(u) = 4u(1 - u) is the quadratic Lagrange polynomial and S1=(u,v,4u(1−u)h(v))S_1 = (u, v, 4u(1 - u) h(v)).
  • L5, L6. In the same way S2S_2 has height 4v(1−v)h(u)4v(1 - v) h(u), and TT has height 16uv(1−u)(1−v)16 u v (1 - u)(1 - v), since the only non zero crossing height is p11=1p_{11} = 1.
  • L7, L8. U=V=HU = V = H, and the 4×44 \times 4 control net of the result has the xx and yy coordinates 0,14,34,10, \frac14, \frac34, 1 and the heights
(0000031001−100000),\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 3 & 1 & 0 \\ 0 & 1 & -1 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix},

In the inner 2×22 \times 2 block the refined nets of S1S_1, S2S_2 and TT have the heights (2020)\begin{pmatrix} 2 & 0 \\ 2 & 0 \end{pmatrix}, (2200)\begin{pmatrix} 2 & 2 \\ 0 & 0 \end{pmatrix} and (1111)\begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix}, so for example 3=2+2−13 = 2 + 2 - 1.

The heights of the three parts at two points show how the terms cancel:

point (u,v)(u, v) S1S_1 S2S_2 TT S=S1+S2−TS = S_1 + S_2 - T target
(14,12)(\frac14, \frac12), on g1g_1 34\frac34 54\frac54 34\frac34 54\frac54 h(14)=54h(\frac14) = \frac54
(14,14)(\frac14, \frac14), off the network 1516\frac{15}{16} 1516\frac{15}{16} 916\frac{9}{16} 2116\frac{21}{16} none

On the u-curve g1g_1 the loft S1S_1 alone gives 34\frac34, the height of the quadratic through the three crossing points, and misses the curve. There S2−TS_2 - T adds the missing 12\frac12.

Correctness

The proof has two parts. First, each interpolation of lines L4 to L6 is well defined, and S1S_1, S2S_2 and TT are the functions displayed above: the collocation matrix of averaged knots is nonsingular by the Schoenberg-Whitney theorem, and interpolation is linear in the data, so interpolating control points entry by entry interpolates the curves. Second, on the line u=uku = u_k, S1=fkS_1 = f_k because ϕi(uk)=δik\phi_i(u_k) = \delta_{ik}, while S2(uk,v)=∑jψj(v)gj(uk)=∑jψj(v)pkj=T(uk,v)S_2(u_k, v) = \sum_j \psi_j(v) g_j(u_k) = \sum_j \psi_j(v) p_{kj} = T(u_k, v) by compatibility, so S(uk,v)=fk(v)S(u_k, v) = f_k(v). The line v=vlv = v_l is symmetric. Knot insertion in lines L1, L2 and L7 never changes a curve or a surface, so the sum of the refined nets is the sum of the surfaces. The full proof, with the operator form I−P⊕Q=(I−P)(I−Q)I - \mathcal{P} \oplus \mathcal{Q} = (I - \mathcal{P})(I - \mathcal{Q}), is in the proof file.

Proof: The Gordon surface of a compatible network contains every curve of the networkfull

We use the pseudocode and the notation of the page. The input curves f0,…,fnf_0, \dots, f_n have degree qq, the curves g0,…,gmg_0, \dots, g_m have degree pp, the parameters increase, n≥pn \ge p, m≥qm \ge q, the outer parameters u0,unu_0, u_n and v0,vmv_0, v_m are the ends of the knot vectors of the other family, and the network is compatible: fi(vj)=gj(ui)=pijf_i(v_j) = g_j(u_i) = p_{ij}.

Lemma 1 (knot insertion)

Inserting a knot into a B-spline curve, by Boehm's rule, gives a new control polygon on the new knot vector that defines the same curve. Inserting a knot into a surface row by row or column by column gives the same surface. This is the theorem on the Boehm knot insertion page, applied to each row or column of the net, since a tensor product surface at fixed vv is a curve in uu whose control points are the values at vv of the column curves. So lines L1, L2 and L7 do not change any curve or surface, and after line L1 every fif_i is fi(v)=∑lMl(v)Fi[l]f_i(v) = \sum_l M_l(v) F_i[l] with the same B-spline basis MlM_l on VcV_c; likewise gj(u)=∑kNk(u)Gj[k]g_j(u) = \sum_k N_k(u) G_j[k] on UcU_c.

Lemma 2 (the interpolation operator)

Let U1U_1 be the averaged knot vector of degree pp for the parameters u0,…,unu_0, \dots, u_n, with basis N01,…,Nn1N^1_0, \dots, N^1_n, and let AA be the collocation matrix Aik=Nk1(ui)A_{ik} = N^1_k(u_i). Then AA is nonsingular, and the splines

ϕi(u)=∑k=0nNk1(u)(A−1)ki,i=0,…,n,\phi_i(u) = \sum_{k=0}^{n} N^1_k(u) (A^{-1})_{ki}, \qquad i = 0, \dots, n,

satisfy ϕi(ur)=δri\phi_i(u_r) = \delta_{ri}. For any data d0,…,dnd_0, \dots, d_n in a vector space, the spline that line L4 computes with control points c=A−1dc = A^{-1} d is ∑iϕi(u)di\sum_i \phi_i(u) d_i.

Proof. With averaged knots each inner knot is the mean of pp consecutive parameters, tk+p=(uk+⋯+uk+p−1)/pt_{k+p} = (u_k + \dots + u_{k+p-1})/p for k=1,…,n−pk = 1, \dots, n - p, so tk<uk<tk+p+1t_k < u_k < t_{k+p+1} for the inner indices, and u0u_0 and unu_n are the end knots. These are the conditions of the Schoenberg-Whitney theorem, so AA is nonsingular. Then ϕi(ur)=∑kArk(A−1)ki=δri\phi_i(u_r) = \sum_k A_{rk} (A^{-1})_{ki} = \delta_{ri}. The spline with control points cc is ∑kNk1(u)ck=∑kNk1(u)∑i(A−1)kidi=∑iϕi(u)di\sum_k N^1_k(u) c_k = \sum_k N^1_k(u) \sum_i (A^{-1})_{ki} d_i = \sum_i \phi_i(u) d_i. The data may be points, or tuples of control points, since the computation is linear and is done for each entry. □\square

Let ψ0,…,ψm\psi_0, \dots, \psi_m be the splines of Lemma 2 for the parameters vjv_j, degree qq and the knots V2V_2, with ψj(vl)=δlj\psi_j(v_l) = \delta_{lj}.

Lemma 3 (the three surfaces)

The surfaces of lines L4, L5 and L6 are

S1(u,v)=∑iϕi(u)fi(v),S2(u,v)=∑jψj(v)gj(u),T(u,v)=∑i,jϕi(u)ψj(v)pij.S_1(u, v) = \sum_i \phi_i(u) f_i(v), \qquad S_2(u, v) = \sum_j \psi_j(v) g_j(u), \qquad T(u, v) = \sum_{i, j} \phi_i(u) \psi_j(v) p_{ij}.

Proof. In line L4 the data for the index ll are F0[l],…,Fn[l]F_0[l], \dots, F_n[l], so by Lemma 2 the control points of S1S_1 are P1[k][l]=∑i(A−1)kiFi[l]P_1[k][l] = \sum_i (A^{-1})_{ki} F_i[l]. Then

S1(u,v)=∑k,lNk1(u)Ml(v)P1[k][l]=∑i(∑kNk1(u)(A−1)ki)(∑lMl(v)Fi[l])=∑iϕi(u)fi(v),S_1(u, v) = \sum_{k, l} N^1_k(u) M_l(v) P_1[k][l] = \sum_i \Bigl(\sum_k N^1_k(u) (A^{-1})_{ki}\Bigr) \Bigl(\sum_l M_l(v) F_i[l]\Bigr) = \sum_i \phi_i(u) f_i(v),

using Lemma 1 for the last factor. The proof for S2S_2 is the same with the roles of uu and vv exchanged. In line L6, the first step gives for each jj the spline ∑iϕi(u)pij\sum_i \phi_i(u) p_{ij} in uu, with control points R[k][j]R[k][j]. The second step interpolates, for each kk, the values R[k][0],…,R[k][m]R[k][0], \dots, R[k][m] at v0,…,vmv_0, \dots, v_m, which by Lemma 2 gives control points P3[k][l]P_3[k][l] with ∑lMl2(v)P3[k][l]=∑jψj(v)R[k][j]\sum_l M^2_l(v) P_3[k][l] = \sum_j \psi_j(v) R[k][j]. Hence T(u,v)=∑kNk1(u)∑jψj(v)R[k][j]=∑jψj(v)∑iϕi(u)pijT(u, v) = \sum_k N^1_k(u) \sum_j \psi_j(v) R[k][j] = \sum_j \psi_j(v) \sum_i \phi_i(u) p_{ij}. □\square

Theorem

If line L3 does not report an incompatible network, the surface SS returned in line L8 satisfies S(ur,v)=fr(v)S(u_r, v) = f_r(v) for all rr and all vv in [v0,vm][v_0, v_m], and S(u,vs)=gs(u)S(u, v_s) = g_s(u) for all ss and all uu in [u0,un][u_0, u_n].

Proof. By Lemma 1 the refined nets of line L7 define the same three surfaces on one basis U×VU \times V. A tensor product surface depends linearly on its net, so the net P1+P2−P3P_1 + P_2 - P_3 defines S=S1+S2−TS = S_1 + S_2 - T. Fix rr. By Lemma 3 and ϕi(ur)=δri\phi_i(u_r) = \delta_{ri},

S1(ur,v)=fr(v),S2(ur,v)=∑jψj(v)gj(ur),T(ur,v)=∑jψj(v)prj.S_1(u_r, v) = f_r(v), \qquad S_2(u_r, v) = \sum_j \psi_j(v) g_j(u_r), \qquad T(u_r, v) = \sum_j \psi_j(v) p_{rj}.

Line L3 passed, so gj(ur)=prjg_j(u_r) = p_{rj} for every jj, and the last two terms are equal. Hence S(ur,v)=fr(v)S(u_r, v) = f_r(v). For ss fixed, ψj(vs)=δsj\psi_j(v_s) = \delta_{sj} gives S2(u,vs)=gs(u)S_2(u, v_s) = g_s(u), S1(u,vs)=∑iϕi(u)fi(vs)=∑iϕi(u)pisS_1(u, v_s) = \sum_i \phi_i(u) f_i(v_s) = \sum_i \phi_i(u) p_{is} and T(u,vs)=∑iϕi(u)pisT(u, v_s) = \sum_i \phi_i(u) p_{is}, so S(u,vs)=gs(u)S(u, v_s) = g_s(u). □\square

The operator form

The theorem is a case of a general fact about Boolean sums. For surfaces FF on the rectangle put PF(u,v)=∑iϕi(u)F(ui,v)\mathcal{P} F(u, v) = \sum_i \phi_i(u) F(u_i, v) and QF(u,v)=∑jψj(v)F(u,vj)\mathcal{Q} F(u, v) = \sum_j \psi_j(v) F(u, v_j). Both are linear, and both are projectors: PPF=PF\mathcal{P}\mathcal{P} F = \mathcal{P} F because ϕi(ur)=δri\phi_i(u_r) = \delta_{ri}. They commute, PQF=QPF=∑i,jϕi(u)ψj(v)F(ui,vj)\mathcal{P}\mathcal{Q} F = \mathcal{Q}\mathcal{P} F = \sum_{i, j} \phi_i(u) \psi_j(v) F(u_i, v_j), because they act on different variables. The Boolean sum P⊕Q=P+Q−PQ\mathcal{P} \oplus \mathcal{Q} = \mathcal{P} + \mathcal{Q} - \mathcal{P}\mathcal{Q} satisfies

I−P⊕Q=(I−P)(I−Q).I - \mathcal{P} \oplus \mathcal{Q} = (I - \mathcal{P})(I - \mathcal{Q}).

For any surface GG, (I−P)G(I - \mathcal{P}) G vanishes on every line u=uru = u_r, since PG(ur,v)=G(ur,v)\mathcal{P} G(u_r, v) = G(u_r, v). As (I−P)(I - \mathcal{P}) and (I−Q)(I - \mathcal{Q}) commute, the error F−(P⊕Q)FF - (\mathcal{P} \oplus \mathcal{Q}) F vanishes on every line u=uru = u_r and on every line v=vsv = v_s. For a compatible network, the surface SS of the theorem itself contains the network, so the network comes from some FF, and (P⊕Q)F(\mathcal{P} \oplus \mathcal{Q}) F uses only the values of FF on the network, which are the curves. This is the form in which Gordon stated the result in 1971.

Rejection

If line L3 reports an incompatible network, then fi(vj)≠gj(ui)f_i(v_j) \ne g_j(u_i) for some i,ji, j, and no surface can contain both curves, since it would take two values at (ui,vj)(u_i, v_j). So the report is correct. With exact arithmetic the test in line L3 is exact; with a tolerance, a network that passes is compatible only to that tolerance, and the theorem holds for the network after the curves of one family are moved by at most that amount at the crossings.

Complexity

Let NN be the total size: the numbers of curves n+1n + 1 and m+1m + 1 and the numbers of control points of a curve after lines L1 and L2. Each interpolation solves one (n+1)×(n+1)(n + 1) \times (n + 1) or (m+1)×(m+1)(m + 1) \times (m + 1) system with many right sides, one for each control point of a curve. The implementation uses dense Gaussian elimination, which costs Θ(n3+n2N)\Theta(n^3 + n^2 N) for S1S_1, and the same with mm for S2S_2 and TT. Line L7 inserts O(N)O(N) knots into nets of O(N2)O(N^2) points. Each insertion changes pp or qq points in each of O(N)O(N) rows or columns, but the implementation copies the whole row, so line L7 costs O(N3)O(N^3). The total is O(N3)O(N^3). The collocation matrices are banded, with at most pp or qq non zero entries beside the diagonal in each row, so a banded solver and an insertion in place bring the cost down to Θ(N2)\Theta(N^2) for fixed degrees, which is the size of the output. The extra space is the three nets, Θ(N2)\Theta(N^2).

Properties

  • Exact on the network. For a compatible network the surface contains every curve exactly, not within a tolerance, and the implementation checks this in exact rational arithmetic.
  • Linear. The surface depends linearly on the curves, so moving one curve moves the surface by a fixed blend of that change.
  • Not local. Spline interpolation is global: the cardinal splines ϕi\phi_i are non zero, though decaying, far from uiu_i. Changing one curve changes the surface everywhere, mostly near the curve.
  • Affine invariant. The construction commutes with affine maps of space, because it only takes affine combinations of points: ∑iϕi=1\sum_i \phi_i = 1, and 1+1−1=11 + 1 - 1 = 1 in the Boolean sum.
  • Reproduces a surface of its own space. If the network is cut from a surface FF whose sections u↦F(u,v)u \mapsto F(u, v) are all splines on U1U_1, then PF=F\mathcal{P} F = F and S=F+QF−QPF=FS = F + \mathcal{Q} F - \mathcal{Q} \mathcal{P} F = F. The same holds with vv and V2V_2.

Variants and improvements

  • Polynomial Gordon surfaces. With Lagrange interpolation in place of splines, as in the example, ϕi\phi_i and ψj\psi_j are Lagrange polynomials. This is fine for three or four curves per family and oscillates for many.
  • Gordon-Coons surfaces with derivatives. Hermite projectors that also interpolate cross derivatives along the curves give G1G^1 or C1C^1 joins to other surfaces. They need consistent twists at the crossing points, see twist compatibility.
  • Unequal crossing parameters. In practice the u-curves cross the v-curves at different parameters. Piegl and Tiller reparametrize the curves first so that all crossings of a family happen at common parameters.
  • Transfinite interpolation grids. The same Boolean sum with linear or spline blends maps a square to a curved domain in structured grid generation.
  • Rational curves. The construction runs on homogeneous control points. It interpolates the curves only if the network is compatible in homogeneous form, that is, if the weights also agree at the crossings.
  • Irregular networks. A network with triangles or n-gons among its cells needs n-sided patches, not a single tensor product surface.

When to use it

Use a Gordon surface when the shape is given by section curves in two directions that cross in a regular grid, as for hulls, fuselages, car bodies and turbine blades, and the surface must contain all of them exactly. If only one family of sections is given, use skinning. If only four boundary curves are given, the Coons patch is enough. If the curves are measured and contain noise, an exact interpolant follows the noise, and an approximating surface fitted by least squares is better.

Pitfalls

  • Incompatible networks. Curves that are drawn separately rarely meet exactly. A small mismatch makes the problem impossible, so the program must either move the curves until they meet, or work to a tolerance and accept that the surface misses the curves by about the size of the mismatch.
  • Knot growth. The merged knot vectors hold every knot of every curve and of the interpolation, so a network of curves with unrelated knots gives a surface with many control points.
  • Wiggles from the parameters. The shape between the curves depends on the parameters uiu_i and vjv_j. Parameters that do not follow the spacing of the curves give bulges and waves, as in all interpolation.
  • Shape between curves is not designed. The surface interpolates the curves, but nothing controls its fairness between them. Check it with reflection lines or curvature plots.
  • Degrees. The implementation needs one degree per family and at least p+1p + 1 v-curves and q+1q + 1 u-curves. With fewer curves, use a lower interpolation degree and raise the degrees of the parts before line L7.

Implementations

The Python file in this folder has gordon_surface(u_curves, v_curves, us, vs), which returns the surface as knot vectors, degrees and a control net, and gordon_parts, which returns the three surfaces S1S_1, S2S_2 and TT. The shared helper si3spline.py holds the basis functions, the evaluation, Boehm knot insertion, the knot merge and global interpolation with an exact Gauss-Jordan solver. With integer or Fraction input everything is exact. The tests check the worked example, and on random compatible networks whose curves have different knot vectors they check that each iso-curve of the result at uiu_i or vjv_j has exactly the control points of the input curve refined to the same knots. They also check that the sum of the nets equals the Boolean sum of the three parts at random points, that one loft alone misses the other family, and that incompatible networks and too few curves are rejected.

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

gordon_surface.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/gordon-surface/. The authors are listed in prov/8125BY.jsonl.

"""Gordon surface through a compatible network of B-spline curves.

The input is two families of curves. The v-curves f_i(v), i = 0..n, have one
degree q and lie on the surface at u = us[i]. The u-curves g_j(u), j = 0..m,
have one degree p and lie at v = vs[j]. The network is compatible when
f_i(vs[j]) = g_j(us[i]) for all i and j. The surface is the Boolean sum

    S = S1 + S2 - T,

where S1 interpolates the v-curves across u with degree p, S2 interpolates the
u-curves across v with degree q, and T interpolates the points of
intersection with the same two interpolation operators. All three are brought
to one pair of knot vectors by knot insertion, which does not change them, and
then the control nets are added. With int or Fraction input every step is
exact. The caller raises the curves of a family to one degree first.
"""

from si3spline import (add, check_curve, curve_point, interpolate, merge_knots,
                       refine, refine_surface, sub)


def _degree(curve):
    knots, ctrl = curve
    p = len(knots) - len(ctrl) - 1
    check_curve(knots, p, ctrl)
    return p


def _common(curves):
    """Lines L1 and L2: one degree and one knot vector for a family of curves."""
    degs = {_degree(c) for c in curves}
    if len(degs) != 1:
        raise ValueError("the curves of a family must have one degree")
    p = degs.pop()
    K = merge_knots([c[0] for c in curves])
    return p, K, [refine(c[0], p, c[1], K)[1] for c in curves]


def _interpolate_across(params, rows, deg):
    """Interpolate the l-th entries of the rows at params, for every l.

    rows[i] is a list of points; the result R has R[k][l], k over the new basis.
    All entries share one matrix, so the rows are flattened and solved at once.
    """
    d = len(rows[0][0])
    flat = [tuple(x for pt in r for x in pt) for r in rows]
    knots, sol = interpolate(params, flat, deg)
    return knots, [[tuple(s[l * d:(l + 1) * d]) for l in range(len(rows[0]))] for s in sol]


def gordon_parts(u_curves, v_curves, us, vs, tol=0):
    """The three surfaces S1, S2 and T, each as (U, V, p, q, P)."""
    if len(v_curves) != len(us) or len(u_curves) != len(vs):
        raise ValueError("one parameter per curve")
    q, Vc, F = _common(v_curves)  # @line L1
    p, Uc, G = _common(u_curves)  # @line L2
    if len(us) < p + 1 or len(vs) < q + 1:
        raise ValueError("need at least p + 1 v-curves and q + 1 u-curves")
    if (us[0], us[-1]) != (Uc[0], Uc[-1]) or (vs[0], vs[-1]) != (Vc[0], Vc[-1]):
        raise ValueError("the outer curves of each family must lie on the domain boundary")
    pts = [[curve_point(Vc, q, F[i], v) for v in vs] for i in range(len(us))]  # @line L3
    for i, u in enumerate(us):
        for j in range(len(vs)):
            g = curve_point(Uc, p, G[j], u)
            if max(abs(a - b) for a, b in zip(pts[i][j], g)) > tol:  # @line L3
                raise ValueError("the network is not compatible at curve pair (%d, %d)" % (i, j))
    U1, P1 = _interpolate_across(us, F, p)  # @line L4
    V2, Q = _interpolate_across(vs, G, q)  # @line L5
    P2 = [[Q[l][k] for l in range(len(Q))] for k in range(len(Q[0]))]
    _, R = _interpolate_across(us, pts, p)  # @line L6
    RT = [[R[k][j] for k in range(len(R))] for j in range(len(vs))]
    _, X = _interpolate_across(vs, RT, q)  # @line L6
    P3 = [[X[l][k] for l in range(len(X))] for k in range(len(X[0]))]
    return (U1, Vc, p, q, P1), (Uc, V2, p, q, P2), (U1, V2, p, q, P3)


def gordon_surface(u_curves, v_curves, us, vs, tol=0):
    """The Gordon surface as one B-spline surface (U, V, p, q, P)."""
    S1, S2, T = gordon_parts(u_curves, v_curves, us, vs, tol)
    p, q = S1[2], S1[3]
    U = merge_knots([S1[0], S2[0]])  # @line L7
    V = merge_knots([S1[1], S2[1]])  # @line L7
    A, B, C = (refine_surface(S[0], S[1], p, q, S[4], U, V) for S in (S1, S2, T))  # @line L7
    P = [[sub(add(A[k][l], B[k][l]), C[k][l]) for l in range(len(A[0]))]  # @line L8
         for k in range(len(A))]
    return U, V, p, q, P  # @line L8
si3spline.py 225 lines
# SPDX-License-Identifier: MIT
# Shared helper of the si3 cad pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Small B-spline tools for the si3 pages.

A point is a tuple of numbers of any fixed length. A B-spline curve of degree
p is a knot vector (a list of n + p + 2 non-decreasing numbers, clamped, that
is with p + 1 equal knots at each end) and a list of n + 1 control points.
Entries may be int or Fraction, and then every result here is exact. No
function here is the algorithm of a page: the basis functions, de Boor
evaluation, knot insertion and global interpolation are the standard tools of
the pages b-spline, de-boor-algorithm, boehm-knot-insertion and
curve-interpolation.
"""

from fractions import Fraction


# ---- points ----------------------------------------------------------------

def add(a, b):
    return tuple(x + y for x, y in zip(a, b))


def sub(a, b):
    return tuple(x - y for x, y in zip(a, b))


def scale(s, a):
    return tuple(s * x for x in a)


def lincomb(ws, ps):
    """Return the sum of ws[i] * ps[i]; ps is not empty."""
    out = [0] * len(ps[0])
    for w, p in zip(ws, ps):
        if w:
            for d in range(len(out)):
                out[d] += w * p[d]
    return tuple(out)


# ---- basis and evaluation --------------------------------------------------

def check_curve(knots, p, ctrl):
    if p < 0 or len(knots) != len(ctrl) + p + 1:
        raise ValueError("a curve of degree p with n + 1 control points needs n + p + 2 knots")
    if any(knots[i] > knots[i + 1] for i in range(len(knots) - 1)):
        raise ValueError("knots must not decrease")
    if knots[p] >= knots[len(ctrl)]:
        raise ValueError("the parameter interval is empty")


def find_span(knots, p, t):
    """The index k with knots[k] <= t < knots[k + 1], in p..n, and n at the right end."""
    n = len(knots) - p - 2
    if t >= knots[n + 1]:
        return n
    if t <= knots[p]:
        return p
    k = p
    while knots[k + 1] <= t:
        k += 1
    return k


def basis_funs(knots, p, k, t):
    """The values N_{k-p,p}(t), ..., N_{k,p}(t), by the Cox-de Boor recursion."""
    N = [Fraction(1)] + [Fraction(0)] * p
    left = [0] * (p + 1)
    right = [0] * (p + 1)
    for j in range(1, p + 1):
        left[j] = t - knots[k + 1 - j]
        right[j] = knots[k + j] - t
        saved = Fraction(0)
        for r in range(j):
            tmp = N[r] / (right[r + 1] + left[j - r])
            N[r] = saved + right[r + 1] * tmp
            saved = left[j - r] * tmp
        N[j] = saved
    return N


def basis_row(knots, p, t):
    """All n + 1 basis values at t, as one list."""
    n = len(knots) - p - 2
    k = find_span(knots, p, t)
    row = [Fraction(0)] * (n + 1)
    for r, val in enumerate(basis_funs(knots, p, k, t)):
        row[k - p + r] = val
    return row


def curve_point(knots, p, ctrl, t):
    """The point of the curve at t, as the basis combination of the control points."""
    k = find_span(knots, p, t)
    N = basis_funs(knots, p, k, t)
    return lincomb(N, ctrl[k - p:k + 1])


def derivative_curve(knots, p, ctrl):
    """The derivative as a B-spline of degree p - 1 on the knots without their ends."""
    if p == 0:
        raise ValueError("the degree must be positive")
    Q = []
    for i in range(len(ctrl) - 1):
        h = knots[i + p + 1] - knots[i + 1]
        Q.append(scale(Fraction(p) / h, sub(ctrl[i + 1], ctrl[i])) if h else
                 tuple(0 for _ in ctrl[0]))
    return knots[1:-1], p - 1, Q


# ---- knot insertion --------------------------------------------------------

def insert_knot(knots, p, ctrl, t):
    """Insert the inner knot t once (Boehm). The curve does not change."""
    if not knots[p] < t < knots[len(ctrl)]:
        raise ValueError("only inner knots can be inserted")
    if knots.count(t) + 1 > p:
        raise ValueError("an inner knot can have multiplicity at most p")
    k = find_span(knots, p, t)
    new = []
    for i in range(len(ctrl) + 1):
        if i <= k - p:
            new.append(ctrl[i])
        elif i > k:
            new.append(ctrl[i - 1])
        else:
            a = (t - knots[i]) / Fraction(knots[i + p] - knots[i])
            new.append(add(scale(1 - a, ctrl[i - 1]), scale(a, ctrl[i])))
    return knots[:k + 1] + [t] + knots[k + 1:], new


def missing_knots(knots, target):
    """The multiset target minus the multiset knots; raises if knots is not a part of target."""
    out = []
    for t in sorted(set(target)):
        d = target.count(t) - knots.count(t)
        if d < 0:
            raise ValueError("the target knot vector must contain the old one")
        out += [t] * d
    return out


def refine(knots, p, ctrl, target):
    """Insert knots until the knot vector equals target. The curve does not change."""
    for t in missing_knots(knots, target):
        knots, ctrl = insert_knot(knots, p, ctrl, t)
    return knots, ctrl


def merge_knots(vectors):
    """The least knot vector that contains each vector, as multisets."""
    vals = sorted(set(t for v in vectors for t in v))
    out = []
    for t in vals:
        out += [t] * max(v.count(t) for v in vectors)
    return out


# ---- tensor product surfaces -----------------------------------------------
# A surface is (U, V, p, q, P): degree p on the knots U in u, degree q on the
# knots V in v, and the control net P[k][l], k along u and l along v.

def surface_point(U, V, p, q, P, u, v):
    k = find_span(U, p, u)
    N = basis_funs(U, p, k, u)
    return lincomb(N, [curve_point(V, q, P[k - p + r], v) for r in range(p + 1)])


def iso_curve_u(U, p, P, u):
    """Control points over V of the curve v -> S(u, v) for a fixed u."""
    k = find_span(U, p, u)
    N = basis_funs(U, p, k, u)
    return [lincomb(N, [P[k - p + r][l] for r in range(p + 1)]) for l in range(len(P[0]))]


def iso_curve_v(V, q, P, v):
    """Control points over U of the curve u -> S(u, v) for a fixed v."""
    return [curve_point(V, q, row, v) for row in P]


def refine_surface(U, V, p, q, P, U2, V2):
    """The same surface on the finer knot vectors U2 and V2."""
    cols = [refine(U, p, [row[l] for row in P], U2)[1] for l in range(len(P[0]))]
    P = [[cols[l][k] for l in range(len(cols))] for k in range(len(cols[0]))]
    return [refine(V, q, row, V2)[1] for row in P]


# ---- interpolation ---------------------------------------------------------

def solve(A, B):
    """Solve A X = B by Gauss-Jordan elimination with exact pivots. B is a list of points."""
    n = len(A)
    M = [[Fraction(x) for x in A[i]] + [Fraction(x) for x in B[i]] for i in range(n)]
    w = len(M[0])
    for c in range(n):
        r = next((r for r in range(c, n) if M[r][c] != 0), None)
        if r is None:
            raise ValueError("singular system")
        M[c], M[r] = M[r], M[c]
        piv = M[c][c]
        M[c] = [x / piv for x in M[c]]
        for r in range(n):
            if r != c and M[r][c] != 0:
                f = M[r][c]
                M[r] = [x - f * y for x, y in zip(M[r], M[c])]
    return [tuple(M[i][n:w]) for i in range(n)]


def averaging_knots(params, p):
    """Clamped knots whose inner knots are means of p consecutive parameters."""
    n = len(params) - 1
    inner = [sum(params[j:j + p], Fraction(0)) / p for j in range(1, n - p + 1)]
    return [params[0]] * (p + 1) + inner + [params[-1]] * (p + 1)


def interpolate(params, points, p):
    """The B-spline of degree p through points[k] at params[k], with averaging knots."""
    if len(params) != len(points) or len(points) < p + 1:
        raise ValueError("need at least p + 1 points, one parameter each")
    if any(params[k] >= params[k + 1] for k in range(len(params) - 1)):
        raise ValueError("the parameters must increase")
    knots = averaging_knots(params, p)
    A = [basis_row(knots, p, t) for t in params]
    return knots, solve(A, points)
test_gordon_surface.py 129 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/gordon-surface/. The authors are listed in prov/8125BY.jsonl.

import random
import unittest
from fractions import Fraction as Fr

from gordon_surface import gordon_parts, gordon_surface
from si3spline import (add, curve_point, interpolate, iso_curve_u, iso_curve_v,
                       refine, sub, surface_point)

H = [0, 0, 0, Fr(1, 2), 1, 1, 1]


def example_network():
    """The network of the page: two leaning bumps that cross at the centre."""
    zero_u = [(Fr(0), Fr(0), Fr(0)), (Fr(1, 2), Fr(0), Fr(0)), (Fr(1), Fr(0), Fr(0))]
    bump_u = [(Fr(0), Fr(1, 2), Fr(0)), (Fr(1, 4), Fr(1, 2), Fr(2)),
              (Fr(3, 4), Fr(1, 2), Fr(0)), (Fr(1), Fr(1, 2), Fr(0))]
    top_u = [(x, Fr(1), z) for x, _, z in zero_u]
    u_curves = [([0, 0, 0, 1, 1, 1], zero_u), (H, bump_u), ([0, 0, 0, 1, 1, 1], top_u)]
    swap = lambda c: [(y, x, z) for x, y, z in c]
    v_curves = [(k, swap(c)) for k, c in u_curves]
    halves = [Fr(0), Fr(1, 2), Fr(1)]
    return u_curves, v_curves, halves, halves


def random_family(rng, cross, own, grid, deg, extra):
    """Curves through grid[i][j] at own[j], each with its own extra parameters.

    Curve i of the family passes through grid[i][j] at the parameter own[j].
    Its other interpolation points are random, so each curve gets its own
    averaging knots, and the family needs the knot merge.
    """
    curves = []
    for i in range(len(cross)):
        ts = set(own) | {Fr(0), Fr(1)}
        while len(ts) < len(own) + 2 + extra:
            ts.add(Fr(rng.randint(1, 99), 100))
        ts = sorted(ts)
        pts = [grid[i][own.index(t)] if t in own else
               tuple(Fr(rng.randint(-9, 9)) for _ in range(3)) for t in ts]
        curves.append(interpolate(ts, pts, deg))
    return curves


def random_network(rng, n, m, p, q):
    us = sorted(rng.sample([Fr(k, 20) for k in range(1, 20)], n - 2) + [Fr(0), Fr(1)])
    vs = sorted(rng.sample([Fr(k, 20) for k in range(1, 20)], m - 2) + [Fr(0), Fr(1)])
    grid = [[tuple(Fr(rng.randint(-9, 9)) for _ in range(3)) for _ in vs] for _ in us]
    v_curves = random_family(rng, us, vs, grid, q, rng.randint(0, 3))
    gridT = [[grid[i][j] for i in range(len(us))] for j in range(len(vs))]
    u_curves = random_family(rng, vs, us, gridT, p, rng.randint(0, 3))
    return u_curves, v_curves, us, vs


class TestGordonSurface(unittest.TestCase):
    def assert_interpolates(self, u_curves, v_curves, us, vs, S, rng):
        U, V, p, q, P = S
        for u, (kn, ctrl) in zip(us, v_curves):
            # the iso-curve at u and the input curve on the same knots: equal nets
            self.assertEqual(iso_curve_u(U, p, P, u), refine(kn, q, ctrl, V)[1])
            t = Fr(rng.randint(0, 1000), 1000)
            self.assertEqual(surface_point(U, V, p, q, P, u, t), curve_point(kn, q, ctrl, t))
        for v, (kn, ctrl) in zip(vs, u_curves):
            self.assertEqual(iso_curve_v(V, q, P, v), refine(kn, p, ctrl, U)[1])
            t = Fr(rng.randint(0, 1000), 1000)
            self.assertEqual(surface_point(U, V, p, q, P, t, v), curve_point(kn, p, ctrl, t))

    def test_example(self):
        net = example_network()
        S = gordon_surface(*net)
        U, V, p, q, P = S
        self.assertEqual((U, V, p, q), (H, H, 2, 2))
        heights = [[z for _, _, z in row] for row in P]
        self.assertEqual(heights, [[0, 0, 0, 0], [0, 3, 1, 0], [0, 1, -1, 0], [0, 0, 0, 0]])
        self.assertEqual(surface_point(*S, Fr(1, 2), Fr(1, 2)), (Fr(1, 2), Fr(1, 2), 1))
        self.assertEqual(surface_point(*S, Fr(1, 4), Fr(1, 4))[2], Fr(21, 16))
        S1, S2, T = gordon_parts(*net)
        z = [surface_point(*X, Fr(1, 4), Fr(1, 2))[2] for X in (S1, S2, T, S)]
        self.assertEqual(z, [Fr(3, 4), Fr(5, 4), Fr(3, 4), Fr(5, 4)])
        self.assert_interpolates(*net, S, random.Random(1))

    def test_random_networks_are_interpolated(self):
        rng = random.Random(7)
        for _ in range(12):
            p, q = rng.randint(1, 3), rng.randint(1, 3)
            n, m = p + rng.randint(0, 2), q + rng.randint(0, 2)
            net = random_network(rng, n + 1, m + 1, p, q)
            S = gordon_surface(*net)
            self.assert_interpolates(*net, S, rng)

    def test_net_sum_equals_boolean_sum(self):
        rng = random.Random(3)
        net = random_network(rng, 4, 3, 2, 2)
        S = gordon_surface(*net)
        S1, S2, T = gordon_parts(*net)
        for _ in range(10):
            u, v = Fr(rng.randint(0, 50), 50), Fr(rng.randint(0, 50), 50)
            boolean = sub(add(surface_point(*S1, u, v), surface_point(*S2, u, v)),
                          surface_point(*T, u, v))
            self.assertEqual(surface_point(*S, u, v), boolean)

    def test_lofts_alone_miss_the_other_family(self):
        u_curves, v_curves, us, vs = example_network()
        S1, _, _ = gordon_parts(u_curves, v_curves, us, vs)
        # S1 takes the v-curves, but at v = 1/2 it is not the bump u-curve
        kn, ctrl = u_curves[1]
        self.assertNotEqual(surface_point(*S1, Fr(1, 8), Fr(1, 2)),
                            curve_point(kn, 2, ctrl, Fr(1, 8)))

    def test_incompatible_network_is_rejected(self):
        u_curves, v_curves, us, vs = example_network()
        kn, ctrl = v_curves[1]
        moved = list(ctrl)
        moved[1] = (moved[1][0], moved[1][1], moved[1][2] + 1)
        bad = list(v_curves)
        bad[1] = (kn, moved)
        with self.assertRaises(ValueError):
            gordon_surface(u_curves, bad, us, vs)

    def test_too_few_curves_is_rejected(self):
        u_curves, v_curves, us, vs = example_network()
        with self.assertRaises(ValueError):
            gordon_surface(u_curves, v_curves[:2], us[:2], vs)


if __name__ == "__main__":
    unittest.main()

History

Coons described his patches at MIT in the 1960s, and Gordon, at General Motors Research, extended the idea in 1969 from four boundary curves to a whole network of curves, blended with splines. In 1971 he put the patches of Coons and his own surfaces into one theory of blending function methods, in which the surfaces are Boolean sums of commuting projectors. Piegl and Tiller give the B-spline form used here, with compatible knot vectors and the sum of control nets.

References

  1. Gordon 1969, the surface through a rectangular network of curves as the sum of two spline blended lofts minus the tensor product spline interpolant of the crossing points
  2. William J. Gordon, Blending-function methods of bivariate and multivariate interpolation and approximation, SIAM Journal on Numerical Analysis 8(1), 158 to 177, 1971, the Boolean sum P + Q - PQ of commuting projectors, and the statement that it interpolates wherever P or Q does
  3. Les Piegl and Wayne Tiller, The NURBS Book, second edition, Springer, 1997, Section 10.5, Interpolation of a Bidirectional Curve Network (p. 485), and Section 9.2.1 (global curve interpolation with averaged knots)
  4. Gerald Farin, Curves and Surfaces for CAGD, A Practical Guide, fifth edition, Morgan Kaufmann, 2002, Chapter 22, Coons Patches (Boolean sums of blending projectors)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example