Search code examples
c#colorsinterpolationvtkparaview

how to color unstructured grid (.vtu) in vtk?


My whole source code is present here including block.vtu file for testing purpose. I will discuss important functions here.

This code can color *.vtp, *.vtk. But same code fails to color *.vtu.

I have list of vtkactors. And I specify which actor to set properties from.

  1. This code which gets all column names from an actor:
public static List<string> getColumns(int actorIndex)
{
    List<string> colums = new List<string>();
    vtkActor actor = actors[actorIndex];
    var output = actor.GetMapper().GetInputAsDataSet();
    var cell = output.GetCellData();
    for (int i = 0; i < cell.GetNumberOfArrays(); i++)
    {
        var colm = cell.GetArrayName(i);
        if (colm != null) colums.Add(colm);
    }
    return colums; //['CU_OK','D1','Domain','IJK','IX','IY'...]
}
  1. This code colors the model of specific column:
public static void setColors(int actorIndex, string column)
{
    List<string> colums = new List<string>();
    vtkActor actor = actors[actorIndex];
    var output = actor.GetMapper().GetInputAsDataSet();
    var colors = vtkUnsignedCharArray.New();
    colors.SetNumberOfComponents(3);
    colors.SetName("Colors");

    var cell = output.GetCellData();
    for (int i = 0; i < cell.GetNumberOfArrays(); i++)
    {
        var cells = cell.GetArray(i);
        if (cells == null) continue;
        if (column != cell.GetArrayName(i)) continue;
        for (int j = 0; j < cells.GetNumberOfTuples(); j++)
        {
            var c = color_range((cells.GetComponent(i, j) / cell.GetArray(i).GetRange()[1]) * 100);
            var R = c.R;
            var G = c.G;
            var B = c.B;
            colors.InsertNextTuple3((double)R, (double)G, (double)B);
            colors.InsertNextTuple3((double)R, (double)G, (double)B);  //even tried with and without inserting second tuple
        }

    }
    output.GetPointData().SetScalars(colors); //even tried with GetCellData(), but no luck
}
  1. This code gets range(min,max) of values from specific column:
public static double[] getRange(int actorIndex, string column)
{
    List<string> colums = new List<string>();
    vtkActor actor = actors[actorIndex];
    var output = actor.GetMapper().GetInputAsDataSet();
    var cell = output.GetCellData();
    for (int i = 0; i < cell.GetNumberOfArrays(); i++)
    {
        var colm = cell.GetArrayName(i);
        var arr = cell.GetArray(i);
        if (column == colm)
        {
            if (arr == null) return null;
            return arr.GetRange(); // new double[] { min, max }
        }
    }
    return null;
}

ACTUAL OUTPUT for VTP and VTK (GOOD):

The result is fine. This is how drillholes are shown in by my code. colored drillhole

EXPECTED OUTPUT for VTU (GOOD):

See how its colored. This is what PARAVIEW software can show: Paraview Block Model

ACTUAL OUTPUT for VTU (BAD):

This is how my code show that model: My Block Model

I also tried:

I tried following question, but vtu color has no effect. I tried cell data too instead of point data. I also tried lookup table, it do shows with color scalebar but has no effect on model color.

The strange things are that:

  • all vtp files work (to color)and all vtu files fails
  • vtu file show colorful fine on paraview, but not in my code

Solution

  • I am not sure if this is a proper solution, but it might work if you create a new mapper at the end of the setColors function.

                output.GetPointData().SetScalars(colors);
    
    +           vtkDataSetMapper mapper = vtkDataSetMapper.New();
    +           mapper.SetInput(output);
    +           actor.SetMapper(mapper);
            }