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
- Model
- Real RAM
- Properties
- deterministicofflineiterative
- Evidence
- Proof sketchCode in 1 languages
- 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: curves along which varies and curves along which 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 , B-spline curves of one degree with clamped knot vectors on , and the u-curves , B-spline curves of one degree on . Increasing parameters and , with and . The curve is meant to lie on the surface at , and at .
Compatibility. The network is compatible if the curves meet where they should:
Output. A B-spline surface of degree with
No surface can do this for an incompatible network, since would have two values. The network must also be rectangular: each u-curve crosses every v-curve once, at the same parameters for all . Curves of one family with different degrees must first be raised to one degree, which does not change them.
The algorithm
Write for the operator that interpolates data given at by a B-spline curve of degree with averaged knots, as on the curve interpolation page, and for the same operator in with degree . Applied to points that are themselves control points of curves, the operator works entry by entry.
GORDON(f_0..f_n, g_0..g_m, u_0..u_n, v_0..v_m)- 1
refine all f_i to one knot vector V_c by knot insertion; F_i ← the control points of f_i on V_c - 2
refine all g_j to one knot vector U_c by knot insertion; G_j ← the control points of g_j on U_c - 3
p_ij ← f_i(v_j); if p_ij ≠ g_j(u_i) for some i, j then report "incompatible network" - 4
S1 ← ℒ_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 - 5
S2 ← ℒ_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 - 6
T ← ℒ_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 - 7
U ← U_1 merged with U_c; V ← V_c merged with V_2; refine S1, S2 and T to U × V by knot insertion - 8
return 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: and use the same knots and the same parameters in , and and use the same knots in . 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
where is the spline on with and is the spline on with . In the language of Gordon 1971, let and for a surface . These are projectors, and they commute. For any that contains the network, , the Boolean sum, and it uses only the values of on the network. With and linear and it is the bilinearly blended Coons patch.
Example
Take three u-curves at and three v-curves at , all of degree (), in the height field form . The outer curves are straight lines at height on the edges of the unit square, Bezier curves with knots . The two middle curves are the same leaning bump : a quadratic B-spline on the knots with heights , so , , and . The middle u-curve is and the middle v-curve is . They cross at the centre at height , and all other crossings are at height , so the network is compatible.
- L1, L2. The merged knot vector of each family is . The outer lines get four control points by the insertion of the knot .
- L4. Three curves at with degree give the averaged knots , so is the quadratic Lagrange polynomial and .
- L5, L6. In the same way has height , and has height , since the only non zero crossing height is .
- L7, L8. , and the control net of the result has the and coordinates and the heights
In the inner block the refined nets of , and have the heights , and , so for example .
The heights of the three parts at two points show how the terms cancel:
| point | target | ||||
|---|---|---|---|---|---|
| , on | |||||
| , off the network | none |
On the u-curve the loft alone gives , the height of the quadratic through the three crossing points, and misses the curve. There adds the missing .
Correctness
The proof has two parts. First, each interpolation of lines L4 to L6 is well defined, and , and 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 , because , while by compatibility, so . The line 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 , 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 have degree , the curves have degree , the parameters increase, , , the outer parameters and are the ends of the knot vectors of the other family, and the network is compatible: .
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 is a curve in whose control points are the values at of the column curves. So lines L1, L2 and L7 do not change any curve or surface, and after line L1 every is with the same B-spline basis on ; likewise on .
Lemma 2 (the interpolation operator)
Let be the averaged knot vector of degree for the parameters , with basis , and let be the collocation matrix . Then is nonsingular, and the splines
satisfy . For any data in a vector space, the spline that line L4 computes with control points is .
Proof. With averaged knots each inner knot is the mean of consecutive parameters, for , so for the inner indices, and and are the end knots. These are the conditions of the Schoenberg-Whitney theorem, so is nonsingular. Then . The spline with control points is . The data may be points, or tuples of control points, since the computation is linear and is done for each entry.
Let be the splines of Lemma 2 for the parameters , degree and the knots , with .
Lemma 3 (the three surfaces)
The surfaces of lines L4, L5 and L6 are
Proof. In line L4 the data for the index are , so by Lemma 2 the control points of are . Then
using Lemma 1 for the last factor. The proof for is the same with the roles of and exchanged. In line L6, the first step gives for each the spline in , with control points . The second step interpolates, for each , the values at , which by Lemma 2 gives control points with . Hence .
Theorem
If line L3 does not report an incompatible network, the surface returned in line L8 satisfies for all and all in , and for all and all in .
Proof. By Lemma 1 the refined nets of line L7 define the same three surfaces on one basis . A tensor product surface depends linearly on its net, so the net defines . Fix . By Lemma 3 and ,
Line L3 passed, so for every , and the last two terms are equal. Hence . For fixed, gives , and , so .
The operator form
The theorem is a case of a general fact about Boolean sums. For surfaces on the rectangle put and . Both are linear, and both are projectors: because . They commute, , because they act on different variables. The Boolean sum satisfies
For any surface , vanishes on every line , since . As and commute, the error vanishes on every line and on every line . For a compatible network, the surface of the theorem itself contains the network, so the network comes from some , and uses only the values of 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 for some , and no surface can contain both curves, since it would take two values at . 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 be the total size: the numbers of curves and and the numbers of control points of a curve after lines L1 and L2. Each interpolation solves one or system with many right sides, one for each control point of a curve. The implementation uses dense Gaussian elimination, which costs for , and the same with for and . Line L7 inserts knots into nets of points. Each insertion changes or points in each of rows or columns, but the implementation copies the whole row, so line L7 costs . The total is . The collocation matrices are banded, with at most or non zero entries beside the diagonal in each row, so a banded solver and an insertion in place bring the cost down to for fixed degrees, which is the size of the output. The extra space is the three nets, .
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 are non zero, though decaying, far from . 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: , and in the Boolean sum.
- Reproduces a surface of its own space. If the network is cut from a surface whose sections are all splines on , then and . The same holds with and .
Variants and improvements
- Polynomial Gordon surfaces. With Lagrange interpolation in place of splines, as in the example, and 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 or 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 and . 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 v-curves and 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 , and . 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 or 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.
# 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
- 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
- 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
- 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)
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 8125BY Edit this page History Report an error Markdown