{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Demonstrates very basic use of PColorMeshItem\n",
    "\"\"\"\n",
    "from pyqtgraph.jupyter import GraphicsLayoutWidget\n",
    "import ipywidgets\n",
    "from IPython.display import display\n",
    "\n",
    "import time\n",
    "import numpy as np\n",
    "import pyqtgraph as pg\n",
    "\n",
    "## Create window with GraphicsView widget\n",
    "pg.mkQApp()\n",
    "win = GraphicsLayoutWidget(css_width=\"800px\", css_height=\"480px\")\n",
    "view = win.addViewBox()\n",
    "\n",
    "\n",
    "## Create data\n",
    "\n",
    "# To enhance the non-grid meshing, we randomize the polygon vertices per and \n",
    "# certain amount\n",
    "randomness = 5\n",
    "\n",
    "# x and y being the vertices of the polygons, they share the same shape\n",
    "# However the shape can be different in both dimension\n",
    "xn = 50 # nb points along x\n",
    "yn = 40 # nb points along y\n",
    "\n",
    "\n",
    "x = np.repeat(np.arange(1, xn+1), yn).reshape(xn, yn)\\\n",
    "    + np.random.random((xn, yn))*randomness\n",
    "y = np.tile(np.arange(1, yn+1), xn).reshape(xn, yn)\\\n",
    "    + np.random.random((xn, yn))*randomness\n",
    "x.sort(axis=0)\n",
    "y.sort(axis=0)\n",
    "\n",
    "\n",
    "# z being the color of the polygons its shape must be decreased by one in each dimension\n",
    "z = np.exp(-(x*xn)**2/1000)[:-1,:-1]\n",
    "\n",
    "## Create image item\n",
    "edgecolors   = None\n",
    "antialiasing = False\n",
    "# edgecolors = {'color':'w', 'width':2} # May be uncommened to see edgecolor effect\n",
    "# antialiasing = True # May be uncommened to see antialiasing effect\n",
    "pcmi = pg.PColorMeshItem(edgecolors=edgecolors, antialiasing=antialiasing)\n",
    "view.addItem(pcmi)\n",
    "textitem = pg.TextItem(anchor=(1, 0))\n",
    "view.addItem(textitem)\n",
    "\n",
    "\n",
    "## Set the animation\n",
    "fps = 25 # Frame per second of the animation\n",
    "\n",
    "# Wave parameters\n",
    "wave_amplitude  = 3\n",
    "wave_speed      = 0.3\n",
    "wave_length     = 10\n",
    "color_speed     = 0.3\n",
    "\n",
    "textpos = None\n",
    "i = 0\n",
    "def updateData():\n",
    "    global i\n",
    "    global textpos\n",
    "    \n",
    "    ## Display the new data set\n",
    "    t0 = time.perf_counter()\n",
    "    new_x = x\n",
    "    new_y = y+wave_amplitude*np.cos(x/wave_length+i)\n",
    "    new_z = np.exp(-(x-np.cos(i*color_speed)*xn)**2/1000)[:-1,:-1]\n",
    "    t1 = time.perf_counter()\n",
    "    pcmi.setData(new_x,\n",
    "                 new_y,\n",
    "                 new_z)\n",
    "    t2 = time.perf_counter()\n",
    "\n",
    "    i += wave_speed\n",
    "    \n",
    "    # display info in top-right corner\n",
    "    textitem.setText(f'{(t2 - t1)*1000:.1f} ms')\n",
    "    if textpos is None:\n",
    "        textpos = pcmi.width(), pcmi.height()\n",
    "        textitem.setPos(*textpos)\n",
    "\n",
    "updateData()\n",
    "\n",
    "def onPlay(change):\n",
    "    updateData()\n",
    "    win.request_draw()\n",
    "\n",
    "play = ipywidgets.Play(\n",
    "    interval = 40,\n",
    "    min = 0,\n",
    "    max = 1000,\n",
    ")\n",
    "play.observe(onPlay, names='value')\n",
    "\n",
    "display(play)\n",
    "display(win)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
