Changeset 1178:3c176c65d33b in lemon-0.x
- Timestamp:
- 02/25/05 15:50:22 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1584
- Location:
- src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/demo/graph_to_eps_demo.cc
r1164 r1178 24 24 using namespace lemon; 25 25 26 class ColorSet : public MapBase<int,Color>27 {28 public:29 Color operator[](int i) const30 {31 switch(i%8){32 case 0: return Color(0,0,0);33 case 1: return Color(1,0,0);34 case 2: return Color(0,1,0);35 case 3: return Color(0,0,1);36 case 4: return Color(1,1,0);37 case 5: return Color(1,0,1);38 case 6: return Color(0,1,1);39 case 7: return Color(1,1,1);40 }41 return Color(0,0,0);42 }43 } colorSet;44 45 26 class IdMap :public MapBase<ListGraph::Node,int> 46 27 { … … 53 34 int main() 54 35 { 36 ColorSet colorSet; 37 55 38 ListGraph g; 56 39 typedef ListGraph::Node Node; … … 166 149 run(); 167 150 151 ListGraph h; 152 ListGraph::NodeMap<int> hcolors(h); 153 ListGraph::NodeMap<Xy> hcoords(h); 154 155 int cols=int(sqrt(double(colorSet.size()))); 156 for(int i=0;i<int(colorSet.size());i++) { 157 Node n=h.addNode(); 158 hcoords[n]=Xy(i%cols,i/cols); 159 hcolors[n]=i; 160 } 161 162 graphToEps(h,"graph_to_eps_demo_out_colors.eps").scale(60). 163 title("Sample .eps figure (parallel edges and arrowheads)"). 164 copyright("(C) 2005 LEMON Project"). 165 coords(hcoords). 166 nodeScale(.45). 167 distantColorNodeTexts(). 168 // distantBWNodeTexts(). 169 nodeTexts(hcolors).nodeTextSize(.6). 170 nodeColors(composeMap(colorSet,hcolors)). 171 run(); 172 173 168 174 } -
src/lemon/graph_to_eps.h
r1164 r1178 52 52 Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; 53 53 ///Returns the red component 54 double getR() {return _r;} 54 55 ///\todo \c red() could be a better name... 56 double getR() const {return _r;} 55 57 ///Returns the green component 56 double getG() {return _g;}58 double getG() const {return _g;} 57 59 ///Returns the blue component 58 double getB() {return _b;}60 double getB() const {return _b;} 59 61 ///Set the color components 60 62 void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; 61 63 }; 62 64 65 ///Maps <tt>int</tt>s to different \ref Color "Color"s 66 67 ///This map assing one of the predefined \ref Color "Color"s 68 ///to each <tt>int</tt>. It is possible to change the colors as well as their 69 ///number. The integer range is cyclically mapped to the provided set of colors. 70 /// 71 ///This is a true \ref concept::ReferenceMap "reference map", so you can also 72 ///change the actual colors. 73 74 class ColorSet : public MapBase<int,Color> 75 { 76 std::vector<Color> colors; 77 public: 78 ///Constructor 79 80 ///Constructor 81 ///\param have_white indicates wheter white is 82 ///amongst the provided color (\c true) or not (\c false). If it is true, 83 ///white will be assigned to \c 0. 84 ///\param num the number of the allocated colors. If it is \c 0 85 ///the default color configuration is set up (26 color plus the while). 86 ///If \c num is less then 26/27 then the default color list is cut. Otherwise 87 ///the color list is filled repeatedly with the default color list. 88 ColorSet(bool have_white=false,int num=0) 89 { 90 do { 91 if(have_white) colors.push_back(Color(1,1,1)); 92 93 colors.push_back(Color(0,0,0)); 94 colors.push_back(Color(1,0,0)); 95 colors.push_back(Color(0,1,0)); 96 colors.push_back(Color(0,0,1)); 97 colors.push_back(Color(1,1,0)); 98 colors.push_back(Color(1,0,1)); 99 colors.push_back(Color(0,1,1)); 100 101 colors.push_back(Color(.5,0,0)); 102 colors.push_back(Color(0,.5,0)); 103 colors.push_back(Color(0,0,.5)); 104 colors.push_back(Color(.5,.5,0)); 105 colors.push_back(Color(.5,0,.5)); 106 colors.push_back(Color(0,.5,.5)); 107 108 colors.push_back(Color(.5,.5,.5)); 109 colors.push_back(Color(1,.5,.5)); 110 colors.push_back(Color(.5,1,.5)); 111 colors.push_back(Color(.5,.5,1)); 112 colors.push_back(Color(1,1,.5)); 113 colors.push_back(Color(1,.5,1)); 114 colors.push_back(Color(.5,1,1)); 115 116 colors.push_back(Color(1,.5,0)); 117 colors.push_back(Color(.5,1,0)); 118 colors.push_back(Color(1,0,.5)); 119 colors.push_back(Color(0,1,.5)); 120 colors.push_back(Color(0,.5,1)); 121 colors.push_back(Color(.5,0,1)); 122 } while(int(colors.size())<num); 123 // colors.push_back(Color(1,1,1)); 124 if(num>0) colors.resize(num); 125 } 126 ///\e 127 Color &operator[](int i) 128 { 129 return colors[i%colors.size()]; 130 } 131 ///\e 132 const Color &operator[](int i) const 133 { 134 return colors[i%colors.size()]; 135 } 136 ///\e 137 void set(int i,const Color &c) 138 { 139 colors[i%colors.size()]=c; 140 } 141 ///Sets the number of the exiting colors. 142 void resize(int s) { colors.resize(s);} 143 ///Returns the munber of the existing colors. 144 std::size_t size() { return colors.size();} 145 }; 146 147 ///Returns a visible distinct \ref Color 148 149 ///Returns a \ref Color which is as different from the given parameter 150 ///as it is possible. 151 inline Color distantColor(const Color &c) 152 { 153 return Color(c.getR()<.5?1:0,c.getG()<.5?1:0,c.getB()<.5?1:0); 154 } 155 ///Returns black for light colors and white for the dark ones. 156 157 ///Returns black for light colors and white for the dark ones. 158 ///\todo weighted average would be better 159 inline Color distantBW(const Color &c){ 160 double v=(c.getR()+c.getR()+c.getR())<1.5?1:0; 161 162 return Color(v,v,v); 163 } 164 63 165 ///Default traits class of \ref GraphToEps 64 166 … … 126 228 std::string _title; 127 229 std::string _copyright; 128 230 231 enum NodeTextColorType 232 { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; 233 ConstMap<typename Graph::Node,Color > _nodeTextColors; 234 129 235 ///Constructor 130 236 … … 150 256 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), 151 257 _undir(false), 152 _pleaseRemoveOsStream(_pros), _scaleToA4(false) {} 258 _pleaseRemoveOsStream(_pros), _scaleToA4(false), 259 _nodeTextColorType(SAME_COL), _nodeTextColors(Color(0,0,0)) 260 {} 153 261 }; 154 262 … … 224 332 return xy<double>(v.y,-v.x); 225 333 } 226 template<class xy>227 static std::string psOut(const xy &p)334 template<class TT> 335 static std::string psOut(const xy<TT> &p) 228 336 { 229 337 std::ostringstream os; 230 338 os << p.x << ' ' << p.y; 339 return os.str(); 340 } 341 static std::string psOut(const Color &c) 342 { 343 std::ostringstream os; 344 os << c.getR() << ' ' << c.getG() << ' ' << c.getB(); 231 345 return os.str(); 232 346 } … … 345 459 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); 346 460 } 461 template<class X> struct NodeTextColorsTraits : public T { 462 const X &_nodeTextColors; 463 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} 464 }; 465 ///Sets the map of the node text colors 466 467 ///Sets the map of the node text colors 468 ///\param x must be a node map with \ref Color values. 469 template<class X> GraphToEps<NodeTextColorsTraits<X> > 470 nodeTextColors(const X &x) 471 { 472 dontPrint=true; 473 _nodeTextColorType=CUST_COL; 474 return GraphToEps<NodeTextColorsTraits<X> > 475 (NodeTextColorsTraits<X>(*this,x)); 476 } 347 477 template<class X> struct EdgeColorsTraits : public T { 348 478 const X &_edgeColors; … … 436 566 /// 437 567 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} 568 569 ///Sets the color of the node texts to be different from the node color 570 571 ///Sets the color of the node texts to be as different from the node color 572 ///as it is possible 573 /// 574 GraphToEps<T> &distantColorNodeTexts() 575 {_nodeTextColorType=DIST_COL;return *this;} 576 ///Sets the color of the node texts to be black or white and always visible. 577 578 ///Sets the color of the node texts to be black or white according to 579 ///which is more 580 ///different from the node color 581 /// 582 GraphToEps<T> &distantBWNodeTexts() 583 {_nodeTextColorType=DIST_BW;return *this;} 584 438 585 ///Gives a preamble block for node Postscript block. 439 586 … … 601 748 std::vector<Edge> el; 602 749 for(EdgeIt e(g);e!=INVALID;++e) 603 if(!_undir||g.source(e)<g.target(e)) el.push_back(e); 750 if((!_undir||g.source(e)<g.target(e))&&_edgeWidths[e]>0) 751 el.push_back(e); 604 752 sort(el.begin(),el.end(),edgeLess(g)); 605 753 … … 687 835 } 688 836 else for(EdgeIt e(g);e!=INVALID;++e) 689 if( !_undir||g.source(e)<g.target(e))837 if((!_undir||g.source(e)<g.target(e))&&_edgeWidths[e]>0) 690 838 if(_drawArrows) { 691 839 xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]); … … 742 890 os << "/fosi " << _nodeTextSize << " def\n"; 743 891 os << "(Helvetica) findfont fosi scalefont setfont\n"; 744 os << "0 0 0 setrgbcolor\n"; 745 for(NodeIt n(g);n!=INVALID;++n) 892 for(NodeIt n(g);n!=INVALID;++n) { 893 switch(_nodeTextColorType) { 894 case DIST_COL: 895 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n"; 896 break; 897 case DIST_BW: 898 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n"; 899 break; 900 case CUST_COL: 901 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n"; 902 break; 903 default: 904 os << "0 0 0 setrgbcolor\n"; 905 } 746 906 os << _coords[n].x << ' ' << _coords[n].y 747 907 << " (" << _nodeTexts[n] << ") cshow\n"; 908 } 748 909 os << "grestore\n"; 749 910 } … … 777 938 ///example shows how to use these parameters. 778 939 ///\code 779 /// graphToEps(g ).scale(10).coords(coords)940 /// graphToEps(g,os).scale(10).coords(coords) 780 941 /// .nodeScale(2).nodeSizes(sizes) 781 942 /// .edgeWidthScale(.4).run(); -
src/lemon/maps.h
r1172 r1178 187 187 }; 188 188 189 ///Convert the \c Value of a maps to another type. 190 191 ///This \ref concept::ReadMap "read only map" 192 ///converts the \c Value of a maps to type \c T. 193 ///Its \c Value is inherited from \c M. 194 /// 195 ///Actually, 196 ///\code 197 /// ConvertMap<X> sh(x,v); 198 ///\endcode 199 ///it is equivalent with 200 ///\code 201 /// ConstMap<X::Key, X::Value> c_tmp(v); 202 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); 203 ///\endcode 204 ///\bug wrong documentation 205 template<class M, class T> 206 class ConvertMap 207 { 208 const M &m; 209 public: 210 typedef typename M::Key Key; 211 typedef T Value; 212 213 ///Constructor 214 215 ///Constructor 216 ///\param _m is the undelying map 217 ///\param _v is the convert value 218 ConvertMap(const M &_m) : m(_m) {}; 219 Value operator[](Key k) const {return m[k];} 220 }; 221 222 ///Returns an \ref ConvertMap class 223 224 ///This function just returns an \ref ConvertMap class. 225 ///\relates ConvertMap 226 ///\todo The order of the template parameters are changed. 227 template<class T, class M> 228 inline ConvertMap<M,T> convertMap(const M &m) 229 { 230 return ConvertMap<M,T>(m); 231 } 189 232 190 233 ///Sum of two maps
Note: See TracChangeset
for help on using the changeset viewer.