[英]How to create numpy vertices for cube from volume
I came across the following example in the numpy-stl docs which produces a mesh object for a cube and prints its 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
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:
您已经有一个边为 2 的立方体。取给定体积的立方根并缩放参考立方体:
vertices *= volume**(1/3) / 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.