00001
00002
00003
00004
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 # include <realroot/polynomial.hpp>
00019
00020 # define TMPL template<class V>
00021 # define SELF viewer<axel,V>
00022 # define BoundingBox bounding_box<double,V>
00023
00024 namespace mmx {
00025 namespace shape {
00026
00027 struct axel {};
00028
00029 template<class O, class V=default_env> struct viewer;
00030
00032
00036 template<class V>
00037 struct viewer<axel,V> : public BoundingBox
00038 {
00039 typedef mmx::shape::color< REF_OF(V) > Color;
00040
00042 char *file;
00043 std::stringstream vw;
00044
00045 Color color;
00046 Width width;
00047
00049 viewer(): file((char*)"tmp.axl"), vw(std::ofstream::out | std::ofstream::app), color(0.75,0.75,0.75) {}
00050
00057 viewer(char *fic) : file(fic), vw(std::ofstream::out | std::ofstream::app), color(0.75,0.75,0.75) {}
00058
00060 viewer(const SELF& axl)
00061 :BoundingBox(axl.xmin(),axl.xmax(),axl.ymin(),axl.ymax(), axl.zmin(), axl.zmax()),
00062 file(axl.file), vw(axl.vw.str(), std::ofstream::out | std::ofstream::app),
00063 color(axl.color) {}
00064
00066 ~viewer() {}
00067
00068
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
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
00121 int save() const
00122 {
00123 std::ofstream f(file, std::ofstream::out);
00124 if( !f )
00125 {
00126 std::cerr << "Creation refused" << std::endl;
00127 return (0);
00128 }
00129 f << "<axl>"<<std::endl;
00130 f << vw.str();
00131 f << "</axl>"<<std::endl;
00132 f.close();
00133
00134 return (1);
00135 }
00136
00137 void view(char* opt) const
00138 {
00139 std::ofstream f(file, std::ofstream::out | std::ofstream::app);
00140 f << "</axl>\n";
00141 f.close();
00142
00143 char cmd[1000];
00144 strcpy(cmd,"axel \0");
00145 strcat(cmd,file);
00146 strcat(cmd," "); \
00147 strcat(cmd,opt);
00148 strcat(cmd," &");
00149 system(cmd);
00150 }
00151
00155 void precision(int p)
00156 {
00157 vw.precision(p);
00158 }
00159
00160 };
00161
00162 TMPL inline std::ostream& operator<<(std::ostream& os, const SELF & g)
00163 {
00164 os <<"Axel("<<g.file<<")"; return os;
00165 }
00166
00167 TMPL inline SELF& operator<<(SELF & os, char s)
00168 {
00169 os.vw.put(s); return os;
00170 }
00171
00172 TMPL inline SELF& operator<<(SELF & os, const char* s)
00173 {
00174 os.vw<<s; return os;
00175 }
00176
00177 TMPL inline SELF& operator<<(SELF & os, const std::string& s)
00178 {
00179 os.vw<<s; return os;
00180 }
00181
00182 TMPL inline SELF& operator<<(SELF& os, unsigned s)
00183 {
00184 os.vw<<s;return os;
00185 }
00186
00187 TMPL inline SELF& operator<<(SELF& os, int s)
00188 {
00189 os.vw<<s;return os;
00190 }
00191
00192 TMPL inline SELF& operator<<(SELF & os, double s)
00193 {
00194 os.vw<<s;return os;
00195 }
00196
00197 TMPL SELF&
00198 operator<<(SELF& os, const BoundingBox& bx) {
00199 for(unsigned i=0;i<3;i++)
00200 for(unsigned j=0;j<2;j++)
00201 os(i,j)=bx(i,j);
00202 return os;
00203 }
00204
00205 TMPL inline SELF&
00206 operator<<(SELF& os, const typename SELF::Color& c) {
00207 os.color.r=c.r;
00208 os.color.g=c.g;
00209 os.color.b=c.b;
00210 return os;
00211 }
00212
00213 template <class V,class C, class VARIANT> inline SELF&
00214 operator<<(SELF& os, const polynomial<C, VARIANT>& mp ) {
00215 print(os.vw, mp.rep(), polynomial<C, VARIANT>::Ring::vars()); return os;
00216 }
00217
00218
00219 template<class V, class C, class W>
00220 SELF& operator<<(SELF& os, const graphic<C,W>& s)
00221 {
00222 os <<"<mesh type=\"off\">\n";
00223 os <<s.nbv() << " " << s.nbe() << " " << s.nbf() <<"\n";
00224
00225 for(unsigned i =0;i<s.nbv();i++)
00226 os<<s.vertex_coord(3*i)<<" "<<s.vertex_coord(3*i+1)<<" "<<s.vertex_coord(3*i+2)<<"\n";
00227
00228 for(unsigned i=0;i<s.nbe() ;i++)
00229 os << s.edge_index(2*i) <<" " << s.edge_index(2*i+1) << "\n";
00230
00231 for(unsigned i=0;i<s.nbf();i++)
00232 os <<"3 "<< s.face_index(3*i) <<" "<< s.face_index(3*i+1) <<" "<< s.face_index(3*i+2) <<"\n";
00233 os <<"</mesh>\n";
00234
00235 return os;
00236 }
00237
00238
00239
00240
00241 }
00242 }
00243
00244 # undef TMPL
00245 # undef SELF
00246 # undef BoundingBox
00247 # endif // shape_axel_hpp