shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/viewer_axel.hpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *      This file is part of the source code of AXEL library          *
00003 *   Author(s): G. Gatellier                                          *
00004 *              B. Mourrain, GALAAD, INRIA                            *
00005 **********************************************************************/
00006 # ifndef shape_axel_ostream_hpp
00007 # define shape_axel_ostream_hpp
00008 /********************************************************************/
00009 # include <stdlib.h>
00010 # include <string.h>
00011 # include <sstream>
00012 # include <fstream>
00013 # include <shape/color.hpp>
00014 # include <shape/width.hpp>
00015 # include <shape/bounding_box.hpp>
00016 # include <shape/graphic.hpp>
00017 
00018 # define TMPL template<class V>
00019 # define SELF viewer<axel,V>
00020 # define BoundingBox bounding_box<double,V>
00021 /*********************************************************************/
00022 namespace mmx {
00023   namespace shape {
00024 //====================================================================
00025 struct axel {};
00026 
00027 template<class O, class V=default_env> struct viewer;
00028 
00030 
00034 template<class V> 
00035 struct viewer<axel,V> : public BoundingBox 
00036 {
00037     typedef mmx::shape::color< REF_OF(V) > Color;
00038 
00040     char *file;
00041     std::stringstream vw;
00042 
00043     Color color;
00044     Width width;
00045 
00047     viewer(): file((char*)"tmp.axl"), vw(std::ofstream::out | std::ofstream::app), color(0.75,0.75,0.75) {}
00048 
00049     viewer(char *fic) : file(fic), vw(std::ofstream::out | std::ofstream::app), color(0.75,0.75,0.75) {}
00050 
00051   
00053     viewer(const SELF& axl)
00054       :BoundingBox(axl.xmin(),axl.xmax(),axl.ymin(),axl.ymax(), axl.zmin(), axl.zmax()),  
00055        file(axl.file), vw(axl.vw.str(), std::ofstream::out | std::ofstream::app),
00056        color(axl.color) {}
00057     
00058 
00060     ~viewer() {}
00061 
00069     SELF& set_color(double r, double g, double b)
00070     {
00071       color=Color(r,g,b); return *this;
00072     }
00073 
00074     SELF& set_width(const shape::Width & w)
00075     {
00076       width=w; return *this;
00077     }
00078 
00079     SELF& operator=(const SELF & axl)
00080     {
00081       this->BoundingBox::operator=(axl);
00082       file=axl.file; vw<<axl.vw.str(); 
00083       color=axl.color;
00084       return *this;
00085     }
00086 
00087 
00091     void init() {
00092       //        std::ofstream f(file,std::ios::out);
00093     }
00094 
00098     int view() const
00099       {
00100         std::ofstream f(file, std::ofstream::out);
00101         if( !f )
00102           {
00103             std::cerr << "Creation refused" << std::endl;
00104             return (0);
00105           }
00106         f << "<axl>"<<std::endl;
00107         f << vw.str();
00108         f << "</axl>"<<std::endl;
00109         f.close();
00110 
00111         char cmd[1000];
00112         strcpy(cmd,"axel \0");
00113         strcat(cmd,file);                       \
00114         strcat(cmd," &");
00115         return system(cmd);
00116       }
00117 
00118     void view(char* opt) const
00119       {
00120         std::ofstream f(file, std::ofstream::out | std::ofstream::app);
00121         f << "</axl>\n";
00122         f.close();
00123 
00124         char cmd[1000];
00125         strcpy(cmd,"axel \0");
00126         strcat(cmd,file);
00127         strcat(cmd," ");        \
00128         strcat(cmd,opt);
00129         strcat(cmd," &");
00130         system(cmd);
00131       }
00132   };
00133 
00134   TMPL inline std::ostream& operator<<(std::ostream& os, const SELF & g)
00135   {
00136     os <<"Axel("<<g.file<<")"; return os;
00137   }
00138 
00139   TMPL inline SELF& operator<<(SELF & os, char s)
00140   {
00141     os.vw.put(s); return os;
00142   }
00143 
00144   TMPL inline SELF& operator<<(SELF & os, const char* s)
00145   {
00146     os.vw<<s; return os;
00147   }
00148 
00149   TMPL inline SELF& operator<<(SELF & os, const std::string& s)
00150   {
00151     os.vw<<s; return os;
00152   }
00153 
00154   TMPL inline SELF& operator<<(SELF& os, unsigned s)
00155   {
00156     os.vw<<s;return os;
00157   }
00158 
00159   TMPL inline SELF& operator<<(SELF& os, int s)
00160   {
00161     os.vw<<s;return os;
00162   }
00163 
00164   TMPL inline SELF& operator<<(SELF & os, double s)
00165   {
00166     os.vw<<s;return os;
00167   }
00168 
00169   TMPL SELF& 
00170   operator<<(SELF& os, const BoundingBox& bx) {
00171     for(unsigned i=0;i<3;i++)
00172       for(unsigned j=0;j<2;j++)
00173         os(i,j)=bx(i,j);
00174     return os;
00175   }
00176 
00177   TMPL inline SELF& 
00178   operator<<(SELF& os, const typename SELF::Color& c) {
00179     os.color.r=c.r;
00180     os.color.g=c.g;
00181     os.color.b=c.b;
00182     return os;
00183   }
00184 
00185   template<class V, class C, class W>
00186   SELF& operator<<(SELF& os, const graphic<C,W>& s)
00187   {
00188     if (s.type == 0) {
00189       os <<"<pointset  size=\""<<s.nbv<<"\" color=\"rgb\">\n";
00190       for(unsigned i =0;i<s.nbv;i++)
00191         os<<s.vertices[3*i]<<" "<<s.vertices[3*i+1]<<" "<<s.vertices[3*i+2]
00192           <<" 255 75 75\n";
00193       os <<"</pointset>\n";
00194     } else if (s.type == 1) {
00195       typedef fxv<double,3> point;
00196       point * P = (point*)(s.vertices);
00197       int   * Vs = (s.indices);
00198       os <<"<curve type=\"mesh\">\n<vect>\n";
00199       os <<"VECT\n";
00200       os <<s.nbi/2 << " " << s.nbi <<" "<<s.nbi/2<<"\n";
00201       for(unsigned i =0;i<s.nbi/2;i++)
00202         os<<"2 "; os<<"\n";
00203       for(unsigned i =0;i<s.nbi/2;i++)
00204         os<<"1 "; os<<"\n";
00205       
00206       for(unsigned i=0;i<s.nbi/2 ;i++) {
00207         os << P[Vs[2*i]][0]   <<" "<< P[Vs[2*i]][1] <<" "<< P[Vs[2*i]][2] <<" ";
00208         os << P[Vs[2*i+1]][0] <<" "<< P[Vs[2*i+1]][1] <<" "<< P[Vs[2*i+1]][2];
00209         os << "\n";
00210       }
00211       for(unsigned i =0;i<s.nbi/2;i++)
00212         os<<os.color.r<<" "<<os.color.g<<" "<<os.color.b<<" "<<"1\n";
00213       os <<"</vect>\n</curve>\n";
00214     } else if (s.type == 2) {
00215       std::cout<<"Surface"<<std::endl;
00216       typedef fxv<double,3> point;
00217       int   * Vs = (s.indices);
00218       os <<"<surface type=\"mesh\">\n<off>\n";
00219       os <<s.nbv<<" "<<s.nbi/3<<" 0\n";
00220       for(unsigned i=0;i<s.nbv;i++)
00221         os<<s.vertices[3*i]<<" "<<s.vertices[3*i+1]<<" "<<s.vertices[3*i+2] <<"\n";
00222       
00223       for(unsigned i=0;i<s.nbi/3;i++)
00224         os <<"3 "<< Vs[3*i]   <<" "<< Vs[3*i+1] <<" "<< Vs[3*i+2] <<"\n";
00225       os <<"</off>\n</surface>\n";
00226     }
00227     return os;
00228 }
00229 
00230 
00231 
00232 //====================================================================
00233 } //namespace shape
00234 } //namespace mmx 
00235 
00236 # undef TMPL
00237 # undef SELF
00238 # undef BoundingBox
00239 # endif // shape_axel_hpp