marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
2.05k stars 266 forks source link

Mesh.volume() and Mesh.is_closed() don't work with Boxes and Cylinders #1171

Open jkunimune opened 3 months ago

jkunimune commented 3 months ago

Calculating the volume of a Box or a Cylinder seems to always return zero for Box and Cylinder objects, even though they plot just fine and seem overall well-formed. Similarly, is_closed() seems to always return False. Pyramids and Spheres work fine. I'm thinking it may be related to the fact that Boxes and Cylinders are wholly composed of non-triangular cells, since calling triangulate() causes them to give nonzero volumes (but they still don't register as closed).

import vedo

box = vedo.Box()
print("Box:", box.volume(), box.is_closed())

box = box.triangulate()
print("Triangulated box:", box.volume(), box.is_closed())

cylinder = vedo.Cylinder()
print("Cylinder:", cylinder.volume(), cylinder.is_closed())

cylinder = cylinder.triangulate()
print("Triangulated cylinder:", cylinder.volume(), cylinder.is_closed())

pyramid = vedo.Pyramid()
print("Pyramid:", pyramid.volume(), pyramid.is_closed())

sphere = vedo.Sphere()
print("Sphere:", sphere.volume(), sphere.is_closed())

# expected result: all volumes are nonzero and all booleans are True
# actual result:
# Box: 0.0 False
# Triangulated box: 6.000000000000002 False
# Cylinder: 0.0 False
# Triangulated cylinder: 6.211656935437641 False
# Pyramid: 0.43333333333333357 True
# Sphere: 4.157386497443854 True
marcomusy commented 3 months ago

Thanks for reporting. I've added documentation for this behavior in both is_closed() and volume() methods, but also in Box and Cylinder. Checking whether the mesh is triangular in volume() might be computationally expensive if these methods are called in loops or for large meshes.

import vedo

box = vedo.Box().triangulate().clean()
print("Box     :", box.volume(), box.is_closed(), box.is_manifold())

cylinder = vedo.Cylinder().triangulate().clean()
print("Cylinder:", cylinder.volume(), cylinder.is_closed(), cylinder.is_manifold())

pyramid = vedo.Pyramid() # not necessary, already triangulated
print("Pyramid :", pyramid.volume(), pyramid.is_closed(), pyramid.is_manifold())

sphere = vedo.Sphere()
print("Sphere  :", sphere.volume(), sphere.is_closed(), sphere.is_manifold())