vtkmodules.numpy_interface.vtk_aos_array#

VTKAOSArray — numpy-compatible mixin for vtkAOSDataArrayTemplate.

This module provides the VTKAOSArray mixin class and registers overrides for all vtkAOSDataArrayTemplate instantiations and concrete AOS subclasses (vtkFloatArray, vtkDoubleArray, etc.) so that AOS arrays coming from VTK automatically have numpy-compatible operations. The single contiguous buffer is exposed as a zero-copy numpy array view.

Module Contents#

Classes#

VTKAOSArray

A numpy-compatible view of VTK’s array-of-structures data arrays.

Functions#

_reshape_for_broadcast

Reshape for VTK-style broadcasting between arrays of different ndim.

_aos_to_ndarray

Convert a VTKAOSArray to its cached ndarray view.

_aos_to_ndarray_arg

Convert VTKAOSArray to ndarray, recursing into lists/tuples.

_register_aos_overrides

Register VTKAOSArray mixin as override for all AOS template types.

Data#

API#

vtkmodules.numpy_interface.vtk_aos_array._UNINITIALIZED#

‘object(…)’

vtkmodules.numpy_interface.vtk_aos_array._reshape_for_broadcast(a, b)#

Reshape for VTK-style broadcasting between arrays of different ndim.

When a 1-D array of length n is combined with a 2-D array of shape (n, k), numpy’s standard broadcasting would fail because it aligns shapes from the right: (n,) vs (n, k) becomes (1, n) vs (n, k). VTK arrays expect per-tuple scalars to broadcast across components, so we reshape (n,) to (n, 1) instead.

When the 1-D length does not match the leading dimension of the other operand (e.g. a reduction result of shape (k,) against (n, k)), we leave the shapes alone and let numpy handle it with its standard right-aligned broadcasting rules.

vtkmodules.numpy_interface.vtk_aos_array._aos_to_ndarray(x)#

Convert a VTKAOSArray to its cached ndarray view.

vtkmodules.numpy_interface.vtk_aos_array._aos_to_ndarray_arg(arg)#

Convert VTKAOSArray to ndarray, recursing into lists/tuples.

class vtkmodules.numpy_interface.vtk_aos_array.VTKAOSArray(*args, **kwargs)#

Bases: vtkmodules.numpy_interface._vtk_array_mixin.VTKDataArrayMixin

A numpy-compatible view of VTK’s array-of-structures data arrays.

VTK stores most numeric data in AOS (array-of-structures) layout: all components of a tuple are contiguous in memory. This mixin exposes the underlying buffer as a zero-copy numpy array view, so that indexing, slicing, and arithmetic work without copying data.

This mixin is automatically applied to vtkAOSDataArrayTemplate instantiations and all concrete AOS subclasses (vtkFloatArray, vtkDoubleArray, vtkIntArray, etc.). Any AOS array that crosses from C++ to Python is already a VTKAOSArray instance.

Properties

shape : tuple of int (ntuples,) for 1-component, (ntuples, ncomps) otherwise. dtype : numpy.dtype The element data type. dataset : vtkDataSet or None The owning VTK dataset, when the array is attached to one. association : int or None The attribute association (POINT, CELL, etc.).

Zero-Copy Access

The internal VTK buffer is shared with numpy — no data is copied when you read array values::

arr = polydata.point_data["Normals"]
normals = np.asarray(arr)   # zero-copy view
normals[:, 2] = 0           # modifies VTK data in place

Memory Safety

The mixin observes BufferChangedEvent from VTK. If the underlying buffer is reallocated (e.g. by SetNumberOfTuples), the cached numpy view is automatically invalidated to prevent use-after-free.

Arithmetic

Element-wise operations (+, -, *, /, comparisons, ufuncs) produce new VTK-backed AOS arrays with metadata (dataset, association) propagated. Reductions (sum, mean, etc.) return plain scalars or numpy arrays.

NumPy Integration

  • numpy.asarray(arr) returns the zero-copy view directly.

  • All numpy ufuncs and __array_function__ calls are supported.

  • The array implements the buffer protocol (PEP 3118).

Examples

Arrays from VTK are already the right type::

arr = polydata.point_data["velocity"]
print(arr.shape)           # (n, 3)
print(np.sum(arr, axis=0)) # per-component sum

Create from numpy::

from vtkmodules.util.numpy_support import numpy_to_vtk
vtk_arr = numpy_to_vtk(my_array)

See Also

VTKSOAArray : Mixin for structure-of-arrays VTK arrays.

Initialization

_init_from_data(data)#

Populate this array from an iterable (list, tuple, numpy array, etc.).

The data is converted to match this array’s VTK data type. 1-D and 2-D inputs are supported; for 2-D inputs the second dimension sets the number of components.

property _array_view#
_init_array_view()#

Extract a zero-copy numpy array view from the VTK buffer.

_setup_observer()#

Set up BufferChangedEvent observer for memory safety.

property dtype#
property nbytes#
__array__(dtype=None, copy=None)#
__array_ufunc__(ufunc, method, *inputs, **kwargs)#
__array_function__(func, types, args, kwargs)#
__getitem__(index)#
__setitem__(index, value)#
_binop(other, op)#
_rbinop(other, op)#
__add__(other)#
__radd__(other)#
__sub__(other)#
__rsub__(other)#
__mul__(other)#
__rmul__(other)#
__truediv__(other)#
__rtruediv__(other)#
__floordiv__(other)#
__rfloordiv__(other)#
__pow__(other)#
__rpow__(other)#
__mod__(other)#
__rmod__(other)#
__lt__(other)#
__le__(other)#
__eq__(other)#
__ne__(other)#
__ge__(other)#
__gt__(other)#
__neg__()#
__abs__()#
__iter__()#
__repr__()#
__str__()#
static _from_array(narray)#

Create a VTK-backed AOS array from a numpy array.

Returns a VTKAOSArray-overridden VTK array with the numpy data. The VTK array holds a reference to the numpy array to prevent GC.

vtkmodules.numpy_interface.vtk_aos_array._register_aos_overrides()#

Register VTKAOSArray mixin as override for all AOS template types.