{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 渲染\n",
"\n",
"## 几何渲染\n",
"\n",
"展示一些使用 `dash_vtk` 渲染网格的例子。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# from dash import jupyter_dash\n",
"\n",
"# jupyter_dash.infer_jupyter_proxy_config()\n",
"from dash import Dash, html\n",
"\n",
"import dash_vtk\n",
"from dash_vtk.utils import to_mesh_state\n",
"\n",
"try:\n",
" # VTK 9+\n",
" from vtkmodules.vtkImagingCore import vtkRTAnalyticSource\n",
"except ImportError:\n",
" # VTK =< 8\n",
" from vtk.vtkImagingCore import vtkRTAnalyticSource"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# 使用 VTK 获取一些数据\n",
"data_source = vtkRTAnalyticSource()\n",
"data_source.Update() # <= 执行源文件以生成输出\n",
"dataset = data_source.GetOutput()\n",
"# 使用 helper 获取可以直接传递给 Mesh 的网格结构,RTData 是字段的名称\n",
"mesh_state = to_mesh_state(dataset)\n",
"\n",
"content = dash_vtk.View([\n",
" dash_vtk.GeometryRepresentation([\n",
" dash_vtk.Mesh(state=mesh_state)\n",
" ]),\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"tags": [
"hidden-input"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Dash setup\n",
"app = Dash(__name__)\n",
"server = app.server\n",
"\n",
"app.layout = html.Div(\n",
" style={\"width\": \"100%\", \"height\": \"400px\"},\n",
" children=[content],\n",
")\n",
"\n",
"if __name__ == \"__main__\":\n",
" app.run(host=\"localhost\",\n",
" # jupyter_server_url=\"\"\n",
" port=\"8060\", \n",
" jupyter_height=400, \n",
" jupyter_width=\"70%\")\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Use helper to get a mesh structure that can be passed as-is to a Mesh\n",
"from dash_vtk.utils import to_mesh_state\n",
"mesh_state = to_mesh_state(dataset)\n",
"\n",
"content = dash_vtk.View([\n",
" dash_vtk.GeometryRepresentation([\n",
" dash_vtk.Mesh(state=mesh_state)\n",
" ]),\n",
"])\n",
"\n",
"# Dash setup\n",
"app = Dash(__name__)\n",
"server = app.server\n",
"\n",
"app.layout = html.Div(\n",
" style={\"width\": \"100%\", \"height\": \"calc(100vh - 15px)\"},\n",
" children=[content],\n",
")\n",
"\n",
"if __name__ == \"__main__\":\n",
" app.run(debug=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 体积渲染\n",
"\n",
"上一个例子是使用 3D 图像并提取其网格进行渲染。让我们保持相同的数据,但以体积渲染的方式展示它。"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from dash import Dash, html\n",
"\n",
"import dash_vtk\n",
"from dash_vtk.utils import to_volume_state\n",
"\n",
"try:\n",
" # VTK 9+\n",
" from vtkmodules.vtkImagingCore import vtkRTAnalyticSource\n",
"except ImportError:\n",
" # VTK =< 8\n",
" from vtk.vtkImagingCore import vtkRTAnalyticSource\n",
"\n",
"# Use VTK to get some data\n",
"data_source = vtkRTAnalyticSource()\n",
"data_source.Update() # <= Execute source to produce an output\n",
"dataset = data_source.GetOutput()\n",
"\n",
"# Use helper to get a volume structure that can be passed as-is to a Volume\n",
"volume_state = to_volume_state(dataset) # No need to select field\n",
"\n",
"content = dash_vtk.View([\n",
" dash_vtk.VolumeRepresentation([\n",
" # GUI to control Volume Rendering\n",
" # + Setup good default at startup\n",
" dash_vtk.VolumeController(),\n",
" # Actual volume\n",
" dash_vtk.Volume(state=volume_state),\n",
" ]),\n",
"])\n",
"\n",
"# Dash setup\n",
"app = Dash(__name__)\n",
"server = app.server\n",
"\n",
"app.layout = html.Div(\n",
" style={\"width\": \"100%\", \"height\": \"400px\"},\n",
" children=[content],\n",
")\n",
"\n",
"if __name__ == \"__main__\":\n",
" app.run(debug=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py311",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}