Skip to content

Make EPA Polytope Expansion Deterministic - #670

Merged
SeanCurtis-TRI merged 4 commits into
flexible-collision-library:masterfrom
cohnt:deterministic-epa-border-edges
Oct 21, 2026
Merged

SeanCurtis-TRI merged 4 commits into
flexible-collision-library:masterfrom
cohnt:deterministic-epa-border-edges

Conversation

@cohnt

@cohnt cohnt commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

expandPolytope() iterates border_edges, an unordered_set of pointers, when adding the new edges and faces, so they are appended to the polytope in an order that depends on where the heap placed them. libccd breaks near-ties in its nearest-feature search by list order, so for identical inputs the witness points and normal of a penetrating convex-convex query can differ between processes (we hit this through Drake, as run-to-run differences in signed-distance gradients, leading to different solver outcomes).

This records the border edges in the order ComputeVisiblePatch() finds them, which is deterministic, and iterates that instead. On the mesh pair where we saw it, repeated queries with the heap perturbed went from two distinct results to one, and that one was already among the original's results. Runtime is unchanged (a first version that rescanned the polytope's edge list instead cost ~9%).


This change is Reviewable

cohnt and others added 3 commits October 12, 2026 21:13
expandPolytope() connected the new support vertex to the silhouette by
iterating border_edges, an std::unordered_set<ccd_pt_edge_t*>. Its
iteration order follows the pointer hashes, i.e. where the heap happened
to place the edges, and the new edges and faces are appended to the
polytope's lists in that order. libccd's nearest-feature search breaks
near-ties (ccdEq) by list order, so the face EPA expands next, and the
reported witness points and normal, could differ from one process to the
next for identical inputs (ASLR, or any change in earlier allocations).

Visit the border edges in the polytope's own edge-list order instead,
which is deterministic. The set is still used for membership. On a pair
of convex meshes where the original code returned two different normals
across 20,000 calls with the heap perturbed between calls, it now always
returns one of them; the penetration depth was identical in both.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014MVCAGt56vQmZCx1g6RjUZ
… list

The previous commit made expandPolytope() deterministic by walking the
polytope's whole edge list to put border_edges in a stable order, which
costs a pass over every edge per expansion. ComputeVisiblePatch() already
finds the border edges in a deterministic order (it follows the faces'
edge arrays, never iterating the sets), so record that order as they are
classified and iterate it directly. The extra cost is one push_back per
border edge. The new output parameter defaults to null, so existing
callers are unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014MVCAGt56vQmZCx1g6RjUZ
The EPA tests call ComputeVisiblePatchRecursive() directly, so give its new
border_edge_order parameter the same null default as ComputeVisiblePatch().

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014MVCAGt56vQmZCx1g6RjUZ

@cohnt cohnt left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cohnt reviewed 1 file and all commit messages.
Reviewable status: 0 of 1 files reviewed, all discussions resolved.

@sherm1

sherm1 commented Oct 14, 2026

Copy link
Copy Markdown
Member

I ran CI and assigned @SeanCurtis-TRI for review next week. LMK if you need it reviewed sooner.

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't provide a meaningless nullptr default value for the new parameter; the correctness (at least so far as determinancy goes) of the algorithm depends on it.

Instead, update the tests to respect the required argument.

@SeanCurtis-TRI reviewed 1 file and all commit messages, and made 4 comments.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on cohnt).


include/fcl/narrowphase/detail/convexity_based_algorithm/gjk_libccd-inl.h line 1272 at r1 (raw file):

                        std::unordered_set<ccd_pt_edge_t*>* internal_edges,
                        std::vector<ccd_pt_edge_t*>* border_edge_order) {
  if (border_edges->insert(edge).second && border_edge_order != nullptr) {

nit: This should not allow for the possibility of a null border_edge_order (in the same way it implicitly doesn't allow for null border_edges or internal_edges.


include/fcl/narrowphase/detail/convexity_based_algorithm/gjk_libccd-inl.h line 1322 at r1 (raw file):

    std::unordered_set<ccd_pt_face_t*>* hidden_faces,
    std::unordered_set<ccd_pt_edge_t*>* internal_edges,
    std::vector<ccd_pt_edge_t*>* border_edge_order = nullptr) {

nit: remove the default nullptr value. There is no valid invocation in which border_edge_order can/should be null.


include/fcl/narrowphase/detail/convexity_based_algorithm/gjk_libccd-inl.h line 1427 at r1 (raw file):

    std::unordered_set<ccd_pt_face_t*>* visible_faces,
    std::unordered_set<ccd_pt_edge_t*>* internal_edges,
    std::vector<ccd_pt_edge_t*>* border_edge_order = nullptr) {

nit: Remove the default value.

There is no valid call to ComputeVisiblePatch() or
ComputeVisiblePatchRecursive() without it, so drop the nullptr default
and the null check, and have the tests pass (and check) the vector.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

@cohnt cohnt left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha

@cohnt made 4 comments and resolved 3 discussions.
Reviewable status: 0 of 2 files reviewed, all discussions resolved (waiting on SeanCurtis-TRI).


include/fcl/narrowphase/detail/convexity_based_algorithm/gjk_libccd-inl.h line 1272 at r1 (raw file):

Previously, SeanCurtis-TRI (Sean Curtis) wrote…

nit: This should not allow for the possibility of a null border_edge_order (in the same way it implicitly doesn't allow for null border_edges or internal_edges.

Done.


include/fcl/narrowphase/detail/convexity_based_algorithm/gjk_libccd-inl.h line 1322 at r1 (raw file):

Previously, SeanCurtis-TRI (Sean Curtis) wrote…

nit: remove the default nullptr value. There is no valid invocation in which border_edge_order can/should be null.

Done.


include/fcl/narrowphase/detail/convexity_based_algorithm/gjk_libccd-inl.h line 1427 at r1 (raw file):

Previously, SeanCurtis-TRI (Sean Curtis) wrote…

nit: Remove the default value.

Done.

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:LGTM:

@SeanCurtis-TRI reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on cohnt).

@SeanCurtis-TRI
SeanCurtis-TRI merged commit ae86ecd into flexible-collision-library:master Oct 21, 2026
6 checks passed
@cohnt
cohnt deleted the deterministic-epa-border-edges branch October 27, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants