I came across the following example in the numpy-stl docs which produces a mesh object for a cube and prints its volume --
import numpy as np
from stl import mesh
# Define the 8 vertices of the cube
vertices = np.array(
[
[-1, -1, -1],
[+1, -1, -1],
[+1, +1, -1],
[-1, +1, -1],
[-1, -1, +1],
[+1, -1, +1],
[+1, +1, +1],
[-1, +1, +1],
]
)
# Define the 12 triangles composing the cube
faces = np.array(
[
[0, 3, 1],
[1, 3, 2],
[0, 4, 7],
[0, 7, 3],
[4, 5, 6],
[4, 6, 7],
[5, 1, 2],
[5, 2, 6],
[2, 3, 6],
[3, 7, 6],
[0, 1, 5],
[0, 5, 4],
]
)
# Create the mesh
cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
cube.vectors[i][j] = vertices[f[j], :]
volume, cog, inertia = cube.get_mass_properties()
print(f"Volume: {volume}") # 8.0
However, I'd really like to reverse this process. Given an existing volume measurement, how could I then generate the cubes vertices?
This is basic linear algebra; there's virtually no programming here. You already have a cube with an edge of 2. Take the cube root of your given volume and scale the reference cube:
vertices *= volume**(1/3) / 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.