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] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

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