vtkMapper Interpolate Scalars Before Mapping does not work as expected

vtkUnstructedGrid scalar color does not match when SetInterpolateScalarsBeforeMapping(1) and SetInterpolateScalarsBeforeMapping(0)
here is the test code, this vtu file displays right in paraview like this.


void main(int argc, char* argv[])
{
	// Create the reader for the data.
	std::string filename = "2.vtu";
	std::cout << "Loading " << filename.c_str() << std::endl;
	vtkNew<vtkXMLUnstructuredGridReader> reader;
	reader->SetFileName(filename.c_str());
	reader->Update();
	vtkDataSet* data = reader->GetOutput();
	double range[2]{ 0 };
	data->GetPointData()->GetArray("Order 1")->GetRange(range, 3);
	vtkNew<vtkLookupTable> lt;
	lt->SetRange(range);
	lt->SetNumberOfColors(15);
	lt->Build();

	vtkNew<vtkDataSetMapper> mapper;
	mapper->SetLookupTable(lt.GetPointer());
	mapper->SetInputConnection(reader->GetOutputPort());
	mapper->SetScalarVisibility(true);
	mapper->SetScalarModeToUsePointFieldData();
	mapper->ColorByArrayComponent("Order 1", 3);
	mapper->SetScalarRange(range);
	mapper->SetInterpolateScalarsBeforeMapping(1);
	mapper->Update();
	vtkNew<vtkNamedColors> colors;
	vtkNew<vtkActor> actor;
	actor->SetMapper(mapper.GetPointer());
	vtkNew<vtkRenderer> renderer;
	renderer->AddActor(actor.GetPointer());
	renderer->SetBackground(colors->GetColor3d("Wheat").GetData());
	renderer->UseHiddenLineRemovalOn();
	vtkNew<vtkRenderWindow> renderWindow;
	renderWindow->AddRenderer(renderer.GetPointer());
	renderWindow->SetSize(640, 480);

	vtkNew<vtkRenderWindowInteractor> interactor;
	interactor->SetRenderWindow(renderWindow.GetPointer());
	renderer->ResetCamera();
	renderer->ResetCameraClippingRange();
	renderWindow->Render();
	interactor->Start();
}

here is the result when SetInterpolateScalarsBeforeMapping(1)

but it should be like this SetInterpolateScalarsBeforeMapping(0)

So is this a bug or something wrong with my code?
Need help and thanks.

Do you have solve this? if do, how to solve this? Thanks!

Hi,
Did you have any luck with this? I am facing a similar error.

This is what I get before using InterpolateScalarsBeforeMappingOn()

and this is what happens after

Below is the Python code I am using

#Extract vorticity
mesh = gradient_filter.GetOutput()

#Create a lookup table
lookup_table = vtkLookupTable()
lookup_table.SetRange(-0.5, 0.5)
lookup_table.SetHueRange(0.6667,0.0)
lookup_table.Build()
print(f"{lookup_table}")

#Create a mapper and actor
mapper = vtkDataSetMapper()
mapper.SetLookupTable(lookup_table);
mapper.SetInputData(mesh)
mapper.ColorByArrayComponent(“Vorticity”,2)
mapper.SetScalarRange([-0.5,0.5])
mapper.SetScalarModeToUsePointFieldData()
#mapper.SetInterpolateScalarsBeforeMapping(1)
mapper.InterpolateScalarsBeforeMappingOn()
mapper.Update()

#Check the scalar range of “Vorticity”
point_data = mesh.GetPointData()
vorticity_array = point_data.GetArray(“Vorticity”)
print(f"{point_data.GetArray(‘Vorticity’)}“)
min_value = vorticity_array.GetRange(2)[0]
max_value = vorticity_array.GetRange(2)[1]
print(f"Vorticity Scalar Range: {min_value} to {max_value}”)
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetRepresentationToSurface()

#Create a renderer and set up a background color
renderer = vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(1.0, 1.0, 1.0) # Set background to white

#Create a render window
render_window = vtkRenderWindow()
#render_window.SetOffScreenRendering(1)
render_window.AddRenderer(renderer)
render_window.SetSize(1080, 1080)
camera = renderer.GetActiveCamera()
camera.SetPosition(0, 0, 30) # Position of the camera
camera.SetFocalPoint(0, 0, 0) # Focus on the center of the object

#Render the scene
render_window.Render()

You almost got it right. Ask the mapper to use the scalar range already given to the lookup table instead of using the mapper’s default ScalarRange which is [0,1]. This is why everything is navy blue in your screenshot.

mapper->SetUseLookupTableScalarRange(true);

I am facing the issue even with using SetUseLookupTableScalarRange(true) flag.

Can you please share dataset corresponding to the mesh in your code?

MESH.DAT (3.5 MB)
4475000.B (1.4 MB)
postprocess_2D.so (47.0 KB)
postprocess_2D.py (3.8 KB)

I have a MESH.DAT file which I read using Tecplot reader. My dataset (.B file) contain arrays which are read through a Fortran code wrapped in Python library. I add those arrays to my mesh block and then create a rendered scene. I have also attached the code and library I am using.

Thank you for your time :slight_smile:

Could you instead send a .vtu file please? .so shared libs are not easily redistributable, it won’t work on my machine.

Instead, can you modify your postprocess_2D.py to generate a .vtu file? add these lines after you get output of gradient filter and share the mesh.vtu file here.

# Extract the gradients of u and v from the gradient filter output
mesh = gradient_filter.GetOutput()
writer = vtkXMLUnstructuredGridWriter()
writer.SetInputData(mesh)
writer.SetFileName("mesh.vtu")
writer.Update()

output.vtu (2.7 MB)
This is the vtu file. Although, the interpolation works alright when I load this in ParaView.

Hi!
Any luck with the data?

Yes, you have to additionally set the vector component on the lookup table so that it generates texture coordinates correctyl.

Call lookup_table.SetVectorComponent(2) anytime before rendering. It doesn’t need to be done before the lookup table is built.

Complete code that uses vtk >= 9.4.0

import vtk

reader = vtk.vtkXMLUnstructuredGridReader(file_name="./output.vtu")

#Create a lookup table
lut = vtk.vtkLookupTable(range=(-0.5, 0.5), hue_range=(0.6667, 0.0))
lut.Build()

mapper = vtk.vtkDataSetMapper(
    interpolate_scalars_before_mapping=True,
    lookup_table=lut,
    scalar_mode=vtk.VTK_SCALAR_MODE_USE_POINT_FIELD_DATA,
    use_lookup_table_scalar_range=True,)
mapper.ColorByArrayComponent("Vorticity", 2)
mapper.SetScalarModeToUsePointFieldData()

# create a connection from reader's output to mapper's input
reader >> mapper

actor = vtk.vtkActor(mapper=mapper)
#Create a renderer and set up a background color
renderer = vtk.vtkRenderer(background=(1, 1, 1))
renderer.AddActor(actor)

camera = renderer.GetActiveCamera()
camera.SetPosition(0, 0, 30) # Position of the camera
camera.SetFocalPoint(0, 0, 0) # Focus on the center of the object

#Create a render window
window = vtk.vtkRenderWindow(size=(1080, 1080))
window.AddRenderer(renderer)

# Ensure lookup table uses the correct vector component of "Vorticity"
lut.SetVectorComponent(2)

interactor = vtk.vtkRenderWindowInteractor(render_window=window)
interactor.Start()