Exercise: measuring surfaces
Before this unit: Marching cubes (previous unit); Day 1: Images, filters and frequencies.
See also: Day 12: Thresholding; Day 14: Labelling; Day 31: Level sets.
Goal
By the end of this exercise you should be able to:
- compute the area and volume of a small triangle mesh, and compare them with voxel counts and voxel faces,
- explain why voxel faces overestimate area at every resolution, and why marching cubes converges,
- use the Euler characteristic and boundary edges to check a mesh’s topology,
- reason about the choice of c, smoothing, anisotropic spacing, surfaces of binary masks, and mesh size.
Do the pen-and-paper part first, then the demo. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
Background
| Idea | What it means |
|---|---|
| Triangle area | \tfrac12\lVert(\mathbf b - \mathbf a)\times(\mathbf c - \mathbf a)\rVert |
| Enclosed volume | \sum \tfrac16\,\mathbf a\cdot(\mathbf b\times\mathbf c) over the triangles (closed, outward-oriented mesh) |
| Voxel estimates | volume = voxels inside × voxel volume; area = exposed voxel faces × face area |
| Euler characteristic | \chi = V - E + F; one closed surface with g holes: 2 - 2g; components add |
| Boundary edge | an edge with a triangle on one side only (a crack, or where the surface leaves the volume) |
| Half-way level | a symmetric blur of a step from a to b passes (a+b)/2 at the true edge |
By hand
B1. One bright voxel. A voxel of value 1 has six face neighbours of value 0 (and all other voxels are 0); c = 0.5. (a) Where are the vertices of the marching-cubes surface? What shape is it, with how many triangles? (b) Compute its area. (Each triangle is equilateral; what is its side?) (c) Compute its volume, e.g. as 8 tetrahedra with a corner at the centre. (d) Compare with the voxel count (volume) and the exposed voxel faces (area).
B2. Same voxel. For which c does the octahedron have volume 1, the volume of the voxel? (The vertex on an edge from 1 to 0 lies at distance 1 - c from the centre.) What is its area then?
B3. Pixel edges in 2D. Take all pixels whose centres lie within distance 3 of the centre of one pixel (a digital disk of radius 3). (a) How many pixels are there? Compare with \pi \cdot 3^2. (b) Count the exposed pixel edges (each of length 1) and compare with 2\pi \cdot 3. (c) Show that for this convex shape the pixel-edge perimeter equals the perimeter of its bounding box. What does that give for a large disk of radius r, compared with 2\pi r?
B4. Euler characteristic. Compute \chi and say what it tells you: (a) the octahedron of B1; (b) the surface of a cube made of 12 triangles (8 vertices, 18 edges); (c) the octahedron with one triangle removed (how many boundary edges?); (d) a mesh of two separate spheres; (e) a torus made of a 6 \times 4 grid of quads, each split into two triangles.
B5. Choosing c. (a) A nucleus has grey value 200, the background 20, and the image is blurred symmetrically. Which c puts the surface at the true boundary of a large, flat part of the nucleus? (b) The nucleus is small (radius 2 voxels) and the blur is \sigma = 1.5 voxels. Will the surface at that c be too large or too small? Why? (c) Measuring the volume at c = 0.45, 0.5, 0.55 (normalised values) gives 2163, 2115, 2068 for one object and 1750, 1525, 1318 for another. Which measurement do you trust more, and why?
B6. Spacing. A spherical nucleus of radius 5 µm is imaged with \Delta x = \Delta y = 0.2 µm and \Delta z = 0.5 µm. (a) The vertex positions are computed with \Delta z = 0.2 µm by mistake. What shape do you get, and what are its volume and area? (The surface area of a flattened spheroid with semi-axes 5, 5, 2 is 200.0 µm².) Compare with the true sphere. (b) The positions are correct but the gradient normals ignore \Delta z. What is wrong now, and what is still right?
B7. Mesh size. A surface of area 10^6 voxel² is extracted (about 3 triangles per voxel², 2 triangles per vertex). (a) Estimate the number of triangles and vertices. (b) Each coordinate and each index takes 4 bytes. How much memory does the mesh need with shared vertices, and as a triangle soup (3 separate vertices per triangle)? (c) Vertex clustering with a cell of 3 voxels reduces the triangles about 10-fold on smooth surfaces. Why can it change the topology?
B8. Binary masks. A large flat surface tilted slightly against the voxel grid is extracted from a 0/1 mask at c = 0.5. (a) Where do all the vertices lie on their edges? What does the surface look like? (b) Why does its area stay too large however fine the voxels are? (c) Name two remedies.
B1. (a) The 6 edges from the voxel to its face neighbours are crossed at t = 0.5: vertices at distance 0.5 from the centre along \pm x, \pm y, \pm z. They form an octahedron: 8 triangles. (b) Each side joins e.g. (0.5, 0, 0) and (0, 0.5, 0): length \sqrt{0.5} = 0.707. An equilateral triangle of side s has area \tfrac{\sqrt3}{4}s^2 = \tfrac{\sqrt3}{4}\cdot 0.5 = 0.2165; times 8: \sqrt3 = 1.732. (c) Each of the 8 tetrahedra (centre plus one face) has three perpendicular edges of length 0.5: volume \tfrac16 \cdot 0.5^3 = \tfrac1{48}; total \tfrac{8}{48} = \tfrac16 = 0.167. (d) Voxel count: volume 1; voxel faces: area 6. The octahedron is much smaller (a single voxel is all boundary); for large objects the volume estimates agree.
B2. With vertices at distance r = 1 - c: volume 8 \cdot \tfrac16 r^3 = \tfrac43 r^3 = 1, so r = (3/4)^{1/3} = 0.909 and c = 0.091. Area 8 \cdot \tfrac{\sqrt3}{4}(r\sqrt2)^2 = 4\sqrt3\,r^2 = 5.72, still less than the 6 faces.
B3. (a) 29 pixels (rows of 1, 5, 5, 7, 5, 5, 1 pixels, i.e. offsets with x^2 + y^2 \le 9), against \pi \cdot 9 = 28.27. (b) 28 exposed edges, against 2\pi \cdot 3 = 18.85: 1.49 times too long. (c) Walking around a convex pixel shape, the boundary moves right as often as the bounding box is wide, and so on: the horizontal steps add up to twice its width, the vertical ones to twice its height. Here 2(7 + 7) = 28. For a large disk the box is 2r \times 2r: perimeter 8r, against 2\pi r, a factor 4/\pi = 1.27, at any resolution.
B4. (a) 6 - 12 + 8 = 2: one closed surface without holes. (b) 8 - 18 + 12 = 2: the same. (c) 6 - 12 + 7 = 1, with 3 boundary edges: the surface has a hole (a crack). (d) 2 + 2 = 4: two closed surfaces. (e) V = 24, E = 3 \cdot 24 = 72, F = 48: \chi = 0, one hole (a torus).
B5. (a) c = (20 + 200)/2 = 110. (b) Too small. At the boundary of a small ball the blur averages over a neighbourhood that lies mostly outside (the boundary curves away), so the value there is below 110, and the level 110 lies further inside: the partial-volume effect. (c) The first: changing c by ±0.05 changes its volume by about ±2 %, so the result hardly depends on c. The second changes by about ±14 %: its volume is mostly a choice of c (a heavily blurred or small object).
B6. (a) The z coordinates shrink by 0.2/0.5 = 0.4: a flattened spheroid with semi-axes 5, 5, 2 µm. Volume \tfrac43\pi \cdot 5 \cdot 5 \cdot 2 = 209.4 µm³ instead of 523.6 (exactly 0.4 times), area 200.0 µm² instead of 4\pi \cdot 25 = 314.2 (0.64 times). (b) The normals are tilted (their z component is 2.5 times too large before normalising), so the shading is wrong; the vertex positions, area and volume are still right.
B7. (a) About 3 \times 10^6 triangles and 1.5 \times 10^6 vertices. (b) Shared: vertices 1.5 \times 10^6 \cdot 3 \cdot 4 B = 18 MB, triangles 3 \times 10^6 \cdot 3 \cdot 4 B = 36 MB, together 54 MB. Soup: 3 \times 10^6 \cdot 3 \cdot 12 B = 108 MB, twice as much (and the connectivity is lost). (c) It merges all vertices in a cell, regardless of the surface: two sheets of the surface that pass through the same cell (a thin plate, two objects close together) are glued together, and small holes or thin parts collapse. The result can be non-manifold, and \chi can change.
B8. (a) Every crossed edge goes from 1 to 0, so t = 0.5: every vertex at an edge midpoint. The surface is a staircase of terraces following the voxels, with flat steps. (b) As with voxel faces, the steps get smaller with finer voxels but there are more of them; the excess area per unit of true area stays (in the demo about 8 % for a sphere). (c) Blur the mask slightly (σ ≈ 1 voxel) before extraction, or compute a signed distance function from the mask and extract its zero level; or smooth the mesh afterwards (which shrinks it).
Math questions
B9. (math) Derive V = \sum\tfrac16\,\mathbf a\cdot(\mathbf b\times\mathbf c) from the divergence theorem with \mathbf F = \mathbf x/3, and show that it does not depend on the choice of origin when the mesh is closed.
B10. (math) Show that replacing a surface patch with normal \mathbf n by voxel faces multiplies its area by |n_x| + |n_y| + |n_z|, and that the average of |n_x| over the unit sphere is \tfrac12. Conclude the factor \tfrac32 for a sphere, and derive 4/\pi in 2D.
B11. (math) (a) For a closed triangle mesh show 2E = 3F and hence F = 2(V - \chi). (b) Show that a torus built from an m \times n grid of quads (each split into 2 triangles, wrapping around in both directions) has \chi = 0.
B12. (math) (a) Show that a step from a to b, blurred by a symmetric kernel, passes (a + b)/2 at the step. (b) Use the coarea formula \frac{dV}{dc} = -\oiint_{\{I = c\}} \frac{dA}{\lVert\nabla I\rVert} to explain why a blurred edge makes the volume more sensitive to c.
B9. \nabla\cdot(\mathbf x/3) = 1, so V = \oiint \tfrac13\mathbf x\cdot\mathbf n\,dA. On a flat triangle with unit normal \mathbf n = (\mathbf b - \mathbf a)\times(\mathbf c - \mathbf a)/(2\,\text{area}), every point has \mathbf x\cdot\mathbf n = \mathbf a\cdot\mathbf n. So the triangle contributes \tfrac13\,\mathbf a\cdot\mathbf n\cdot\text{area} = \tfrac16\,\mathbf a\cdot\big((\mathbf b - \mathbf a)\times(\mathbf c - \mathbf a)\big) = \tfrac16\,\mathbf a\cdot(\mathbf b\times\mathbf c) (the terms with \mathbf a\times vanish in the triple product). Shifting all points by \mathbf t adds \tfrac13\,\mathbf t\cdot\oiint\mathbf n\,dA = 0, because the vector area of a closed surface is zero.
B10. The voxel faces approximating the patch are its projections onto the three coordinate planes, with areas |n_x|\,dA, |n_y|\,dA, |n_z|\,dA. On the unit sphere with n_x = \cos\theta: \langle|\cos\theta|\rangle = \frac{1}{4\pi}\int_0^{2\pi}\!\!\int_0^\pi |\cos\theta|\sin\theta\,d\theta\,d\phi = \frac{2\pi \cdot 1}{4\pi} = \tfrac12. Sum of three: \tfrac32. In 2D, (|\cos\phi| + |\sin\phi|) averaged over the circle: 2 \cdot \frac{1}{2\pi}\int_0^{2\pi}|\cos\phi|\,d\phi = 2\cdot\frac{4}{2\pi} = \frac4\pi.
B11. (a) Every triangle has 3 edges and every edge of a closed mesh belongs to exactly 2 triangles: 3F = 2E. Then \chi = V - \tfrac32F + F = V - \tfrac12F, i.e. F = 2(V - \chi). (b) V = mn grid points; each quad contributes 3 new edges (one horizontal, one vertical, one diagonal) when the grid wraps around: E = 3mn; F = 2mn. \chi = mn - 3mn + 2mn = 0.
B12. (a) I(x) = a + (b - a)\int_{-\infty}^{x}g(s)\,ds; with g symmetric, \int_{-\infty}^0 g = \tfrac12, so I(0) = (a + b)/2. (b) Raising c by dc moves each surface point inwards by dc/\lVert\nabla I\rVert, removing a shell of volume \oiint dc/\lVert\nabla I\rVert\,dA. Blur lowers \lVert\nabla I\rVert at the edge (the same step spread over a wider distance), so the same dc moves the surface further: a steeper volume-against-c curve.
Explore: voxel faces vs marching cubes
A disk (2D) and a ball (3D) of radius R pixels/voxels, as a grey image (each pixel ≈ the fraction of it inside, as a camera would record it) or a binary mask. Left: the disk with the pixels above ½, the pixel-edge boundary (red), the marching-squares contour (blue) and the true circle (dashed). Middle: relative errors against R on log–log axes, the dots at the current R. Right: the numbers.
R1. Preset voxel faces overestimate area (R = 6, grey). Read the area from voxel faces and from marching cubes, and the volume from voxel count and marching cubes. Compare with 4\pi R^2 and \tfrac43\pi R^3. Compare the 2D pixel-edge perimeter with B3.
R2. Preset marching cubes converges (R = 16). What happens to the four errors between R = 6 and R = 16? Which one does not improve, and why (B10)?
R3. Switch to binary mask and move R. What happens to the marching-cubes (and marching-squares) error now? Explain with B8.
R1. Voxel faces 674 against 4\pi \cdot 36 = 452.4 (+49.0 %); marching cubes 449.4 (−0.7 %). Volume: 903 voxels (−0.2 %) and marching cubes 889.8 (−1.7 %) against 904.8. In 2D, 48 pixel edges against 37.7 (+27.3 %) = the bounding box 2(12 + 12), as in B3; marching squares 37.7 (−0.1 %).
R2. At R = 16: voxel faces still +50.1 %; marching cubes area +0.07 %, voxel count +0.09 %, marching-cubes volume −0.23 %. The face count does not improve: the staircase factor |n_x| + |n_y| + |n_z| does not depend on the voxel size. The other errors fall roughly like 1/R to 1/R^2 (the volume curves jump about because of how the boundary voxels happen to fall).
R3. With a binary mask every vertex sits at an edge midpoint. The marching-cubes area stays about 7–8 % too large (+7.4 % at R = 6, +8.3 % at R = 16), and the marching-squares perimeter about 6 % (+5.6 % and +5.8 %): the terraces do not go away with resolution. The volumes are still good: the mask has the right amount of inside.
Explore: volume against c
The volume enclosed by the marching-cubes surface as a function of c (click in the plot or use the slider), for a ball of radius 8 voxels that was binary and then blurred with \sigma, or for the nuclei stack of volumes3d/ (values scaled to background 0, nuclei about 1; true total volume 79.2 µm³). Right: a slice and its contour at the current c.
I1. Preset a plateau in volume vs c (\sigma = 0.5). Read the volume at c = 0.5 and the change for c \pm 0.05. How close is it to \tfrac43\pi\cdot8^3?
I2. Preset heavy blur: c decides the volume (\sigma = 2.5). The same questions. Why is the volume at c = 0.5 so much smaller now (B5)?
I3. Switch to the nuclei stack. Read the volume at c = 0.3, 0.5 and 0.7 and compare with 79.2 µm³. Which c would you report, and how would you justify it?
I1. 2115.5 voxels³ against \tfrac43\pi\cdot512 = 2144.7 (−1.4 %); c \pm 0.05 changes it by about 2.3 %. A flat part of the curve: the measurement is robust.
I2. 1524.5 (−28.9 %), and c \pm 0.05 changes it by about 14 %. The blur (\sigma = 2.5 on a radius of 8) spreads the ball’s brightness outwards; at the true radius the value is well below 0.5, so the half-way surface lies inside (partial volume), and the gentle slope of the blurred edge makes the volume sensitive to c.
I3. About 94.4 µm³ at c = 0.3, 77.2 at c = 0.5 and 62.2 at c = 0.7 (blur 0.5 voxels). c = 0.5, half-way between background and nuclei, is closest to 79.2 (−2.5 %), as expected for objects much larger than the blur; report c together with the result, and the spread over a reasonable range of c.
Explore: binary masks and decimation
A tilted ellipsoid (semi-axes 16, 9, 11 voxels; true volume 6635 voxels³) extracted from a grey image, a binary mask, or the mask blurred with \sigma = 1. The cluster cell size decimates the mesh by vertex clustering. Face normals, so facets and terraces show; drag to rotate.
D1. Preset terraces from a binary mask. Compare the area and volume with the grey-level image and the blurred mask. Which inputs agree on the volume, and which on the area?
D2. Preset decimate 10×. How many triangles are left, and how much do area and volume change? Increase the cell size to 6: what happens to \chi and the non-manifold edges?
D3. With a cell size of 3, compare the decimated binary mask with the decimated grey image. Does decimation remove the terraces?
D1. Binary mask: area 1932.9, volume 6632.5. Grey image: area 1779.4, volume 6605.2. Blurred mask: area 1758.2, volume 6479.8. All three volumes are within 2.5 % of 6635; the binary mask’s area is 8.6 % larger than the grey image’s (terraces). Blurring the mask removes the terraces but shrinks the object (−2.3 % volume): blur rounds a convex object inwards.
D2. 508 triangles instead of 5332 (10.5 times fewer), area −2.1 %, volume −4.3 %: the coarse mesh cuts off the curved parts. At cell size 6: 138 triangles, area −7.2 %, volume −16.7 %, \chi = 4 and 3 non-manifold edges: vertices from different parts of the surface were merged into the same cell, and the mesh is no longer a single closed surface.
D3. With cell size 3 the binary mask gives 544 triangles and its area drops to 1759.5, close to the grey image’s decimated 1741.2: the merged vertices average over the steps, so the coarse mesh no longer follows the terraces. Decimation smooths at the scale of its cells (with the shrinkage seen in D2).
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in marchingcubes/src/surface_measure/exercise.rs. A mesh is a list of vertex positions and a list of triangles, three vertex numbers each, anticlockwise seen from outside. The download makes the meshes for you (with the marching cubes of the previous unit); here you measure them.
C1. triangle_area(a, b, c): \tfrac12\lVert(\mathbf b - \mathbf a)\times(\mathbf c - \mathbf a)\rVert; mesh_area(mesh): the sum over the triangles. The checks are B1(b) and the unit cube.
C2. mesh_volume(mesh): \sum \tfrac16\,\mathbf a\cdot(\mathbf b\times\mathbf c) over the triangles. The checks are B1(c), the unit cube, the cube moved away from the origin (B9), and the cube with its triangles turned inside out (-1).
C3. voxel_estimates(mask, spacing): the volume and the area from voxels:
volume = 0; area = 0
for every voxel in the mask:
volume = volume + Δx · Δy · Δz
for each of its 6 face neighbours (one step along x, y or z, both ways):
if the neighbour is outside the mask or outside the volume:
area = area + the area of that face # Δy·Δz across x, and so on
The checks are B1(d), and B3 (the digital disk of radius 3 as a slab one voxel thick: 29 voxels and 28 exposed pixel edges round its outline).
C4. topology(mesh): V, E, F, the number of boundary edges (edges with only one triangle) and \chi = V - E + F, counting every edge once. The checks are B4(a), (b), (c) and (e).
Then cargo run -p marchingcubes -- surface-measure and read what it prints. For balls of radius 4 to 16 voxels, recorded as grey values (each voxel holds the fraction of it inside), it compares the area from voxel faces, from the mesh of the grey image and from the mesh of the binary mask (voxels more than half inside), and the volume from the voxel count and from the mesh, all as errors against 4\pi R^2 and \tfrac43\pi R^3. Then: the volume of a binary ball of radius 8, blurred with σ = 0.5, 1.5 and 2.5 voxels, at c = 0.45, 0.5, 0.55; a ball of radius 10 imaged with \Delta z = 2.5 voxels, measured with and without the spacing; and V, E, F, boundary edges and \chi for one ball, two balls, a torus and a ball whose top is cut off by the edge of the volume.
C5. Read the first table. Which estimates get better as R grows, and which do not? Compare the voxel faces with B10 and the binary mask with B8.
C6. Read the volumes against c and the spacing lines. When does the choice of c matter, and why is the volume at c = 0.5 too small after a strong blur (B5)? By what factor does ignoring \Delta z change the volume, and the area?
C7. Explain each \chi and the boundary edges of the last table (B4).
The model solution is marchingcubes/src/surface_measure/solution.rs in the download.
C5. The voxel faces overestimate the area by 48 to 55 % at every radius: the staircase factor |n_x| + |n_y| + |n_z|, 1.5 on average, does not depend on the voxel size (B10). The mesh of the grey image converges: −1.4 % at R = 4, then −0.1 to +0.3 % from R = 8 on. The mesh of the binary mask stays 7.5 to 10.9 % too large at every radius: all its vertices sit at edge midpoints and it follows the terraces of the voxels (B8). The volumes are good either way: the voxel count is within 1.5 % from R = 6 on, the grey mesh within 2 % (−3.6 % at R = 4, −0.2 % at R = 16).
C6. For σ = 0.5 the volume at c = 0.5 is 2 142.9 against the true 2 144.7, and c \pm 0.05 changes it by only ±2.3 %. For σ = 1.5 it is 1 932.3 (±7.6 %), for σ = 2.5 only 1 549.1, 28 % too small (±14.1 %). A strong blur on a small ball spreads its brightness outwards: at the true boundary the value is below 0.5, so the level 0.5 lies inside the ball (partial volume), and the gentle slope makes the volume depend strongly on c. Ignoring \Delta z = 2.5 multiplies every z by 0.4, and so the volume by exactly 0.400 (1 650.2 against 4 125.5); the area falls from 1 256.1 to 794.4, a factor 0.63: between 0.4 and 1, depending on how the surface is oriented (B6).
C7. One ball: one closed surface without holes, \chi = 2 (672 − 2 010 + 1 340). Two balls: two such surfaces, \chi = 4. The torus: one closed surface with one hole, \chi = 0. The ball cut off by the edge of the volume: its surface is open where it leaves the volume, with 40 boundary edges round the hole, and \chi = 1, like the octahedron missing a triangle (B4c). Padding the volume with background would close it again. In all closed meshes E = \tfrac32F (B11).
Where this leads
Optional further reading: dual methods (surface nets, dual contouring) place one vertex per cell instead of per edge, need no case table, and handle label images naturally. Instead of extracting a surface, a volume can also be rendered directly (projections, volume rendering with a transfer function): the 3D-graphics day, which also shows how the meshes of this day are drawn on the screen.