{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Spherical augmentations\n\nCredit: C Ambroise\n\nA simple example on how to use augmentations in the spherical domain.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import axes3d\nfrom surfify.utils import icosahedron, neighbors\nfrom surfify.plotting import plot_trisurf\nfrom surfify.augmentation import SphericalRandomRotation, SphericalRandomCut\n\ncoords, triangles = icosahedron(order=3)\nneighs = neighbors(coords, triangles, direct_neighbor=True)\nprint(coords.shape)\nprint(triangles.shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Random cuts\n\nDisplay random cut outputs with different parameters.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tri_texture = np.array([[1, 1]]*len(coords))\naugmentations = []\nprint(\"initializing random cut augmentations...\")\nfor idx in range(5):\n    aug = SphericalRandomCut(\n        coords, triangles, neighs=neighs, patch_size=(4 - idx),\n        n_patches=(idx + 1))\n    augmentations.append(aug)\n\nfig, ax = plt.subplots(2, 3, subplot_kw={\n        \"projection\": \"3d\", \"aspect\": \"auto\"}, figsize=(10, 10))\ncolorbar = False\nplot_trisurf(coords, triangles, tri_texture[:, 0], fig=fig, ax=ax[0, 0],\n             alpha=0.3, colorbar=colorbar, edgecolors=\"white\")\nfor idx in range(5):\n    if idx == 4:\n        colorbar = True\n    augmented_texture = augmentations[idx](tri_texture)\n    plot_trisurf(coords, triangles, augmented_texture[:, 0],\n                 ax=ax[(idx + 1) // 3, (idx + 1) % 3], fig=fig,\n                 alpha=0.3, colorbar=colorbar, edgecolors=\"white\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Rotation\n\nDisplay 90\u00b0 rotations of a texture on the icosahedron following the x axis\n(green).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tri_texture = np.array([[1, 1]] + [[0, 0]] * (len(coords) - 1))\nrotated_coords = []\naugmentations = []\nprint(\"Initializing random rotation augmentations...\")\nfor idx, angle in enumerate(range(90, 271, 90)):\n    aug = SphericalRandomRotation(coords, triangles, angles=(angle, 0, 0))\n    augmentations.append(aug)\n\n# We also set the vertices neighbouring vertex 0 to 1\ntri_texture[neighs[0]] = [1, 1]\nfig, ax = plt.subplots(2, 2, subplot_kw={\n        \"projection\": \"3d\", \"aspect\": \"auto\"}, figsize=(10, 10))\nax[0, 0].plot([0, 0], [0, 0], [-1, 1], c=\"red\")\nax[0, 0].plot([0, 0], [-1, 1], [0, 0], c=\"blue\")\nax[0, 0].plot([-1, 1], [0, 0], [0, 0], c=\"green\")\ncolorbar = False\nplot_trisurf(coords, triangles, tri_texture[:, 0], fig=fig, ax=ax[0, 0],\n             alpha=0.3, colorbar=colorbar, edgecolors=\"white\")\nfor idx, angle in enumerate(range(90, 271, 90)):\n    if idx == 2:\n        colorbar = True\n    augmented_texture = augmentations[idx](tri_texture)\n    ax[(idx + 1) // 2, (idx + 1) % 2].plot([0, 0], [0, 0], [-1, 1], c=\"red\")\n    ax[(idx + 1) // 2, (idx + 1) % 2].plot([0, 0], [-1, 1], [0, 0], c=\"blue\")\n    ax[(idx + 1) // 2, (idx + 1) % 2].plot([-1, 1], [0, 0], [0, 0], c=\"green\")\n    print(\"Angle rotation {}: {}\".format(idx, augmentations[idx].angles))\n    plot_trisurf(coords, triangles, augmented_texture[:, 0],\n                 ax=ax[(idx + 1) // 2, (idx + 1) % 2], fig=fig,\n                 colorbar=colorbar, alpha=0.3, edgecolors=\"white\")\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
}