Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, 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_plots_viewer 4 #ifndef tools_sg_plots_viewer 5 #define tools_sg_plots_viewer 5 #define tools_sg_plots_viewer 6 6 7 // used in geant4 offscreen plotting. 7 // used in geant4 offscreen plotting. 8 8 9 #include "viewer" 9 #include "viewer" 10 10 11 #include "ortho" 11 #include "ortho" 12 #include "plots" 12 #include "plots" 13 13 14 #include "h2plot_cp" 14 #include "h2plot_cp" 15 #include "f2plot" 15 #include "f2plot" 16 #include "xy2plot" 16 #include "xy2plot" 17 #include "fit2plot" 17 #include "fit2plot" 18 #include "cloud2plot_cp" 18 #include "cloud2plot_cp" 19 19 20 #include "zb_action" 20 #include "zb_action" 21 #include "../wps" 21 #include "../wps" 22 22 23 #include "gl2ps_action" 23 #include "gl2ps_action" 24 24 25 namespace tools { 25 namespace tools { 26 namespace sg { 26 namespace sg { 27 27 28 class plots_viewer : public viewer { 28 class plots_viewer : public viewer { 29 TOOLS_HEADER(plots_viewer,tools::sg::plots_v 29 TOOLS_HEADER(plots_viewer,tools::sg::plots_viewer,viewer) 30 public: 30 public: 31 virtual void set_size(unsigned int a_width,u 31 virtual void set_size(unsigned int a_width,unsigned int a_height) { 32 parent::set_size(a_width,a_height); 32 parent::set_size(a_width,a_height); 33 m_plots.adjust_size(a_width,a_height); 33 m_plots.adjust_size(a_width,a_height); 34 } 34 } 35 public: 35 public: 36 plots_viewer(std::ostream& a_out,const base_ 36 plots_viewer(std::ostream& a_out,const base_freetype& a_ttf, 37 unsigned int a_cols = 1,unsigne 37 unsigned int a_cols = 1,unsigned int a_rows = 1, 38 unsigned int a_width = 500,unsi 38 unsigned int a_width = 500,unsigned int a_height = 500) 39 :parent(a_out,a_width,a_height) 39 :parent(a_out,a_width,a_height) 40 ,m_plots(a_ttf) 40 ,m_plots(a_ttf) 41 ,m_wps(a_out) 41 ,m_wps(a_out) 42 { 42 { 43 create_sg(); 43 create_sg(); 44 m_plots.cols = a_cols; 44 m_plots.cols = a_cols; 45 m_plots.rows = a_rows; 45 m_plots.rows = a_rows; 46 m_plots.adjust_size(width(),height()); 46 m_plots.adjust_size(width(),height()); 47 } 47 } 48 virtual ~plots_viewer() { 48 virtual ~plots_viewer() { 49 //WARNING : nodes may refer m_zb_mgr,m_gl2 49 //WARNING : nodes may refer m_zb_mgr,m_gl2ps_mgr (to handle gstos/texs), then we have to delete them first. 50 m_sg.clear(); 50 m_sg.clear(); 51 m_plots.clear_sg(); 51 m_plots.clear_sg(); 52 } 52 } 53 public: 53 public: 54 plots_viewer(const plots_viewer& a_from) 54 plots_viewer(const plots_viewer& a_from) 55 :parent(a_from) 55 :parent(a_from) 56 ,m_camera(a_from.m_camera) 56 ,m_camera(a_from.m_camera) 57 ,m_plots(a_from.m_plots) 57 ,m_plots(a_from.m_plots) 58 ,m_wps(a_from.m_out) 58 ,m_wps(a_from.m_out) 59 { 59 { 60 create_sg(); 60 create_sg(); 61 m_plots.adjust_size(width(),height()); 61 m_plots.adjust_size(width(),height()); 62 } 62 } 63 plots_viewer& operator=(const plots_viewer& 63 plots_viewer& operator=(const plots_viewer& a_from){ 64 parent::operator=(a_from); 64 parent::operator=(a_from); 65 m_camera = a_from.m_camera; 65 m_camera = a_from.m_camera; 66 m_plots = a_from.m_plots; 66 m_plots = a_from.m_plots; 67 create_sg(); 67 create_sg(); 68 m_plots.adjust_size(width(),height()); 68 m_plots.adjust_size(width(),height()); 69 return *this; 69 return *this; 70 } 70 } 71 public: 71 public: 72 #include <tools/plotter_common.icc> 72 #include <tools/plotter_common.icc> 73 73 74 sg::zb_manager& zb_manager() {return m_zb_mg 74 sg::zb_manager& zb_manager() {return m_zb_mgr;} 75 sg::gl2ps_manager& gl2ps_manager() {return m 75 sg::gl2ps_manager& gl2ps_manager() {return m_gl2ps_mgr;} 76 76 77 public: 77 public: 78 typedef bool (*png_writer)(std::ostream&,con 78 typedef bool (*png_writer)(std::ostream&,const std::string&, 79 unsigned char*,un 79 unsigned char*,unsigned int,unsigned int,unsigned int); 80 80 81 bool write_inzb_png(png_writer a_writer,cons 81 bool write_inzb_png(png_writer a_writer,const std::string& a_file,unsigned int a_width,unsigned int a_height) { 82 // for example : 82 // for example : 83 // #include <toolx/png> 83 // #include <toolx/png> 84 // ... 84 // ... 85 // viewer.write_inzb_png(toolx::png::wri 85 // viewer.write_inzb_png(toolx::png::write,"out.png"); 86 // 86 // 87 zb_action action(m_zb_mgr,m_out,a_width,a_ 87 zb_action action(m_zb_mgr,m_out,a_width,a_height); 88 action.clear_color_buffer(m_clear_color); 88 action.clear_color_buffer(m_clear_color); 89 action.clear_depth_buffer(); 89 action.clear_depth_buffer(); 90 sg().render(action); 90 sg().render(action); 91 91 92 unsigned int bpp = 3; 92 unsigned int bpp = 3; 93 uchar* buffer = new unsigned char[a_width* 93 uchar* buffer = new unsigned char[a_width*a_height*bpp]; 94 if(!buffer) { 94 if(!buffer) { 95 m_out << "tools::sg::plots_viewer::write 95 m_out << "tools::sg::plots_viewer::write_inzb_png : can't alloc buffer." << std::endl; 96 return false; 96 return false; 97 } 97 } 98 unsigned char* pos = buffer; 98 unsigned char* pos = buffer; 99 float r,g,b; 99 float r,g,b; 100 for(unsigned int row=0;row<a_height;row++) 100 for(unsigned int row=0;row<a_height;row++) { 101 for(unsigned int col=0;col<a_width;col++ 101 for(unsigned int col=0;col<a_width;col++) { 102 zb_action::get_rgb(&action,col,a_heigh 102 zb_action::get_rgb(&action,col,a_height-row-1,r,g,b); 103 *pos = (uchar)(255.0F*r);pos++; 103 *pos = (uchar)(255.0F*r);pos++; 104 *pos = (uchar)(255.0F*g);pos++; 104 *pos = (uchar)(255.0F*g);pos++; 105 *pos = (uchar)(255.0F*b);pos++; 105 *pos = (uchar)(255.0F*b);pos++; 106 } 106 } 107 } 107 } 108 108 109 bool status = (*a_writer)(m_out,a_file,buf 109 bool status = (*a_writer)(m_out,a_file,buffer,a_width,a_height,bpp); 110 if(!status) { 110 if(!status) { 111 m_out << "tools::sg::plots_viewer::write 111 m_out << "tools::sg::plots_viewer::write_inzb_png : can't write " << a_file << "." << std::endl; 112 } 112 } 113 delete [] buffer; 113 delete [] buffer; 114 return status; 114 return status; 115 } 115 } 116 bool write_inzb_png(png_writer a_writer,cons 116 bool write_inzb_png(png_writer a_writer,const std::string& a_file) { 117 return write_inzb_png(a_writer,a_file,widt 117 return write_inzb_png(a_writer,a_file,width(),height()); 118 } 118 } 119 119 120 typedef bool (*jpeg_writer)(std::ostream&,co 120 typedef bool (*jpeg_writer)(std::ostream&,const std::string&, 121 unsigned char*,u 121 unsigned char*,unsigned int,unsigned int,unsigned int,int); 122 122 123 bool write_inzb_jpeg(jpeg_writer a_writer,co 123 bool write_inzb_jpeg(jpeg_writer a_writer,const std::string& a_file,unsigned int a_width,unsigned int a_height,int a_quality = 100) { 124 // for example : 124 // for example : 125 // #include <toolx/jpeg> 125 // #include <toolx/jpeg> 126 // ... 126 // ... 127 // viewer.write_inzb_jpeg(toolx::jpeg::w 127 // viewer.write_inzb_jpeg(toolx::jpeg::write,"out.jpeg"); 128 // 128 // 129 zb_action action(m_zb_mgr,m_out,a_width,a_ 129 zb_action action(m_zb_mgr,m_out,a_width,a_height); 130 action.clear_color_buffer(m_clear_color); 130 action.clear_color_buffer(m_clear_color); 131 action.clear_depth_buffer(); 131 action.clear_depth_buffer(); 132 sg().render(action); 132 sg().render(action); 133 133 134 unsigned int bpp = 3; 134 unsigned int bpp = 3; 135 uchar* buffer = new unsigned char[a_width* 135 uchar* buffer = new unsigned char[a_width*a_height*bpp]; 136 if(!buffer) { 136 if(!buffer) { 137 m_out << "tools::sg::plots_viewer::write 137 m_out << "tools::sg::plots_viewer::write_inzb_jpeg : can't alloc buffer." << std::endl; 138 return false; 138 return false; 139 } 139 } 140 unsigned char* pos = buffer; 140 unsigned char* pos = buffer; 141 float r,g,b; 141 float r,g,b; 142 for(unsigned int row=0;row<a_height;row++) 142 for(unsigned int row=0;row<a_height;row++) { 143 for(unsigned int col=0;col<a_width;col++ 143 for(unsigned int col=0;col<a_width;col++) { 144 zb_action::get_rgb(&action,col,a_heigh 144 zb_action::get_rgb(&action,col,a_height-row-1,r,g,b); 145 *pos = (uchar)(255.0F*r);pos++; 145 *pos = (uchar)(255.0F*r);pos++; 146 *pos = (uchar)(255.0F*g);pos++; 146 *pos = (uchar)(255.0F*g);pos++; 147 *pos = (uchar)(255.0F*b);pos++; 147 *pos = (uchar)(255.0F*b);pos++; 148 } 148 } 149 } 149 } 150 150 151 bool status = (*a_writer)(m_out,a_file,buf 151 bool status = (*a_writer)(m_out,a_file,buffer,a_width,a_height,bpp,a_quality); 152 if(!status) { 152 if(!status) { 153 m_out << "tools::sg::plots_viewer::write 153 m_out << "tools::sg::plots_viewer::write_inzb_jpeg : can't write " << a_file << "." << std::endl; 154 } 154 } 155 delete [] buffer; 155 delete [] buffer; 156 return status; 156 return status; 157 } 157 } 158 158 159 bool write_inzb_jpeg(jpeg_writer a_writer,co 159 bool write_inzb_jpeg(jpeg_writer a_writer,const std::string& a_file,int a_quality = 100) { 160 return write_inzb_jpeg(a_writer,a_file,wid 160 return write_inzb_jpeg(a_writer,a_file,width(),height(),a_quality); 161 } 161 } 162 162 163 bool write_inzb_ps(const std::string& a_file 163 bool write_inzb_ps(const std::string& a_file,unsigned int a_width,unsigned int a_height,bool a_anonymous = false) { 164 zb_action action(m_zb_mgr,m_out,a_width,a_ 164 zb_action action(m_zb_mgr,m_out,a_width,a_height); 165 action.clear_color_buffer(m_clear_color); 165 action.clear_color_buffer(m_clear_color); 166 action.clear_depth_buffer(); 166 action.clear_depth_buffer(); 167 sg().render(action); 167 sg().render(action); 168 wps wps(m_out); 168 wps wps(m_out); 169 if(!wps.open_file(a_file,a_anonymous)) { 169 if(!wps.open_file(a_file,a_anonymous)) { 170 m_out << "tools::viewplot::write_inzb_ps 170 m_out << "tools::viewplot::write_inzb_ps : can't open " << a_file << "." << std::endl; 171 return false; 171 return false; 172 } 172 } 173 wps.PS_BEGIN_PAGE(); 173 wps.PS_BEGIN_PAGE(); 174 wps.PS_PAGE_SCALE(float(a_width),float(a_h 174 wps.PS_PAGE_SCALE(float(a_width),float(a_height)); 175 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,s 175 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,sg::zb_action::get_rgb,&action); 176 wps.PS_END_PAGE(); 176 wps.PS_END_PAGE(); 177 wps.close_file(); 177 wps.close_file(); 178 return true; 178 return true; 179 } 179 } 180 180 181 bool write_inzb_ps(const std::string& a_file 181 bool write_inzb_ps(const std::string& a_file,bool a_anonymous = false) { 182 return write_inzb_ps(a_file,width(),height 182 return write_inzb_ps(a_file,width(),height(),a_anonymous); 183 } 183 } 184 184 185 bool open_inzb_ps_file(const std::string& a_ 185 bool open_inzb_ps_file(const std::string& a_file,bool a_anonymous = false) { 186 if(!m_wps.open_file(a_file,a_anonymous)) { 186 if(!m_wps.open_file(a_file,a_anonymous)) { 187 m_out << "tools::plots_viewer::open_inzb 187 m_out << "tools::plots_viewer::open_inzb_ps_file : can't open " << a_file << "." << std::endl; 188 return false; 188 return false; 189 } 189 } 190 return true; 190 return true; 191 } 191 } 192 bool write_inzb_ps_page(unsigned int a_width 192 bool write_inzb_ps_page(unsigned int a_width,unsigned int a_height) { 193 sg::zb_action action(m_zb_mgr,m_out,a_widt 193 sg::zb_action action(m_zb_mgr,m_out,a_width,a_height); 194 action.clear_color_buffer(m_clear_color); 194 action.clear_color_buffer(m_clear_color); 195 action.clear_depth_buffer(); 195 action.clear_depth_buffer(); 196 sg().render(action); 196 sg().render(action); 197 m_wps.PS_BEGIN_PAGE(); 197 m_wps.PS_BEGIN_PAGE(); 198 m_wps.PS_PAGE_SCALE(float(a_width),float(a 198 m_wps.PS_PAGE_SCALE(float(a_width),float(a_height)); 199 m_wps.PS_IMAGE(a_width,a_height,wps::rgb_4 199 m_wps.PS_IMAGE(a_width,a_height,wps::rgb_4,sg::zb_action::get_rgb,&action); 200 m_wps.PS_END_PAGE(); 200 m_wps.PS_END_PAGE(); 201 return true; 201 return true; 202 } 202 } 203 bool write_inzb_ps_page() {return write_inzb 203 bool write_inzb_ps_page() {return write_inzb_ps_page(width(),height());} 204 bool close_inzb_ps_file() {return m_wps.clos 204 bool close_inzb_ps_file() {return m_wps.close_file();} 205 205 206 bool write_gl2ps(const std::string& a_file,i 206 bool write_gl2ps(const std::string& a_file,int a_gl2ps_format,unsigned int a_width,unsigned int a_height) { 207 gl2ps_action action(m_gl2ps_mgr,m_out,a_wi 207 gl2ps_action action(m_gl2ps_mgr,m_out,a_width,a_height); 208 action.clear_color(m_clear_color.r(),m_cle 208 action.clear_color(m_clear_color.r(),m_clear_color.g(),m_clear_color.b(),m_clear_color.a()); 209 if(!action.open(a_file,a_gl2ps_format)) re 209 if(!action.open(a_file,a_gl2ps_format)) return false; 210 sg().render(action); 210 sg().render(action); 211 action.close(); 211 action.close(); 212 return true; 212 return true; 213 } 213 } 214 bool write_gl2ps(const std::string& a_file,i 214 bool write_gl2ps(const std::string& a_file,int a_gl2ps_format) {return write_gl2ps(a_file,a_gl2ps_format,width(),height());} 215 215 216 protected: 216 protected: 217 void create_sg() { 217 void create_sg() { 218 m_sg.clear(); 218 m_sg.clear(); 219 219 220 m_camera.height = 1; 220 m_camera.height = 1; 221 float z = 10*1; 221 float z = 10*1; 222 m_camera.znear = 0.1f*z; 222 m_camera.znear = 0.1f*z; 223 m_camera.zfar = 10*z; //100*z induces prob 223 m_camera.zfar = 10*z; //100*z induces problems with lego rendering. 224 m_camera.position = vec3f(0,0,z); 224 m_camera.position = vec3f(0,0,z); 225 m_camera.orientation = rotf(vec3f(0,0,1),0 225 m_camera.orientation = rotf(vec3f(0,0,1),0); 226 m_camera.focal = z; 226 m_camera.focal = z; 227 m_sg.add(new noderef(m_camera)); 227 m_sg.add(new noderef(m_camera)); 228 228 229 m_sg.add(new noderef(m_plots)); 229 m_sg.add(new noderef(m_plots)); 230 } 230 } 231 231 232 protected: 232 protected: 233 sg::zb_manager m_zb_mgr; 233 sg::zb_manager m_zb_mgr; 234 sg::gl2ps_manager m_gl2ps_mgr; 234 sg::gl2ps_manager m_gl2ps_mgr; 235 ortho m_camera; 235 ortho m_camera; 236 sg::plots m_plots; 236 sg::plots m_plots; 237 wps m_wps; 237 wps m_wps; 238 }; 238 }; 239 239 240 }} 240 }} 241 241 242 #endif 242 #endif 243 243