Geant4 Cross Reference |
1 // Copyright (C) 2018, Guy Barrand. All rights 1 // Copyright (C) 2018, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_sg_write_paper 4 #ifndef tools_sg_write_paper 5 #define tools_sg_write_paper 5 #define tools_sg_write_paper 6 6 7 // To traverse a scene graph and "export" it 7 // To traverse a scene graph and "export" it at various file formats 8 // by using various rendering actions. 8 // by using various rendering actions. 9 // For example by using gl2ps_action, handle 9 // For example by using gl2ps_action, handle the formats: 10 // gl2ps_eps: gl2ps producing eps 10 // gl2ps_eps: gl2ps producing eps 11 // gl2ps_ps: gl2ps producing ps 11 // gl2ps_ps: gl2ps producing ps 12 // gl2ps_pdf: gl2ps producing pdf 12 // gl2ps_pdf: gl2ps producing pdf 13 // gl2ps_svg: gl2ps producing svg 13 // gl2ps_svg: gl2ps producing svg 14 // gl2ps_tex: gl2ps producing tex 14 // gl2ps_tex: gl2ps producing tex 15 // gl2ps_pgf: gl2ps producing pgf 15 // gl2ps_pgf: gl2ps producing pgf 16 // By using the zb_action (zb for zbuffer): 16 // By using the zb_action (zb for zbuffer): 17 // inzb_ps: tools::sg offscreen zbuffer put << 17 // zb_ps: tools::sg offscreen zbuffer put in a PostScript file. 18 // inzb_png: zbuffer put in a png file. It << 18 // zb_png: zbuffer put in a png file. It needs to provide a "png_writer" function. 19 // inzb_jpeg: zbuffer put in a jpeg file. I << 19 // zb_jpeg: zbuffer put in a jpeg file. It needs to provide a "jpeg_writer" function. 20 20 21 #include "zb_action" 21 #include "zb_action" 22 #include "node" 22 #include "node" 23 #include "gl2ps_action" 23 #include "gl2ps_action" 24 #include "../wps" 24 #include "../wps" 25 #include "../typedefs" 25 #include "../typedefs" 26 #include "../touplow" 26 #include "../touplow" 27 27 28 namespace tools { 28 namespace tools { 29 namespace sg { 29 namespace sg { 30 30 31 typedef bool (*png_writer)(std::ostream&,const 31 typedef bool (*png_writer)(std::ostream&,const std::string&, 32 unsigned char*,unsi 32 unsigned char*,unsigned int,unsigned int,unsigned int); 33 33 34 typedef bool (*jpeg_writer)(std::ostream&,cons 34 typedef bool (*jpeg_writer)(std::ostream&,const std::string&, 35 unsigned char*,uns 35 unsigned char*,unsigned int,unsigned int,unsigned int,int); 36 36 37 inline bool write_paper(std::ostream& a_out, 37 inline bool write_paper(std::ostream& a_out, 38 gl2ps_manager& a_gl2ps 38 gl2ps_manager& a_gl2ps_mgr,zb_manager& a_zb_mgr, 39 png_writer a_png_write 39 png_writer a_png_writer,jpeg_writer a_jpeg_writer, 40 float a_back_r,float a 40 float a_back_r,float a_back_g,float a_back_b,float a_back_a, 41 node& a_scene_graph, 41 node& a_scene_graph, 42 unsigned int a_width,u 42 unsigned int a_width,unsigned int a_height, 43 const std::string& a_f << 43 const std::string& a_file,const std::string& a__format) { 44 bool a_do_transparency << 45 bool a_top_to_bottom, << 46 const std::string& a_o << 47 if(!a_width || !a_height) return false; 44 if(!a_width || !a_height) return false; 48 std::string a_format = a__format; 45 std::string a_format = a__format; 49 tolowercase(a_format); //handle legacy. 46 tolowercase(a_format); //handle legacy. 50 int gl2ps_format; 47 int gl2ps_format; 51 if(gl2ps_s2format(a_format,gl2ps_format)) { << 48 if(s2format(a_format,gl2ps_format)) { 52 int sort = -1; << 53 if(a_opts_1.size() && !gl2ps_s2sort(a_opts << 54 a_out << "tools::sg::write_paper: bad gl << 55 return false; << 56 } << 57 int options = -1; << 58 if(a_opts_2.size() && !gl2ps_s2options(a_o << 59 a_out << "tools::sg::write_paper: bad gl << 60 return false; << 61 } << 62 gl2ps_action action(a_gl2ps_mgr,a_out,a_wi 49 gl2ps_action action(a_gl2ps_mgr,a_out,a_width,a_height); 63 action.clear_color(a_back_r,a_back_g,a_bac 50 action.clear_color(a_back_r,a_back_g,a_back_b,a_back_a); 64 if(!action.open(a_file,gl2ps_format,sort,o << 51 if(!action.open(a_file,gl2ps_format)) return false; 65 action.set_do_transparency(false); << 66 action.set_have_to_do_transparency(false); << 67 a_scene_graph.render(action); 52 a_scene_graph.render(action); 68 if(!action.end()) { //check that matrices << 69 a_out << "tools::sg::write_paper: bad gl << 70 action.close(); << 71 return false; << 72 } else if(a_do_transparency) { << 73 if(action.have_to_do_transparency()) { << 74 //a_out << "tools::sg::write_paper: wa << 75 action.set_do_transparency(true); << 76 a_scene_graph.render(action); << 77 if(!action.end()) { //check that matri << 78 a_out << "tools::sg::write_paper: ba << 79 action.close(); << 80 return false; << 81 } << 82 } << 83 } << 84 action.close(); 53 action.close(); 85 return true; 54 return true; 86 } 55 } 87 56 88 zb_action action(a_zb_mgr,a_out,a_width,a_he 57 zb_action action(a_zb_mgr,a_out,a_width,a_height); 89 action.clear_color_buffer(a_back_r,a_back_g, << 58 action.zbuffer().clear_color_buffer(0); 90 action.clear_depth_buffer(); << 59 action.add_color(a_back_r,a_back_g,a_back_b,a_back_a); 91 action.set_do_transparency(false); << 60 action.zbuffer().clear_depth_buffer(); 92 action.set_have_to_do_transparency(false); << 93 a_scene_graph.render(action); 61 a_scene_graph.render(action); 94 if(!action.end()) { //check that matrices st << 95 a_out << "tools::sg::write_paper: bad zb_a << 96 return false; << 97 } else if(a_do_transparency) { << 98 if(action.have_to_do_transparency()) { << 99 action.set_do_transparency(true); << 100 a_scene_graph.render(action); << 101 if(!action.end()) { //check that matrice << 102 a_out << "tools::sg::write_paper: bad << 103 return false; << 104 } << 105 } << 106 } << 107 62 108 if((a_format=="zb_ps")||(a_format=="inzb_ps" 63 if((a_format=="zb_ps")||(a_format=="inzb_ps")) { 109 wps wps(a_out); 64 wps wps(a_out); 110 if(!wps.open_file(a_file)) { 65 if(!wps.open_file(a_file)) { 111 a_out << "tools::sg::write_paper : can't 66 a_out << "tools::sg::write_paper : can't open " << a_file << "." << std::endl; 112 return false; 67 return false; 113 } 68 } 114 wps.PS_BEGIN_PAGE(); 69 wps.PS_BEGIN_PAGE(); 115 wps.PS_PAGE_SCALE(float(a_width),float(a_h 70 wps.PS_PAGE_SCALE(float(a_width),float(a_height)); 116 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,z 71 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,zb_action::get_rgb,&action); 117 wps.PS_END_PAGE(); 72 wps.PS_END_PAGE(); 118 wps.close_file(); 73 wps.close_file(); 119 return true; 74 return true; 120 } 75 } 121 76 122 if((a_format=="zb_png")||(a_format=="inzb_pn 77 if((a_format=="zb_png")||(a_format=="inzb_png")) { 123 if(!a_png_writer) { 78 if(!a_png_writer) { 124 a_out << "tools::sg::write_paper : no pn 79 a_out << "tools::sg::write_paper : no png_writer given." << std::endl; 125 return false; 80 return false; 126 } 81 } 127 82 128 std::vector<unsigned char> buffer; << 83 size_t sz; 129 if(!action.get_rgbas(a_top_to_bottom,buffe << 84 unsigned char* buffer = action.get_rgbas(sz); >> 85 if(!buffer) { 130 a_out << "tools::sg::write_paper : can't 86 a_out << "tools::sg::write_paper : can't get rgba image." << std::endl; 131 return false; 87 return false; 132 } 88 } 133 89 134 if(!a_png_writer(a_out,a_file,vec_data(buf << 90 if(!a_png_writer(a_out,a_file,buffer,a_width,a_height,4)) { 135 a_out << "tools::sg::write_paper : tools 91 a_out << "tools::sg::write_paper : tools::png::write() failed." << std::endl; >> 92 delete [] buffer; 136 return false; 93 return false; 137 } 94 } >> 95 delete [] buffer; 138 return true; 96 return true; 139 } 97 } 140 98 141 if((a_format=="zb_jpeg")||(a_format=="zb_jpg 99 if((a_format=="zb_jpeg")||(a_format=="zb_jpg")||(a_format=="inzb_jpeg")) { 142 100 143 if(!a_jpeg_writer) { 101 if(!a_jpeg_writer) { 144 a_out << "tools::sg::write_paper : no jp 102 a_out << "tools::sg::write_paper : no jpeg_writer given." << std::endl; 145 return false; 103 return false; 146 } 104 } 147 105 148 std::vector<unsigned char> buffer; << 106 size_t sz; 149 if(!action.get_rgbs(a_top_to_bottom,buffer << 107 unsigned char* buffer = action.get_rgbs(sz); >> 108 if(!buffer) { 150 a_out << "tools::sg::write_paper : can't 109 a_out << "tools::sg::write_paper : can't get rgb image." << std::endl; 151 return false; 110 return false; 152 } 111 } 153 112 154 if(!a_jpeg_writer(a_out,a_file,vec_data(bu << 113 if(!a_jpeg_writer(a_out,a_file,buffer,a_width,a_height,3,100)) { 155 a_out << "tools::sg::write_paper : tools 114 a_out << "tools::sg::write_paper : tools::jpeg::write() failed." << std::endl; >> 115 delete [] buffer; 156 return false; 116 return false; 157 } 117 } >> 118 delete [] buffer; 158 return true; 119 return true; 159 } 120 } 160 121 161 a_out << "tools::sg::write_paper : unknown f 122 a_out << "tools::sg::write_paper : unknown format " << a_format << std::endl; 162 return false; 123 return false; 163 } << 164 << 165 inline bool write_paper(std::ostream& a_out, << 166 gl2ps_manager& a_gl2ps << 167 png_writer a_png_write << 168 const tools::colorf& a << 169 node& a_scene_graph, << 170 unsigned int a_width,u << 171 const std::string& a_f << 172 bool a_do_transparency << 173 const std::string& a_o << 174 return tools::sg::write_paper(a_out,a_gl2ps_ << 175 a_png_writer,a << 176 a_back.r(),a_b << 177 a_scene_graph, << 178 a_do_transpare << 179 } << 180 << 181 inline bool write_paper(std::ostream& a_out, << 182 gl2ps_manager& a_gl2ps << 183 png_writer a_png_write << 184 const tools::colorf& a << 185 node& a_scene_graph, << 186 unsigned int a_width,u << 187 const std::string& a_f << 188 return tools::sg::write_paper(a_out,a_gl2ps_ << 189 a_png_writer,a << 190 a_back.r(),a_b << 191 a_scene_graph, << 192 true,true,std: << 193 } 124 } 194 125 195 }} 126 }} 196 127 197 #endif 128 #endif