Warning, /include/Geant4/tools/sg/plots_viewer is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_sg_plots_viewer
0005 #define tools_sg_plots_viewer
0006
0007 // used in geant4 offscreen plotting.
0008
0009 #include "viewer"
0010
0011 #include "ortho"
0012 #include "plots"
0013
0014 #include "h2plot_cp"
0015 #include "f2plot"
0016 #include "xy2plot"
0017 #include "fit2plot"
0018 #include "cloud2plot_cp"
0019
0020 #include "zb_action"
0021 #include "../wps"
0022
0023 #include "gl2ps_action"
0024
0025 namespace tools {
0026 namespace sg {
0027
0028 class plots_viewer : public viewer {
0029 TOOLS_HEADER(plots_viewer,tools::sg::plots_viewer,viewer)
0030 public:
0031 virtual void set_size(unsigned int a_width,unsigned int a_height) {
0032 parent::set_size(a_width,a_height);
0033 m_plots.adjust_size(a_width,a_height);
0034 }
0035 public:
0036 plots_viewer(std::ostream& a_out,const base_freetype& a_ttf,
0037 unsigned int a_cols = 1,unsigned int a_rows = 1,
0038 unsigned int a_width = 500,unsigned int a_height = 500)
0039 :parent(a_out,a_width,a_height)
0040 ,m_plots(a_ttf)
0041 ,m_wps(a_out)
0042 {
0043 create_sg();
0044 m_plots.cols = a_cols;
0045 m_plots.rows = a_rows;
0046 m_plots.adjust_size(width(),height());
0047 }
0048 virtual ~plots_viewer() {
0049 //WARNING : nodes may refer m_zb_mgr,m_gl2ps_mgr (to handle gstos/texs), then we have to delete them first.
0050 m_sg.clear();
0051 m_plots.clear_sg();
0052 }
0053 public:
0054 plots_viewer(const plots_viewer& a_from)
0055 :parent(a_from)
0056 ,m_camera(a_from.m_camera)
0057 ,m_plots(a_from.m_plots)
0058 ,m_wps(a_from.m_out)
0059 {
0060 create_sg();
0061 m_plots.adjust_size(width(),height());
0062 }
0063 plots_viewer& operator=(const plots_viewer& a_from){
0064 parent::operator=(a_from);
0065 m_camera = a_from.m_camera;
0066 m_plots = a_from.m_plots;
0067 create_sg();
0068 m_plots.adjust_size(width(),height());
0069 return *this;
0070 }
0071 public:
0072 #include <tools/plotter_common.icc>
0073
0074 sg::zb_manager& zb_manager() {return m_zb_mgr;}
0075 sg::gl2ps_manager& gl2ps_manager() {return m_gl2ps_mgr;}
0076
0077 public:
0078 typedef bool (*png_writer)(std::ostream&,const std::string&,
0079 unsigned char*,unsigned int,unsigned int,unsigned int);
0080
0081 bool write_inzb_png(png_writer a_writer,const std::string& a_file,unsigned int a_width,unsigned int a_height) {
0082 // for example :
0083 // #include <toolx/png>
0084 // ...
0085 // viewer.write_inzb_png(toolx::png::write,"out.png");
0086 //
0087 zb_action action(m_zb_mgr,m_out,a_width,a_height);
0088 action.clear_color_buffer(m_clear_color);
0089 action.clear_depth_buffer();
0090 sg().render(action);
0091
0092 unsigned int bpp = 3;
0093 uchar* buffer = new unsigned char[a_width*a_height*bpp];
0094 if(!buffer) {
0095 m_out << "tools::sg::plots_viewer::write_inzb_png : can't alloc buffer." << std::endl;
0096 return false;
0097 }
0098 unsigned char* pos = buffer;
0099 float r,g,b;
0100 for(unsigned int row=0;row<a_height;row++) {
0101 for(unsigned int col=0;col<a_width;col++) {
0102 zb_action::get_rgb(&action,col,a_height-row-1,r,g,b);
0103 *pos = (uchar)(255.0F*r);pos++;
0104 *pos = (uchar)(255.0F*g);pos++;
0105 *pos = (uchar)(255.0F*b);pos++;
0106 }
0107 }
0108
0109 bool status = (*a_writer)(m_out,a_file,buffer,a_width,a_height,bpp);
0110 if(!status) {
0111 m_out << "tools::sg::plots_viewer::write_inzb_png : can't write " << a_file << "." << std::endl;
0112 }
0113 delete [] buffer;
0114 return status;
0115 }
0116 bool write_inzb_png(png_writer a_writer,const std::string& a_file) {
0117 return write_inzb_png(a_writer,a_file,width(),height());
0118 }
0119
0120 typedef bool (*jpeg_writer)(std::ostream&,const std::string&,
0121 unsigned char*,unsigned int,unsigned int,unsigned int,int);
0122
0123 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) {
0124 // for example :
0125 // #include <toolx/jpeg>
0126 // ...
0127 // viewer.write_inzb_jpeg(toolx::jpeg::write,"out.jpeg");
0128 //
0129 zb_action action(m_zb_mgr,m_out,a_width,a_height);
0130 action.clear_color_buffer(m_clear_color);
0131 action.clear_depth_buffer();
0132 sg().render(action);
0133
0134 unsigned int bpp = 3;
0135 uchar* buffer = new unsigned char[a_width*a_height*bpp];
0136 if(!buffer) {
0137 m_out << "tools::sg::plots_viewer::write_inzb_jpeg : can't alloc buffer." << std::endl;
0138 return false;
0139 }
0140 unsigned char* pos = buffer;
0141 float r,g,b;
0142 for(unsigned int row=0;row<a_height;row++) {
0143 for(unsigned int col=0;col<a_width;col++) {
0144 zb_action::get_rgb(&action,col,a_height-row-1,r,g,b);
0145 *pos = (uchar)(255.0F*r);pos++;
0146 *pos = (uchar)(255.0F*g);pos++;
0147 *pos = (uchar)(255.0F*b);pos++;
0148 }
0149 }
0150
0151 bool status = (*a_writer)(m_out,a_file,buffer,a_width,a_height,bpp,a_quality);
0152 if(!status) {
0153 m_out << "tools::sg::plots_viewer::write_inzb_jpeg : can't write " << a_file << "." << std::endl;
0154 }
0155 delete [] buffer;
0156 return status;
0157 }
0158
0159 bool write_inzb_jpeg(jpeg_writer a_writer,const std::string& a_file,int a_quality = 100) {
0160 return write_inzb_jpeg(a_writer,a_file,width(),height(),a_quality);
0161 }
0162
0163 bool write_inzb_ps(const std::string& a_file,unsigned int a_width,unsigned int a_height,bool a_anonymous = false) {
0164 zb_action action(m_zb_mgr,m_out,a_width,a_height);
0165 action.clear_color_buffer(m_clear_color);
0166 action.clear_depth_buffer();
0167 sg().render(action);
0168 wps wps(m_out);
0169 if(!wps.open_file(a_file,a_anonymous)) {
0170 m_out << "tools::viewplot::write_inzb_ps : can't open " << a_file << "." << std::endl;
0171 return false;
0172 }
0173 wps.PS_BEGIN_PAGE();
0174 wps.PS_PAGE_SCALE(float(a_width),float(a_height));
0175 wps.PS_IMAGE(a_width,a_height,wps::rgb_4,sg::zb_action::get_rgb,&action);
0176 wps.PS_END_PAGE();
0177 wps.close_file();
0178 return true;
0179 }
0180
0181 bool write_inzb_ps(const std::string& a_file,bool a_anonymous = false) {
0182 return write_inzb_ps(a_file,width(),height(),a_anonymous);
0183 }
0184
0185 bool open_inzb_ps_file(const std::string& a_file,bool a_anonymous = false) {
0186 if(!m_wps.open_file(a_file,a_anonymous)) {
0187 m_out << "tools::plots_viewer::open_inzb_ps_file : can't open " << a_file << "." << std::endl;
0188 return false;
0189 }
0190 return true;
0191 }
0192 bool write_inzb_ps_page(unsigned int a_width,unsigned int a_height) {
0193 sg::zb_action action(m_zb_mgr,m_out,a_width,a_height);
0194 action.clear_color_buffer(m_clear_color);
0195 action.clear_depth_buffer();
0196 sg().render(action);
0197 m_wps.PS_BEGIN_PAGE();
0198 m_wps.PS_PAGE_SCALE(float(a_width),float(a_height));
0199 m_wps.PS_IMAGE(a_width,a_height,wps::rgb_4,sg::zb_action::get_rgb,&action);
0200 m_wps.PS_END_PAGE();
0201 return true;
0202 }
0203 bool write_inzb_ps_page() {return write_inzb_ps_page(width(),height());}
0204 bool close_inzb_ps_file() {return m_wps.close_file();}
0205
0206 bool write_gl2ps(const std::string& a_file,int a_gl2ps_format,unsigned int a_width,unsigned int a_height) {
0207 gl2ps_action action(m_gl2ps_mgr,m_out,a_width,a_height);
0208 action.clear_color(m_clear_color.r(),m_clear_color.g(),m_clear_color.b(),m_clear_color.a());
0209 if(!action.open(a_file,a_gl2ps_format)) return false;
0210 sg().render(action);
0211 action.close();
0212 return true;
0213 }
0214 bool write_gl2ps(const std::string& a_file,int a_gl2ps_format) {return write_gl2ps(a_file,a_gl2ps_format,width(),height());}
0215
0216 protected:
0217 void create_sg() {
0218 m_sg.clear();
0219
0220 m_camera.height = 1;
0221 float z = 10*1;
0222 m_camera.znear = 0.1f*z;
0223 m_camera.zfar = 10*z; //100*z induces problems with lego rendering.
0224 m_camera.position = vec3f(0,0,z);
0225 m_camera.orientation = rotf(vec3f(0,0,1),0);
0226 m_camera.focal = z;
0227 m_sg.add(new noderef(m_camera));
0228
0229 m_sg.add(new noderef(m_plots));
0230 }
0231
0232 protected:
0233 sg::zb_manager m_zb_mgr;
0234 sg::gl2ps_manager m_gl2ps_mgr;
0235 ortho m_camera;
0236 sg::plots m_plots;
0237 wps m_wps;
0238 };
0239
0240 }}
0241
0242 #endif
0243