vtkAppendPolyData java problem

Dear all,

I am new to using vtk, please accept my apologies for any silly question. I am using java on windows 10 64b platform and I am currently trying to append two vtk files having the same structure: unstructured grid with equal number of cells and cell data arrays. Here is the piece of code which fails:

vtkAppendPolyData append1 = new vtkAppendPolyData();

vtkUnstructuredGridReader reader1 = new vtkUnstructuredGridReader();
reader1.SetFileName(filename1);
reader1.ReadAllScalarsOn();
reader1.Update();

vtkUnstructuredGridReader reader2 = new vtkUnstructuredGridReader();
reader2.SetFileName(filename2);
reader2.ReadAllScalarsOn();
reader2.Update();

append1.AddInputData(reader1.GetOutput());
append1.AddInputData(reader2.GetOutput());
append1.Update();

System.out.println(append1.GetOutput().GetCellData().GetNumberOfArrays());
System.out.println(reader1.GetOutput().GetCellData().GetNumberOfArrays());
System.out.println(reader2.GetOutput().GetCellData().GetNumberOfArrays());

These last calls return:
0
8
8

This seems to me that append fails. Note that if I only add one of the two files, the correct number of arrays is returned. I could not find other threads on this, at least they did not allow me to solve the issue so far.

Many thanks

Do the two unstructured grids loaded in have the same cell array names?

Thank you for your answer. The two unstructured grids have same number of cell data arrays of same size but with different names. Under Paraview the appendattribute filter works well, this is what I would like to do in my java program.

Ah! ParaView uses a custom class (not available in VTK) called vtkMergeArrays. This combines cell arrays with different names into the output dataset. vtkAppendPolyData, on the other hand, assumes that the inputs have arrays with the same names - if they do not, the arrays are not copied to the output. Moreover, you would want to use vtkAppendDataSet - vtkAppendPolyData only handles polydata inputs while your readers are producing vtkUnstructuredGrid.

For your application, you’ll need to combine the cell data arrays manually. Something like

vtkUnstructuredGridReader reader1 = new vtkUnstructuredGridReader();
reader1.SetFileName(filename1);
reader1.ReadAllScalarsOn();
reader1.Update();

vtkUnstructuredGridReader reader2 = new vtkUnstructuredGridReader();
reader2.SetFileName(filename2);
reader2.ReadAllScalarsOn();
reader2.Update();

vtkUnstructuredGrid combined = new vtkUnstructuedGrid();
combined.DeepCopy(reader1.GetOutput());
vktCellData cd = combined.GetCellData();
vtkUnstructuredGrid ug2 = reader2.GetOutput());
vtkCellData ug2cd = ug2.GetCellData();
for (int i = 0; i < ug2cd.GetNumberOfArrays(); ++i)
{
  cd.AddArray(ug2cd.GetArray(i));
}

Then you can use combined later on in your pipeline. I haven’t tested the code, but it should be pretty close to what you need.

1 Like

Wow ! Thanks ! I will test this later on today but I have a pretty good feeling :slight_smile:

Works like a charm ! Thank you so much for your help :slight_smile:

1 Like