{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e868fdd8",
   "metadata": {},
   "source": [
    "# Mesh to distance transform image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de499549",
   "metadata": {},
   "outputs": [],
   "source": [
    "import shapeworks as sw\n",
    "import numpy as np\n",
    "import pyvista as pv\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b33c0cf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "pv.set_jupyter_backend('static')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1738608",
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA = \"../Data/\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75e29e0d",
   "metadata": {},
   "source": [
    "### Load mesh to convert to distance transform image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3808b129",
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = DATA + \"ellipsoid_05.vtk\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bd83ad5",
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh = sw.Mesh(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbb3fc0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh.boundingBox()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "147c2d66",
   "metadata": {},
   "outputs": [],
   "source": [
    "pv_mesh = sw.sw2vtkMesh(mesh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8444d64d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#help(pv_mesh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03d69cce",
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh.center()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3d8c9e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = pv.Plotter(shape=(1,1), border=False)\n",
    "\n",
    "# used to maintain bounds even when meshes' positions change\n",
    "a = pv.UniformGrid()\n",
    "a.dimensions = np.array([36,21,34])\n",
    "a.origin = (-14,-4,5)\n",
    "a.spacing = (1,1,1)\n",
    "outline = a.outline()\n",
    "\n",
    "p.subplot(0,0)\n",
    "p.add_text(\"mesh\", position='lower_left')\n",
    "p.add_mesh(outline)\n",
    "p.add_mesh(pv_mesh, opacity=1.0)\n",
    "p.show_grid()\n",
    "p.camera_position = [-35,45,70]\n",
    "p.set_viewup([0,1,0])\n",
    "la = mesh.center()\n",
    "la[1]-=6\n",
    "p.set_focus(la)\n",
    "\n",
    "p.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cb5839b",
   "metadata": {},
   "source": [
    "### compute distance transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc1b4644",
   "metadata": {},
   "outputs": [],
   "source": [
    "#help(mesh1.toDistanceTransform)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eea4de6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "dt = mesh.toDistanceTransform()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17aa4c97",
   "metadata": {},
   "outputs": [],
   "source": [
    "dt.center()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9bea19b",
   "metadata": {},
   "outputs": [],
   "source": [
    "dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57fc752c",
   "metadata": {},
   "outputs": [],
   "source": [
    "pv_dt = sw.sw2vtkImage(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102e8866",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = pv.Plotter(shape=(1,1), border=False, multi_samples=1)\n",
    "\n",
    "# used to maintain bounds even when meshes' positions change\n",
    "a = pv.UniformGrid()\n",
    "a.dimensions = np.array([36,21,34])\n",
    "a.origin = (-14,-4,5)\n",
    "a.spacing = (1,1,1)\n",
    "outline = a.outline()\n",
    "\n",
    "p.subplot(0,0)\n",
    "p.add_text(\"distance transform\", position='lower_left')\n",
    "p.add_mesh(outline)\n",
    "p.add_mesh(pv_mesh)\n",
    "p.add_volume(pv_dt)\n",
    "p.show_grid()\n",
    "p.camera_position = [-35,45,70]\n",
    "p.set_viewup([0,1,0])\n",
    "la = dt.center()\n",
    "la[1]-=6\n",
    "p.set_focus(la)\n",
    "\n",
    "p.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b520f38d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.7.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
