Skip to content

Commit

Permalink
Merge pull request #138 from Kitware/fix-vtk-import
Browse files Browse the repository at this point in the history
fix(vtk): use explicit import
  • Loading branch information
jourdain authored Nov 16, 2024
2 parents bceea41 + 628da61 commit 44da332
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
57 changes: 41 additions & 16 deletions pan3d/explorers/slicer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import sys
import json
import vtk
from pathlib import Path

from vtkmodules.vtkRenderingCore import (
vtkRenderer,
vtkRenderWindowInteractor,
vtkRenderWindow,
vtkDataSetMapper,
vtkActor,
vtkPolyDataMapper,
vtkTextProperty,
)
from vtkmodules.vtkRenderingAnnotation import (
vtkScalarBarActor,
)
from vtkmodules.vtkCommonDataModel import (
vtkPlane,
)
from vtkmodules.vtkFiltersModeling import (
vtkOutlineFilter,
)
from vtkmodules.vtkFiltersCore import (
vtkCutter,
)

# VTK factory initialization
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleSwitch # noqa
import vtkmodules.vtkRenderingOpenGL2 # noqa

from pan3d.xarray.algorithm import vtkXArrayRectilinearSource

from trame.app import get_server
Expand Down Expand Up @@ -89,18 +114,18 @@ def _setup_vtk(self, source=None, import_state=None):
color_range = ds.point_data[self.state.color_by].GetRange()

# Build rendering pipeline
self.renderer = vtk.vtkRenderer()
self.interactor = vtk.vtkRenderWindowInteractor()
self.render_window = vtk.vtkRenderWindow()
self.renderer = vtkRenderer()
self.interactor = vtkRenderWindowInteractor()
self.render_window = vtkRenderWindow()

plane = vtk.vtkPlane()
plane = vtkPlane()
plane.SetOrigin(self.origin)
plane.SetNormal(self.normal)
cutter = vtk.vtkCutter()
cutter = vtkCutter()
cutter.SetCutFunction(plane)
cutter.input_connection = self.source.output_port
slice_actor = vtk.vtkActor()
slice_mapper = vtk.vtkDataSetMapper()
slice_actor = vtkActor()
slice_mapper = vtkDataSetMapper()
slice_mapper.SetInputConnection(cutter.GetOutputPort())
slice_mapper.SetScalarRange(*color_range)
slice_mapper.SelectColorArray(self.state.color_by)
Expand All @@ -112,9 +137,9 @@ def _setup_vtk(self, source=None, import_state=None):
self.slice_actor = slice_actor
self.slice_mapper = slice_mapper

outline = vtk.vtkOutlineFilter()
outline_actor = vtk.vtkActor()
outline_mapper = vtk.vtkPolyDataMapper()
outline = vtkOutlineFilter()
outline_actor = vtkActor()
outline_mapper = vtkPolyDataMapper()
outline.input_connection = self.source.output_port
outline_mapper.SetInputConnection(outline.GetOutputPort())
outline_actor.SetMapper(outline_mapper)
Expand All @@ -123,8 +148,8 @@ def _setup_vtk(self, source=None, import_state=None):
self.outline_actor = outline_actor
self.outline_mapper = outline_mapper

data_actor = vtk.vtkActor()
data_mapper = vtk.vtkDataSetMapper()
data_actor = vtkActor()
data_mapper = vtkDataSetMapper()
data_mapper.input_connection = self.source.output_port
data_mapper.SetScalarRange(*color_range)
data_actor.SetMapper(data_mapper)
Expand All @@ -133,14 +158,14 @@ def _setup_vtk(self, source=None, import_state=None):
self.data_actor = data_actor
self.data_mapper = data_mapper

sbar_actor = vtk.vtkScalarBarActor()
sbar_actor = vtkScalarBarActor()
sbar_actor.SetLookupTable(self.slice_mapper.GetLookupTable())
sbar_actor.SetMaximumHeightInPixels(600)
sbar_actor.SetMaximumWidthInPixels(100)
sbar_actor.SetTitleRatio(0.2)
lprop: vtk.vtkTextProperty = sbar_actor.GetLabelTextProperty()
lprop: vtkTextProperty = sbar_actor.GetLabelTextProperty()
lprop.SetColor(0.5, 0.5, 0.5)
tprop: vtk.vtkTextProperty = sbar_actor.GetTitleTextProperty()
tprop: vtkTextProperty = sbar_actor.GetTitleTextProperty()
tprop.SetColor(0.5, 0.5, 0.5)
self.sbar_actor = sbar_actor

Expand Down
11 changes: 7 additions & 4 deletions pan3d/utils/convert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import math
import vtk
import base64

from vtkmodules.vtkCommonCore import vtkUnsignedCharArray
from vtkmodules.vtkCommonDataModel import vtkImageData
from vtkmodules.vtkIOImage import vtkPNGWriter


def to_float(v):
v = float(v)
Expand All @@ -26,15 +29,15 @@ def update_camera(camera, props):


def to_image(lut, samples=255):
colorArray = vtk.vtkUnsignedCharArray()
colorArray = vtkUnsignedCharArray()
colorArray.SetNumberOfComponents(3)
colorArray.SetNumberOfTuples(samples)

dataRange = lut.GetRange()
delta = (dataRange[1] - dataRange[0]) / float(samples)

# Add the color array to an image data
imgData = vtk.vtkImageData()
imgData = vtkImageData()
imgData.SetDimensions(samples, 1, 1)
imgData.GetPointData().SetScalars(colorArray)

Expand All @@ -47,7 +50,7 @@ def to_image(lut, samples=255):
b = int(round(rgb[2] * 255))
colorArray.SetTuple3(i, r, g, b)

writer = vtk.vtkPNGWriter()
writer = vtkPNGWriter()
writer.WriteToMemoryOn()
writer.SetInputData(imgData)
writer.SetCompressionLevel(6)
Expand Down
22 changes: 10 additions & 12 deletions pan3d/utils/presets.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import vtk
from pathlib import Path
import numpy as np

from vtkmodules.vtkRenderingCore import vtkColorTransferFunction, vtkActor
from vtkmodules.vtkCommonCore import vtkLookupTable

hsv_colors = {
"Rainbow": {
"Hue": (0.666, 0.0),
Expand Down Expand Up @@ -32,7 +34,7 @@
for cmap in data:
name = cmap["Name"]
srgb = np.array(cmap["RGBPoints"])
tfunc = vtk.vtkColorTransferFunction()
tfunc = vtkColorTransferFunction()
for arr in np.split(srgb, len(srgb) / 4):
tfunc.AddRGBPoint(arr[0], arr[1], arr[2], arr[3])
info = {"TF": tfunc, "Range": (srgb[0], srgb[-4])}
Expand All @@ -41,8 +43,8 @@
print("Error loading diverging color maps : ", e)


def convert_tfunc_to_lut(tfunc: vtk.vtkColorTransferFunction, tfrange):
lut = vtk.vtkLookupTable()
def convert_tfunc_to_lut(tfunc: vtkColorTransferFunction, tfrange):
lut = vtkLookupTable()
lut.SetNumberOfTableValues(256)
tflen = tfrange[1] - tfrange[0]
for i in range(256):
Expand All @@ -53,11 +55,9 @@ def convert_tfunc_to_lut(tfunc: vtk.vtkColorTransferFunction, tfrange):
return lut


def apply_preset(
actor: vtk.vtkActor, srange, preset: str, nan_color=[0, 0, 0, 0]
) -> None:
def apply_preset(actor: vtkActor, srange, preset: str, nan_color=[0, 0, 0, 0]) -> None:
if preset in list(hsv_colors.keys()):
lut = vtk.vtkLookupTable()
lut = vtkLookupTable()
lut.SetNumberOfTableValues(256)
mapper = actor.GetMapper()
mapper.SetLookupTable(lut)
Expand All @@ -81,9 +81,7 @@ def apply_preset(
mapper.SetScalarRange(srange[0], srange[1])


def use_preset(
sactor: vtk.vtkActor, dactor: vtk.vtkActor, sbar: vtk.vtkActor, preset: str
) -> None:
def use_preset(sactor: vtkActor, dactor: vtkActor, sbar: vtkActor, preset: str) -> None:
"""
Given the slice, data, and scalar bar actor, applies the provided preset
and updates the actors and the scalar bar
Expand All @@ -99,7 +97,7 @@ def use_preset(
sbar.SetLookupTable(sactor.GetMapper().GetLookupTable())


def update_preset(actor: vtk.vtkActor, sbar: vtk.vtkActor, logcale: bool) -> None:
def update_preset(actor: vtkActor, sbar: vtkActor, logcale: bool) -> None:
"""
Given an actor, scalar bar, and the option for whether to use log scale,
make changes to the lookup table for the actor, and update the scalar bar
Expand Down
26 changes: 19 additions & 7 deletions pan3d/viewers/preview.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import vtk
from vtkmodules.vtkInteractionWidgets import vtkOrientationMarkerWidget
from vtkmodules.vtkRenderingAnnotation import vtkAxesActor

from vtkmodules.vtkRenderingCore import (
vtkRenderer,
vtkRenderWindowInteractor,
vtkRenderWindow,
vtkActor,
vtkPolyDataMapper,
)
from vtkmodules.vtkFiltersGeometry import vtkDataSetSurfaceFilter

# VTK factory initialization
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleSwitch # noqa
import vtkmodules.vtkRenderingOpenGL2 # noqa

import json
import traceback
from pathlib import Path
Expand Down Expand Up @@ -101,9 +113,9 @@ def __init__(self, server=None, local_rendering=None):
# -------------------------------------------------------------------------

def _setup_vtk(self):
self.renderer = vtk.vtkRenderer(background=(0.8, 0.8, 0.8))
self.interactor = vtk.vtkRenderWindowInteractor()
self.render_window = vtk.vtkRenderWindow(off_screen_rendering=1)
self.renderer = vtkRenderer(background=(0.8, 0.8, 0.8))
self.interactor = vtkRenderWindowInteractor()
self.render_window = vtkRenderWindow(off_screen_rendering=1)

self.render_window.AddRenderer(self.renderer)
self.interactor.SetRenderWindow(self.render_window)
Expand All @@ -112,11 +124,11 @@ def _setup_vtk(self):
self.source = vtkXArrayRectilinearSource()

# Need explicit geometry extraction when used with WASM
self.geometry = vtk.vtkDataSetSurfaceFilter(
self.geometry = vtkDataSetSurfaceFilter(
input_connection=self.source.output_port
)
self.mapper = vtk.vtkPolyDataMapper(input_connection=self.geometry.output_port)
self.actor = vtk.vtkActor(mapper=self.mapper, visibility=0)
self.mapper = vtkPolyDataMapper(input_connection=self.geometry.output_port)
self.actor = vtkActor(mapper=self.mapper, visibility=0)

self.interactor.Initialize()

Expand Down

0 comments on commit 44da332

Please sign in to comment.