Python trimesh xcross部分产生了奇怪的结果

2024-05-19 03:04:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的盒子(宽:400,长:400,高:50):

enter image description here

下面是创建该框的代码:

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()

。。。但我得到的是:

enter image description here

您可以清楚地看到,长方体横截面没有正确定位,因为它应该从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()

enter image description here

…如果我要一个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.

编辑2

备选方案:也许可以绕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()
    

enter image description here


Tags: thetonewnpsectionslicevectornormal
1条回答
网友
1楼 · 发布于 2024-05-19 03:04:40

经过长时间的反复试验,我发现这种方法可以解决这个问题:

def cross_section(mesh, plane_origin=[0,0,0], plane_normal=[1,0,0]):
  
    slice_ = mesh.section(plane_origin=plane_origin, 
                          plane_normal=plane_normal)

    # transformation matrix for to_planar 
    # I don't know why
    to_2D = trimesh.geometry.align_vectors(plane_normal, [0,0,-1])
    
    slice_2D, to_3D = slice_.to_planar(to_2D = to_2D)
    
    return slice_2D, to_3D


slice_2D, to_3D = cross_section(mesh)
slice_2D.show()

enter image description here

相关问题 更多 >

    热门问题