Skip to content

Commit 41a0e87

Browse files
committed
initial (broken) support for binary synchronization of PlainGeometry info.
1 parent 9415747 commit 41a0e87

File tree

4 files changed

+78
-10
lines changed

4 files changed

+78
-10
lines changed

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
}

js/src/jupyter-threejs.js

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

44
window.THREE = THREE;
@@ -588,7 +588,6 @@ define(["jupyter-js-widgets", "underscore", "three"],
588588
},
589589
});
590590

591-
592591

593592
var PlainGeometryView = ThreeView.extend({
594593
update: function() {
@@ -1657,18 +1656,79 @@ define(["jupyter-js-widgets", "underscore", "three"],
16571656
})
16581657
});
16591658

1659+
1660+
1661+
var typesToArray = {
1662+
int8: Int8Array,
1663+
int16: Int16Array,
1664+
int32: Int32Array,
1665+
int64: Int64Array,
1666+
uint8: UInt8Array,
1667+
uint16: UInt16Array,
1668+
uint32: UInt32Array,
1669+
uint64: UInt64Array,
1670+
float32: Float32Array,
1671+
float64: Float64Array
1672+
}
1673+
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+
1703+
var JSONToArray = function(obj, manager) {
1704+
// obj is {shape: list, dtype: string, array: buffer}
1705+
// return an ndarray object
1706+
return ndarray(typesToArray[obj.dtype](obj.buffer), obj.shape);
1707+
}
1708+
1709+
var arrayToJSON = function(obj, manager) {
1710+
// serialize to {shape: list, dtype: string, array: buffer}
1711+
return {shape: obj.shape, dtype: obj.dtype, buffer: obj.data}
1712+
}
1713+
16601714
var PlainGeometryModel = GeometryModel.extend({
16611715
defaults: _.extend({}, GeometryModel.prototype.defaults, {
16621716
_model_name: 'PlainGeometryModel',
16631717
_view_name: 'PlainGeometryView',
16641718

1665-
vertices: [],
1719+
vertices: ndarray(new Float32Array(), [0,3]),
16661720
colors: [],
1667-
faces: [],
1668-
faceColors: [],
1721+
faces: ndarray(new UInt64Array(), [0, 3]),
1722+
faceColors: ndarray(new UInt64(), [0,3,3]),
16691723
faceNormals: []
16701724
// todo: faceVertexUvs
16711725
})
1726+
}, {
1727+
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 },
1731+
}, GeometryModel.serializers)
16721732
});
16731733

16741734
var SphereGeometryModel = GeometryModel.extend({

pythreejs/pythreejs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
CFloat, Bool)
1818
from ._package import npm_pkg_name
1919
from math import pi, sqrt
20+
from .traits_numpy import array_serialization, shape_constraints
21+
from traittypes import Array
2022

2123
def vector3(trait_type=CFloat, default=None, **kwargs):
2224
if default is None:
@@ -268,11 +270,16 @@ class PlainGeometry(Geometry):
268270
_view_name = Unicode('PlainGeometryView').tag(sync=True)
269271
_model_name = Unicode('PlainGeometryModel').tag(sync=True)
270272

271-
vertices = List(vector3(CFloat)).tag(sync=True)
273+
# 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))
276+
# 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))
278+
#vertices = List(vector3(CFloat)).tag(sync=True)
272279
colors = List(Color).tag(sync=True)
273-
faces = List(List(CFloat)).tag(sync=True)
280+
#faces = List(List(CFloat)).tag(sync=True)
274281
# TODO: type this better. Can handle lists of string colors, or lists of lists of string colors.
275-
faceColors = Tuple().tag(sync=True)
282+
#faceColors = Tuple().tag(sync=True)
276283
# TODO: type this better. Can handle lists of vector3(CFloat), or lists of lists of vector3(CFloat).
277284
faceNormals = Tuple().tag(sync=True)
278285
# todo: faceVertexUvs = List(vector3(vector2(CFloat))).tag(sync=True)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def run(self):
135135
'pythreejs/staticlab/jupyter-threejs.bundle.js.manifest',
136136
]),
137137
],
138-
'install_requires': ['ipywidgets>=6.0.0'],
138+
'install_requires': ['ipywidgets>=6.0.0', 'traittypes'],
139139
'packages': find_packages(),
140140
'zip_safe': False,
141141
'cmdclass': {

0 commit comments

Comments
 (0)