question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transferring PolyData Faces into a Numpy Array

See original GitHub issue

A fast way to transfer the face data, as stored in the Polydata object, into a Numpy object array containing the indexes of all the points of each simplex.

This should be a fairly simple for loop in C, I would add it myself, but I A) don’t know C yet, and B) don’t know how to integrate C and Python. However, the pseudocode in Python could look something like this:

simplexes = []
index = 0
while index < face_array.shape[0]:
    number_of_points = face_array[index]
    points = face_array[index+1:index+1+number_of_points]
    simplexes.append(points)```

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
darikgcommented, Mar 11, 2022

It’d definitely be nice to have an official solution for this.

Maybe:

from typing import List
from pyvista import _vtk, PolyData
from numpy import split, ndarray

def face_arrays(mesh: PolyData) -> List[ndarray]:
    cells = mesh.GetPolys()
    c = _vtk.vtk_to_numpy(cells.GetConnectivityArray())
    o = _vtk.vtk_to_numpy(cells.GetOffsetsArray())
    return split(c, o[1:-1])
2reactions
banesullivancommented, Mar 13, 2022

For meshes with uniform cell types (all tirangles or all quads), we could modify @darikg’s to the following wich would return an ndarray with int64 dtype

from typing import List
from pyvista import _vtk, PolyData
from numpy import split, ndarray

def face_arrays(mesh: PolyData) -> List[ndarray]:
    cells = mesh.GetPolys()
    c = _vtk.vtk_to_numpy(cells.GetConnectivityArray())
    o = _vtk.vtk_to_numpy(cells.GetOffsetsArray())
    faces = split(c, o[1:-1])
    if len(np.unique(np.diff(o))) == 1:
        return np.array(faces)
    return faces
>>> import pyvista as pv
>>> face_arrays(pv.Sphere()).dtype
dtype('int64')
Read more comments on GitHub >

github_iconTop Results From Across the Web

pyvista.PolyData — PyVista 0.37.0 documentation
The line connectivity array. Like faces , this array requires padding indicating the number of points in a line segment. For example, the...
Read more >
Reading a .VTK polydata file and converting it into Numpy array
You can use dataset_adapter from vtk.numpy_interface : from vtk.numpy_interface import dataset_adapter as dsa polydata = reader.
Read more >
Import Surface data from Pyvista (Vertices, Face)
PolyData (arr_3d) surf = cloud.delaunay_2d() verts = surf.points # Dim: ... As you note napari above is expecting faces in an Nx3 array....
Read more >
Working With Numpy Arrays - the Vascular Modelling Toolkit
Cell Data. In order to make use of the data in numpy/python, we create a unique numpy array for every VTK array defining...
Read more >
Converting your NumPy arrays to VTK arrays and files
Well, as if there weren't enough deterrents in employing VTK, you will quickly realize that using your precious data – which let's face...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found