jimy-byerley / pymadcad

Simple yet powerful CAD (Computer Aided Design) library, written with Python.
https://madcad.netlify.app/
GNU Lesser General Public License v3.0
205 stars 15 forks source link

mesh display not working with `numpy>=1.25` #85

Closed jimy-byerley closed 1 year ago

jimy-byerley commented 1 year ago

As reported by @bourbonut , with any numpy following 1.25 we are getting the following backtrace when rendering a mesh with eg. show

tried to display <madcad.mesh.mesh.Mesh object at 0x7f18a89c6bc0>
Traceback (most recent call last):
  File "/home/nut/github/pymadcad/madcad/rendering/base.py", line 282, in dequeue
    self.displays[key] = self.display(displayable, self.displays.get(key))
  File "/home/nut/github/pymadcad/madcad/rendering/base.py", line 364, in display
    disp = obj.display(self)
  File "/home/nut/github/pymadcad/madcad/mesh/mesh.py", line 668, in display
    return MeshDisplay(scene,
  File "/home/nut/github/pymadcad/madcad/rendering/d3/mesh.py", line 25, in __init__
    self.disp_faces = FacesDisplay(scene, self.vertices, normals, faces, color=color, reflect=reflect, layer=0)
  File "/home/nut/github/pymadcad/madcad/rendering/d3/mesh.py", line 338, in __init__
    self.vb_faces = scene.ctx.buffer(np.array(faces, 'u4', copy=False))
  File "/home/nut/.pyenv/versions/qt/lib/python3.10/site-packages/moderngl/__init__.py", line 1732, in buffer
    res.mglo, res._size, res._glo = self.mglo.buffer(data, reserve, dynamic)
ValueError: ndarray is not C-contiguous
Exception ignored in: <function FacesDisplay.__del__ at 0x7f1877890940>
Traceback (most recent call last):
  File "/home/nut/github/pymadcad/madcad/rendering/d3/mesh.py", line 357, in __del__
    if self.va:
AttributeError: 'FacesDisplay' object has no attribute 'va'

This is because unstructured_to_structured was before copying the source array and producing a contiguous array. Since numpy 1.25.0, is is producing a view when field spacing makes it possible. this is an optimization on their side but produces non-contiguous buffers so moderngl doesn't accept it.