{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ipyvolume.pylab as p3\n",
    "import cortex\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p3.examples.klein_bottle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%%javascript\n",
    "// for development, no need to execute this cell\n",
    "require.onResourceLoad = function(context, map)\n",
    "{\n",
    "    console.log(\"loading \" +map.name)\n",
    "   // require.undef(map.name);\n",
    "};\n",
    "require.undef(\"nbextensions/ipyvolume/index\")\n",
    "require([\"nbextensions/ipyvolume/index\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import urllib.request\n",
    "url = 'https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif'\n",
    "url = 'http://www.i-programmer.info/images/stories/News/2015/Mar/A/jupyter.jpg'\n",
    "im = Image.open(urllib.request.urlopen(url))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pts, polys = cortex.db.get_surf('S1', 'fiducial', merge=True)\n",
    "pts_flat, polys_flat = cortex.db.get_surf('S1', 'flat', merge=True, nudge=True)\n",
    "pts_inf, polys_inf = cortex.db.get_surf('S1', 'inflated', merge=True, nudge=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x, y, z = pts.T\n",
    "x_flat, y_flat, z_flat = pts_flat.T\n",
    "\n",
    "def n(x):\n",
    "    return (x - x.min()) / x.ptp()\n",
    "u = n(x_flat)\n",
    "v = n(y_flat)\n",
    "r = n(x)\n",
    "g = n(y)\n",
    "b = n(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# go to spherical coordinates\n",
    "x_inf, y_inf, z_inf = pts_inf.T\n",
    "r = np.sqrt(x_inf**2 + y_inf**2 + z_inf**2)\n",
    "phi = np.arctan2(x_inf,-z_inf)\n",
    "theta = np.arccos(y_inf/r)\n",
    "r.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# clip r\n",
    "r = np.minimum(r, 60)\n",
    "r.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# back to cartesian\n",
    "z_sphere = -r * np.sin(theta) * np.cos(phi)\n",
    "x_sphere =  r * np.sin(theta) * np.sin(phi)\n",
    "y_sphere =  r * np.cos(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "b = 60\n",
    "x_box = np.clip(x_inf, -b, b)\n",
    "y_box = np.clip(y_inf, -b, b)\n",
    "z_box = np.clip(z_inf, -b, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "theta = ((theta + (np.pi)) % (2*np.pi)) - (np.pi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_flat2 = (phi)*60 #-(((phi+np.pi) % (2*np.pi))- np.pi) * 30\n",
    "#x_flat = -(((phi+2*np.pi) % (2*np.pi))- np.pi) * 30\n",
    "y_flat2 = -(theta-np.pi/2) * 30*2\n",
    "z_flat2 = phi * 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "p3.figure()\n",
    "p3.style.use('dark')\n",
    "#p3.style.use(['light', 'minimal'])\n",
    "print(p3.gcf().style)\n",
    "x, y, z = pts.T\n",
    "x2, y2, z2 = pts_flat.T\n",
    "m = p3.plot_trisurf([x_flat2, x_box, x_sphere, x_inf, x, x_flat],\n",
    "                    [y_flat2, y_box, y_sphere, y_inf, y, y_flat],\n",
    "                    [z_flat2, z_box, z_sphere, z_inf, z, z_flat], polys_flat, u=u, v=v, texture=im)\n",
    "p3.animation_control(m, interval=1000)\n",
    "p3.squarelim()\n",
    "p3.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p3.movie('jupyter-brain.gif')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
