Subject: [Viper-dev] A patch for writing simulation frames to eps/pdf/ps

From: Johan Hake <[email-masked]>
Date: Mon, 8 Sep 2008 22:27:18 +0200

Hello!

I added support for writing simulation frames to either eps/ps/pdf format. 
This is nice for high quality pictures for publications.

I used vtkGL2PSExporter that uses GL2PS to make the actuall writings. 

The patch is attached.

On another but related note. 
Would it be possible/usefull to add movie makeing support in viper? Scitools, 
the packages that Johannes is maintaining, have a very usefull function, 
movie(). In its simplest case you just call this function with eg:

   movie('image_*.eps')

It support many file formats, and a bunch of backends. You can check it out 
(literally) by:

 svn co https://svn.simula.no:40081/int/scriptingsuite/trunk/packages/scitools

(Its probably only available for internal simula, sorry...)

and look into :

  lib/scitools/easyviz/movie.py

and see what you think. I have used it to create some nice movies with png 
files from viper. A pseudo code example could be:

  p = plot(u)
  for t in range(10):
     u = new_values()
     p.update()
     p.write_png()
  p.make_movie()

What do you say?

Johan
# HG changeset patch
# User "Johan Hake <[email-masked]>"
# Date 1220904553 -7200
# Node ID 69c12190b87e76724a6ab118c03ea2ea15cbd965
# Parent  8f218239fb545e400f54f238e76f411b4903af21
Added:
 - support to write a simulation frame to a eps/ps/pdf image.
 - the keypress event of 'm' and 'n' for writing
   a simulation frame to a 'pdf' and 'eps' respectively
 - output information of keypress events for saving either
   data (vtk) or images (png,eps,pdf).

diff -r 8f218239fb54 -r 69c12190b87e src/viper/viper.py
--- a/src/viper/viper.py	Fri Sep 05 13:48:21 2008 +0200
+++ b/src/viper/viper.py	Mon Sep 08 22:09:13 2008 +0200
@@ -284,6 +284,12 @@
         self.pngwriter.SetInput(w2if.GetOutput())
         self.w2if = w2if
 
+        self.pswriter = vtk.vtkGL2PSExporter()
+        self.pswriter.SetRenderWindow(self.renWin)
+        self.psfilecounter  = -1
+        self.epsfilecounter = -1
+        self.pdffilecounter = -1
+        
         self.rawfilecounter = -1
 
     def write_vtk(self, filename=None):
@@ -307,6 +313,35 @@
         self.pngwriter.SetFileName(filename)
         self.w2if.Modified()
         self.pngwriter.Write()
+
+    def write_ps(self, filename=None, compress = False, format = "eps"):
+        """Write a simulation frame to file in eps/ps/pdf format.""" 
+        if not self.basename: self.init_writer()
+        assert(isinstance(format,str))
+        if compress:
+            self.pswriter.CompressOn()
+        else:
+            self.pswriter.CompressOff()
+        if format.lower() == "pdf":
+            self.pdffilecounter += 1
+            self.pswriter.SetFileFormatToPDF()
+            # Allways compress using pdf
+            self.pswriter.CompressOn()
+            counter = self.pdffilecounter
+        elif format.lower() == "ps":
+            self.psfilecounter += 1
+            self.pswriter.SetFileFormatToPS()
+            counter = self.psfilecounter
+        else:
+            self.epsfilecounter += 1
+            self.pswriter.SetFileFormatToEPS()
+            counter = self.epsfilecounter
+        if filename is None:
+            filename = "%s%.4d" % (self.basename, counter)
+        self.pswriter.SetFilePrefix(filename)
+        
+        self._update()
+        self.pswriter.Write()
 
     def write_raw(self, sol=None, dofmap=None):
         """Write a simulation frame to file, i.e. dump the numpy array to file
@@ -651,6 +686,8 @@
 Viper specific:
 * Keypress v: write data to file in vtk format
 * Keypress i: write image to file in png format
+* Keypress m: write image to file in eps format
+* Keypress n: write image to file in pdf format
 * Keypress o: add outline 
 * Keypress h: display this message
 
@@ -727,14 +764,21 @@
                     print "dz = ", bounds[5] - bounds[4]
                 self.outline = True
         elif key == "i":
+            print "Writing a simulation frame to a 'png' file"
             self.write_png()
         elif key == "v":
+            print "Writing data to a 'vtk' file"
             self.write_vtk()
+        elif key == "m":
+            print "Writing a simulation frame to an 'eps' file"
+            self.write_ps()
+        elif key == "n":
+            print "Writing a simulation frame to a 'pdf' file"
+            self.write_ps(format="pdf")
         elif key == "h":
             self.display_keybindings()
         elif key == 'e':
             raise SystemExit
-        
 
     def begin_interaction(self, obj, event):
         self.glyphActor.VisibilityOn()

---

From: Ilmar Wilbers <[email-masked]>
Date: Mon, 08 Sep 2008 22:50:28 +0200

I think you need special permissions for that repository, even at 
Simula. But SciTools (actually, it is Easyviz that is the interesting 
part, it is part of SciTools), is available at scitools.googlecode.com

ilmar

Johan Hake wrote:
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Viper-dev mailing list
> [email-masked]
> http://fenics.org/mailman/listinfo/viper-dev

---

From: Johan Hake <[email-masked]>
Date: Mon, 8 Sep 2008 23:21:03 +0200

> part, it is part of SciTools), is available at scitools.googlecode.com

Yes of course, thanks!

Johan

> ilmar
>
> Johan Hake wrote:
> > Hello!
> >
> > I added support for writing simulation frames to either eps/ps/pdf
> > format. This is nice for high quality pictures for publications.
> >
> > Would it be possible/usefull to add movie makeing support in viper?
> > Scitools, the packages that Johannes is maintaining, have a very usefull
> > function, movie(). In its simplest case you just call this function with
> > eg:
> >
> >    movie('image_*.eps')
> >
> > It support many file formats, and a bunch of backends. You can check it
> > out (literally) by:
> >
> >  svn co
> > https://svn.simula.no:40081/int/scriptingsuite/trunk/packages/scitools
> >
> > and see what you think. I have used it to create some nice movies with
> > png files from viper. A pseudo code example could be:
> >
> >   p = plot(u)
> >   for t in range(10):
> >      u = new_values()
> >      p.update()
> >      p.write_png()
> >   p.make_movie()
> >
> > What do you say?
> >
> > Johan
> >
> >