./jerome{m}jack in

~/Notes/Extended Marching Cubes.md · note · seedling · 7 min · updated

Extended Marching Cubes

// status: seedling

Extended Marching Cubes (EMC) is Marching Cubes with one addition: in the cells where the surface has a crease, it inserts an extra vertex on the crease. It comes from Feature Sensitive Surface Extraction from Volume Data by Kobbelt, Botsch, Schwanecke and Seidel (SIGGRAPH 2001), a year before Dual Contouring, which borrowed its vertex rule and applied it to every cell.

It is worth knowing for two reasons. It is the smallest change that makes an existing Marching Cubes pipeline keep its edges, and it explains where Dual Contouring's central idea came from.

The problem it fixes

Marching Cubes samples the surface where it crosses grid edges and joins those samples with flat patches. A vertex can sit nowhere else. So a box edge that runs through the middle of a row of cells is replaced by a chamfer, one cell wide, with a sawtooth where the edge drifts across the grid.

Refining the grid shrinks the chamfer and does not remove it. The paper's sharper point is about shading: the positions converge as the grid gets finer, the normals near the feature do not. A lit Marching Cubes box always has a soft band along its edges, however many triangles you spend.

The idea in two dimensions

The paper explains itself with a 2D picture, and so will this note. At each crossing you know the point and the surface normal, which together give a tangent line. In a cell with two crossings, intersect the two tangents. If the outline is straight, the intersection lies on the chord and nothing changes. If the outline turns a corner inside the cell, the tangents meet at the corner.

contour · liveThe extended extractor in two dimensions. Red vertices are the ones it added, where a cell's two tangents meet. Switch to marching to take them away, or to dual to see the same rule applied to every cell.open ↗

The red vertices are the additions. Switch to marching to see the same outline without them, and hover a cell to see the two tangent lines it is intersecting.

What makes this work is an assumption the paper states plainly: the surface is not smooth at a feature, but it is smooth on each side of it. Point and normal on each side give a good local approximation of that side, and the two approximations meet where the feature is.

The algorithm, cell by cell

The structure stays that of Marching Cubes: every cell is processed on its own, from its eight corner values. Three things are added.

1. Detect. Take the unit normals n_i at the cell's edge intersections and find the pair furthest apart. If their dot product is below a threshold, the cell holds a feature. The paper suggests 0.9, about 26° between the two normals.

2. Classify. Let n* be the direction perpendicular to that widest pair. If every other normal lies near the plane the pair spans, the feature is an edge. If some normal points well out of it, three surfaces meet here and it is a corner. The paper suggests 0.7 for that test, about 44° out of the plane.

c
typedef struct { float x, y, z; } Vec3;

static float dot3(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }

static Vec3 cross3(Vec3 a, Vec3 b) {
    return (Vec3){ a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x };
}

typedef enum { FEATURE_NONE, FEATURE_EDGE, FEATURE_CORNER } Feature;

// n: the unit normals at this cell's edge intersections.
Feature classify_cell(const Vec3 *n, int count, float sharp, float corner) {
    // The opening angle of the normal cone: the two normals furthest apart.
    float widest = 1.0f;
    int a = 0, b = 0;
    for (int i = 0; i < count; i++) {
        for (int j = i + 1; j < count; j++) {
            float d = dot3(n[i], n[j]);
            if (d < widest) { widest = d; a = i; b = j; }
        }
    }
    if (widest >= sharp) return FEATURE_NONE;          // the paper suggests 0.9

    // Does any normal leave the plane those two span?
    Vec3 axis = cross3(n[a], n[b]);
    float len = sqrtf(dot3(axis, axis));               // > 0: the two differ
    float out = 0.0f;
    for (int i = 0; i < count; i++) {
        out = fmaxf(out, fabsf(dot3(n[i], axis)) / len);
    }
    return out > corner ? FEATURE_CORNER : FEATURE_EDGE;   // and 0.7 here
}

3. Place the vertex. Find the point p that is closest, in the least-squares sense, to all the tangent planes:

\min_p \sum_i \big(n_i \cdot (p - s_i)\big)^2

where s_i are the intersection points. This is the same quadratic error function as in Dual Contouring, solved the same way, with the pseudo-inverse from a singular value decomposition. The classification earns its keep here: for an edge, the smallest singular value is set to zero outright, because an edge leaves the point free to slide along it and the data should not be trusted to say where.

4. Re-triangulate the cell. Instead of the standard Marching Cubes patch, connect the cell's intersection points to the new vertex as a triangle fan.

5. Flip edges. After all cells are done, the fans do not yet form the feature: each one was built without looking at its neighbours, so the mesh edges still cross the crease. One pass fixes it. Flip every edge whose flip would make it connect two feature vertices. Because there is at most one feature vertex per cell, the flips cannot interfere with each other, and afterwards the feature line exists in the mesh as a chain of edges.

Step 5 is the part with no 2D counterpart, and the part that makes EMC less local than it first looks.

Directed distances

The paper's other contribution is about the input. A scalar distance field stores, at each grid point, the distance to the nearest surface. Near a feature, two neighbouring grid points can be nearest to different faces, and interpolating between their two distances gives a crossing that is on neither.

Their fix is to store three numbers per grid point: the distance to the surface along +x, +y and +z. Marching Cubes only ever asks where the surface crosses a grid edge, and a directed distance answers exactly that. The crossings become exact, at three times the storage.

If you can evaluate the field anywhere, you do not need the data structure: bisecting along each edge gives the same exact crossing. That is what the demo above does.

What it costs

  • Normals. The method needs the surface normal at every edge intersection, from the field's gradient. Estimating it from the sampled grid works and is the weaker option; the paper shows both.
  • More triangles. At the same resolution EMC always emits more than Marching Cubes, since every feature cell gains a vertex and a fan.
  • A second pass with connectivity. The edge flip needs a real mesh with adjacency, not a triangle soup, and it crosses cell borders. That matters if you extract in parallel, on the GPU, or per chunk: see Marching Cubes on the GPU - Count, Scan, Emit for a pipeline this would complicate.
  • Two thresholds. They are intuitive and they are still thresholds. With noisy normals the paper recommends keeping the corner threshold high, because calling an edge a corner gives a badly placed vertex.
  • One feature per cell. Two creases in one cell get one vertex between them.

Extended Marching Cubes or Dual Contouring?

Both read the same data and solve the same small least-squares problem. The differences are structural:

  • EMC decides, then acts. Cells are tested, and only those that pass get a vertex. Dual Contouring gives every surface cell a vertex from the same solve and never asks whether it is a feature.
  • EMC keeps the Marching Cubes tables. That includes their ambiguous cases. Dual Contouring has no case table and its own failure mode instead, when two sheets share a cell.
  • Level of detail. EMC lives on a uniform grid, like Marching Cubes. Dual Contouring's octree simplification is half its paper.
  • Triangle count. EMC adds to Marching Cubes' count; Dual Contouring emits one quad per crossed edge.

If you have a Marching Cubes mesher that works and some content with hard edges, EMC is the smaller change. If you are choosing from scratch, the comparison table in the Dual Contouring note is the place to start.

Things that tripped me up

  • A tight curve passes for a corner. Choose the disc in the demo. At the default grid a red vertex already appears on it, where the normal turns more than the threshold across a single cell; refine the grid and it goes. The threshold cannot tell a curve it is under-sampling from a crease.
  • The corner is not always in the cell that sees it. A corner can poke into a cell without its two sides crossing different edges of that cell. The neighbour then detects the feature and solves for a point outside itself. The demo's clamp toggle shows both choices: hold the vertex at the cell wall, or let it leave.
  • The paper does not say whether n* is normalised. The code above divides the cross product by its length, so that 0.7 means the same angle whichever pair is widest. That is a reading of the paper, not its text. The length is never zero when the test runs, since the pair already failed the sharpness test.
  • The 2D picture hides the edge flip. The paper is explicit that after the fans are inserted the triangle connectivity "does not reflect the presence of features". The new vertices are on the crease; the triangles still straddle it until the edges are flipped.

References

  • Kobbelt, L., Botsch, M., Schwanecke, U., & Seidel, H.-P. — Feature Sensitive Surface Extraction from Volume Data (SIGGRAPH 2001) · PDF from RWTH Aachen · doi:10.1145/383259.383265
  • Ju, T., Losasso, F., Schaefer, S., & Warren, J. — Dual Contouring of Hermite Data (SIGGRAPH 2002) · doi:10.1145/566570.566586. Section 2.2 describes itself against EMC.
  • Lorensen, W., & Cline, H. — Marching Cubes: A High Resolution 3D Surface Construction Algorithm (SIGGRAPH 1987) · doi:10.1145/37401.37422

Back to Notes · see also Marching Cubes · Dual Contouring · Normals for Implicit Surfaces