Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/sg/write_paper is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2018, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_sg_write_paper
0005 #define tools_sg_write_paper
0006 
0007 //  To traverse a scene graph and "export" it at various file formats
0008 // by using various rendering actions.
0009 //  For example by using gl2ps_action, handle the formats:
0010 //    gl2ps_eps: gl2ps producing eps
0011 //    gl2ps_ps:  gl2ps producing ps
0012 //    gl2ps_pdf: gl2ps producing pdf
0013 //    gl2ps_svg: gl2ps producing svg
0014 //    gl2ps_tex: gl2ps producing tex
0015 //    gl2ps_pgf: gl2ps producing pgf
0016 // By using the zb_action (zb for zbuffer):
0017 //    inzb_ps: tools::sg offscreen zbuffer put in a PostScript file.
0018 //    inzb_png: zbuffer put in a png file. It needs to provide a "png_writer" function.
0019 //    inzb_jpeg: zbuffer put in a jpeg file. It needs to provide a "jpeg_writer" function.
0020 
0021 #include "zb_action"
0022 #include "node"
0023 #include "gl2ps_action"
0024 #include "../wps"
0025 #include "../typedefs"
0026 #include "../touplow"
0027 
0028 namespace tools {
0029 namespace sg {
0030 
0031 typedef bool (*png_writer)(std::ostream&,const std::string&,
0032                            unsigned char*,unsigned int,unsigned int,unsigned int);
0033 
0034 typedef bool (*jpeg_writer)(std::ostream&,const std::string&,
0035                             unsigned char*,unsigned int,unsigned int,unsigned int,int);
0036 
0037 inline bool write_paper(std::ostream& a_out,
0038                         gl2ps_manager& a_gl2ps_mgr,zb_manager& a_zb_mgr,
0039                         png_writer a_png_writer,jpeg_writer a_jpeg_writer,
0040                         float a_back_r,float a_back_g,float a_back_b,float a_back_a,
0041                         node& a_scene_graph,
0042                         unsigned int a_width,unsigned int a_height,
0043                         const std::string& a_file,const std::string& a__format,
0044                         bool a_do_transparency,
0045                         bool a_top_to_bottom,
0046                         const std::string& a_opts_1,const std::string& a_opts_2) {
0047   if(!a_width || !a_height) return false;
0048   std::string a_format = a__format;
0049   tolowercase(a_format);  //handle legacy.
0050   int gl2ps_format;
0051   if(gl2ps_s2format(a_format,gl2ps_format)) {
0052     int sort = -1;
0053     if(a_opts_1.size() && !gl2ps_s2sort(a_opts_1,sort)) {
0054       a_out << "tools::sg::write_paper: bad gl2ps sort " << sout(a_opts_1) << "." << std::endl;
0055       return false;
0056     }
0057     int options = -1;
0058     if(a_opts_2.size() && !gl2ps_s2options(a_opts_2,options)) {
0059       a_out << "tools::sg::write_paper: bad gl2ps options " << sout(a_opts_2) << "." << std::endl;
0060       return false;
0061     }
0062     gl2ps_action action(a_gl2ps_mgr,a_out,a_width,a_height);
0063     action.clear_color(a_back_r,a_back_g,a_back_b,a_back_a);
0064     if(!action.open(a_file,gl2ps_format,sort,options)) return false;
0065     action.set_do_transparency(false);
0066     action.set_have_to_do_transparency(false);
0067     a_scene_graph.render(action);
0068     if(!action.end()) { //check that matrices stack are ok.
0069       a_out << "tools::sg::write_paper: bad gl2ps_action end." << std::endl;
0070       action.close();
0071       return false;
0072     } else if(a_do_transparency) {
0073       if(action.have_to_do_transparency()) {
0074         //a_out << "tools::sg::write_paper: warning: gl2ps does not handle transparency." << std::endl;
0075         action.set_do_transparency(true);
0076         a_scene_graph.render(action);
0077         if(!action.end()) { //check that matrices stack are ok.
0078           a_out << "tools::sg::write_paper: bad gl2ps_action end." << std::endl;
0079           action.close();
0080           return false;
0081         }
0082       }
0083     }
0084     action.close();
0085     return true;
0086   }
0087 
0088   zb_action action(a_zb_mgr,a_out,a_width,a_height);
0089   action.clear_color_buffer(a_back_r,a_back_g,a_back_b,a_back_a);
0090   action.clear_depth_buffer();
0091   action.set_do_transparency(false);
0092   action.set_have_to_do_transparency(false);
0093   a_scene_graph.render(action);
0094   if(!action.end()) { //check that matrices stack are ok.
0095     a_out << "tools::sg::write_paper: bad zb_action end." << std::endl;
0096     return false;
0097   } else if(a_do_transparency) {
0098     if(action.have_to_do_transparency()) {
0099       action.set_do_transparency(true);
0100       a_scene_graph.render(action);
0101       if(!action.end()) { //check that matrices stack are ok.
0102         a_out << "tools::sg::write_paper: bad zb_action end." << std::endl;
0103         return false;
0104       }
0105     }
0106   }
0107 
0108   if((a_format=="zb_ps")||(a_format=="inzb_ps")) {
0109     wps wps(a_out);
0110     if(!wps.open_file(a_file)) {
0111       a_out << "tools::sg::write_paper : can't open " << a_file << "." << std::endl;
0112       return false;
0113     }
0114     wps.PS_BEGIN_PAGE();
0115     wps.PS_PAGE_SCALE(float(a_width),float(a_height));
0116     wps.PS_IMAGE(a_width,a_height,wps::rgb_4,zb_action::get_rgb,&action);
0117     wps.PS_END_PAGE();
0118     wps.close_file();
0119     return true;
0120   }
0121 
0122   if((a_format=="zb_png")||(a_format=="inzb_png")) {
0123     if(!a_png_writer) {
0124       a_out << "tools::sg::write_paper : no png_writer given." << std::endl;
0125       return false;
0126     }
0127 
0128     std::vector<unsigned char> buffer;
0129     if(!action.get_rgbas(a_top_to_bottom,buffer)) {
0130       a_out << "tools::sg::write_paper : can't get rgba image." << std::endl;
0131       return false;
0132     }
0133 
0134     if(!a_png_writer(a_out,a_file,vec_data(buffer),a_width,a_height,4)) {
0135       a_out << "tools::sg::write_paper : tools::png::write() failed." << std::endl;
0136       return false;
0137     }
0138     return true;
0139   }
0140 
0141   if((a_format=="zb_jpeg")||(a_format=="zb_jpg")||(a_format=="inzb_jpeg")) {
0142 
0143     if(!a_jpeg_writer) {
0144       a_out << "tools::sg::write_paper : no jpeg_writer given." << std::endl;
0145       return false;
0146     }
0147 
0148     std::vector<unsigned char> buffer;
0149     if(!action.get_rgbs(a_top_to_bottom,buffer)) {
0150       a_out << "tools::sg::write_paper : can't get rgb image." << std::endl;
0151       return false;
0152     }
0153 
0154     if(!a_jpeg_writer(a_out,a_file,vec_data(buffer),a_width,a_height,3,100)) {
0155       a_out << "tools::sg::write_paper : tools::jpeg::write() failed." << std::endl;
0156       return false;
0157     }
0158     return true;
0159   }
0160 
0161   a_out << "tools::sg::write_paper : unknown format " << a_format << std::endl;
0162   return false;
0163 }
0164 
0165 inline bool write_paper(std::ostream& a_out,
0166                         gl2ps_manager& a_gl2ps_mgr,zb_manager& a_zb_mgr,
0167                         png_writer a_png_writer,jpeg_writer a_jpeg_writer,
0168                         const tools::colorf& a_back,
0169                         node& a_scene_graph,
0170                         unsigned int a_width,unsigned int a_height,
0171                         const std::string& a_file,const std::string& a_format,
0172                         bool a_do_transparency,bool a_top_to_bottom,
0173                         const std::string& a_opts_1,const std::string& a_opts_2) {
0174   return tools::sg::write_paper(a_out,a_gl2ps_mgr,a_zb_mgr,
0175                                 a_png_writer,a_jpeg_writer,
0176                                 a_back.r(),a_back.g(),a_back.b(),a_back.a(),
0177                                 a_scene_graph,a_width,a_height,a_file,a_format,
0178                                 a_do_transparency,a_top_to_bottom,a_opts_1,a_opts_2);
0179 }
0180 
0181 inline bool write_paper(std::ostream& a_out,
0182                         gl2ps_manager& a_gl2ps_mgr,zb_manager& a_zb_mgr,
0183                         png_writer a_png_writer,jpeg_writer a_jpeg_writer,
0184                         const tools::colorf& a_back,
0185                         node& a_scene_graph,
0186                         unsigned int a_width,unsigned int a_height,
0187                         const std::string& a_file,const std::string& a_format) {
0188   return tools::sg::write_paper(a_out,a_gl2ps_mgr,a_zb_mgr,
0189                                 a_png_writer,a_jpeg_writer,
0190                                 a_back.r(),a_back.g(),a_back.b(),a_back.a(),
0191                                 a_scene_graph,a_width,a_height,a_file,a_format,
0192                                 true,true,std::string(),std::string());
0193 }
0194 
0195 }}
0196 
0197 #endif