# HG changeset patch # User Alpar Juttner # Date 1208444070 -3600 # Node ID 6e7aee618f037c144a58dce8fb91cffbfa9ff11f # Parent 1c9a9e2f7d4d1580a93efaceac7c7a75c7cfd726# Parent 0775d2ba2afb444ef8d440264ebbd91d4b8a60fc Merge with graph_to_eps.h & Co. diff -r 1c9a9e2f7d4d -r 6e7aee618f03 .hgignore --- a/.hgignore Thu Apr 17 15:18:45 2008 +0100 +++ b/.hgignore Thu Apr 17 15:54:30 2008 +0100 @@ -24,6 +24,7 @@ .dirstamp .libs/* .deps/* +demo/*.eps syntax: regexp (.*/)?\#[^/]*\#$ diff -r 1c9a9e2f7d4d -r 6e7aee618f03 demo/Makefile.am --- a/demo/Makefile.am Thu Apr 17 15:18:45 2008 +0100 +++ b/demo/Makefile.am Thu Apr 17 15:54:30 2008 +0100 @@ -4,11 +4,13 @@ if WANT_DEMO noinst_PROGRAMS += \ - demo/arg_parser_demo \ + demo/arg_parser_demo \ + demo/graph_to_eps_demo \ demo/lgf_demo endif WANT_DEMO demo_arg_parser_demo_SOURCES = demo/arg_parser_demo.cc +demo_graph_to_eps_demo_SOURCES = demo/graph_to_eps_demo.cc demo_lgf_demo_SOURCES = demo/lgf_demo.cc diff -r 1c9a9e2f7d4d -r 6e7aee618f03 demo/arg_parser_demo.cc --- a/demo/arg_parser_demo.cc Thu Apr 17 15:18:45 2008 +0100 +++ b/demo/arg_parser_demo.cc Thu Apr 17 15:54:30 2008 +0100 @@ -37,6 +37,7 @@ bool g1,g2,g3; ap.refOption("n", "An integer input.", i, true) .refOption("val", "A double input.", d) + .doubleOption("val2", "A double input.", d) .synonym("vals","val") .refOption("name", "A string input.", s) .refOption("f", "A switch.", b) @@ -45,7 +46,7 @@ .refOption("grb","Choice B",g2) .refOption("grc","Choice C",g3) .optionGroup("gr","gra") - .optionGroup("gr","grb") + .optionGroup("gr","grbkk") .optionGroup("gr","grc") .mandatoryGroup("gr") .onlyOneGroup("gr") diff -r 1c9a9e2f7d4d -r 6e7aee618f03 demo/graph_to_eps_demo.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/graph_to_eps_demo.cc Thu Apr 17 15:54:30 2008 +0100 @@ -0,0 +1,208 @@ +/* -*- C++ -*- + * + * This file is a part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2003-2008 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +/// \ingroup demos +/// \file +/// \brief Demo of the graph grawing function \ref graphToEps() +/// +/// This demo program shows examples how to use the function \ref +/// graphToEps(). It takes no input but simply creates six +/// .eps files demonstrating the capability of \ref +/// graphToEps(), and showing how to draw directed/graphs, +/// how to handle parallel egdes, how to change the properties (like +/// color, shape, size, title etc.) of nodes and arcs individually +/// using appropriate \ref maps-page "graph maps". +/// +/// \include graph_to_eps_demo.cc + +#include + +#include +#include +#include + +using namespace std; +using namespace lemon; + +int main() +{ + Palette palette; + Palette paletteW(true); + + ListDigraph g; + typedef ListDigraph::Node Node; + typedef ListDigraph::NodeIt NodeIt; + typedef ListDigraph::Arc Arc; + typedef dim2::Point Point; + + Node n1=g.addNode(); + Node n2=g.addNode(); + Node n3=g.addNode(); + Node n4=g.addNode(); + Node n5=g.addNode(); + + ListDigraph::NodeMap coords(g); + ListDigraph::NodeMap sizes(g); + ListDigraph::NodeMap colors(g); + ListDigraph::NodeMap shapes(g); + ListDigraph::ArcMap ecolors(g); + ListDigraph::ArcMap widths(g); + + coords[n1]=Point(50,50); sizes[n1]=1; colors[n1]=1; shapes[n1]=0; + coords[n2]=Point(50,70); sizes[n2]=2; colors[n2]=2; shapes[n2]=2; + coords[n3]=Point(70,70); sizes[n3]=1; colors[n3]=3; shapes[n3]=0; + coords[n4]=Point(70,50); sizes[n4]=2; colors[n4]=4; shapes[n4]=1; + coords[n5]=Point(85,60); sizes[n5]=3; colors[n5]=5; shapes[n5]=2; + + Arc e; + + e=g.addArc(n1,n2); ecolors[e]=0; widths[e]=1; + e=g.addArc(n2,n3); ecolors[e]=0; widths[e]=1; + e=g.addArc(n3,n5); ecolors[e]=0; widths[e]=3; + e=g.addArc(n5,n4); ecolors[e]=0; widths[e]=1; + e=g.addArc(n4,n1); ecolors[e]=0; widths[e]=1; + e=g.addArc(n2,n4); ecolors[e]=1; widths[e]=2; + e=g.addArc(n3,n4); ecolors[e]=2; widths[e]=1; + + IdMap id(g); + + cout << "Create 'graph_to_eps_demo_out_pure.eps'" << endl; + graphToEps(g,"graph_to_eps_demo_out_pure.eps"). + //scale(10). + coords(coords). + title("Sample .eps figure"). + copyright("(C) 2003-2007 LEMON Project"). + run(); + + cout << "Create 'graph_to_eps_demo_out.eps'" << endl; + graphToEps(g,"graph_to_eps_demo_out.eps"). + //scale(10). + coords(coords). + title("Sample .eps figure"). + copyright("(C) 2003-2007 LEMON Project"). + absoluteNodeSizes().absoluteArcWidths(). + nodeScale(2).nodeSizes(sizes). + nodeShapes(shapes). + nodeColors(composeMap(palette,colors)). + arcColors(composeMap(palette,ecolors)). + arcWidthScale(.4).arcWidths(widths). + nodeTexts(id).nodeTextSize(3). + run(); + + + cout << "Create 'graph_to_eps_demo_out_arr.eps'" << endl; + graphToEps(g,"graph_to_eps_demo_out_arr.eps"). + //scale(10). + title("Sample .eps figure (with arrowheads)"). + copyright("(C) 2003-2007 LEMON Project"). + absoluteNodeSizes().absoluteArcWidths(). + nodeColors(composeMap(palette,colors)). + coords(coords). + nodeScale(2).nodeSizes(sizes). + nodeShapes(shapes). + arcColors(composeMap(palette,ecolors)). + arcWidthScale(.4).arcWidths(widths). + nodeTexts(id).nodeTextSize(3). + drawArrows().arrowWidth(1).arrowLength(1). + run(); + + e=g.addArc(n1,n4); ecolors[e]=2; widths[e]=1; + e=g.addArc(n4,n1); ecolors[e]=1; widths[e]=2; + + e=g.addArc(n1,n2); ecolors[e]=1; widths[e]=1; + e=g.addArc(n1,n2); ecolors[e]=2; widths[e]=1; + e=g.addArc(n1,n2); ecolors[e]=3; widths[e]=1; + e=g.addArc(n1,n2); ecolors[e]=4; widths[e]=1; + e=g.addArc(n1,n2); ecolors[e]=5; widths[e]=1; + e=g.addArc(n1,n2); ecolors[e]=6; widths[e]=1; + e=g.addArc(n1,n2); ecolors[e]=7; widths[e]=1; + + cout << "Create 'graph_to_eps_demo_out_par.eps'" << endl; + graphToEps(g,"graph_to_eps_demo_out_par.eps"). + //scale(10). + title("Sample .eps figure (parallel arcs)"). + copyright("(C) 2003-2007 LEMON Project"). + absoluteNodeSizes().absoluteArcWidths(). + nodeShapes(shapes). + coords(coords). + nodeScale(2).nodeSizes(sizes). + nodeColors(composeMap(palette,colors)). + arcColors(composeMap(palette,ecolors)). + arcWidthScale(.4).arcWidths(widths). + nodeTexts(id).nodeTextSize(3). + enableParallel().parArcDist(1.5). + run(); + + cout << "Create 'graph_to_eps_demo_out_par_arr.eps'" << endl; + graphToEps(g,"graph_to_eps_demo_out_par_arr.eps"). + //scale(10). + title("Sample .eps figure (parallel arcs and arrowheads)"). + copyright("(C) 2003-2007 LEMON Project"). + absoluteNodeSizes().absoluteArcWidths(). + nodeScale(2).nodeSizes(sizes). + coords(coords). + nodeShapes(shapes). + nodeColors(composeMap(palette,colors)). + arcColors(composeMap(palette,ecolors)). + arcWidthScale(.3).arcWidths(widths). + nodeTexts(id).nodeTextSize(3). + enableParallel().parArcDist(1). + drawArrows().arrowWidth(1).arrowLength(1). + run(); + + cout << "Create 'graph_to_eps_demo_out_a4.eps'" << endl; + graphToEps(g,"graph_to_eps_demo_out_a4.eps").scaleToA4(). + title("Sample .eps figure (fits to A4)"). + copyright("(C) 2003-2007 LEMON Project"). + absoluteNodeSizes().absoluteArcWidths(). + nodeScale(2).nodeSizes(sizes). + coords(coords). + nodeShapes(shapes). + nodeColors(composeMap(palette,colors)). + arcColors(composeMap(palette,ecolors)). + arcWidthScale(.3).arcWidths(widths). + nodeTexts(id).nodeTextSize(3). + enableParallel().parArcDist(1). + drawArrows().arrowWidth(1).arrowLength(1). + run(); + + ListDigraph h; + ListDigraph::NodeMap hcolors(h); + ListDigraph::NodeMap hcoords(h); + + int cols=int(sqrt(double(palette.size()))); + for(int i=0;isecond.ingroup), "Option already in option group: '"+opt+"'"); GroupData &g=_groups[group]; diff -r 1c9a9e2f7d4d -r 6e7aee618f03 lemon/bits/bezier.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/bits/bezier.h Thu Apr 17 15:54:30 2008 +0100 @@ -0,0 +1,176 @@ +/* -*- C++ -*- + * + * This file is a part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2003-2008 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_BEZIER_H +#define LEMON_BEZIER_H + +///\ingroup misc +///\file +///\brief Classes to compute with Bezier curves. +/// +///Up to now this file is used internally by \ref graph_to_eps.h +/// +///\author Alpar Juttner + +#include + +namespace lemon { + namespace dim2 { + +class BezierBase { +public: + typedef Point Point; +protected: + static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;} +}; + +class Bezier1 : public BezierBase +{ +public: + Point p1,p2; + + Bezier1() {} + Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {} + + Point operator()(double t) const + { + // return conv(conv(p1,p2,t),conv(p2,p3,t),t); + return conv(p1,p2,t); + } + Bezier1 before(double t) const + { + return Bezier1(p1,conv(p1,p2,t)); + } + + Bezier1 after(double t) const + { + return Bezier1(conv(p1,p2,t),p2); + } + + Bezier1 revert() const { return Bezier1(p2,p1);} + Bezier1 operator()(double a,double b) const { return before(b).after(a/b); } + Point grad() const { return p2-p1; } + Point norm() const { return rot90(p2-p1); } + Point grad(double) const { return grad(); } + Point norm(double t) const { return rot90(grad(t)); } +}; + +class Bezier2 : public BezierBase +{ +public: + Point p1,p2,p3; + + Bezier2() {} + Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {} + Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {} + Point operator()(double t) const + { + // return conv(conv(p1,p2,t),conv(p2,p3,t),t); + return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3; + } + Bezier2 before(double t) const + { + Point q(conv(p1,p2,t)); + Point r(conv(p2,p3,t)); + return Bezier2(p1,q,conv(q,r,t)); + } + + Bezier2 after(double t) const + { + Point q(conv(p1,p2,t)); + Point r(conv(p2,p3,t)); + return Bezier2(conv(q,r,t),r,p3); + } + Bezier2 revert() const { return Bezier2(p3,p2,p1);} + Bezier2 operator()(double a,double b) const { return before(b).after(a/b); } + Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); } + Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); } + Point grad(double t) const { return grad()(t); } + Point norm(double t) const { return rot90(grad(t)); } +}; + +class Bezier3 : public BezierBase +{ +public: + Point p1,p2,p3,p4; + + Bezier3() {} + Bezier3(Point _p1, Point _p2, Point _p3, Point _p4) + : p1(_p1), p2(_p2), p3(_p3), p4(_p4) {} + Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), + p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {} + Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)), + p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {} + + Point operator()(double t) const + { + // return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t); + return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+ + (3*t*t*(1-t))*p3+(t*t*t)*p4; + } + Bezier3 before(double t) const + { + Point p(conv(p1,p2,t)); + Point q(conv(p2,p3,t)); + Point r(conv(p3,p4,t)); + Point a(conv(p,q,t)); + Point b(conv(q,r,t)); + Point c(conv(a,b,t)); + return Bezier3(p1,p,a,c); + } + + Bezier3 after(double t) const + { + Point p(conv(p1,p2,t)); + Point q(conv(p2,p3,t)); + Point r(conv(p3,p4,t)); + Point a(conv(p,q,t)); + Point b(conv(q,r,t)); + Point c(conv(a,b,t)); + return Bezier3(c,b,r,p4); + } + Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);} + Bezier3 operator()(double a,double b) const { return before(b).after(a/b); } + Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); } + Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1), + 3.0*rot90(p3-p2), + 3.0*rot90(p4-p3)); } + Point grad(double t) const { return grad()(t); } + Point norm(double t) const { return rot90(grad(t)); } + + template + R recSplit(F &_f,const S &_s,D _d) const + { + const Point a=(p1+p2)/2; + const Point b=(p2+p3)/2; + const Point c=(p3+p4)/2; + const Point d=(a+b)/2; + const Point e=(b+c)/2; + const Point f=(d+e)/2; + R f1=_f(Bezier3(p1,a,d,e),_d); + R f2=_f(Bezier3(e,d,c,p4),_d); + return _s(f1,f2); + } + +}; + + +} //END OF NAMESPACE dim2 +} //END OF NAMESPACE lemon + +#endif // LEMON_BEZIER_H diff -r 1c9a9e2f7d4d -r 6e7aee618f03 lemon/color.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/color.cc Thu Apr 17 15:54:30 2008 +0100 @@ -0,0 +1,44 @@ +/* -*- C++ -*- + * + * This file is a part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2003-2008 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +///\file +///\brief Color constants + +#include + +namespace lemon { + + const Color WHITE(1,1,1); + + const Color BLACK(0,0,0); + const Color RED(1,0,0); + const Color GREEN(0,1,0); + const Color BLUE(0,0,1); + const Color YELLOW(1,1,0); + const Color MAGENTA(1,0,1); + const Color CYAN(0,1,1); + + const Color GREY(0,0,0); + const Color DARK_RED(.5,0,0); + const Color DARK_GREEN(0,.5,0); + const Color DARK_BLUE(0,0,.5); + const Color DARK_YELLOW(.5,.5,0); + const Color DARK_MAGENTA(.5,0,.5); + const Color DARK_CYAN(0,.5,.5); + +} //namespace lemon diff -r 1c9a9e2f7d4d -r 6e7aee618f03 lemon/color.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/color.h Thu Apr 17 15:54:30 2008 +0100 @@ -0,0 +1,207 @@ +/* -*- C++ -*- + * + * This file is a part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2003-2008 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_COLOR_H +#define LEMON_COLOR_H + +#include +#include +#include + + +///\ingroup misc +///\file +///\brief Tools to manage RGB colors. +/// +///\author Alpar Juttner + +namespace lemon { + + + /// \addtogroup misc + /// @{ + + ///Data structure representing RGB colors. + + ///Data structure representing RGB colors. + class Color + { + double _r,_g,_b; + public: + ///Default constructor + Color() {} + ///Constructor + Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; + ///Set the red component + double & red() {return _r;} + ///Return the red component + const double & red() const {return _r;} + ///Set the green component + double & green() {return _g;} + ///Return the green component + const double & green() const {return _g;} + ///Set the blue component + double & blue() {return _b;} + ///Return the blue component + const double & blue() const {return _b;} + ///Set the color components + void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; + }; + + /// White color constant + extern const Color WHITE; + /// Black color constant + extern const Color BLACK; + /// Red color constant + extern const Color RED; + /// Green color constant + extern const Color GREEN; + /// Blue color constant + extern const Color BLUE; + /// Yellow color constant + extern const Color YELLOW; + /// Magenta color constant + extern const Color MAGENTA; + /// Cyan color constant + extern const Color CYAN; + /// Grey color constant + extern const Color GREY; + /// Dark red color constant + extern const Color DARK_RED; + /// Dark green color constant + extern const Color DARK_GREEN; + /// Drak blue color constant + extern const Color DARK_BLUE; + /// Dark yellow color constant + extern const Color DARK_YELLOW; + /// Dark magenta color constant + extern const Color DARK_MAGENTA; + /// Dark cyan color constant + extern const Color DARK_CYAN; + + ///Map ints to different \ref Color "Color"s + + ///This map assigns one of the predefined \ref Color "Color"s to + ///each int. It is possible to change the colors as well as + ///their number. The integer range is cyclically mapped to the + ///provided set of colors. + /// + ///This is a true \ref concepts::ReferenceMap "reference map", so + ///you can also change the actual colors. + + class Palette : public MapBase + { + std::vector colors; + public: + ///Constructor + + ///Constructor + ///\param have_white indicates whether white is amongst the + ///provided initial colors (\c true) or not (\c false). If it is true, + ///white will be assigned to \c 0. + ///\param num the number of the allocated colors. If it is \c -1, + ///the default color configuration is set up (26 color plus optionaly the + ///white). If \c num is less then 26/27 then the default color + ///list is cut. Otherwise the color list is filled repeatedly with + ///the default color list. (The colors can be changed later on.) + Palette(bool have_white=false,int num=-1) + { + if (num==0) return; + do { + if(have_white) colors.push_back(Color(1,1,1)); + + colors.push_back(Color(0,0,0)); + colors.push_back(Color(1,0,0)); + colors.push_back(Color(0,1,0)); + colors.push_back(Color(0,0,1)); + colors.push_back(Color(1,1,0)); + colors.push_back(Color(1,0,1)); + colors.push_back(Color(0,1,1)); + + colors.push_back(Color(.5,0,0)); + colors.push_back(Color(0,.5,0)); + colors.push_back(Color(0,0,.5)); + colors.push_back(Color(.5,.5,0)); + colors.push_back(Color(.5,0,.5)); + colors.push_back(Color(0,.5,.5)); + + colors.push_back(Color(.5,.5,.5)); + colors.push_back(Color(1,.5,.5)); + colors.push_back(Color(.5,1,.5)); + colors.push_back(Color(.5,.5,1)); + colors.push_back(Color(1,1,.5)); + colors.push_back(Color(1,.5,1)); + colors.push_back(Color(.5,1,1)); + + colors.push_back(Color(1,.5,0)); + colors.push_back(Color(.5,1,0)); + colors.push_back(Color(1,0,.5)); + colors.push_back(Color(0,1,.5)); + colors.push_back(Color(0,.5,1)); + colors.push_back(Color(.5,0,1)); + } while(int(colors.size())=0) colors.resize(num); + } + ///\e + Color &operator[](int i) + { + return colors[i%colors.size()]; + } + ///\e + const Color &operator[](int i) const + { + return colors[i%colors.size()]; + } + ///\e + void set(int i,const Color &c) + { + colors[i%colors.size()]=c; + } + ///Add a new color to the end of the color list. + void add(const Color &c) + { + colors.push_back(c); + } + + ///Sets the number of the exiting colors. + void resize(int s) { colors.resize(s);} + ///Returns the number of the existing colors. + int size() const { return int(colors.size());} + }; + + ///Returns a visibly distinct \ref Color + + ///Returns a \ref Color which is as different from the given parameter + ///as it is possible. + inline Color distantColor(const Color &c) + { + return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0); + } + ///Returns black for light colors and white for the dark ones. + + ///Returns black for light colors and white for the dark ones. + inline Color distantBW(const Color &c){ + return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK; + } + + /// @} + +} //END OF NAMESPACE LEMON + +#endif // LEMON_COLOR_H diff -r 1c9a9e2f7d4d -r 6e7aee618f03 lemon/graph_to_eps.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/graph_to_eps.h Thu Apr 17 15:54:30 2008 +0100 @@ -0,0 +1,1244 @@ +/* -*- C++ -*- + * + * This file is a part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2003-2008 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_GRAPH_TO_EPS_H +#define LEMON_GRAPH_TO_EPS_H + +#include +#include +#include +#include +#include + +#ifndef WIN32 +#include +#include +#else +#include +#endif + +#include +#include +#include +#include +#include +#include + + +///\ingroup eps_io +///\file +///\brief A well configurable tool for visualizing graphs + +namespace lemon { + + namespace _graph_to_eps_bits { + template + class _NegY { + public: + typedef typename MT::Key Key; + typedef typename MT::Value Value; + const MT ↦ + int yscale; + _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} + Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} + }; + } + +///Default traits class of \ref GraphToEps + +///Default traits class of \ref GraphToEps +/// +///\c G is the type of the underlying graph. +template +struct DefaultGraphToEpsTraits +{ + typedef G Graph; + typedef typename Graph::Node Node; + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::Arc Arc; + typedef typename Graph::ArcIt ArcIt; + typedef typename Graph::InArcIt InArcIt; + typedef typename Graph::OutArcIt OutArcIt; + + + const Graph &g; + + std::ostream& os; + + typedef ConstMap > CoordsMapType; + CoordsMapType _coords; + ConstMap _nodeSizes; + ConstMap _nodeShapes; + + ConstMap _nodeColors; + ConstMap _arcColors; + + ConstMap _arcWidths; + + double _arcWidthScale; + + double _nodeScale; + double _xBorder, _yBorder; + double _scale; + double _nodeBorderQuotient; + + bool _drawArrows; + double _arrowLength, _arrowWidth; + + bool _showNodes, _showArcs; + + bool _enableParallel; + double _parArcDist; + + bool _showNodeText; + ConstMap _nodeTexts; + double _nodeTextSize; + + bool _showNodePsText; + ConstMap _nodePsTexts; + char *_nodePsTextsPreamble; + + bool _undirected; + + bool _pleaseRemoveOsStream; + + bool _scaleToA4; + + std::string _title; + std::string _copyright; + + enum NodeTextColorType + { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; + ConstMap _nodeTextColors; + + bool _autoNodeScale; + bool _autoArcWidthScale; + + bool _absoluteNodeSizes; + bool _absoluteArcWidths; + + bool _negY; + + bool _preScale; + ///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 std::cout + DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, + bool _pros=false) : + g(_g), os(_os), + _coords(dim2::Point(1,1)), _nodeSizes(1), _nodeShapes(0), + _nodeColors(WHITE), _arcColors(BLACK), + _arcWidths(1.0), _arcWidthScale(0.003), + _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), + _nodeBorderQuotient(.1), + _drawArrows(false), _arrowLength(1), _arrowWidth(0.3), + _showNodes(true), _showArcs(true), + _enableParallel(false), _parArcDist(1), + _showNodeText(false), _nodeTexts(false), _nodeTextSize(1), + _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), + _undirected(lemon::UndirectedTagIndicator::value), + _pleaseRemoveOsStream(_pros), _scaleToA4(false), + _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), + _autoNodeScale(false), + _autoArcWidthScale(false), + _absoluteNodeSizes(false), + _absoluteArcWidths(false), + _negY(false), + _preScale(true) + {} +}; + +///Auxiliary class to implement the named parameters of \ref graphToEps() + +///Auxiliary class to implement the named parameters of \ref graphToEps() +template class GraphToEps : public T +{ + // Can't believe it is required by the C++ standard + using T::g; + using T::os; + + using T::_coords; + using T::_nodeSizes; + using T::_nodeShapes; + using T::_nodeColors; + using T::_arcColors; + using T::_arcWidths; + + using T::_arcWidthScale; + using T::_nodeScale; + using T::_xBorder; + using T::_yBorder; + using T::_scale; + using T::_nodeBorderQuotient; + + using T::_drawArrows; + using T::_arrowLength; + using T::_arrowWidth; + + using T::_showNodes; + using T::_showArcs; + + using T::_enableParallel; + using T::_parArcDist; + + using T::_showNodeText; + using T::_nodeTexts; + using T::_nodeTextSize; + + using T::_showNodePsText; + using T::_nodePsTexts; + using T::_nodePsTextsPreamble; + + using T::_undirected; + + using T::_pleaseRemoveOsStream; + + using T::_scaleToA4; + + using T::_title; + using T::_copyright; + + using T::NodeTextColorType; + using T::CUST_COL; + using T::DIST_COL; + using T::DIST_BW; + using T::_nodeTextColorType; + using T::_nodeTextColors; + + using T::_autoNodeScale; + using T::_autoArcWidthScale; + + using T::_absoluteNodeSizes; + using T::_absoluteArcWidths; + + + using T::_negY; + using T::_preScale; + + // dradnats ++C eht yb deriuqer si ti eveileb t'naC + + typedef typename T::Graph Graph; + typedef typename Graph::Node Node; + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::Arc Arc; + typedef typename Graph::ArcIt ArcIt; + typedef typename Graph::InArcIt InArcIt; + typedef typename Graph::OutArcIt OutArcIt; + + static const int INTERPOL_PREC; + static const double A4HEIGHT; + static const double A4WIDTH; + static const double A4BORDER; + + bool dontPrint; + +public: + ///Node shapes + + ///Node shapes + /// + enum NodeShapes { + /// = 0 + ///\image html nodeshape_0.png + ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm + CIRCLE=0, + /// = 1 + ///\image html nodeshape_1.png + ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm + /// + SQUARE=1, + /// = 2 + ///\image html nodeshape_2.png + ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm + /// + DIAMOND=2, + /// = 3 + ///\image html nodeshape_3.png + ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm + /// + MALE=3, + /// = 4 + ///\image html nodeshape_4.png + ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm + /// + FEMALE=4 + }; + +private: + class arcLess { + const Graph &g; + public: + arcLess(const Graph &_g) : g(_g) {} + bool operator()(Arc a,Arc b) const + { + Node ai=std::min(g.source(a),g.target(a)); + Node aa=std::max(g.source(a),g.target(a)); + Node bi=std::min(g.source(b),g.target(b)); + Node ba=std::max(g.source(b),g.target(b)); + return ai + static std::string psOut(const dim2::Point &p) + { + std::ostringstream os; + os << p.x << ' ' << p.y; + return os.str(); + } + static std::string psOut(const Color &c) + { + std::ostringstream os; + os << c.red() << ' ' << c.green() << ' ' << c.blue(); + return os.str(); + } + +public: + GraphToEps(const T &t) : T(t), dontPrint(false) {}; + + template struct CoordsTraits : public T { + typedef X CoordsMapType; + const X &_coords; + CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} + }; + ///Sets the map of the node coordinates + + ///Sets the map of the node coordinates. + ///\param x must be a node map with dim2::Point or + ///\ref dim2::Point "dim2::Point" values. + template GraphToEps > coords(const X &x) { + dontPrint=true; + return GraphToEps >(CoordsTraits(*this,x)); + } + template struct NodeSizesTraits : public T { + const X &_nodeSizes; + NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} + }; + ///Sets the map of the node sizes + + ///Sets the map of the node sizes + ///\param x must be a node map with \c double (or convertible) values. + template GraphToEps > nodeSizes(const X &x) + { + dontPrint=true; + return GraphToEps >(NodeSizesTraits(*this,x)); + } + template struct NodeShapesTraits : public T { + const X &_nodeShapes; + NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} + }; + ///Sets the map of the node shapes + + ///Sets the map of the node shapes. + ///The available shape values + ///can be found in \ref NodeShapes "enum NodeShapes". + ///\param x must be a node map with \c int (or convertible) values. + ///\sa NodeShapes + template GraphToEps > nodeShapes(const X &x) + { + dontPrint=true; + return GraphToEps >(NodeShapesTraits(*this,x)); + } + template struct NodeTextsTraits : public T { + const X &_nodeTexts; + NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} + }; + ///Sets the text printed on the nodes + + ///Sets the text printed on the nodes + ///\param x must be a node map with type that can be pushed to a standard + ///ostream. + template GraphToEps > nodeTexts(const X &x) + { + dontPrint=true; + _showNodeText=true; + return GraphToEps >(NodeTextsTraits(*this,x)); + } + template 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 so 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() + template GraphToEps > nodePsTexts(const X &x) + { + dontPrint=true; + _showNodePsText=true; + return GraphToEps >(NodePsTextsTraits(*this,x)); + } + template struct ArcWidthsTraits : public T { + const X &_arcWidths; + ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} + }; + ///Sets the map of the arc widths + + ///Sets the map of the arc widths + ///\param x must be a arc map with \c double (or convertible) values. + template GraphToEps > arcWidths(const X &x) + { + dontPrint=true; + return GraphToEps >(ArcWidthsTraits(*this,x)); + } + + template struct NodeColorsTraits : public T { + const X &_nodeColors; + NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} + }; + ///Sets the map of the node colors + + ///Sets the map of the node colors + ///\param x must be a node map with \ref Color values. + /// + ///\sa Palette + template GraphToEps > + nodeColors(const X &x) + { + dontPrint=true; + return GraphToEps >(NodeColorsTraits(*this,x)); + } + template struct NodeTextColorsTraits : public T { + const X &_nodeTextColors; + NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} + }; + ///Sets the map of the node text colors + + ///Sets the map of the node text colors + ///\param x must be a node map with \ref Color values. + /// + ///\sa Palette + template GraphToEps > + nodeTextColors(const X &x) + { + dontPrint=true; + _nodeTextColorType=CUST_COL; + return GraphToEps > + (NodeTextColorsTraits(*this,x)); + } + template struct ArcColorsTraits : public T { + const X &_arcColors; + ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} + }; + ///Sets the map of the arc colors + + ///Sets the map of the arc colors + ///\param x must be a arc map with \ref Color values. + /// + ///\sa Palette + template GraphToEps > + arcColors(const X &x) + { + dontPrint=true; + return GraphToEps >(ArcColorsTraits(*this,x)); + } + ///Sets a global scale factor for node sizes + + ///Sets a global scale factor for node sizes. + /// + /// If nodeSizes() is not given, this function simply sets the node + /// sizes to \c d. If nodeSizes() is given, but + /// autoNodeScale() is not, then the node size given by + /// nodeSizes() will be multiplied by the value \c d. + /// If both nodeSizes() and autoNodeScale() are used, then the + /// node sizes will be scaled in such a way that the greatest size will be + /// equal to \c d. + /// \sa nodeSizes() + /// \sa autoNodeScale() + GraphToEps &nodeScale(double d=.01) {_nodeScale=d;return *this;} + ///Turns on/off the automatic node width scaling. + + ///Turns on/off the automatic node width scaling. + /// + ///\sa nodeScale() + /// + GraphToEps &autoNodeScale(bool b=true) { + _autoNodeScale=b;return *this; + } + + ///Turns on/off the absolutematic node width scaling. + + ///Turns on/off the absolutematic node width scaling. + /// + ///\sa nodeScale() + /// + GraphToEps &absoluteNodeSizes(bool b=true) { + _absoluteNodeSizes=b;return *this; + } + + ///Negates the Y coordinates. + + ///Negates the Y coordinates. + /// + GraphToEps &negateY(bool b=true) { + _negY=b;return *this; + } + + ///Turn on/off pre-scaling + + ///By default graphToEps() rescales the whole image in order to avoid + ///very big or very small bounding boxes. + /// + ///This (p)rescaling can be turned off with this function. + /// + GraphToEps &preScale(bool b=true) { + _preScale=b;return *this; + } + + ///Sets a global scale factor for arc widths + + /// Sets a global scale factor for arc widths. + /// + /// If arcWidths() is not given, this function simply sets the arc + /// widths to \c d. If arcWidths() is given, but + /// autoArcWidthScale() is not, then the arc withs given by + /// arcWidths() will be multiplied by the value \c d. + /// If both arcWidths() and autoArcWidthScale() are used, then the + /// arc withs will be scaled in such a way that the greatest width will be + /// equal to \c d. + GraphToEps &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} + ///Turns on/off the automatic arc width scaling. + + ///Turns on/off the automatic arc width scaling. + /// + ///\sa arcWidthScale() + /// + GraphToEps &autoArcWidthScale(bool b=true) { + _autoArcWidthScale=b;return *this; + } + ///Turns on/off the absolutematic arc width scaling. + + ///Turns on/off the absolutematic arc width scaling. + /// + ///\sa arcWidthScale() + /// + GraphToEps &absoluteArcWidths(bool b=true) { + _absoluteArcWidths=b;return *this; + } + ///Sets a global scale factor for the whole picture + + ///Sets a global scale factor for the whole picture + /// + + GraphToEps &scale(double d) {_scale=d;return *this;} + ///Sets the width of the border around the picture + + ///Sets the width of the border around the picture + /// + GraphToEps &border(double b=10) {_xBorder=_yBorder=b;return *this;} + ///Sets the width of the border around the picture + + ///Sets the width of the border around the picture + /// + GraphToEps &border(double x, double y) { + _xBorder=x;_yBorder=y;return *this; + } + ///Sets whether to draw arrows + + ///Sets whether to draw arrows + /// + GraphToEps &drawArrows(bool b=true) {_drawArrows=b;return *this;} + ///Sets the length of the arrowheads + + ///Sets the length of the arrowheads + /// + GraphToEps &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} + ///Sets the width of the arrowheads + + ///Sets the width of the arrowheads + /// + GraphToEps &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} + + ///Scales the drawing to fit to A4 page + + ///Scales the drawing to fit to A4 page + /// + GraphToEps &scaleToA4() {_scaleToA4=true;return *this;} + + ///Enables parallel arcs + + ///Enables parallel arcs + GraphToEps &enableParallel(bool b=true) {_enableParallel=b;return *this;} + + ///Sets the distance + + ///Sets the distance + /// + GraphToEps &parArcDist(double d) {_parArcDist*=d;return *this;} + + ///Hides the arcs + + ///Hides the arcs + /// + GraphToEps &hideArcs(bool b=true) {_showArcs=!b;return *this;} + ///Hides the nodes + + ///Hides the nodes + /// + GraphToEps &hideNodes(bool b=true) {_showNodes=!b;return *this;} + + ///Sets the size of the node texts + + ///Sets the size of the node texts + /// + GraphToEps &nodeTextSize(double d) {_nodeTextSize=d;return *this;} + + ///Sets the color of the node texts to be different from the node color + + ///Sets the color of the node texts to be as different from the node color + ///as it is possible + /// + GraphToEps &distantColorNodeTexts() + {_nodeTextColorType=DIST_COL;return *this;} + ///Sets the color of the node texts to be black or white and always visible. + + ///Sets the color of the node texts to be black or white according to + ///which is more + ///different from the node color + /// + GraphToEps &distantBWNodeTexts() + {_nodeTextColorType=DIST_BW;return *this;} + + ///Gives a preamble block for node Postscript block. + + ///Gives a preamble block for node Postscript block. + /// + ///\sa nodePsTexts() + GraphToEps & nodePsTextsPreamble(const char *str) { + _nodePsTextsPreamble=str ;return *this; + } + ///Sets whether the the graph is undirected + + ///Sets whether the the graph is undirected. + /// + ///This setting is the default for undirected graphs. + /// + ///\sa directed() + GraphToEps &undirected(bool b=true) {_undirected=b;return *this;} + + ///Sets whether the the graph is directed + + ///Sets whether the the graph is directed. + ///Use it to show the edges as a pair of directed ones. + /// + ///This setting is the default for digraphs. + /// + ///\sa undirected() + GraphToEps &directed(bool b=true) {_undirected=!b;return *this;} + + ///Sets the title. + + ///Sets the title of the generated image, + ///namely it inserts a %%Title: DSC field to the header of + ///the EPS file. + GraphToEps &title(const std::string &t) {_title=t;return *this;} + ///Sets the copyright statement. + + ///Sets the copyright statement of the generated image, + ///namely it inserts a %%Copyright: DSC field to the header of + ///the EPS file. + GraphToEps ©right(const std::string &t) {_copyright=t;return *this;} + +protected: + bool isInsideNode(dim2::Point p, double r,int t) + { + switch(t) { + case CIRCLE: + case MALE: + case FEMALE: + return p.normSquare()<=r*r; + case SQUARE: + return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; + case DIAMOND: + return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; + } + return false; + } + +public: + ~GraphToEps() { } + + ///Draws the graph. + + ///Like other functions using + ///\ref named-templ-func-param "named template parameters", + ///this function calls the algorithm itself, i.e. in this case + ///it draws the graph. + void run() { + //\todo better 'epsilon' would be nice here. + const double EPSILON=1e-9; + if(dontPrint) return; + + _graph_to_eps_bits::_NegY + mycoords(_coords,_negY); + + os << "%!PS-Adobe-2.0 EPSF-2.0\n"; + if(_title.size()>0) os << "%%Title: " << _title << '\n'; + if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; +// << "%%Copyright: XXXX\n" + os << "%%Creator: LEMON, graphToEps()\n"; + + { +#ifndef WIN32 + timeval tv; + gettimeofday(&tv, 0); + + char cbuf[26]; + ctime_r(&tv.tv_sec,cbuf); + os << "%%CreationDate: " << cbuf; +#else + SYSTEMTIME time; + char buf1[11], buf2[9], buf3[5]; + + GetSystemTime(&time); + if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, + "ddd MMM dd", buf1, 11) && + GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time, + "HH':'mm':'ss", buf2, 9) && + GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, + "yyyy", buf3, 5)) { + os << "%%CreationDate: " << buf1 << ' ' + << buf2 << ' ' << buf3 << std::endl; + } +#endif + } + + if (_autoArcWidthScale) { + double max_w=0; + for(ArcIt e(g);e!=INVALID;++e) + max_w=std::max(double(_arcWidths[e]),max_w); + ///\todo better 'epsilon' would be nice here. + if(max_w>EPSILON) { + _arcWidthScale/=max_w; + } + } + + if (_autoNodeScale) { + double max_s=0; + for(NodeIt n(g);n!=INVALID;++n) + max_s=std::max(double(_nodeSizes[n]),max_s); + ///\todo better 'epsilon' would be nice here. + if(max_s>EPSILON) { + _nodeScale/=max_s; + } + } + + double diag_len = 1; + if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { + dim2::BoundingBox bb; + for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); + if (bb.empty()) { + bb = dim2::BoundingBox(dim2::Point(0,0)); + } + diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); + if(diag_len bb; + for(NodeIt n(g);n!=INVALID;++n) { + double ns=_nodeSizes[n]*_nodeScale; + dim2::Point p(ns,ns); + switch(_nodeShapes[n]) { + case CIRCLE: + case SQUARE: + case DIAMOND: + bb.add(p+mycoords[n]); + bb.add(-p+mycoords[n]); + break; + case MALE: + bb.add(-p+mycoords[n]); + bb.add(dim2::Point(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); + break; + case FEMALE: + bb.add(p+mycoords[n]); + bb.add(dim2::Point(-ns,-3.01*ns)+mycoords[n]); + break; + } + } + if (bb.empty()) { + bb = dim2::BoundingBox(dim2::Point(0,0)); + } + + if(_scaleToA4) + os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; + else { + if(_preScale) { + //Rescale so that BoundingBox won't be neither to big nor too small. + while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; + while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; + } + + os << "%%BoundingBox: " + << int(floor(bb.left() * _scale - _xBorder)) << ' ' + << int(floor(bb.bottom() * _scale - _yBorder)) << ' ' + << int(ceil(bb.right() * _scale + _xBorder)) << ' ' + << int(ceil(bb.top() * _scale + _yBorder)) << '\n'; + } + + os << "%%EndComments\n"; + + //x1 y1 x2 y2 x3 y3 cr cg cb w + os << "/lb { setlinewidth setrgbcolor newpath moveto\n" + << " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; + os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; + //x y r + os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n"; + //x y r + os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" + << " 2 index 1 index sub 2 index 2 index add lineto\n" + << " 2 index 1 index sub 2 index 2 index sub lineto\n" + << " 2 index 1 index add 2 index 2 index sub lineto\n" + << " closepath pop pop pop} bind def\n"; + //x y r + os << "/di { newpath 2 index 1 index add 2 index moveto\n" + << " 2 index 2 index 2 index add lineto\n" + << " 2 index 1 index sub 2 index lineto\n" + << " 2 index 2 index 2 index sub lineto\n" + << " closepath pop pop pop} bind def\n"; + // x y r cr cg cb + os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" + << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" + << " } bind def\n"; + os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" + << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" + << " } bind def\n"; + os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" + << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" + << " } bind def\n"; + os << "/nfemale { 0 0 0 setrgbcolor 3 index " + << _nodeBorderQuotient/(1+_nodeBorderQuotient) + << " 1.5 mul mul setlinewidth\n" + << " newpath 5 index 5 index moveto " + << "5 index 5 index 5 index 3.01 mul sub\n" + << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n" + << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n" + << " 5 index 5 index 5 index c fill\n" + << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" + << " } bind def\n"; + os << "/nmale {\n" + << " 0 0 0 setrgbcolor 3 index " + << _nodeBorderQuotient/(1+_nodeBorderQuotient) + <<" 1.5 mul mul setlinewidth\n" + << " newpath 5 index 5 index moveto\n" + << " 5 index 4 index 1 mul 1.5 mul add\n" + << " 5 index 5 index 3 sqrt 1.5 mul mul add\n" + << " 1 index 1 index lineto\n" + << " 1 index 1 index 7 index sub moveto\n" + << " 1 index 1 index lineto\n" + << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n" + << " stroke\n" + << " 5 index 5 index 5 index c fill\n" + << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" + << " } bind def\n"; + + + os << "/arrl " << _arrowLength << " def\n"; + os << "/arrw " << _arrowWidth << " def\n"; + // l dx_norm dy_norm + os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; + //len w dx_norm dy_norm x1 y1 cr cg cb + os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n" + << " /w exch def /len exch def\n" + // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" + << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" + << " len w sub arrl sub dx dy lrl\n" + << " arrw dy dx neg lrl\n" + << " dx arrl w add mul dy w 2 div arrw add mul sub\n" + << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" + << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" + << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" + << " arrw dy dx neg lrl\n" + << " len w sub arrl sub neg dx dy lrl\n" + << " closepath fill } bind def\n"; + os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" + << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; + + os << "\ngsave\n"; + if(_scaleToA4) + if(bb.height()>bb.width()) { + double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), + (A4WIDTH-2*A4BORDER)/bb.width()); + os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' + << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER + << " translate\n" + << sc << " dup scale\n" + << -bb.left() << ' ' << -bb.bottom() << " translate\n"; + } + else { + //\todo Verify centering + double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), + (A4WIDTH-2*A4BORDER)/bb.height()); + os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' + << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER + << " translate\n" + << sc << " dup scale\n90 rotate\n" + << -bb.left() << ' ' << -bb.top() << " translate\n"; + } + else if(_scale!=1.0) os << _scale << " dup scale\n"; + + if(_showArcs) { + os << "%Arcs:\ngsave\n"; + if(_enableParallel) { + std::vector el; + for(ArcIt e(g);e!=INVALID;++e) + if((!_undirected||g.source(e)0 + &&g.source(e)!=g.target(e)) + el.push_back(e); + std::sort(el.begin(),el.end(),arcLess(g)); + + typename std::vector::iterator j; + for(typename std::vector::iterator i=el.begin();i!=el.end();i=j) { + for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; + + double sw=0; + for(typename std::vector::iterator e=i;e!=j;++e) + sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; + sw-=_parArcDist; + sw/=-2.0; + dim2::Point + dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); + double l=std::sqrt(dvec.normSquare()); + //\todo better 'epsilon' would be nice here. + dim2::Point d(dvec/std::max(l,EPSILON)); + dim2::Point m; +// m=dim2::Point(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0; + +// m=dim2::Point(mycoords[g.source(*i)])+ +// dvec*(double(_nodeSizes[g.source(*i)])/ +// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); + + m=dim2::Point(mycoords[g.source(*i)])+ + d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; + + for(typename std::vector::iterator e=i;e!=j;++e) { + sw+=_arcWidths[*e]*_arcWidthScale/2.0; + dim2::Point mm=m+rot90(d)*sw/.75; + if(_drawArrows) { + int node_shape; + dim2::Point s=mycoords[g.source(*e)]; + dim2::Point t=mycoords[g.target(*e)]; + double rn=_nodeSizes[g.target(*e)]*_nodeScale; + node_shape=_nodeShapes[g.target(*e)]; + dim2::Bezier3 bez(s,mm,mm,t); + double t1=0,t2=1; + for(int ii=0;ii apoint=bez((t1+t2)/2); + rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; + rn*=rn; + t2=(t1+t2)/2;t1=0; + for(int ii=0;iirn) t1=(t1+t2)/2; + else t2=(t1+t2)/2; + dim2::Point linend=bez((t1+t2)/2); + bez=bez.before((t1+t2)/2); +// rn=_nodeSizes[g.source(*e)]*_nodeScale; +// node_shape=_nodeShapes[g.source(*e)]; +// t1=0;t2=1; +// for(int i=0;i dd(rot90(linend-apoint)); + dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ + std::sqrt(dd.normSquare()); + os << "newpath " << psOut(apoint) << " moveto " + << psOut(linend+dd) << " lineto " + << psOut(linend-dd) << " lineto closepath fill\n"; + } + else { + os << mycoords[g.source(*e)].x << ' ' + << mycoords[g.source(*e)].y << ' ' + << mm.x << ' ' << mm.y << ' ' + << mycoords[g.target(*e)].x << ' ' + << mycoords[g.target(*e)].y << ' ' + << _arcColors[*e].red() << ' ' + << _arcColors[*e].green() << ' ' + << _arcColors[*e].blue() << ' ' + << _arcWidths[*e]*_arcWidthScale << " lb\n"; + } + sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; + } + } + } + else for(ArcIt e(g);e!=INVALID;++e) + if((!_undirected||g.source(e)0 + &&g.source(e)!=g.target(e)) + if(_drawArrows) { + dim2::Point d(mycoords[g.target(e)]-mycoords[g.source(e)]); + double rn=_nodeSizes[g.target(e)]*_nodeScale; + int node_shape=_nodeShapes[g.target(e)]; + double t1=0,t2=1; + for(int i=0;i GraphToEps > edgeWidths(const X &x) + { + return arcWidths(x); + } + + ///An alias for arcColors() + + ///An alias for arcColors() + /// + template GraphToEps > + edgeColors(const X &x) + { + return arcColors(x); + } + + ///An alias for arcWidthScale() + + ///An alias for arcWidthScale() + /// + GraphToEps &edgeWidthScale(double d) {return arcWidthScale(d);} + + ///An alias for autoArcWidthScale() + + ///An alias for autoArcWidthScale() + /// + GraphToEps &autoEdgeWidthScale(bool b=true) + { + return autoArcWidthScale(b); + } + + ///An alias for absoluteArcWidths() + + ///An alias for absoluteArcWidths() + /// + GraphToEps &absoluteEdgeWidths(bool b=true) + { + return absoluteArcWidths(b); + } + + ///An alias for parArcDist() + + ///An alias for parArcDist() + /// + GraphToEps &parEdgeDist(double d) {return parArcDist(d);} + + ///An alias for hideArcs() + + ///An alias for hideArcs() + /// + GraphToEps &hideEdges(bool b=true) {return hideArcs(b);} + + ///@} +}; + +template +const int GraphToEps::INTERPOL_PREC = 20; +template +const double GraphToEps::A4HEIGHT = 841.8897637795276; +template +const double GraphToEps::A4WIDTH = 595.275590551181; +template +const double GraphToEps::A4BORDER = 15; + + +///Generates an EPS file from a graph + +///\ingroup eps_io +///Generates an EPS file from a graph. +///\param g is a reference to the graph to be printed +///\param os is a reference to the output stream. +///By default it is std::cout +/// +///This function also has a lot of +///\ref named-templ-func-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,os).scale(10).coords(coords) +/// .nodeScale(2).nodeSizes(sizes) +/// .arcWidthScale(.4).run(); +///\endcode +///\warning Don't forget to put the \ref GraphToEps::run() "run()" +///to the end of the parameter list. +///\sa GraphToEps +///\sa graphToEps(G &g, const char *file_name) +template +GraphToEps > +graphToEps(G &g, std::ostream& os=std::cout) +{ + return + GraphToEps >(DefaultGraphToEpsTraits(g,os)); +} + +///Generates an EPS file from a graph + +///\ingroup eps_io +///This function does the same as +///\ref graphToEps(G &g,std::ostream& os) +///but it writes its output into the file \c file_name +///instead of a stream. +///\sa graphToEps(G &g, std::ostream& os) +template +GraphToEps > +graphToEps(G &g,const char *file_name) +{ + return GraphToEps > + (DefaultGraphToEpsTraits(g,*new std::ofstream(file_name),true)); +} + +///Generates an EPS file from a graph + +///\ingroup eps_io +///This function does the same as +///\ref graphToEps(G &g,std::ostream& os) +///but it writes its output into the file \c file_name +///instead of a stream. +///\sa graphToEps(G &g, std::ostream& os) +template +GraphToEps > +graphToEps(G &g,const std::string& file_name) +{ + return GraphToEps > + (DefaultGraphToEpsTraits(g,*new std::ofstream(file_name.c_str()),true)); +} + +} //END OF NAMESPACE LEMON + +#endif // LEMON_GRAPH_TO_EPS_H