Diploma Thesis Percolation Simulation
C++ Sourcecode Documentation

www.AndreasKrueger.de/thesis/code

Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

svg.h

Go to the documentation of this file.
00001 // svg.h
00002 // output of spheres to SVG graphics
00003 //
00004 // (C) 14.10.2001 cpp__at__AndreasKrueger__dot__de
00005 // version 1.1 last change: 16.3.2002
00006 
00007 #ifndef _STRING_
00008 #include <string>
00009 #endif
00010 
00011 #ifndef _LIST_
00012 #include <list>
00013 #endif
00014 
00015 #ifndef _STRSTREAM_
00016  #include <strstream>
00017  using std::ostrstream;
00018 #endif
00019 
00020 
00021 namespace graphics {
00022 
00023 
00024 class RGBcolor {
00025         REAL R;
00026         REAL G;
00027         REAL B;
00028 public:
00029         RGBcolor();
00030         RGBcolor(REAL, REAL, REAL);
00031         string svg();
00032 };
00033 
00034 RGBcolor::RGBcolor(){R=0; G=0; B=0;}
00035 RGBcolor::RGBcolor(REAL R_, REAL G_, REAL B_) : R(R_), G(G_), B(B_) {};
00036 
00037 string RGBcolor::svg(){ 
00038         ostrstream temp;
00039         temp<<"rgb("<<R<<"%,"<<G<<"%,"<<B<<"%)";
00040         temp <<'\0';
00041         return temp.str();
00042 }
00043 
00044 
00045 class shape {
00046 protected:
00047         RGBcolor fillcolor;
00048         RGBcolor strokecolor;
00049         REAL opacity;
00050 public:
00051         shape (){};
00052         shape (RGBcolor fillcolor_, RGBcolor strokecolor_, REAL opacity_)
00053                 : fillcolor(fillcolor_), strokecolor(strokecolor_), opacity(opacity_){};
00054         string svgstyle(bool fill, bool stroke, bool opac);
00055         virtual string svg(bool fill, bool stroke, bool opac) {return "";} ;
00056 };
00057 
00058 
00059 string shape::svgstyle(bool fill, bool stroke, bool opac){
00060         ostrstream temp;
00061         if (fill||stroke||opac) {
00062                 if (fill)   temp<<"fill=\""<<fillcolor.svg()<<"\" ";
00063                 if (stroke) temp<<"stroke=\""<<strokecolor.svg()<<"\" ";
00064                 if (opac)   temp<<"opacity=\""<<opacity<<"\" ";
00065         }
00066         temp <<'\0';
00067         return temp.str();
00068 }
00069 
00070 
00071 
00072 class circle : public shape {
00073         COORDFLOAT cx;
00074         COORDFLOAT cy;
00075         REAL r;
00076 public:
00077         circle(){};
00078         circle(COORDFLOAT cx_, COORDFLOAT cy_, REAL r_) : cx(cx_), cy(cy_), r(r_) {};
00079         circle(COORDFLOAT cx_, COORDFLOAT cy_, REAL r_, RGBcolor fillcolor_, RGBcolor strokecolor_, REAL opacity_);
00080         string svg(bool fill, bool stroke, bool opac); 
00081 };
00082 
00083 circle::circle(COORDFLOAT cx_, COORDFLOAT cy_, REAL r_, RGBcolor fillcolor_, RGBcolor strokecolor_, REAL opacity_)      
00084                 : cx(cx_), cy(cy_), r(r_), shape(fillcolor_, strokecolor_, opacity_) {};
00085 
00086 string circle::svg(bool fill, bool stroke, bool opac){
00087         ostrstream temp;
00088         temp<<"<circle cx=\""<<cx<<"\" cy=\""<<cy<<"\" r=\""<<r<<"\" ";
00089         temp<<svgstyle(fill, stroke, opac);
00090         temp<<" />";
00091         temp <<'\0';
00092         return temp.str();
00093 }
00094 
00095 
00096 
00097 
00098 class rect : public shape {
00099         COORDFLOAT x;
00100         COORDFLOAT y;
00101         COORDFLOAT width;
00102         COORDFLOAT height;
00103 public:
00104         rect(COORDFLOAT x_, COORDFLOAT y_, COORDFLOAT width_, COORDFLOAT height_)       
00105                 : x(x_), y(y_), width(width_), height(height_) {};
00106         rect(COORDFLOAT x_, COORDFLOAT y_, COORDFLOAT width_, COORDFLOAT height_, 
00107                 RGBcolor fillcolor_, RGBcolor strokecolor_, REAL opacity_)
00108                 : x(x_), y(y_), width(width_), height(height_), shape (fillcolor_, strokecolor_, opacity_) {};
00109         string svg(bool fill, bool stroke, bool opac); 
00110 };
00111 
00112 string rect::svg(bool fill, bool stroke, bool opac){
00113         ostrstream temp;
00114         temp<<"<rect x=\""<<x<<"\" y=\""<<y<<"\" width=\""<<width<<"\" height=\""<<height<<"\" ";
00115         temp<<svgstyle(fill, stroke, opac) << "/>";
00116         temp <<'\0';
00117         return temp.str();
00118 }
00119 
00120 
00121 
00122 class svg {
00123         string filename;
00124         string xmlHeader;
00125         string svgParameter;
00126         string svgBody;
00127 public:
00128         svg();
00129         void setBody(string svgBody_);
00130         void append2Body(string element);
00131         void append2Body(shape element, bool fill, bool stroke, bool opac);
00132         void append2Body(std::list<shape*> & listofelements, bool fill, bool stroke, bool opac);
00133         void choose_header(int number);
00134         void choose_parameters(int number);
00135         void svg::write2file(string filename_);
00136 };
00137 
00138 svg::svg(){svgBody="";};
00139 
00140 void svg::setBody(string svgBody_){
00141         svgBody=svgBody_;
00142 }
00143         
00144 void svg::append2Body(string element){
00145         svgBody+="\n";
00146         svgBody+=element;
00147 }
00148 
00149 void svg::append2Body(shape element, bool fill, bool stroke, bool opac){
00150         svgBody+="\n";
00151         svgBody+=element.svg(fill, stroke, opac);
00152 }
00153 
00154 void svg::append2Body(std::list<shape*> & listofelements, bool fill, bool stroke, bool opac){
00155         std::list<shape*>::iterator e;
00156         string svgobject;
00157         for (e=listofelements.begin();e!=listofelements.end();e++){
00158                 svgBody+= (*e)->svg(fill, stroke, opac);
00159                 svgBody+="\n";
00160         }
00161 }
00162 
00163 void delete_objects(std::list<shape*> & listofelements){
00164         std::list<shape*>::iterator e;
00165         for (e=listofelements.begin();e!=listofelements.end();e++){
00166                 delete (*e);
00167         }
00168 }
00169                                                                                                                                                         
00170 const string XML_HEADER_1="<?xml version=\"1.0\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \n\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">";
00171 
00172 void svg::choose_header(int number){
00173         switch(number){
00174         case 1:
00175                 xmlHeader=XML_HEADER_1;
00176         break;
00177         }
00178 }
00179 
00180 const string SVG_PARAMETER_0="";
00181 const string SVG_PARAMETER_2="width=\"12cm\" height=\"3.5cm\"";
00182 const string SVG_PARAMETER_1="width=\"12cm\" height=\"3.5cm\" viewBox=\"0 0 1200 350\"";
00183 
00184 void svg::choose_parameters(int number){
00185         switch(number){
00186         case 0:
00187                 svgParameter=SVG_PARAMETER_0;
00188         break;
00189         case 1:
00190                 svgParameter=SVG_PARAMETER_1;
00191         break;
00192         case 2:
00193                 svgParameter=SVG_PARAMETER_2;
00194         break;
00195         }
00196 }
00197 
00198 void finddebug(){
00199         cout <<"founddebug"<<endl;
00200 }
00201 
00202 void svg::write2file(string filename_){
00203         filename=filename_;
00204         FILE* outFile= fopen( filename.c_str(), "w" );
00205         if (outFile==0){
00206                 cout << filename<<endl;
00207                 finddebug();
00208         }
00209         fprintf(outFile,"%s",xmlHeader.c_str());
00210 
00211         fprintf(outFile,"%s","\n<svg ");
00212         fprintf(outFile,"%s",svgParameter.c_str());
00213         fprintf(outFile,"%s",">\n");
00214         fprintf(outFile,"%s",svgBody.c_str());
00215         fprintf(outFile,"%s","\n</svg>\n");
00216         fclose( outFile ) ;
00217 }
00218 
00219 
00220 
00221 
00222 
00223 void svg_test(){
00224         svg myPicture;
00225         myPicture.choose_header(1);
00226         myPicture.choose_parameters(0);
00227         myPicture.setBody("");
00228         RGBcolor color1=RGBcolor(100,0,0);
00229         RGBcolor color2=RGBcolor(0,0,80);
00230         RGBcolor color3=RGBcolor(0,100,0);
00231         myPicture.append2Body(rect (0,0,1000, 1000,color1,color2, 1).svg(true, true, true));
00232         myPicture.append2Body(circle (300,300,50,color2,color3, 0.5).svg(true, true, true));
00233         myPicture.append2Body(circle (330,300,50,color2,color3, 0.5).svg(true, true, true));
00234         string filename=FILEHEAD2; filename+="test.svg";
00235         myPicture.write2file(filename);
00236         cout <<"Written the file "<<filename<<". \nPlease watch it using a SVG plugin."<<endl;
00237 }
00238 
00239 string real2string(REAL arg){
00240         ostrstream temp;
00241         temp<<arg;
00242         temp <<'\0';
00243         return temp.str();
00244 }
00245 
00246 
00247 void svg_circles(COORDFLOAT L, NUMBER N, REAL R){
00248 
00249         std::list<shape*> objects;
00250         shape* object;
00251 
00252         RGBcolor color1=RGBcolor(80,0,0);
00253         RGBcolor color2=RGBcolor(0,0,0);
00254         RGBcolor color3=RGBcolor(0,0,100);
00255 
00256         int i=N;
00257         for(i; i>0; i--){
00258                 object= new circle (long_random(L),long_random(L), R, color3, color2, 0.7);
00259                 objects.push_back(object);
00260         }
00261         svg myPicture;
00262         myPicture.choose_header(1);
00263         myPicture.choose_parameters(0);
00264         myPicture.setBody(rect (0,0,L, L,color1,color2, 0.8).svg(true, true, true));
00265 
00266         string group="";
00267         group+="<g style=\"fill:"+color3.svg()+"; ";
00268         group+="stroke-width:"+real2string(R/40)+"; stroke:" + color2.svg() + "\">\n";
00269 
00270         // idea: don't keep the huge string, 
00271         // but directly write it into a file
00272 
00273         myPicture.append2Body(group);
00274          myPicture.append2Body(objects, false, false, true);
00275         myPicture.append2Body("</g>");
00276 
00277         string filename=FILEHEAD2; 
00278         ostrstream temp2; 
00279         temp2<<"random_N"<<N<<"_L"<<L<<"_R"<<R<<".svg";
00280         temp2 <<'\0';
00281         filename+=temp2.str();
00282         myPicture.write2file(filename);
00283         cout <<"Written the file "<<filename<<". \nPlease watch it using a SVG plugin."<<endl;
00284         delete_objects(objects);
00285 }
00286 } // end of namespace graphics
00287 
00288 namespace frontend{
00289 void svg_frontend(){
00290         cout <<"SVG Scalable Vector Graphics\nPlease choose\n"<<endl;
00291         cout <<"a test (t)"<<endl;
00292         cout <<"throw random discs (r)"<<endl;
00293 
00294         char a;  cin>>a;
00295 
00296         switch (a) {
00297         case 't': graphics::svg_test(); break;
00298                 case 'r': 
00299                         COORDFLOAT L=500;
00300                         cout<<"number of discs : "; 
00301                         NUMBER N; cin >>N;
00302                         REAL Rcrit=R_critical_guessed (N, L, 2);
00303                         cout<<"choose radius (r.critical="<<Rcrit<<") : "; 
00304                         REAL R; cin >>R;
00305                         graphics::svg_circles(L, N, R); break;
00306         }
00307 }
00308 
00309 } // end of namespace frontend




Diploma Thesis Sourcecode Documentation
check out the text and the executable binaries

www.AndreasKrueger.de/thesis/code