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

Commit

Permalink
Generate names for 3d graphics files independently.
Browse files Browse the repository at this point in the history
Instead of basing everything on a single call to graphics_filename,
we now have one call for (almost) every file which is being generated.
This ensures that the files with the corresponding extensions are indeed new.
Up until now, creating multiple 3d results in a single cell would likely
lead to clashes.
  • Loading branch information
gagern committed Sep 5, 2014
1 parent 61809d6 commit 84a2f30
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/sage/plot/plot3d/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ from sage.modules.free_module_element import vector

from sage.rings.real_double import RDF
from sage.misc.functional import sqrt, atan, acos
from sage.misc.temporary_file import tmp_filename
from sage.misc.temporary_file import tmp_filename, graphics_filename

from texture import Texture, is_Texture
from transform cimport Transformation, point_c, face_c
Expand Down Expand Up @@ -1141,58 +1141,64 @@ end_scene""" % (render_params.antialiasing,
axes = opts['axes']

import sage.misc.misc
try:
filename = kwds.pop('filename')
except KeyError:
filename = tmp_filename()
basename = kwds.pop('filename', None)
def makename(ext):
if basename is not None:
return basename + ext
else:
# when testing this, modify graphics_filename so that it
# returns files which don't automatically share a base name,
# e.g.: "import random; i = random.randint(0, 10000)"
return graphics_filename(ext=ext)

from sage.plot.plot import EMBEDDED_MODE
from sage.doctest import DOCTEST_MODE
ext = None
filename = None

# Tachyon resolution options
if DOCTEST_MODE:
opts = '-res 10 10'
filename = os.path.join(sage.misc.misc.SAGE_TMP, "tmp")
basename = None
elif EMBEDDED_MODE:
opts = '-res %s %s'%(figsize[0]*100, figsize[1]*100)
filename = sage.misc.temporary_file.graphics_filename()[:-4]
basename = None
else:
opts = '-res %s %s'%(figsize[0]*100, figsize[1]*100)

if DOCTEST_MODE or viewer=='tachyon' or (viewer=='java3d' and EMBEDDED_MODE):
T = self._prepare_for_tachyon(frame, axes, frame_aspect_ratio, aspect_ratio, zoom)
tachyon_rt(T.tachyon(), filename+".png", verbosity, True, opts)
ext = "png"
filename = makename(".png")
tachyon_rt(T.tachyon(), filename, verbosity, True, opts)
import sage.misc.viewer
viewer_app = sage.misc.viewer.png_viewer()

if DOCTEST_MODE or viewer=='java3d':
f = open(filename+".obj", "w")
f.write("mtllib %s.mtl\n" % filename)
mtl = makename(".mtl")
filename = makename(".obj")
f = open(filename, "w")
f.write("mtllib %s\n" % mtl)
f.write(self.obj())
f.close()
f = open(filename+".mtl", "w")
f = open(mtl, "w")
f.write(self.mtl_str())
f.close()
ext = "obj"
viewer_app = os.path.join(sage.misc.misc.SAGE_LOCAL, "bin/sage3d")

if DOCTEST_MODE or viewer=='jmol':
# Temporary hack: encode the desired applet size in the end of the filename:
# (This will be removed once we have dynamic resizing of applets in the browser.)
base, ext = os.path.splitext(filename)
fg = figsize[0]
filename = '%s-size%s%s'%(base, fg*100, ext)
fg = figsize[0]*100
sizedname = lambda ext: makename("-size{}{}".format(fg*100, ext))

if EMBEDDED_MODE:
ext = "jmol"
# jmol doesn't seem to correctly parse the ?params part of a URL
archive_name = "%s-%s.%s.zip" % (filename, randint(0, 1 << 30), ext)
archive_name = sizedname(
"-{}.jmol.zip".format(randint(0, 1 << 30)))
filename = sizedname(".jmol")
else:
ext = "spt"
archive_name = "%s.%s.zip" % (filename, ext)
with open(filename + '.' + ext, 'w') as f:
archive_name = sizedname(".spt.zip".format(fg))
filename = sizedname(".spt")
with open(filename, 'w') as f:
f.write('set defaultdirectory "{0}"\n'.format(archive_name))
f.write('script SCRIPT\n')

Expand All @@ -1209,14 +1215,14 @@ end_scene""" % (render_params.antialiasing,
# button.
import sagenb
path = "cells/%s/%s" %(sagenb.notebook.interact.SAGE_CELL_ID, archive_name)
with open(filename + '.' + ext, 'w') as f:
with open(filename, 'w') as f:
f.write('set defaultdirectory "%s"\n' % path)
f.write('script SCRIPT\n')

# Filename for the static image
png_path = '.jmol_images'
sage.misc.misc.sage_makedirs(png_path)
png_name = os.path.join(png_path, filename + ".jmol.png")
png_name = os.path.join(png_path, filename + ".png")

from sage.interfaces.jmoldata import JmolData
jdata = JmolData()
Expand All @@ -1231,23 +1237,23 @@ end_scene""" % (render_params.antialiasing,
T = self._prepare_for_tachyon(frame, axes, frame_aspect_ratio, aspect_ratio, zoom)
tachyon_rt(T.tachyon(), png_name, verbosity, True, opts)

if viewer == 'canvas3d':
if DOCTEST_MODE or (viewer == 'canvas3d' and EMBEDDED_MODE):
T = self._prepare_for_tachyon(frame, axes, frame_aspect_ratio, aspect_ratio, zoom)
data = flatten_list(T.json_repr(T.default_render_params()))
f = open(filename + '.canvas3d', 'w')
filename = makename('.canvas3d')
f = open(filename, 'w')
f.write('[%s]' % ','.join(data))
f.close()
ext = 'canvas3d'

if ext is None:
if filename is None:
raise ValueError("Unknown 3d plot type: %s" % viewer)

if not DOCTEST_MODE and not EMBEDDED_MODE:
if verbosity:
pipes = "2>&1"
else:
pipes = "2>/dev/null 1>/dev/null &"
os.system('%s "%s.%s" %s' % (viewer_app, filename, ext, pipes))
os.system('%s "%s" %s' % (viewer_app, filename, pipes))

def save_image(self, filename=None, *args, **kwds):
r"""
Expand Down

0 comments on commit 84a2f30

Please sign in to comment.