Created
April 5, 2019 19:52
-
-
Save PBrockmann/ee69c705ab5fca8854323e807cbd8863 to your computer and use it in GitHub Desktop.
Test on updating a BufferGeometry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Test on updating a BufferGeometry" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pythreejs import *\n", | |
"from IPython.display import display" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from ipywidgets import widgets" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"view_width = 600\n", | |
"view_height = 600\n", | |
"radius = 100" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def vertex(point, radius):\n", | |
" lambda1 = point[0] * np.pi / 180\n", | |
" phi1 = point[1] * np.pi / 180\n", | |
" return [\n", | |
" radius * np.cos(phi1) * np.cos(lambda1),\n", | |
" radius * np.sin(phi1),\n", | |
" -radius * np.cos(phi1) * np.sin(lambda1)\n", | |
" ]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"180 90\n" | |
] | |
} | |
], | |
"source": [ | |
"# Create 2 axis to build a grid\n", | |
"delta = 2. # must be a float 4. is a crude resolution, should be 1.\n", | |
"axis1 = np.arange(-180+delta/2, 180+delta/2, delta)\n", | |
"axis2 = np.arange(-90+delta/2, 90+delta/2, delta)\n", | |
"isize=len(axis1)\n", | |
"jsize=len(axis2)\n", | |
"print isize, jsize" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2.4534391582786697e-06 0.9999966171132442\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"(16200, 12)" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Generate a variable for testing (normally read from a netCDF file)\n", | |
"# Example with a 12 time step (lstep)\n", | |
"b = np.random.rand(isize*jsize,12)\n", | |
"print b.min(), b.max()\n", | |
"b.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Create color map \n", | |
"\n", | |
"import matplotlib\n", | |
"from matplotlib import cm\n", | |
"\n", | |
"norm = matplotlib.colors.Normalize(vmin=b.min(), vmax=b.max(), clip=True)\n", | |
"cmap = matplotlib.cm.viridis" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Create position list\n", | |
"\n", | |
"p = []\n", | |
"dx = 360/isize\n", | |
"dy = 180/jsize\n", | |
"for i in range(0,isize):\n", | |
" x = axis1[i]\n", | |
" for j in range(0,jsize):\n", | |
" y = axis2[j]\n", | |
" pt = [ [x - dx/2, y - dy/2],\n", | |
" [x + dx/2, y - dy/2],\n", | |
" [x - dx/2, y + dy/2],\n", | |
" [x + dx/2, y + dy/2] ]\n", | |
" \n", | |
" # quad (0,1,2,3) is made from 2 triangles\n", | |
" # face 1: 0,1,3\n", | |
" p.append(vertex(pt[0], radius))\n", | |
" p.append(vertex(pt[1], radius))\n", | |
" p.append(vertex(pt[3], radius))\n", | |
" \n", | |
" # face 1: 0,3,2\n", | |
" p.append(vertex(pt[0], radius))\n", | |
" p.append(vertex(pt[3], radius))\n", | |
" p.append(vertex(pt[2], radius)) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def changeColors(arr):\n", | |
" c = []\n", | |
" for i in range(0,len(arr)):\n", | |
" [r,g,b] = matplotlib.colors.to_rgb(cmap(norm(arr[i])))\n", | |
" c.append([r,g,b])\n", | |
" c.append([r,g,b])\n", | |
" c.append([r,g,b])\n", | |
" c.append([r,g,b])\n", | |
" c.append([r,g,b])\n", | |
" c.append([r,g,b])\n", | |
" return c" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"lstep = 0\n", | |
"def changeMeshColors(ev):\n", | |
" global lstep\n", | |
" geometry.attributes['color'].array = changeColors(b[:,lstep]) \n", | |
" lstep +=1\n", | |
" if lstep > b.shape[1]: lstep = 0\n", | |
" \n", | |
"button = widgets.Button(description=\"Next step\")\n", | |
"button.on_click(changeMeshColors)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"geometry = BufferGeometry(attributes=dict(\n", | |
" position=BufferAttribute(p),\n", | |
" color=BufferAttribute(changeColors(b[:,lstep]))\n", | |
"))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"mesh = Mesh(\n", | |
" geometry=geometry,\n", | |
" material=MeshBasicMaterial(vertexColors='FaceColors'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "f394b7823f5c4d288ec2ea907b8aabfc", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"UmVuZGVyZXIoY2FtZXJhPVBlcnNwZWN0aXZlQ2FtZXJhKHBvc2l0aW9uPSg0MDAuMCwgNjAuMCwgMTAwLjApLCBxdWF0ZXJuaW9uPSgwLjAsIDAuMCwgMC4wLCAxLjApLCBzY2FsZT0oMS4wLCDigKY=\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "457c29f50ef04d2596e528c50967aa88", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"Button(description=u'Next step', style=ButtonStyle())" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"camera = PerspectiveCamera( position=[400, 60, 100], aspect=view_width/view_height)\n", | |
"scene = Scene(children=[mesh])\n", | |
"controls = OrbitControls(controlling=camera, enablePan=False)\n", | |
"renderer = Renderer(camera=camera, scene=scene,\n", | |
" controls=[controls],\n", | |
" width=view_width, height=view_height)\n", | |
"display(renderer, button)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"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.16" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment