Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
renderers now work correctly for Graphics3dGroup and TransformGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
nilesjohnson committed May 26, 2015
1 parent 1f756f1 commit 6444541
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 14 deletions.
15 changes: 15 additions & 0 deletions src/sage/plot/plot3d/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,9 @@ class TransformGroup(Graphics3dGroup):
return self._bounding_box

def x3d_str(self):
rrr = renderers.x3d.X3dRenderer()
return rrr.render_transform_group(self, render_params=None)

r"""
To apply a transformation to a set of objects in x3d, simply make them
all children of an x3d Transform node.
Expand All @@ -1779,6 +1782,9 @@ class TransformGroup(Graphics3dGroup):
return s

def json_repr(self, render_params):
rrr = renderers.json.JsonRenderer()
return rrr.render_transform_group(self, render_params)

"""
Transformations are applied at the leaf nodes.
Expand All @@ -1795,6 +1801,9 @@ class TransformGroup(Graphics3dGroup):
return rep

def tachyon_repr(self, render_params):
rrr = renderers.tachyon.TachyonRenderer()
return rrr.render_transform_group(self, render_params)

"""
Transformations for Tachyon are applied at the leaf nodes.
Expand All @@ -1810,6 +1819,9 @@ class TransformGroup(Graphics3dGroup):
return rep

def obj_repr(self, render_params):
rrr = renderers.obj.ObjRenderer()
return rrr.render_transform_group(self, render_params)

"""
Transformations for .obj files are applied at the leaf nodes.
Expand Down Expand Up @@ -1841,6 +1853,9 @@ class TransformGroup(Graphics3dGroup):
return rep

def jmol_repr(self, render_params):
rrr = renderers.jmol.JMOLRenderer()
return rrr.render_transform_group(self, render_params)

r"""
Transformations for jmol are applied at the leaf nodes.
Expand Down
11 changes: 10 additions & 1 deletion src/sage/plot/plot3d/renderers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ def render_graphics3d(self, obj, render_params):
return ''

def render_graphics3d_group(self, obj, render_params):
"""
By default, apply render to each object in the group
"""
return [g.render(render_params, renderer=self) for g in obj.all]
def render_transform_group(self, obj, render_params):
return self.render_graphics3d_group(obj, render_params)
"""
Apply transformation to leaf nodes.
"""
render_params.push_transform(obj.get_transformation())
rep = [g.render(render_params, renderer=self) for g in obj.all]
render_params.pop_transform()
return rep

def render_primitive_object(self, obj, render_params):
return self.render_graphics3d(obj, render_params)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/plot/plot3d/renderers/canvas3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def render_graphics3d(self, obj, render_params):

# def render_graphics3d_group(self, obj, render_params):
# return self.render_graphics3d(obj, render_params)
def render_transform_group(self, obj, render_params):
return self.render_graphics3d_group(obj, render_params)
# def render_transform_group(self, obj, render_params):
# return self.render_graphics3d_group(obj, render_params)

def render_primitive_object(self, obj, render_params):
return self.render_graphics3d(obj, render_params)
Expand Down
8 changes: 4 additions & 4 deletions src/sage/plot/plot3d/renderers/jmol.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def render_graphics3d_image(self,obj):
def render_graphics3d(self, obj, render_params):
return ''

def render_graphics3d_group(self, obj, render_params):
return [g.render(render_params, renderer=self) for g in obj.all]
def render_transform_group(self, obj, render_params):
return self.render_graphics3d_group(obj, render_params)
# def render_graphics3d_group(self, obj, render_params):
# return [g.render(render_params, renderer=self) for g in obj.all]
# def render_transform_group(self, obj, render_params):
# return self.render_graphics3d_group(obj, render_params)

def render_primitive_object(self, obj, render_params):
return self.render_graphics3d(obj, render_params)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/plot/plot3d/renderers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def render_graphics3d(self, obj, render_params):

# def render_graphics3d_group(self, obj, render_params):
# return self.render_graphics3d(obj, render_params)
def render_transform_group(self, obj, render_params):
return self.render_graphics3d_group(obj, render_params)
# def render_transform_group(self, obj, render_params):
# return self.render_graphics3d_group(obj, render_params)

def render_primitive_object(self, obj, render_params):
return self.render_graphics3d(obj, render_params)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/plot/plot3d/renderers/obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def render_graphics3d(self, grob, render_params):

# def render_graphics3d_group(self, grob, render_params):
# return self.render_graphics3d(grob, render_params)
def render_transform_group(self, grob, render_params):
return self.render_graphics3d_group(grob, render_params)
# def render_transform_group(self, grob, render_params):
# return self.render_graphics3d_group(grob, render_params)

def render_primitive_grobect(self, grob, render_params):
return self.render_graphics3d(grob, render_params)
Expand Down
119 changes: 119 additions & 0 deletions src/sage/plot/plot3d/renderers/tachyon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from __future__ import absolute_import

import os
from . import register, Graphics3dRenderer


class TachyonRenderer(Graphics3dRenderer):

def render_graphics3d(self, obj, render_params):
"""
Unless otherwise changed, all rendering methods fall back to this
one.
"""
return ''

# def render_graphics3d_group(self, obj, render_params):
# return self.render_graphics3d(obj, render_params)
# def render_transform_group(self, obj, render_params):
# return self.render_graphics3d_group(obj, render_params)

def render_primitive_object(self, obj, render_params):
return self.render_graphics3d(obj, render_params)
def render_line(self, obj, render_params):
return self.render_primitive_objectd(obj, render_params)
def render_point(self, obj, render_params):
return self.render_primitive_object(obj, render_params)

def render_index_face_set(self, obj, render_params):

"""
Return a tachyon object for ``self``.
EXAMPLES:
A basic test with a triangle::
sage: G = polygon([(0,0,1), (1,1,1), (2,0,1)])
sage: s = G.tachyon_repr(G.default_render_params()); s
['TRI V0 0 0 1 V1 1 1 1 V2 2 0 1', ...]
A simple colored one::
sage: from sage.plot.plot3d.index_face_set import IndexFaceSet
sage: from sage.plot.plot3d.texture import Texture
sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)]
sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]]
sage: col = rainbow(10, 'rgbtuple')
sage: t_list=[Texture(col[i]) for i in range(10)]
sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list)
sage: S.tachyon_repr(S.default_render_params())
['TRI V0 2 0 0 V1 1 0 1 V2 1 1 0',
'TEXTURE... AMBIENT 0.3 DIFFUSE 0.7 SPECULAR 0 OPACITY 1.0... COLOR 1 0 0 ... TEXFUNC 0',...]
"""
transform = render_params.transform
lines = []
faces = obj.faces()

if transform is None:
transform_point = tuple
else:
transform_point = transform.transform_point

for face in faces:
P = transform_point(face[0])
Q = transform_point(face[1])
R = transform_point(face[2])
lines.append(format_tachyon_triangle(P, Q, R))
if True:#obj.global_texture:
lines.append(obj.texture.id)
else:
lines.append(format_tachyon_texture(face.color))
# stupid triangulation of polygon
if len(face) > 3:
for k in range(3,len(face)):
Q = R
R = transform_point(face[k])
lines.append(format_tachyon_triangle(P, Q, R))
if True:#obj.global_texture:
lines.append(obj.texture.id)
else:
lines.append(format_tachyon_texture(face.color))
return lines


def render_box(self, obj, render_params):
return self.render_index_face_set(obj, render_params)

def render_parametric_surface(self, obj, render_params):
obj.triangulate()
return self.render_index_face_set(obj, render_params)
def render_sphere(self, obj, render_params):
return self.render_parametric_surface(obj, render_params)
def render_cylinder(self, obj, render_params):
return self.render_parametric_surface(obj, render_params)
def render_torus(self, obj, render_params):
return self.render_parametric_surface(obj, render_params)
def render_cone(self, obj, render_params):
return self.render_parametric_surface(obj, render_params)
def render_mobius_strip(self, obj, render_params):
return self.render_parametric_surface(obj, render_params)

def render_implicit_surface(self, obj, render_params):
obj.triangulate()
return self.render_index_face_set(obj, render_params)

register(TachyonRenderer)


def format_tachyon_texture(rgb):
return "TEXTURE\n AMBIENT 0.3 DIFFUSE 0.7 SPECULAR 0 OPACITY 1.0\n COLOR %g %g %g \n TEXFUNC 0\n" % (rgb.r, rgb.g, rgb.b)

def format_tachyon_triangle(P, Q, R):
return "TRI V0 %g %g %g "%P + "V1 %g %g %g "%Q + "V2 %g %g %g\n"%R






4 changes: 2 additions & 2 deletions src/sage/plot/plot3d/renderers/wavefront.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def render_graphics3d(self, obj, render_params):

# def render_graphics3d_group(self, obj, render_params):
# return self.render_graphics3d(obj, render_params)
def render_transform_group(self, obj, render_params):
return self.render_graphics3d_group(obj, render_params)
# def render_transform_group(self, obj, render_params):
# return self.render_graphics3d_group(obj, render_params)

def render_primitive_object(self, obj, render_params):
return self.render_graphics3d(obj, render_params)
Expand Down
22 changes: 21 additions & 1 deletion src/sage/plot/plot3d/renderers/x3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,27 @@ def render_graphics3d_group(self, obj, render_params):
return "\n".join([g.x3d_str() for g in obj.all])

def render_transform_group(self, obj, render_params):
return self.render_graphics3d_group(obj, render_params)
r"""
To apply a transformation to a set of objects in x3d, simply make them
all children of an x3d Transform node.
EXAMPLES::
sage: sphere((1,2,3)).x3d_str()
"<Transform translation='1 2 3'>\n<Shape><Sphere radius='1.0'/><Appearance><Material diffuseColor='0.4 0.4 1.0' shininess='1.0' specularColor='0.0 0.0 0.0'/></Appearance></Shape>\n\n</Transform>"
"""
from sage.plot.plot3d.base import Graphics3dGroup
s = "<Transform"
if obj._rot is not None:
s += " rotation='%s %s %s %s'"%tuple(obj._rot)
if obj._trans is not None:
s += " translation='%s %s %s'"%tuple(obj._trans)
if obj._scale is not None:
s += " scale='%s %s %s'"%tuple(obj._scale)
s += ">\n"
s += Graphics3dGroup.x3d_str(obj)
s += "\n</Transform>"
return s

def render_primitive_object(self, obj, render_params):
return self.render_graphics3d(obj, render_params)
Expand Down

0 comments on commit 6444541

Please sign in to comment.