Reputation: 374
I am using VTK python bindings to color a large point cloud based on the height of the points. I am able to color them properly using vtk.vtkLookupTable
, and looping through all the points and getting color based on z value using GetColor
function, but looping through large number of points is very slow.
Below mentioned code will show how i did that
Code :
lookUpTable = vtk.vtkLookupTable()
lookUpTable.SetTableRange(minz,maxz)
lookUpTable.Build()
# Create a unsigned char array to color
uCharArray = vtk.vtkUnsignedCharArray()
uCharArray.SetNumberOfComponents(3)
uCharArray.SetName("colors")
After we have created LookupTable
and UnsignedCharArray
, i am looping through all the points and filling up the above defined array.
# Assign color by extracting each color
for i in range(outputPolyData.GetNumberOfPoints()):
point = outputPolyData.GetPoint(i)
# Get the color from lookup table
color = [0]*3
lookUpTable.GetColor(point[2],color)
# Convert each color to 255
for j in range(len(color)):
color[j] = int(255 * color[j])
uCharArray.InsertTypedTuple(i,color)
# # Set Scalars
outputPolyData.GetPointData().SetScalars(uCharArray)
But to color 1000000
points, its taking 3.180 seconds. I actually have 8000000 points. It would take a lot of time if i loop over every point to assign color.
I am pretty sure there would be other way to do this. If any one can point me in the right direction, i would be glad.
Upvotes: 1
Views: 1317
Reputation: 374
I solved it using MapScalars
function in vtkLookupTable
. This function is used inside the class to generate lookup tables.
I passed the Z
array to the function, it gave me an array of colors.
array = lookUpTable.MapScalars(vtkzArray,vtk.VTK_COLOR_MODE_DEFAULT,-1)
outputPolyData.GetPointData().SetScalars(array)
Here vtkzArray
should be of type vtkDataArray
. I converted numpy array to vtkDataArray
using vtk_to_numpy
from numpy_support class.
Upvotes: 3