Skip to content
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

Obtaining the area of a mesh section #2214

Closed
omaralvarez opened this issue Apr 29, 2024 · 5 comments
Closed

Obtaining the area of a mesh section #2214

omaralvarez opened this issue Apr 29, 2024 · 5 comments

Comments

@omaralvarez
Copy link

I am trying to get the area of the intersection of a mesh with a plane. Right now I am using the following code to get the section:

slice = mesh.section(
                plane_normal=n,
                plane_origin=p,
            )

Is it possible to get instead of the outline of the section a filled polygon? Is it possible to get a mesh from a Path3D?

@mikedh
Copy link
Owner

mikedh commented Apr 29, 2024

Hey, I think you want slice.polygons_full[0].area or sum(p.area for p in slice.polygons_full)

@omaralvarez
Copy link
Author

Amazing, I will test it out and see if it works!! Thanks a lot!

@omaralvarez
Copy link
Author

I'm getting the following error:

AttributeError: 'Path3D' object has no attribute 'polygons_full'

@mikedh
Copy link
Owner

mikedh commented Apr 29, 2024

Ah you have to move it to a plane from 3D:

In [14]: m = trimesh.creation.box()

In [15]: s = m.section(plane_origin=m.center_mass, plane_normal=[0,0,1])

In [17]: sum(p.area for p in s.to_planar()[0].polygons_full)
Out[17]: 1.0

@omaralvarez
Copy link
Author

Works like a charm, just for refererence I ended up with:

slice = mesh.section(
    plane_normal=n,
    plane_origin=p,
)
if slice is not None:
    area = sum(p.area for p in slice.to_planar()[0].polygons_full)
else:
    area = 0.0

If plane does not intersect you get None and it errors out.

Thanks a lot for the help!

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

No branches or pull requests

2 participants