# HG changeset patch
# User alpar
# Date 1105000790 0
# Node ID f901ff02b2d7c1200847b946eacdf5ee582d37c1
# Parent  6a62b1b4cf2312f654b66a157efafbbe9683cd91
graphToEps also accepts an output file name parameter.

diff -r 6a62b1b4cf23 -r f901ff02b2d7 src/work/alpar/graph_to_eps.cc
--- a/src/work/alpar/graph_to_eps.cc	Wed Jan 05 16:59:50 2005 +0000
+++ b/src/work/alpar/graph_to_eps.cc	Thu Jan 06 08:39:50 2005 +0000
@@ -1,4 +1,6 @@
 #include <iostream>
+#include <fstream>
+#include <algorithm>
 #include<math.h>
 
 #include<lemon/xy.h>
@@ -72,20 +74,28 @@
   bool _drawArrows;
   double _arrowLength, _arrowWidth;
   
+  bool _enableParallel;
+
+  bool _pleaseRemoveOsStream;
   ///Constructor
 
   ///Constructor
   ///\param _g is a reference to the graph to be printed
   ///\param _os is a reference to the output stream.
+  ///\param _os is a reference to the output stream.
+  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
+  ///will be explicitly deallocated by the destructor.
   ///By default it is <tt>std::cout</tt>
-  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout) :
+  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
+			  bool _pros=false) :
     g(_g), os(_os),
     _coords(xy<double>(1,1)), _nodeSizes(1.0),
     _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
     _edgeWidths(1), _edgeWidthScale(0.3),
     _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
     _nodeBorderQuotient(.1),
-    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3) {}
+    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
+    _enableParallel(false), _pleaseRemoveOsStream(_pros) {}
 };
 
 ///Helper class to implement the named parameters of \ref graphToEps()
@@ -105,6 +115,22 @@
 
   bool dontPrint;
 
+  class edgeLess {
+    const Graph &g;
+  public:
+    edgeLess(const Graph &_g) : g(_g) {}
+    bool operator()(Edge a,Edge b) const 
+    {
+      Node ai=min(g.source(a),g.target(a));
+      Node aa=max(g.source(a),g.target(a));
+      Node bi=min(g.source(b),g.target(b));
+      Node ba=max(g.source(b),g.target(b));
+      return ai<bi ||
+	(ai==bi && (aa < ba || 
+		    (aa==ba && ai==g.source(a) && bi==g.target(b))));
+    }
+  };
+    
 public:
   GraphToEps(const T &t) : T(t), dontPrint(false) {};
   
@@ -218,6 +244,12 @@
   ///
   GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
   
+  ///Enables parallel edges
+
+  ///Enables parallel edges
+  ///\todo Unimplemented
+  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
+  
   ~GraphToEps() 
   {
     if(dontPrint) return;
@@ -265,7 +297,15 @@
 	 << "       closepath fill } bind def\n";
     os << "\ngsave\n";
     if(_scale!=1.0) os << _scale << " dup scale\n";
+    
     os << "%Edges:\ngsave\n";
+    
+    vector<Edge> el;
+    if(_enableParallel) {
+      for(EdgeIt e(g);e!=INVALID;++e) el.push_back(e);
+      sort(el.begin(),el.end(),edgeLess(g));
+    }
+    
     for(NodeIt n(g);n!=INVALID;++n)
       for(OutEdgeIt e(g,n);e!=INVALID;++e)
 	if(_drawArrows) {
@@ -299,6 +339,10 @@
 	   << _nodeColors[n].getG() << ' '
 	   << _nodeColors[n].getB() << " n\n"; 
     os << "grestore\ngrestore\n";
+
+
+    //CleanUp:
+    if(_pleaseRemoveOsStream) {delete &os;}
   } 
 };
 
@@ -321,11 +365,40 @@
 ///\endcode
 ///\sa GraphToEps
 template<class G>
-GraphToEps<DefaultGraphToEpsTraits<G> > graphToEps(G &g,std::ostream& os=std::cout)
+GraphToEps<DefaultGraphToEpsTraits<G> > 
+graphToEps(G &g,std::ostream& os=std::cout)
 {
-  return GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g));
+  return 
+    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
 }
  
+///Generates an EPS file from a graph
+
+///\ingroup misc
+///Generates an EPS file from a graph.
+///\param g is a reference to the graph to be printed
+///\param file_name is the output file_name.
+///
+///This function also has a lot of \ref named-templ-param "named parameters",
+///they are declared as the members of class \ref GraphToEps. The following
+///example shows how to use these parameters.
+///\code
+/// graphToEps(g).scale(10).coords(coords)
+///              .nodeScale(2).nodeSizes(sizes)
+///              .edgeWidthScale(.4);
+///\endcode
+///\sa GraphToEps
+///\todo Avoid duplicated documentation
+///\bug Exception handling is missing? (Or we can just ignore it?)
+template<class G>
+GraphToEps<DefaultGraphToEpsTraits<G> > 
+graphToEps(G &g,char *file_name)
+{
+  return GraphToEps<DefaultGraphToEpsTraits<G> >
+    (DefaultGraphToEpsTraits<G>(g,*new ofstream(file_name),true));
+}
+
+
 }
 
 using namespace lemon;
@@ -385,7 +458,12 @@
   e=g.addEdge(n2,n4); ecolors[e]=1; widths[e]=2;
   e=g.addEdge(n3,n4); ecolors[e]=2; widths[e]=1;
   
-  graphToEps(g).scale(10).coords(coords).
+  graphToEps(g,"proba.eps").scale(10).coords(coords).
+    nodeScale(2).nodeSizes(sizes).
+    nodeColors(composeMap(colorSet,colors)).
+    edgeColors(composeMap(colorSet,ecolors)).
+    edgeWidthScale(.4).edgeWidths(widths);
+  graphToEps(g,"proba_arr.eps").scale(10).coords(coords).
     nodeScale(2).nodeSizes(sizes).
     nodeColors(composeMap(colorSet,colors)).
     edgeColors(composeMap(colorSet,ecolors)).