Repository navigation
[Bugs] ifc-converter: keep geometry whose normal is NaN, recomputing the normal - #1160
Conversation
keepValidTriangles dropped a vertex with a non-finite normal, and every triangle using it, though the position was fine: a NaN normal from a degenerate face cost real geometry. Only a non-finite position drops a vertex now; a kept vertex's non-finite normal becomes the area-weighted normal of the kept triangles around it (zero if they have no area).
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Aymericr
left a comment
There was a problem hiding this comment.
Maintainer cross-review: looks good, approve in substance (posting as a comment because GitHub won't let this account approve its own PR).
- Dropping only on non-finite positions and recomputing NaN normals from the kept, compacted triangles is the right split. Normals are shading, not shape.
- Area-weighted sum with a zero fallback for degenerate faces is sound, and the
|| 0avoids-0after rounding. - The test spies
GetVertexArrayto force NaN normals and checks triangle counts match a clean conversion plus finite, unit-length normals. That covers the regression well.
No UI change, so no screenshots needed. Owner: Bugs. Merge once quality and cli-smoke are green and Bugbot/Codex come back clean.
Aymericr
left a comment
There was a problem hiding this comment.
[Maintainer] Cross-review at 0a291e5
Approved at 0a291e5.
CI at this head: quality ✅, cli-smoke ✅, Changed paths ✅, Cursor Bugbot ✅ (no findings). mergeable_state is clean.
Geometry robustness review (packages/ifc-converter/src/geometry-validity.ts)
- Only a non-finite position drops a vertex now (
finiteTriple(mesh.positions, …), :78). A non-finite normal no longer discards otherwise valid geometry. - Recompute (
recomputeNonFiniteNormals, :135-163): it runs on the compacted buffers, so indices and positions agree, and it only touches vertices whose normal is non-finite. Valid input normals are kept as they are. It's an area-weighted sum of face cross products, which is standard. - Degenerate / zero-area faces: a collinear triangle contributes a zero cross product. A vertex whose faces are all zero-area gets length 0, and the normal falls back to
[0,0,0](:161). The|| 0also turnsNaNand-0into 0, so if the cross product overflows (finite but huge coordinates, givingInfinity/Infinity) the result is still 0 rather than NaN. I checked these cases numerically: right triangle →(0,0,1), collinear → zeros, 1e200 coordinates → zeros, and no NaN is left in any case. - Isolated vertices: after compaction, only vertices referenced by a kept triangle are emitted (:86-120), so a vertex with no faces can't reach the recompute.
- Normalization: the sum is divided by its
Math.hypotand rounded to 3 decimals, which matches the existing normal precision, so the length is within ±0.001 of 1. - Test: it spies
GetVertexArrayto give every vertex a NaN normal, asserts the same per-mesh triangle counts as a clean conversion, zero skipped vertices, and finite normals with length ≤ 1.001. It fails without the fix (1,002 vertices dropped).
Non-blocking notes
- Zero-normal fallback (:161). A
[0,0,0]normal is NaN-free, and in practice it only occurs on vertices whose faces all have zero area, which rasterize nothing. Shaders thatnormalize()it are undefined, though. A fixed unit fallback such as(0,0,1)would be safer if one of those vertices is ever shared with visible faces. The test could also assert length > 0 for non-degenerate meshes. - Performance on large meshes (:141-157). The
Maplookups plus the per-triangle[a,b,c],faceand[a,b,c]corner arrays allocate on the hot path. In a quick local benchmark (Node, 1M triangles, every normal NaN), this version took about 2.1 s. The same algorithm with aUint8Arraybroken-mask and aFloat64Array(vertexCount * 3)accumulator took about 0.28 s. That only matters in the worst case, since meshes with no bad normals exit after one O(V) scan, but it would help on mobile. - Reporting. Recomputed normals aren't counted anywhere in the report now. A
recomputedNormalscount would keep this visible in diagnostics without showing the user a notice.
No merge from me.
Only a non-finite position drops a vertex now. A NaN normal on a vertex that is kept gets recomputed from the faces of the surrounding kept triangles, or set to zero when those faces have no area. Until now, geometry with bad normals was dropped entirely.
Test:
imported-mesh-conversion.test.tsrunsconvertIfcToPascalWithReportwith web-ifc'sGetVertexArrayspied so every normal is NaN. It expects the same meshes and triangle counts as a clean conversion, with finite normals. Without the fix it fails with 1,002 vertices dropped. With the fix, all 103 converter tests pass.