question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calculating a volume of a custom mesh using mesh.volume() doesn't work properly

See original GitHub issue

Hi, I’m currently trying to build tubes and tubelike meshes, that might branch, etc. (I’m aware that there is a Tube shape in the Vedo library). I want to cut them and cap them at the places where they have opened up after cutting.
Additionally, I would like to calculate the total volume enclosed by the mesh before and after the procedure. Here’s an example below:

from vedo import *
import math

r = 1
coord_start_z = 0
coord_end_z = 1
angle = 0
number_of_vertices = 520
start_x = 0
start_y = 0
coordinates_all = []


for i in range(0,number_of_vertices):
    angle = (i/number_of_vertices)*2*3.141592653589793
    coord_x = start_x + r * math.cos(angle)
    coord_y = start_y + r * math.sin(angle)

    coordinates_all.append((coord_x,coord_y,coord_start_z))
    coordinates_all.append((coord_x,coord_y,coord_end_z))

faces = []
faces_base_start = [i for i in range(0,number_of_vertices*2) if i%2 == 0]
faces_base_end = [i for i in range(0,number_of_vertices*2) if i%2 != 0]
for i in range(0,number_of_vertices):
        faces_start = []
        if i + 1 == number_of_vertices:
            faces.append((i*2,i*2+1,1,0))
        else:
            faces.append((i*2,i*2+1,i*2+3,i*2+2))
        
faces.append(tuple(faces_base_start))
faces.append(tuple(faces_base_end))

tube =  Mesh([coordinates_all,faces])
cut_tube = tube.clone().cutWithPlane(origin=(0, 0, 0), normal=(1, 0, 0))
capped_cut_tube = cut_tube.clone().cap()
capped_cut_tube.c('r',alpha=0.5)

print('Volume of the original tube is: ' + str(tube.volume()))
print('Volume of the cut and capped tube is: ' + str(capped_cut_tube.volume()))

show(tube,axes=1,at=0,shape=(2,1))
show(capped_cut_tube,axes=1,at=1,shape=(2,1))

Unfortunately, the volume of the first tube is computed as 0 (it should be around 3.141592) and the second one is incorrectly calculated as well (0.8742, even though the volume is roughly halved by the split).

What could be the issue here?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lukablagojecommented, Oct 12, 2021

Thanks!

0reactions
marcomusycommented, Oct 13, 2021

I actually did not think of the possibility to sweep a line to create tubes:

from vedo import *

pts = Circle().points().tolist()
ln1 = Line(pts+[pts[0]])

surf1 = ln1.sweep(( 2,0.2,3), res=3).c('red3')

pts = ln1.points().tolist()
ln2 = Line(pts+[pts[0]])
surf3 = ln2.sweep((3,0.3,3), res=3).c('green3') # the right branch

surf2 = ln1.sweep((-2,0.2,3), res=3).c('blue3')
surf2.cutWithMesh(surf3) # this cuts, but must increase resolution (res)

show(surf1, surf2, surf3, axes=1).close()

m = merge(surf1, surf2, surf3)
print(m.bounds())

################# second solution/approach
line1 = Line([[0,0,0],[2,.2,3],[-2,-.1,6]], lw=4).c('red4')
line2 = Line([[2,.2,3],[3,-.1,6]], lw=4)
lines = merge(line1, line2)
tube = lines.implicitModeller(distance=0.3, res=(100,50,100), bounds=m.bounds()).smooth()
tube.flat().lw(0.1).alpha(0.5)

show(line1,line2, tube, axes=1)

Screenshot from 2021-10-13 14-45-16

But probably the second solution is what you are after (but this one can become very memory-consuming for large networks:

Screenshot from 2021-10-13 15-01-21

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calculating the volume of a closed custom mesh?
Iterate over all triangles. Take the face consisting of these 3 vertices and connect it with the apex to form a pyramid. Calculate...
Read more >
Calculating the volume of a mesh – Nervous System blog
The idea behind calculating the volume of a mesh is to calculate a volume for each triangle of the mesh and add them...
Read more >
How would one calculate a 3d Mesh volume in Unity?
I am not quite sure what you are looking to do, this seems to calculate the volume of a mesh (cubic units), was...
Read more >
How calculate a volume of this mesh 3D? - Stack Overflow
In Meshlab: Try the poisson option under point set; Or use i.e. Blender to close the volume manually. Once this is done, it...
Read more >
Calculating volume of clipped 3D mesh · Issue #71 - GitHub
The volume calculation of the clipped mesh is incorrect because it is not a closed surface while the input cylinder is a completely...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found