Pete - 4 months ago 31

Python Question

The following code snippet configures a python/VTKFloatArray for future filling with vertices coordinates of the unit cube. 8 tuples of 3 components (x,y,z) are set. It seems that the setters do not commute. Is this the expected behaviour ? The number of components seems to have to be set first. Could anyone reproduce this ? Thanks for your answer

`import vtk`

import numpy as np

from itertools import product as itprod

vertices = np.array(list(itprod([0, 1], repeat=3)))

print vertices.shape[0] //8 vertices

print vertices.shape[1] //3 coordinates x-y-z

array = vtk.vtkFloatArray()

array.SetNumberOfComponents(vertices.shape[1])

array.SetNumberOfTuples(vertices.shape[0])

print array // number of tuples is 8, number of components is 3 OK

array = vtk.vtkFloatArray()

array.SetNumberOfTuples(vertices.shape[0])

array.SetNumberOfComponents(vertices.shape[1])

print array // number of tuples is 2 number of components is 3 WRONG

Answer

VTK is always a fickle thing, especially when it comes to documentation. I found some information on `SetNumberOfTuples`

and `SetNumberOfComponents`

at the corresponding links.

The former (`SetNumberOfTuples`

):

Set the number of tuples (a component group) in the array.

Note that this may allocate space depending on the number of components. [...]

The latter (`SetNumberOfComponents`

):

Set/Get the dimension (n) of the components.

Must be >= 1. Make sure that this is set before allocation.

As I see it, the former may allocate space, but the latter has to be called before any allocation. So, they indeed do not commute, you need to call the latter first, and that should be the working order (which is in line with your results).

The links obviously don't correspond to the python implementation, but the fact that the C++ version is not supposed to commute, you should not expect commutativity in python either.