{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# K3D - Release 2.1.0\n", "\n", "Final tests\n", "\n", "\n", "\n", "\n", " \n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import k3d\n", "k3d.version_info" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "plot = k3d.plot()\n", "plot.display()\n", "\n", "plot += k3d.vectors(\n", " (0,0,0,0,0,0,0,0,0), \n", " (1,0,0,0,1,0,0,0,1), \n", " colors=(0xff0000, 0xff0000, 0xffff00, 0xffff00, 0x00ff00, 0x00ff00), \n", " labels=('x', 'y', 'z')\n", ")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## surface \n", "\n", " \n", " \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.randint(0, 0xFFFFFF, 1)[0].dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "plot.grid_auto_fit = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Nx = 50\n", "Ny = 50\n", "xmin,xmax = -3,3\n", "ymin,ymax = -0,3\n", "\n", "\n", "x = np.linspace(xmin,xmax,Nx)\n", "y = np.linspace(ymin,ymax,Ny)\n", "x,y = np.meshgrid(x,y)\n", "\n", "surface_plot = k3d.surface(np.sin(x**2+y**2),xmin=xmin,xmax=xmax,\\\n", " ymin=ymin,ymax=ymax, color=int(np.random.randint(0, 0xFFFFFF, 1)[0]))\n", "plot += surface_plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time \n", "\n", "\n", "Nx,Ny = 40,50\n", "dx,dy = 1.4,2.7\n", "x0,y0 = .2,1.3\n", "xmin,xmax = x0-dx, x0+dx\n", "ymin,ymax = y0-dy, y0+dy\n", "x = np.linspace(xmin,xmax,Nx)\n", "y = np.linspace(ymin,ymax,Ny)\n", "x,y = np.meshgrid(x,y)\n", "surface_plot2 = k3d.surface(np.sin(x**2+y**2),xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, color=int(np.random.randint(0, 0xFFFFFF, 1)[0]))\n", "plot += surface_plot2\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## text\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "texture_text = k3d.texture_text(\"K3D.text(\\\"X max\\\", position=(3, 0, 0) )\", position=(0, 0, 1), size=10)\n", "plot += texture_text" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "text = k3d.text(\"O\", position=(0, 0, 1) )\n", "plot += text" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "text.text = \"\\int_0^1 dx \\sin x\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import time\n", "for i in range(26):\n", " text.position = [0,0,1+np.sin(0.1*i)]\n", " time.sleep(0.1)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## points" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = x.flatten()[::4]\n", "y = y.flatten()[::4]\n", "z = np.sin(x**2+y**2) \n", "points = np.vstack([x,y,z]).T \n", "points_plt = k3d.points(points,colors=np.ones_like(x)*0xFF0F00 ,point_size=.1)\n", "plot += points_plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## line " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Nx,Ny = 10,65" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.linspace(-3,0,Nx)\n", "y = np.linspace(-3,0,Ny)\n", "x, y = np.meshgrid(x,y)\n", "\n", "for i in range(0,Nx):\n", " x_ = x[:,i]\n", " y_ = y[:,i]\n", " z = np.sin(x_**2+y_**2)\n", " points = np.vstack([x_,y_,z]).T\n", " vars()['line_%d'%i] = k3d.line(points,color=0xFF0000 ,width=1)\n", " plot += vars()['line_%d'%i]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## vectors" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Nx,Ny = 100,100\n", "x = np.linspace(xmin,xmax,Nx)\n", "y = np.linspace(ymin,ymax,Ny)\n", "dx, dy = x[1]-x[0],y[1]-y[0]\n", "\n", "x,y = np.meshgrid(x,y)\n", "z = np.sin(x**2+y**2)\n", "dx,dy \n", "if True:\n", " dFy,dFx = np.gradient(z)\n", " dFy,dFx = dFy/dy,dFx/dx \n", "else:\n", " dFx,dFy = 2*x*np.cos(x**2+y**2),2*y*np.cos(x**2+y**2)\n", "\n", "origins = np.vstack([x.flatten(),y.flatten(),z.flatten()]).T\n", "vectors = 0.01*(np.vstack([dFx.flatten(),dFy.flatten(),-np.ones(Nx*Ny)] )).T\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def normalized(a, axis=-1, order=2):\n", " l2 = np.atleast_1d(np.linalg.norm(a, order, axis))\n", " l2[l2==0] = 1\n", " return a / np.expand_dims(l2, axis)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "vector_plot = k3d.vectors(-0.4*normalized(vectors[::10]), origins[::10], color=0xffff00)\n", "plot += vector_plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# marching cubes\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Nz = Nx\n", "zmin,zmax = -1,1\n", "ymin,ymax = 2,6\n", "\n", "x = np.linspace(xmin,xmax,Nx//2)\n", "y = np.linspace(ymin,ymax,Ny//2)\n", "z = np.linspace(zmin,zmax,Nz//2)\n", "x,y,z = np.meshgrid(x,y,z)\n", "F = np.sin(x**2+y**2)-z\n", "\n", "marching_cubes =k3d.marching_cubes(np.moveaxis(F,2,0),\\\n", " xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, \\\n", " zmin=zmin, zmax=zmax, level=0.0,color=0xff0000)\n", "plot += marching_cubes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "plot.camera_auto_fit = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import widgets\n", "\n", "class plot_ctrl(object):\n", " plot = None\n", " def __init__(self,id1,plot):\n", " self.id = id1\n", " ref_names = [ n for n,v in globals().items() if self.id==id(v)] \n", " if len(ref_names)==1:\n", " desc = ref_names[0]\n", " else:\n", " desc = str(self.id)\n", " self.w = widgets.Checkbox(description=desc,value=True)\n", " self.w.observe(self.runme)\n", " if type(self).plot == None:\n", " type(self).plot = plot\n", " type(self).objs = plot.objects.copy()\n", "\n", " def runme(self,change):\n", " if change['name']=='value':\n", " for obj in type(self).objs:\n", " if obj.id == self.id: #int(self.w.description):\n", " if self.w.value==True:\n", " type(self).plot += obj\n", " #print(\"Added obj\")\n", " else:\n", " type(self).plot -= obj \n", " #print(\"Removed obj\")\n", " \n", " @classmethod\n", " def VBox(cls,plot,ncol = 4):\n", " names = {obj.id:obj.__class__.__name__ for obj in plot.objects}\n", " cls.l = [cls(id1=key,plot=plot) for key in names.keys()]\n", " \n", " if ncol < len(cls.l):\n", " checkboxes = widgets.VBox( [widgets.HBox([ el.w for el in cls.l[i:i+ncol]]) for i in range(0,len(cls.l),ncol)] ) \n", " else:\n", " checkboxes = widgets.VBox([el.w for el in cls.l])\n", " return widgets.VBox([checkboxes,plot])\n", " \n", "plot_ctrl.VBox(plot)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.0" }, "widgets": { "state": { "e8e906fba93843f3af98e01c81918353": { "views": [ { "cell_index": 2 } ] } }, "version": "1.2.0" } }, "nbformat": 4, "nbformat_minor": 1 }