Data structureadvanced
Hierarchical B-spline
Also called HB-spline.
Forsey and Bartels's spline built from nested levels of B-spline bases chosen by refinement regions, used by Vuong and coauthors for adaptive analysis.
- Time
- O(N p^2) worst · average · best
- Space
- O(k) extra
- Where
- N = the number of tensor B-splines of all levels together; k = the number of selected basis functions; p = the degree
- Model
- Real RAM
- Properties
- deterministiciterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- David R. Forsey, Richard H. Bartels, 1988
A hierarchical B-spline space is built from a sequence of nested tensor product B-spline spaces, one per level, each finer than the last, and a nested sequence of domains . The basis takes, from each level , the B-splines whose support lies inside but not inside . So the functions are coarse where the domains stop and fine where they go on. Forsey and Bartels introduced hierarchical refinement in 1988 to edit surfaces locally. Kraft's selection rule of 1997 makes the functions linearly independent, and Vuong, Giannelli, Jüttler and Simeon used it in 2011 for adaptive isogeometric analysis.
In plain words
Start with a coarse grid of smooth bumps that covers the whole surface. In the places where you need more detail, mark a region and lay a second grid of bumps there, with half the spacing. Inside a smaller region you can lay a third grid, and so on.
You cannot keep every bump of every grid, because a coarse bump that sits inside a fine region is then just a sum of fine bumps. So the rule is: keep a bump of a grid if it fits inside the region of its own grid, and drop it if it fits inside the region of the next, finer grid.
The bumps that remain are independent, and together they can make every shape that the grids can make. They do not add up to 1.
The abstract type
A hierarchical B-spline space of degree with levels stores, for each level , a tensor product knot vector and a domain . The operations are:
- BASIS: the list of selected B-splines, as pairs (level, index).
- EVALUATE: the value of a function given by coefficients on the selected B-splines.
- REFINE: add a region to , where is a union of cells of level inside .
- FINEST: the coefficients of a selected B-spline in the tensor B-splines of the finest level, by the two-scale relation.
Representation
Level has, in each direction, the open uniform knot vector of degree on with spacing : copies of 0, the points , and copies of . Its knots contain those of level , so by knot refinement each B-spline of level is a combination of B-splines of level , the two-scale relation. A cell of level is a square of side between grid lines. The B-spline is nonzero on the cells in each direction.
Each domain is a set of cells of level , with the whole square, a union of cells of level , and . Set . "The support of lies in " means that every cell of the support is a cell of . The two-scale coefficients are stored as one sparse matrix per level and direction, computed once by Boehm insertion of all the midpoints.
Operations
BASIS(levels, Ω) ▷ Kraft's selection- 1
for ℓ ← 0 to L − 1 do - 2
for each tensor B-spline β of level ℓ do - 3
if supp β ⊆ Ω^ℓ and not supp β ⊆ Ω^(ℓ+1) then - 4
add (ℓ, β) to H
EVALUATE(c, x, y)- 1
s ← 0 - 2
for each (ℓ, (i, j)) with coefficient c in the list do - 3
s ← s + c · N^ℓ_i(x) · N^ℓ_j(y) ▷ Cox-de Boor on the knots of level ℓ - 4
return s
REFINE adds the four children of each given cell to and checks that the cell is in ; then BASIS runs again. FINEST applies the two-scale matrices of the levels to each index and multiplies the two results.
Example
Take , and three levels, with and . Level 0 has biquadratic B-splines. The four with have their support inside , so they are dropped, and 32 remain. Level 1 has B-splines; the 16 with lie in , and 4 of them, with , lie in , so 12 are kept. Level 2 keeps the 16 B-splines that lie in . The basis has functions, and their expansions in the finest level have rank 60.
The functions are not a partition of unity. At their sum is , because there the kept functions of all three levels overlap. Far from the refined region, at , only level 0 functions are nonzero, and the sum is 1. The constant 1 is still in the space, with other coefficients, because the space contains all the level 0 B-splines.
Correctness
The two-scale relation writes a B-spline of level as a nonnegative combination of B-splines of level whose supports lie inside its own. From this, by induction from the finest level down, every B-spline of level whose support lies in is in the span of the selected functions: either the rule selects it, or its support lies in and its children do. Two facts follow. The space contains the whole level 0 spline space, and enlarging the domains gives a larger space, so refinement is nested. The selected functions are linearly independent: on a cell of outside only functions of levels up to are nonzero, and B-splines of one level are linearly independent on each cell. The proofs are in the correctness proof.
Proof: Kraft's hierarchical basis is linearly independent, contains each level on its domain, and gives nested spacesfull
We use the representation of the page. Level has the tensor B-splines of degree on the knot vector of level , and its cells are the squares of side . The support cells of are the cells of level on which is not identically zero; they form a rectangle, and in the interior of each of them. The domains satisfy , , each is a union of cells of level , and . "" means that all support cells of lie in . is the output of lines L1 to L4, its functions of level , and their span.
Lemma 1 (local linear independence). For a cell of level , the B-splines of level that are not zero on are linearly independent as functions on .
Proof. In one direction, the B-splines of degree that are nonzero on a knot interval span the polynomials of degree at most there, a space of dimension , by the Curry-Schoenberg theorem restricted to one interval; so they are independent on it. The tensor products of two independent families on two intervals are independent on the rectangle: if , then for each the coefficients vanish, and then each .
Lemma 2 (two-scale relation). Each of level is over B-splines of level with , and every with has its support inside the support of .
Proof. Work in one direction; the tensor product of the two directions then gives the claim. The knots of level contain those of level , so inserting the new knots one at a time by Boehm's rule writes in the B-splines of level . Each new coefficient is with , and the start vector is a unit vector, so all coefficients are nonnegative. The support of runs between two knots of level , which are knots of level , so is a union of fine knot intervals. A fine B-spline whose support is not inside is nonzero on some fine interval outside . On the sum equals , and the fine B-splines that are nonzero on are independent there by Lemma 1, so .
Lemma 3 (each level on its domain). Every B-spline of level with lies in .
Proof. By downward induction on . If , line L3 selects . This always happens for , since is empty. Otherwise and . By Lemma 2, with each of level and support inside that of , so , and by induction . So .
Theorem 1 (linear independence). The functions of are linearly independent.
Proof. Let . We show by induction on that for every . Assume it for the levels below , so . Let . Since , it has a support cell of level that is not in . Since is a union of cells of level , the interior of does not meet . Every function of a level in lies in , so it is zero on the interior of . Hence on . By Lemma 1 every of level that is not zero on has , and is one of them.
Theorem 2 (the space). contains every tensor product spline of level 0 and every B-spline of level whose support lies in . In particular the constant 1 lies in .
Proof. Every level 0 B-spline lies in , so Lemma 3 applies to it and to the other levels. The constant is the sum of the level 0 B-splines, by the partition of unity of B-splines on an open knot vector.
Theorem 3 (nested spaces). Let for every , both sequences with the properties above, and let be the selection for . Then . So REFINE never loses a function of the old space.
Proof. Each has , so by Lemma 3 for .
Correctness of the operations. Lines L1 to L4 test exactly the rule of Theorem 1 for each tensor B-spline of each level. Lines L5 to L8 sum the given coefficients times the values of the selected B-splines, computed by the Cox-de Boor recursion on the knot vector of their level. REFINE keeps and the alignment to cells of level , so the hypotheses above hold after every call. The tests check Theorem 1, the containment of Lemma 3 and Theorem 3 by exact ranks of the expansions in the finest level.
Complexity
Let be the number of tensor B-splines of all levels together and the number of selected ones. BASIS tests each of the B-splines against cells of its support, with a hash set of cells, so it takes time. EVALUATE as written takes time, since each B-spline costs by Cox-de Boor; with a cell index that lists the functions nonzero on each cell, only functions are touched. FINEST on level costs for the output. The extra space is for the basis, besides the domains.
Variants
- Truncated hierarchical B-splines. Giannelli, Jüttler and Speleers cut from each coarse function its part in the finer B-splines that are inside the next domain. The truncated basis spans the same space, is a partition of unity and has smaller supports.
- Other refinements between levels. The construction needs only nested spaces of the same degree, so the levels may use nonuniform knots and different knot insertions in each direction.
- Forsey and Bartels's overlays. The original hierarchical editing adds offsets on finer patches to the coarse surface. It is a representation for modelling, not a basis selection.
- Admissible meshes. For analysis, the domains are often restricted so that only a bounded number of levels have functions that are nonzero on a cell.
When to use it
Use hierarchical B-splines for adaptive isogeometric analysis and local surface fitting when you want a simple data structure, the tensor product structure of each level, and nested spaces under refinement. If the method needs partition of unity or the overlap of supports matters, use the truncated form. For local refinement that is not dyadic, compare T-splines, LR B-splines and PHT-splines.
Pitfalls
- Keeping functions that lie in the finer domain. Without the second test of line L3, a coarse function inside is a combination of fine ones, and the set is dependent.
- Assuming partition of unity. The sum of the selected functions is not 1 near the refined regions. A rational form or a convex hull argument that needs it fails.
- Domains that are not aligned. must be a union of cells of level . A domain that cuts a cell gives supports that are partly inside, and the rule is then ambiguous.
- Overlap of many levels. A coarse function can stay in the basis while its whole interior is covered by fine functions, so the number of functions nonzero at a point grows with the number of levels.
Implementations
The Python module builds the open uniform knot vectors of the levels, the two-scale matrices by Boehm insertion of all midpoints, the domains as sets of cells, Kraft's selection of lines L1 to L4, EVALUATE by Cox-de Boor, the expansion in the finest level, and an exact sparse rank. The tests check that one level gives the tensor B-splines of a separate Cox-de Boor table, that the two-scale relation holds at random points with positive coefficients, the counts of the example, that the selected functions of random hierarchies of degree 2 and 3 are independent and contain every B-spline of level that lies in , that enlarging the domains keeps the old space inside the new one, and that the sum of the functions is not 1 while the constant 1 is in the space.
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/hierarchical-b-spline/. The authors are listed in prov/4MZ00N.jsonl.
"""Bivariate hierarchical B-splines with Kraft's selection, in exact arithmetic.
Level l has the open uniform knot vector of degree p on [0, n0] with spacing
2^-l in each direction, so the spline spaces of the levels are nested. A
B-spline of level l is a pair (i, j) of indices into that knot vector. A cell
of level l is a pair (a, b) for the square [a, a + 1] x [b, b + 1] scaled by
2^-l.
The domains Omega^0 = [0, n0]^2 and Omega^1, ..., Omega^(L-1) are sets of
cells, Omega^l a union of cells of level l - 1 inside Omega^(l-1).
Omega^L is empty. The hierarchical basis has the B-splines of level l whose
support lies in Omega^l but not in Omega^(l+1).
"""
from fractions import Fraction
def open_knots(n0, p, level):
k = 2**level
return [Fraction(0)] * p + [Fraction(a, k) for a in range(n0 * k + 1)] + [Fraction(n0)] * p
def insert_knot(knots, coeffs, u, p):
"""Boehm's rule on a whole coefficient vector: the same spline with u added."""
k = max(i for i in range(len(knots) - 1) if knots[i] <= u < knots[i + 1])
new = []
for i in range(len(coeffs) + 1):
if i <= k - p:
new.append(coeffs[i])
elif i > k:
new.append(coeffs[i - 1])
else:
a = (u - knots[i]) / (knots[i + p] - knots[i])
new.append(a * coeffs[i] + (1 - a) * coeffs[i - 1])
return sorted(knots + [u]), new
def two_scale(n0, p, level):
"""R[i] = {j: c}: B-spline i of the level is sum_j c N_j of the next level."""
coarse = open_knots(n0, p, level)
fine = open_knots(n0, p, level + 1)
new = sorted(set(fine) - set(coarse))
R = []
for i in range(len(coarse) - p - 1):
knots, c = list(coarse), [Fraction(int(r == i)) for r in range(len(coarse) - p - 1)]
for u in new:
knots, c = insert_knot(knots, c, u, p)
assert knots == fine
R.append({j: x for j, x in enumerate(c) if x})
return R
def bspline(knots, i, p, x):
"""N_i of degree p on the knots at x, by Cox-de Boor; closed at the right end."""
last = knots[-1]
vals = [Fraction(int(knots[k] <= x < knots[k + 1] or (x == last and knots[k] < knots[k + 1] == last))) for k in range(i, i + p + 1)]
for d in range(1, p + 1):
vals = [
((x - knots[i + k]) / (knots[i + k + d] - knots[i + k]) * vals[k] if knots[i + k + d] != knots[i + k] else 0)
+ ((knots[i + k + d + 1] - x) / (knots[i + k + d + 1] - knots[i + k + 1]) * vals[k + 1] if knots[i + k + d + 1] != knots[i + k + 1] else 0)
for k in range(len(vals) - 1)
]
return vals[0]
class Hierarchy:
def __init__(self, n0, p, levels):
self.n0, self.p, self.L = n0, p, levels
self.knots = [open_knots(n0, p, l) for l in range(levels)]
self.R = [two_scale(n0, p, l) for l in range(levels - 1)]
self.omega = [{(a, b) for a in range(n0) for b in range(n0)}] + [set() for _ in range(levels - 1)]
def size(self, level):
return self.n0 * 2**level + self.p
def cells(self, level, i):
"""The cells of the level, in one direction, where N_i is not zero."""
return range(max(i - self.p, 0), min(i, self.n0 * 2**level - 1) + 1)
def support(self, level, B):
i, j = B
return [(a, b) for a in self.cells(level, i) for b in self.cells(level, j)]
def inside(self, level, B, m):
"""Does the support of the level-`level` function B lie in Omega^m (m = level or level + 1)?"""
if m >= self.L:
return False
if m == level:
return all(c in self.omega[m] for c in self.support(level, B))
return all((2 * a + x, 2 * b + y) in self.omega[m] for a, b in self.support(level, B) for x in (0, 1) for y in (0, 1))
def refine(self, level, coarse_cells):
"""Add to Omega^level the given cells of level - 1, which must lie in Omega^(level-1)."""
for a, b in coarse_cells:
if (a, b) not in self.omega[level - 1]:
raise ValueError("Omega^l must lie inside Omega^(l-1)")
self.omega[level] |= {(2 * a + x, 2 * b + y) for x in (0, 1) for y in (0, 1)}
def basis(self):
"""Kraft's selection: a list of (level, (i, j))."""
H = []
for l in range(self.L): # @line L1
for i in range(self.size(l)): # @line L2
for j in range(self.size(l)):
if self.inside(l, (i, j), l) and not self.inside(l, (i, j), l + 1): # @line L3
H.append((l, (i, j))) # @line L4
return H
def evaluate(self, coeffs, x, y):
"""sum of c * B(x, y) over a dict {(level, (i, j)): c}."""
total = Fraction(0) # @line L5
for (l, (i, j)), c in coeffs.items(): # @line L6
total += c * bspline(self.knots[l], i, self.p, x) * bspline(self.knots[l], j, self.p, y) # @line L7
return total # @line L8
def finest(self, level, B):
"""B as {(i, j): c} in the tensor B-splines of the finest level, by the two-scale relation."""
cs, ct = {B[0]: Fraction(1)}, {B[1]: Fraction(1)}
for l in range(level, self.L - 1):
cs = _apply(self.R[l], cs)
ct = _apply(self.R[l], ct)
return {(a, b): x * y for a, x in cs.items() for b, y in ct.items()}
def _apply(R, c):
out = {}
for i, x in c.items():
for j, y in R[i].items():
out[j] = out.get(j, Fraction(0)) + x * y
return out
def rank(vectors):
"""Exact rank of sparse vectors given as dicts, by incremental elimination."""
pivots = {}
for v in vectors:
v = {k: x for k, x in v.items() if x}
while v:
k = min(v)
if k not in pivots:
pivots[k] = v
break
p = pivots[k]
f = v[k] / p[k]
for kk, x in p.items():
v[kk] = v.get(kk, Fraction(0)) - f * x
v = {kk: x for kk, x in v.items() if x}
return len(pivots)
test_hierarchical_b_spline.py 117 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hierarchical-b-spline/. The authors are listed in prov/4MZ00N.jsonl.
import random
from fractions import Fraction as F
from hierarchical_b_spline import Hierarchy, bspline, open_knots, rank
def all_basis(knots, p, x):
"""Reference: every B-spline of degree p at x by the triangular Cox-de Boor table."""
n = len(knots) - p - 1
if x == knots[-1]:
return [F(int(i == n - 1)) for i in range(n)]
N = [F(int(knots[i] <= x < knots[i + 1])) for i in range(len(knots) - 1)]
for d in range(1, p + 1):
N = [
((x - knots[i]) / (knots[i + d] - knots[i]) * N[i] if knots[i + d] > knots[i] else 0)
+ ((knots[i + d + 1] - x) / (knots[i + d + 1] - knots[i + 1]) * N[i + 1] if knots[i + d + 1] > knots[i + 1] else 0)
for i in range(len(N) - 1)
]
return N
def example(p=2):
"""Three levels on [0, 4]^2: Omega^1 = [0, 2]^2 and Omega^2 = [0, 1]^2."""
H = Hierarchy(4, p, 3)
H.refine(1, [(a, b) for a in range(2) for b in range(2)])
H.refine(2, [(a, b) for a in range(2) for b in range(2)])
return H
def random_hierarchy(rng, p):
H = Hierarchy(4, p, 3)
H.refine(1, rng.sample(sorted(H.omega[0]), 7))
H.refine(2, rng.sample(sorted(H.omega[1]), 9))
return H
def pts(rng, k):
return [(F(rng.randint(0, 400), 100), F(rng.randint(0, 400), 100)) for _ in range(k)]
def test_one_level_is_the_tensor_b_spline_basis():
H = Hierarchy(3, 2, 1)
basis = H.basis()
assert len(basis) == 25
rng = random.Random(1)
for x, y in pts(rng, 10):
if x > 3 or y > 3:
continue
Nx, Ny = all_basis(H.knots[0], 2, x), all_basis(H.knots[0], 2, y)
for _, (i, j) in basis:
assert H.evaluate({(0, (i, j)): 1}, x, y) == Nx[i] * Ny[j]
def test_two_scale_relation_against_direct_evaluation():
rng = random.Random(2)
for p in (2, 3):
H = Hierarchy(4, p, 2)
fine = H.knots[1]
for i, row in enumerate(H.R[0]):
for _ in range(5):
x = F(rng.randint(0, 400), 100)
assert bspline(H.knots[0], i, p, x) == sum(c * bspline(fine, j, p, x) for j, c in row.items())
assert all(c > 0 for c in row.values())
def test_kraft_selection_on_the_example():
H = example()
basis = H.basis()
by_level = [sum(1 for l, _ in basis if l == k) for k in range(3)]
# level 0: the 36 B-splines minus the 4 whose support [i-2, i+1] lies in [0, 2]
assert by_level == [32, 12, 16]
assert rank([H.finest(l, B) for l, B in basis]) == len(basis)
def test_basis_is_independent_and_contains_each_level_on_its_domain():
rng = random.Random(3)
for p in (2, 3):
H = random_hierarchy(rng, p)
basis = H.basis()
vecs = [H.finest(l, B) for l, B in basis]
r = rank(vecs)
assert r == len(basis)
for l in range(3):
for i in range(H.size(l)):
for j in range(H.size(l)):
if H.inside(l, (i, j), l):
assert rank(vecs + [H.finest(l, (i, j))]) == r
def test_enlarging_the_domains_gives_nested_spaces():
rng = random.Random(4)
H = random_hierarchy(rng, 2)
old = [H.finest(l, B) for l, B in H.basis()]
for _ in range(3):
level = rng.choice([1, 2])
H.refine(level, rng.sample(sorted(H.omega[level - 1]), 2))
new = [H.finest(l, B) for l, B in H.basis()]
assert rank(new) == len(new)
assert rank(new + old) == len(new)
old = new
def test_no_partition_of_unity_but_constants_are_in_the_space():
H = example()
basis = H.basis()
total = {}
for l, B in basis:
for k, c in H.finest(l, B).items():
total[k] = total.get(k, F(0)) + c
ones = {(a, b): F(1) for a in range(H.size(2)) for b in range(H.size(2))}
assert total != ones # the plain sum is not 1
assert H.evaluate({b: 1 for b in basis}, F(1, 2), F(1, 2)) > 1
vecs = [H.finest(l, B) for l, B in basis]
assert rank(vecs + [ones]) == len(vecs) # but 1 is in the span
History
Forsey and Bartels introduced hierarchical B-spline refinement for interactive surface editing at SIGGRAPH 1988. Kraft defined the selection by nested domains and proved the functions linearly independent in 1997. Vuong, Giannelli, Jüttler and Simeon applied it to adaptive isogeometric analysis in 2011. Giannelli, Jüttler and Speleers introduced the truncated basis in 2012 and proved its stability in 2014.
References
- David R. Forsey and Richard H. Bartels, Hierarchical B-spline refinement, Computer Graphics 22(4) (SIGGRAPH 1988), 205 to 212, overlays of finer B-spline patches on a coarse surface for local editing (general locator)
- Rainer Kraft, Adaptive and linearly independent multilevel B-splines, in A. Le Méhauté, C. Rabut and L. L. Schumaker (editors), Surface Fitting and Multiresolution Methods, Vanderbilt University Press, 1997, the selection of B-splines of each level by the nested domains, and its linear independence (general locator)
- A.-V. Vuong, C. Giannelli, B. Jüttler and B. Simeon, A hierarchical approach to adaptive local refinement in isogeometric analysis, Computer Methods in Applied Mechanics and Engineering 200(49-52) (2011), 3554 to 3567, the definition of the hierarchical basis by Kraft's rule, its linear independence, the nested spaces under refinement, and adaptive isogeometric analysis with it (general locator)
- Carlotta Giannelli, Bert Jüttler and Hendrik Speleers, THB-splines, the truncated basis for hierarchical splines, Computer Aided Geometric Design 29(7) (2012), 485 to 498, the comparison of the hierarchical basis with the truncated basis, which spans the same space (general locator)
Where this leads
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 | W4 complete |
|
Tag 4MZ00N Edit this page History Report an error Markdown