lemon/eps.cc
author deba
Tue, 10 Jun 2008 11:36:17 +0000
changeset 2612 3d65053d01a3
parent 2391 14a343be7a5a
permissions -rw-r--r--
Bug fix initialization
The std::numeric_limits<double>::min() means the smallest positive number,
and not the smallest number in the whole range of double.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <lemon/eps.h>
    20 
    21 namespace lemon {
    22   
    23   void EpsDrawer::defMacros()
    24   {
    25     out << "/clmode true def\n" <<
    26       "/cshowmode false def\n" <<
    27       "/defont (Helvetica) findfont def\n" <<
    28       "/fosi 12 def\n" <<
    29       "\n" <<
    30       "/st { clmode { currentpoint stroke newpath moveto } if } bind def\n" <<
    31       "/str { currentpoint stroke newpath moveto /clmode true def } bind def\n"
    32 	<<
    33       "/fl { currentpoint fill newpath moveto /clmode true def } bind def\n" <<
    34       "/eofl { currentpoint eofill newpath moveto /clmode true def } bind def\n"
    35 	<<
    36       "/cl { currentpoint clip newpath moveto /clmode true def } bind def\n"
    37 	<<
    38       "/eocl { currentpoint eoclip newpath moveto /clmode true def } bind def\n"
    39 	<<
    40       "\n" <<
    41       "/l { moveto lineto st } bind def\n" <<
    42       "/lt { lineto st } bind def\n" <<
    43       "/mt { moveto } bind def\n" <<
    44       "/c { dup 3 index add 2 index moveto 0 360 arc st } bind def\n" <<
    45       "/collect { /clmode false def currentpoint newpath moveto } bind def\n" <<
    46       "\n" <<
    47       "/fontset { defont fosi scalefont setfont } bind def\n" <<
    48       "/stfs { /fosi exch def fontset } bind def\n" <<
    49       "/cshow { dup stringwidth pop\n" <<
    50       "   neg 2 div 0 rmoveto show } bind def\n" <<
    51       "/xshow { cshowmode { cshow } { show } ifelse } def\n" <<
    52       "\n" <<
    53       "fontset\n" <<
    54       "newpath\n" <<
    55       "0 0 moveto\n" <<
    56       "1 setlinecap\n";
    57   }
    58 
    59   void EpsDrawer::init(int x1,int y1,int x2,int y2)
    60   {
    61     out << "%!PS-Adobe-2.0 EPSF-2.0\n" <<
    62       "%%BoundingBox: " << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 <<
    63       "\n%%EndComments\n";
    64     defMacros();
    65   }
    66 
    67   void EpsDrawer::init(double x1,double y1,double x2,double y2)
    68   {
    69     out << "%!PS-Adobe-2.0\n" <<
    70       "%%HiResBoundingBox: " << 
    71       x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 <<
    72       "\n%%EndComments\n";
    73     defMacros();
    74   }
    75 
    76 
    77   EpsDrawer::EpsDrawer(std::ostream &os,int x,int y) : local_stream(false),
    78 						       out(os)
    79   {
    80     init(0,0,x,y);
    81   }
    82 
    83   EpsDrawer::EpsDrawer(std::ostream &os,int x1,int y1,int x2,int y2) : 
    84     local_stream(false),
    85     out(os)
    86   {
    87     init(x1,y1,x2,y2);
    88   }
    89 
    90   EpsDrawer::EpsDrawer(std::ostream &os,dim2::Point<double> s) : local_stream(false),
    91 							out(os)
    92   {
    93     init(0.0,0.0,s.x,s.y);
    94   }
    95 
    96   EpsDrawer::EpsDrawer(std::ostream &os,dim2::Point<double> a, dim2::Point<double> b) :
    97     local_stream(false),
    98     out(os)
    99   {
   100     init(a.x,a.y,b.x,b.y);
   101   }
   102 
   103 
   104   EpsDrawer::EpsDrawer(const std::string &name,int x,int y) :
   105     local_stream(true),
   106     out(*new std::ofstream(name.c_str()))
   107   {
   108     init(0,0,x,y);
   109   }
   110 
   111   EpsDrawer::EpsDrawer(const std::string &name,int x1,int y1,int x2,int y2) : 
   112     local_stream(true),
   113     out(*new std::ofstream(name.c_str()))
   114   {
   115     init(x1,y1,x2,y2);
   116   }
   117   
   118   EpsDrawer::EpsDrawer(const std::string &name,dim2::Point<double> s) :
   119     local_stream(true),
   120     out(*new std::ofstream(name.c_str()))
   121   {
   122     init(0.0,0.0,s.x,s.y);
   123   }
   124 
   125   EpsDrawer::EpsDrawer(const std::string &name,dim2::Point<double> a, dim2::Point<double> b) :
   126     local_stream(true),
   127     out(*new std::ofstream(name.c_str()))
   128   {
   129     init(a.x,a.y,b.x,b.y);
   130   }
   131 
   132 
   133   EpsDrawer::~EpsDrawer()
   134   {
   135     out << "showpage\n";
   136     if(local_stream) delete &out;
   137   }
   138 
   139   EpsDrawer &EpsDrawer::save()
   140   {
   141     out << "gsave\n";
   142     return *this;  
   143   }
   144 
   145   EpsDrawer &EpsDrawer::restore()
   146   {
   147     out << "grestore\n";
   148     return *this;  
   149   }
   150  
   151   EpsDrawer &EpsDrawer::line(double x1,double y1,double x2,double y2)
   152   {
   153     out << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << " l\n";
   154     return *this;
   155   
   156   }
   157 
   158   EpsDrawer &EpsDrawer::lineTo(double x1,double y1)
   159   {
   160     out << x1 << ' ' << y1 << " lt\n";
   161     return *this;
   162   
   163   }
   164 
   165   EpsDrawer &EpsDrawer::moveTo(double x1,double y1)
   166   {
   167     out << x1 << ' ' << y1 << " mt\n";
   168     return *this;  
   169   }
   170 
   171   EpsDrawer &EpsDrawer::circle(double x,double y, double r)
   172   {
   173     out << x << ' ' << y << ' ' << r << " c\n";
   174     return *this;  
   175   }
   176 
   177   EpsDrawer &EpsDrawer::operator<<(const std::string &s)
   178   {
   179     out << "(" << s <<") xshow\n";
   180     return *this;
   181   }
   182 
   183   EpsDrawer &EpsDrawer::operator<<(const char *s)
   184   {
   185     out << "(" << s <<") xshow\n";
   186     return *this;
   187   }
   188 
   189   EpsDrawer &EpsDrawer::operator<<(int i)
   190   {
   191     out << "(" << i <<") xshow\n";
   192     return *this;
   193   }
   194 
   195   EpsDrawer &EpsDrawer::operator<<(double d)
   196   {
   197     out << "(" << d <<") xshow\n";
   198     return *this;
   199   }
   200 
   201   EpsDrawer &EpsDrawer::fontSize(double si)
   202   {
   203     out << si << " stfs\n";
   204     return *this;
   205   }
   206   EpsDrawer &EpsDrawer::font(std::string s)
   207   {
   208     out << "/defont ("<<s<<") findfont def fontset\n";
   209     return *this;
   210   }
   211 
   212 
   213   EpsDrawer &EpsDrawer::collect()
   214   {
   215     out << "collect\n";
   216     return *this;  
   217   }
   218 
   219   EpsDrawer &EpsDrawer::closePath()
   220   {
   221     out << "closepath\n";
   222     return *this;
   223   }
   224 
   225   EpsDrawer &EpsDrawer::stroke()
   226   {
   227     out << "str\n";
   228     return *this;  
   229   }
   230   EpsDrawer &EpsDrawer::fill()
   231   {
   232     out << "fl\n";
   233     return *this;  
   234   }
   235   EpsDrawer &EpsDrawer::eoFill()
   236   {
   237     out << "eofl\n";
   238     return *this;  
   239   }
   240   EpsDrawer &EpsDrawer::clip()
   241   {
   242     out << "cl\n";
   243     return *this;  
   244   }
   245   EpsDrawer &EpsDrawer::eoClip()
   246   {
   247     out << "eocl\n";
   248     return *this;  
   249   }
   250 
   251   EpsDrawer &EpsDrawer::lineWidth(double w)
   252   {
   253     out << w << " setlinewidth\n";
   254     return *this;  
   255   }
   256 
   257   EpsDrawer &EpsDrawer::lineCap(int i)
   258   {
   259     out << i << " setlinecap\n";
   260     return *this;  
   261   }
   262 
   263   EpsDrawer &EpsDrawer::lineJoin(int i)
   264   {
   265     out << i << " setlinejoin\n";
   266     return *this;  
   267   }
   268 
   269   EpsDrawer &EpsDrawer::miterLimit(double w)
   270   {
   271     out << w << " setmiterlimit\n";
   272     return *this;  
   273   }
   274 
   275   EpsDrawer &EpsDrawer::color(double r, double g, double b)
   276   {
   277     out << r << ' ' << g << ' ' << b << " setrgbcolor\n";
   278     return *this;  
   279   }
   280 
   281   EpsDrawer &EpsDrawer::translate(double x,double y)
   282   {
   283     out << x << ' ' << y << " translate\n";
   284     return *this;  
   285   }
   286 
   287   EpsDrawer &EpsDrawer::rotate(double r)
   288   {
   289     out << r << " rotate\n";
   290     return *this;  
   291   }
   292   EpsDrawer &EpsDrawer::scale(double sx, double sy)
   293   {
   294     out << sx << ' ' << sy << " scale\n";
   295     return *this;  
   296   }
   297   
   298   EpsDrawer &EpsDrawer::clear()
   299   {
   300     out << "erasepage\n";
   301     return *this;  
   302   }
   303   
   304   EpsDrawer &EpsDrawer::centerMode(bool m)
   305   {
   306     if(m) out << "/cshowmode true def\n";
   307     else out << "/cshowmode false def\n";
   308 
   309     return *this;  
   310   }
   311   
   312   EpsDrawer &EpsDrawer::flush()
   313   {
   314     out << "flush\n";
   315     //  fflush(fp);
   316     return *this;
   317   }
   318 
   319   EpsDrawer &EpsDrawer::node(NodeShapes t, double x, double y, double r,
   320 			     Color col, Color brd)
   321   {
   322     out << "gsave\n"
   323 	<< brd.red() << ' ' << brd.green() << ' ' << brd.blue() 
   324 	<< " setrgbcolor\n";
   325     switch(t) {
   326     case CIRCLE:
   327       out << "newpath " << x << ' ' << y << ' ' << r 
   328 	  << " dup 3 index add 2 index moveto 0 360 arc fill\n";
   329       break;
   330     case SQUARE:
   331       out << "newpath\n"
   332 	  << x-r << ' ' << y-r << " moveto\n"
   333 	  << x-r << ' ' << y+r << " lineto\n"
   334 	  << x+r << ' ' << y+r << " lineto\n"
   335 	  << x+r << ' ' << y-r << " lineto closepath fill\n";
   336       break;
   337     case DIAMOND:
   338       out << "newpath\n"
   339 	  << x-r << ' ' << y   << " moveto\n"
   340 	  << x   << ' ' << y+r << " lineto\n"
   341 	  << x+r << ' ' << y   << " lineto\n"
   342 	  << x   << ' ' << y-r << " lineto closepath fill\n";
   343       break;
   344     case MALE:
   345       break;
   346     case FEMALE:
   347       break;
   348     }
   349     r/=1.1;
   350     out << col.red() << ' ' << col.green() << ' ' << col.blue() 
   351 	<< " setrgbcolor\n";
   352     switch(t) {
   353     case CIRCLE:
   354       out << "newpath " << x << ' ' << y << ' ' << r 
   355 	  << " dup 3 index add 2 index moveto 0 360 arc fill\n";
   356       break;
   357     case SQUARE:
   358       out << "newpath\n"
   359 	  << x-r << ' ' << y-r << " moveto\n"
   360 	  << x-r << ' ' << y+r << " lineto\n"
   361 	  << x+r << ' ' << y+r << " lineto\n"
   362 	  << x+r << ' ' << y-r << " lineto closepath fill\n";
   363       break;
   364     case DIAMOND:
   365       out << "newpath\n"
   366 	  << x-r << ' ' << y   << " moveto\n"
   367 	  << x   << ' ' << y+r << " lineto\n"
   368 	  << x+r << ' ' << y   << " lineto\n"
   369 	  << x   << ' ' << y-r << " lineto closepath fill\n";
   370       break;
   371     case MALE:
   372       break;
   373     case FEMALE:
   374       break;
   375     }
   376 
   377     out << "grestore\n";
   378     return *this;
   379   }
   380   
   381 }