Skip to content

Commit 4900ac5

Browse files
committed
Very, *very* rough wip binary array transfer.
But it works. It really works.
1 parent 41a0e87 commit 4900ac5

File tree

3 files changed

+84
-71
lines changed

3 files changed

+84
-71
lines changed

examples/Examples.ipynb

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
{
3131
"data": {
3232
"application/vnd.jupyter.widget-view+json": {
33-
"model_id": "0f14e88a9bb9477e9aca2906b06100ff"
33+
"model_id": "29cc65e6687a4d0b8521f061b4df0984"
3434
}
3535
},
3636
"metadata": {},
@@ -327,13 +327,13 @@
327327
},
328328
{
329329
"cell_type": "code",
330-
"execution_count": 10,
330+
"execution_count": 22,
331331
"metadata": {},
332332
"outputs": [
333333
{
334334
"data": {
335335
"application/vnd.jupyter.widget-view+json": {
336-
"model_id": "23aec8b72cad43209c5c0ebcf95eac32"
336+
"model_id": "14b729d1bb674cefb80aa5a9d0a3f990"
337337
}
338338
},
339339
"metadata": {},
@@ -349,7 +349,7 @@
349349
"\n",
350350
"\n",
351351
"#vcm = LineBasicMaterial(vertexColors='VertexColors')\n",
352-
"vertices = [\n",
352+
"vertices = np.asarray([\n",
353353
"[0, 0, 0],\n",
354354
"[0, 0, 1],\n",
355355
"[0, 1, 0],\n",
@@ -358,9 +358,9 @@
358358
"[1, 0, 1],\n",
359359
"[1, 1, 0],\n",
360360
"[1, 1, 1]\n",
361-
"]\n",
361+
"], dtype='float32')\n",
362362
"\n",
363-
"faces = [\n",
363+
"faces = np.asarray([\n",
364364
" [0, 1, 3],\n",
365365
" [0, 2, 3],\n",
366366
" [0, 2, 4],\n",
@@ -373,15 +373,15 @@
373373
" [3, 5, 7],\n",
374374
" [4, 5, 6],\n",
375375
" [5, 6, 7]\n",
376-
" ]\n",
376+
" ])\n",
377377
"\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",
378+
"vertexcolors = [(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",
379+
"faceColors=np.asarray([[vertexcolors[i] for i in f] for f in faces])\n",
383380
"\n",
384-
"myobjectCube = Mesh(geometry=cubeGeometry, material = LambertMaterial(vertexColors = 'VertexColors'))\n",
381+
"\n",
382+
"cubeGeometry = PlainGeometry(vertices=vertices, faces=faces, faceColors = faceColors)\n",
383+
"\n",
384+
"myobjectCube = Mesh(geometry=cubeGeometry, material = PhongMaterial(color='white', vertexColors = 'VertexColors'))\n",
385385
"cCube = PerspectiveCamera(position=[3, 3, 3], fov=20,\n",
386386
" children=[DirectionalLight(color='#ffffff', position=[-3, 5, 1], intensity=0.5)])\n",
387387
"sceneCube = Scene(children=[myobjectCube, AmbientLight(color='#dddddd')])\n",
@@ -392,6 +392,54 @@
392392
"display(rendererCube)"
393393
]
394394
},
395+
{
396+
"cell_type": "code",
397+
"execution_count": 12,
398+
"metadata": {},
399+
"outputs": [
400+
{
401+
"data": {
402+
"text/plain": [
403+
"array([[ 0., 0., 0.],\n",
404+
" [ 0., 0., 1.],\n",
405+
" [ 0., 1., 0.],\n",
406+
" [ 0., 1., 1.],\n",
407+
" [ 1., 0., 0.],\n",
408+
" [ 1., 0., 1.],\n",
409+
" [ 1., 1., 0.],\n",
410+
" [ 1., 1., 1.]], dtype=float32)"
411+
]
412+
},
413+
"execution_count": 12,
414+
"metadata": {},
415+
"output_type": "execute_result"
416+
}
417+
],
418+
"source": [
419+
"cubeGeometry.vertices"
420+
]
421+
},
422+
{
423+
"cell_type": "code",
424+
"execution_count": 7,
425+
"metadata": {},
426+
"outputs": [
427+
{
428+
"data": {
429+
"text/plain": [
430+
"'7.0.0.dev0'"
431+
]
432+
},
433+
"execution_count": 7,
434+
"metadata": {},
435+
"output_type": "execute_result"
436+
}
437+
],
438+
"source": [
439+
"import ipywidgets\n",
440+
"ipywidgets.__version__"
441+
]
442+
},
395443
{
396444
"cell_type": "markdown",
397445
"metadata": {},

js/src/jupyter-threejs.js

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
define(["jupyter-js-widgets", "underscore", "three", "ndarray"],
2-
function(widgets, _, THREE) {
2+
function(widgets, _, THREE, ndarray) {
33

44
window.THREE = THREE;
55
require("./examples/js/renderers/Projector.js");
@@ -607,22 +607,22 @@ define(["jupyter-js-widgets", "underscore", "three", "ndarray"],
607607
}
608608

609609
var toVec = function(a) {
610-
return new THREE.Vector3(a[0], a[1], a[2]);
610+
return new THREE.Vector3(a.get(0), a.get(1), a.get(2));
611611
}
612612
var toColor = function(a) {
613-
return new THREE.Color(a);
613+
return new THREE.Color(a.get(0), a.get(1), a.get(2));
614614
}
615615

616616
var i, len;
617617
var f;
618618
var face;
619-
for(i = 0, len=vertices.length; i<len; i+=1) {
620-
geometry.vertices.push(toVec(vertices[i]));
619+
for(i = 0, len=vertices.shape[0]; i<len; i+=1) {
620+
geometry.vertices.push(toVec(vertices.pick(i)));
621621
}
622-
for(i=0, len=faces.length; i<len; i+=1) {
623-
f = faces[i];
622+
for(i=0, len=faces.shape[0]; i<len; i+=1) {
623+
f = faces.pick(i, null);
624624
normal = faceNormals && faceNormals[i];
625-
color = faceColors && faceColors[i];
625+
color = faceColors && faceColors.pick(i);
626626
if (normal) {
627627
if (Array.isArray(normal[0])) {
628628
normal = normal.map(toVec);
@@ -631,13 +631,9 @@ define(["jupyter-js-widgets", "underscore", "three", "ndarray"],
631631
}
632632
}
633633
if (color) {
634-
if (Array.isArray(color)) {
635-
color = color.map(toColor);
636-
} else {
637-
color = toColor(color);
638-
}
634+
color = [color.pick(0), color.pick(1), color.pick(2)].map(toColor);
639635
}
640-
face = new THREE.Face3(f[0], f[1], f[2], normal, color);
636+
face = new THREE.Face3(f.get(0), f.get(1), f.get(2), normal, color);
641637
geometry.faces.push(face);
642638
}
643639
for(i=0, len=colors.length; i<len; i+=1) {
@@ -1662,48 +1658,17 @@ define(["jupyter-js-widgets", "underscore", "three", "ndarray"],
16621658
int8: Int8Array,
16631659
int16: Int16Array,
16641660
int32: Int32Array,
1665-
int64: Int64Array,
1666-
uint8: UInt8Array,
1667-
uint16: UInt16Array,
1668-
uint32: UInt32Array,
1669-
uint64: UInt64Array,
1661+
uint8: Uint8Array,
1662+
uint16: Uint16Array,
1663+
uint32: Uint32Array,
16701664
float32: Float32Array,
16711665
float64: Float64Array
16721666
}
16731667

1674-
var arrayToTypes = {
1675-
'Int8Array': 'int8',
1676-
'Int16Array': 'int16',
1677-
'Int32Array': 'int32',
1678-
'Int64Array': 'int64',
1679-
'UInt8Array': 'uint8',
1680-
'UInt16Array': 'uint16',
1681-
'UInt32Array': 'uint32',
1682-
'UInt64Array': 'uint64',
1683-
'Float32Array': 'float32',
1684-
'Float64Array': 'float64'
1685-
}
1686-
1687-
var makearray = function(buffer, dtype) {
1688-
var types = {
1689-
int8: Int8Array,
1690-
int16: Int16Array,
1691-
int32: Int32Array,
1692-
int64: Int64Array,
1693-
uint8: UInt8Array,
1694-
uint16: UInt16Array,
1695-
uint32: UInt32Array,
1696-
uint64: UInt64Array,
1697-
float32: Float32Array,
1698-
float64: Float64Array
1699-
}
1700-
return types[dtype](buffer);
1701-
}
1702-
17031668
var JSONToArray = function(obj, manager) {
1704-
// obj is {shape: list, dtype: string, array: buffer}
1669+
// obj is {shape: list, dtype: string, array: DataView}
17051670
// return an ndarray object
1706-
return ndarray(typesToArray[obj.dtype](obj.buffer), obj.shape);
1671+
return ndarray(new typesToArray[obj.dtype](obj.buffer.buffer), obj.shape);
17071672
}
17081673

17091674
var arrayToJSON = function(obj, manager) {
@@ -1718,16 +1683,16 @@ define(["jupyter-js-widgets", "underscore", "three", "ndarray"],
17181683

17191684
vertices: ndarray(new Float32Array(), [0,3]),
17201685
colors: [],
1721-
faces: ndarray(new UInt64Array(), [0, 3]),
1722-
faceColors: ndarray(new UInt64(), [0,3,3]),
1686+
faces: ndarray(new Uint32Array(), [0, 3]),
1687+
faceColors: ndarray(new Uint8Array(), [0,3,3]),
17231688
faceNormals: []
17241689
// todo: faceVertexUvs
17251690
})
17261691
}, {
17271692
serializers: _.extend({
1728-
vertices: { deserialize: json_to_array, serialize: array_to_json },
1729-
faces: { deserialize: json_to_array, serialize: array_to_json },
1730-
faceColors: { deserialize: json_to_array, serialize: array_to_json },
1693+
vertices: { deserialize: JSONToArray, serialize: arrayToJSON },
1694+
faces: { deserialize: JSONToArray, serialize: arrayToJSON },
1695+
faceColors: { deserialize: JSONToArray, serialize: arrayToJSON },
17311696
}, GeometryModel.serializers)
17321697
});
17331698

pythreejs/pythreejs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ class PlainGeometry(Geometry):
271271
_model_name = Unicode('PlainGeometryModel').tag(sync=True)
272272

273273
# TODO: vertices: numpy shape (*, 3), dtype float32
274-
vertices = Array(dtype='float32').tag(sync=True).valid(shape_constraints(None,3))
275-
faces = Array(dtype='uint32').tag(sync=True).valid(shape_constraints(None,3))
274+
vertices = Array(dtype='float32').tag(sync=True, **array_serialization).valid(shape_constraints(None,3))
275+
faces = Array(dtype='uint32').tag(sync=True, **array_serialization).valid(shape_constraints(None,3))
276276
# list of [[v1_r,v1_g,v1_b], [v2_r,v2_g,v2_b], [v3_r,v3_g,v3_b]] for each face
277-
faceColors = Array(dtype='uint8').tag(sync=True).valid(shape_constraints(None,3,3))
277+
faceColors = Array(dtype='float32').tag(sync=True, **array_serialization).valid(shape_constraints(None,3,3))
278278
#vertices = List(vector3(CFloat)).tag(sync=True)
279279
colors = List(Color).tag(sync=True)
280280
#faces = List(List(CFloat)).tag(sync=True)

0 commit comments

Comments
 (0)