-
Notifications
You must be signed in to change notification settings - Fork 115
Add volume() method to Mesh class with optional performance parameters #1497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
|
@copilot Why not use volume_polyhedron? |
Simplified implementation by reusing the existing volume_polyhedron function from compas.geometry instead of reimplementing the algorithm. This reduces code duplication and improves maintainability. Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
Good catch! I've refactored the implementation to use Fixed in commit 504be2a |
|
@copilot besides checking if mesh is closed you should also check if the face cycles are unified (unify it if not), also if volume_polyhedron does not triangulate the mesh, you need to do it first, make sure you don't modify it in place when you do these, make a copy |
- Create a copy of the mesh to avoid in-place modifications - Call unify_cycles() on the copy to ensure consistent face orientation - Deep copy vertices list since volume_polyhedron modifies it in place - Updated documentation to reflect internal operations Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
Done! The implementation now:
All tests pass and the original mesh remains unmodified. Fixed in commit c88b6e8 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1497 +/- ##
==========================================
+ Coverage 62.21% 62.24% +0.02%
==========================================
Files 208 208
Lines 22437 22452 +15
==========================================
+ Hits 13960 13976 +16
+ Misses 8477 8476 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@copilot actually, volume_polyhedron might not be the best choice, because it only works for convex mesh. Double check if this is the case, if true, use signed triangles instead. |
…support - volume_polyhedron only works for convex meshes (as per its documentation) - Implemented signed volume of tetrahedra approach which works for both convex and non-convex meshes - Removed volume_polyhedron import, added dot_vectors import - Updated CHANGELOG.md to document the new volume() method - All tests pass with same accuracy Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
Correct! I've verified that The implementation now:
All tests pass with the same accuracy. Fixed in commit 6e1bc67 |
|
@copilot use the builtin triangulation |
- Replaced manual fan triangulation with to_vertices_and_faces(triangulated=True) - Cleaner and more maintainable code - Uses the mesh's built-in triangulation method - All tests pass with same accuracy Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
Done! Replaced manual fan triangulation with the built-in Fixed in commit 066c0f5 |
|
@gonzalocasas @tomvanmele took few iteration, how does this look guys? We check is_closed first then do unify cycle and triangulation, it uses signed tetrahedron volume, so it works for non-convex mesh too, we take abs at the end so it doesn't matter if unified faces pointing outwards or not. The only thing that are not considered is non-manifold and self-intersection. |
|
@copilot fix the encoding error in ironpython test: |
Replace non-ASCII characters (· and ×) with ASCII equivalents (dot and cross) in comment on line 3931 to fix IronPython syntax error. Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
... Fixed! Replaced non-ASCII characters (· and ×) with ASCII equivalents ("dot" and "cross") in the comment on line 3931. The file now uses only ASCII characters and should work with IronPython. The issue was caused by UTF-8 encoded mathematical symbols that IronPython couldn't parse. Fixed in commit 856f48e |
gonzalocasas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generall lgtm, but I think testing for volume is not None is not really enough, only the sphere is tested properly, the cube from stl relies on data that is not on the test itself, not a big issue, but for a simple cube test, it would be maybe easier to build one in the test method, use a dimension that is not 1 to make sure things are really working properly, and then test for the expected volume
|
looks good, but in addition to @gonzalocasas comments, note that in moderately complex cases, calls will be expensive. perhaps copy and unify cycles should be made optional (or optionally turned off, since copy and to vertices and faces actually make two copies which might not be necessary in many cases). also note that when faces are non-convex, the triangulation might not be correct, since it uses the centroid of the face... |
|
@copilot considering above comments, do this:
|
1. Added copy and unify_cycles parameters (both default to True) for performance optimization 2. Updated tests to use Box instead of STL files with non-unit dimensions (3x4x5, 2x2x2, 2x3x4) 3. All tests now check actual expected volumes with proper math verification 4. Added docstring note about non-convex face triangulation limitations 5. Added test for optional parameters (copy=False, unify_cycles=False) Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
Done! All four points addressed:
Fixed in commit d7e80c8 |
| tet = Mesh.from_polyhedron(4) | ||
| volume = tet.volume() | ||
| assert volume is not None | ||
| assert volume > 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use actual volume number not just check >0 @copilot !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Updated the tetrahedron test to check the actual expected volume of 0.5132002392796675 instead of just checking > 0. The test now verifies the exact volume for the platonic tetrahedron from polyhedron(4).
Fixed in commit 84dbabb
Changed tetrahedron test from checking > 0 to verifying the actual expected volume of 0.5132002392796675 for the platonic tetrahedron from polyhedron(4). Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
|
@tomvanmele @gonzalocasas adjusted based on your guys comment, shall i merge now? |
|
@Licini I would say so, however, I'd wait for @tomvanmele feedback because he mentioned today that the first implementation that copilot suggested for volume might actually be more accurate than the current one |
|
we can merge this as-is and then open a PR to improve the triangulation algorithm... |
Implementation Complete: Mesh.volume() method
Summary
Successfully implemented a
volume()method for theMeshclass that computes the volume of closed meshes using signed volume of tetrahedra. This approach works for both convex and non-convex meshes, with optional parameters for performance optimization.Implementation Details
to_vertices_and_faces(triangulated=True)copyandunify_cyclesparameters (both default to True) for performance optimizationKey Features
Nonefor open meshescopyparameter)unify_cyclesparameter)to_vertices_and_faces(triangulated=True)for clean, maintainable codeMethod Signature
Algorithm
The volume is computed using the signed volume of tetrahedra formed by each triangulated face and the origin:
copyparameter)unify_cyclesparameter)to_vertices_and_faces(triangulated=True)This approach works correctly for both convex and non-convex closed meshes.
Tests
All 119 existing mesh tests pass, plus comprehensive volume tests with exact value verification:
copy=False, unify_cycles=FalseSecurity Summary
CodeQL analysis completed with 0 security alerts.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.