{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Spherical neighbors\n\nCredit: A Grigis\n\nA simple example on how to build spherical neighbors using an icosahedron.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import axes3d\nfrom surfify.utils import icosahedron, neighbors, get_rectangular_projection\nfrom surfify.plotting import plot_trisurf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Direct Neighbor\n\nDisplay direct neighbors for some vertices.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "vertices, triangles = icosahedron(order=2)\nneighs = neighbors(vertices, triangles, depth=2, direct_neighbor=True)\ncolors = [\"red\", \"green\", \"blue\", \"orange\", \"purple\", \"brown\", \"pink\",\n          \"gray\", \"olive\", \"cyan\", \"yellow\", \"tan\", \"salmon\", \"violet\",\n          \"steelblue\", \"lime\", \"navy\"] * 5\nfig, ax = plt.subplots(1, 1, subplot_kw={\n    \"projection\": \"3d\", \"aspect\": \"auto\"}, figsize=(10, 10))\nplot_trisurf(vertices, triangles=triangles, colorbar=False, fig=fig, ax=ax)\nfor vidx in (0, 4):\n    for cnt, idx in enumerate(neighs[vidx]):\n        point = vertices[idx]\n        ax.scatter(point[0], point[1], point[2], marker=\"o\", c=colors[cnt],\n                   s=100)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Rectagular Tangent Plane Neighbor\n\nDisplay 3x3 rectangular tangent plane neighbors for some vertices and\nthe associated projection on the sphere.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "zoom = 2\nfig, ax = plt.subplots(1, 1, subplot_kw={\n        \"projection\": \"3d\", \"aspect\": \"auto\"}, figsize=(10, 10))\nplot_trisurf(vertices, triangles=triangles, fig=fig, ax=ax)\nfor idx in (13, 40):\n    node = vertices[idx]\n    node_rec_neighs, node_tplane_neighs = get_rectangular_projection(\n        node, size=3, zoom=zoom)\n    ax.scatter(node[0], node[1], node[2], marker=\"^\", c=colors[0], s=100)\n    for cnt, point in enumerate(node_tplane_neighs):\n        ax.scatter(point[0], point[1], point[2], marker=\"o\", c=colors[cnt + 1],\n                   s=100)\n    for cnt, point in enumerate(node_rec_neighs):\n        ax.scatter(point[0], point[1], point[2], marker=\"X\", c=colors[cnt + 1],\n                   s=100)\n\nplt.show()"
      ]
    }
  ],
  "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.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}