Repository navigation
contour and slice of structured grids by flying edges (#316) - #401
Open
berkgeveci wants to merge 5 commits into
Open
berkgeveci wants to merge 5 commits into
berkgeveci wants to merge 5 commits into
Conversation
A structured grid of solid cells -- an image, a rectilinear or a curvilinear grid -- with a point field and positions on the points is contoured by flying edges, as VTK's vtkFlyingEdges3D: each point's edges along +x, +y and +z classified once per value (a byte a point), the points each row emits and the triangles each row of cells makes counted and scanned, then written once, the triangles' pass walking each row of cells with a running count for the four rows of points around it. No per-corner arrays, no per-cell arrays, no sort. Crossings are read through the same views, placed from the end first by position and named by the end they land on (_end_hit), as the general path places them, so the triangles, points and carried values are its own, bit for bit, up to their order. On an 11M-cell image at ten values (CPU, M1 Max) the peak above the input falls from 2.24 to 0.32 GB and the time from 259 to 219 ms; on Metal, 201 to 136 ms.
slice by a Plane through an image or a rectilinear grid of solid cells goes by flying edges over each row's stretch near the plane, as VTK's vtkFlyingEdgesPlaneCutter. Each (row, value) has a window of points, found from the plane's equation -- along a row the distance is affine in i on an image and monotone in the axis on a rectilinear grid -- widened by the largest change of distance along an edge and by rounding, so every crossed edge is inside; only the windows' flags are kept. The distance is computed where it is read, Plane.value of the point's position as implicit_values stores it, so the output is the general path's, bit for bit, up to its order. The flying-edges passes take the windows for contour too, whose windows are whole rows. A plane through a 432^3 image (80M cells, CPU, M1 Max) took 177 ms and 0.71 GB above the input; it takes 14 ms and 0.03 GB.
Hidden and refined cells (Ghosts.MASKED_CELLS, an AMR box's) make no triangles in the flying-edges contour and the plane cutter, and crossings only they use are dropped afterwards, as for cells with a NaN corner. The output is the general path's, which first extracts the other cells into an unstructured mesh, bit for bit, up to order, ghost marks included; contour no longer extracts them for a grid flying edges takes. A plane through a curvilinear grid takes the cutter too: every row, the distance computed where read rather than stored for every point. At 256^3 (CPU, M1 Max): a plane through an image with a tenth of its cells refined, 116 ms and 1.49 GB to 12 ms and 0.01 GB; through a curvilinear grid, 47 ms to 7 ms.
This was referenced Nov 9, 2026
Closed
A 2D structured grid (a sheet) takes the flying-edges points' passes, one layer of cells along z, through an (i, j) reader of its cells' views (_Sheet; _Solid for a solid grid), and two line passes, counting and writing, that walk each row of cells with the first number of its two rows of points. Each cell's lines are its crossings going round its sides through its view, paired as contour._level_lines pairs them -- each rising crossing with the next falling one, the values above on the line's right -- and a line whose two crossings land on one point is dropped; a cell none of whose sides is crossed is skipped from the points' codes. Hidden, refined and NaN cells are left out as in 3D. The lines, points and values are the general path's, bit for bit, up to order, orientation included. On a 4096^2 image (CPU, M1 Max): at ten values 0.18 GB above the input against 14.1, 252 ms against 251; at one value 0.02 GB against 2.01, 33 ms against 53.
6 tasks done
…arts on main: the flying-edges check stays in contour)
Collaborator
Author
|
Merged |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #316:
contourof structured grids by flying edges, solid grids' triangles and 2D grids' lines, andslice/slice_planeof structured grids by a plane cutter, hidden and refined cells (AMR boxes) left out in place.A structured grid of solid cells with a point field and positions on the points is now contoured by flying edges, as VTK's
vtkFlyingEdges3Dand Viskores' flying edges. The grid can be an image, a rectilinear grid or a curvilinear one. Crossings are counted along the grid's rows, numbered by a scan and written once. There are no per-corner arrays, no per-cell arrays and no sort. The new code is inalgorithms/_flying_edges.py. The oldtack.deprecatedimplementation (removed in 55009d3) served as a reference for the passes; this is written afresh on the dataset API.The same output as the general path
The triangles, points and carried values are the general path's, bit for bit, up to their order:
get_ijk, as its cells read them);_end_hit);The passes
Rows that emit nothing are skipped. NaNs are flagged in the first pass, and a crossing whose cells all hold a NaN is compacted away, as the general path never makes it.
Measurements
M1 Max. Memory is the peak above the input (tracemalloc, CPU only).
The issue gives
vtkFlyingEdges3Dat 0.39 GB on the first case, and 0.270 s on the 432³ image at two values.This is not VTK's full algorithm:
The time is already on par with VTK, and trimming saves little for surfaces that cross whole rows. On Metal the passes that walk rows dominate (107 of the 139 ms at 432³), so splitting rows into shorter pieces would help more there.
The plane cutter
The second commit adds the plane cutter.
sliceby aPlanethrough an image or a rectilinear grid of solid cells (and soslice_plane) goes by flying edges over each row's stretch near the plane only, as VTK'svtkFlyingEdgesPlaneCutter:Plane.valueof the point's position, the expressionimplicit_valuesstores, so the bits are the same and nothing is kept for the other points.contouras well, whose windows are whole rows.slice_planeof a 432³ image (80M cells), CPUcontourof the plane's distanceThe issue gives VTK's cutter at 8 ms; what remains here is mostly the launches of ten small kernels.
The cutter is checked against the general
slice_plane, bit for bit, up to order, in f32 and f64:Other implicit functions, 2D grids and unmerged points go the old way.
Hidden cells and curvilinear planes
The third commit adds two things.
Hidden and refined cells (
Ghosts.MASKED_CELLS, every AMR box with a finer level) are left out in place by both paths:Before,
contourcalledvisible(), which extracts the other cells into an unstructured mesh, so a box with one refined cell lost flying edges altogether. The output is still the general path's (which extracts first), bit for bit, up to order, the cells' ghost marks included.A plane through a curvilinear grid takes the cutter too. Its points may lie anywhere, so every row is visited, but the distance is still computed where read rather than stored for every point.
2D grids
The fourth commit adds 2D grids. A sheet's contour lines go by flying edges too.
_level_linespairs them: each rising crossing with the next falling one, so the values above lie on the line's right.The lines, points and values are the general path's, bit for bit, up to order, orientation included.
The general path derives and sorts the mesh's sides and keeps every side's crossing per value, which is where its memory goes.
Checked
test_structured_contour.py(22 tests, quick tier), flying edges and the cutter against the general path, bit for bit: hidden, refined and duplicate cells on images, rectilinear and curvilinear grids, and 2D grids' lines (values on points, NaNs, hidden cells), as well as:🤖 Generated with Claude Code