alpar@128: /* -*- C++ -*- alpar@128: * alpar@128: * This file is a part of LEMON, a generic C++ optimization library alpar@128: * alpar@128: * Copyright (C) 2003-2008 alpar@128: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@128: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@128: * alpar@128: * Permission to use, modify and distribute this software is granted alpar@128: * provided that this copyright notice appears in all copies. For alpar@128: * precise terms see the accompanying LICENSE file. alpar@128: * alpar@128: * This software is provided "AS IS" with no warranty of any kind, alpar@128: * express or implied, and with no claim as to its suitability for any alpar@128: * purpose. alpar@128: * alpar@128: */ alpar@128: alpar@128: #ifndef LEMON_GRAPH_TO_EPS_H alpar@128: #define LEMON_GRAPH_TO_EPS_H alpar@128: alpar@128: #include alpar@128: #include alpar@128: #include alpar@128: #include alpar@128: #include alpar@128: deba@134: #ifndef WIN32 deba@134: #include alpar@128: #include deba@134: #else alpar@143: #define WIN32_LEAN_AND_MEAN alpar@143: #define NOMINMAX deba@134: #include deba@134: #endif alpar@128: alpar@128: #include alpar@128: #include alpar@128: #include alpar@128: #include alpar@128: #include alpar@128: #include alpar@128: alpar@128: alpar@128: ///\ingroup eps_io alpar@128: ///\file alpar@133: ///\brief A well configurable tool for visualizing graphs alpar@128: alpar@128: namespace lemon { alpar@128: alpar@131: namespace _graph_to_eps_bits { alpar@131: template alpar@131: class _NegY { alpar@131: public: alpar@131: typedef typename MT::Key Key; alpar@131: typedef typename MT::Value Value; alpar@131: const MT ↦ alpar@131: int yscale; alpar@131: _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} alpar@131: Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} alpar@131: }; alpar@131: } alpar@131: alpar@128: ///Default traits class of \ref GraphToEps alpar@128: kpeter@206: ///Default traits class of \ref GraphToEps. alpar@128: /// alpar@128: ///\c G is the type of the underlying graph. alpar@128: template alpar@128: struct DefaultGraphToEpsTraits alpar@128: { alpar@128: typedef G Graph; alpar@128: typedef typename Graph::Node Node; alpar@128: typedef typename Graph::NodeIt NodeIt; alpar@128: typedef typename Graph::Arc Arc; alpar@128: typedef typename Graph::ArcIt ArcIt; alpar@128: typedef typename Graph::InArcIt InArcIt; alpar@128: typedef typename Graph::OutArcIt OutArcIt; alpar@128: alpar@128: alpar@128: const Graph &g; alpar@128: alpar@128: std::ostream& os; alpar@128: alpar@128: typedef ConstMap > CoordsMapType; alpar@128: CoordsMapType _coords; alpar@128: ConstMap _nodeSizes; alpar@128: ConstMap _nodeShapes; alpar@128: alpar@128: ConstMap _nodeColors; alpar@128: ConstMap _arcColors; alpar@128: alpar@128: ConstMap _arcWidths; alpar@128: alpar@128: double _arcWidthScale; alpar@128: alpar@128: double _nodeScale; alpar@128: double _xBorder, _yBorder; alpar@128: double _scale; alpar@128: double _nodeBorderQuotient; alpar@128: alpar@128: bool _drawArrows; alpar@128: double _arrowLength, _arrowWidth; alpar@128: alpar@128: bool _showNodes, _showArcs; alpar@128: alpar@128: bool _enableParallel; alpar@128: double _parArcDist; alpar@128: alpar@128: bool _showNodeText; alpar@128: ConstMap _nodeTexts; alpar@128: double _nodeTextSize; alpar@128: alpar@128: bool _showNodePsText; alpar@128: ConstMap _nodePsTexts; alpar@128: char *_nodePsTextsPreamble; alpar@128: alpar@128: bool _undirected; alpar@128: alpar@128: bool _pleaseRemoveOsStream; alpar@128: alpar@128: bool _scaleToA4; alpar@128: alpar@128: std::string _title; alpar@128: std::string _copyright; alpar@128: alpar@128: enum NodeTextColorType alpar@128: { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; alpar@128: ConstMap _nodeTextColors; alpar@128: alpar@128: bool _autoNodeScale; alpar@128: bool _autoArcWidthScale; alpar@128: alpar@128: bool _absoluteNodeSizes; alpar@128: bool _absoluteArcWidths; alpar@128: alpar@128: bool _negY; alpar@128: alpar@128: bool _preScale; alpar@128: ///Constructor alpar@128: alpar@128: ///Constructor kpeter@206: ///\param _g Reference to the graph to be printed. kpeter@206: ///\param _os Reference to the output stream. kpeter@206: ///\param _os Reference to the output stream. By default it is std::cout. alpar@128: ///\param _pros If it is \c true, then the \c ostream referenced by \c _os alpar@128: ///will be explicitly deallocated by the destructor. alpar@128: DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, alpar@128: bool _pros=false) : alpar@128: g(_g), os(_os), alpar@132: _coords(dim2::Point(1,1)), _nodeSizes(1), _nodeShapes(0), alpar@128: _nodeColors(WHITE), _arcColors(BLACK), alpar@128: _arcWidths(1.0), _arcWidthScale(0.003), alpar@132: _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), alpar@128: _nodeBorderQuotient(.1), alpar@128: _drawArrows(false), _arrowLength(1), _arrowWidth(0.3), alpar@128: _showNodes(true), _showArcs(true), alpar@128: _enableParallel(false), _parArcDist(1), alpar@128: _showNodeText(false), _nodeTexts(false), _nodeTextSize(1), alpar@128: _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), alpar@131: _undirected(lemon::UndirectedTagIndicator::value), alpar@128: _pleaseRemoveOsStream(_pros), _scaleToA4(false), alpar@128: _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), alpar@128: _autoNodeScale(false), alpar@128: _autoArcWidthScale(false), alpar@128: _absoluteNodeSizes(false), alpar@128: _absoluteArcWidths(false), alpar@128: _negY(false), alpar@128: _preScale(true) alpar@128: {} alpar@128: }; alpar@128: alpar@133: ///Auxiliary class to implement the named parameters of \ref graphToEps() alpar@128: kpeter@206: ///Auxiliary class to implement the named parameters of \ref graphToEps(). kpeter@206: /// kpeter@206: ///For detailed examples see the \ref graph_to_eps_demo.cc demo file. alpar@128: template class GraphToEps : public T alpar@128: { alpar@128: // Can't believe it is required by the C++ standard alpar@128: using T::g; alpar@128: using T::os; alpar@128: alpar@128: using T::_coords; alpar@128: using T::_nodeSizes; alpar@128: using T::_nodeShapes; alpar@128: using T::_nodeColors; alpar@128: using T::_arcColors; alpar@128: using T::_arcWidths; alpar@128: alpar@128: using T::_arcWidthScale; alpar@128: using T::_nodeScale; alpar@128: using T::_xBorder; alpar@128: using T::_yBorder; alpar@128: using T::_scale; alpar@128: using T::_nodeBorderQuotient; alpar@128: alpar@128: using T::_drawArrows; alpar@128: using T::_arrowLength; alpar@128: using T::_arrowWidth; alpar@128: alpar@128: using T::_showNodes; alpar@128: using T::_showArcs; alpar@128: alpar@128: using T::_enableParallel; alpar@128: using T::_parArcDist; alpar@128: alpar@128: using T::_showNodeText; alpar@128: using T::_nodeTexts; alpar@128: using T::_nodeTextSize; alpar@128: alpar@128: using T::_showNodePsText; alpar@128: using T::_nodePsTexts; alpar@128: using T::_nodePsTextsPreamble; alpar@128: alpar@128: using T::_undirected; alpar@128: alpar@128: using T::_pleaseRemoveOsStream; alpar@128: alpar@128: using T::_scaleToA4; alpar@128: alpar@128: using T::_title; alpar@128: using T::_copyright; alpar@128: alpar@128: using T::NodeTextColorType; alpar@128: using T::CUST_COL; alpar@128: using T::DIST_COL; alpar@128: using T::DIST_BW; alpar@128: using T::_nodeTextColorType; alpar@128: using T::_nodeTextColors; alpar@128: alpar@128: using T::_autoNodeScale; alpar@128: using T::_autoArcWidthScale; alpar@128: alpar@128: using T::_absoluteNodeSizes; alpar@128: using T::_absoluteArcWidths; alpar@128: alpar@128: alpar@128: using T::_negY; alpar@128: using T::_preScale; alpar@128: alpar@128: // dradnats ++C eht yb deriuqer si ti eveileb t'naC alpar@128: alpar@128: typedef typename T::Graph Graph; alpar@128: typedef typename Graph::Node Node; alpar@128: typedef typename Graph::NodeIt NodeIt; alpar@128: typedef typename Graph::Arc Arc; alpar@128: typedef typename Graph::ArcIt ArcIt; alpar@128: typedef typename Graph::InArcIt InArcIt; alpar@128: typedef typename Graph::OutArcIt OutArcIt; alpar@128: alpar@128: static const int INTERPOL_PREC; alpar@128: static const double A4HEIGHT; alpar@128: static const double A4WIDTH; alpar@128: static const double A4BORDER; alpar@128: alpar@128: bool dontPrint; alpar@128: alpar@128: public: alpar@128: ///Node shapes alpar@128: kpeter@206: ///Node shapes. alpar@128: /// alpar@128: enum NodeShapes { alpar@128: /// = 0 alpar@128: ///\image html nodeshape_0.png alpar@128: ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm alpar@128: CIRCLE=0, alpar@128: /// = 1 alpar@128: ///\image html nodeshape_1.png alpar@128: ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm alpar@128: /// alpar@128: SQUARE=1, alpar@128: /// = 2 alpar@128: ///\image html nodeshape_2.png alpar@128: ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm alpar@128: /// alpar@128: DIAMOND=2, alpar@128: /// = 3 alpar@128: ///\image html nodeshape_3.png alpar@128: ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm alpar@128: /// alpar@128: MALE=3, alpar@128: /// = 4 alpar@128: ///\image html nodeshape_4.png alpar@128: ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm alpar@128: /// alpar@128: FEMALE=4 alpar@128: }; alpar@128: alpar@128: private: alpar@128: class arcLess { alpar@128: const Graph &g; alpar@128: public: alpar@128: arcLess(const Graph &_g) : g(_g) {} alpar@128: bool operator()(Arc a,Arc b) const alpar@128: { alpar@128: Node ai=std::min(g.source(a),g.target(a)); alpar@128: Node aa=std::max(g.source(a),g.target(a)); alpar@128: Node bi=std::min(g.source(b),g.target(b)); alpar@128: Node ba=std::max(g.source(b),g.target(b)); alpar@128: return ai alpar@128: static std::string psOut(const dim2::Point &p) alpar@128: { alpar@128: std::ostringstream os; alpar@128: os << p.x << ' ' << p.y; alpar@128: return os.str(); alpar@128: } alpar@128: static std::string psOut(const Color &c) alpar@128: { alpar@128: std::ostringstream os; alpar@128: os << c.red() << ' ' << c.green() << ' ' << c.blue(); alpar@128: return os.str(); alpar@128: } alpar@128: alpar@128: public: alpar@128: GraphToEps(const T &t) : T(t), dontPrint(false) {}; alpar@128: alpar@128: template struct CoordsTraits : public T { alpar@128: typedef X CoordsMapType; alpar@128: const X &_coords; alpar@128: CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} alpar@128: }; alpar@128: ///Sets the map of the node coordinates alpar@128: alpar@128: ///Sets the map of the node coordinates. kpeter@206: ///\param x must be a node map with \ref dim2::Point "dim2::Point" or alpar@128: ///\ref dim2::Point "dim2::Point" values. alpar@128: template GraphToEps > coords(const X &x) { alpar@128: dontPrint=true; alpar@128: return GraphToEps >(CoordsTraits(*this,x)); alpar@128: } alpar@128: template struct NodeSizesTraits : public T { alpar@128: const X &_nodeSizes; alpar@128: NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} alpar@128: }; alpar@128: ///Sets the map of the node sizes alpar@128: kpeter@206: ///Sets the map of the node sizes. alpar@128: ///\param x must be a node map with \c double (or convertible) values. alpar@128: template GraphToEps > nodeSizes(const X &x) alpar@128: { alpar@128: dontPrint=true; alpar@128: return GraphToEps >(NodeSizesTraits(*this,x)); alpar@128: } alpar@128: template struct NodeShapesTraits : public T { alpar@128: const X &_nodeShapes; alpar@128: NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} alpar@128: }; alpar@128: ///Sets the map of the node shapes alpar@128: alpar@128: ///Sets the map of the node shapes. alpar@133: ///The available shape values alpar@128: ///can be found in \ref NodeShapes "enum NodeShapes". alpar@128: ///\param x must be a node map with \c int (or convertible) values. alpar@128: ///\sa NodeShapes alpar@128: template GraphToEps > nodeShapes(const X &x) alpar@128: { alpar@128: dontPrint=true; alpar@128: return GraphToEps >(NodeShapesTraits(*this,x)); alpar@128: } alpar@128: template struct NodeTextsTraits : public T { alpar@128: const X &_nodeTexts; alpar@128: NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} alpar@128: }; alpar@128: ///Sets the text printed on the nodes alpar@128: kpeter@206: ///Sets the text printed on the nodes. alpar@128: ///\param x must be a node map with type that can be pushed to a standard kpeter@206: ///\c ostream. alpar@128: template GraphToEps > nodeTexts(const X &x) alpar@128: { alpar@128: dontPrint=true; alpar@128: _showNodeText=true; alpar@128: return GraphToEps >(NodeTextsTraits(*this,x)); alpar@128: } alpar@128: template struct NodePsTextsTraits : public T { alpar@128: const X &_nodePsTexts; alpar@128: NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} alpar@128: }; alpar@128: ///Inserts a PostScript block to the nodes alpar@128: alpar@128: ///With this command it is possible to insert a verbatim PostScript alpar@128: ///block to the nodes. kpeter@206: ///The PS current point will be moved to the center of the node before alpar@128: ///the PostScript block inserted. alpar@128: /// alpar@128: ///Before and after the block a newline character is inserted so you alpar@128: ///don't have to bother with the separators. alpar@128: /// alpar@128: ///\param x must be a node map with type that can be pushed to a standard kpeter@206: ///\c ostream. alpar@128: /// alpar@128: ///\sa nodePsTextsPreamble() alpar@128: template GraphToEps > nodePsTexts(const X &x) alpar@128: { alpar@128: dontPrint=true; alpar@128: _showNodePsText=true; alpar@128: return GraphToEps >(NodePsTextsTraits(*this,x)); alpar@128: } alpar@128: template struct ArcWidthsTraits : public T { alpar@128: const X &_arcWidths; alpar@128: ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} alpar@128: }; alpar@128: ///Sets the map of the arc widths alpar@128: kpeter@206: ///Sets the map of the arc widths. kpeter@157: ///\param x must be an arc map with \c double (or convertible) values. alpar@128: template GraphToEps > arcWidths(const X &x) alpar@128: { alpar@128: dontPrint=true; alpar@128: return GraphToEps >(ArcWidthsTraits(*this,x)); alpar@128: } alpar@128: alpar@128: template struct NodeColorsTraits : public T { alpar@128: const X &_nodeColors; alpar@128: NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} alpar@128: }; alpar@128: ///Sets the map of the node colors alpar@128: kpeter@206: ///Sets the map of the node colors. alpar@128: ///\param x must be a node map with \ref Color values. alpar@128: /// alpar@128: ///\sa Palette alpar@128: template GraphToEps > alpar@128: nodeColors(const X &x) alpar@128: { alpar@128: dontPrint=true; alpar@128: return GraphToEps >(NodeColorsTraits(*this,x)); alpar@128: } alpar@128: template struct NodeTextColorsTraits : public T { alpar@128: const X &_nodeTextColors; alpar@128: NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} alpar@128: }; alpar@128: ///Sets the map of the node text colors alpar@128: kpeter@206: ///Sets the map of the node text colors. alpar@128: ///\param x must be a node map with \ref Color values. alpar@128: /// alpar@128: ///\sa Palette alpar@128: template GraphToEps > alpar@128: nodeTextColors(const X &x) alpar@128: { alpar@128: dontPrint=true; alpar@128: _nodeTextColorType=CUST_COL; alpar@128: return GraphToEps > alpar@128: (NodeTextColorsTraits(*this,x)); alpar@128: } alpar@128: template struct ArcColorsTraits : public T { alpar@128: const X &_arcColors; alpar@128: ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} alpar@128: }; alpar@128: ///Sets the map of the arc colors alpar@128: kpeter@206: ///Sets the map of the arc colors. kpeter@157: ///\param x must be an arc map with \ref Color values. alpar@128: /// alpar@128: ///\sa Palette alpar@128: template GraphToEps > alpar@128: arcColors(const X &x) alpar@128: { alpar@128: dontPrint=true; alpar@128: return GraphToEps >(ArcColorsTraits(*this,x)); alpar@128: } alpar@128: ///Sets a global scale factor for node sizes alpar@128: alpar@128: ///Sets a global scale factor for node sizes. alpar@128: /// alpar@128: /// If nodeSizes() is not given, this function simply sets the node alpar@128: /// sizes to \c d. If nodeSizes() is given, but alpar@128: /// autoNodeScale() is not, then the node size given by alpar@128: /// nodeSizes() will be multiplied by the value \c d. alpar@128: /// If both nodeSizes() and autoNodeScale() are used, then the alpar@128: /// node sizes will be scaled in such a way that the greatest size will be alpar@128: /// equal to \c d. alpar@128: /// \sa nodeSizes() alpar@128: /// \sa autoNodeScale() alpar@132: GraphToEps &nodeScale(double d=.01) {_nodeScale=d;return *this;} kpeter@206: ///Turns on/off the automatic node size scaling. alpar@128: kpeter@206: ///Turns on/off the automatic node size scaling. alpar@128: /// alpar@128: ///\sa nodeScale() alpar@128: /// alpar@128: GraphToEps &autoNodeScale(bool b=true) { alpar@128: _autoNodeScale=b;return *this; alpar@128: } alpar@128: kpeter@206: ///Turns on/off the absolutematic node size scaling. alpar@128: kpeter@206: ///Turns on/off the absolutematic node size scaling. alpar@128: /// alpar@128: ///\sa nodeScale() alpar@128: /// alpar@128: GraphToEps &absoluteNodeSizes(bool b=true) { alpar@128: _absoluteNodeSizes=b;return *this; alpar@128: } alpar@128: alpar@128: ///Negates the Y coordinates. alpar@128: GraphToEps &negateY(bool b=true) { alpar@128: _negY=b;return *this; alpar@128: } alpar@128: alpar@133: ///Turn on/off pre-scaling alpar@128: alpar@128: ///By default graphToEps() rescales the whole image in order to avoid alpar@128: ///very big or very small bounding boxes. alpar@128: /// alpar@128: ///This (p)rescaling can be turned off with this function. alpar@128: /// alpar@128: GraphToEps &preScale(bool b=true) { alpar@128: _preScale=b;return *this; alpar@128: } alpar@128: alpar@128: ///Sets a global scale factor for arc widths alpar@128: alpar@128: /// Sets a global scale factor for arc widths. alpar@128: /// alpar@128: /// If arcWidths() is not given, this function simply sets the arc alpar@128: /// widths to \c d. If arcWidths() is given, but alpar@128: /// autoArcWidthScale() is not, then the arc withs given by alpar@128: /// arcWidths() will be multiplied by the value \c d. alpar@128: /// If both arcWidths() and autoArcWidthScale() are used, then the alpar@128: /// arc withs will be scaled in such a way that the greatest width will be alpar@128: /// equal to \c d. alpar@132: GraphToEps &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} alpar@128: ///Turns on/off the automatic arc width scaling. alpar@128: alpar@128: ///Turns on/off the automatic arc width scaling. alpar@128: /// alpar@128: ///\sa arcWidthScale() alpar@128: /// alpar@128: GraphToEps &autoArcWidthScale(bool b=true) { alpar@128: _autoArcWidthScale=b;return *this; alpar@128: } alpar@128: ///Turns on/off the absolutematic arc width scaling. alpar@128: alpar@128: ///Turns on/off the absolutematic arc width scaling. alpar@128: /// alpar@128: ///\sa arcWidthScale() alpar@128: /// alpar@128: GraphToEps &absoluteArcWidths(bool b=true) { alpar@128: _absoluteArcWidths=b;return *this; alpar@128: } alpar@128: ///Sets a global scale factor for the whole picture alpar@128: GraphToEps &scale(double d) {_scale=d;return *this;} alpar@128: ///Sets the width of the border around the picture alpar@133: GraphToEps &border(double b=10) {_xBorder=_yBorder=b;return *this;} alpar@128: ///Sets the width of the border around the picture alpar@128: GraphToEps &border(double x, double y) { alpar@128: _xBorder=x;_yBorder=y;return *this; alpar@128: } alpar@128: ///Sets whether to draw arrows alpar@128: GraphToEps &drawArrows(bool b=true) {_drawArrows=b;return *this;} alpar@128: ///Sets the length of the arrowheads alpar@133: GraphToEps &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} alpar@128: ///Sets the width of the arrowheads alpar@133: GraphToEps &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} alpar@128: alpar@128: ///Scales the drawing to fit to A4 page alpar@128: GraphToEps &scaleToA4() {_scaleToA4=true;return *this;} alpar@128: alpar@128: ///Enables parallel arcs alpar@128: GraphToEps &enableParallel(bool b=true) {_enableParallel=b;return *this;} alpar@128: kpeter@206: ///Sets the distance between parallel arcs alpar@128: GraphToEps &parArcDist(double d) {_parArcDist*=d;return *this;} alpar@128: alpar@128: ///Hides the arcs alpar@128: GraphToEps &hideArcs(bool b=true) {_showArcs=!b;return *this;} alpar@128: ///Hides the nodes alpar@128: GraphToEps &hideNodes(bool b=true) {_showNodes=!b;return *this;} alpar@128: alpar@128: ///Sets the size of the node texts alpar@128: GraphToEps &nodeTextSize(double d) {_nodeTextSize=d;return *this;} alpar@128: alpar@128: ///Sets the color of the node texts to be different from the node color alpar@128: alpar@128: ///Sets the color of the node texts to be as different from the node color kpeter@206: ///as it is possible. alpar@128: GraphToEps &distantColorNodeTexts() alpar@128: {_nodeTextColorType=DIST_COL;return *this;} alpar@128: ///Sets the color of the node texts to be black or white and always visible. alpar@128: alpar@128: ///Sets the color of the node texts to be black or white according to kpeter@206: ///which is more different from the node color. alpar@128: GraphToEps &distantBWNodeTexts() alpar@128: {_nodeTextColorType=DIST_BW;return *this;} alpar@128: alpar@128: ///Gives a preamble block for node Postscript block. alpar@128: alpar@128: ///Gives a preamble block for node Postscript block. alpar@128: /// alpar@128: ///\sa nodePsTexts() alpar@128: GraphToEps & nodePsTextsPreamble(const char *str) { alpar@128: _nodePsTextsPreamble=str ;return *this; alpar@128: } kpeter@206: ///Sets whether the graph is undirected alpar@128: kpeter@206: ///Sets whether the graph is undirected. alpar@128: /// alpar@133: ///This setting is the default for undirected graphs. alpar@133: /// alpar@133: ///\sa directed() alpar@133: GraphToEps &undirected(bool b=true) {_undirected=b;return *this;} alpar@128: kpeter@206: ///Sets whether the graph is directed alpar@128: kpeter@206: ///Sets whether the graph is directed. alpar@128: ///Use it to show the edges as a pair of directed ones. alpar@133: /// alpar@133: ///This setting is the default for digraphs. alpar@133: /// alpar@133: ///\sa undirected() alpar@131: GraphToEps &directed(bool b=true) {_undirected=!b;return *this;} alpar@133: alpar@128: ///Sets the title. alpar@128: alpar@128: ///Sets the title of the generated image, alpar@128: ///namely it inserts a %%Title: DSC field to the header of alpar@128: ///the EPS file. alpar@128: GraphToEps &title(const std::string &t) {_title=t;return *this;} alpar@128: ///Sets the copyright statement. alpar@128: alpar@128: ///Sets the copyright statement of the generated image, alpar@128: ///namely it inserts a %%Copyright: DSC field to the header of alpar@128: ///the EPS file. alpar@128: GraphToEps ©right(const std::string &t) {_copyright=t;return *this;} alpar@128: alpar@128: protected: alpar@128: bool isInsideNode(dim2::Point p, double r,int t) alpar@128: { alpar@128: switch(t) { alpar@128: case CIRCLE: alpar@128: case MALE: alpar@128: case FEMALE: alpar@128: return p.normSquare()<=r*r; alpar@128: case SQUARE: alpar@128: return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; alpar@128: case DIAMOND: alpar@128: return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; alpar@128: } alpar@128: return false; alpar@128: } alpar@128: alpar@128: public: alpar@128: ~GraphToEps() { } alpar@128: alpar@128: ///Draws the graph. alpar@128: alpar@128: ///Like other functions using alpar@128: ///\ref named-templ-func-param "named template parameters", alpar@133: ///this function calls the algorithm itself, i.e. in this case alpar@128: ///it draws the graph. alpar@128: void run() { alpar@133: //\todo better 'epsilon' would be nice here. alpar@128: const double EPSILON=1e-9; alpar@128: if(dontPrint) return; alpar@128: alpar@131: _graph_to_eps_bits::_NegY alpar@131: mycoords(_coords,_negY); alpar@128: alpar@128: os << "%!PS-Adobe-2.0 EPSF-2.0\n"; alpar@128: if(_title.size()>0) os << "%%Title: " << _title << '\n'; alpar@128: if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; alpar@128: os << "%%Creator: LEMON, graphToEps()\n"; deba@134: deba@134: { deba@134: #ifndef WIN32 alpar@128: timeval tv; alpar@128: gettimeofday(&tv, 0); deba@134: deba@134: char cbuf[26]; alpar@128: ctime_r(&tv.tv_sec,cbuf); alpar@128: os << "%%CreationDate: " << cbuf; deba@134: #else deba@134: SYSTEMTIME time; deba@134: char buf1[11], buf2[9], buf3[5]; deba@134: deba@134: GetSystemTime(&time); deba@134: if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, deba@134: "ddd MMM dd", buf1, 11) && deba@134: GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time, deba@134: "HH':'mm':'ss", buf2, 9) && deba@134: GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, deba@134: "yyyy", buf3, 5)) { deba@134: os << "%%CreationDate: " << buf1 << ' ' deba@134: << buf2 << ' ' << buf3 << std::endl; deba@134: } deba@134: #endif alpar@128: } alpar@128: alpar@128: if (_autoArcWidthScale) { alpar@128: double max_w=0; alpar@128: for(ArcIt e(g);e!=INVALID;++e) alpar@128: max_w=std::max(double(_arcWidths[e]),max_w); kpeter@206: //\todo better 'epsilon' would be nice here. alpar@128: if(max_w>EPSILON) { alpar@128: _arcWidthScale/=max_w; alpar@128: } alpar@128: } alpar@128: alpar@128: if (_autoNodeScale) { alpar@128: double max_s=0; alpar@128: for(NodeIt n(g);n!=INVALID;++n) alpar@128: max_s=std::max(double(_nodeSizes[n]),max_s); kpeter@206: //\todo better 'epsilon' would be nice here. alpar@128: if(max_s>EPSILON) { alpar@128: _nodeScale/=max_s; alpar@128: } alpar@128: } alpar@128: alpar@128: double diag_len = 1; alpar@128: if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { alpar@128: dim2::BoundingBox bb; alpar@128: for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); alpar@128: if (bb.empty()) { alpar@128: bb = dim2::BoundingBox(dim2::Point(0,0)); alpar@128: } alpar@128: diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); alpar@128: if(diag_len bb; alpar@128: for(NodeIt n(g);n!=INVALID;++n) { alpar@128: double ns=_nodeSizes[n]*_nodeScale; alpar@128: dim2::Point p(ns,ns); alpar@128: switch(_nodeShapes[n]) { alpar@128: case CIRCLE: alpar@128: case SQUARE: alpar@128: case DIAMOND: alpar@128: bb.add(p+mycoords[n]); alpar@128: bb.add(-p+mycoords[n]); alpar@128: break; alpar@128: case MALE: alpar@128: bb.add(-p+mycoords[n]); alpar@128: bb.add(dim2::Point(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); alpar@128: break; alpar@128: case FEMALE: alpar@128: bb.add(p+mycoords[n]); alpar@128: bb.add(dim2::Point(-ns,-3.01*ns)+mycoords[n]); alpar@128: break; alpar@128: } alpar@128: } alpar@128: if (bb.empty()) { alpar@128: bb = dim2::BoundingBox(dim2::Point(0,0)); alpar@128: } alpar@128: alpar@128: if(_scaleToA4) alpar@128: os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; alpar@128: else { alpar@128: if(_preScale) { alpar@128: //Rescale so that BoundingBox won't be neither to big nor too small. alpar@128: while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; alpar@128: while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; alpar@128: } alpar@128: alpar@128: os << "%%BoundingBox: " alpar@128: << int(floor(bb.left() * _scale - _xBorder)) << ' ' alpar@128: << int(floor(bb.bottom() * _scale - _yBorder)) << ' ' alpar@128: << int(ceil(bb.right() * _scale + _xBorder)) << ' ' alpar@128: << int(ceil(bb.top() * _scale + _yBorder)) << '\n'; alpar@128: } alpar@128: alpar@128: os << "%%EndComments\n"; alpar@128: alpar@128: //x1 y1 x2 y2 x3 y3 cr cg cb w alpar@128: os << "/lb { setlinewidth setrgbcolor newpath moveto\n" alpar@128: << " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; alpar@128: os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; alpar@128: //x y r alpar@128: os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n"; alpar@128: //x y r alpar@128: os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" alpar@128: << " 2 index 1 index sub 2 index 2 index add lineto\n" alpar@128: << " 2 index 1 index sub 2 index 2 index sub lineto\n" alpar@128: << " 2 index 1 index add 2 index 2 index sub lineto\n" alpar@128: << " closepath pop pop pop} bind def\n"; alpar@128: //x y r alpar@128: os << "/di { newpath 2 index 1 index add 2 index moveto\n" alpar@128: << " 2 index 2 index 2 index add lineto\n" alpar@128: << " 2 index 1 index sub 2 index lineto\n" alpar@128: << " 2 index 2 index 2 index sub lineto\n" alpar@128: << " closepath pop pop pop} bind def\n"; alpar@128: // x y r cr cg cb alpar@128: os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" alpar@128: << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" alpar@128: << " } bind def\n"; alpar@128: os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" alpar@128: << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" alpar@128: << " } bind def\n"; alpar@128: os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" alpar@128: << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" alpar@128: << " } bind def\n"; alpar@128: os << "/nfemale { 0 0 0 setrgbcolor 3 index " alpar@128: << _nodeBorderQuotient/(1+_nodeBorderQuotient) alpar@128: << " 1.5 mul mul setlinewidth\n" alpar@128: << " newpath 5 index 5 index moveto " alpar@128: << "5 index 5 index 5 index 3.01 mul sub\n" alpar@128: << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n" alpar@128: << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n" alpar@128: << " 5 index 5 index 5 index c fill\n" alpar@128: << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" alpar@128: << " } bind def\n"; alpar@128: os << "/nmale {\n" alpar@128: << " 0 0 0 setrgbcolor 3 index " alpar@128: << _nodeBorderQuotient/(1+_nodeBorderQuotient) alpar@128: <<" 1.5 mul mul setlinewidth\n" alpar@128: << " newpath 5 index 5 index moveto\n" alpar@128: << " 5 index 4 index 1 mul 1.5 mul add\n" alpar@128: << " 5 index 5 index 3 sqrt 1.5 mul mul add\n" alpar@128: << " 1 index 1 index lineto\n" alpar@128: << " 1 index 1 index 7 index sub moveto\n" alpar@128: << " 1 index 1 index lineto\n" alpar@128: << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n" alpar@128: << " stroke\n" alpar@128: << " 5 index 5 index 5 index c fill\n" alpar@128: << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" alpar@128: << " } bind def\n"; alpar@128: alpar@128: alpar@128: os << "/arrl " << _arrowLength << " def\n"; alpar@128: os << "/arrw " << _arrowWidth << " def\n"; alpar@128: // l dx_norm dy_norm alpar@128: os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; alpar@128: //len w dx_norm dy_norm x1 y1 cr cg cb alpar@128: os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n" alpar@128: << " /w exch def /len exch def\n" alpar@128: // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" alpar@128: << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" alpar@128: << " len w sub arrl sub dx dy lrl\n" alpar@128: << " arrw dy dx neg lrl\n" alpar@128: << " dx arrl w add mul dy w 2 div arrw add mul sub\n" alpar@128: << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" alpar@128: << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" alpar@128: << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" alpar@128: << " arrw dy dx neg lrl\n" alpar@128: << " len w sub arrl sub neg dx dy lrl\n" alpar@128: << " closepath fill } bind def\n"; alpar@128: os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" alpar@128: << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; alpar@128: alpar@128: os << "\ngsave\n"; alpar@128: if(_scaleToA4) alpar@128: if(bb.height()>bb.width()) { alpar@128: double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), alpar@128: (A4WIDTH-2*A4BORDER)/bb.width()); alpar@128: os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' alpar@128: << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER alpar@128: << " translate\n" alpar@128: << sc << " dup scale\n" alpar@128: << -bb.left() << ' ' << -bb.bottom() << " translate\n"; alpar@128: } alpar@128: else { alpar@128: //\todo Verify centering alpar@128: double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), alpar@128: (A4WIDTH-2*A4BORDER)/bb.height()); alpar@128: os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' alpar@128: << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER alpar@128: << " translate\n" alpar@128: << sc << " dup scale\n90 rotate\n" alpar@128: << -bb.left() << ' ' << -bb.top() << " translate\n"; alpar@128: } alpar@128: else if(_scale!=1.0) os << _scale << " dup scale\n"; alpar@128: alpar@128: if(_showArcs) { alpar@128: os << "%Arcs:\ngsave\n"; alpar@128: if(_enableParallel) { alpar@128: std::vector el; alpar@128: for(ArcIt e(g);e!=INVALID;++e) alpar@128: if((!_undirected||g.source(e)0 alpar@128: &&g.source(e)!=g.target(e)) alpar@128: el.push_back(e); alpar@128: std::sort(el.begin(),el.end(),arcLess(g)); alpar@128: alpar@128: typename std::vector::iterator j; alpar@128: for(typename std::vector::iterator i=el.begin();i!=el.end();i=j) { alpar@128: for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; alpar@128: alpar@128: double sw=0; alpar@128: for(typename std::vector::iterator e=i;e!=j;++e) alpar@128: sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; alpar@128: sw-=_parArcDist; alpar@128: sw/=-2.0; alpar@128: dim2::Point alpar@128: dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); alpar@128: double l=std::sqrt(dvec.normSquare()); alpar@133: //\todo better 'epsilon' would be nice here. alpar@128: dim2::Point d(dvec/std::max(l,EPSILON)); alpar@128: dim2::Point m; alpar@128: // m=dim2::Point(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0; alpar@128: alpar@128: // m=dim2::Point(mycoords[g.source(*i)])+ alpar@128: // dvec*(double(_nodeSizes[g.source(*i)])/ alpar@128: // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); alpar@128: alpar@128: m=dim2::Point(mycoords[g.source(*i)])+ alpar@128: d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; alpar@128: alpar@128: for(typename std::vector::iterator e=i;e!=j;++e) { alpar@128: sw+=_arcWidths[*e]*_arcWidthScale/2.0; alpar@128: dim2::Point mm=m+rot90(d)*sw/.75; alpar@128: if(_drawArrows) { alpar@128: int node_shape; alpar@128: dim2::Point s=mycoords[g.source(*e)]; alpar@128: dim2::Point t=mycoords[g.target(*e)]; alpar@128: double rn=_nodeSizes[g.target(*e)]*_nodeScale; alpar@128: node_shape=_nodeShapes[g.target(*e)]; alpar@128: dim2::Bezier3 bez(s,mm,mm,t); alpar@128: double t1=0,t2=1; alpar@128: for(int ii=0;ii apoint=bez((t1+t2)/2); alpar@128: rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; alpar@128: rn*=rn; alpar@128: t2=(t1+t2)/2;t1=0; alpar@128: for(int ii=0;iirn) t1=(t1+t2)/2; alpar@128: else t2=(t1+t2)/2; alpar@128: dim2::Point linend=bez((t1+t2)/2); alpar@128: bez=bez.before((t1+t2)/2); alpar@128: // rn=_nodeSizes[g.source(*e)]*_nodeScale; alpar@128: // node_shape=_nodeShapes[g.source(*e)]; alpar@128: // t1=0;t2=1; alpar@128: // for(int i=0;i dd(rot90(linend-apoint)); alpar@128: dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ alpar@128: std::sqrt(dd.normSquare()); alpar@128: os << "newpath " << psOut(apoint) << " moveto " alpar@128: << psOut(linend+dd) << " lineto " alpar@128: << psOut(linend-dd) << " lineto closepath fill\n"; alpar@128: } alpar@128: else { alpar@128: os << mycoords[g.source(*e)].x << ' ' alpar@128: << mycoords[g.source(*e)].y << ' ' alpar@128: << mm.x << ' ' << mm.y << ' ' alpar@128: << mycoords[g.target(*e)].x << ' ' alpar@128: << mycoords[g.target(*e)].y << ' ' alpar@128: << _arcColors[*e].red() << ' ' alpar@128: << _arcColors[*e].green() << ' ' alpar@128: << _arcColors[*e].blue() << ' ' alpar@128: << _arcWidths[*e]*_arcWidthScale << " lb\n"; alpar@128: } alpar@128: sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; alpar@128: } alpar@128: } alpar@128: } alpar@128: else for(ArcIt e(g);e!=INVALID;++e) alpar@128: if((!_undirected||g.source(e)0 alpar@184: &&g.source(e)!=g.target(e)) { alpar@128: if(_drawArrows) { alpar@128: dim2::Point d(mycoords[g.target(e)]-mycoords[g.source(e)]); alpar@128: double rn=_nodeSizes[g.target(e)]*_nodeScale; alpar@128: int node_shape=_nodeShapes[g.target(e)]; alpar@128: double t1=0,t2=1; alpar@128: for(int i=0;i GraphToEps > edgeWidths(const X &x) alpar@130: { alpar@130: return arcWidths(x); alpar@130: } alpar@130: alpar@130: ///An alias for arcColors() alpar@130: template GraphToEps > alpar@130: edgeColors(const X &x) alpar@130: { alpar@130: return arcColors(x); alpar@130: } alpar@130: alpar@130: ///An alias for arcWidthScale() alpar@130: GraphToEps &edgeWidthScale(double d) {return arcWidthScale(d);} alpar@130: alpar@130: ///An alias for autoArcWidthScale() alpar@130: GraphToEps &autoEdgeWidthScale(bool b=true) alpar@130: { alpar@130: return autoArcWidthScale(b); alpar@130: } alpar@130: alpar@130: ///An alias for absoluteArcWidths() alpar@130: GraphToEps &absoluteEdgeWidths(bool b=true) alpar@130: { alpar@130: return absoluteArcWidths(b); alpar@130: } alpar@130: alpar@130: ///An alias for parArcDist() alpar@130: GraphToEps &parEdgeDist(double d) {return parArcDist(d);} alpar@130: alpar@130: ///An alias for hideArcs() alpar@130: GraphToEps &hideEdges(bool b=true) {return hideArcs(b);} alpar@130: alpar@130: ///@} alpar@128: }; alpar@128: alpar@128: template alpar@128: const int GraphToEps::INTERPOL_PREC = 20; alpar@128: template alpar@128: const double GraphToEps::A4HEIGHT = 841.8897637795276; alpar@128: template alpar@128: const double GraphToEps::A4WIDTH = 595.275590551181; alpar@128: template alpar@128: const double GraphToEps::A4BORDER = 15; alpar@128: alpar@128: alpar@128: ///Generates an EPS file from a graph alpar@128: alpar@128: ///\ingroup eps_io alpar@128: ///Generates an EPS file from a graph. kpeter@206: ///\param g Reference to the graph to be printed. kpeter@206: ///\param os Reference to the output stream. kpeter@206: ///By default it is std::cout. alpar@128: /// alpar@128: ///This function also has a lot of alpar@128: ///\ref named-templ-func-param "named parameters", alpar@128: ///they are declared as the members of class \ref GraphToEps. The following alpar@128: ///example shows how to use these parameters. alpar@128: ///\code alpar@128: /// graphToEps(g,os).scale(10).coords(coords) alpar@128: /// .nodeScale(2).nodeSizes(sizes) alpar@128: /// .arcWidthScale(.4).run(); alpar@128: ///\endcode kpeter@206: /// kpeter@206: ///For more detailed examples see the \ref graph_to_eps_demo.cc demo file. kpeter@206: /// alpar@128: ///\warning Don't forget to put the \ref GraphToEps::run() "run()" alpar@128: ///to the end of the parameter list. alpar@128: ///\sa GraphToEps alpar@128: ///\sa graphToEps(G &g, const char *file_name) alpar@128: template alpar@128: GraphToEps > alpar@128: graphToEps(G &g, std::ostream& os=std::cout) alpar@128: { alpar@128: return alpar@128: GraphToEps >(DefaultGraphToEpsTraits(g,os)); alpar@128: } alpar@128: alpar@128: ///Generates an EPS file from a graph alpar@128: alpar@128: ///\ingroup eps_io alpar@128: ///This function does the same as alpar@128: ///\ref graphToEps(G &g,std::ostream& os) alpar@128: ///but it writes its output into the file \c file_name alpar@128: ///instead of a stream. alpar@128: ///\sa graphToEps(G &g, std::ostream& os) alpar@128: template alpar@128: GraphToEps > alpar@128: graphToEps(G &g,const char *file_name) alpar@128: { alpar@128: return GraphToEps > alpar@128: (DefaultGraphToEpsTraits(g,*new std::ofstream(file_name),true)); alpar@128: } alpar@128: alpar@128: ///Generates an EPS file from a graph alpar@128: alpar@128: ///\ingroup eps_io alpar@128: ///This function does the same as alpar@128: ///\ref graphToEps(G &g,std::ostream& os) alpar@128: ///but it writes its output into the file \c file_name alpar@128: ///instead of a stream. alpar@128: ///\sa graphToEps(G &g, std::ostream& os) alpar@128: template alpar@128: GraphToEps > alpar@128: graphToEps(G &g,const std::string& file_name) alpar@128: { alpar@128: return GraphToEps > alpar@128: (DefaultGraphToEpsTraits(g,*new std::ofstream(file_name.c_str()),true)); alpar@128: } alpar@128: alpar@128: } //END OF NAMESPACE LEMON alpar@128: alpar@128: #endif // LEMON_GRAPH_TO_EPS_H