Algorithm Search⌘K

Data structurecore

Corner table

Also called Rossignac corner table.

Rossignac's compact triangle mesh structure with two integer arrays, the vertex and the opposite corner of each corner, which supports all adjacency queries.

Time
O(n + m) expected buildO(1) next · opposite · swingO(d) ring
Space
Θ(n + m) extra
Where
d = the degree of the vertex; m = the number of triangles; n = the number of vertices

A corner table stores a triangle mesh in two integer arrays. Each triangle tt has three corners 3t3t, 3t+13t + 1 and 3t+23t + 2, one at each of its vertices. The array VV gives the vertex of each corner, and the array OO gives the opposite corner: the corner of the next triangle across the edge that faces the corner. All other relations come from the index arithmetic: the triangle of corner cc is ⌊c/3⌋\lfloor c/3 \rfloor, the next corner in the triangle is 3⌊c/3⌋+(c+1) mod 33\lfloor c/3 \rfloor + (c+1) \bmod 3, and one step around a vertex is next(O[next(c)])\mathrm{next}(O[\mathrm{next}(c)]). Jarek Rossignac and his students described it in 2001 for the Edgebreaker compression scheme. It works for consistently oriented manifold meshes of triangles, and it uses about half the memory of a half-edge structure.

In plain words

Each triangle has three corners. Give every corner a number: the corners of the first triangle are 0, 1 and 2, the corners of the second are 3, 4 and 5, and so on. Then the triangle of a corner, and the other two corners of that triangle, follow from the number alone.

For each corner you keep two numbers. The first is the point where the corner sits. The second is the corner across from it in the triangle next door, on the far side of the opposite side. With these two lists you can step from a triangle to its neighbours, and you can walk around a point from triangle to triangle until you come back. You store nothing else.

The abstract type

The structure holds a triangle mesh with nn vertices and mm triangles. The mesh is a manifold mesh in the edge sense (each edge has one or two triangles) and consistently oriented (the two triangles of an inner edge use it in opposite directions). The operations are:

  • BUILD(T)\mathrm{BUILD}(T): make the structure from a triangle list TT, as in an indexed face set, or fail if an edge is used twice in one direction.
  • NEXT(c)\mathrm{NEXT}(c), PREV(c)\mathrm{PREV}(c), TRIANGLE(c)\mathrm{TRIANGLE}(c), VERTEX(c)\mathrm{VERTEX}(c): the relations inside one triangle.
  • OPPOSITE(c)\mathrm{OPPOSITE}(c): the corner across the edge that faces cc, or −1-1 on a border.
  • LEFT(c)=O[next(c)]\mathrm{LEFT}(c) = O[\mathrm{next}(c)] and RIGHT(c)=O[prev(c)]\mathrm{RIGHT}(c) = O[\mathrm{prev}(c)]: the corners of the two neighbouring triangles that share an edge at cc.
  • SWING(c)\mathrm{SWING}(c): the corner of the same vertex in the next triangle counterclockwise around it, or −1-1.
  • RING(c)\mathrm{RING}(c): all corners of the fan of the vertex of cc, in order.

Counterclockwise here means the orientation of the mesh: with the right-hand rule for the vertex order, it is counterclockwise when seen from the side of the normal.

Representation

array size meaning
V[c]V[c] 3m3m the vertex of corner cc; V[3t],V[3t+1],V[3t+2]V[3t], V[3t+1], V[3t+2] is triangle tt in order
O[c]O[c] 3m3m the opposite corner of cc, or −1-1 if the edge that faces cc is a border edge
C[v]C[v] nn one corner of vertex vv, or −1-1 (optional)
positions nn the coordinates of each vertex

The array VV is the index buffer of the triangle list, so the table adds only OO, and CC if walks start from a vertex. The edge that faces corner cc runs from V[next(c)]V[\mathrm{next}(c)] to V[prev(c)]V[\mathrm{prev}(c)]. The opposite corner d=O[c]d = O[c] is the corner whose edge runs the other way, from V[c′]V[c'] to V[c′′]V[c''] with c′=prev(c)c' = \mathrm{prev}(c) and c′′=next(c)c'' = \mathrm{next}(c). So O[O[c]]=cO[O[c]] = c.

Operations

  1. BUILD(T) ▷ T: m triangles, consistently oriented
  2. 1for t = 0 .. m − 1, k = 0, 1, 2 do V[3t + k] ← T[t][k]; O[3t + k] ← −1
  3. 2H ← empty hash map from directed edges to corners
  4. 3for each corner c do
  5. 4key ← (V[NEXT(c)], V[PREV(c)]) ▷ the edge that faces c
  6. 5if key is in H then fail ▷ an edge used twice in one direction
  7. 6H[key] ← c
  8. 7for each corner c do
  9. 8d ← H[(V[PREV(c)], V[NEXT(c)])], or −1 if the key is absent
  10. 9O[c] ← d
  11. NEXT(c)
  12. 10return 3⌊c/3⌋ + (c + 1) mod 3
  13. PREV(c)
  14. 11return NEXT(NEXT(c))
  15. SWING(c) ▷ the next corner of V[c], counterclockwise
  16. 12d ← O[NEXT(c)]; if d = −1 then return −1 else return NEXT(d)
  17. RING(c)
  18. 13s ← c; while UNSWING(s) ∉ {−1, c} do s ← UNSWING(s); if UNSWING(s) = c then s ← c
  19. 14output s, SWING(s), SWING(SWING(s)), ... until −1 or back at s

Here UNSWING(c)=PREV(O[PREV(c)])(c) = \mathrm{PREV}(O[\mathrm{PREV}(c)]) is the step the other way. Line L8 looks for the corner whose edge is the reverse of the edge of cc, which is the corner across that edge. Line L5 catches two kinds of bad input: a face with the wrong orientation, and an edge with three or more triangles, since two of them must use it in the same direction.

SWING follows from the definitions. Let cc sit at vv in the triangle (v,a,b)(v, a, b). The corner next(c)\mathrm{next}(c) sits at aa and faces the edge from bb to vv. Its opposite corner dd is in the triangle on the other side of {v,b}\{v, b\}, and it faces the edge from vv to bb, so next(d)\mathrm{next}(d) sits at vv. Line L13 walks back to the first triangle of a border fan, so that line L14 lists the whole fan.

Example

A tetrahedron with the vertices 0=(0,0,0)0 = (0,0,0), 1=(1,0,0)1 = (1,0,0), 2=(0,1,0)2 = (0,1,0), 3=(0,0,1)3 = (0,0,1) and the triangles (0,2,1)(0, 2, 1), (0,1,3)(0, 1, 3), (1,2,3)(1, 2, 3), (0,3,2)(0, 3, 2). By the right-hand rule each normal points out; for example, the normal of (0,2,1)(0, 2, 1) is (0,0,−1)(0, 0, -1). BUILD gives:

corner cc 0 1 2 3 4 5 6 7 8 9 10 11
V[c]V[c] 0 2 1 0 1 3 1 2 3 0 3 2
O[c]O[c] 8 5 10 7 11 1 9 3 0 6 2 4

Corner 0 sits at vertex 0 and faces the edge from 2 to 1. Corner 8 sits at vertex 3 in triangle 2, (1,2,3)(1, 2, 3), and faces the edge from 1 to 2, the same edge the other way. So O[0]=8O[0] = 8 and O[8]=0O[8] = 0.

Now walk around vertex 0 from corner 0. next(0)=1\mathrm{next}(0) = 1, O[1]=5O[1] = 5, and next(5)=3\mathrm{next}(5) = 3, so SWING(0)=3(0) = 3, the corner of vertex 0 in triangle 1. Then next(3)=4\mathrm{next}(3) = 4, O[4]=11O[4] = 11, next(11)=9\mathrm{next}(11) = 9, so SWING(3)=9(3) = 9, in triangle 3. Then next(9)=10\mathrm{next}(9) = 10, O[10]=2O[10] = 2, next(2)=0\mathrm{next}(2) = 0, and the walk is back at corner 0. RING(0)=[0,3,9](0) = [0, 3, 9], and the neighbours V[next(c)]V[\mathrm{next}(c)] in order are 2,1,32, 1, 3.

For the square of two triangles (0,1,2)(0, 1, 2) and (0,2,3)(0, 2, 3), only the diagonal is an inner edge: O=[−1,5,−1,−1,−1,1]O = [-1, 5, -1, -1, -1, 1]. The fan of vertex 2 is a path. RING(2)(2) first steps back to corner 4 in triangle 1, since UNSWING(4)=−1(4) = -1, and returns [4,2][4, 2], with the neighbours 3,0,13, 0, 1.

Correctness

BUILD stores each directed edge with the corner that faces it, and it fails if a directed edge appears twice. Then O[c]O[c] is the corner that faces the reverse edge, which is the corner across the edge in the other triangle, and OO is its own inverse. SWING crosses the edge {v,b}\{v, b\} after cc and stays at the vertex vv, and UNSWING is its inverse. So the corners of a vertex, joined by SWING, form one path or one cycle for each fan, and RING lists one of them from its start. The full proof is below.

Proof: BUILD computes the opposite corners, and SWING walks once around the fan of a vertexfull

Setup. The input is a list of mm triangles, each with three distinct vertices. Corner c=3t+kc = 3t + k is corner kk of triangle tt, with V[c]=T[t][k]V[c] = T[t][k], next(c)=3⌊c/3⌋+(c+1) mod 3\mathrm{next}(c) = 3\lfloor c/3 \rfloor + (c + 1) \bmod 3 and prev(c)=next(next(c))\mathrm{prev}(c) = \mathrm{next}(\mathrm{next}(c)). The edge of cc is the directed edge ϵ(c)=(V[next(c)],V[prev(c)])\epsilon(c) = (V[\mathrm{next}(c)], V[\mathrm{prev}(c)]), the side of its triangle that does not touch cc, in the direction of the triangle. Each directed side of each triangle is ϵ(c)\epsilon(c) for exactly one corner cc of that triangle.

Claim 1. If BUILD does not fail, then for each corner cc, O[c]O[c] is the corner dd with ϵ(d)=rev(ϵ(c))\epsilon(d) = \mathrm{rev}(\epsilon(c)), the reverse edge, if such a dd exists, and −1-1 if not. BUILD fails exactly when some directed edge is the edge of two corners.

Proof. Lines L3 to L6 insert the key ϵ(c)\epsilon(c) for each corner. Line L5 fails if the key is already there, that is, if two corners have the same directed edge. Otherwise HH maps each directed edge that occurs to the one corner that has it. Line L8 looks up the reverse (V[prev(c)],V[next(c)])(V[\mathrm{prev}(c)], V[\mathrm{next}(c)]) of ϵ(c)\epsilon(c), and line L9 stores the answer or −1-1. □\square

Consequence. OO is an involution on the corners with O[c]≠−1O[c] \ne -1: if ϵ(d)=rev(ϵ(c))\epsilon(d) = \mathrm{rev}(\epsilon(c)), then ϵ(c)=rev(ϵ(d))\epsilon(c) = \mathrm{rev}(\epsilon(d)), so O[d]=cO[d] = c. Also O[c]≠cO[c] \ne c and O[c]O[c] is in another triangle, because a triangle with three distinct vertices does not use one edge in both directions. If a mesh is consistently oriented and each edge has one or two triangles, then each inner edge is used once in each direction, so BUILD does not fail, and O[c]=−1O[c] = -1 exactly for the corners whose edge is a border edge. An edge with three or more triangles has two uses in the same direction, so BUILD fails on it.

Claim 2. Let cc be a corner of vertex vv in the triangle (v,a,b)(v, a, b), so V[next(c)]=aV[\mathrm{next}(c)] = a and V[prev(c)]=bV[\mathrm{prev}(c)] = b. If the edge {v,b}\{v, b\} has a second triangle, then SWING(c)(c) is the corner of vv in that triangle. If not, SWING(c)=−1(c) = -1.

Proof. The edge of next(c)\mathrm{next}(c) is (V[prev(c)],V[c])=(b,v)(V[\mathrm{prev}(c)], V[c]) = (b, v). By claim 1, d=O[next(c)]d = O[\mathrm{next}(c)] (line L12) is the corner whose edge is (v,b)(v, b), which is in the other triangle of the edge {v,b}\{v, b\}, or −1-1 if there is none. Since ϵ(d)=(V[next(d)],V[prev(d)])=(v,b)\epsilon(d) = (V[\mathrm{next}(d)], V[\mathrm{prev}(d)]) = (v, b), the corner next(d)\mathrm{next}(d) has the vertex vv. □\square

In the same way UNSWING(c)=prev(O[prev(c)])(c) = \mathrm{prev}(O[\mathrm{prev}(c)]) is the corner of vv in the other triangle of the edge {v,a}\{v, a\}. The two maps are inverse to each other where both are defined: the triangle after (v,a,b)(v, a, b) across {v,b}\{v, b\} has bb as the vertex after vv, so its UNSWING goes back across {v,b}\{v, b\}.

Claim 3. RING(c)(c) (lines L13 and L14) returns each corner of the fan of V[c]V[c] that contains cc exactly once, in the order of SWING, and stops.

Proof. Consider the graph on the corners of vv with an edge from xx to SWING(x)(x). By claim 2 each step crosses one edge {v,w}\{v, w\} into the other triangle at that edge, so the connected component of cc is exactly the fan of cc: the triangles at vv that are joined to the triangle of cc through edges at vv. SWING and UNSWING are inverse partial maps, so each corner has at most one successor and at most one predecessor, and the component is a single path or a single cycle. Line L13 walks back with UNSWING. On a cycle it comes back to cc and starts there. On a path it stops at the first corner, the one with no predecessor. The walk cannot repeat a corner of a path, because the predecessor is unique, so it stops after at most the number of corners in the fan. Line L14 then walks forward with SWING from the start until it reaches −1-1 (the end of the path) or the start again (the cycle is closed). It outputs each corner of the component once. □\square

Termination. BUILD has two loops over the 3m3m corners. RING does at most two passes over one fan. So every operation stops. ■\blacksquare

Complexity

  • BUILD does 3m3m insertions and 3m3m lookups in a hash table, so it takes O(n+m)O(n + m) expected time. Sorting the 3m3m directed edges instead gives O(mlog⁡m)O(m \log m) worst-case time.
  • NEXT, PREV, TRIANGLE, VERTEX, OPPOSITE, LEFT, RIGHT and SWING take O(1)O(1) time.
  • RING takes O(d)O(d) time for a vertex in dd triangles.
  • The space is 6m6m integers for VV and OO, and nn more for CC. For a closed mesh, m≈2nm \approx 2n, so the table has about 12n12n integers, or 6n6n in addition to the index buffer. A half-edge structure with next, twin, vertex and face for each of 3m3m half-edges has about 24n24n integers.

Variants

  • The directed-edge structure. It numbers the half-edges as 3t+k3t + k in the same way and stores the twin of each half-edge, which is the same information as OO with another naming. Botsch and others describe it in Section 2.4 of their book.
  • Corner tables for quads and polygons. For quad meshes, the corners are 4t+k4t + k. For mixed polygons, an extra array of face offsets replaces the index arithmetic.
  • Non-manifold vertices. A vertex with two fans, such as the tip of two cones that touch, has two separate rings. The table still works for each edge, but RING from one corner finds only its own fan. Some programs split such vertices into one copy for each fan.
  • Compression. Edgebreaker walks the triangles with OO and writes one of five symbols for each triangle. This was the first use of the table.
  • Local edits. An edge flip changes two triangles and six entries of OO. An edge collapse, as in mesh simplification, removes two triangles and needs the arrays to be compacted or the dead triangles to be marked.

When to use it

Use a corner table for a triangle mesh that a program walks often but changes little: smoothing, curvature, geodesics, compression and parameterization. It is easy to build from an indexed face set, it is two flat arrays that are fast to copy and to send to a graphics card, and it needs no pointers. For meshes with polygons of mixed sizes, or with many local edits, a doubly connected edge list is simpler. For non-manifold meshes, use a structure such as the radial edge structure.

Pitfalls

  • A flipped triangle. If one triangle has the wrong orientation, its edges have the same direction as those of its neighbours, and BUILD fails or leaves borders where there are none. Run consistent mesh orientation first.
  • Swing at a border. SWING returns −1-1 at a border edge. A walk that starts in the middle of a border fan and only swings forward misses the triangles before it. Walk back first, as line L13 does.
  • Duplicate vertices. Two vertices at the same position are different vertices for the table, and the edge between them is a border. Weld first, see vertex welding.
  • Degenerate triangles. A triangle with a repeated vertex has an edge from a vertex to itself, which breaks the swing around that vertex. Remove such triangles before BUILD.
  • The sign convention. Some texts define swing with prev\mathrm{prev} in place of next\mathrm{next}, which turns the other way. Check the convention of the code you use.

Implementations

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.

corner_table.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/corner-table/. The authors are listed in prov/FBTW3E.jsonl.

"""The corner table of Rossignac: a triangle mesh as two integer arrays V and O.

Corner c = 3t + k is corner k of triangle t. V[c] is its vertex and O[c] is the
opposite corner, the corner of the neighbouring triangle across the edge that
does not touch c, or -1 on a border. The mesh must be oriented consistently
and every edge must have one or two triangles.
"""

from typing import Iterator, Sequence


def next_corner(c: int) -> int:
    """The next corner in the same triangle."""
    return 3 * (c // 3) + (c + 1) % 3  # @line L10


def prev_corner(c: int) -> int:
    """The previous corner in the same triangle."""
    return next_corner(next_corner(c))  # @line L11


def triangle(c: int) -> int:
    return c // 3


class CornerTable:
    """The arrays V and O, and one corner for each vertex."""

    def __init__(self, n_vertices: int, triangles: Sequence[Sequence[int]]) -> None:
        """Build V and O. Raise ValueError if an edge is used twice in one direction."""
        self.V: list[int] = []
        for t in triangles:  # @line L1
            if len(t) != 3 or len(set(t)) != 3:
                raise ValueError(f"not a triangle: {t}")
            self.V.extend(t)
        nc = len(self.V)
        self.O: list[int] = [-1] * nc
        h: dict[tuple[int, int], int] = {}  # @line L2
        for c in range(nc):  # @line L3
            key = (self.V[next_corner(c)], self.V[prev_corner(c)])  # @line L4
            if key in h:  # @line L5
                raise ValueError(f"directed edge {key} is used twice")
            h[key] = c  # @line L6
        for c in range(nc):  # @line L7
            d = h.get((self.V[prev_corner(c)], self.V[next_corner(c)]), -1)  # @line L8
            self.O[c] = d  # @line L9
        self.C: list[int] = [-1] * n_vertices
        for c in range(nc):
            if self.C[self.V[c]] == -1:
                self.C[self.V[c]] = c

    def opposite(self, c: int) -> int:
        return self.O[c]

    def left(self, c: int) -> int:
        """The corner opposite the edge from V[c] to V[prev(c)], seen from c."""
        return self.O[next_corner(c)]

    def right(self, c: int) -> int:
        return self.O[prev_corner(c)]

    def swing(self, c: int) -> int:
        """The corner of the same vertex in the next triangle counterclockwise, or -1."""
        d = self.O[next_corner(c)]  # @line L12
        return -1 if d == -1 else next_corner(d)

    def unswing(self, c: int) -> int:
        """The corner of the same vertex in the next triangle clockwise, or -1."""
        d = self.O[prev_corner(c)]
        return -1 if d == -1 else prev_corner(d)

    def ring(self, c: int) -> list[int]:
        """The corners of the fan of V[c] that contains c, in counterclockwise order.

        For an inner vertex the list starts at c. For a border vertex it starts
        at the most clockwise triangle.
        """
        start = c
        while True:  # @line L13
            u = self.unswing(start)
            if u == c:
                start = c
                break
            if u == -1:
                break
            start = u
        out = [start]
        x = self.swing(start)  # @line L14
        while x != -1 and x != start:
            out.append(x)
            x = self.swing(x)
        return out

    def vertex_neighbors(self, v: int) -> list[int]:
        """The neighbours of v in counterclockwise order (for one fan)."""
        if self.C[v] == -1:
            return []
        r = self.ring(self.C[v])
        out = [self.V[next_corner(c)] for c in r]
        if self.unswing(r[0]) == -1:  # border vertex: add the last neighbour
            out.append(self.V[prev_corner(r[-1])])
        return out

    def border_corners(self) -> Iterator[int]:
        """Corners whose opposite edge is a border edge."""
        return (c for c in range(len(self.V)) if self.O[c] == -1)
test_corner_table.py 165 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/corner-table/. The authors are listed in prov/FBTW3E.jsonl.

import random

from corner_table import CornerTable, next_corner, prev_corner, triangle

# ---- brute force: compare every pair of corners ----


def brute_opposite(tris):
    V = [v for t in tris for v in t]
    O = []
    for c in range(len(V)):
        a, b = V[next_corner(c)], V[prev_corner(c)]
        found = [d for d in range(len(V)) if triangle(d) != triangle(c)
                 and V[next_corner(d)] == b and V[prev_corner(d)] == a]
        assert len(found) <= 1
        O.append(found[0] if found else -1)
    return O


def brute_fans(tris, v):
    """The fans of v: classes of the triangles at v, joined when they share an edge at v."""
    at = [t for t in range(len(tris)) if v in tris[t]]
    parent = {t: t for t in at}

    def find(x):
        while parent[x] != x:
            x = parent[x]
        return x

    for i in at:
        for j in at:
            if i < j and len((set(tris[i]) & set(tris[j])) - {v}) == 1:
                parent[find(i)] = find(j)
    classes = {}
    for t in at:
        classes.setdefault(find(t), set()).add(t)
    return list(classes.values())


# ---- meshes ----


def tetrahedron():
    return 4, [(0, 2, 1), (0, 1, 3), (1, 2, 3), (0, 3, 2)]


def octahedron():
    # 0, 1: poles; 2..5: equator counterclockwise seen from pole 0
    eq = [2, 3, 4, 5]
    tris = []
    for i in range(4):
        a, b = eq[i], eq[(i + 1) % 4]
        tris += [(0, a, b), (1, b, a)]
    return 6, tris


def grid(rng, a, b, torus):
    """An a by b grid of squares, each cut by a random diagonal; glued to a torus or not."""
    nx, ny = (a, b) if torus else (a + 1, b + 1)

    def idx(i, j):
        return (i % nx) * ny + (j % ny)

    tris = []
    for i in range(a):
        for j in range(b):
            p, q, r, s = idx(i, j), idx(i + 1, j), idx(i + 1, j + 1), idx(i, j + 1)
            if rng.random() < 0.5:
                tris += [(p, q, r), (p, r, s)]
            else:
                tris += [(p, q, s), (q, r, s)]
    return nx * ny, tris


def scramble(rng, n, tris):
    perm = list(range(n))
    rng.shuffle(perm)
    out = []
    for t in tris:
        k = rng.randrange(3)
        t = t[k:] + t[:k]
        out.append(tuple(perm[v] for v in t))
    rng.shuffle(out)
    return n, out


def check(n, tris):
    ct = CornerTable(n, tris)
    assert ct.O == brute_opposite(tris)
    for c in range(len(ct.V)):
        if ct.O[c] != -1:
            assert ct.O[ct.O[c]] == c
            if ct.swing(c) != -1:
                assert ct.V[ct.swing(c)] == ct.V[c]
                assert ct.unswing(ct.swing(c)) == c
    for v in range(n):
        fans = brute_fans(tris, v)
        if not fans:
            assert ct.C[v] == -1
            continue
        assert len(fans) == 1
        r = ct.ring(ct.C[v])
        assert {triangle(c) for c in r} == fans[0] and len(r) == len(fans[0])
        assert all(ct.V[c] == v for c in r)
        for x, y in zip(r, r[1:]):  # consecutive triangles share the edge after the first
            assert ct.V[prev_corner(x)] == ct.V[next_corner(y)]
        nb = ct.vertex_neighbors(v)
        assert set(nb) == {w for t in fans[0] for w in tris[t]} - {v}
    return ct


# ---- tests ----


def test_page_example_tetrahedron():
    n, tris = tetrahedron()
    ct = check(n, tris)
    assert ct.V == [0, 2, 1, 0, 1, 3, 1, 2, 3, 0, 3, 2]
    assert ct.O == [8, 5, 10, 7, 11, 1, 9, 3, 0, 6, 2, 4]
    assert ct.swing(0) == 3 and ct.swing(3) == 9 and ct.swing(9) == 0
    assert ct.ring(0) == [0, 3, 9]
    assert ct.vertex_neighbors(0) == [2, 1, 3]
    assert list(ct.border_corners()) == []


def test_octahedron_and_border():
    check(*octahedron())
    n, tris = 4, [(0, 1, 2), (0, 2, 3)]  # a square of two triangles
    ct = check(n, tris)
    assert ct.O == [-1, 5, -1, -1, -1, 1]
    assert ct.vertex_neighbors(0) == [1, 2, 3]
    assert ct.ring(4) == [4, 2] and ct.ring(2) == [4, 2]
    assert ct.vertex_neighbors(2) == [3, 0, 1]
    assert ct.ring(3) == [0, 3]


def test_rejects_bad_orientation_and_non_manifold_edges():
    for tris in ([(0, 1, 2), (0, 1, 3)], [(0, 1, 2), (1, 0, 3), (0, 1, 4)]):
        try:
            CornerTable(5, tris)
        except ValueError:
            continue
        raise AssertionError(tris)


def test_non_manifold_vertex_gives_one_fan():
    tris = [(0, 1, 2), (0, 2, 3), (0, 3, 4), (0, 4, 1), (0, 6, 5), (0, 7, 6), (0, 8, 7), (0, 5, 8)]
    ct = CornerTable(9, tris)
    assert ct.O == brute_opposite(tris)
    assert len(ct.ring(0)) == 4 and len(ct.ring(12)) == 4
    assert {triangle(c) for c in ct.ring(0)} == {0, 1, 2, 3}


def test_random_grids():
    rng = random.Random(2001)
    for it in range(500):
        a, b = rng.randint(3, 5), rng.randint(3, 5)
        n, tris = grid(rng, a, b, torus=it % 2 == 0)
        n, tris = scramble(rng, n, tris)
        ct = check(n, tris)
        border = sum(1 for _ in ct.border_corners())
        assert border == (0 if it % 2 == 0 else 2 * (a + b))

History

Jarek Rossignac, Alla Safonova and Andrzej Szymczak described the corner table in 2001, at Shape Modeling International in Genova, to make the Edgebreaker compression of triangle mesh connectivity simple to program. Edgebreaker itself is from Rossignac's earlier work of 1999. The directed-edge structure of Campagna, Kobbelt and Seidel, from 1998, uses the same implicit numbering of half-edges in triangles.

References

  1. Jarek Rossignac, Alla Safonova and Andrzej Szymczak, 3D compression made simple: Edgebreaker on a corner-table, Shape Modeling International 2001, Genova, pages 278 to 283, the description of the corner table (the arrays V and O and the operators next, previous, opposite, left, right and swing)
  2. Mario Botsch, Leif Kobbelt, Mark Pauly, Pierre Alliez and Bruno Lévy, Polygon Mesh Processing, AK Peters, 2010, Chapter 2, Mesh Data Structures, Section 2.4, Directed-Edge Data Structure (the same idea of implicit triangle indices)

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.

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