{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using dicom2stl.py to extract an iso-surface from a volume"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook gives a basic introduction to using the `'dicom2stl.py'` script to extract an iso-surface from a volume image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "\n",
    "# download dicom2stl if it's not here already\n",
    "if not os.path.isdir('dicom2stl'):\n",
    "    !{'git clone https://github.com/dave3d/dicom2stl.git'}\n",
    "\n",
    "# Get the latest version\n",
    "!{'cd dicom2stl; git pull'}\n",
    "    \n",
    "# Install required packages\n",
    "!{sys.executable} -m pip install SimpleITK\n",
    "!{sys.executable} -m pip install vtk\n",
    "!{sys.executable} -m pip install itkwidgets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a test volume that is 4 Gaussian blobs arranged in a tetrahedron"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dicom2stl.tests import create_data\n",
    "tetra = create_data.make_tetra()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display the tetra volume using [ITK Widgets](https://github.com/InsightSoftwareConsortium/itkwidgets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itkwidgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itkwidgets.view(tetra, cmap='Grayscale', vmin=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write the tetra volume to a file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import SimpleITK as sitk\n",
    "sitk.WriteImage(tetra, \"tetra.nii.gz\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Show the command line options for dicom2stl.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!{'./dicom2stl/dicom2stl.py -h'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extract an iso-surface from the tetra volume\n",
    "The `'-i'` flag tells the script the intensity value to use for the iso-surface, `150` in this case.  The `'-o'` flag specifies the output file, `tetra.stl`.  The script can output STL, VTK or PLY files.  And `tetra.nii.gz` is input volume."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!{'./dicom2stl/dicom2stl.py -i 150 -o tetra.stl tetra.nii.gz'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load the mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dicom2stl.utils import vtkutils\n",
    "mesh = vtkutils.readMesh('tetra.stl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display the mesh with the volume"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itkwidgets.view(tetra, cmap='Grayscale', geometries=[mesh], vmin=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}