Skip to content

Commit ec43169

Browse files
committed
Complete BufferAttributes
1 parent 0b4add5 commit ec43169

16 files changed

+208
-123
lines changed

examples/BufferAttributes and BufferGeometry.ipynb

Lines changed: 119 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
6-
"metadata": {
7-
"collapsed": true
8-
},
5+
"execution_count": 1,
6+
"metadata": {},
97
"outputs": [],
108
"source": [
119
"from pythreejs import *\n",
12-
"from IPython.display import display"
10+
"from IPython.display import display\n",
11+
"import numpy as np"
1312
]
1413
},
1514
{
1615
"cell_type": "code",
17-
"execution_count": null,
18-
"metadata": {
19-
"collapsed": true
20-
},
16+
"execution_count": 2,
17+
"metadata": {},
2118
"outputs": [],
2219
"source": [
2320
"#\n",
@@ -28,58 +25,49 @@
2825
},
2926
{
3027
"cell_type": "code",
31-
"execution_count": null,
32-
"metadata": {
33-
"collapsed": false
34-
},
28+
"execution_count": 3,
29+
"metadata": {},
3530
"outputs": [],
3631
"source": [
3732
"b = BufferAttribute(\n",
3833
" array=[\n",
39-
" 11, 12, 13,\n",
40-
" 21, 22, 23,\n",
41-
" 31, 32, 33\n",
34+
" [11, 12, 13],\n",
35+
" [21, 22, 23],\n",
36+
" [31, 32, 33],\n",
4237
" ],\n",
43-
" itemSize=3, \n",
4438
" normalized=False)"
4539
]
4640
},
4741
{
4842
"cell_type": "code",
49-
"execution_count": null,
50-
"metadata": {
51-
"collapsed": false
52-
},
43+
"execution_count": 4,
44+
"metadata": {},
5345
"outputs": [],
5446
"source": [
5547
"b.array = [ \n",
56-
" 11, 12, 13,\n",
57-
" 21, 00, 23,\n",
58-
" 31, 32, 33\n",
48+
" [11, 12, 13],\n",
49+
" [21, 00, 23],\n",
50+
" [31, 32, 33],\n",
5951
"]"
6052
]
6153
},
6254
{
6355
"cell_type": "code",
64-
"execution_count": null,
65-
"metadata": {
66-
"collapsed": true
67-
},
56+
"execution_count": 5,
57+
"metadata": {},
6858
"outputs": [],
6959
"source": [
7060
"# should be able to modify elements in the array directly \n",
7161
"# but currently, change events are not fired when arrays are \n",
7262
"# updated using element-wise assignment\n",
7363
"# WON'T WORK\n",
74-
"b.array[4] = 0"
64+
"b.array[0, 1] = 0"
7565
]
7666
},
7767
{
7868
"cell_type": "code",
79-
"execution_count": null,
80-
"metadata": {
81-
"collapsed": true
82-
},
69+
"execution_count": 6,
70+
"metadata": {},
8371
"outputs": [],
8472
"source": [
8573
"#\n",
@@ -89,51 +77,126 @@
8977
},
9078
{
9179
"cell_type": "code",
92-
"execution_count": null,
93-
"metadata": {
94-
"collapsed": true
95-
},
80+
"execution_count": 7,
81+
"metadata": {},
9682
"outputs": [],
9783
"source": [
9884
"# Simple square\n",
9985
"vertices = BufferAttribute(\n",
100-
" array=[\n",
101-
" -1.0, -1.0, 1.0,\n",
102-
" 1.0, -1.0, 1.0,\n",
103-
" 1.0, 1.0, 1.0,\n",
86+
" array=np.array([\n",
87+
" [-1.0, -1.0, 1.0],\n",
88+
" [ 1.0, -1.0, 1.0],\n",
89+
" [ 1.0, 1.0, 1.0],\n",
10490
" \n",
105-
" 1.0, 1.0, 1.0,\n",
106-
" -1.0, 1.0, 1.0,\n",
107-
" -1.0, -1.0, 1.0\n",
108-
" ],\n",
109-
" itemSize=3,\n",
110-
" normalized=False)\n",
111-
"\n",
112-
"geometry = BufferGeometry(\n",
113-
" \n",
91+
" [ 1.0, 1.0, 1.0],\n",
92+
" [-1.0, 1.0, 1.0],\n",
93+
" [-1.0, -1.0, 1.0],\n",
94+
" ], dtype=np.float32),\n",
95+
" normalized=False)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 8,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"geometry = PlainBufferGeometry(\n",
105+
" attributes={'position': vertices},\n",
114106
")"
115107
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 9,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"data": {
116+
"application/vnd.jupyter.widget-view+json": {
117+
"model_id": "37642eda6faf4d1091b88bca6647ad87",
118+
"version_major": "2",
119+
"version_minor": "0"
120+
},
121+
"text/plain": [
122+
"A Jupyter Widget"
123+
]
124+
},
125+
"metadata": {},
126+
"output_type": "display_data"
127+
}
128+
],
129+
"source": [
130+
"geometry"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": 10,
136+
"metadata": {},
137+
"outputs": [
138+
{
139+
"data": {
140+
"text/plain": [
141+
"float"
142+
]
143+
},
144+
"execution_count": 10,
145+
"metadata": {},
146+
"output_type": "execute_result"
147+
}
148+
],
149+
"source": [
150+
"np.float"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": 11,
156+
"metadata": {},
157+
"outputs": [
158+
{
159+
"data": {
160+
"text/plain": [
161+
"numpy.float64"
162+
]
163+
},
164+
"execution_count": 11,
165+
"metadata": {},
166+
"output_type": "execute_result"
167+
}
168+
],
169+
"source": [
170+
"np.float64"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": null,
176+
"metadata": {},
177+
"outputs": [],
178+
"source": []
116179
}
117180
],
118181
"metadata": {
119182
"kernelspec": {
120-
"display_name": "Python [py27]",
183+
"display_name": "Python 3",
121184
"language": "python",
122-
"name": "Python [py27]"
185+
"name": "python3"
123186
},
124187
"language_info": {
125188
"codemirror_mode": {
126189
"name": "ipython",
127-
"version": 2
190+
"version": 3
128191
},
129192
"file_extension": ".py",
130193
"mimetype": "text/x-python",
131194
"name": "python",
132195
"nbconvert_exporter": "python",
133-
"pygments_lexer": "ipython2",
134-
"version": "2.7.12"
196+
"pygments_lexer": "ipython3",
197+
"version": "3.5.3"
135198
}
136199
},
137200
"nbformat": 4,
138-
"nbformat_minor": 0
201+
"nbformat_minor": 1
139202
}

js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
"dependencies": {
3131
"@jupyter-widgets/base": "0.5.1",
32-
"ndarray": "^1.0.18",
32+
"jupyter-datawidgets": "^1.0.0",
3333
"three": "^0.85.2",
3434
"underscore": "^1.8.3"
3535
}

js/scripts/prop-types.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,21 @@ _.extend(ArrayType.prototype, BaseType.prototype, {
258258
});
259259

260260
// TODO: support more than Float32Array
261-
function ArrayBufferType(arrayType) {
262-
// this.arrayType = arrayType || 'Float32Array';
261+
function ArrayBufferType(arrayType, shape_constraint) {
262+
this.arrayType = arrayType;
263+
this.shape_constraint = shape_constraint;
263264
this.defaultValue = [];
264265
}
265266
_.extend(ArrayBufferType.prototype, BaseType.prototype, {
266267
getTraitlet: function() {
267-
return 'Array().tag(sync=True, **array_serialization)';
268+
var r = `NDArray(dtype=${this.arrayType || 'None'}).tag(sync=True, **array_serialization)`;
269+
if (this.shape_constraint) {
270+
var constraint = this.shape_constraint.reduce(function(wip, element) {
271+
return wip + `, ${element === null ? 'None' : element}`;
272+
}, this);
273+
r += `.valid(shape_constraint([${this.shape_constraint}]))`
274+
}
275+
return r;
268276
},
269277
getPropertyConverterFn: function() {
270278
return 'convertArrayBuffer';

js/scripts/templates/js_wrapper.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
var _ = require('underscore');
77
var widgets = require('@jupyter-widgets/base');
8+
var datawidgets = require('jupyter-datawidgets');
89

910
var {{ superClass.modelName }} = require('{{ superClass.requirePath }}').{{ superClass.modelName }};
1011

js/scripts/templates/py_wrapper.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ from traitlets import (
66
Tuple, List, Dict, Float, CFloat, Bool, Union, Any,
77
)
88

9+
from ipydatawidgets import NDArray, array_serialization, NDArrayWidget
10+
911
from {{ py_base_relative_path }}enums import *
1012
from {{ py_base_relative_path }}traits import *
1113

js/scripts/three-class-config.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ module.exports = {
196196
array: new Types.ArrayBuffer(),
197197
count: new Types.Int(0),
198198
dynamic: new Types.Bool(false),
199-
itemSize: new Types.Int(1),
199+
//itemSize: new Types.Int(1), // Item size is inferred from array
200200
needsUpdate: new Types.Bool(false),
201201
normalized: new Types.Bool(true),
202202
version: new Types.Int(-1),
203203
},
204-
constructorArgs: [ 'array', 'itemSize', 'normalized' ],
204+
constructorArgs: [ 'array', 'normalized' ],
205205
propsDefinedByThree: [ 'count', 'version' ]
206206
},
207207
BufferGeometry: {
@@ -266,17 +266,13 @@ module.exports = {
266266
},
267267
},
268268
PlainBufferGeometry: {
269-
relativePath: './geometries/PlainGeometry',
270-
superClass: 'Geometry',
269+
relativePath: './geometries/PlainBufferGeometry',
270+
superClass: 'BufferGeometry',
271271
properties: {
272272
attributes: new Types.ThreeTypeDict('BufferAttribute'),
273-
position: new Types.ThreeType('BufferAttribute'),
274-
normal: new Types.ThreeType('BufferAttribute'),
275-
color: new Types.ThreeType('BufferAttribute'),
276-
index: new Types.ThreeType('BufferAttribute'),
277273
morphAttributes: new Types.ThreeTypeDict('BufferAttribute'),
278274
MaxIndex: new Types.Int(65535),
279-
//groups: new Types.Group(),
275+
//groups: new Types.GeometryGroup(),
280276
//drawRange: new Types.DrawRange(),
281277
_ref_geometry: new Types.ThreeType('Geometry', {allowNull: false}),
282278
},

js/src/_base/Three.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var _ = require('underscore');
22
var widgets = require("@jupyter-widgets/base");
33
var Promise = require('bluebird');
44
var $ = require('jquery');
5+
var ndarray = require('ndarray');
56

67
var Enums = require('./enums');
78

@@ -186,7 +187,6 @@ var ThreeModel = widgets.WidgetModel.extend({
186187
if (id != null) {
187188
return { id: id };
188189
}
189-
190190
return;
191191

192192
},
@@ -656,13 +656,11 @@ var ThreeModel = widgets.WidgetModel.extend({
656656

657657
// ArrayBuffer
658658
convertArrayBufferModelToThree: function(arr, propName) {
659-
// TODO: support other ArrayBuffer types
660-
return new Float32Array(arr);
659+
return arr.data;
661660
},
662661

663662
convertArrayBufferThreeToModel: function(arrBuffer, propName) {
664-
// TODO: support other ArrayBuffer types
665-
return Array.prototype.slice.call(arrBuffer);
663+
return ndarray(arrBuffer);
666664
},
667665

668666
// Color

0 commit comments

Comments
 (0)