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 size_t sz; 129 if(!action.get_rgbas(a_top_to_bottom,buffe << 129 unsigned char* buffer = action.get_rgbas(sz,a_top_to_bottom); >> 130 if(!buffer) { 130 a_out << "tools::sg::write_paper : can't 131 a_out << "tools::sg::write_paper : can't get rgba image." << std::endl; 131 return false; 132 return false; 132 } 133 } 133 134 134 if(!a_png_writer(a_out,a_file,vec_data(buf << 135 if(!a_png_writer(a_out,a_file,buffer,a_width,a_height,4)) { 135 a_out << "tools::sg::write_paper : tools 136 a_out << "tools::sg::write_paper : tools::png::write() failed." << std::endl; >> 137 delete [] buffer; 136 return false; 138 return false; 137 } 139 } >> 140 delete [] buffer; 138 return true; 141 return true; 139 } 142 } 140 143 141 if((a_format=="zb_jpeg")||(a_format=="zb_jpg 144 if((a_format=="zb_jpeg")||(a_format=="zb_jpg")||(a_format=="inzb_jpeg")) { 142 145 143 if(!a_jpeg_writer) { 146 if(!a_jpeg_writer) { 144 a_out << "tools::sg::write_paper : no jp 147 a_out << "tools::sg::write_paper : no jpeg_writer given." << std::endl; 145 return false; 148 return false; 146 } 149 } 147 150 148 std::vector<unsigned char> buffer; << 151 size_t sz; 149 if(!action.get_rgbs(a_top_to_bottom,buffer << 152 unsigned char* buffer = action.get_rgbs(sz,a_top_to_bottom); >> 153 if(!buffer) { 150 a_out << "tools::sg::write_paper : can't 154 a_out << "tools::sg::write_paper : can't get rgb image." << std::endl; 151 return false; 155 return false; 152 } 156 } 153 157 154 if(!a_jpeg_writer(a_out,a_file,vec_data(bu << 158 if(!a_jpeg_writer(a_out,a_file,buffer,a_width,a_height,3,100)) { 155 a_out << "tools::sg::write_paper : tools 159 a_out << "tools::sg::write_paper : tools::jpeg::write() failed." << std::endl; >> 160 delete [] buffer; 156 return false; 161 return false; 157 } 162 } >> 163 delete [] buffer; 158 return true; 164 return true; 159 } 165 } 160 166 161 a_out << "tools::sg::write_paper : unknown f 167 a_out << "tools::sg::write_paper : unknown format " << a_format << std::endl; 162 return false; 168 return false; 163 } 169 } 164 170 165 inline bool write_paper(std::ostream& a_out, 171 inline bool write_paper(std::ostream& a_out, 166 gl2ps_manager& a_gl2ps 172 gl2ps_manager& a_gl2ps_mgr,zb_manager& a_zb_mgr, 167 png_writer a_png_write 173 png_writer a_png_writer,jpeg_writer a_jpeg_writer, 168 const tools::colorf& a 174 const tools::colorf& a_back, 169 node& a_scene_graph, 175 node& a_scene_graph, 170 unsigned int a_width,u 176 unsigned int a_width,unsigned int a_height, 171 const std::string& a_f 177 const std::string& a_file,const std::string& a_format, 172 bool a_do_transparency 178 bool a_do_transparency,bool a_top_to_bottom, 173 const std::string& a_o 179 const std::string& a_opts_1,const std::string& a_opts_2) { 174 return tools::sg::write_paper(a_out,a_gl2ps_ 180 return tools::sg::write_paper(a_out,a_gl2ps_mgr,a_zb_mgr, 175 a_png_writer,a 181 a_png_writer,a_jpeg_writer, 176 a_back.r(),a_b 182 a_back.r(),a_back.g(),a_back.b(),a_back.a(), 177 a_scene_graph, 183 a_scene_graph,a_width,a_height,a_file,a_format, 178 a_do_transpare 184 a_do_transparency,a_top_to_bottom,a_opts_1,a_opts_2); 179 } 185 } 180 186 181 inline bool write_paper(std::ostream& a_out, 187 inline bool write_paper(std::ostream& a_out, 182 gl2ps_manager& a_gl2ps 188 gl2ps_manager& a_gl2ps_mgr,zb_manager& a_zb_mgr, 183 png_writer a_png_write 189 png_writer a_png_writer,jpeg_writer a_jpeg_writer, 184 const tools::colorf& a 190 const tools::colorf& a_back, 185 node& a_scene_graph, 191 node& a_scene_graph, 186 unsigned int a_width,u 192 unsigned int a_width,unsigned int a_height, 187 const std::string& a_f 193 const std::string& a_file,const std::string& a_format) { 188 return tools::sg::write_paper(a_out,a_gl2ps_ 194 return tools::sg::write_paper(a_out,a_gl2ps_mgr,a_zb_mgr, 189 a_png_writer,a 195 a_png_writer,a_jpeg_writer, 190 a_back.r(),a_b 196 a_back.r(),a_back.g(),a_back.b(),a_back.a(), 191 a_scene_graph, 197 a_scene_graph,a_width,a_height,a_file,a_format, 192 true,true,std: 198 true,true,std::string(),std::string()); 193 } 199 } 194 200 195 }} 201 }} 196 202 197 #endif 203 #endif