def build_view_child(dimensions, spacing, origin, field, enabled, i, j, k, window, level): slice_prop = {"colorWindow": window, "colorLevel": level} child = [ dash_vtk.ShareDataSet( dash_vtk.ImageData( dimensions=dimensions, spacing=spacing, origin=origin, children=dash_vtk.PointData( dash_vtk.DataArray(registration="setScalars", values=field)), ), ), ] if "Volume" in enabled: child.append( dash_vtk.VolumeRepresentation( [dash_vtk.VolumeController(), dash_vtk.ShareDataSet()], )) if "i" in enabled: child.append( dash_vtk.SliceRepresentation( iSlice=int(round(i)), property=slice_prop, children=dash_vtk.ShareDataSet(), )) if "j" in enabled: child.append( dash_vtk.SliceRepresentation( jSlice=int(round(j)), property=slice_prop, children=dash_vtk.ShareDataSet(), )) if "k" in enabled: child.append( dash_vtk.SliceRepresentation( kSlice=int(round(k)), property=slice_prop, children=dash_vtk.ShareDataSet(), )) return child
from vtk.vtkImagingCore import vtkRTAnalyticSource # Use VTK to get some data data_source = vtkRTAnalyticSource() data_source.Update() # <= Execute source to produce an output dataset = data_source.GetOutput() # Use helper to get a volume structure that can be passed as-is to a Volume volume_state = to_volume_state(dataset) # No need to select field content = dash_vtk.View([ dash_vtk.VolumeRepresentation([ # GUI to control Volume Rendering # + Setup good default at startup dash_vtk.VolumeController(), # Actual volume dash_vtk.ShareDataSet([ dash_vtk.Volume(state=volume_state), ]), ]), dash_vtk.SliceRepresentation(iSlice=10, children=[ dash_vtk.ShareDataSet(), ]), dash_vtk.SliceRepresentation(jSlice=10, children=[ dash_vtk.ShareDataSet(), ]), dash_vtk.SliceRepresentation(kSlice=10, children=[ dash_vtk.ShareDataSet(),
field = [random.random() * i for i in range(10 * 10 * 10)] content = dash_vtk.View( [ dash_vtk.VolumeRepresentation( [ dash_vtk.VolumeController(), dash_vtk.ShareDataSet( [ dash_vtk.ImageData( dimensions=[10, 10, 10], spacing=[1, 1, 1], origin=[-4, -4, -4], children=[ dash_vtk.PointData( [ dash_vtk.DataArray( registration="setScalars", values=field, ) ] ) ], ), ] ), ] ), dash_vtk.SliceRepresentation( property={"colorWindow": 500, "colorLevel": 200}, iSlice=5, children=[dash_vtk.ShareDataSet()],
def dcm_to_volume(dir_path): itk_image = itk.imread(dir_path) vtk_image = itk.vtk_image_from_image(itk_image) volume_state = to_volume_state(vtk_image) return volume_state # Place a DICOM series (a set of per-file slices) in a directory. ITK sorts, sets spatial metadata, etc. demo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) volume_state = dcm_to_volume(os.path.join(demo_dir, "data", "mri_pancreas")) app = dash.Dash(__name__) server = app.server vtk_view = dash_vtk.View( dash_vtk.VolumeRepresentation( [dash_vtk.VolumeController(), dash_vtk.Volume(state=volume_state)] ) ) app.layout = html.Div( style={"height": "calc(100vh - 50px)", "width": "100%"}, children=[html.Div(style={"height": "100%", "width": "100%"}, children=vtk_view)], ) if __name__ == "__main__": app.run_server(debug=True)
import vtk # Data file path demo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) head_vti = os.path.join(demo_dir, "data", "head.vti") # Load dataset from dist reader = vtk.vtkXMLImageDataReader() reader.SetFileName(head_vti) reader.Update() volume_state = to_volume_state(reader.GetOutput()) vtk_view = dash_vtk.View( dash_vtk.VolumeRepresentation(children=[ dash_vtk.VolumeController(), dash_vtk.Volume(state=volume_state), ])) app = dash.Dash(__name__) server = app.server app.layout = html.Div( style={ "height": "calc(100vh - 16px)", "width": "100%" }, children=[html.Div(vtk_view, style={ "height": "100%", "width": "100%" })], )
import dash import dash_html_components as html import dash_vtk content = dash_vtk.View([ dash_vtk.VolumeRepresentation([ # GUI to control Volume Rendering # + Setup good default at startup dash_vtk.VolumeController(), # Actual Imagedata dash_vtk.ImageData( dimensions=[5, 5, 5], origin=[-2, -2, -2], spacing=[1, 1, 1], children=[ dash_vtk.PointData([ dash_vtk.DataArray( registration="setScalars", values=list(range(5*5*5)), ) ]) ], ), ]), ]) # Dash setup app = dash.Dash(__name__) server = app.server
kSlice=47, property=slice_property, children=dash_vtk.ShareDataSet(), ), ], ) volume_view = dash_vtk.View( id="volume-view", background=[0, 0, 0], cameraPosition=[1, 0, 0], cameraViewUp=[0, 0, -1], cameraParallelProjection=False, children=[ dash_vtk.VolumeRepresentation([ html.Div(dash_vtk.VolumeController(), style={"display": "none"}), dash_vtk.ShareDataSet(), ]) ], ) app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) server = app.server app.layout = dbc.Container( fluid=True, style={"height": "calc(100vh - 30px)"}, children=[ html.Div( style={ "height": "20%", "display": "flex",
field = fd.u origin = [fd.x.mean(), fd.y.mean(), fd.z.mean()] ranges = np.array([axis.ptp() for axis in [fd.x, fd.y, fd.z]]) dimensions = np.array( [np.unique(axis).shape[0] for axis in [fd.x, fd.y, fd.z]]) x, y, z = dimensions spacing = np.round(ranges / dimensions).astype(int) content = dash_vtk.View( dash_vtk.VolumeRepresentation([ dash_vtk.VolumeController(), dash_vtk.ImageData( dimensions=dimensions, spacing=spacing, origin=origin, children=dash_vtk.PointData( dash_vtk.DataArray(registration="setScalars", values=field)), ), ])) # Dash setup app = dash.Dash(__name__) server = app.server app.layout = html.Div( style={ "width": "100%", "height": "calc(100vh - 15px)" }, children=[content],