Kitware / itk-vtk-viewer

2D / 3D web image, mesh, and point set viewer using itk-wasm and vtk.js
https://kitware.github.io/itk-vtk-viewer/
BSD 3-Clause "New" or "Revised" License
210 stars 64 forks source link

PolyData scalar colors incorrectly shown #299

Closed rubenverhack closed 4 years ago

rubenverhack commented 4 years ago

Thank you for the development of this great tool!

I'm new to VTK, but I was implementing a Python script that reads two VTk files, combines them using an AppendFilter and writes them to a file.

I have noticed that if I render the output file using the following script. The colors are rendered correctly. However, if I open the file using itk-vtk-viewer, then the whole object is a single color.

There might be an issue with how the file was created, so I added the script for combining the two vtk objects as well. object-.zip

import vtk

def read_vtk(path):
    reader = vtk.vtkPolyDataReader()
    reader.SetFileName(path)
    reader.ReadAllVectorsOn()
    reader.ReadAllScalarsOn()
    reader.Update()

    return reader
output = read_vtk('output.vtk')

# Create a mapper and actor
mapper = vtk.vtkPolyDataMapper()
# mapper.ScalarVisibilityOn()
# mapper.SetInputConnection(cleanFilter.GetOutputPort())
mapper.SetInputConnection(output.GetOutputPort())
mapper.SetColorModeToDirectScalars()

actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Create a renderer, render window, and interactor
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# Add the actors to the scene
renderer.AddActor(actor)
renderer.SetBackground(.3, .2, .1) #  Background color dark red

# Render and interact
renderWindow.Render()
renderWindowInteractor.Start()

Output in itk-vtk-viewer: image

Output in renderer: image

output.zip

Script to combine the two VTK files:

import vtk

def read_vtk(path):
    reader = vtk.vtkPolyDataReader()
    reader.SetFileName(path)
    reader.ReadAllVectorsOn()
    reader.ReadAllScalarsOn()
    reader.Update()

    return reader

path = "object1.vtk"
sphereSource = read_vtk(path)

sphereSource.Update()
Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Colors.SetName('Colors')
Cellarray = sphereSource.GetOutput().GetPolys().GetNumberOfCells()
Colors.SetNumberOfTuples(Cellarray)
for c in range(Cellarray):
    Colors.InsertTuple3(c, 255, 0, 0)

sphereSource.GetOutput().GetCellData().SetScalars(Colors)
sphereSource.Update()

path = "object2.vtk"
coneSource = read_vtk(path)

Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Colors.SetName('Colors')
Cellarray = coneSource.GetOutput().GetPolys().GetNumberOfCells()
Colors.SetNumberOfTuples(Cellarray)
for c in range(Cellarray):
    Colors.InsertTuple3(c, 0, 0, 255)
coneSource.GetOutput().GetCellData().SetScalars(Colors)
coneSource.Update()

# Append the two meshes
appendFilter = vtk.vtkAppendPolyData()
appendFilter.AddInputData(sphereSource.GetOutput())
appendFilter.AddInputData(coneSource.GetOutput())

appendFilter.Update()

writer = vtk.vtkPolyDataWriter()
writer.SetInputData(appendFilter.GetOutput())
writer.SetFileName('output.vtk')
writer.Update()
writer.Write()

writer = vtk.vtkXMLPolyDataWriter()
writer.SetInputData(appendFilter.GetOutput())
# writer.SetColorModeToDirectScalars()
writer.SetFileName('output.vtp')
writer.Update()
writer.Write()
rubenverhack commented 4 years ago

vtk version 9.0.0 on Ubuntu 20.04 using python 3.8.2

rubenverhack commented 4 years ago

itk-vtk-viewer version 10.2.1

rubenverhack commented 4 years ago

I suspect that the differnce between the renderer and itk-vtk-viewer might lay in:

mapper.SetColorModeToDirectScalars()

Is there a way to enable this?

thewtex commented 4 years ago

@rubenverhack thank you for the kind words and detailed, reproducible issue!

Yes, #300 will address this:

localhost_8080

rubenverhack commented 4 years ago

That was fast! Thank you!