daisy
daisy

Reputation: 21

How to get the perimeter of the cross-section between mesh and plane by python?

I use a plane to cut the mesh through meshcut and get a cut surface. The contour of this section is represented by discrete points, how can I get the perimeter of this contour?

The profile of this section is similar to an ellipse but not a regular shape.Could you give me some advises to compute the perimeter or have you found some code about perimeter computation in meshcut?

More details: Here are the points I got from meshcut. They can be viewed as two-dimensional points. How can I get the convex hull of these discrete points and calculate the perimeter of the convex hull? enter image description here

Upvotes: 2

Views: 897

Answers (1)

blunova
blunova

Reputation: 2532

You have added the trimesh tag, so I'm going to suggest a very simple solution using this library applied to the Stanford bunny.

Here is our bunny:

enter image description here

Load it (I'm also adding type hints):

mesh: trimesh.base.Trimesh = trimesh.load_mesh(r"path\to\bunny.ply", process=False, maintain_order=True)

We can take a three-dimensional cross-section using a plane by specifying its normal and a point:

section: trimesh.path.Path3D = mesh.section(
    plane_origin=[0.0, 0.0, 30.0], plane_normal=[0.0, 0.0, 1.0]
)

We can convert this three-dimensional cross-section to a two-dimensional one.

section_2d: trimesh.path.Path2D = section.to_planar()[0]

Let's plot it. As you can see, it's clearly a non-convex shape:

plt.scatter(section_2d.vertices[:, 0], section_2d.vertices[:, 1])
plt.show()

enter image description here

You can get the perimeter of the section as follows:

section_2d.length
>>> 251.61385868533435

Upvotes: 2

Related Questions