pyvista / pymeshfix

Python Wrapper for MeshFix: easily repair holes in surface meshes
http://pymeshfix.pyvista.org
GNU General Public License v3.0
278 stars 29 forks source link

Error while running meshfix.repair() #41

Open MarcelHeu opened 2 years ago

MarcelHeu commented 2 years ago

Hey,

If i try to repair an input mesh file, the following error will follow:

Windows fatal exception: access violation

Main thread:
Current thread 0x000031d4 (most recent call first):
  File "C:\Developer\WPy64-3950\python-3.9.5.amd64\lib\site-packages\pymeshfix\meshfix.py", line 207 in repair
  File "C:\Users\Marcel\Desktop\untitled5.py", line 84 in <module>
  File "C:\Developer\WPy64-3950\python-3.9.5.amd64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 465 in exec_code
  File "C:\Developer\WPy64-3950\python-3.9.5.amd64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 585 in runfile
  File "C:\Users\Marcel\AppData\Local\Temp\ipykernel_6628\358323758.py", line 1 in <module>

Restarting kernel...

i got the following pyvista Report:

--------------------------------------------------------------------------------
  Date: Thu Feb 24 12:00:46 2022 Mitteleuropäische Zeit

                OS : Windows
            CPU(s) : 8
           Machine : AMD64
      Architecture : 64bit
               RAM : 31.7 GiB
       Environment : IPython
       File system : unknown
       GPU Details : None

  Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit
  (AMD64)]

           pyvista : 0.33.2
               vtk : 9.1.0
             numpy : 1.21.0
           imageio : 2.9.0
           appdirs : 1.4.4
            scooby : 0.5.11
        matplotlib : 3.4.2
             PyQt5 : 5.12.3
           IPython : 7.25.0
          colorcet : 1.0.0
             scipy : 1.7.0
              tqdm : 4.61.1

  Intel(R) Math Kernel Library Version 2020.0.4 Product Build 20200917 for
  Intel(R) 64 architecture applications
--------------------------------------------------------------------------------
banesullivan commented 2 years ago

Could you share the mesh file?

MarcelHeu commented 2 years ago

Thanks for your answer, here you go:

https://www.file-upload.net/download-14873962/waxup_slm_cad.stl.html

waxup_slm_cad.stl.zip

MarcelHeu commented 2 years ago

hey @banesullivan,

can you assume what could be the issue or do i have to pass maybe another parameter to the function?

best regards, Marcel

adeak commented 2 years ago

I can reproduce a segfault on my debian linux (which is the same thing as "Windows fatal exception: access violation" in your case). Here's the snippet I used, taken from your question on Stack Overflow:

import pymeshfix
import trimesh

model = trimesh.load("waxup_slm_cad.stl")

# Create object from vertex and face arrays
meshfix = pymeshfix.MeshFix(model.vertices, model.faces)

# Repair the mesh
meshfix.repair()

I don't know exactly what's going on, but I've investigated a bit:

  1. Your mesh reads fine, and according to pyvista.wrap(model) it's composed entirely of triangles, and .clean() does nothing with the mesh. This suggests no funny business with degenerate cells.
  2. The segfault happens deep inside the call stack, during a call to duplicateNonManifoldVertices().

Here's the layout of execution: meshfix.repair() -> https://github.com/pyvista/pymeshfix/blob/a7f91fcb8443604761b1f5c37386ae83942dc225/pymeshfix/meshfix.py#L207-L209

-> https://github.com/pyvista/pymeshfix/blob/a7f91fcb8443604761b1f5c37386ae83942dc225/pymeshfix/cython/_meshfix.pyx#L427-L429

(note that this is before the actual repairing starts; the segfault happens during tin.load_array())

https://github.com/pyvista/pymeshfix/blob/a7f91fcb8443604761b1f5c37386ae83942dc225/pymeshfix/cython/_meshfix.pyx#L214-L215

-> Basic_TMesh_wrap::loadArray() https://github.com/pyvista/pymeshfix/blob/a7f91fcb8443604761b1f5c37386ae83942dc225/pymeshfix/cython/meshfix.cpp#L319-L320

-> https://github.com/pyvista/pymeshfix/blob/a7f91fcb8443604761b1f5c37386ae83942dc225/pymeshfix/cython/checkAndRepair.cpp#L310-L323

We're getting close: the segfault happens on line 317, inside the call to Basic_TMesh::cutAndStitch():

https://github.com/pyvista/pymeshfix/blob/a7f91fcb8443604761b1f5c37386ae83942dc225/pymeshfix/cython/io.cpp#L1003-L1004

And the final destination is Basic_TMesh::duplicateNonManifoldVertices():

https://github.com/pyvista/pymeshfix/blob/a7f91fcb8443604761b1f5c37386ae83942dc225/pymeshfix/cython/checkAndRepair.cpp#L128-L169

The segfault seems to happen on line 138 of the above:

https://github.com/pyvista/pymeshfix/blob/a7f91fcb8443604761b1f5c37386ae83942dc225/pymeshfix/cython/checkAndRepair.cpp#L136-L138

Since I'm not familiar with the library I'm not going to try and reverse-engineer that data structure with somewhat opaque member names :) Hopefully this will help someone who knows the library figure out what's wrong.

chen112p commented 2 years ago

maybe this problem has been fixed. I didnt run into any issue loading the geometry with pyvista

adeak commented 2 years ago

@chen112p loading the mesh is fine. What breaks is repair() of pymeshfix, see my snippet in https://github.com/pyvista/pymeshfix/issues/41#issuecomment-1121573606.

MarioAuditore commented 1 year ago

Got similar error, while trying to repair my mesh. Any workarounds?

carbocation commented 1 year ago

For what it's worth, I get this error nondeterministically. When using faulthandler to try to localize it, I see the following:

Current thread 0x000000020b9b3a80 (most recent call first):
  File "/opt/homebrew/lib/python3.10/site-packages/pymeshfix/meshfix.py", line 201 in repair

That line number is the point at which clean_from_arrays gets called:

https://github.com/pyvista/pymeshfix/blob/ce69762a29602b2fb13b53df6e73ebddf7963b55/pymeshfix/meshfix.py#L201-L203

(Actually, I see this is the exact same problem that @adeak has already pinpointed more precisely.)

adeak commented 1 year ago

For what it's worth, I get this error nondeterministically.

Unfortunately segfaults inherently have a tendency to be nondeterministic. The reason for a segfault is that the process tries to access (actually, write to) memory that doesn't belong to it. When you have a minor off-by-few indexing issue you might end up addressing memory that is out of bounds for the given object (say, an array), but still within the memory allocated to your process. In this situation you can normally change the value at the corresponding memory address, so instead of a segfault you get silently corrupted state for your program (something somewhere changed accidentally). But when the referenced memory address is so far from the intended place (or when the constellation of memory allocation in a modern operating system is such) that it doesn't belong to the same process, you get the segfault.

MarioAuditore commented 1 year ago

Hello again. I managed to avoid this problem by using pyvista like this:

    pv_mesh = pv.wrap(model.get_mesh())
    pv_mesh.save('pyvista_mesh.ply')

    tin = PyTMesh(False)

    tin.load_file('pyvista_mesh.ply')
    tin.fill_small_boundaries(nbe=nbe, refine=refine)

P.S. model.get_mesh() is my class, which returns trimesh