-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add notebook to try some 3D visualizations
- Loading branch information
1 parent
28565ce
commit 9b81dbe
Showing
2 changed files
with
359 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,341 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# 3D Visualization Examples" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"See https://github.com/RobotLocomotion/drake/issues/12645" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Add and show `itkwidgets` examples\n", | ||
"\n", | ||
"Demonstrate a few `itkwidgets` widget examples inside of this notebook.\n", | ||
"\n", | ||
"See https://github.com/InsightSoftwareConsortium/itkwidgets" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Necessary Imports" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Import for the itkwidgets examples\n", | ||
"# Assumes itk and itkwidgets have been installed\n", | ||
"\n", | ||
"import os\n", | ||
"from urllib.request import urlretrieve\n", | ||
"\n", | ||
"import itk\n", | ||
"from itkwidgets import view\n", | ||
"\n", | ||
"import vtk" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Vtk poly data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Download data\n", | ||
"file_name = 'Bunny.vtp'\n", | ||
"if not os.path.exists(file_name):\n", | ||
" url = 'https://data.kitware.com/api/v1/file/5d23b917877dfcc902def289/download'\n", | ||
" urlretrieve(url, file_name)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"reader = vtk.vtkXMLPolyDataReader()\n", | ||
"reader.SetFileName(file_name)\n", | ||
"reader.Update()\n", | ||
"bunny = reader.GetOutput()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"view(geometries=bunny)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cone_source = vtk.vtkConeSource()\n", | ||
"cone_source.SetResolution(20)\n", | ||
"cone_source.Update()\n", | ||
"cone = cone_source.GetOutput()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"view(geometries=cone)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 3D Image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Download data\n", | ||
"file_name = '005_32months_T2_RegT1_Reg2Atlas_ManualBrainMask_Stripped.nrrd'\n", | ||
"if not os.path.exists(file_name):\n", | ||
" url = 'https://data.kitware.com/api/v1/file/564a5b078d777f7522dbfaa6/download'\n", | ||
" urlretrieve(url, file_name)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"image = itk.imread(file_name)\n", | ||
"view(image, rotate=True, vmin=4000, vmax=17000, gradient_opacity=0.4)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Mesh\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Download data\n", | ||
"file_name = 'cow.vtk'\n", | ||
"if not os.path.exists(file_name):\n", | ||
" url = 'https://data.kitware.com/api/v1/file/5aa18c268d777f06857863fe/download'\n", | ||
" urlretrieve(url, file_name)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mesh = itk.meshread(file_name)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"view(geometries=mesh)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Add and show `open3d` example\n", | ||
"\n", | ||
"Demonstrate an open3d example inside of this notebook.\n", | ||
"See http://www.open3d.org/docs/release/tutorial/Basic/working_with_numpy.html" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Necessary Imports" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import open3d as o3d\n", | ||
"from open3d import JVisualizer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# generate some neat n times 3 matrix using a variant of sync function\n", | ||
"x = np.linspace(-3, 3, 401)\n", | ||
"mesh_x, mesh_y = np.meshgrid(x, x)\n", | ||
"z = np.sinc((np.power(mesh_x, 2) + np.power(mesh_y, 2)))\n", | ||
"z_norm = (z - z.min()) / (z.max() - z.min())\n", | ||
"xyz = np.zeros((np.size(mesh_x), 3))\n", | ||
"xyz[:, 0] = np.reshape(mesh_x, -1)\n", | ||
"xyz[:, 1] = np.reshape(mesh_y, -1)\n", | ||
"xyz[:, 2] = np.reshape(z_norm, -1)\n", | ||
"\n", | ||
"# Pass xyz to Open3D.o3d.geometry.PointCloud and visualize\n", | ||
"pcd = o3d.geometry.PointCloud()\n", | ||
"pcd.points = o3d.utility.Vector3dVector(xyz)\n", | ||
"\n", | ||
"# http://www.open3d.org/docs/release/tutorial/Basic/jupyter.html\n", | ||
"visualizer = JVisualizer()\n", | ||
"visualizer.add_geometry(pcd)\n", | ||
"visualizer.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Add and show `pyntcloud` example\n", | ||
"\n", | ||
"https://github.com/daavoo/pyntcloud/blob/master/README.rst\n", | ||
"\n", | ||
"Example Binder link:\n", | ||
"https://mybinder.org/v2/gh/daavoo/pyntcloud/master?filepath=examples/[visualization]%20Polylines.ipynb\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Necessary Imports" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import pandas as pd\n", | ||
"from pyntcloud import PyntCloud" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"positions = np.random.rand(1000, 3) * 10\n", | ||
"positions -= positions.mean(0)\n", | ||
"\n", | ||
"points = pd.DataFrame(\n", | ||
" positions.astype(np.float32), \n", | ||
" columns=['x', 'y', 'z'])\n", | ||
"\n", | ||
"points[\"red\"] = (np.random.rand(1000) * 255).astype(np.uint8)\n", | ||
"points[\"green\"] = (np.random.rand(1000) * 255).astype(np.uint8)\n", | ||
"points[\"blue\"] = (np.random.rand(1000) * 255).astype(np.uint8)\n", | ||
"\n", | ||
"cloud = PyntCloud(points)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"lines = [\n", | ||
" {\n", | ||
" \"color\": \"red\",\n", | ||
" \"vertices\": [[0, 0, 0], [10, 0, 0]]\n", | ||
" },\n", | ||
" {\n", | ||
" \"color\": \"green\",\n", | ||
" \"vertices\": [[0, 0, 0], [0, 10, 0]]\n", | ||
" },\n", | ||
" {\n", | ||
" \"color\": \"blue\",\n", | ||
" \"vertices\": [[0, 0, 0], [0, 0, 10]]\n", | ||
" }\n", | ||
"]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cloud.plot(polylines=lines)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.6.9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |