How do I create a colormap in VTK?
Question:
I created a simple unstructured square in VTK:
x = [0,10,0,10]
y = [0, 0, 10, 10]
z = [0,0,0,0]
data = np.asarray([x,y,z]).T
for i in range(0, len(x)):
points.InsertPoint(i, data[i])
quad = [2,3,1,0]
ugrid.InsertNextCell(vtk.VTK_QUAD, 4, quad)
ugrid.SetPoints(points)
Say I wanted to create a colormap of the temperature across the square. I know the temperature at the corners:
temp = [0,20,40,60]
How can I color the entire square knowing these values?
VTK provides one example to create a colormap in their tutorials (the ColoredElevationMap tutorial), however, I don’t completely understand it and I believe there is another simpler way to create a colormap in VTK that I don’t know of.
Answers:
You should add your data array and set it as the scalars (= default array to use in VTK, particularly for coloring)
temperature = vtk.vtkIntArray()
temperature.SetName("Temp")
temp = [00,20,40,60]
for t in temp:
temperature.InsertNextValue(t)
ugrid.GetPointData().SetScalars(temperature)
Then in the rendering part, this array will be used by default for coloration. You still have to update the color range:
mapper.SetScalarRange(ugrid.GetScalarRange())
You can take a look to this example (edit: update link thanks to @paulo-carvalho)
Adding to Nico Vuaille’s answer. The color of the map can be changed using a lookup table such as:
lut = vtk.vtkLookupTable()
lut.SetHueRange(0, 0)
lut.SetSaturationRange(0, 0)
lut.SetValueRange(0.2, 1.0)
lut.Build()
and
Mapper.SetLookupTable(lut)
I created a simple unstructured square in VTK:
x = [0,10,0,10]
y = [0, 0, 10, 10]
z = [0,0,0,0]
data = np.asarray([x,y,z]).T
for i in range(0, len(x)):
points.InsertPoint(i, data[i])
quad = [2,3,1,0]
ugrid.InsertNextCell(vtk.VTK_QUAD, 4, quad)
ugrid.SetPoints(points)
Say I wanted to create a colormap of the temperature across the square. I know the temperature at the corners:
temp = [0,20,40,60]
How can I color the entire square knowing these values?
VTK provides one example to create a colormap in their tutorials (the ColoredElevationMap tutorial), however, I don’t completely understand it and I believe there is another simpler way to create a colormap in VTK that I don’t know of.
You should add your data array and set it as the scalars (= default array to use in VTK, particularly for coloring)
temperature = vtk.vtkIntArray()
temperature.SetName("Temp")
temp = [00,20,40,60]
for t in temp:
temperature.InsertNextValue(t)
ugrid.GetPointData().SetScalars(temperature)
Then in the rendering part, this array will be used by default for coloration. You still have to update the color range:
mapper.SetScalarRange(ugrid.GetScalarRange())
You can take a look to this example (edit: update link thanks to @paulo-carvalho)
Adding to Nico Vuaille’s answer. The color of the map can be changed using a lookup table such as:
lut = vtk.vtkLookupTable()
lut.SetHueRange(0, 0)
lut.SetSaturationRange(0, 0)
lut.SetValueRange(0.2, 1.0)
lut.Build()
and
Mapper.SetLookupTable(lut)