Skip to content

Commit fb873df

Browse files
author
Jason Grout
committed
Prototype a sage pythreejs bridge
1 parent eb346b4 commit fb873df

File tree

2 files changed

+62
-67
lines changed

2 files changed

+62
-67
lines changed

pythreejs.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ require(["threejs-all", "notebook/js/widgets/widget"], function() {
266266
var vertices = this.model.get('vertices');
267267
var face3 = this.model.get('face3');
268268
var face4 = this.model.get('face4');
269-
var i, len;
269+
var facen = this.model.get('facen');
270+
var face;
271+
var i, f, len;
270272
var v0, v1, v2;
271273
var f0,f1,f2,f3;
272274
for(i = 0, len=vertices.length; i<len; i+=3) {
@@ -282,7 +284,14 @@ require(["threejs-all", "notebook/js/widgets/widget"], function() {
282284
geometry.faces.push(new THREE.Face3(f0, f1, f2));
283285
geometry.faces.push(new THREE.Face3(f0, f2, f3));
284286
}
285-
287+
for(i=0, len=facen.length; i<len; i++) {
288+
face = facen[i];
289+
f0 = face[0];
290+
for(f=1, lenf=f.length-1; f<lenf; f++) {
291+
geometry.faces.push(new THREE.Face3(f0, face[f], face[f+1]))
292+
}
293+
}
294+
286295
geometry.mergeVertices();
287296
geometry.computeFaceNormals();
288297
geometry.computeVertexNormals();

pythreejs.py

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -453,71 +453,6 @@ class SpotLight(PointLight):
453453
angle = CFloat(10, sync=True)
454454
exponent = CFloat(0.5, sync=True)
455455

456-
class SageGraphics(Mesh):
457-
plot = Instance('sage.plot.plot3d.base.Graphics3d')
458-
# TODO material type option
459-
460-
def _plot_changed(self, name, old, new):
461-
self.type = new.scenetree_json()['type']
462-
if (self.type == 'object'):
463-
self.type = new.scenetree_json()['geometry']['type']
464-
self.material = self.material_from_object(new)
465-
else:
466-
self.type = new.scenetree_json()['children'][0]['geometry']['type']
467-
self.material = self.material_from_other(new)
468-
if(self.type == 'index_face_set'):
469-
self.geometry = self.geometry_from_plot(new)
470-
elif(self.type == 'sphere'):
471-
self.geometry = self.geometry_from_sphere(new)
472-
elif(self.type == 'box'):
473-
self.geometry = self.geometry_from_box(new)
474-
475-
476-
def material_from_object(self, p):
477-
# TODO: do this without scenetree_json()
478-
t = p.texture.scenetree_json()
479-
m = LambertMaterial(side='DoubleSide')
480-
m.color = t['color']
481-
m.opacity = t['opacity']
482-
# TODO: support other attributes
483-
return m
484-
485-
def material_from_other(self, p):
486-
# TODO: do this without scenetree_json()
487-
t = p.scenetree_json()['children'][0]['texture']
488-
m = LambertMaterial(side='DoubleSide')
489-
m.color = t['color']
490-
m.opacity = t['opacity']
491-
# TODO: support other attributes
492-
return m
493-
494-
def geometry_from_box(self, p):
495-
g = BoxGeometry()
496-
g.width = p.scenetree_json()['geometry']['size'][0]
497-
g.height = p.scenetree_json()['geometry']['size'][1]
498-
g.depth = p.scenetree_json()['geometry']['size'][2]
499-
return g
500-
501-
def geometry_from_sphere(self, p):
502-
g = SphereGeometry()
503-
g.radius = p.scenetree_json()['children'][0]['geometry']['radius']
504-
return g
505-
506-
def geometry_from_plot(self, p):
507-
from itertools import groupby, chain
508-
def flatten(ll):
509-
return list(chain.from_iterable(ll))
510-
p.triangulate()
511-
512-
g = FaceGeometry()
513-
g.vertices = flatten(p.vertices())
514-
f = p.index_faces()
515-
f.sort(key=len)
516-
faces = {k:flatten(v) for k,v in groupby(f,len)}
517-
g.face3 = faces.get(3,[])
518-
g.face4 = faces.get(4,[])
519-
return g
520-
521456
lights = {
522457
'colors': [
523458
AmbientLight(color=(0.312,0.188,0.4)),
@@ -534,3 +469,54 @@ def flatten(ll):
534469
DirectionalLight(position=[-1,-1,-1], color=[.7,.7,.7]),
535470
],
536471
}
472+
473+
#######################################################################
474+
## Sage Graphics
475+
#######################################################################
476+
477+
def convert_sage_graphics(p):
478+
D = p.scenetree_json()
479+
obj = sage_handlers[D['type']](D)
480+
# TODO: make a scene, renderer, and camera, and put obj inside of the scene as a child
481+
# return the renderer object.
482+
return obj
483+
484+
def json_object(j):
485+
geometry = sage_handlers[j['geometry']['type']](j['geometry'])
486+
material = sage_handlers['texture'](j['texture'])
487+
return Mesh(geometry = geometry, material = material)
488+
489+
def json_group(j):
490+
m = j['matrix']
491+
# TODO: transpose m
492+
children = [sage_handlers[c['type']](c) for c in j['children']]
493+
return Object3d(matrix=m, children=children)
494+
495+
def json_texture(j)
496+
return LambertMaterial(side='DoubleSide',
497+
color = j['color'],
498+
opacity = j['opacity']
499+
transparent = j['opacity'] < 1)
500+
def json_index_face_set(j):
501+
# TODO: add a facen attribute to FaceGeometry
502+
g = FaceGeometry(vertices = j['vertices'],
503+
face3 = j['face3'],
504+
face4 = j['face4'],
505+
facen = j['facen'])
506+
507+
def json_sphere(j):
508+
return SphereGeometry(radius = j['radius'])
509+
510+
def json_box(j):
511+
return BoxGeometry(width = j['size'][0],
512+
height = j['size'][1],
513+
depth = j['size'][2])
514+
515+
sage_handlers = {
516+
'object': json_object,
517+
'group': json_group,
518+
'texture': json_texture
519+
'index_face_set': json_index_face_set,
520+
'sphere': json_sphere,
521+
'box': json_box,
522+
}

0 commit comments

Comments
 (0)