{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# K3D - Release 1.01\n", "\n", "Final tests\n", "\n", "\n", "\n", "\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import k3d\n", "k3d.version" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from k3d import K3D\n", "import numpy as np\n", "\n", "plot = K3D()\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, 0x0000ff, 0x0000ff, 0x00ff00, 0x00ff00), \n", " labels=('Axis x', 'Axis y', 'Axis z')\n", ")\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## surface \n", "\n", " \n", " \n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "Nx = 260\n", "Ny = 260\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,indexing='ij')\n", "plot += K3D.surface(np.sin(x**2+y**2),xmin=xmin,xmax=xmax,ymin=ymin,\\\n", " ymax=ymax, color=np.random.randint(0, 0xFFFFFF, 1)[0])\n", "\n", "plot.camera_auto_fit = False\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 18.9 ms, sys: 1.82 ms, total: 20.7 ms\n", "Wall time: 19.3 ms\n" ] } ], "source": [ "%%time \n", "\n", "for i in range(4):\n", " Nx,Ny = np.random.randint(80,100,size=2)\n", " dx,dy = 2*np.random.random(2)\n", " x0,y0 = 1*np.random.random(2)\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,indexing='ij')\n", " plot += K3D.surface(np.sin(x**2+y**2),\\ \n", " xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, color=np.random.randint(0, 0xFFFFFF, 1)[0])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## text\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot += K3D.text(\"O\", position=(0, 0, 0) )\n", "plot += K3D.text(\"X max\", position=(3, 0, 0) )\n", "plot += K3D.text(\"X min\", position=(-3, 0, 0) )\n", "plot += K3D.text(\"Z min\", position=(0, 0, -1) )\n", "plot += K3D.text(\"Z max\", position=(0, 0, 1) )\n", "plot += K3D.text(\"Y min\", position=(0, -3, 0) )\n", "plot += K3D.text(\"Y max\", position=(0, 3, 0) )" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## points" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = x.flatten()\n", "y = y.flatten()\n", "z = np.sin(x**2+y**2) \n", "points = np.vstack([x,y,z]).T \n", "plot += K3D.points(points,colors=np.ones_like(x)*0xFF0F00 ,point_size=4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## line " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.linspace(xmin,xmax,Nx)\n", "y = np.linspace(ymin,ymax,Ny)\n", "x, y = np.meshgrid(x,y,indexing='ij')\n", "\n", "for i in range(0,Ny-1,5):\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", " plot += K3D.line(points,color=0x00FF00 ,width=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## vectors" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "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,indexing='ij')\n", "z = np.sin(x**2+y**2)\n", "dx,dy \n", "dFx,dFy = np.gradient(z)\n", "\n", "origins = np.vstack([x.flatten(),y.flatten(),z.flatten()]).T\n", "vectors = 0.01*(np.vstack([dFx.flatten()/dx,dFy.flatten()/dy,-np.ones(Nx*Ny)] )).T\n", "vector_plot = K3D.vectors(origins,vectors)\n", "plot += vector_plot" ] }, { "cell_type": "code", "execution_count": 9, "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": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot -= vector_plot\n", "vector_plot = K3D.vectors(origins[::8],-0.4*normalized(vectors[::8]),color=0x00ff00, line_width=2)\n", "plot += vector_plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# marching cubes\n", "\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Nz = Nx\n", "zmin,zmax = -1,1\n", "ymin,ymax = -1,3\n", "\n", "x = np.linspace(xmin,xmax,Nx)\n", "y = np.linspace(ymin,ymax,Ny)\n", "z = np.linspace(zmin,zmax,Nz)\n", "x,y,z = np.meshgrid(x,y,z,indexing='ij')\n", "plot += K3D.marching_cubes(np.sin(x**2+y**2)-z,\\\n", " xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, \\\n", " zmin=zmin, zmax=zmax, level=0.0,color=0xff0000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }