So here’s how to use this function assuming you mean to export an numpy.ndarray object named NumPy_data of type float32 : NumPy_data_shape = NumPy_data.shape However, all that one needs to do is create a 1D representation of the array using ‘numpy.ndarray’ methods such as flatten or ravel. At least that’s what I thought (yeah, yeah, I suck). Upon first inspection, one might think that 3D NumPy arrays weren’t possible to convert. num_array : a contiguous 1D or 2D, real numpy array. The numpy data is gc'd and VTK will point to garbage which will in WARNING: You must maintain a reference to the passed numpy array, if Such that the numpy array can be released. (shallow copy) and uses more memory but detaches the two arrays This is not as efficient as the default behavior If the second argument is set to 1, the array is deep-copied fromįrom numpy. This function is very efficient, so large arrays should not be a However, only 1, and 2 dimensional arrays are supported. This function only works for real arrays that are contiguous.Ĭomplex arrays are NOT handled. With the result being numpy_to_vtk(num_array, deep=0, array_type=None)Ĭonverts a contiguous real numpy Array to a VTK array object. Let us first inspect the docstring of the first function which can be accessed as follows, assuming you have VTK installed in your Python distro: from vtk.util import numpy_support The functions of interest to us are numpy_to_vtk and vtk_to_numpy. However, I’m here to try and elucidate their usage. Of course, given the near-absence of documentation and/or examples, using it is as convoluted as doing anything in VTK. So, given the popularity of Python and the fact that VTK is exposed in its near entirety to Python, the VTK folk decided to create the numpy_support module which resides under vtk.util. The second way is by means of exporting your data into VTK-readable files using the PyEVTK package, a way which as you’ll see is great if you want to process and/or visualize that data in VTK-based applications. The first way is using the _support module that comes with VTK and allows you to ‘easily’ convert your data. Since, looping in Python must be avoided like the black plague I will be focusing on the two ways I prefer. The traditional/ugly way, is creating new VTK objects, setting a bunch of properties like dimensions etc, and looping over your NumPy data to copy and populate your new objects. C++ is about putting hair on your chest :). And why would it be? VTK was made in C++ and C++ isn’t about ease-of-use and concise programing. Well, as if there weren’t enough deterrents in employing VTK, you will quickly realize that using your precious data – which let’s face it – will be stored in NumPy ndarray objects, with VTK ain’t all that straightforward. Now lets say you were convinced (ha!) and decided to start including VTK in your scripts for visualization and processing. I wrote, or rather ranted, in my previous post about the value of VTK. In this post I will show how to ‘convert’ NumPy arrays to VTK arrays and files by means of the _support module and the little-known PyEVTK package respectively.
0 Comments
Leave a Reply. |