【问题标题】:Python trimesh x-cross section produces weird resultPython trimesh x-cross section会产生奇怪的结果
【发布时间】:2021-11-19 00:42:06
【问题描述】:

我有一个简单的盒子(宽度: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-normal 获得横截面效果很好:

# 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' 对象没有属性 'to_planar'

如何获得 x 法线的正确横截面?

编辑

我也在这里发布了这个问题: https://github.com/mikedh/trimesh/issues/1359

我认为您需要在使用时明确指定矩阵 to_planar,就好像矩阵是未指定的函数必须适合 飞机可能不是你想要的。

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

【问题讨论】:

    标签: python trimesh


    【解决方案1】:

    经过长时间的反复试验,我找到了修复它的方法:

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

    【讨论】:

      猜你喜欢
      • 2015-10-04
      • 1970-01-01
      • 2014-02-23
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      相关资源
      最近更新 更多