[Lemon-commits] [lemon_svn] alpar: r1481 - hugo/trunk/src/lemon

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:45:47 CET 2006


Author: alpar
Date: Sun Jan 16 23:34:51 2005
New Revision: 1481

Modified:
   hugo/trunk/src/lemon/graph_to_eps.h

Log:
- Parallel edges look a bit better
- Possibility to insert verbatim PS blocks for each node

Modified: hugo/trunk/src/lemon/graph_to_eps.h
==============================================================================
--- hugo/trunk/src/lemon/graph_to_eps.h	(original)
+++ hugo/trunk/src/lemon/graph_to_eps.h	Sun Jan 16 23:34:51 2005
@@ -105,6 +105,10 @@
   ConstMap<typename Graph::Node,bool > _nodeTexts;  
   double _nodeTextSize;
 
+  bool _showNodePsText;
+  ConstMap<typename Graph::Node,bool > _nodePsTexts;  
+  char *_nodePsTextsPreamble;
+  
   bool _undir;
   bool _pleaseRemoveOsStream;
   ///Constructor
@@ -128,6 +132,7 @@
     _showNodes(true), _showEdges(true),
     _enableParallel(false), _parEdgeDist(1),
     _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
+    _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
     _undir(false),
     _pleaseRemoveOsStream(_pros) {}
 };
@@ -224,7 +229,33 @@
     _showNodeText=true;
     return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
   }
-   template<class X> struct EdgeWidthsTraits : public T {
+  template<class X> struct NodePsTextsTraits : public T {
+    const X &_nodePsTexts;
+    NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
+  };
+  ///Inserts a PostScript block to the nodes
+
+  ///With this command it is possible to insert a verbatim PostScript
+  ///block to the nodes.
+  ///The PS current point will be moved to the centre of the node before
+  ///the PostScript block inserted.
+  ///
+  ///Before and after the block a newline character is inserted to you
+  ///don't have to bother with the separators.
+  ///
+  ///\param x must be a node map with type that can be pushed to a standard
+  ///ostream.
+  ///
+  ///\sa nodePsTextsPreamble()
+  ///\todo Offer the choise not to move to the centre but pass the coordinates
+  ///to the Postscript block inserted.
+  template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
+  {
+    dontPrint=true;
+    _showNodePsText=true;
+    return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
+  }
+  template<class X> struct EdgeWidthsTraits : public T {
     const X &_edgeWidths;
     EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
   };
@@ -337,6 +368,14 @@
   ///Sets the size of the node texts
   ///
   GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
+  ///Gives a preamble block for node Postscript block.
+  
+  ///Gives a preamble block for node Postscript block.
+  ///
+  ///\sa nodePsTexts()
+  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
+    _nodePsTextsPreamble=s ;return *this;
+  }
   ///Sets whether the the graph is undirected
 
   ///Sets whether the the graph is undirected
@@ -401,9 +440,8 @@
     os << "\ngsave\n";
     if(_scale!=1.0) os << _scale << " dup scale\n";
     
-    os << "%Edges:\ngsave\n";
-    
-    if(_showEdges)
+    if(_showEdges) {
+      os << "%Edges:\ngsave\n";      
       if(_enableParallel) {
 	std::vector<Edge> el;
 	for(EdgeIt e(g);e!=INVALID;++e)
@@ -419,21 +457,29 @@
 	    sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
 	  sw-=_parEdgeDist;
 	  sw/=-2.0;
-	  xy<double> d(_coords[g.target(*i)]-_coords[g.source(*i)]);
-	  double l=sqrt(d.normSquare());
-	  d/=l;
-	  
+	  xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]);
+	  double l=sqrt(dvec.normSquare());
+	  xy<double> d(dvec/l);
+ 	  xy<double> m;
+// 	  m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0;
+
+//  	  m=xy<double>(_coords[g.source(*i)])+
+// 	    dvec*(double(_nodeSizes[g.source(*i)])/
+// 	       (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
+
+ 	  m=xy<double>(_coords[g.source(*i)])+
+	    d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
+
 	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
 	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
-	    xy<double> m(_coords[g.target(*e)]+_coords[g.source(*e)]);
-	    m=m/2.0+rot(d)*sw/.75;
+	    xy<double> mm=m+rot(d)*sw/.75;
 	    if(_drawArrows) {
 	      const int INERPOL_PREC=20;
 	      xy<double> s=_coords[g.source(*e)];
 	      xy<double> t=_coords[g.target(*e)];
 	      double rn=_nodeSizes[g.target(*e)]*_nodeScale;
 	      rn*=rn;
-	      Bezier3 bez(s,m,m,t);
+	      Bezier3 bez(s,mm,mm,t);
 	      double t1=0,t2=1;
 	      for(int i=0;i<INERPOL_PREC;++i)
 		if((bez((t1+t2)/2)-t).normSquare()>rn) t1=(t1+t2)/2;
@@ -472,7 +518,7 @@
 	    else {
 	      os << _coords[g.source(*e)].x << ' '
 		 << _coords[g.source(*e)].y << ' '
-		 << m.x << ' ' << m.y << ' '
+		 << mm.x << ' ' << mm.y << ' '
 		 << _coords[g.target(*e)].x << ' '
 		 << _coords[g.target(*e)].y << ' '
 		 << _edgeColors[*e].getR() << ' '
@@ -509,24 +555,37 @@
 		  << _edgeColors[e].getG() << ' '
 		  << _edgeColors[e].getB() << ' '
 		  << _edgeWidths[e]*_edgeWidthScale << " l\n";
-    os << "grestore\n%Nodes:\ngsave\n";
-    if(_showNodes)
+      os << "grestore\n";
+    }
+    if(_showNodes) {
+      os << "%Nodes:\ngsave\n";
       for(NodeIt n(g);n!=INVALID;++n)
 	os << _coords[n].x << ' ' << _coords[n].y << ' '
 	   << _nodeSizes[n]*_nodeScale << ' '
 	   << _nodeColors[n].getR() << ' '
 	   << _nodeColors[n].getG() << ' '
-	   << _nodeColors[n].getB() << " n\n"; 
+	   << _nodeColors[n].getB() << " n\n";
+      os << "grestore\n";
+    }
     if(_showNodeText) {
-      os << "grestore\n%Node texts:\ngsave\n";
+      os << "%Node texts:\ngsave\n";
       os << "/fosi " << _nodeTextSize << " def\n";
       os << "(Helvetica) findfont fosi scalefont setfont\n";
       os << "0 0 0 setrgbcolor\n";
       for(NodeIt n(g);n!=INVALID;++n)
 	os << _coords[n].x << ' ' << _coords[n].y
 	   << " (" << _nodeTexts[n] << ") cshow\n";
+      os << "grestore\n";
     }
-    os << "grestore\ngrestore\n";
+    if(_showNodePsText) {
+      os << "%Node PS blocks:\ngsave\n";
+      for(NodeIt n(g);n!=INVALID;++n)
+	os << _coords[n].x << ' ' << _coords[n].y
+	   << " moveto\n" << _nodePsTexts[n] << "\n";
+      os << "grestore\n";
+    }
+    
+    os << "grestore\n";
 
     //CleanUp:
     if(_pleaseRemoveOsStream) {delete &os;}
@@ -576,22 +635,6 @@
     (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
 }
 
-//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?)
-
 } //END OF NAMESPACE LEMON
 
 #endif // LEMON_GRAPH_TO_EPS_H



More information about the Lemon-commits mailing list