Skip to content

irteus: speed up face-to-triangle via libGLU (pqp/glvertices) - #654

Open
inabajsk wants to merge 4 commits into
euslisp:masterfrom
inabajsk:glu-tess-integration
Open

inabajsk wants to merge 4 commits into
euslisp:masterfrom
inabajsk:glu-tess-integration

Conversation

@inabajsk

@inabajsk inabajsk commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

geometry::face-to-triangle-aux (irtgeo.l) is an O(n^2) ear-clipping
triangulator. It's called on every face by two hot paths:

  • cascaded-coords's :make-pqpmodel (pqp.l) — run once per link
    during robot/body init, uncached.
  • gl::make-glvertices-from-faces (irtgl.l) — run whenever a render
    mesh is (re)built from a faceset.

For faces with many holes/vertices (e.g. plates with many mounting
holes cut out) this dominates model build time; on a real multi-link
robot model with several such faces, build times of minutes were
observed to be spent almost entirely in ear-clipping.

Fix

Adds geometry::face-to-triangle-fast (irtgeo.l): a drop-in
replacement for face-to-triangle-aux that triangulates via libGLU's
sweep-line tessellator when it's linked in (eusgl/irteusgl),
falling back to the original ear-clipping implementation when it isn't
(a non-GL eus/irteus build, where pqp.l is still loaded) or when
GLU reports an error for a particular face.

*glu-tess-available* is computed once via
(and (fboundp 'gl::eus_tess_reset) (fboundp 'gl::gluNewTess)), so the
fallback is safe even in a build where the gl:: package/symbols
don't exist at all — this is a plain symbol-existence check, not a
try/catch, so it can't itself error on a non-GL build.

Wired into:

  • pqp.l's :make-pqpmodel: face-to-triangle-aux -> face-to-triangle-fast
  • irtgl.l's make-glvertices-from-faces: same swap

This depends on the low-level eus_tess_* GLU tessellation-callback
collector primitives added to EusLisp core in
euslisp/EusLisp#542 — but only for the speedup; without them (older
EusLisp core, or a non-GL build), this PR is a no-op functionally and
falls back to the exact original algorithm.

Testing

Extended the existing irteus/test/test-triangulation.l (rather than
adding a new file, so it keeps running via .travis.sh's
for test_l in irteus/test/*.l CI loop automatically) with:

  • test-triangulation-fast-consistency-{0,1}: reuse the existing
    5-hole multi-hole test fixtures, but assert face-to-triangle-fast
    matches face-to-triangle-aux in triangle count, total area, and
    line-intersection behavior against the same probe lines.

  • test-triangulation-benchmark-many-holes: builds a plate with 6
    holes, times both paths, and asserts they still agree (triangle
    count + area) while printing the actual speedup. On the CI/dev
    machine used here:

    ;; triangulation-benchmark-many-holes: 6 holes, 5 boundary vertices + 78 hole vertices
    ;;   face-to-triangle-aux  (ear clipping): 86 triangles in 2.0619 sec
    ;;   face-to-triangle-fast (GLU when available): 86 triangles in 0.002253 sec
    ;;   -> GLU is available here, speedup 915.352x
    

All 6 tests in the file pass (irteusgl test/test-triangulation.l).

Separately verified the non-GL fallback path by forcing
geo::*glu-tess-available* to nil inside a working irteusgl
process (exercising the identical branch a real non-GL build would
take) and confirming both :make-pqpmodel and
gl::make-glvertices-from-faces still complete correctly via the
ear-clipping path.

This was validated against a real-world workload before opening this
PR: applying the equivalent of this fix downstream (as an
application-level method override, not touching irteus core) cut
total generation time across ~30 previously-slowest robot models in a
larger robot-model-generation batch from ~8985s to ~818s (~11x
aggregate), with individual robots seeing 6x-28x speedups depending on
face/hole complexity.

Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com

Update: added face-to-triangle-delaunay for a mesh-quality comparison

Also added geometry::face-to-triangle-delaunay (irtgeo.l): a
from-scratch, dependency-free 2D Delaunay triangulation
(Bowyer-Watson), added purely to compare mesh quality against the
two triangulators above -- not wired into pqp.l/irtgl.l; GLU
remains the right choice for those (this implementation is O(n^2),
no spatial acceleration structure, uncompetitive on speed).

Delaunay triangulation maximizes the minimum angle over all
triangulations of a point set (the empty-circumcircle property), so
it's expected to avoid the long thin slivers that both ear-clipping
and GLU's sweep-line can produce. The new
test-triangulation-quality-comparison test demonstrates this on the
same 6-hole face used by triangulation-benchmark-many-holes:

;;   aux      (ear clipping):  max/min triangle-area ratio ~4166
;;   fast     (GLU)         :  max/min triangle-area ratio ~3998
;;   delaunay (Bowyer-Watson): max/min triangle-area ratio  ~159

(total covered area matches across all three; only how each cuts the
same region into triangles differs.)

Caveat, documented in the code: this is an unconstrained Bowyer-Watson

  • centroid-based inside/outside filter, not a rigorous constrained
    Delaunay triangulation, and its plain double-precision
    in-circumcircle test is unreliable on symmetric/co-circular input (an
    evenly-spaced ring of holes reproducibly triggered >10% spurious
    area in testing). Mitigated by inserting points in a deterministic
    (reproducible) shuffled order and retrying with a different shuffle,
    checked against the true polygon area (shoelace formula) each time;
    falls back to a loud warning if every attempt still disagrees beyond
    tolerance, rather than silently returning a wrong mesh.

inabajsk and others added 3 commits September 14, 2026 09:28
geometry::face-to-triangle-aux is O(n^2) ear-clipping, called on every
face by cascaded-coords's :make-pqpmodel and by
gl::make-glvertices-from-faces on every render-mesh rebuild. For faces
with many holes/vertices this dominates model build time.

Add geometry::face-to-triangle-fast (irtgeo.l), a drop-in replacement
that triangulates via libGLU's sweep-line tessellator when it's linked
in (eusgl/irteusgl), falling back to the original ear-clipping path on
a non-GL build or if GLU errors on a particular face --
*glu-tess-available* is computed once via fboundp so the fallback is
safe even when the gl:: package doesn't exist at all. Wire it into
pqp.l's :make-pqpmodel and irtgl.l's make-glvertices-from-faces.

Extend irteus/test/test-triangulation.l with:
 - fast-vs-aux consistency checks (triangle count, total area,
   line-intersection behavior) reusing the existing multi-hole cases
 - a benchmark against a face with more holes, demonstrating real
   speedup (86 triangles, ~2.06s -> ~0.0023s, ~915x on this box)
   rather than just adding the low-level GLU collector primitives in
   isolation (see euslisp/EusLisp#542)

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Adds a from-scratch, dependency-free 2D Delaunay triangulation
(Bowyer-Watson incremental algorithm) to irtgeo.l, purely to compare
mesh quality against face-to-triangle-aux (ear clipping) and
face-to-triangle-fast (GLU sweep-line, this branch's earlier commit).
Not wired into pqp.l or irtgl.l -- GLU remains the right choice there
since this implementation is O(n^2) with no spatial acceleration
structure, uncompetitive on speed.

Delaunay triangulation maximizes the minimum angle over all
triangulations of a point set (the empty-circumcircle property), so
it's expected to avoid long thin slivers that both ear-clipping and
GLU's sweep-line can produce. The new
test-triangulation-quality-comparison test demonstrates this
concretely: on the same 6-hole face already used by
triangulation-benchmark-many-holes, max/min triangle area ratio is
~4166 (ear-clipping) / ~3998 (GLU) vs ~159 (Delaunay), while total
covered area matches across all three.

The implementation is an unconstrained Bowyer-Watson followed by a
centroid-based inside-boundary/outside-holes filter, not a rigorous
constrained Delaunay triangulation -- and its plain double-precision
in-circumcircle test is unreliable on symmetric/co-circular input
(an evenly-spaced ring of holes reproducibly triggered >10% spurious
area). Mitigated by inserting points in a deterministic (reproducible)
shuffled order and retrying with a different shuffle, checked against
the true polygon area computed directly via the shoelace formula, each
time; falls back to warning loudly if every attempt still disagrees
beyond tolerance rather than silently returning a wrong mesh.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
- catkin: GitHub Actions now forces Node 24 for actions targeting
  Node 20, but ubuntu:18.04's glibc (2.27) can't run it (needs
  >= 2.28). Set ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION so
  actions/checkout keeps using the glibc-2.17 Node 20 build the
  job already stages into /__e/node20.
- linux-arm (bullseye): Debian bullseye's LTS support has ended, so
  deb.debian.org no longer serves bullseye(-security) packages.
  Switch to the snapshot.debian.org mirrors the image ships
  (commented out) and skip the Valid-Until check for this frozen
  suite's Release file.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@inabajsk

Copy link
Copy Markdown
Contributor Author

CI was failing on two jobs unrelated to the code change here, both root-caused and fixed in 1812a10:

  • catkin: GitHub Actions started forcing Node 24 for actions that target Node 20 (e.g. actions/checkout@v2), but the ubuntu:18.04 container's glibc (2.27) is too old to run Node 24 (needs >= 2.28), so the checkout step crashed with GLIBC_2.28' not found. The job already had a workaround that installs a glibc-2.17-compatible Node 20 build into /__e/node20, but the forced-Node-24 upgrade bypassed it. Setting ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true on the job keeps actions/checkout on Node 20, where that workaround applies again.
  • linux-arm (arm64v8/debian:bullseye): Debian bullseye's LTS support ended, so deb.debian.org no longer serves bullseye/bullseye-security packages (404s). .travis.sh now switches to the snapshot.debian.org mirror lines the bullseye image already ships (commented out by default) and skips the Valid-Until check, since a frozen suite's Release file goes stale quickly.

Verified locally with docker run debian:bullseye before pushing, and confirmed both jobs (and the rest of the matrix) pass on the latest run: https://github.com/euslisp/jskeus/actions/runs/34900620942

Three interactive irteus demos exploring a lightweight, EusLisp-native
rigid-body physics visualization: prim-drop.l (multi-shape drop onto a
floor), prim-box.l (same shapes bouncing inside an enclosed box), and
hand-grasp.l (a hand catching each shape via IK-driven finger closing).

Each body carries a real density-derived mass and per-shape diagonal
moment-of-inertia tensor. Body-to-body tunnelling is prevented with a
conservative-advancement CCD scheme (pair-toi-bound/min-pair-toi/
ccd-advance-frame in prim-drop.l) rather than fixed substepping, with a
guaranteed full-frame catch-up step so a busy multi-body frame never
silently drops simulated time.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

1 participant