wolph / numpy-stl

Simple library to make working with STL files (and 3D objects in general) fast and easy.
http://numpy-stl.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
624 stars 105 forks source link

surface area for stl #77

Closed vivek4890 closed 6 years ago

vivek4890 commented 6 years ago

Hello,

I want to calculate surface area for stl file, is there any method I can use to get surface area like "get_mass_properties()", I used it to calculate volume. I'm new in Python please suggest how can I get the surface area for any .stl file.

thanks !

Uvar commented 6 years ago

This is not a numpy-stl issue, but basic calculus.

If I am correct, @WoLpH added a property to each polygon containing its area, which you can then just sum: myMesh.areas.sum()

or calculate it by hand by doing a vector cross product

v0 = myMesh.data['vectors'][:, 0]
v1 = myMesh.data['vectors'][:, 1]
v2 = myMesh.data['vectors'][:, 2]

normals = 0,5*numpy.cross(v1 - v0, v2 - v0) # half the surface area of the parallelogram spanned by the vectors
wolph commented 6 years ago

Before myMesh.areas is available you'll need to call the update_areas() method. But after that it should work as @Uvar suggested :)

https://github.com/WoLpH/numpy-stl/blob/2cc27fc50f5f9f4e3dd8ed64e7f6d3c7ab00ef77/stl/base.py#L314-L316