Skip to content

Commit

Permalink
Add notebook to try some 3D visualizations
Browse files Browse the repository at this point in the history
  • Loading branch information
BetsyMcPhail committed May 14, 2020
1 parent 28565ce commit 9b81dbe
Show file tree
Hide file tree
Showing 2 changed files with 359 additions and 1 deletion.
19 changes: 18 additions & 1 deletion .binder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,36 @@
# named "latest" is not allowed, however they do not (yet) enforce that
# restriction.
FROM robotlocomotion/drake:latest

ARG NB_USER=jovyan
ARG NB_UID=1000
ARG NB_GID=100
EXPOSE 7000/tcp
EXPOSE 8888/tcp
RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get update -qq \
&& apt-get install --no-install-recommends -qq \
&& apt-get install --no-install-recommends -qq python3-pip \
-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew \
-o Dpkg::Use-Pty=0 \
locales \
xvfb \
&& rm -rf /var/lib/apt/lists/* \
&& locale-gen en_US.UTF-8 \
&& useradd -d "/home/$NB_USER" -G $NB_GID -mU -s /bin/bash "$NB_USER"

RUN pip3 install wheel
RUN pip3 install itkwidgets
RUN pip3 install itk
RUN pip3 install open3d==0.9.0
RUN pip3 install pandas
RUN pip3 install pyntcloud

RUN jupyter nbextension install --py --symlink --sys-prefix itkwidgets
RUN jupyter nbextension enable --py --sys-prefix itkwidgets
RUN jupyter nbextension enable --py --sys-prefix widgetsnbextension
RUN jupyter nbextension install --py --symlink --sys-prefix open3d
RUN jupyter nbextension enable --py --sys-prefix open3d

ENV HOME="/home/$NB_USER" \
LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8 \
Expand All @@ -34,6 +49,8 @@ COPY ["tutorials/*.ipynb", "$HOME/tutorials/"]
RUN chown -R $NB_UID:$NB_GID \
/opt/drake/bin/binder-entrypoint \
"$HOME/tutorials"

ENV PYTHONPATH="/home/jovyan/.local/lib/python3.6/site-packages:$PYTHONPATH"
USER "$NB_USER"
ENTRYPOINT ["/opt/drake/bin/binder-entrypoint"]
CMD ["jupyter", "notebook", "--ip", "0.0.0.0"]
341 changes: 341 additions & 0 deletions tutorials/3dvisualization_tests.ipynb
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
}

0 comments on commit 9b81dbe

Please sign in to comment.