[英]How to create numpy vertices for cube from volume
我在numpy-stl文档中遇到了以下示例,该示例为立方体生成网格 object 并打印其体积 -
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
但是,我真的很想扭转这个过程。 给定一个现有的体积测量,我怎么能生成立方体顶点?
这是基本的线性代数; 这里几乎没有编程。 您已经有一个边为 2 的立方体。取给定体积的立方根并缩放参考立方体:
vertices *= volume**(1/3) / 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.