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 // inzb_ps: tools::sg offscreen zbuffer put in a PostScript file. 18 // inzb_png: zbuffer put in a png file. It 18 // inzb_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 // inzb_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 44 bool a_do_transparency, 45 bool a_top_to_bottom, 45 bool a_top_to_bottom, 46 const std::string& a_o 46 const std::string& a_opts_1,const std::string& a_opts_2) { 47 if(!a_width || !a_height) return false; 47 if(!a_width || !a_height) return false; 48 std::string a_format = a__format; 48 std::string a_format = a__format; 49 tolowercase(a_format); //handle legacy. 49 tolowercase(a_format); //handle legacy. 50 int gl2ps_format; 50 int gl2ps_format; 51 if(gl2ps_s2format(a_format,gl2ps_format)) { 51 if(gl2ps_s2format(a_format,gl2ps_format)) { 52 int sort = -1; 52 int sort = -1; 53 if(a_opts_1.size() && !gl2ps_s2sort(a_opts 53 if(a_opts_1.size() && !gl2ps_s2sort(a_opts_1,sort)) { 54 a_out << "tools::sg::write_paper: bad gl 54 a_out << "tools::sg::write_paper: bad gl2ps sort " << sout(a_opts_1) << "." << std::endl; 55 return false; 55 return false; 56 } 56 } 57 int options = -1; 57 int options = -1; 58 if(a_opts_2.size() && !gl2ps_s2options(a_o 58 if(a_opts_2.size() && !gl2ps_s2options(a_opts_2,options)) { 59 a_out << "tools::sg::write_paper: bad gl 59 a_out << "tools::sg::write_paper: bad gl2ps options " << sout(a_opts_2) << "." << std::endl; 60 return false; 60 return false; 61 } 61 } 62 gl2ps_action action(a_gl2ps_mgr,a_out,a_wi 62 gl2ps_action action(a_gl2ps_mgr,a_out,a_width,a_height); 63 action.clear_color(a_back_r,a_back_g,a_bac 63 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 64 if(!action.open(a_file,gl2ps_format,sort,options)) return false; 65 action.set_do_transparency(false); 65 action.set_do_transparency(false); 66 action.set_have_to_do_transparency(false); 66 action.set_have_to_do_transparency(false); 67 a_scene_graph.render(action); 67 a_scene_graph.render(action); 68 if(!action.end()) { //check that matrices 68 if(!action.end()) { //check that matrices stack are ok. 69 a_out << "tools::sg::write_paper: bad gl 69 a_out << "tools::sg::write_paper: bad gl2ps_action end." << std::endl; 70 action.close(); 70 action.close(); 71 return false; 71 return false; 72 } else if(a_do_transparency) { 72 } else if(a_do_transparency) { 73 if(action.have_to_do_transparency()) { 73 if(action.have_to_do_transparency()) { 74 //a_out << "tools::sg::write_paper: wa 74 //a_out << "tools::sg::write_paper: warning: gl2ps does not handle transparency." << std::endl; 75 action.set_do_transparency(true); 75 action.set_do_transparency(true); 76 a_scene_graph.render(action); 76 a_scene_graph.render(action); 77 if(!action.end()) { //check that matri 77 if(!action.end()) { //check that matrices stack are ok. 78 a_out << "tools::sg::write_paper: ba 78 a_out << "tools::sg::write_paper: bad gl2ps_action end." << std::endl; 79 action.close(); 79 action.close(); 80 return false; 80 return false; 81 } 81 } 82 } 82 } 83 } 83 } 84 action.close(); 84 action.close(); 85 return true; 85 return true; 86 } 86 } 87 87 88 zb_action action(a_zb_mgr,a_out,a_width,a_he 88 zb_action action(a_zb_mgr,a_out,a_width,a_height); 89 action.clear_color_buffer(a_back_r,a_back_g, 89 action.clear_color_buffer(a_back_r,a_back_g,a_back_b,a_back_a); 90 action.clear_depth_buffer(); 90 action.clear_depth_buffer(); 91 action.set_do_transparency(false); 91 action.set_do_transparency(false); 92 action.set_have_to_do_transparency(false); 92 action.set_have_to_do_transparency(false); 93 a_scene_graph.render(action); 93 a_scene_graph.render(action); 94 if(!action.end()) { //check that matrices st 94 if(!action.end()) { //check that matrices stack are ok. 95 a_out << "tools::sg::write_paper: bad zb_a 95 a_out << "tools::sg::write_paper: bad zb_action end." << std::endl; 96 return false; 96 return false; 97 } else if(a_do_transparency) { 97 } else if(a_do_transparency) { 98 if(action.have_to_do_transparency()) { 98 if(action.have_to_do_transparency()) { 99 action.set_do_transparency(true); 99 action.set_do_transparency(true); 100 a_scene_graph.render(action); 100 a_scene_graph.render(action); 101 if(!action.end()) { //check that matrice 101 if(!action.end()) { //check that matrices stack are ok. 102 a_out << "tools::sg::write_paper: bad 102 a_out << "tools::sg::write_paper: bad zb_action end." << std::endl; 103 return false; 103 return false; 104 } 104 } 105 } 105 } 106 } 106 } 107 107 108 if((a_format=="zb_ps")||(a_format=="inzb_ps" 108 if((a_format=="zb_ps")||(a_format=="inzb_ps")) { 109 wps wps(a_out); 109 wps wps(a_out); 110 if(!wps.open_file(a_file)) { 110 if(!wps.open_file(a_file)) { 111 a_out << "tools::sg::write_paper : can't 111 a_out << "tools::sg::write_paper : can't open " << a_file << "." << std::endl; 112 return false; 112 return false; 113 } 113 } 114 wps.PS_BEGIN_PAGE(); 114 wps.PS_BEGIN_PAGE(); 115 wps.PS_PAGE_SCALE(float(a_width),float(a_h 115 wps.PS_PAGE_SCALE(float(a_width),float(a_height)); 116 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,z 116 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,zb_action::get_rgb,&action); 117 wps.PS_END_PAGE(); 117 wps.PS_END_PAGE(); 118 wps.close_file(); 118 wps.close_file(); 119 return true; 119 return true; 120 } 120 } 121 121 122 if((a_format=="zb_png")||(a_format=="inzb_pn 122 if((a_format=="zb_png")||(a_format=="inzb_png")) { 123 if(!a_png_writer) { 123 if(!a_png_writer) { 124 a_out << "tools::sg::write_paper : no pn 124 a_out << "tools::sg::write_paper : no png_writer given." << std::endl; 125 return false; 125 return false; 126 } 126 } 127 127 128 std::vector<unsigned char> buffer; 128 std::vector<unsigned char> buffer; 129 if(!action.get_rgbas(a_top_to_bottom,buffe 129 if(!action.get_rgbas(a_top_to_bottom,buffer)) { 130 a_out << "tools::sg::write_paper : can't 130 a_out << "tools::sg::write_paper : can't get rgba image." << std::endl; 131 return false; 131 return false; 132 } 132 } 133 133 134 if(!a_png_writer(a_out,a_file,vec_data(buf 134 if(!a_png_writer(a_out,a_file,vec_data(buffer),a_width,a_height,4)) { 135 a_out << "tools::sg::write_paper : tools 135 a_out << "tools::sg::write_paper : tools::png::write() failed." << std::endl; 136 return false; 136 return false; 137 } 137 } 138 return true; 138 return true; 139 } 139 } 140 140 141 if((a_format=="zb_jpeg")||(a_format=="zb_jpg 141 if((a_format=="zb_jpeg")||(a_format=="zb_jpg")||(a_format=="inzb_jpeg")) { 142 142 143 if(!a_jpeg_writer) { 143 if(!a_jpeg_writer) { 144 a_out << "tools::sg::write_paper : no jp 144 a_out << "tools::sg::write_paper : no jpeg_writer given." << std::endl; 145 return false; 145 return false; 146 } 146 } 147 147 148 std::vector<unsigned char> buffer; 148 std::vector<unsigned char> buffer; 149 if(!action.get_rgbs(a_top_to_bottom,buffer 149 if(!action.get_rgbs(a_top_to_bottom,buffer)) { 150 a_out << "tools::sg::write_paper : can't 150 a_out << "tools::sg::write_paper : can't get rgb image." << std::endl; 151 return false; 151 return false; 152 } 152 } 153 153 154 if(!a_jpeg_writer(a_out,a_file,vec_data(bu 154 if(!a_jpeg_writer(a_out,a_file,vec_data(buffer),a_width,a_height,3,100)) { 155 a_out << "tools::sg::write_paper : tools 155 a_out << "tools::sg::write_paper : tools::jpeg::write() failed." << std::endl; 156 return false; 156 return false; 157 } 157 } 158 return true; 158 return true; 159 } 159 } 160 160 161 a_out << "tools::sg::write_paper : unknown f 161 a_out << "tools::sg::write_paper : unknown format " << a_format << std::endl; 162 return false; 162 return false; 163 } 163 } 164 164 165 inline bool write_paper(std::ostream& a_out, 165 inline bool write_paper(std::ostream& a_out, 166 gl2ps_manager& a_gl2ps 166 gl2ps_manager& a_gl2ps_mgr,zb_manager& a_zb_mgr, 167 png_writer a_png_write 167 png_writer a_png_writer,jpeg_writer a_jpeg_writer, 168 const tools::colorf& a 168 const tools::colorf& a_back, 169 node& a_scene_graph, 169 node& a_scene_graph, 170 unsigned int a_width,u 170 unsigned int a_width,unsigned int a_height, 171 const std::string& a_f 171 const std::string& a_file,const std::string& a_format, 172 bool a_do_transparency 172 bool a_do_transparency,bool a_top_to_bottom, 173 const std::string& a_o 173 const std::string& a_opts_1,const std::string& a_opts_2) { 174 return tools::sg::write_paper(a_out,a_gl2ps_ 174 return tools::sg::write_paper(a_out,a_gl2ps_mgr,a_zb_mgr, 175 a_png_writer,a 175 a_png_writer,a_jpeg_writer, 176 a_back.r(),a_b 176 a_back.r(),a_back.g(),a_back.b(),a_back.a(), 177 a_scene_graph, 177 a_scene_graph,a_width,a_height,a_file,a_format, 178 a_do_transpare 178 a_do_transparency,a_top_to_bottom,a_opts_1,a_opts_2); 179 } 179 } 180 180 181 inline bool write_paper(std::ostream& a_out, 181 inline bool write_paper(std::ostream& a_out, 182 gl2ps_manager& a_gl2ps 182 gl2ps_manager& a_gl2ps_mgr,zb_manager& a_zb_mgr, 183 png_writer a_png_write 183 png_writer a_png_writer,jpeg_writer a_jpeg_writer, 184 const tools::colorf& a 184 const tools::colorf& a_back, 185 node& a_scene_graph, 185 node& a_scene_graph, 186 unsigned int a_width,u 186 unsigned int a_width,unsigned int a_height, 187 const std::string& a_f 187 const std::string& a_file,const std::string& a_format) { 188 return tools::sg::write_paper(a_out,a_gl2ps_ 188 return tools::sg::write_paper(a_out,a_gl2ps_mgr,a_zb_mgr, 189 a_png_writer,a 189 a_png_writer,a_jpeg_writer, 190 a_back.r(),a_b 190 a_back.r(),a_back.g(),a_back.b(),a_back.a(), 191 a_scene_graph, 191 a_scene_graph,a_width,a_height,a_file,a_format, 192 true,true,std: 192 true,true,std::string(),std::string()); 193 } 193 } 194 194 195 }} 195 }} 196 196 197 #endif 197 #endif