src/lemon/graph_to_eps.h
changeset 1107 d972653c89d5
parent 1103 f196dc4f1b31
child 1108 253b66e7e41d
equal deleted inserted replaced
7:a30c8963de57 8:e95d3521892e
   150 
   150 
   151 ///Helper class to implement the named parameters of \ref graphToEps()
   151 ///Helper class to implement the named parameters of \ref graphToEps()
   152 ///\todo Is 'helper class' a good name for this?
   152 ///\todo Is 'helper class' a good name for this?
   153 ///
   153 ///
   154 ///\todo Follow PostScript's DSC.
   154 ///\todo Follow PostScript's DSC.
   155 ///\todo Use own dictionary.
   155 /// Use own dictionary.
       
   156 ///\todo Provide a way to set %%Title: and %%Copyright:. Set %%CreationDate:
       
   157 ///\todo Useful new features.
       
   158 /// - Linestyles: dotted, dashed etc.
       
   159 /// - A second color and percent value for the lines.
   156 template<class T> class GraphToEps : public T 
   160 template<class T> class GraphToEps : public T 
   157 {
   161 {
   158   typedef typename T::Graph Graph;
   162   typedef typename T::Graph Graph;
   159   typedef typename Graph::Node Node;
   163   typedef typename Graph::Node Node;
   160   typedef typename Graph::NodeIt NodeIt;
   164   typedef typename Graph::NodeIt NodeIt;
   165 
   169 
   166   static const int INTERPOL_PREC=20;
   170   static const int INTERPOL_PREC=20;
   167 
   171 
   168   bool dontPrint;
   172   bool dontPrint;
   169 
   173 
   170   enum NodeShapes { CIRCLE=0, SQUARE=1, DIAMOND=2 };
   174 public:
   171 		   
   175   ///Node shapes
       
   176 
       
   177   ///Node shapes
       
   178   ///
       
   179   enum NodeShapes { 
       
   180     /// = 0
       
   181     ///\image html nodeshape_0.png
       
   182     ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
       
   183     CIRCLE=0, 
       
   184     /// = 1
       
   185     ///\image html nodeshape_1.png
       
   186     ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
       
   187     ///
       
   188     SQUARE=1, 
       
   189     /// = 2
       
   190     ///\image html nodeshape_2.png
       
   191     ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
       
   192     ///
       
   193     DIAMOND=2
       
   194   };
       
   195 
       
   196 private:
   172   class edgeLess {
   197   class edgeLess {
   173     const Graph &g;
   198     const Graph &g;
   174   public:
   199   public:
   175     edgeLess(const Graph &_g) : g(_g) {}
   200     edgeLess(const Graph &_g) : g(_g) {}
   176     bool operator()(Edge a,Edge b) const 
   201     bool operator()(Edge a,Edge b) const 
   233     const X &_nodeShapes;
   258     const X &_nodeShapes;
   234     NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
   259     NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
   235   };
   260   };
   236   ///Sets the map of the node shapes
   261   ///Sets the map of the node shapes
   237 
   262 
   238   ///Sets the map of the node shapes
   263   ///Sets the map of the node shapes.
       
   264   ///The availabe shape values
       
   265   ///can be found in \ref NodeShapes "enum NodeShapes".
   239   ///\param x must be a node map with \c int (or convertible) values. 
   266   ///\param x must be a node map with \c int (or convertible) values. 
   240   ///\todo Incomplete doc.
   267   ///\sa NodeShapes
   241   template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
   268   template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
   242   {
   269   {
   243     dontPrint=true;
   270     dontPrint=true;
   244     return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
   271     return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
   245   }
   272   }
   447   ///it draws the graph.
   474   ///it draws the graph.
   448   void run() {
   475   void run() {
   449     if(dontPrint) return;
   476     if(dontPrint) return;
   450     
   477     
   451     os << "%!PS-Adobe-2.0 EPSF-2.0\n";
   478     os << "%!PS-Adobe-2.0 EPSF-2.0\n";
   452     //\todo: Chech whether the graph is empty.
   479     os << "%%Title: LEMON GraphToEps figure\n" ///\todo setTitle() is needed
       
   480 //        << "%%Copyright: XXXX\n"
       
   481        << "%%Creator: LEMON GraphToEps function\n"
       
   482 //        << "%%CreationDate: XXXXXXX\n"
       
   483       ;
       
   484     ///\todo: Chech whether the graph is empty.
   453     BoundingBox<double> bb;
   485     BoundingBox<double> bb;
   454     for(NodeIt n(g);n!=INVALID;++n) {
   486     for(NodeIt n(g);n!=INVALID;++n) {
   455       double ns=_nodeSizes[n]*_nodeScale;
   487       double ns=_nodeSizes[n]*_nodeScale;
   456       xy<double> p(ns,ns);
   488       xy<double> p(ns,ns);
   457       bb+=p+_coords[n];
   489       bb+=p+_coords[n];
   460     if(!_scaleToA4) os << "%%BoundingBox: "
   492     if(!_scaleToA4) os << "%%BoundingBox: "
   461 		      << bb.left()*  _scale-_xBorder << ' '
   493 		      << bb.left()*  _scale-_xBorder << ' '
   462 		      << bb.bottom()*_scale-_yBorder << ' '
   494 		      << bb.bottom()*_scale-_yBorder << ' '
   463 		      << bb.right()* _scale+_xBorder << ' '
   495 		      << bb.right()* _scale+_xBorder << ' '
   464 		      << bb.top()*   _scale+_yBorder << '\n';
   496 		      << bb.top()*   _scale+_yBorder << '\n';
       
   497 
       
   498     os << "%%EndComments\n";
       
   499     
   465     //x1 y1 x2 y2 x3 y3 cr cg cb w
   500     //x1 y1 x2 y2 x3 y3 cr cg cb w
   466     os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
   501     os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
   467        << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
   502        << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
   468     os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
   503     os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
   469     //x y r
   504     //x y r
   737 ///but it writes its output into the file \c file_name
   772 ///but it writes its output into the file \c file_name
   738 ///instead of a stream.
   773 ///instead of a stream.
   739 ///\sa graphToEps(G &g, std::ostream& os)
   774 ///\sa graphToEps(G &g, std::ostream& os)
   740 template<class G>
   775 template<class G>
   741 GraphToEps<DefaultGraphToEpsTraits<G> > 
   776 GraphToEps<DefaultGraphToEpsTraits<G> > 
   742 graphToEps(G &g,char *file_name)
   777 graphToEps(G &g,const char *file_name)
   743 {
   778 {
   744   return GraphToEps<DefaultGraphToEpsTraits<G> >
   779   return GraphToEps<DefaultGraphToEpsTraits<G> >
   745     (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
   780     (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
   746 }
   781 }
   747 
   782