Skip to content

Add VolumeBelow/CenterOfVolumeBelow for Ellipsoid, Cylinder, Capsule and Cone#730

Merged
caguero merged 2 commits intomainfrom
buoyancy-new-shapes
Mar 19, 2026
Merged

Add VolumeBelow/CenterOfVolumeBelow for Ellipsoid, Cylinder, Capsule and Cone#730
caguero merged 2 commits intomainfrom
buoyancy-new-shapes

Conversation

@caguero
Copy link
Copy Markdown
Contributor

@caguero caguero commented Mar 16, 2026

🎉 New feature

Summary

Continuing with #724 , this patch extends the volume API (VolumeBelow and CenterOfVolumeBelow) — previously available only on Sphere and Box — to Ellipsoid, Cylinder, Capsule, and Cone.

Each shape computes the volume and centroid of the region below an arbitrary cutting plane, which is the key primitive needed for buoyancy force and torque calculations in simulation.

Algorithms

Shape Complexity Technique Notes
Ellipsoid O(1) Affine map to unit sphere + spherical cap formula 1 sqrt, 0 trig, 0 heap allocs
Cylinder O(1) Closed-form circular segment antiderivatives (Cavalieri's principle) 2-5 trig calls, 0 heap allocs. Shared helpers in WetVolume.hh
Capsule O(1) Decomposes into Cylinder + 2 Sphere hemispheres Stability guard for tiny hemisphere slices avoids catastrophic cancellation
Cone O(1) 10-point Gauss-Legendre quadrature over smooth sub-intervals <= 90 integrand evals. Varying radius prevents closed-form. Kink-point subdivision ensures smoothness

All implementations are header-only, zero-allocation, and suitable for real-time stepping.

Changes

  • detail/WetVolume.hh (new) — shared circular-segment and GL10 quadrature helpers, eliminating duplication between Cylinder and Cone.
  • detail/{Ellipsoid,Cylinder,Capsule,Cone}.hhVolumeBelow and CenterOfVolumeBelow implementations.
  • Public headers — method declarations added.
  • C++ tests — per-shape tests (horizontal, vertical, oblique, tilted planes; complement symmetry; rotational offset) + VolumeBelowFloat tests for all four shapes.
  • Python pybind11volume_below / center_of_volume_below bindings and tests for all four shapes.
  • Ruby SWIGVolumeBelow / CenterOfVolumeBelow added to Cylinder.i, Cone.i. New Ellipsoid.i, Capsule.i created (preparatory; not in active build).

Test plan

  • All 193 existing + new C++ tests pass (ctest --output-on-failure)
  • New Python binding tests pass for all four shapes
  • Float instantiation tests compile and pass for all four shapes
  • CI passes on all platforms

Checklist

  • Signed all commits for DCO
  • Added a screen capture or video to the PR description that demonstrates the feature
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • Updated Bazel files (if adding new files). Created an issue otherwise.
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers
  • Was GenAI used to generate this PR? If so, make sure to add "Generated-by" to your commits. (See this policy for more info.)

Generated-by: Claude

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by and Generated-by messages.

Backports: If this is a backport, please use Rebase and Merge instead.

@caguero caguero force-pushed the buoyancy-new-shapes branch 4 times, most recently from 13df206 to d377d08 Compare March 16, 2026 21:47
… Cone

- Extract shared circular-segment and GL10 quadrature helpers into
  detail/WetVolume.hh, replacing duplicate code in Cylinder and Cone
- Improve Capsule CenterOfVolumeBelow numerical stability by guarding
  against catastrophic cancellation when hemisphere slice volume is tiny
- Add float instantiation tests for all four shapes
- Add Python pybind11 bindings and tests for volume_below and
  center_of_volume_below on all four shapes
- Add Ruby SWIG bindings for VolumeBelow/CenterOfVolumeBelow on
  Cylinder, Cone, and new Ellipsoid/Capsule .i files
- Fix MSVC C4305 truncation warning in Ellipsoid::Volume

Generated-by: Claude
Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
@caguero caguero force-pushed the buoyancy-new-shapes branch from d377d08 to 3b0a8b0 Compare March 16, 2026 22:21
@caguero caguero requested review from ahcorde and arjo129 March 17, 2026 12:16
Copy link
Copy Markdown
Contributor

@ahcorde ahcorde left a comment

Choose a reason for hiding this comment

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

it's being a while since we didn't update the Ruby files. Not sure if we have any policy about this.

@github-project-automation github-project-automation bot moved this from Inbox to In review in Core development Mar 19, 2026
@caguero caguero merged commit 2860804 into main Mar 19, 2026
12 checks passed
@caguero caguero deleted the buoyancy-new-shapes branch March 19, 2026 12:43
@github-project-automation github-project-automation bot moved this from In review to Done in Core development Mar 19, 2026
@caguero
Copy link
Copy Markdown
Contributor Author

caguero commented Mar 19, 2026

@Mergifyio backport gz-math9 gz-math8 gz-math7

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Mar 19, 2026

backport gz-math9 gz-math8 gz-math7

✅ Backports have been created

Details

Cherry-pick of 2860804 has failed:

On branch mergify/bp/gz-math8/pr-730
Your branch is up to date with 'origin/gz-math8'.

You are currently cherry-picking commit 2860804.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   include/gz/math/Capsule.hh
	modified:   include/gz/math/Cone.hh
	modified:   include/gz/math/Cylinder.hh
	modified:   include/gz/math/Ellipsoid.hh
	modified:   include/gz/math/detail/Capsule.hh
	modified:   include/gz/math/detail/Cone.hh
	modified:   include/gz/math/detail/Cylinder.hh
	modified:   include/gz/math/detail/Ellipsoid.hh
	new file:   include/gz/math/detail/WetVolume.hh
	modified:   src/Capsule_TEST.cc
	modified:   src/Cone_TEST.cc
	modified:   src/Cylinder_TEST.cc
	modified:   src/Ellipsoid_TEST.cc
	modified:   src/python_pybind11/src/Capsule.hh
	modified:   src/python_pybind11/src/Cone.hh
	modified:   src/python_pybind11/src/Cylinder.hh
	modified:   src/python_pybind11/src/Ellipsoid.hh
	new file:   src/ruby/Capsule.i
	modified:   src/ruby/Cone.i
	modified:   src/ruby/Cylinder.i
	new file:   src/ruby/Ellipsoid.i

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/python_pybind11/test/Capsule_TEST.py
	both modified:   src/python_pybind11/test/Cone_TEST.py
	both modified:   src/python_pybind11/test/Cylinder_TEST.py
	both modified:   src/python_pybind11/test/Ellipsoid_TEST.py

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Cherry-pick of 2860804 has failed:

On branch mergify/bp/gz-math7/pr-730
Your branch is up to date with 'origin/gz-math7'.

You are currently cherry-picking commit 2860804.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   include/gz/math/Capsule.hh
	modified:   include/gz/math/Cone.hh
	modified:   include/gz/math/Cylinder.hh
	modified:   include/gz/math/Ellipsoid.hh
	modified:   include/gz/math/detail/Ellipsoid.hh
	new file:   include/gz/math/detail/WetVolume.hh
	modified:   src/Capsule_TEST.cc
	modified:   src/Cone_TEST.cc
	modified:   src/Cylinder_TEST.cc
	modified:   src/Ellipsoid_TEST.cc
	modified:   src/python_pybind11/src/Capsule.hh
	modified:   src/python_pybind11/src/Cone.hh
	modified:   src/python_pybind11/src/Cylinder.hh
	modified:   src/python_pybind11/src/Ellipsoid.hh
	new file:   src/ruby/Capsule.i
	modified:   src/ruby/Cone.i
	modified:   src/ruby/Cylinder.i
	new file:   src/ruby/Ellipsoid.i

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   include/gz/math/detail/Capsule.hh
	both modified:   include/gz/math/detail/Cone.hh
	both modified:   include/gz/math/detail/Cylinder.hh
	both modified:   src/python_pybind11/test/Capsule_TEST.py
	both modified:   src/python_pybind11/test/Cone_TEST.py
	both modified:   src/python_pybind11/test/Cylinder_TEST.py
	both modified:   src/python_pybind11/test/Ellipsoid_TEST.py

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

mergify bot pushed a commit that referenced this pull request Mar 19, 2026
… Cone (#730)

- Extract shared circular-segment and GL10 quadrature helpers into
  detail/WetVolume.hh, replacing duplicate code in Cylinder and Cone
- Improve Capsule CenterOfVolumeBelow numerical stability by guarding
  against catastrophic cancellation when hemisphere slice volume is tiny
- Add float instantiation tests for all four shapes
- Add Python pybind11 bindings and tests for volume_below and
  center_of_volume_below on all four shapes
- Add Ruby SWIG bindings for VolumeBelow/CenterOfVolumeBelow on
  Cylinder, Cone, and new Ellipsoid/Capsule .i files
- Fix MSVC C4305 truncation warning in Ellipsoid::Volume

Generated-by: Claude

Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
(cherry picked from commit 2860804)
mergify bot pushed a commit that referenced this pull request Mar 19, 2026
… Cone (#730)

- Extract shared circular-segment and GL10 quadrature helpers into
  detail/WetVolume.hh, replacing duplicate code in Cylinder and Cone
- Improve Capsule CenterOfVolumeBelow numerical stability by guarding
  against catastrophic cancellation when hemisphere slice volume is tiny
- Add float instantiation tests for all four shapes
- Add Python pybind11 bindings and tests for volume_below and
  center_of_volume_below on all four shapes
- Add Ruby SWIG bindings for VolumeBelow/CenterOfVolumeBelow on
  Cylinder, Cone, and new Ellipsoid/Capsule .i files
- Fix MSVC C4305 truncation warning in Ellipsoid::Volume

Generated-by: Claude

Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
(cherry picked from commit 2860804)

# Conflicts:
#	src/python_pybind11/test/Capsule_TEST.py
#	src/python_pybind11/test/Cone_TEST.py
#	src/python_pybind11/test/Cylinder_TEST.py
#	src/python_pybind11/test/Ellipsoid_TEST.py
mergify bot pushed a commit that referenced this pull request Mar 19, 2026
… Cone (#730)

- Extract shared circular-segment and GL10 quadrature helpers into
  detail/WetVolume.hh, replacing duplicate code in Cylinder and Cone
- Improve Capsule CenterOfVolumeBelow numerical stability by guarding
  against catastrophic cancellation when hemisphere slice volume is tiny
- Add float instantiation tests for all four shapes
- Add Python pybind11 bindings and tests for volume_below and
  center_of_volume_below on all four shapes
- Add Ruby SWIG bindings for VolumeBelow/CenterOfVolumeBelow on
  Cylinder, Cone, and new Ellipsoid/Capsule .i files
- Fix MSVC C4305 truncation warning in Ellipsoid::Volume

Generated-by: Claude

Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
(cherry picked from commit 2860804)

# Conflicts:
#	include/gz/math/detail/Capsule.hh
#	include/gz/math/detail/Cone.hh
#	include/gz/math/detail/Cylinder.hh
#	src/python_pybind11/test/Capsule_TEST.py
#	src/python_pybind11/test/Cone_TEST.py
#	src/python_pybind11/test/Cylinder_TEST.py
#	src/python_pybind11/test/Ellipsoid_TEST.py
caguero added a commit that referenced this pull request Mar 19, 2026
… Cone (#730)

Generated-by: Claude
Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
caguero added a commit that referenced this pull request Mar 19, 2026
… Cone (#730)

Generated-by: Claude
Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
caguero added a commit that referenced this pull request Mar 19, 2026
… Cone (#730)

- Extract shared circular-segment and GL10 quadrature helpers into
  detail/WetVolume.hh, replacing duplicate code in Cylinder and Cone
- Improve Capsule CenterOfVolumeBelow numerical stability by guarding
  against catastrophic cancellation when hemisphere slice volume is tiny
- Add float instantiation tests for all four shapes
- Add Python pybind11 bindings and tests for volume_below and
  center_of_volume_below on all four shapes
- Add Ruby SWIG bindings for VolumeBelow/CenterOfVolumeBelow on
  Cylinder, Cone, and new Ellipsoid/Capsule .i files
- Fix MSVC C4305 truncation warning in Ellipsoid::Volume

Generated-by: Claude

Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
(cherry picked from commit 2860804)
caguero added a commit that referenced this pull request Mar 19, 2026
… Cone (#730)

Generated-by: Claude
Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
caguero added a commit that referenced this pull request Mar 19, 2026
… Cone (#730)

Generated-by: Claude
Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
caguero added a commit that referenced this pull request Mar 19, 2026
…730)

Generated-by: Claude
Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
caguero added a commit that referenced this pull request Mar 19, 2026
…730)

Generated-by: Claude
Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
caguero added a commit that referenced this pull request Mar 23, 2026
…730)

Generated-by: Claude
Signed-off-by: Carlos Agüero <caguero@osrfoundation.org>
@scpeters scpeters mentioned this pull request Mar 26, 2026
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants