我有一个简单的盒子(宽:400,长:400,高:50):
下面是创建该框的代码:
import numpy as np
import trimesh
# Vertices
vert = np.array([[-200., -200., 0.],
[-200., -200., 50.],
[-200., 200., 0.],
[-200., 200., 50.],
[ 200., -200., 0.],
[ 200., -200., 50.],
[ 200., 200., 0.],
[ 200., 200., 50.]])
# Faces
fa = np.array([[6, 0, 4],
[0, 6, 2],
[4, 6, 5],
[6, 7, 5],
[6, 2, 7],
[2, 3, 7],
[2, 0, 3],
[0, 1, 3],
[0, 4, 1],
[4, 5, 1],
[7, 1, 5],
[1, 7, 3]])
# MESH
mesh = trimesh.Trimesh(vertices= vert,
faces=fa)
如您所见,长方体的上表面位于z=0
,下表面位于z=50
现在,当我做横截面时,用x作为法线,我希望能清楚地看到这一点:
# X - Normal => AXIS WRONG
slice_ = mesh.section(plane_origin=(0,0,0),
plane_normal=[1,0,0])
slice_2D, to_3D = slice_.to_planar()
slice_2D.show()
。。。但我得到的是:
您可以清楚地看到,长方体横截面没有正确定位,因为它应该从z=0
开始,延伸到z=50
(上图的水平轴)
有趣的是,使用z-法线获得横截面非常有效:
# Z - Normal => OK
slice_ = mesh.section(plane_origin=(0,0,0),
plane_normal=[0,0,1])
slice_2D, to_3D = slice_.to_planar()
slice_2D.show()
…如果我要一个z=-10的横截面,那里不应该有盒子,它会理所当然地抱怨
# Z - Normal => OK
slice_ = mesh.section(plane_origin=(0,0,-10),
plane_normal=[0,0,1])
slice_2D, to_3D = slice_.to_planar()
slice_2D.show()
AttributeError: 'NoneType' object has no attribute 'to_planar'
如何获得x法线的正确横截面
我也在这里发布了这个问题: https://github.com/mikedh/trimesh/issues/1359
I think you need to specify the matrix explicitly when using to_planar, as if the matrix is unspecified the function has to fit a plane which may not be what you want.
备选方案:也许可以绕x轴旋转网格90度,然后做z形截面。我试过了,但不知怎的它不起作用:
from scipy.spatial.transform import Rotation as R
def rotate_vector(vec, rotation_degrees=90, rotation_axis = np.array([1, 0, 0])):
# Function that turns a vector a given angle about a given axis
rotation_radians = np.radians(rotation_degrees)
rotation_vector = rotation_radians * rotation_axis
rotation = R.from_rotvec(rotation_vector)
rotated_vec = rotation.apply(vec)
return rotated_vec
# Turn all vertices by 90deg around the x-axis
new_vert = []
for v in vert:
print(v)
print(rotate_vector(v))
new_vert.append(rotate_vector(v))
new_vert = []
for v in vert:
new_vert.append(rotate_vector(v))
# Create the mesh
mesh_rot = trimesh.Trimesh(vertices= new_vert,
faces=fa)
# Create a slice with a z-axis normal
slice_ = mesh_rot .section(plane_origin=(0,0,0),
plane_normal=[0,0,1.0])
slice_2D, to_3D = slice_.to_planar()
slice_2D.show()
经过长时间的反复试验,我发现这种方法可以解决这个问题:
相关问题 更多 >
编程相关推荐