Data structureresearch
Polynomial spline over hierarchical T-mesh
Also called PHT-spline.
Deng and coauthors' C1 bicubic splines on a hierarchical T-mesh built from vertex basis functions, with simple local refinement for adaptive analysis.
- Time
- O(V L + n r) worst · average · best
- Space
- O(V + n r) extra
- Where
- L = the number of levels; V = the number of vertices after the step; n = the number of basis functions before the step; r = the number of cells split in the step
- Model
- Real RAM
- Properties
- deterministiciterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Jiansong Deng, Falai Chen, Xin Li, Changqi Hu, Weihua Tong, Zhouwang Yang, Yuyu Feng, 2008
A PHT-spline is a function that is a bicubic polynomial on each cell of a hierarchical T-mesh and has continuous first derivatives. The mesh starts as a grid, and each refinement step splits some cells of the finest level into four by a cross. Deng, Chen, Li, Hu, Tong, Yang and Feng introduced the space and its basis in 2008. The space has dimension , where is the number of boundary vertices and the number of interior crossing vertices. Each such basis vertex carries four basis functions, so the spline is fixed by its value, its two first derivatives and its mixed derivative at the basis vertices. Because the continuity is only , the T-junctions do not spread through the mesh, and refinement is simple and local. This makes PHT-splines a natural choice for adaptive isogeometric analysis.
In plain words
Lay a grid over a square and put a cubic patch on each cell, so that the surface and its slopes match across every edge. Where you need more detail, cut a cell into four by a cross. A new corner in the middle of a side can touch the side of a neighbour that was not cut. That is a T-junction.
Describe the surface by four numbers at each corner: the height, the two slopes and the twist. At a T-junction these numbers must be what the big neighbour cell has at that point, or the surface breaks. At the other corners they are free.
So each free corner gets four bumps. When you cut cells, the old bumps are set to zero at the new free corners, and four new small bumps are added there.
The abstract type
A PHT-spline space on stores a hierarchical T-mesh and a basis of , the functions that are bicubic on each leaf cell. The operations are:
- REFINE: split each cell of , which must be leaf cells of the finest level, into four by a cross, and update the basis.
- EVALUATE: the value, the two first derivatives and the mixed derivative of a function at a point.
- COEFFICIENTS: the coefficients on the basis of a spline given by its data at the basis vertices.
A vertex is a boundary vertex if it lies on the boundary of the square. An interior vertex is a T-junction if it lies inside a side of a leaf cell; this cell is its host. The other interior vertices are crossings. The basis vertices are the boundary vertices and the crossings.
Representation
A cell is the square of side with lower left corner . The leaves are a hash set. Every function is a sparse map from vertices to its Hermite data there. On each leaf cell the function is the bicubic Hermite interpolant of the data at its four corners, the tensor product of the cubic Hermite shapes in and . So EVALUATE locates the leaf that contains the point and sums 16 products.
The basis has four functions for each basis vertex , one for each pair . Let be the level at which became a basis vertex and . In one direction, the two cubic B-splines of have the knots and ; at the boundary the outside knots are replaced by . With and the spacings on the two sides of , at the boundary, and , their values and slopes at are
The data of the function at are the products of the pair in and the pair in : value times value, slope times value, value times slope, and slope times slope. Its data at every other basis vertex are zero. Its data at T-junctions are stored too, so that every leaf can be evaluated from its own corners.
Operations
REFINE(R) ▷ R: leaf cells of the finest level K- 1
split each cell of R into four by a cross; K ← K + 1; classify the vertices - 2
N ← the basis vertices that were not basis vertices before; their level is K - 3
for each basis function b do - 4
for each new vertex w of a split cell P where b is not zero: data_b(w) ← (f, f_x, f_y, f_xy) of b on P at w - 5
for each v in N: data_b(v) ← 0 - 6
for each v in N and (α, β) ∈ {0, 1}²: add the function with data D_v(α, β) at v and 0 elsewhere
Line L4 is the subdivision of the old polynomial on the split cell, the Hermite form of de Casteljau subdivision of a Bézier patch. Line L5 is the modification of Deng and coauthors: the old function keeps its data at the old vertices and gets zero data at the new basis vertices. The classification in line L1 tests, for each vertex and each level, whether the cells of that level to its left and right, or below and above, are leaves that contain the vertex inside a side.
COEFFICIENTS uses the dual functionals of the B-splines: at a basis vertex with data , the coefficient of is , with and in , and the same in .
Example
Take . The level 0 mesh has 9 vertices, all basis vertices, and 36 basis functions, the tensor products of the cubic B-splines on the knots . The function of the centre with has the knots in both directions and the data at .
Split the cell . The centre is a crossing, and and are boundary vertices, so there are 12 basis vertices and 48 functions. The midpoints and lie inside sides of the cells and , so they are T-junctions. The function of above had the value at . After the step its data there are zero, its value at falls from to , and its value at stays . Its data at the T-junction are , which are the values of the host cell's polynomial there.
Now split . Only its centre is a new basis vertex; its four side midpoints are T-junctions. The mesh has 13 basis vertices and 6 T-junctions, and the space has dimension 52. A direct count of all piecewise bicubics with the conditions across every shared edge also gives 52.
Correctness
The proof rests on one fact about hierarchical T-meshes. Every T-junction lies inside a side of exactly one leaf cell, its host, and the corners of the host appeared at an earlier step than the T-junction. A piecewise bicubic has well defined Hermite data at every vertex, including the mixed derivative. On each cell it is the Hermite interpolant of its corner data, and the data at a T-junction are those of the host's polynomial. By induction over the steps, the data at the basis vertices fix all data, and any data at the basis vertices extend to a function. So the map to the data at the basis vertices is a bijection and the dimension is .
The constructed functions are in the space, because lines L4 and L5 keep the T-junction data equal to the host's. Each function has data only at its own basis vertex, given by an invertible block, so the functions are a basis. The blocks of one vertex sum to , so the functions sum to 1. The proofs are in the correctness proof.
Proof: The PHT-spline space has dimension 4(V_b + V_+), and REFINE builds a basis with partition of unityfull
We use the notation of the page. The domain is . A cell of level is a square with . The mesh is the set of the cells of level 0. If the finest level of is , then comes from when we replace some leaves of level by their four children. The vertices are the corners of the leaves. is the set of functions on that are bicubic polynomials on each leaf. The order of a point is the least with . The data of a function at a vertex are there.
Lemma 1 (the mesh). Let .
- Every vertex has order at most the finest level , and a vertex of order is a corner only of leaves of level at least .
- Two leaves whose sides lie on the same line and overlap in a segment of positive length have nested sides.
- A vertex lies inside a side (not at an end) of at most one leaf. If it lies inside a side of a leaf of level , then its order is greater than , the vertex is not on , and each corner of has order at most .
Proof. (1) A corner of a cell of level lies in , so its order is at most . If of order is a corner of a leaf of level , then .
(2) The sides are dyadic intervals . Two dyadic intervals with overlapping interiors are nested.
(3) Say lies inside a side of , and the side is vertical; the other case is the same. Then covers the points next to on one side of the line, above and below . A leaf with as a corner exists, because is a vertex. cannot lie on the side of , because leaves do not overlap. So lies on the other side, and the line is not part of near . The same argument shows that is not on at all: if were on the boundary, the side of would have to lie on the boundary, and then no leaf could have as a corner. Now suppose that also lies inside a side of a leaf . If that side is on the same line, lies on the other side, and covers the points next to there, so no leaf has as a corner. If that side is horizontal, and overlap in a quadrant next to . Both cases are impossible. Last, the coordinate of along the side lies strictly between two neighbouring multiples of , so its order is greater than , while the corners of have order at most .
So the vertices split into the boundary vertices, the T-junctions (the interior vertices that lie inside a side of a leaf, which is their unique host), and the crossings (the other interior vertices). The basis vertices are the boundary vertices and the crossings. The test of CLASSIFY is this definition: lies inside a vertical side of a cell of level exactly when is an integer and is not, and the cell is then one of the two cells of level to the left and right of .
Lemma 2 (data of a spline). Let .
- At every vertex , the data of the polynomials of all leaves that contain are equal. So the data of at are well defined.
- On each leaf , is the bicubic Hermite interpolant of its data at the four corners of .
Proof. (1) Let and be leaves on the two sides of a vertical line , with a common segment of positive length. Since is , and for . Differentiate in : and on . These are polynomials, so the equalities also hold at the ends of . The same holds for a horizontal line. The leaves that contain cover a neighbourhood of in . When we go around , two consecutive leaves share a segment of positive length that ends at . So all their data at are equal.
(2) In one variable, cubic Hermite interpolation on an interval reproduces every cubic, because a cubic with zero value and slope at both ends is zero. The bicubic Hermite interpolant is the tensor product of two such operators, so it reproduces every bicubic.
Lemma 3 (when cellwise data give a spline). Let assign data to every vertex of . Let be the function that is, on each leaf, the bicubic Hermite interpolant of at the corners of the leaf. Then if and only if, for each T-junction with host , equals the data at of the polynomial . Call this condition (T).
Proof. If , then (T) holds by Lemma 2. Conversely, assume (T). Let and be leaves with a common segment of positive length on a line, say . By Lemma 1(2) the sides are nested; say the side of lies in the side of . Each end of is a corner of or lies inside . In the second case is a T-junction with host , by Lemma 1(3). In both cases equals the data of at : in the first case because interpolates at its corners, in the second case by (T).
On the line , the trace on is the cubic Hermite interpolant of the values and slopes of at the ends of , because the shapes are 1 or 0 at and their slopes are 0. In the same way, is the cubic Hermite interpolant of and at the ends. The traces of on are cubics with the same data at the ends, so by Lemma 2(2) in one variable they are equal. So and on , and then also there. So and are continuous across every common segment of two leaves. Each is a polynomial on each leaf, so each is continuous on , and .
Theorem 1 (dimension). The map that takes to its data at the basis vertices is a linear bijection onto . So .
Proof. By Lemmas 2 and 3, (its data at all vertices) is a linear bijection from onto the set of that satisfy (T). Let be any data at the basis vertices. We define at the T-junctions in increasing order. For a T-junction with host , the corners of have a smaller order than by Lemma 1(3), so is already defined at them. Set to the data at of the Hermite interpolant of at the corners of . Then satisfies (T), and it agrees with . Every that satisfies (T) and agrees with is equal to this one, by the same induction. So the restriction to the basis vertices is a bijection from the set of that satisfy (T).
Lemma 4 (one step). Let come from by splitting a set of leaves of the finest level . Then:
- The new vertices are the side midpoints and centres of the cells of . They have order .
- The host of every T-junction of is a leaf of that is not split. Its corners are vertices of .
- The old T-junctions stay T-junctions with the same host, and the old basis vertices stay basis vertices. So the set of line L2 contains only new vertices.
Proof. (1) The new leaves are the children of the cells of . Their corners are the corners of the parents and these five points of order . A point of order is not a vertex of , by Lemma 1(1).
(2) A host of level would make a T-junction of order greater than , by Lemma 1(3). That is not possible by Lemma 1(1). So the host has level at most and is a leaf of that is not split.
(3) An old T-junction has order at most , so its host has level less than , by Lemma 1(3). The host is not split, so still lies inside its side. An old basis vertex does not lie inside a side of an old leaf. It also does not lie inside a side of a new leaf, because that would give an order greater than . So is still a basis vertex.
Invariant. For a basis vertex of level (the step at which it became a basis vertex), let . Let and be , or when or on that side, and let . Let be the matrix with the rows and . Define in the same way in . The function has the data at , where is the entry in row and column of . The invariant after step is this. For each stored function, the stored data (zero where nothing is stored) satisfy (T) on , they are at its own vertex , and they are zero at every other basis vertex.
Theorem 2 (REFINE keeps the invariant). The invariant holds after every step.
Proof. At step 0 there are no T-junctions, and each function stores at only. So the invariant holds.
Assume that it holds for . Let be an old function. By Theorem 1 and Lemma 3 its stored data are its data at all vertices of . The function is also in , because a polynomial on a split cell is a polynomial on each child. Its data at the old vertices do not change. Line L4 computes its data at the new vertices from the polynomial of a split cell that contains the new vertex. If has zero data at its four corners, the polynomial is zero and line L4 can skip . A new vertex on a side shared by two split cells gets the same data from both cells, by Lemma 2(1). So after line L4 the stored data are the data of at all vertices of , and they satisfy (T) on .
Line L5 sets the data at the vertices of to zero. Call the result . Let be a T-junction of with host . By Lemma 4(2) the corners of are vertices of , so by Lemma 4(3) they are not in . Also , because is not a basis vertex. So equals the data of at and at the corners of , and still satisfies (T). At the old basis vertices is unchanged, so it is at its own vertex and zero at the others. At the vertices of it is zero.
Line L6 stores, for each , the data at and zero elsewhere. For a T-junction with host , the corners of are old vertices, so they are not , and the polynomial on is zero. So the stored zero at satisfies (T).
Theorem 3 (basis and partition of unity). After any sequence of steps, the stored functions form a basis of and sum to 1.
Proof. , and the same holds for . The four vectors are the rows of the Kronecker product , with the columns in a fixed order. This product has the determinant . By Theorem 2, the images of the functions under the bijection of Theorem 1 form a block diagonal matrix with these invertible blocks. So the functions are a basis.
The rows of sum to , and so do the rows of . So . At a basis vertex , only the four functions of have nonzero data. So the sum of all the functions has the data at every basis vertex. The constant 1 has the same data. By Theorem 1 the sum is 1.
Theorem 4 (COEFFICIENTS). Let have the data at the basis vertex . Let , , and define in the same way in . Then , with .
Proof. Write as , where and are the orders of the derivatives in and . Then . We have , and
So , and the same holds in . The data at of the sum are
At only the functions of contribute. So the sum has the data at all basis vertices, and by Theorem 1 it equals .
Remarks. (1) A new basis vertex of step has order . By Lemma 1(1), the leaves with corner are the four (or two, at the boundary) children around . The new function of line L6 is zero on every other leaf, because its data are zero at all other vertices. On these leaves it has the same data as the tensor product of the cubic B-splines with the local knots and , . Both are in , so they are equal. At step 0 the same argument shows that the basis is the tensor product cubic B-spline basis. (2) By Theorems 2 and 4, an old function changes in one step to . Both sides have the same data at all basis vertices of . This is the modification of Deng and coauthors. (3) The functions are nonnegative. This is proved in the paper of Deng and coauthors, and it is not proved here. The tests check it at sample points.
So EVALUATE, which evaluates the Hermite interpolant of the stored data on the leaf that contains the point, returns the function. REFINE keeps a basis of with partition of unity, and COEFFICIENTS gives the coordinates of a spline in that basis.
Complexity
Let be the number of vertices after the step, the number of levels, the number of basis functions before the step and the number of split cells. The classification in line L1 does hash lookups per vertex, so . Lines L3 to L5 test each old function against each split cell and compute at most five new data per cell, so ; with an index of the functions by cell, only the functions near the split cells do work. Line L6 adds functions. The extra space is for the classification and for the new data in the worst case. EVALUATE takes time to locate the cell and to evaluate.
Variants
- Modified PHT-splines. Ni, Wang and Deng also allow a cell to be split in half, horizontally or vertically, and build the basis in the same way.
- Bases without decay. Under some refinements the modified old functions become very small, which hurts the condition number. Kang, Xu, Chen and Deng proposed a basis of tensor B-splines on submeshes, at the cost of partition of unity, and later work restores partition of unity with weights.
- Other degrees. Spline spaces over T-meshes with bidegree and smoothness have a simple dimension formula when and , by Deng, Chen and Feng. Bicubic is the case used in practice.
- Related local refinement. Hierarchical and truncated hierarchical B-splines, T-splines and LR B-splines keep cubic splines, so a refinement affects a larger region.
When to use it
Use PHT-splines for adaptive analysis and surface fitting where continuity is enough, for example second order problems in isogeometric analysis, and where you want a refinement that only touches the split cells and their neighbours. Their Hermite data also make interpolation of given values and derivatives at the vertices direct. For problems that need continuity or the higher smoothness of B-splines, use truncated hierarchical B-splines or analysis-suitable T-splines.
Pitfalls
- Free data at T-junctions. The data at a T-junction must be those of the host's polynomial. Setting them freely gives a function that is not , and the tests show the jump in the slope.
- Splitting a coarse cell late. The construction assumes that each step splits leaf cells of the finest level. Splitting an older cell can turn a T-junction into a crossing, and then the basis update needs extra care.
- Decay of the basis. Repeated refinement around the same region can make modified old functions very small. Watch the condition number, or use a basis that does not decay.
- Lower smoothness. The functions are only , so the continuity across cells is lower than for cubic B-splines with simple knots, and the number of functions per cell is higher.
Implementations
The Python module stores the leaves in a hash set and each basis function as exact Hermite data at the vertices, classifies the vertices, refines by lines L1 to L6, evaluates by bicubic Hermite interpolation, and computes the coefficients of a spline from its data. The tests check that level 0 gives the tensor cubic B-splines of a Cox-de Boor reference, the counts of the example, that the number of functions equals and the dimension of a brute force system of all piecewise bicubics with conditions, linear independence by exact rank, partition of unity, continuity across every T-junction by one-sided finite differences fitted exactly with cubics, that wrong T-junction data break it, that one step equals the subtraction formula of Deng and coauthors with B-splines of the new level, nested spaces, and nonnegativity at sample points.
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/pht-spline/. The authors are listed in prov/BQ9V8S.jsonl.
"""PHT-splines (Deng, Chen, Li, Hu, Tong, Yang and Feng), in exact arithmetic.
The domain is [0, n]^2. A cell (l, i, j) is the square of side h = 2^-l with
lower left corner (i h, j h). The mesh starts with the n x n cells of level 0,
and step k + 1 splits some leaf cells of level k into four by a cross. This
is a hierarchical T-mesh.
A function of S(3, 3, 1, 1, T), bicubic on each leaf cell and C^1, is stored
by its Hermite data (f, f_x, f_y, f_xy) at the vertices of the mesh. On each
leaf cell it is the bicubic Hermite interpolant of the data at the four
corners. A vertex is a basis vertex if it lies on the boundary or is an
interior crossing; the other interior vertices are T-junctions, which lie
inside a side of a leaf cell.
Each basis vertex v of level l carries four basis functions. Their data at v
are the products of the values and derivatives at v of the two C^1 cubic
B-splines with knots (v - h, v - h, v, v, v + h) and (v - h, v, v, v + h,
v + h), h = 2^-l, in each direction. At a refinement the old functions are
subdivided on the split cells and their data at the new basis vertices are
set to zero, and four new functions are added at each new basis vertex.
"""
from fractions import Fraction
ZERO = (Fraction(0),) * 4
def hermite_1d(a, b, x):
"""Values and derivatives at x of the cubic Hermite shapes on [a, b].
The shapes are, in order: value at a, slope at a, value at b, slope at b.
"""
h = b - a
t = (x - a) / h
return [
(2 * t**3 - 3 * t**2 + 1, (6 * t**2 - 6 * t) / h),
(h * (t**3 - 2 * t**2 + t), 3 * t**2 - 4 * t + 1),
(-2 * t**3 + 3 * t**2, (-6 * t**2 + 6 * t) / h),
(h * (t**3 - t**2), 3 * t**2 - 2 * t),
]
def cell_box(cell):
l, i, j = cell
h = Fraction(1, 2**l)
return i * h, (i + 1) * h, j * h, (j + 1) * h
def bicubic(corner_data, box, x, y):
"""(f, f_x, f_y, f_xy) at (x, y) of the bicubic Hermite interpolant on the box.
corner_data[(cx, cy)] is the data at the corner, cx, cy in {0, 1}.
"""
x0, x1, y0, y1 = box
X, Y = hermite_1d(x0, x1, x), hermite_1d(y0, y1, y)
out = [Fraction(0)] * 4
for cx in (0, 1):
for cy in (0, 1):
d = corner_data[(cx, cy)]
vx, sx = X[2 * cx], X[2 * cx + 1] # value shape, slope shape: (value, derivative)
vy, sy = Y[2 * cy], Y[2 * cy + 1]
for k, (px, py) in enumerate(((vx, vy), (sx, vy), (vx, sy), (sx, sy))):
out[0] += d[k] * px[0] * py[0]
out[1] += d[k] * px[1] * py[0]
out[2] += d[k] * px[0] * py[1]
out[3] += d[k] * px[1] * py[1]
return tuple(out)
class PHT:
def __init__(self, n):
self.n, self.K = n, 0
self.leaves = {(0, i, j) for i in range(n) for j in range(n)}
self.classify()
self.vlevel = {v: 0 for v in self.basis_vertices}
self.funcs = {(v, a, b): {v: self.vertex_data(v, a, b)} for v in sorted(self.basis_vertices) for a in (0, 1) for b in (0, 1)}
# --- the mesh -------------------------------------------------------
def corners(self, cell):
x0, x1, y0, y1 = cell_box(cell)
return {(0, 0): (x0, y0), (1, 0): (x1, y0), (0, 1): (x0, y1), (1, 1): (x1, y1)}
def classify(self):
"""Split the vertices into boundary, crossing and T-junction vertices."""
self.vertices = {p for c in self.leaves for p in self.corners(c).values()}
self.boundary = {v for v in self.vertices if v[0] in (0, self.n) or v[1] in (0, self.n)}
self.tjunctions = {}
for v in self.vertices - self.boundary:
for l in range(self.K + 1):
s = 2**l
x, y = v[0] * s, v[1] * s
if x.denominator == 1 and y.denominator != 1:
hosts = [(l, int(x) - 1, y.numerator // y.denominator), (l, int(x), y.numerator // y.denominator)]
elif y.denominator == 1 and x.denominator != 1:
hosts = [(l, x.numerator // x.denominator, int(y) - 1), (l, x.numerator // x.denominator, int(y))]
else:
continue
host = [c for c in hosts if c in self.leaves]
if host:
self.tjunctions[v] = host[0]
break
self.crossings = self.vertices - self.boundary - set(self.tjunctions)
self.basis_vertices = self.boundary | self.crossings
def locate(self, x, y):
for l in range(self.K + 1):
s = 2**l
i = min(int(x * s), self.n * s - 1)
j = min(int(y * s), self.n * s - 1)
if (l, i, j) in self.leaves:
return (l, i, j)
raise ValueError("point outside the domain")
# --- functions --------------------------------------------------------
def vertex_data(self, v, a, b):
"""Data at v of the basis function (a, b) of the basis vertex v."""
h = Fraction(1, 2 ** self.vlevel[v])
def one(u, c):
hm = h if u > 0 else Fraction(0)
hp = h if u < self.n else Fraction(0)
return (hp / (hm + hp), -3 / (hm + hp)) if c == 0 else (hm / (hm + hp), 3 / (hm + hp))
(vx, dx), (vy, dy) = one(v[0], a), one(v[1], b)
return (vx * vy, dx * vy, vx * dy, dx * dy)
def cell_values(self, data, cell, x, y):
cs = self.corners(cell)
return bicubic({k: data.get(p, ZERO) for k, p in cs.items()}, cell_box(cell), x, y)
def evaluate(self, data, x, y):
"""(f, f_x, f_y, f_xy) of the function with the given vertex data, at (x, y)."""
return self.cell_values(data, self.locate(x, y), x, y)
def refine(self, cells):
"""One refinement step: split leaf cells of the finest level by a cross."""
cells = set(cells)
if any(c not in self.leaves or c[0] != self.K for c in cells):
raise ValueError("only leaf cells of the finest level are split")
old_vertices = self.vertices
for l, i, j in cells: # @line L1
self.leaves.remove((l, i, j))
self.leaves |= {(l + 1, 2 * i + a, 2 * j + b) for a in (0, 1) for b in (0, 1)}
self.K += 1
self.classify()
new_basis = sorted(v for v in self.basis_vertices if v not in self.vlevel) # @line L2
for v in new_basis:
self.vlevel[v] = self.K
for data in self.funcs.values(): # @line L3
add = {}
for c in cells: # @line L4
cs = self.corners(c)
if not any(data.get(p, ZERO) != ZERO for p in cs.values()):
continue
x0, x1, y0, y1 = cell_box(c)
for w in ((x0 + x1) / 2, y0), ((x0 + x1) / 2, y1), (x0, (y0 + y1) / 2), (x1, (y0 + y1) / 2), ((x0 + x1) / 2, (y0 + y1) / 2):
if w not in old_vertices:
add[w] = self.cell_values(data, c, w[0], w[1])
data.update({w: d for w, d in add.items() if d != ZERO})
for v in new_basis: # @line L5
data.pop(v, None)
for v in new_basis: # @line L6
for a in (0, 1):
for b in (0, 1):
self.funcs[(v, a, b)] = {v: self.vertex_data(v, a, b)}
return new_basis
def coefficients(self, data):
"""Coefficients on the basis of a spline given by its data at the basis vertices.
For each basis vertex the four coefficients are the B-spline coefficients
c = f + s f_x + t f_y + s t f_xy, with s in {-h_minus/3, h_plus/3}.
"""
out = {}
for v in sorted(self.basis_vertices):
h = Fraction(1, 2 ** self.vlevel[v])
f, fx, fy, fxy = data.get(v, ZERO)
zero = Fraction(0)
sx = (-(h if v[0] > 0 else zero) / 3, (h if v[0] < self.n else zero) / 3)
sy = (-(h if v[1] > 0 else zero) / 3, (h if v[1] < self.n else zero) / 3)
for a in (0, 1):
for b in (0, 1):
out[(v, a, b)] = f + sx[a] * fx + sy[b] * fy + sx[a] * sy[b] * fxy
return out
test_pht_spline.py 270 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/pht-spline/. The authors are listed in prov/BQ9V8S.jsonl.
import copy
import random
from fractions import Fraction as F
from pht_spline import PHT, ZERO, cell_box
def cubic_bspline(t, x, end):
"""Reference: cubic B-spline on 5 local knots by Cox-de Boor, closed at the domain end."""
vals = [F(int(t[k] <= x < t[k + 1] or (x == end and t[k] < t[k + 1] == end))) for k in range(4)]
for d in range(1, 4):
vals = [
((x - t[k]) / (t[k + d] - t[k]) * vals[k] if t[k + d] != t[k] else 0)
+ ((t[k + d + 1] - x) / (t[k + d + 1] - t[k + 1]) * vals[k + 1] if t[k + d + 1] != t[k + 1] else 0)
for k in range(len(vals) - 1)
]
return vals[0]
def local_knots(u, h, n, c):
"""Local knots of the C^1 cubic B-spline c in {0, 1} of the vertex u with spacing h."""
lo, hi = max(u - h, F(0)), min(u + h, F(n))
if u == 0:
lo = F(0)
return (lo, lo, u, u, hi) if c == 0 else (lo, u, u, hi, hi)
def tensor_bspline(v, h, n, a, b, x, y):
return cubic_bspline(local_knots(v[0], h, n, a), x, F(n)) * cubic_bspline(local_knots(v[1], h, n, b), y, F(n))
def example():
T = PHT(2)
T.refine([(0, 0, 0)])
T.refine([(1, 1, 1)])
return T
def random_mesh(rng, n=2, steps=3):
T = PHT(n)
for _ in range(steps):
finest = sorted(c for c in T.leaves if c[0] == T.K)
T.refine(rng.sample(finest, min(len(finest), rng.randint(1, 3))))
return T
def cases():
rng = random.Random(1)
yield example()
for _ in range(2):
yield random_mesh(rng)
def pts(rng, k, n):
return [(F(rng.randint(0, 1000 * n), 1000), F(rng.randint(0, 1000 * n), 1000)) for _ in range(k)]
def combine(T, coeffs):
out = {}
for key, c in coeffs.items():
for v, d in T.funcs[key].items():
out[v] = tuple(a + c * b for a, b in zip(out.get(v, ZERO), d))
return out
def rank(vectors):
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, F(0)) - f * x
v = {kk: x for kk, x in v.items() if x}
return len(pivots)
def brute_dimension(T):
"""dim S(3, 3, 1, 1, T) from all piecewise bicubics with C^1 conditions across each shared edge."""
cells = sorted(T.leaves)
idx = {c: k for k, c in enumerate(cells)}
eqs = []
for A in cells:
ax0, ax1, ay0, ay1 = cell_box(A)
for B in cells:
bx0, bx1, by0, by1 = cell_box(B)
if ax1 == bx0 and min(ay1, by1) > max(ay0, by0):
line, swap = ax1, False
elif ay1 == by0 and min(ax1, bx1) > max(ax0, bx0):
line, swap = ay1, True
else:
continue
for q in range(4):
for deriv in (0, 1):
e = {}
for p in range(4):
w = line**p if deriv == 0 else (p * line ** (p - 1) if p else F(0))
k = 4 * q + p if swap else 4 * p + q # the power p is in the normal direction
if w:
e[16 * idx[A] + k] = e.get(16 * idx[A] + k, F(0)) + w
e[16 * idx[B] + k] = e.get(16 * idx[B] + k, F(0)) - w
eqs.append(e)
return 16 * len(cells) - rank(eqs)
def fit_cubic(vals):
"""Coefficients a0..a3 of the cubic through (k, vals[k-1]), k = 1..4."""
rows = [[F(k) ** e for e in range(4)] + [vals[k - 1]] for k in range(1, 5)]
for c in range(4):
piv = next(r for r in range(c, 4) if rows[r][c])
rows[c], rows[piv] = rows[piv], rows[c]
for r in range(4):
if r != c and rows[r][c]:
f = rows[r][c] / rows[c][c]
rows[r] = [a - f * b for a, b in zip(rows[r], rows[c])]
return [rows[c][4] / rows[c][c] for c in range(4)]
def jumps(T, data, p, axis, d):
"""Jumps of f and of its derivative along `axis` across the line through p.
The one-sided values f(p +- k d e) for k = 1..4 are fitted by cubics, which
is exact since f is a cubic on each side; the fits give the one-sided
limits of f and of the derivative at p.
"""
e = (1, 0) if axis == 0 else (0, 1)
R = fit_cubic([T.evaluate(data, p[0] + k * d * e[0], p[1] + k * d * e[1])[0] for k in range(1, 5)])
L = fit_cubic([T.evaluate(data, p[0] - k * d * e[0], p[1] - k * d * e[1])[0] for k in range(1, 5)])
return R[0] - L[0], (R[1] + L[1]) / d
def tjunction_probes(T):
"""Points next to each T-junction on the long side and on the stem, with the normal axis."""
eps = F(1, 2 ** (T.K + 2))
out = []
for v, C in sorted(T.tjunctions.items()):
x0, x1, y0, y1 = cell_box(C)
if v[0] in (x0, x1): # the long side is vertical
out += [((v[0], v[1] + eps), 0), ((v[0], v[1] - eps), 0)]
stem_x = v[0] - eps if v[0] == x0 else v[0] + eps
out.append(((stem_x, v[1]), 1))
else:
out += [((v[0] + eps, v[1]), 1), ((v[0] - eps, v[1]), 1)]
stem_y = v[1] - eps if v[1] == y0 else v[1] + eps
out.append(((v[0], stem_y), 0))
return out, eps / 8
def test_level_0_basis_is_the_tensor_c1_bicubic_b_splines():
T = PHT(3)
assert len(T.funcs) == 4 * 16
rng = random.Random(2)
for (v, a, b), data in T.funcs.items():
for x, y in pts(rng, 4, 3):
assert T.evaluate(data, x, y)[0] == tensor_bspline(v, F(1), 3, a, b, x, y)
def test_dimension_formula_against_brute_force():
T = PHT(2)
dims = [len(T.funcs)]
T.refine([(0, 0, 0)])
dims.append(len(T.funcs))
T.refine([(1, 1, 1)])
dims.append(len(T.funcs))
assert dims == [36, 48, 52]
assert len(T.tjunctions) == 6
for T in cases():
n_basis = 4 * (len(T.boundary) + len(T.crossings))
assert len(T.funcs) == n_basis == brute_dimension(T)
def support_cells(T, data):
"""The leaf cells with a corner where the data are not zero; the function is zero on the others."""
return [c for c in sorted(T.leaves) if any(p in data for p in T.corners(c).values())]
def grid(c, k):
x0, x1, y0, y1 = cell_box(c)
return [(x0 + (x1 - x0) * s / (k + 1), y0 + (y1 - y0) * t / (k + 1)) for s in range(1, k + 1) for t in range(1, k + 1)]
def test_basis_is_linearly_independent():
# a bicubic on a cell is fixed by its values on a 4 x 4 grid, so the rank of
# these values is the rank of the functions
for T in cases():
vecs = []
for data in T.funcs.values():
vecs.append({(c, s): T.cell_values(data, c, x, y)[0] for c in support_cells(T, data) for s, (x, y) in enumerate(grid(c, 4))})
assert rank(vecs) == len(T.funcs)
def test_partition_of_unity():
rng = random.Random(3)
for T in cases():
total = combine(T, {k: F(1) for k in T.funcs})
for x, y in pts(rng, 10, T.n):
assert T.evaluate(total, x, y) == (1, 0, 0, 0)
def test_c1_across_t_junctions_by_finite_differences():
rng = random.Random(4)
for T in cases():
probes, d = tjunction_probes(T)
assert probes
spline = combine(T, {k: F(rng.randint(-9, 9)) for k in T.funcs})
some = rng.sample(sorted(T.funcs), 2)
for p, axis in probes:
assert jumps(T, spline, p, axis, d) == (0, 0)
for key in some:
assert jumps(T, T.funcs[key], p, axis, d) == (0, 0)
def test_wrong_t_junction_data_breaks_c1():
T = example()
spline = combine(T, {k: F(1) for k in T.funcs})
v, C = sorted(T.tjunctions.items())[0]
f, fx, fy, fxy = spline[v]
spline[v] = (f, fx + 1, fy + 1, fxy)
probes, d = tjunction_probes(T)
near = [(p, axis) for p, axis in probes if abs(p[0] - v[0]) + abs(p[1] - v[1]) < F(1, 2**T.K)]
assert any(jumps(T, spline, p, axis, d)[1] != 0 for p, axis in near)
def test_refinement_matches_deng_truncation_formula():
rng = random.Random(5)
T0 = PHT(2)
T = copy.deepcopy(T0)
new = T.refine([(0, 0, 0), (0, 1, 0)])
h = F(1, 2)
for (v0, a0, b0), data in T0.funcs.items():
dual = {}
for v in new:
f, fx, fy, fxy = T0.evaluate(data, v[0], v[1])
sx = (-(h if v[0] > 0 else F(0)) / 3, (h if v[0] < 2 else F(0)) / 3)
sy = (-(h if v[1] > 0 else F(0)) / 3, (h if v[1] < 2 else F(0)) / 3)
for a in (0, 1):
for b in (0, 1):
dual[(v, a, b)] = f + sx[a] * fx + sy[b] * fy + sx[a] * sy[b] * fxy
for x, y in pts(rng, 6, 2):
ref = tensor_bspline(v0, F(1), 2, a0, b0, x, y)
ref -= sum(c * tensor_bspline(v, h, 2, a, b, x, y) for (v, a, b), c in dual.items())
assert T.evaluate(T.funcs[(v0, a0, b0)], x, y)[0] == ref
def test_refinement_gives_nested_spaces():
rng = random.Random(6)
T = PHT(2)
for _ in range(3):
old = copy.deepcopy(T)
spline = combine(old, {k: F(rng.randint(-5, 5)) for k in old.funcs})
finest = sorted(c for c in T.leaves if c[0] == T.K)
T.refine(rng.sample(finest, min(2, len(finest))))
data = {v: old.evaluate(spline, v[0], v[1]) for v in T.basis_vertices}
again = combine(T, T.coefficients(data))
for x, y in pts(rng, 8, 2):
assert T.evaluate(again, x, y)[0] == old.evaluate(spline, x, y)[0]
def test_basis_functions_are_nonnegative_at_samples():
for T in cases():
for data in T.funcs.values():
assert all(T.cell_values(data, c, x, y)[0] >= 0 for c in support_cells(T, data) for x, y in grid(c, 3))
History
Deng, Chen and Feng gave the dimension of spline spaces over T-meshes in 2006. In 2008 Deng, Chen, Li, Hu, Tong, Yang and Feng defined PHT-splines on hierarchical T-meshes, with the level by level basis and its use for surface fitting. Nguyen-Thanh, Nguyen-Xuan, Bordas and Rabczuk used them for isogeometric analysis of elastic solids in 2011. Later work changed the basis to avoid decay and allowed splits in half.
References
- Jiansong Deng, Falai Chen, Xin Li, Changqi Hu, Weihua Tong, Zhouwang Yang and Yuyu Feng, Polynomial splines over hierarchical T-meshes, Graphical Models 70(4) (2008), 76 to 86, the hierarchical T-mesh, the basis vertices, the level by level construction of the basis with modification of the old functions, and its properties (general locator)
- Jiansong Deng, Falai Chen and Yuyu Feng, Dimensions of spline spaces over T-meshes, Journal of Computational and Applied Mathematics 194(2) (2006), 267 to 283, the dimension formula for spline spaces over T-meshes, which gives 4(V_b + V_+) for C1 bicubic splines on hierarchical T-meshes (general locator)
- Qian Ni, Xuhui Wang and Jiansong Deng, Modified PHT-splines, Computer Aided Geometric Design 73 (2019), 37 to 53, Section 2.3, formula (1), the dimension formula; Definition 4, modification by setting the Bezier ordinates of the new basis vertices to zero; Definition 5 and Theorem 6, the basis and its properties, for meshes that also allow splits in half
- N. Nguyen-Thanh, H. Nguyen-Xuan, S. P. A. Bordas and T. Rabczuk, Isogeometric analysis using polynomial splines over hierarchical T-meshes for two-dimensional elastic solids, Computer Methods in Applied Mechanics and Engineering 200(21-22) (2011), 1892 to 1908, adaptive isogeometric analysis of plane elasticity with PHT-splines (general locator)
- Hongmei Kang, Jinlan Xu, Falai Chen and Jiansong Deng, A new basis for PHT-splines, Graphical Models 82 (2015), 149 to 159, the decay of the original basis functions under some refinements, and a basis of tensor B-splines on 2 by 2 submeshes (general locator)
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 BQ9V8S Edit this page History Report an error Markdown