| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
| 20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
| 21 | 21 |
|
| 22 |
#include <sys/time.h> |
|
| 23 |
|
|
| 24 |
#ifdef WIN32 |
|
| 25 |
#include <lemon/bits/mingw32_time.h> |
|
| 26 |
#endif |
|
| 27 |
|
|
| 28 | 22 |
#include<iostream> |
| 29 | 23 |
#include<fstream> |
| 30 | 24 |
#include<sstream> |
| 31 | 25 |
#include<algorithm> |
| 32 | 26 |
#include<vector> |
| 33 | 27 |
|
| 28 |
#ifndef WIN32 |
|
| 29 |
#include<sys/time.h> |
|
| 34 | 30 |
#include<ctime> |
| 31 |
#else |
|
| 32 |
#include<windows.h> |
|
| 33 |
#endif |
|
| 35 | 34 |
|
| 36 | 35 |
#include<lemon/math.h> |
| 37 | 36 |
#include<lemon/bits/invalid.h> |
| 38 | 37 |
#include<lemon/dim2.h> |
| 39 | 38 |
#include<lemon/maps.h> |
| 40 | 39 |
#include<lemon/color.h> |
| 41 | 40 |
#include<lemon/bits/bezier.h> |
| 42 | 41 |
|
| 43 | 42 |
|
| 44 | 43 |
///\ingroup eps_io |
| 45 | 44 |
///\file |
| 46 | 45 |
///\brief A well configurable tool for visualizing graphs |
| 47 | 46 |
|
| 48 | 47 |
namespace lemon {
|
| 49 | 48 |
|
| 50 | 49 |
namespace _graph_to_eps_bits {
|
| 51 | 50 |
template<class MT> |
| 52 | 51 |
class _NegY {
|
| 53 | 52 |
public: |
| 54 | 53 |
typedef typename MT::Key Key; |
| 55 | 54 |
typedef typename MT::Value Value; |
| 56 | 55 |
const MT ↦ |
| 57 | 56 |
int yscale; |
| 58 | 57 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
|
| 59 | 58 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
|
| 60 | 59 |
}; |
| 61 | 60 |
} |
| 62 | 61 |
|
| 63 | 62 |
///Default traits class of \ref GraphToEps |
| 64 | 63 |
|
| 65 | 64 |
///Default traits class of \ref GraphToEps |
| 66 | 65 |
/// |
| 67 | 66 |
///\c G is the type of the underlying graph. |
| 68 | 67 |
template<class G> |
| 69 | 68 |
struct DefaultGraphToEpsTraits |
| 70 | 69 |
{
|
| 71 | 70 |
typedef G Graph; |
| 72 | 71 |
typedef typename Graph::Node Node; |
| 73 | 72 |
typedef typename Graph::NodeIt NodeIt; |
| 74 | 73 |
typedef typename Graph::Arc Arc; |
| 75 | 74 |
typedef typename Graph::ArcIt ArcIt; |
| 76 | 75 |
typedef typename Graph::InArcIt InArcIt; |
| 77 | 76 |
typedef typename Graph::OutArcIt OutArcIt; |
| 78 | 77 |
|
| 79 | 78 |
|
| 80 | 79 |
const Graph &g; |
| 81 | 80 |
|
| 82 | 81 |
std::ostream& os; |
| 83 | 82 |
|
| 84 | 83 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
| 85 | 84 |
CoordsMapType _coords; |
| 86 | 85 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
| 87 | 86 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
| 88 | 87 |
|
| 89 | 88 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
| 90 | 89 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
| 91 | 90 |
|
| 92 | 91 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
| 93 | 92 |
|
| 94 | 93 |
double _arcWidthScale; |
| 95 | 94 |
|
| 96 | 95 |
double _nodeScale; |
| 97 | 96 |
double _xBorder, _yBorder; |
| 98 | 97 |
double _scale; |
| 99 | 98 |
double _nodeBorderQuotient; |
| 100 | 99 |
|
| 101 | 100 |
bool _drawArrows; |
| 102 | 101 |
double _arrowLength, _arrowWidth; |
| 103 | 102 |
|
| 104 | 103 |
bool _showNodes, _showArcs; |
| 105 | 104 |
|
| 106 | 105 |
bool _enableParallel; |
| 107 | 106 |
double _parArcDist; |
| 108 | 107 |
|
| 109 | 108 |
bool _showNodeText; |
| 110 | 109 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
| 111 | 110 |
double _nodeTextSize; |
| 112 | 111 |
|
| 113 | 112 |
bool _showNodePsText; |
| 114 | 113 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
| 115 | 114 |
char *_nodePsTextsPreamble; |
| 116 | 115 |
|
| 117 | 116 |
bool _undirected; |
| 118 | 117 |
|
| 119 | 118 |
bool _pleaseRemoveOsStream; |
| 120 | 119 |
|
| 121 | 120 |
bool _scaleToA4; |
| 122 | 121 |
|
| 123 | 122 |
std::string _title; |
| 124 | 123 |
std::string _copyright; |
| 125 | 124 |
|
| 126 | 125 |
enum NodeTextColorType |
| 127 | 126 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
|
| 128 | 127 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
| 129 | 128 |
|
| 130 | 129 |
bool _autoNodeScale; |
| ... | ... |
@@ -624,199 +623,216 @@ |
| 624 | 623 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
|
| 625 | 624 |
|
| 626 | 625 |
///Sets the color of the node texts to be different from the node color |
| 627 | 626 |
|
| 628 | 627 |
///Sets the color of the node texts to be as different from the node color |
| 629 | 628 |
///as it is possible |
| 630 | 629 |
/// |
| 631 | 630 |
GraphToEps<T> &distantColorNodeTexts() |
| 632 | 631 |
{_nodeTextColorType=DIST_COL;return *this;}
|
| 633 | 632 |
///Sets the color of the node texts to be black or white and always visible. |
| 634 | 633 |
|
| 635 | 634 |
///Sets the color of the node texts to be black or white according to |
| 636 | 635 |
///which is more |
| 637 | 636 |
///different from the node color |
| 638 | 637 |
/// |
| 639 | 638 |
GraphToEps<T> &distantBWNodeTexts() |
| 640 | 639 |
{_nodeTextColorType=DIST_BW;return *this;}
|
| 641 | 640 |
|
| 642 | 641 |
///Gives a preamble block for node Postscript block. |
| 643 | 642 |
|
| 644 | 643 |
///Gives a preamble block for node Postscript block. |
| 645 | 644 |
/// |
| 646 | 645 |
///\sa nodePsTexts() |
| 647 | 646 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) {
|
| 648 | 647 |
_nodePsTextsPreamble=str ;return *this; |
| 649 | 648 |
} |
| 650 | 649 |
///Sets whether the the graph is undirected |
| 651 | 650 |
|
| 652 | 651 |
///Sets whether the the graph is undirected. |
| 653 | 652 |
/// |
| 654 | 653 |
///This setting is the default for undirected graphs. |
| 655 | 654 |
/// |
| 656 | 655 |
///\sa directed() |
| 657 | 656 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
|
| 658 | 657 |
|
| 659 | 658 |
///Sets whether the the graph is directed |
| 660 | 659 |
|
| 661 | 660 |
///Sets whether the the graph is directed. |
| 662 | 661 |
///Use it to show the edges as a pair of directed ones. |
| 663 | 662 |
/// |
| 664 | 663 |
///This setting is the default for digraphs. |
| 665 | 664 |
/// |
| 666 | 665 |
///\sa undirected() |
| 667 | 666 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
|
| 668 | 667 |
|
| 669 | 668 |
///Sets the title. |
| 670 | 669 |
|
| 671 | 670 |
///Sets the title of the generated image, |
| 672 | 671 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
| 673 | 672 |
///the EPS file. |
| 674 | 673 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
|
| 675 | 674 |
///Sets the copyright statement. |
| 676 | 675 |
|
| 677 | 676 |
///Sets the copyright statement of the generated image, |
| 678 | 677 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
| 679 | 678 |
///the EPS file. |
| 680 | 679 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
|
| 681 | 680 |
|
| 682 | 681 |
protected: |
| 683 | 682 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
| 684 | 683 |
{
|
| 685 | 684 |
switch(t) {
|
| 686 | 685 |
case CIRCLE: |
| 687 | 686 |
case MALE: |
| 688 | 687 |
case FEMALE: |
| 689 | 688 |
return p.normSquare()<=r*r; |
| 690 | 689 |
case SQUARE: |
| 691 | 690 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
| 692 | 691 |
case DIAMOND: |
| 693 | 692 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
| 694 | 693 |
} |
| 695 | 694 |
return false; |
| 696 | 695 |
} |
| 697 | 696 |
|
| 698 | 697 |
public: |
| 699 | 698 |
~GraphToEps() { }
|
| 700 | 699 |
|
| 701 | 700 |
///Draws the graph. |
| 702 | 701 |
|
| 703 | 702 |
///Like other functions using |
| 704 | 703 |
///\ref named-templ-func-param "named template parameters", |
| 705 | 704 |
///this function calls the algorithm itself, i.e. in this case |
| 706 | 705 |
///it draws the graph. |
| 707 | 706 |
void run() {
|
| 708 | 707 |
//\todo better 'epsilon' would be nice here. |
| 709 | 708 |
const double EPSILON=1e-9; |
| 710 | 709 |
if(dontPrint) return; |
| 711 | 710 |
|
| 712 | 711 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
| 713 | 712 |
mycoords(_coords,_negY); |
| 714 | 713 |
|
| 715 | 714 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
| 716 | 715 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
| 717 | 716 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
| 718 | 717 |
// << "%%Copyright: XXXX\n" |
| 719 | 718 |
os << "%%Creator: LEMON, graphToEps()\n"; |
| 720 |
|
|
| 721 |
{
|
|
| 722 |
|
|
| 719 |
|
|
| 720 |
{
|
|
| 721 |
#ifndef WIN32 |
|
| 723 | 722 |
timeval tv; |
| 724 | 723 |
gettimeofday(&tv, 0); |
| 724 |
|
|
| 725 |
char cbuf[26]; |
|
| 725 | 726 |
ctime_r(&tv.tv_sec,cbuf); |
| 726 | 727 |
os << "%%CreationDate: " << cbuf; |
| 728 |
#else |
|
| 729 |
SYSTEMTIME time; |
|
| 730 |
char buf1[11], buf2[9], buf3[5]; |
|
| 731 |
|
|
| 732 |
GetSystemTime(&time); |
|
| 733 |
if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
|
| 734 |
"ddd MMM dd", buf1, 11) && |
|
| 735 |
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time, |
|
| 736 |
"HH':'mm':'ss", buf2, 9) && |
|
| 737 |
GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
|
| 738 |
"yyyy", buf3, 5)) {
|
|
| 739 |
os << "%%CreationDate: " << buf1 << ' ' |
|
| 740 |
<< buf2 << ' ' << buf3 << std::endl; |
|
| 741 |
} |
|
| 742 |
#endif |
|
| 727 | 743 |
} |
| 728 | 744 |
|
| 729 | 745 |
if (_autoArcWidthScale) {
|
| 730 | 746 |
double max_w=0; |
| 731 | 747 |
for(ArcIt e(g);e!=INVALID;++e) |
| 732 | 748 |
max_w=std::max(double(_arcWidths[e]),max_w); |
| 733 | 749 |
///\todo better 'epsilon' would be nice here. |
| 734 | 750 |
if(max_w>EPSILON) {
|
| 735 | 751 |
_arcWidthScale/=max_w; |
| 736 | 752 |
} |
| 737 | 753 |
} |
| 738 | 754 |
|
| 739 | 755 |
if (_autoNodeScale) {
|
| 740 | 756 |
double max_s=0; |
| 741 | 757 |
for(NodeIt n(g);n!=INVALID;++n) |
| 742 | 758 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
| 743 | 759 |
///\todo better 'epsilon' would be nice here. |
| 744 | 760 |
if(max_s>EPSILON) {
|
| 745 | 761 |
_nodeScale/=max_s; |
| 746 | 762 |
} |
| 747 | 763 |
} |
| 748 | 764 |
|
| 749 | 765 |
double diag_len = 1; |
| 750 | 766 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
|
| 751 | 767 |
dim2::BoundingBox<double> bb; |
| 752 | 768 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
| 753 | 769 |
if (bb.empty()) {
|
| 754 | 770 |
bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0)); |
| 755 | 771 |
} |
| 756 | 772 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
| 757 | 773 |
if(diag_len<EPSILON) diag_len = 1; |
| 758 | 774 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
| 759 | 775 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
| 760 | 776 |
} |
| 761 | 777 |
|
| 762 | 778 |
dim2::BoundingBox<double> bb; |
| 763 | 779 |
for(NodeIt n(g);n!=INVALID;++n) {
|
| 764 | 780 |
double ns=_nodeSizes[n]*_nodeScale; |
| 765 | 781 |
dim2::Point<double> p(ns,ns); |
| 766 | 782 |
switch(_nodeShapes[n]) {
|
| 767 | 783 |
case CIRCLE: |
| 768 | 784 |
case SQUARE: |
| 769 | 785 |
case DIAMOND: |
| 770 | 786 |
bb.add(p+mycoords[n]); |
| 771 | 787 |
bb.add(-p+mycoords[n]); |
| 772 | 788 |
break; |
| 773 | 789 |
case MALE: |
| 774 | 790 |
bb.add(-p+mycoords[n]); |
| 775 | 791 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
| 776 | 792 |
break; |
| 777 | 793 |
case FEMALE: |
| 778 | 794 |
bb.add(p+mycoords[n]); |
| 779 | 795 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
| 780 | 796 |
break; |
| 781 | 797 |
} |
| 782 | 798 |
} |
| 783 | 799 |
if (bb.empty()) {
|
| 784 | 800 |
bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0)); |
| 785 | 801 |
} |
| 786 | 802 |
|
| 787 | 803 |
if(_scaleToA4) |
| 788 | 804 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
| 789 | 805 |
else {
|
| 790 | 806 |
if(_preScale) {
|
| 791 | 807 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
| 792 | 808 |
while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
| 793 | 809 |
while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
| 794 | 810 |
} |
| 795 | 811 |
|
| 796 | 812 |
os << "%%BoundingBox: " |
| 797 | 813 |
<< int(floor(bb.left() * _scale - _xBorder)) << ' ' |
| 798 | 814 |
<< int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
| 799 | 815 |
<< int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
| 800 | 816 |
<< int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
| 801 | 817 |
} |
| 802 | 818 |
|
| 803 | 819 |
os << "%%EndComments\n"; |
| 804 | 820 |
|
| 805 | 821 |
//x1 y1 x2 y2 x3 y3 cr cg cb w |
| 806 | 822 |
os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
|
| 807 | 823 |
<< " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
| 808 | 824 |
os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
|
| 809 | 825 |
//x y r |
| 810 | 826 |
os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
|
| 811 | 827 |
//x y r |
| 812 | 828 |
os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
|
| 813 | 829 |
<< " 2 index 1 index sub 2 index 2 index add lineto\n" |
| 814 | 830 |
<< " 2 index 1 index sub 2 index 2 index sub lineto\n" |
| 815 | 831 |
<< " 2 index 1 index add 2 index 2 index sub lineto\n" |
| 816 | 832 |
<< " closepath pop pop pop} bind def\n"; |
| 817 | 833 |
//x y r |
| 818 | 834 |
os << "/di { newpath 2 index 1 index add 2 index moveto\n"
|
| 819 | 835 |
<< " 2 index 2 index 2 index add lineto\n" |
| 820 | 836 |
<< " 2 index 1 index sub 2 index lineto\n" |
| 821 | 837 |
<< " 2 index 2 index 2 index sub lineto\n" |
| 822 | 838 |
<< " closepath pop pop pop} bind def\n"; |
0 comments (0 inline)