Can't color a XML Unstructured Grid (*.vtu)

Hi I have code, which 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.

This code which gets all column names from actor index:

        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;
         }

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);
                }

            }
            output.GetPointData().SetScalars(colors);
        }

This code gets range 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();
                }
            }
            return null;
        }

Result looks like this for *.vtp or *.vtk (GOOD):
Screenshot 2022-05-29 213601

Result for *.vtu in paraview (GOOD):

But result for *.vtu in my code (BAD):

Please let me know whats I’m doing wrong.

I think the problem is that you are appending gray colors gere in the code.
And I don’t see where you input your colors array in the setColors function

Hope it helps

Mquro

1 Like

Thanks alot for reply.

Sorry I forgot to explain what color_range is.
It has following syntax: Color color_range(double Percentage)
I specify percentage as

(cells.GetComponent(i, j) / cell.GetArray(i).GetRange()[1]) * 100

and it returns color range from blue to red.
Its filled inside c variable.
For interpolation, I use following code: Color Interpolation Between 3 Colors in .NET - Stack Overflow

Even if I override c and set its value as c = Color.Blue, it would show the same gray color.

But strangely, the same code works fine for vtp, vtk.
Just it fails for vtu files.

I guess the imterpolation maybe overflowing and don’t need that multiply by 100.

And I don’t know why put this line twice:

colors.InsertNextTuple3

Hope it helps

Mauro

1 Like

I have checked colors and are interpolated fine.
Please see color scale in image. Its my own label made, and uses that same color.
I verified following way:

                    var R = c.R;
                    var G = c.G;
                    var B = c.B;
                    Console.WriteLine("rgb(" + R + "," +G + "," + B +")");
/*
rgb(108,89,176) purple like
rgb(132,95,168) purple like
rgb(157,102,160) pink like
rgb(181,108,152) pink like
rgb(206,115,144) pink like
rgb(230,121,136) red like
rgb(255,128,128) red like
rgb(59,76,192) blue like
*/

I’m not clear about colors.InsertNextTuple3.
I started calling it twice, as vtp files working with twice and else raises unsafe errors.
But calling once or twice has no effect on VTU currently.

I think you fprgot to set colors in the mapper

cylinderActor->GetProperty()->SetColor(
      colors->GetColor4d("Tomato").GetData());
1 Like

Although Actor.GetProperty().SetColor(color); works to color the model.
But it color whole model with same color.
Whereas I expected to interpolate colors according to percentages.

Please see:

You need to set an array for each cell

1 Like

Sorry sir, I didn’t understood.
Please can you elaborate a bit more or specify tiny example ?

Here is an example that tells you how to do it… But if you need more help, let me know

Note: I think you should add scalars to the cells instead of the points, but more or less it should be similar.
Experiment with the lookup table definition, you only need two values “red” and “blue” defined as vectors as you were doing.

1 Like

Hi sir, Sorry for late reply.

I tried following article and their resources, 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 and all vtu files are fails
  • vtu file show colorful fine on paraview, but not in my code

I guess we need to handle vtu differently than normally we do.

Thanks for so much.