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 <tools/png> 84 // ... 84 // ... 85 // viewer.write_inzb_png(toolx::png::wri << 85 // viewer.write_inzb_png(tools::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.zbuffer().clear_color_buffer(0); 89 action.clear_depth_buffer(); << 89 action.add_color(m_clear_color.r(),m_clear_color.g(),m_clear_color.b(),m_clear_color.a()); >> 90 action.zbuffer().clear_depth_buffer(); 90 sg().render(action); 91 sg().render(action); 91 92 92 unsigned int bpp = 3; 93 unsigned int bpp = 3; 93 uchar* buffer = new unsigned char[a_width* 94 uchar* buffer = new unsigned char[a_width*a_height*bpp]; 94 if(!buffer) { 95 if(!buffer) { 95 m_out << "tools::sg::plots_viewer::write 96 m_out << "tools::sg::plots_viewer::write_inzb_png : can't alloc buffer." << std::endl; 96 return false; 97 return false; 97 } 98 } 98 unsigned char* pos = buffer; 99 unsigned char* pos = buffer; 99 float r,g,b; << 100 zb_action::VCol r,g,b; 100 for(unsigned int row=0;row<a_height;row++) 101 for(unsigned int row=0;row<a_height;row++) { 101 for(unsigned int col=0;col<a_width;col++ 102 for(unsigned int col=0;col<a_width;col++) { 102 zb_action::get_rgb(&action,col,a_heigh 103 zb_action::get_rgb(&action,col,a_height-row-1,r,g,b); 103 *pos = (uchar)(255.0F*r);pos++; 104 *pos = (uchar)(255.0F*r);pos++; 104 *pos = (uchar)(255.0F*g);pos++; 105 *pos = (uchar)(255.0F*g);pos++; 105 *pos = (uchar)(255.0F*b);pos++; 106 *pos = (uchar)(255.0F*b);pos++; 106 } 107 } 107 } 108 } 108 109 109 bool status = (*a_writer)(m_out,a_file,buf 110 bool status = (*a_writer)(m_out,a_file,buffer,a_width,a_height,bpp); 110 if(!status) { 111 if(!status) { 111 m_out << "tools::sg::plots_viewer::write 112 m_out << "tools::sg::plots_viewer::write_inzb_png : can't write " << a_file << "." << std::endl; 112 } 113 } 113 delete [] buffer; 114 delete [] buffer; 114 return status; 115 return status; 115 } 116 } 116 bool write_inzb_png(png_writer a_writer,cons 117 bool write_inzb_png(png_writer a_writer,const std::string& a_file) { 117 return write_inzb_png(a_writer,a_file,widt 118 return write_inzb_png(a_writer,a_file,width(),height()); 118 } 119 } 119 120 120 typedef bool (*jpeg_writer)(std::ostream&,co 121 typedef bool (*jpeg_writer)(std::ostream&,const std::string&, 121 unsigned char*,u 122 unsigned char*,unsigned int,unsigned int,unsigned int,int); 122 123 123 bool write_inzb_jpeg(jpeg_writer a_writer,co 124 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 : 125 // for example : 125 // #include <toolx/jpeg> << 126 // #include <tools/jpeg> 126 // ... 127 // ... 127 // viewer.write_inzb_jpeg(toolx::jpeg::w << 128 // viewer.write_inzb_jpeg(tools::jpeg::write,"out.jpeg"); 128 // 129 // 129 zb_action action(m_zb_mgr,m_out,a_width,a_ 130 zb_action action(m_zb_mgr,m_out,a_width,a_height); 130 action.clear_color_buffer(m_clear_color); << 131 action.zbuffer().clear_color_buffer(0); 131 action.clear_depth_buffer(); << 132 action.add_color(m_clear_color.r(),m_clear_color.g(),m_clear_color.b(),m_clear_color.a()); >> 133 action.zbuffer().clear_depth_buffer(); 132 sg().render(action); 134 sg().render(action); 133 135 134 unsigned int bpp = 3; 136 unsigned int bpp = 3; 135 uchar* buffer = new unsigned char[a_width* 137 uchar* buffer = new unsigned char[a_width*a_height*bpp]; 136 if(!buffer) { 138 if(!buffer) { 137 m_out << "tools::sg::plots_viewer::write 139 m_out << "tools::sg::plots_viewer::write_inzb_jpeg : can't alloc buffer." << std::endl; 138 return false; 140 return false; 139 } 141 } 140 unsigned char* pos = buffer; 142 unsigned char* pos = buffer; 141 float r,g,b; << 143 zb_action::VCol r,g,b; 142 for(unsigned int row=0;row<a_height;row++) 144 for(unsigned int row=0;row<a_height;row++) { 143 for(unsigned int col=0;col<a_width;col++ 145 for(unsigned int col=0;col<a_width;col++) { 144 zb_action::get_rgb(&action,col,a_heigh 146 zb_action::get_rgb(&action,col,a_height-row-1,r,g,b); 145 *pos = (uchar)(255.0F*r);pos++; 147 *pos = (uchar)(255.0F*r);pos++; 146 *pos = (uchar)(255.0F*g);pos++; 148 *pos = (uchar)(255.0F*g);pos++; 147 *pos = (uchar)(255.0F*b);pos++; 149 *pos = (uchar)(255.0F*b);pos++; 148 } 150 } 149 } 151 } 150 152 151 bool status = (*a_writer)(m_out,a_file,buf 153 bool status = (*a_writer)(m_out,a_file,buffer,a_width,a_height,bpp,a_quality); 152 if(!status) { 154 if(!status) { 153 m_out << "tools::sg::plots_viewer::write 155 m_out << "tools::sg::plots_viewer::write_inzb_jpeg : can't write " << a_file << "." << std::endl; 154 } 156 } 155 delete [] buffer; 157 delete [] buffer; 156 return status; 158 return status; 157 } 159 } 158 160 159 bool write_inzb_jpeg(jpeg_writer a_writer,co 161 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 162 return write_inzb_jpeg(a_writer,a_file,width(),height(),a_quality); 161 } 163 } 162 164 163 bool write_inzb_ps(const std::string& a_file 165 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_ 166 zb_action action(m_zb_mgr,m_out,a_width,a_height); 165 action.clear_color_buffer(m_clear_color); << 167 action.zbuffer().clear_color_buffer(0); 166 action.clear_depth_buffer(); << 168 action.add_color(m_clear_color.r(),m_clear_color.g(),m_clear_color.b(),m_clear_color.a()); >> 169 action.zbuffer().clear_depth_buffer(); 167 sg().render(action); 170 sg().render(action); 168 wps wps(m_out); 171 wps wps(m_out); 169 if(!wps.open_file(a_file,a_anonymous)) { 172 if(!wps.open_file(a_file,a_anonymous)) { 170 m_out << "tools::viewplot::write_inzb_ps 173 m_out << "tools::viewplot::write_inzb_ps : can't open " << a_file << "." << std::endl; 171 return false; 174 return false; 172 } 175 } 173 wps.PS_BEGIN_PAGE(); 176 wps.PS_BEGIN_PAGE(); 174 wps.PS_PAGE_SCALE(float(a_width),float(a_h 177 wps.PS_PAGE_SCALE(float(a_width),float(a_height)); 175 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,s 178 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,sg::zb_action::get_rgb,&action); 176 wps.PS_END_PAGE(); 179 wps.PS_END_PAGE(); 177 wps.close_file(); 180 wps.close_file(); 178 return true; 181 return true; 179 } 182 } 180 183 181 bool write_inzb_ps(const std::string& a_file 184 bool write_inzb_ps(const std::string& a_file,bool a_anonymous = false) { 182 return write_inzb_ps(a_file,width(),height 185 return write_inzb_ps(a_file,width(),height(),a_anonymous); 183 } 186 } 184 187 185 bool open_inzb_ps_file(const std::string& a_ 188 bool open_inzb_ps_file(const std::string& a_file,bool a_anonymous = false) { 186 if(!m_wps.open_file(a_file,a_anonymous)) { 189 if(!m_wps.open_file(a_file,a_anonymous)) { 187 m_out << "tools::plots_viewer::open_inzb 190 m_out << "tools::plots_viewer::open_inzb_ps_file : can't open " << a_file << "." << std::endl; 188 return false; 191 return false; 189 } 192 } 190 return true; 193 return true; 191 } 194 } 192 bool write_inzb_ps_page(unsigned int a_width 195 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 196 sg::zb_action action(m_zb_mgr,m_out,a_width,a_height); 194 action.clear_color_buffer(m_clear_color); << 197 action.zbuffer().clear_color_buffer(0); 195 action.clear_depth_buffer(); << 198 action.add_color(m_clear_color.r(),m_clear_color.g(),m_clear_color.b(),m_clear_color.a()); >> 199 action.zbuffer().clear_depth_buffer(); 196 sg().render(action); 200 sg().render(action); 197 m_wps.PS_BEGIN_PAGE(); 201 m_wps.PS_BEGIN_PAGE(); 198 m_wps.PS_PAGE_SCALE(float(a_width),float(a 202 m_wps.PS_PAGE_SCALE(float(a_width),float(a_height)); 199 m_wps.PS_IMAGE(a_width,a_height,wps::rgb_4 203 m_wps.PS_IMAGE(a_width,a_height,wps::rgb_4,sg::zb_action::get_rgb,&action); 200 m_wps.PS_END_PAGE(); 204 m_wps.PS_END_PAGE(); 201 return true; 205 return true; 202 } 206 } 203 bool write_inzb_ps_page() {return write_inzb 207 bool write_inzb_ps_page() {return write_inzb_ps_page(width(),height());} 204 bool close_inzb_ps_file() {return m_wps.clos 208 bool close_inzb_ps_file() {return m_wps.close_file();} 205 209 206 bool write_gl2ps(const std::string& a_file,i 210 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 211 gl2ps_action action(m_gl2ps_mgr,m_out,a_width,a_height); 208 action.clear_color(m_clear_color.r(),m_cle 212 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 213 if(!action.open(a_file,a_gl2ps_format)) return false; 210 sg().render(action); 214 sg().render(action); 211 action.close(); 215 action.close(); 212 return true; 216 return true; 213 } 217 } 214 bool write_gl2ps(const std::string& a_file,i 218 bool write_gl2ps(const std::string& a_file,int a_gl2ps_format) {return write_gl2ps(a_file,a_gl2ps_format,width(),height());} 215 219 216 protected: 220 protected: 217 void create_sg() { 221 void create_sg() { 218 m_sg.clear(); 222 m_sg.clear(); 219 223 220 m_camera.height = 1; 224 m_camera.height = 1; 221 float z = 10*1; 225 float z = 10*1; 222 m_camera.znear = 0.1f*z; 226 m_camera.znear = 0.1f*z; 223 m_camera.zfar = 10*z; //100*z induces prob 227 m_camera.zfar = 10*z; //100*z induces problems with lego rendering. 224 m_camera.position = vec3f(0,0,z); 228 m_camera.position = vec3f(0,0,z); 225 m_camera.orientation = rotf(vec3f(0,0,1),0 229 m_camera.orientation = rotf(vec3f(0,0,1),0); 226 m_camera.focal = z; 230 m_camera.focal = z; 227 m_sg.add(new noderef(m_camera)); 231 m_sg.add(new noderef(m_camera)); 228 232 229 m_sg.add(new noderef(m_plots)); 233 m_sg.add(new noderef(m_plots)); 230 } 234 } 231 235 232 protected: 236 protected: 233 sg::zb_manager m_zb_mgr; 237 sg::zb_manager m_zb_mgr; 234 sg::gl2ps_manager m_gl2ps_mgr; 238 sg::gl2ps_manager m_gl2ps_mgr; 235 ortho m_camera; 239 ortho m_camera; 236 sg::plots m_plots; 240 sg::plots m_plots; 237 wps m_wps; 241 wps m_wps; 238 }; 242 }; 239 243 240 }} 244 }} 241 245 242 #endif 246 #endif 243 247