# HG changeset patch # User alpar # Date 1140417672 0 # Node ID 9a59a6cacfd9d517a7da507944484d3bbcb75e87 # Parent bd88ea06ab69d39c18a4bfe3c99ae5f1f2c21460 - RGB color related stuff is in color.h now - eps.h: A simple class to create .eps figures (demo: eps_demo.h) diff -r bd88ea06ab69 -r 9a59a6cacfd9 demo/Makefile.am --- a/demo/Makefile.am Mon Feb 20 06:38:18 2006 +0000 +++ b/demo/Makefile.am Mon Feb 20 06:41:12 2006 +0000 @@ -8,6 +8,7 @@ dijkstra_demo \ reader_writer_demo \ dim_to_lgf \ + eps_demo \ graph_to_eps_demo \ graph_orientation \ min_route \ @@ -32,6 +33,8 @@ dijkstra_demo_SOURCES = dijkstra_demo.cc +eps_demo_SOURCES = eps_demo.cc + reader_writer_demo_SOURCES = reader_writer_demo.cc dim_to_lgf_SOURCES = dim_to_lgf.cc diff -r bd88ea06ab69 -r 9a59a6cacfd9 demo/eps_demo.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/eps_demo.cc Mon Feb 20 06:41:12 2006 +0000 @@ -0,0 +1,110 @@ +/* -*- C++ -*- + * + * This file is a part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2003-2006 + * 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 EPS grawing class \ref EpsDrawer() +/// +/// This demo program shows examples how to use the class \ref +/// EpsDrawer(). It takes no input but simply creates a file +/// eps_demo.eps demonstrating the capability of \ref +/// EpsDrawer(). +/// +/// \include eps_demo.cc + +#include +#include + +using namespace lemon; + +void kosar(EpsDrawer &ed) +{ + double d,r; + + r = sqrt(2); + + ed.save(); + + ed.translate(256,256).scale(256,256); + ed.lineWidth(1/256); + + ed.collect(); + + ed.moveTo(0,1); + + for(d=0;d + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + + +///\ingroup misc +///\file +///\brief Tools to manage RGB colors. +/// +///\author Alpar Juttner + +namespace lemon { + +///Data structure representing RGB colors. + +///Data structure representing RGB colors. +///\ingroup misc +class Color +{ + double _r,_g,_b; +public: + ///Default constructor + Color() {} + ///Constructor + Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; + ///Returns the red component + double & red() {return _r;} + ///Returns the red component + const double & red() const {return _r;} + ///Returns the green component + double & green() {return _g;} + ///Returns the green component + const double & green() const {return _g;} + ///Returns the blue component + double & blue() {return _b;} + ///Returns 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; }; +}; + +///Maps 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 concept::ReferenceMap "reference map", so you can also +///change the actual colors. + +class ColorSet : public MapBase +{ + std::vector colors; +public: + ///Constructor + + ///Constructor + ///\param have_white indicates whether white is + ///amongst the provided color (\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 0 + ///the default color configuration is set up (26 color plus the while). + ///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.) + ColorSet(bool have_white=false,int num=0) + { + 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; + } + ///Sets the number of the exiting colors. + void resize(int s) { colors.resize(s);} + ///Returns the number of the existing colors. + std::size_t size() const { return colors.size();} +}; + +///Returns a visible 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){ + double v=(.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5?1:0; + return Color(v,v,v); +} + +} //END OF NAMESPACE LEMON + +#endif // LEMON_COLOR_H diff -r bd88ea06ab69 -r 9a59a6cacfd9 lemon/eps.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/eps.cc Mon Feb 20 06:41:12 2006 +0000 @@ -0,0 +1,301 @@ +#include + +namespace lemon { + + void EpsDrawer::defMacros() + { + out << "/clmode true def\n" << + "/cshowmode false def\n" << + "/defont (Helvetica) findfont def\n" << + "/fosi 12 def\n" << + "\n" << + "/st { clmode { currentpoint stroke newpath moveto } if } bind def\n" << + "/str { currentpoint stroke newpath moveto /clmode true def } bind def\n" + << + "/fl { currentpoint fill newpath moveto /clmode true def } bind def\n" << + "/eofl { currentpoint eofill newpath moveto /clmode true def } bind def\n" + << + "/cl { currentpoint clip newpath moveto /clmode true def } bind def\n" + << + "/eocl { currentpoint eoclip newpath moveto /clmode true def } bind def\n" + << + "\n" << + "/l { moveto lineto st } bind def\n" << + "/lt { lineto st } bind def\n" << + "/mt { moveto } bind def\n" << + "/c { dup 3 index add 2 index moveto 0 360 arc st } bind def\n" << + "/collect { /clmode false def currentpoint newpath moveto } bind def\n" << + "\n" << + "/fontset { defont fosi scalefont setfont } bind def\n" << + "/stfs { /fosi exch def fontset } bind def\n" << + "/cshow { dup stringwidth pop\n" << + " neg 2 div 0 rmoveto show } bind def\n" << + "/xshow { cshowmode { cshow } { show } ifelse } def\n" << + "\n" << + "fontset\n" << + "newpath\n" << + "0 0 moveto\n" << + "1 setlinecap\n"; + } + + void EpsDrawer::init(int x1,int y1,int x2,int y2) + { + out << "%!PS-Adobe-2.0 EPSF-2.0\n" << + "%%BoundingBox: " << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << + "\n%%EndComments\n"; + defMacros(); + } + + void EpsDrawer::init(double x1,double y1,double x2,double y2) + { + out << "%!PS-Adobe-2.0\n" << + "%%HiResBoundingBox: " << + x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << + "\n%%EndComments\n"; + defMacros(); + } + + + EpsDrawer::EpsDrawer(std::ostream &os,int x,int y) : local_stream(false), + out(os) + { + init(0,0,x,y); + } + + EpsDrawer::EpsDrawer(std::ostream &os,int x1,int y1,int x2,int y2) : + local_stream(false), + out(os) + { + init(x1,y1,x2,y2); + } + + EpsDrawer::EpsDrawer(std::ostream &os,xy s) : local_stream(false), + out(os) + { + init(0.0,0.0,s.x,s.y); + } + + EpsDrawer::EpsDrawer(std::ostream &os,xy a, xy b) : + local_stream(false), + out(os) + { + init(a.x,a.y,b.x,b.y); + } + + + EpsDrawer::EpsDrawer(const std::string &name,int x,int y) : + local_stream(true), + out(*new std::ofstream(name.c_str())) + { + init(0,0,x,y); + } + + EpsDrawer::EpsDrawer(const std::string &name,int x1,int y1,int x2,int y2) : + local_stream(true), + out(*new std::ofstream(name.c_str())) + { + init(x1,y1,x2,y2); + } + + EpsDrawer::EpsDrawer(const std::string &name,xy s) : + local_stream(true), + out(*new std::ofstream(name.c_str())) + { + init(0.0,0.0,s.x,s.y); + } + + EpsDrawer::EpsDrawer(const std::string &name,xy a, xy b) : + local_stream(true), + out(*new std::ofstream(name.c_str())) + { + init(a.x,a.y,b.x,b.y); + } + + + EpsDrawer::~EpsDrawer() + { + out << "showpage\n"; + if(local_stream) delete &out; + } + + EpsDrawer &EpsDrawer::save() + { + out << "gsave\n"; + return *this; + } + + EpsDrawer &EpsDrawer::restore() + { + out << "grestore\n"; + return *this; + } + + EpsDrawer &EpsDrawer::line(double x1,double y1,double x2,double y2) + { + out << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << " l\n"; + return *this; + + } + + EpsDrawer &EpsDrawer::lineTo(double x1,double y1) + { + out << x1 << ' ' << y1 << " lt\n"; + return *this; + + } + + EpsDrawer &EpsDrawer::moveTo(double x1,double y1) + { + out << x1 << ' ' << y1 << " mt\n"; + return *this; + } + + EpsDrawer &EpsDrawer::circle(double x,double y, double r) + { + out << x << ' ' << y << ' ' << r << " c\n"; + return *this; + } + + EpsDrawer &EpsDrawer::operator<<(const std::string &s) + { + out << "(" << s <<") xshow\n"; + return *this; + } + + EpsDrawer &EpsDrawer::operator<<(const char *s) + { + out << "(" << s <<") xshow\n"; + return *this; + } + + EpsDrawer &EpsDrawer::operator<<(int i) + { + out << "(" << i <<") xshow\n"; + return *this; + } + + EpsDrawer &EpsDrawer::operator<<(double d) + { + out << "(" << d <<") xshow\n"; + return *this; + } + + EpsDrawer &EpsDrawer::fontSize(double si) + { + out << si << " stfs\n"; + return *this; + } + EpsDrawer &EpsDrawer::font(std::string s) + { + out << "/defont ("< +#include +#include +#include +#include +#include + + ///\ingroup io_group + ///\file + ///\brief Simple tool to create \c .eps files + /// + ///\author Alpar Juttner + +namespace lemon { + + ///\ingroup io_group + ///\brief A simple tool to create \c .eps files + /// + ///A simple tool to create \c .eps files + ///\author Alpar Juttner + class EpsDrawer + { + void defMacros(); + void init(int x1,int y1,int x2,int y2); + void init(double x1,double y1,double x2,double y2); + bool local_stream; + public: + + std::ostream &out; + + ///\e + + ///The generated file is put to \c os. + /// + /// \c x and \c y determine the upper + ///right corner of the bounding box. The lower left corner is (0,0). + EpsDrawer(std::ostream &os,int x,int y); + ///\e + + ///The generated file is put to \c os. + /// + ///(x1,y1) and (x2,y2) + /// determine the lower left and the upper right corners of + ///the bounding box, respectively. + EpsDrawer(std::ostream &os,int x1,int y1,int x2,int y2); + ///\e + + ///The generated file is put to \c os. + /// + ///\c s determines the upper + ///right corner of the bounding box. The lower left corner is (0,0). + EpsDrawer(std::ostream &os,xy s); + ///\e + + ///The generated file is put to \c os. + /// + ///\c a and \c b + /// determine the lower left and the upper right corners of + ///the bounding box, respectively. + EpsDrawer(std::ostream &os,xy a, xy b); + ///\e + + ///The generated picture is put to the file \c name. + /// + ///\c x and \c y determine the upper + ///right corner of the bounding box. The lower left corner is (0,0). + EpsDrawer(const std::string &name,int x,int y); + ///\e + + ///The generated picture is put to the file \c name. + /// + ///(x1,y1) and (x2,y2) + /// determine the lower left and the upper right corners of + ///the bounding box, respectively. + EpsDrawer(const std::string &name,int x1,int y1,int x2,int y2); + ///\e + + ///The generated picture is put to the file \c name. + /// + ///\c s determines the upper + ///right corner of the bounding box. The lower left corner is (0,0). + EpsDrawer(const std::string &name,xy s); + ///\e + + ///The generated picture is put to the file \c name. + /// + ///\c a and \c b + /// determine the lower left and the upper right corners of + ///the bounding box, respectively. + EpsDrawer(const std::string &name,xy a, xy b); + +// template EpsDrawer(std::ostream &os,BoundingBox b) +// template EpsDrawer(std::ostream &os,BoundingBox b); + + ~EpsDrawer(); + + ///Save the current graphic state. + + ///This function saves the current coordinate system, and the parameters + ///set by \ref color(), \ref lineWidth() etc. + ///The can be \ref restore "restore()"d later. + /// + ///The \ref save() - \ref restore() pairs can be nested. + /// + EpsDrawer &save(); + ///Restore the saves graphic state. + + EpsDrawer &restore(); + + ///Draw a line + EpsDrawer &line(double x1,double y1,double x2,double y2); + ///Draw a line from the current point + EpsDrawer &lineTo(double x1,double y1); + ///Move the current point + EpsDrawer &moveTo(double x1,double y1); + ///Draw a circle + EpsDrawer &circle(double x,double y, double r); + + ///Draw a line + template EpsDrawer &line(xy p1,xy p2) + { + return line(p1.x,p1.y,p2.x,p2.y); + } + ///Draw a line from the current point + template EpsDrawer &lineTo(xy p) + { + return lineTo(p.x,p.y); + } + ///Move the current point + template EpsDrawer &moveTo(xy p) + { + return moveTo(p.x,p.y); + } + ///Draw a circle + template EpsDrawer &circle(xy p, double r) + { + return circle(p.x,p.y,r); + } + + ///Set the font size + EpsDrawer &fontSize(double si); + ///Set the fint type + EpsDrawer &font(std::string ); + ///Sets whether text output is centerized of not + + ///Sets whether text output is centerized of not. + /// + ///\warning \ref save() doesn't save this setting. + /// + EpsDrawer ¢erMode(bool m); + ///Turn to collect mode. + + ///If you call this function, then the drawing operations like \ref line(), + ///\ref lineTo(), \ref moveTo() will not take place immediately, but istead + ///they + ///are collected. These operations form a \e path. + ///Then you can \ref stroke(), \ref fill(), \ref eofill(), \ref clip() or + ///\ref eoclip() it. + ///When drawing, you can also use \ref closePath() to - surprise - close the + ///current path. + /// + ///This example draws a red filled diamond. + ///\code + /// EpsDraw ed("diamond.eps",-1,-1,1,1); + /// ed.color(1,0,0,).collect().line(0,-1,1,0).lineTo(0,1) + /// .lineTo(-1,0).closePath().fill(); + ///\endcode + EpsDrawer &collect(); + ///Close the current drawing path + EpsDrawer &closePath(); + ///Stroke (draw) a path + + ///Stroke (draw) a path. + ///\sa collect + /// + EpsDrawer &stroke(); + ///Fill a path + + ///Fill a path. + ///\sa collect + /// + EpsDrawer &fill(); + ///Even-odd fill a path + + ///Even-odd fill a path. + ///\sa collect + /// + EpsDrawer &eoFill(); + ///Set a clipping area. + + ///This function sets a clipping area. After that, the drawing operations + ///will affect only this area. + ///\sa collect + /// + EpsDrawer &clip(); + ///Set a clipping area using even-odd rule. + + ///This function sets a clipping area using even-odd rule. + ///After that, the drawing operations + ///will affect only this area. + ///\sa collect + /// + EpsDrawer &eoClip(); + + ///Set the line width. + EpsDrawer &lineWidth(double w); + ///Set the style of the line ends + + ///\param i It can be 0, 1 or 2 + /// + EpsDrawer &lineCap(int i); + ///Set the style of the line joins + + ///\param i It can be 0, 1 or 2 + /// + EpsDrawer &lineJoin(int i); + ///Set the cut length of near parallel joining lines. + EpsDrawer &miterLimit(double w); + + ///Set the drawing color + EpsDrawer &color(double r, double g, double b); + ///Set the drawing color + EpsDrawer &color(Color c) + { + return color(c.red(),c.green(),c.blue()); + } + + ///Translate the coordinate system + EpsDrawer &translate(double x,double y); + ///Translate the coordinate system + template EpsDrawer &translate(xy p) + { + return translate(p.x,p.y); + } + ///Rotate the coordinate system + EpsDrawer &rotate(double r); + ///Scale the coordinate system + EpsDrawer &scale(double sx, double sy); + ///Scale the coordinate system + EpsDrawer &scale(double s) { return scale(s,s); } + ///Scale the coordinate system + template EpsDrawer &scale(xy p) + { + return scale(p.x,p.y); + } + + ///\e + EpsDrawer &flush(); + ///Clear the image + EpsDrawer &clear(); + + ///Print a text at the current point + EpsDrawer &operator<<(const std::string &s); + ///Print a text at the current point + EpsDrawer &operator<<(const char *s); + ///Print a number at the current point + EpsDrawer &operator<<(int i); + ///Print a number at the current point + EpsDrawer &operator<<(double d); + ///Print a coordinate at the current point + template + EpsDrawer &operator<<(xy p) + { + out << "((" << p.x << ',' << p.y <<")) show\n"; + return *this; + } + + }; + +} + +#endif diff -r bd88ea06ab69 -r 9a59a6cacfd9 lemon/graph_to_eps.h --- a/lemon/graph_to_eps.h Mon Feb 20 06:38:18 2006 +0000 +++ b/lemon/graph_to_eps.h Mon Feb 20 06:41:12 2006 +0000 @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -44,133 +45,6 @@ namespace lemon { -///Data structure representing RGB colors. - -///Data structure representing RGB colors. -///\ingroup misc -class Color -{ - double _r,_g,_b; -public: - ///Default constructor - Color() {} - ///Constructor - Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; - ///Returns the red component - double & red() {return _r;} - ///Returns the red component - const double & red() const {return _r;} - ///Returns the green component - double & green() {return _g;} - ///Returns the green component - const double & green() const {return _g;} - ///Returns the blue component - double & blue() {return _b;} - ///Returns 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; }; -}; - -///Maps 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 concept::ReferenceMap "reference map", so you can also -///change the actual colors. - -class ColorSet : public MapBase -{ - std::vector colors; -public: - ///Constructor - - ///Constructor - ///\param have_white indicates whether white is - ///amongst the provided color (\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 0 - ///the default color configuration is set up (26 color plus the while). - ///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.) - ColorSet(bool have_white=false,int num=0) - { - 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; - } - ///Sets the number of the exiting colors. - void resize(int s) { colors.resize(s);} - ///Returns the number of the existing colors. - std::size_t size() const { return colors.size();} -}; - -///Returns a visible 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){ - double v=(.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5?1:0; - return Color(v,v,v); -} - template class _NegY { public: