# Ticket #14428: tachyon_converter.sage

File tachyon_converter.sage, 3.3 KB (added by , 7 years ago) |
---|

Line | |
---|---|

1 | """ |

2 | Converts3d graphics objects to Tachyon objects |

3 | |

4 | Also creates new Tachyon object from a tachyon scene string |

5 | |

6 | EXAMPLES |

7 | |

8 | Join two Tachyon objects:: |

9 | |

10 | sage: f = lambda t: (t,t^2,t^3) |

11 | sage: T = Tachyon(camera_center=(5,0,4)) |

12 | sage: T.texture('t') |

13 | sage: T.light((-20,-20,40), 0.2, (1,1,1)) |

14 | sage: T.parametric_plot(f,-5,5,'t',min_depth=6) |

15 | |

16 | sage: def s(x,y): return float(x^3-3*x*y^2) |

17 | sage: t=Tachyon() |

18 | sage: t.light((4,3,2),0.2,(1,1,1)) |

19 | sage: t.texture('tex',ambient=.1,diffuse=.9,specular=.1,opacity=1,color=(1,0,0)) |

20 | sage: t.plot(s,(-1,1),(-1,1),'tex') |

21 | |

22 | sage: P = scene_string_tachyon_obj(t.str(),T) |

23 | sage: P.show() |

24 | |

25 | Convert a Graphics3d object to Tachyon, and join it with another Tachyon object:: |

26 | |

27 | sage: var('r,t'); |

28 | sage: enneper_scale = 6 |

29 | sage: enneper_x(r,t) = enneper_scale*(r*cos(t) - (r*cos(t))^3/3 + (r*cos(t))*(r*sin(t))^2) |

30 | sage: enneper_y(r,t) = enneper_scale*(-r*sin(t) + (r*sin(t))^3/3 - (r*sin(t))*(r*cos(t))^2) |

31 | sage: enneper_z(r,t) = enneper_scale*((r*cos(t))^2 - (r*sin(t))^2) |

32 | |

33 | sage: E = parametric_plot3d([enneper_x,enneper_y,enneper_z], (r,0,2), (t,0,2*pi), color=Color(.8,1,1, space='hsv'), frame=False) |

34 | |

35 | sage: T = Tachyon(camera_center=(5,2,2), look_at=(0,1,0)) |

36 | sage: T.light((10,3,2), 0.2, (1,1,1)) |

37 | sage: T.texture('s', ambient=0.1, diffuse=0.9, specular=0.1, opacity=.6, color=(0,.5,0)) |

38 | sage: T.plane((0,0,0),(1,1,1),'s') |

39 | |

40 | sage: Et = graphic_obj_tachyon_obj(E,T) |

41 | sage: Et.show() |

42 | |

43 | |

44 | """ |

45 | |

46 | def graphic_obj_scene_string(G): |

47 | """ |

48 | Convert graphics object to tachyon scene string. |

49 | |

50 | This works for Graphics3d objects, but hasn't been |

51 | tested for other graphics types. |

52 | """ |

53 | opts = G._process_viewing_options({}) |

54 | prep = G._prepare_for_tachyon(opts['frame'],opts['axes'], opts['frame_aspect_ratio'],opts['aspect_ratio'],zoom=1) |

55 | scene_str = prep.tachyon() |

56 | return scene_str |

57 | |

58 | def scene_string_tachyon_obj(scene_str,T=None): |

59 | """ |

60 | Extract Tachyon objects from given scene string. |

61 | |

62 | Do this by throwing away everything in the |

63 | resolution and camera blocks, and stripping the 'end_scene' |

64 | away. |

65 | |

66 | Either append the resulting objects to given Tachyon object T, |

67 | or create new Tachyon object |

68 | """ |

69 | if T is None: |

70 | # just some basic Tachyon object |

71 | # really you probably want to make your own |

72 | T = Tachyon(xres=500,yres=500, camera_center=(2,0,0)) |

73 | T.light((4,3,2), 0.2, (1,1,1)) |

74 | pre,camera_stuff,post = scene_str.split('camera') |

75 | obj_str = post.replace('\nend_scene','') |

76 | |

77 | # |

78 | # The key here is that Tachyon objects render |

79 | # themselves by calling the .str() method on |

80 | # each item in the ._objects list. |

81 | # |

82 | # So we just add something to the list which |

83 | # produces the appropriate string. |

84 | # |

85 | T._objects.append(StringContainer(obj_str)) |

86 | return T |

87 | |

88 | def graphic_obj_tachyon_obj(G,T=None): |

89 | """ |

90 | Convert Graphics3d object to Tachyon object, either by creating a new |

91 | one, or appending to the given object T. |

92 | """ |

93 | scene_str = graphic_obj_scene_string(G) |

94 | return scene_string_tachyon_obj(scene_str,T) |

95 | |

96 | |

97 | |

98 | class StringContainer(SageObject): |

99 | """ |

100 | Just a thing that has a .str() method and returns the given string |

101 | """ |

102 | def __init__(self,obj_str): |

103 | self._obj_str = str(obj_str) |

104 | def str(self): |

105 | return self._obj_str |