Repository navigation
fix(LBIE): run quality-improvement passes in grid units; optimization never makes the mesh worse - #787
Merged
Conversation
… never makes the mesh worse
`cvc hexahedralize --improve optimization -q 1` on the bunny (`cvc sdf -d 16,16,16
-a v2`) threw a vertex to (0.24, 0.32, -0.75), far outside the volume, and took the
worst corner condition number from 191 to 14905.
Units. Mesher::qualityImprove ran its passes after do_duallib_extract had mapped the
vertices to world coordinates, but the passes are written for grid units:
Octree::optimization's first line-search step is 0.5, half a grid cell, and geometric
flow on tetrahedra and hexahedra skips elements with a volume under 1e-5. In the
bunny's volume a cell is 0.01, so the first step was 50 cells, and geometric flow
moved only 178 of its 592 vertices. A smaller first step alone does not fix it:
getGrad mixes length and length^3 terms, so the search direction depends on the
units too. qualityImprove now maps a mesh that came from the octree back onto the
octree's grid for its passes and back to world coordinates afterwards; a vertex no
pass moved keeps its exact coordinates. A mesh with no octree behind it
(LBIE::quality_improve, cvc::geometry::quality_improve) gets a grid whose cell is its
median edge, so it is improved the same way in any units: a sphere's hex mesh used to
end with a worst corner of 28 in its [-1,1] box, 36 scaled up 64 times and 293 (no
change) scaled down 64 times; now 15.3 at every scale.
Line search. optimization moves the worst corner's vertex to the first position where
that corner is no worse than cond_max, the mesh's worst. It did not look at the other
corners the vertex belongs to, so a move could flatten a neighbour and a pass could
end worse than it started (the bunny's grid pass took 169 to 191). A position must now
leave every corner of the hexahedra around the vertex no worse than cond_max, so the
worst corner never gets worse. On the octree's grid a position off the grid does not
qualify either: the bunny's volume ends at the bunny's bounding box, and the grid pass
pushed a vertex on that face 0.3 of a cell out of the volume. When no step
qualifies the vertex now stays exactly where it was.
Worst corner condition number / vertices outside the volume, -q 0 -> -q 1:
- bunny SDF: 191 / 0 -> 14905 / 1 (before) 32.6 / 0 -> 32.6 / 0 (after)
- sphere r=1.2: 14.2 / 0 -> 14.2 / 0 14.2 / 0 -> 13.7 / 0
- sphere r=1.2 in a box 0.02 across:
15.3 / 0 -> 15.3 / 0 12.6 / 0 -> 10.7 / 0
- spheres r=0.5, 1.5 and the CLI tests' tetrahedron SDF: unchanged or better; the
tetrahedron's mesh has flat corners, which still stop optimization.
transfix
force-pushed
the
fix/lbie-hex-optimization-units
branch
from
October 24, 2026 06:03
4ecb10c to
21184be
Compare
transfix
added a commit
that referenced
this pull request
Oct 26, 2026
…can't wreck tets, and real interval meshes (#808) * fix(LBIE): tetrahedral meshes fit the surface in any units, without overlapping tets `fem_gallery --mesh stanford.bunny --problem mesh --dim 64` gave 7,440 tets of one size whose boundary was an axis-aligned staircase, while the hexahedral mesh of the same SDF was adaptive and fitted. Units. Octree::interpRect3Dpts_x/y/z placed each edge crossing at span * grid index, span being the volume's voxel size, but the QEF solution built from the crossings is checked against its cell in grid units (Octree::get_solution), and do_duallib_extract maps every vertex from grid units to the box. Wherever a voxel is not 1 across, every QEF solution therefore "left its cell" and was replaced by the cell's centre, and the QEF error that drives traverse_qef's refinement shrank by span^2, so nothing refined. A tet mesh's boundary vertices are those QEF points: the bunny's SDF (voxels 0.003 across) came out as uniform cubes. A volume with 1-unit voxels (vh4.rawiv) always got a fitted, adaptive boundary. Hexahedra hid it: Octree::get_vtx_new walks each boundary vertex onto the surface afterwards. The crossings are now in grid units, and the normals that weight the QEF are the grid's gradient direction. Overlaps. With real QEF points the adaptive tet mesh overlaps itself where boundary cells of two sizes meet: tetrahedralize() covers each piece of the coarse cell's face twice, once from each cell's QEF point, and the tets overlap unless the segment between the points passes through that piece (it always does for cell centres, which is why the interior can be adaptive). A sphere's mesh covered 5% of its inside twice and had 13% too much volume. traverse_qef now refines a tet mesh in two passes: the first finds the finest level the error tolerance wants anywhere on the surface, the second refines the whole surface to it. It also drops the r < 60 condition for tets, which kept refinement inside a sphere of 60 voxels around the centre of the volume the code was written for. Boundary solutions. A QEF solution is a weighted mean of crossings, so it lies in the closed cell, but on the cell's boundary where every crossing is on one face. Such a solution went to the cell's centre, half a cell off the surface; now only NaN does, and the rest is clamped inside the cell: by a fifth of a cell for tets (a corner on a face makes slivers: a sphere's minimum dihedral angle goes 0.5 -> 11 degrees, boundary within 0.3 voxels), by a hundredth otherwise. Isovalue ties. A sample exactly at the isovalue is on neither side, and the extraction disagreed with itself about it: is_skipcell() said a coarse cell touching the surface only there was not crossed, so it was never refined, while is_intersect() let its edges cross, so it got a vertex at its centre, 3.4 voxels off a sphere through grid points. Integer CT data at an integer isovalue is full of such samples. collapse() now moves the isovalue off the samples, towards the inside. Quadrilaterals and hexahedra keep an octree that stops at oct_depth-2 and start each vertex at its cell's centre before get_vtx_new walks it onto the surface. That is what every volume whose voxels are not 1 across gave them before, so the bunny's hex mesh is unchanged; a refined octree made the hex templates collapse elements, and starting from the QEF point gave worse hexes (a sphere's worst corner condition 35 instead of 15). On vh4.rawiv, whose voxels are 1 across: iso 45 went from 952,750 hexes, about half with repeated corners, to 183,708 with none; iso 80 has 12,798, none repeated. Tets (minimum dihedral angle in degrees): - bunny SDF, --dim 64: 7,440 staircase (26.6) -> 160,090 fitted (0.04) - bunny SDF, --dim 128: 59,460 staircase -> 739,289 fitted (Poisson 8.5 s) - vh4.rawiv iso 45: 347,680 adaptive (0.001) -> 1,286,317 (0.004) - sphere r=0.6 at 33^3, box 1 or 0.01: 684 tets, boundary 2.97 voxels off -> 23,292 tets, boundary within 0.27 voxels, volume 1% over the sphere's The fitted boundary has slivers; geometric flow (next commit) is the remedy. Tests: TetraBoundaryFitsTheSurfaceInAnyUnits (same samples in three boxes give one mesh, on the surface, with the sphere's volume) and TetraMeshesAnIsovalueTheSamplesHit (a sphere through grid points, and unsigned-char data at an integer isovalue) fail before this; AdaptiveHexahedraHaveEightDistinctCorners now also runs in a box whose voxels are 1 across. * fix(LBIE): geometric flow on tetrahedra never makes the worst tet worse `fem_gallery --mesh stanford.bunny --problem mesh --dim 64 --improve geoflow` took the minimum dihedral angle from 26.6 to 0.75 degrees and threw a boundary vertex far outside the bunny. Octree::geometric_flow_tet had three faults; units were not one of them (#787 already runs the pass on the octree's grid): - It read each tet's corners in an order that made volume_tet() negative for every tet AddTetra stores, so every tet failed the "volume < 1e-5" skip and no interior vertex ever moved. Only boundary vertices did. - A boundary vertex moves in the tangent plane, x += dt * (p - (p.n) n), with n taken as a unit normal. n is the gradient of the volume's values, so where it is steeper than 1 the move overshoots along n and the overshoot grows every round: a CT's gradient is tens of units per voxel, and a sphere's in a box 0.01 across sent every vertex to infinity. n is now normalized. - Nothing checked the tets around a vertex, so a move could fold them. A move is now taken only if every tet around the vertex keeps a positive volume and none has a smaller dihedral angle than the worst of them had, so a pass never makes the mesh's worst tet worse. Each tet's worst dihedral cosine is cached and updated as its corners move. The vertex-to-tet table is no longer capped at 50 tets (a coarse cell's centre beside fine cells can be in about a hundred), and a boundary vertex with no boundary face is skipped without the stray "s<n>" printf. Minimum (mean) dihedral angle in degrees, NO_IMPROVE -> GEO_FLOW 1 iteration: - sphere r=0.6 at 17^3: 19.7 (49.8) -> 32.3 (50.6), in a box 8 across with values x1 and in a box 0.01 across with values x100 alike (before: NaN everywhere for the latter) - sphere r=0.6 at 33^3: 10.8 (46.6) -> 21.4 (48.3), 0 inverted, boundary within 0.27 voxels; 1 s for 23,292 tets - bunny SDF, --dim 64: 0.04 (46.3) -> 0.20 (47.7), 160,090 tets, nothing thrown out Test: TetraGeometricFlowNeverMakesTheWorstTetWorse. Before this the same checks on a 33^3 sphere found 357 inverted tets in the unit box and non-finite vertices in the box 0.01 across. * fix(LBIE): tetrahedralize2 meshes the layer between two isovalues, in either order `fem_gallery --mesh stanford.bunny --problem mesh --iso2 -0.012` gave a shell full of sliver fans and holes, and vh4.rawiv's `--iso 45 --iso2 80` layer was sparse noise with a minimum dihedral angle of 0.0005 degrees. Routing. cvc::tetrahedralize2 asks for LBIE's TETRA2 mesh with dual contouring, but do_mesh_geometry turned VOLUME_TET back into TETRA, so the layer went through the one-surface Octree::tetrahedralize on the interval's octree, which knows only the first isovalue. A dual-contoured VOLUME_TET is now TETRA2. Octree::tetrahedralize_interval, which TETRA2 then reaches, is no better: it chooses which isosurface an edge crosses by its cell rather than by the edge, gives an edge that crosses both isosurfaces a pyramid whose apex lies outside the layer, and makes a new grid vertex for every pyramid, so neighbouring tets do not share corners. Mesher::do_duallib_extract instead meshes a TETRA2 layer lo <= v <= hi as the solid min(v - lo, hi - v) >= 0, whose boundary is both isosurfaces, with the one-surface tetrahedralizer the previous commits fixed. The isovalues may come in either order (LBIE wanted the upper one first, and gave nothing otherwise), and a layer thinner than a voxel can break up. The one-isovalue tetrahedralize2(vol, iso) asked for the layer up to LBIE's default inner isovalue, 9.5001, from the data LBIE was first written for, which an SDF never reaches: it returned an empty mesh. It now meshes the solid above iso, the region tetrahedralize() meshes, and GeometryTest.Tetrahedralize2MeshExtraction checks that the two agree. Layer of a sphere, radius 0.6 +- 0.15 at 33^3 (exact volume 1.385): - before: 8,124 tets, volume 0.19, boundary 3.7 voxels off, corners off the layer; 0 tets with the lower isovalue first - after: 48,984 tets either way, volume 1.383, boundary within 0.33 voxels, every corner in the layer, minimum dihedral angle 7.7 degrees, 0 inverted - bunny, --iso2 -0.012 at --dim 64: 45,443 tets with fans and holes -> 232,166 in an even shell, minimum dihedral angle 6.6 -> 0.30 (0.34 after GEO_FLOW) - vh4.rawiv, --iso 45 --iso2 80: a solid layer of 1,948,887 tets with the lungs and the bone as cavities Test: Tetrahedralize2MeshesTheLayerBetweenTwoIsosurfaces (both orders, two boxes) fails before this. docs/GEOMETRY_API.md and the libigl roadmap's P2.2 row are updated. * fix(LBIE): the QEF error that drives refinement sums all three axes Octree::compute_qef and compute_qef_interval compute each cell's QEF residual, which get_err() hands to traverse_qef as the refinement error, as a sum over the three axes: qef=0; for (i=0;i<3;i++) qef = sigma_ni_2_pi_2[i] - (sigma_ni_2_pi[i]*sigma_ni_2_pi[i]) / sigma_ni_2[i]; All six live copies assign instead of adding, so only the z term counted. Where no crossing's normal has a z component the term is 0/0, so the error is NaN and the cell never refines: the side of a cylinder along z stayed coarse while the same cylinder along x or y refined. They now add, through qef_axis_residual(), which gives 0 for an axis no normal has a component on (it constrains nothing) instead of NaN. A capped cylinder r=0.4 at 33^3, triangles / tets: - along x or y: 1,436 / 13,940 before and after - along z: 156 / 540 before, 1,436 / 13,940 after Tet meshes of the gallery's inputs do not change: their surfaces already reach the finest level (bunny SDF at 64^3: 160,090 tets; vh4.rawiv iso 45: 1,286,317). DUALLIB surfaces refine where the x and y terms say so: `cvc iso` on the bunny's 64^3 v2 SDF goes from 19,088 to 28,183 triangles, with the vertices as close to the bunny as before (median 0.23 voxels, 95th percentile 0.55). Hexahedra and quadrilaterals do not use this error. Test: RefinementIsTheSameAlongEveryAxis, which fails before this. * test(LBIE): the tet improvement tests fit a Debug CI build again; GEO_FLOW caches tet volumes package-linux / libcvc-debug failed on this PR: GeometryTest.QualityImproveTetrahedralMesh timed out (300 s), and GeometryTest.TetrahedralizeWithImprovementMethods took 178 s (21 s on other branches). Both run several GEO_FLOW passes over a bunny tet mesh, and that mesh grew with the fitted boundary: 7,440 -> ~290k tets from a 64^3 SDF, and ~63k from 32^3. They only check that each method keeps the topology and leaves finite points. - QualityImproveTetrahedralMesh meshes a 32^3 SDF instead of 64^3: 5.4 s in Release here, down from 31 s. - TetrahedralizeWithImprovementMethods meshes 16^3 instead of 32^3, which LBIE grids as 17^3 instead of 33^3 (any dim from 17 to 32 becomes 33): 11,825 tets, 1.7 s down from 11.7 s. - geometric_flow_tet keeps each tet's volume next to its cached dihedral cosine, from the same determinant the move check already computes, instead of recomputing every incident tet's volume with volume_tet() (Heron's formula, four square roots) in every round: a pass over 63k tets takes 1.3 s instead of 1.8 s. cvcgl_media_audio, which failed in the same job, passes on other branches built on the same master; its deep-graph program has to finish inside one 0.1 s drain, and here it ran while the timed-out test held a core for three 300 s attempts.
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.
Follow-up to #777 (merged as 391b46b). The "#777" columns below are master at 391b46b.
Why
#777 made
Octree::optimization's line search actually run. That exposed the follow-up #777 listed:cvc hexahedralize --improve optimization -q 1on the bunny (cvc bunny,cvc sdf -d 16,16,16 -a v2) throws a vertex to (0.24, 0.32, -0.75), far outside the volume, and takes the worst corner condition number from 191 to 14905.The
-qpasses ran in world units, but the improvement code is written for grid units.LBIE::Mesher::qualityImproveruns afterdo_duallib_extracthas mapped the vertices to world coordinates. Two parts of the improvement code depend on the units:optimization's first step is 0.5, half a grid cell. A bunny cell is 0.01, so 0.5 is 50 cells.Scaling the first step alone does not fix this.
getGradmixes length and length³ terms, so the search direction depends on the units as well. With a corner-relative step, the standalone bunny result still varied from 68 to 179 between scales.A grid-unit pass could still make the mesh worse. The line search checked only the corner it was fixing. A move could flatten a neighbouring corner, so a pass sometimes ended worse than it started: the bunny's grid pass went from 169 to 191. With grid units alone, a sphere in a small box went from 15.3 to 19.4 at
-q 1.What
Mesher::qualityImprove(LBIE_Mesher.cpp): the passes run in grid units.LBIE::quality_improve,cvc::geometry::quality_improve, pycvc) gets a grid whose cell is its median edge length. It is now improved the same way at any scale.NO_IMPROVE.Octree::optimization's line search (octree.cpp):cond_max. The worst corner can no longer get worse during a pass.The incident hexahedra are collected once per move, so each trial step checks only a few hexahedra. LBIE sources stay
DisableFormat. The test files are clang-format clean.Numbers
Worst corner condition number / vertices outside the volume. The volumes are the bunny SDF (
-d 16,16,16 -a v2), the CLI tests' tetrahedron SDF, and spheres frommake_sphere_volume(17³, [-1,1]³). "q0" is the pass inside extraction; "q1" adds one-qpass.Nothing is outside the volume in any row of this PR.
LBIE::quality_improve(no octree) on the extracted, unimproved hex mesh, scaled by 1/64, 1 and 64:Approaches I measured and dropped:
Tests
CvcCliTest.Regression_HexahedralizeOptimizationKeepsVerticesInTheVolume, now also run on the bunnyLbieMesherTest.OptimizationImprovesASmallVolumesMeshPassByPass(q1 must improve on q0, q0 must not be worse than no improvement, nothing outside)-qpass did nothingLbieMesherTest.QualityImproveGivesTheSameMeshInAnyUnits(×1/64 and ×64 must match ×1)For the A/B I ran the new test exes next to #777's
cvc.dll/cvc.exe. The tests also catch a grid-units-only version: the small-box test fails there (19.4 > 15.3).Local runs on Windows/MSVC 14.44, Release, shared:
lbie_mesher_test: 45/45 pass.algorithm_test: 10/10 pass.geometry_test: 111 pass, 8 skipped (the same 8 skip on fix(LBIE): hex optimization copied an uninitialized point; refinement reused vertex 0 #777).cvc_cli_testwithoutIntegration_*: 163 pass, 1 skipped (PlantLogWritesARun, unrelated).Found, not fixed
optimizationinto a no-op for the whole mesh, sincecond_maxstays +inf. The tetrahedron SDF has 6 such corners.testHexacannot see an inverted corner. It takes the square root of the Gram determinant, so the sign is lost. Corners whose orientation is opposite to the mesh's majority exist after extraction (81 of the bunny's 2808 corners), and optimization adds a few.NO_IMPROVEhex meshes, by up to a cell. The octree usesspansomewhere in vertex placement. I did not trace it.getGradmixes length and length³ terms. Here it only ever sees grid units, so it no longer matters, but the gradient itself is not scale-invariant.