Hi All,
The following code creates polygons from point and adds polygons. I eventually write this appended data to a vtk file. I want to color these polygons to a specific RGB color so that when I load the vtk file in Paraview glance, I can see the colors instead of solid colors. I have checked several examples on and I learned that the way to assign colors is to use a lookuptable and a mapper. Now the question is, Can I write a mapper to a vtk file or I should not think about mappers at all in this case?
def create_polygon(points):
"""Create a vtk Polygon from a list of points.
The point will have to be in an order. This function cannot be used to create a
polygon with holes.
Args:
points: A list of points. Here, each point is a list of X, Y, and Z cordinate.
Returns:
A tuple with two elements.
- vtk_points: A list of vtk point objects
- vtk_polygon: A vtk polygon object created from the points provided.
"""
vtk_points = vtk.vtkPoints()
vtk_polygon = vtk.vtkPolygon()
vtk_polygon.GetPointIds().SetNumberOfIds(len(points))
for point in points:
vtk_points.InsertNextPoint(tuple(point))
for i in range(len(points)):
vtk_polygon.GetPointIds().SetId(i, i)
return vtk_points, vtk_polygon
def create_polygons(points_lst):
"""Create a vtk object with multiple vtk polygons.
Args:
points_lst: A list of lists of points. Here, each point is a list of X, Y,
and Z cordinate.
Returns:
A vtk object that has multiple vtk polygons.
"""
vtk_polydata_lst = []
for i in range(len(points_lst)):
# Create a list of vtk polydata objects with polygons
vtk_points, vtk_polygon = create_polygon(points_lst[i])
vtk_polygons = vtk.vtkCellArray()
vtk_polygons.InsertNextCell(vtk_polygon)
vtk_polydata = vtk.vtkPolyData()
vtk_polydata.SetPoints(vtk_points)
vtk_polydata.SetPolys(vtk_polygons)
vtk_polydata.Modified()
vtk_polydata_lst.append(vtk_polydata)
# Create a vtk object with multiple polygons
vtk_polydata_extended = vtk.vtkAppendPolyData()
for i in range(len(vtk_polydata_lst)):
vtk_polydata_extended.AddInputData(vtk_polydata_lst[i])
vtk_polydata_extended.Update()
return vtk_polydata_extended