Skip to content

Commit cd5dbbe

Browse files
authored
Merge pull request #80 from jasongrout/binaryarrays
Use binary arrays to transfer vertex/face/facecolor data for plain geometries
2 parents 590bce7 + 42646ba commit cd5dbbe

File tree

7 files changed

+244
-132
lines changed

7 files changed

+244
-132
lines changed

examples/Examples.ipynb

Lines changed: 94 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"metadata": {
77
"collapsed": true
88
},
@@ -24,19 +24,9 @@
2424
},
2525
{
2626
"cell_type": "code",
27-
"execution_count": 2,
27+
"execution_count": null,
2828
"metadata": {},
29-
"outputs": [
30-
{
31-
"data": {
32-
"application/vnd.jupyter.widget-view+json": {
33-
"model_id": "0f14e88a9bb9477e9aca2906b06100ff"
34-
}
35-
},
36-
"metadata": {},
37-
"output_type": "display_data"
38-
}
39-
],
29+
"outputs": [],
4030
"source": [
4131
"ball = Mesh(geometry=SphereGeometry(radius=1), \n",
4232
" material=LambertMaterial(color='red'),\n",
@@ -56,7 +46,7 @@
5646
},
5747
{
5848
"cell_type": "code",
59-
"execution_count": 3,
49+
"execution_count": null,
6050
"metadata": {
6151
"collapsed": true
6252
},
@@ -67,7 +57,7 @@
6757
},
6858
{
6959
"cell_type": "code",
70-
"execution_count": 4,
60+
"execution_count": null,
7161
"metadata": {
7262
"collapsed": true
7363
},
@@ -91,30 +81,11 @@
9181
},
9282
{
9383
"cell_type": "code",
94-
"execution_count": 5,
84+
"execution_count": null,
9585
"metadata": {
9686
"scrolled": true
9787
},
98-
"outputs": [
99-
{
100-
"data": {
101-
"application/vnd.jupyter.widget-view+json": {
102-
"model_id": "a875f24e139343708b61edf0346dd948"
103-
}
104-
},
105-
"metadata": {},
106-
"output_type": "display_data"
107-
},
108-
{
109-
"data": {
110-
"application/vnd.jupyter.widget-view+json": {
111-
"model_id": "78c0d687a9a2446bb140ab604c57e3aa"
112-
}
113-
},
114-
"metadata": {},
115-
"output_type": "display_data"
116-
}
117-
],
88+
"outputs": [],
11889
"source": [
11990
"nx, ny = (20, 20)\n",
12091
"xmax=1\n",
@@ -162,7 +133,7 @@
162133
},
163134
{
164135
"cell_type": "code",
165-
"execution_count": 6,
136+
"execution_count": null,
166137
"metadata": {
167138
"collapsed": true
168139
},
@@ -182,19 +153,9 @@
182153
},
183154
{
184155
"cell_type": "code",
185-
"execution_count": 7,
156+
"execution_count": null,
186157
"metadata": {},
187-
"outputs": [
188-
{
189-
"data": {
190-
"application/vnd.jupyter.widget-view+json": {
191-
"model_id": "82d74882be1a40abaa66bc448cd47ae1"
192-
}
193-
},
194-
"metadata": {},
195-
"output_type": "display_data"
196-
}
197-
],
158+
"outputs": [],
198159
"source": [
199160
"import numpy as np\n",
200161
"from scipy import ndimage\n",
@@ -238,19 +199,9 @@
238199
},
239200
{
240201
"cell_type": "code",
241-
"execution_count": 8,
202+
"execution_count": null,
242203
"metadata": {},
243-
"outputs": [
244-
{
245-
"data": {
246-
"application/vnd.jupyter.widget-view+json": {
247-
"model_id": "0912fdee6b9f492e9f0d20d410b7fd66"
248-
}
249-
},
250-
"metadata": {},
251-
"output_type": "display_data"
252-
}
253-
],
204+
"outputs": [],
254205
"source": [
255206
"# On windows, linewidth of the material has no effect\n",
256207
"size = 4\n",
@@ -260,14 +211,13 @@
260211
" [0, size, 0],\n",
261212
" [0, 0, 0],\n",
262213
" [0, 0, size]],\n",
263-
" faces=[[0,1,2],[2,3,1]],\n",
264214
" colors = ['red', 'red', 'green', 'green', 'white', 'orange'])\n",
265-
"lines = Mesh(geometry=linesgeom, \n",
266-
" material=BasicMaterial(linewidth=5, vertexColors='VertexColors'), \n",
215+
"lines = Line(geometry=linesgeom, \n",
216+
" material=LineBasicMaterial(linewidth=5, vertexColors='VertexColors'), \n",
267217
" type='LinePieces')\n",
268218
"scene = Scene(children=[lines, DirectionalLight(color='#ccaabb', position=[0,10,0]),AmbientLight(color='#cccccc')])\n",
269219
"c = PerspectiveCamera(position=[0, 10, 10])\n",
270-
"renderer = Renderer(camera=c, scene = scene, controls=[OrbitControls(controlling=c)])\n",
220+
"renderer = Renderer(camera=c, background='black', background_opacity=1, scene = scene, controls=[OrbitControls(controlling=c)])\n",
271221
"display(renderer)"
272222
]
273223
},
@@ -285,19 +235,9 @@
285235
},
286236
{
287237
"cell_type": "code",
288-
"execution_count": 9,
238+
"execution_count": null,
289239
"metadata": {},
290-
"outputs": [
291-
{
292-
"data": {
293-
"application/vnd.jupyter.widget-view+json": {
294-
"model_id": "59e7bbdd8d70458f8fdb9cc94750f1c8"
295-
}
296-
},
297-
"metadata": {},
298-
"output_type": "display_data"
299-
}
300-
],
240+
"outputs": [],
301241
"source": [
302242
"f = \"\"\"\n",
303243
"function f(origu,origv) {\n",
@@ -325,31 +265,86 @@
325265
"display(renderer)"
326266
]
327267
},
268+
{
269+
"cell_type": "markdown",
270+
"metadata": {},
271+
"source": [
272+
"# Indexed Geometries\n",
273+
"\n",
274+
"The PlainGeometry lets you specify vertices and faces for a surface."
275+
]
276+
},
328277
{
329278
"cell_type": "code",
330-
"execution_count": 10,
279+
"execution_count": null,
331280
"metadata": {},
332-
"outputs": [
333-
{
334-
"data": {
335-
"application/vnd.jupyter.widget-view+json": {
336-
"model_id": "23aec8b72cad43209c5c0ebcf95eac32"
337-
}
338-
},
339-
"metadata": {},
340-
"output_type": "display_data"
341-
}
342-
],
281+
"outputs": [],
343282
"source": [
344283
"from pythreejs import *\n",
345284
"import numpy as np\n",
346285
"from IPython.display import display\n",
347-
"from ipywidgets import HTML, Text\n",
348-
"from traitlets import link, dlink\n",
349286
"\n",
287+
"vertices = np.asarray([\n",
288+
"[0, 0, 0],\n",
289+
"[0, 0, 1],\n",
290+
"[0, 1, 0],\n",
291+
"[0, 1, 1],\n",
292+
"[1, 0, 0],\n",
293+
"[1, 0, 1],\n",
294+
"[1, 1, 0],\n",
295+
"[1, 1, 1]\n",
296+
"], dtype='float32')\n",
297+
"\n",
298+
"faces = np.asarray([\n",
299+
" [0, 1, 3],\n",
300+
" [0, 2, 3],\n",
301+
" [0, 2, 4],\n",
302+
" [2, 4, 6],\n",
303+
" [0, 1, 4],\n",
304+
" [1, 4, 5],\n",
305+
" [2, 3, 6],\n",
306+
" [3, 6, 7],\n",
307+
" [1, 3, 5],\n",
308+
" [3, 5, 7],\n",
309+
" [4, 5, 6],\n",
310+
" [5, 6, 7]\n",
311+
" ])\n",
312+
"\n",
313+
"vertexcolors = np.asarray([(0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,1,1), (1,0,1), (1,1,0), (1,1,1)])\n",
314+
"facecolors = [[vertexcolors[i] for i in f] for f in faces]\n",
315+
"cubeGeometry = PlainGeometry(vertices=vertices, faces=faces, faceColors = facecolors)\n",
316+
"\n",
317+
"myobjectCube = Mesh(geometry=cubeGeometry, material = LambertMaterial(vertexColors = 'VertexColors'))\n",
318+
"cCube = PerspectiveCamera(position=[3, 3, 3], fov=20,\n",
319+
" children=[DirectionalLight(color='#ffffff', position=[-3, 5, 1], intensity=0.5)])\n",
320+
"sceneCube = Scene(children=[myobjectCube, AmbientLight(color='#dddddd')])\n",
321+
"\n",
322+
"rendererCube = Renderer(camera=cCube, background='black', background_opacity=1,\n",
323+
" scene = sceneCube, controls=[OrbitControls(controlling=cCube)])\n",
324+
"\n",
325+
"display(rendererCube)"
326+
]
327+
},
328+
{
329+
"cell_type": "markdown",
330+
"metadata": {},
331+
"source": [
332+
"# Buffer Geometries\n",
333+
"\n",
334+
"The PlainBufferGeometry object uses several tricks to speed up both the transfer of data and the rendering of the data."
335+
]
336+
},
337+
{
338+
"cell_type": "code",
339+
"execution_count": null,
340+
"metadata": {},
341+
"outputs": [],
342+
"source": [
343+
"from pythreejs import *\n",
344+
"import numpy as np\n",
345+
"from IPython.display import display\n",
350346
"\n",
351-
"#vcm = LineBasicMaterial(vertexColors='VertexColors')\n",
352-
"vertices = [\n",
347+
"vertices = np.asarray([\n",
353348
"[0, 0, 0],\n",
354349
"[0, 0, 1],\n",
355350
"[0, 1, 0],\n",
@@ -358,9 +353,9 @@
358353
"[1, 0, 1],\n",
359354
"[1, 1, 0],\n",
360355
"[1, 1, 1]\n",
361-
"]\n",
356+
"], dtype='float32')\n",
362357
"\n",
363-
"faces = [\n",
358+
"faces = np.asarray([\n",
364359
" [0, 1, 3],\n",
365360
" [0, 2, 3],\n",
366361
" [0, 2, 4],\n",
@@ -373,13 +368,11 @@
373368
" [3, 5, 7],\n",
374369
" [4, 5, 6],\n",
375370
" [5, 6, 7]\n",
376-
" ]\n",
371+
" ])\n",
372+
"\n",
373+
"vertexcolors = np.asarray([(0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,1,1), (1,0,1), (1,1,0), (1,1,1)])\n",
377374
"\n",
378-
"cubeGeometry = PlainGeometry()\n",
379-
"cubeGeometry.vertices=vertices\n",
380-
"cubeGeometry.faces = faces\n",
381-
"vertexcolors = ['#000000', '#0000FF', '#00FF00', '#FF0000', '#00FFFF', '#FF00FF', '#FFFF00', '#FFFFFF']\n",
382-
"cubeGeometry.faceColors=[[vertexcolors[i] for i in f] for f in faces]\n",
375+
"cubeGeometry = PlainBufferGeometry(vertices=vertices, faces=faces, colors = vertexcolors)\n",
383376
"\n",
384377
"myobjectCube = Mesh(geometry=cubeGeometry, material = LambertMaterial(vertexColors = 'VertexColors'))\n",
385378
"cCube = PerspectiveCamera(position=[3, 3, 3], fov=20,\n",

js/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
"webpack": "^1.12.14"
2121
},
2222
"dependencies": {
23-
"jupyter-js-widgets": "^2.1.4",
2423
"@jupyterlab/nbwidgets": "^0.6.15",
24+
"jupyter-js-widgets": "^2.1.4",
25+
"ndarray": "^1.0.18",
2526
"three": "^0.75.0",
2627
"underscore": "^1.8.3"
2728
}

0 commit comments

Comments
 (0)