Point data error when visualize the difference between two volumetric meshes

Hi,

I have two volumetric liver meshes in vtk unstructured grid format (ground-truth and predicted versions) and I’d like to plot the error, i.e., the difference between the ground-truth and predicted liver meshes, on the predicted mesh / mesh coordinates with a different colour (colour code based on displacement magnitudes). The N-th point in the ground-truth mesh correspond to the N-th point in the predicted mesh.

I have attempted to do this by following script however I got an error message as attached below.

Code:

>>> gtMesh = getNode('gtMesh')
>>> predMesh = getNode('predMesh')
>>> diff_arr = gtMesh - predMesh
>>> gtMesh_np_arr = slicer.util.arrayFromModelPoints(gtMesh)
>>> predMesh_np_arr = slicer.util.arrayFromModelPoints(predMesh)
>>> diff_arr = gtMesh_np_arr - predMesh_np_arr
>>> slicer.util.arrayFromModelPointData(gtMesh, diff_arr)

Error Message:

You need to create a vtk.vtkDoubleArray, fill it with zeros, set its name, and add it as a point data array to the polydata. You can then modify it using slicer.util.arrayFromModelPointData as you did it above.

Meshes are volumetric and have an unstructured grid vtk format. In that case, how can I assign a poly data object to these meshes, given that they are not surface meshes?

I just created poly data object as below with a vtk.vtkDoubleArray, fill it with zeros. However, I cannot add that to the volumetric mesh. Should I extract surface mesh from the volumetric mesh first ?

gtMesh = getNode('gtMesh')
predMesh = getNode('predMesh')
diff_arr = gtMesh - predMesh
gtMesh_np_arr = slicer.util.arrayFromModelPoints(gtMesh)
predMesh_np_arr = slicer.util.arrayFromModelPoints(predMesh)
diff_arr = gtMesh_np_arr - predMesh_np_arr

shape = np.shape(gtMesh_np_arr)
zeros_np_arr = np.zeros(shape)
vtkdubarr.SetArray(zeros_np_arr, 0, 1)

gtMeshPolyData = gtMesh.GetPolyData()
gtMeshPolyData.GetPointData().AddArray(vtkdubarr)

gtMeshPolyData = vtk.vtkPolyData()
gtMeshPolyData.GetPointData().AddArray(vtkdubarr)
gtMeshPolyData.GetPointData().SetActiveScalars("vtkdubarr")

If you work with unstructured grid then you can use the more generic gtMesh.GetMesh() method.

I got poly data from the GetMesh() function, but when I tried slicer.util.arrayFromModelPointData by passing the mesh and the displacement field array, I got ‘only integer scalar arrays can be converted to a scalar index’.

slicer.util.arrayFromModelPointData does not accept the numpy arrays as an arguement?

Code:

gtMesh = getNode('gtMesh')
predMesh = getNode('predMesh')
diff_arr = gtMesh - predMesh
gtMesh_np_arr = slicer.util.arrayFromModelPoints(gtMesh)
predMesh_np_arr = slicer.util.arrayFromModelPoints(predMesh)
diff_arr = gtMesh_np_arr - predMesh_np_arr

shape = np.shape(gtMesh_np_arr)
zeros_np_arr = np.zeros(shape)
vtkdubarr.SetArray(zeros_np_arr, 0, 1)

gtMesh.GetMesh().GetPointData().AddArray(vtkdubarr)
gtMesh.GetMesh().GetPointData().SetActiveScalars("vtkdubarr")

slicer.util.arrayFromModelPointData(gtMesh, diff_arr)

Error Message:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\mnwoki\AppData\Local\NA-MIC\Slicer 5.1.0-2022-07-08\bin\Python\slicer\util.py", line 1722, in arrayFromModelPointData
    arrayVtk = _vtkArrayFromModelData(modelNode, arrayName, 'point')
  File "C:\Users\mnwoki\AppData\Local\NA-MIC\Slicer 5.1.0-2022-07-08\bin\Python\slicer\util.py", line 1706, in _vtkArrayFromModelData
    arrayVtk = modelData.GetArray(arrayName)
TypeError: GetArray argument 1: only integer scalar arrays can be converted to a scalar index

Please upload a sample “gtMesh” and “predMesh” files somewhere and post the download link here so that I can test your code snippet.

Can I ask a posibble solution for this please ? Aything missing with the shared files ?

Hi Lassoan,

Could you please provide an update on this matter? Is there anything I’m doing wrong in the code?

I’m still having this problem and am unable to resolve it. Any suggestions, please?