trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

x-cross-section produces weird result

Open dorianherle opened this issue 3 years ago • 6 comments

I also posted this question here: https://stackoverflow.com/posts/69336886/edit

I have a simple box (w:400, l=400, height=50):

image

Here the code to create that box:

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

The issue

As you can see, the top surface of the box is at z=0, and the lower surface is at z=50. Now, I would expect to see this clearly, when I do a cross-section, using x as a normal:

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

... but what I get is this:

image

You can clearly see that the box cross-section is not positioned correctly, as it should begin at z=0, and extend to z=50.

Interestingly, getting cross-sections with z-normal works perfectly:

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

image

..if I ask for a cross-section at z=-10, where there should be no box, it rightfully complains.

# 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'

I would really appreciate any help to get the correct cross-section for the x-normal!

dorianherle avatar Sep 26 '21 16:09 dorianherle

Hey, 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.

mikedh avatar Sep 26 '21 16:09 mikedh

@mikedh Thank you very much for your fast response! I am not sure to understand what you mean by "specifying the matrix"? Can I find an example somewhere? More specifically, I am not quite sure what is meant by "Homogeneous transformation matrix" in this context..

dorianherle avatar Sep 26 '21 16:09 dorianherle

@mikedh I also tried to simply turn the mesh by 90 degrees around the x-axis and then take a cross-section with a z-normal, but this somehow didn't work as well. See "EDIT 2" here: https://stackoverflow.com/questions/69336886/python-trimesh-x-cross-section-produces-weird-result

dorianherle avatar Sep 26 '21 17:09 dorianherle

@mikedh After long trial and error I found this method to fix it:

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

image

Do you understand why this works? Btw: Thank you so much for creating this awesome python package!

dorianherle avatar Sep 28 '21 13:09 dorianherle

@mikedh After long trial and error I found this method to fix it:

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

image

Do you understand why this works? Btw: Thank you so much for creating this awesome python package!

Thank you so much! I had a similar issue when slicing with plane that is normal to x-axis. Your last comment saved me 👍

ne1114 avatar Mar 30 '22 07:03 ne1114

@mikedh After long trial and error I found this method to fix it:

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

image Do you understand why this works? Btw: Thank you so much for creating this awesome python package!

Thank you so much! I had a similar issue when slicing with plane that is normal to x-axis. Your last comment saved me 👍

Thanks for working through this issue cropped up for me as well.

JeffPS13 avatar Oct 18 '23 15:10 JeffPS13