g4tools  5.4.0
plots_viewer
Go to the documentation of this file.
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 {
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;
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();
62  }
64  parent::operator=(a_from);
65  m_camera = a_from.m_camera;
66  m_plots = a_from.m_plots;
67  create_sg();
69  return *this;
70  }
71 public:
72 #include <tools/plotter_common.icc>
73 
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 <tools/png>
84  // ...
85  // viewer.write_inzb_png(tools::png::write,"out.png");
86  //
87  zb_action action(m_zb_mgr,m_out,a_width,a_height);
88  action.zbuffer().clear_color_buffer(0);
90  action.zbuffer().clear_depth_buffer();
91  sg().render(action);
92 
93  unsigned int bpp = 3;
94  uchar* buffer = new unsigned char[a_width*a_height*bpp];
95  if(!buffer) {
96  m_out << "tools::sg::plots_viewer::write_inzb_png : can't alloc buffer." << std::endl;
97  return false;
98  }
99  unsigned char* pos = buffer;
100  zb_action::VCol r,g,b;
101  for(unsigned int row=0;row<a_height;row++) {
102  for(unsigned int col=0;col<a_width;col++) {
103  zb_action::get_rgb(&action,col,a_height-row-1,r,g,b);
104  *pos = (uchar)(255.0F*r);pos++;
105  *pos = (uchar)(255.0F*g);pos++;
106  *pos = (uchar)(255.0F*b);pos++;
107  }
108  }
109 
110  bool status = (*a_writer)(m_out,a_file,buffer,a_width,a_height,bpp);
111  if(!status) {
112  m_out << "tools::sg::plots_viewer::write_inzb_png : can't write " << a_file << "." << std::endl;
113  }
114  delete [] buffer;
115  return status;
116  }
117  bool write_inzb_png(png_writer a_writer,const std::string& a_file) {
118  return write_inzb_png(a_writer,a_file,width(),height());
119  }
120 
121  typedef bool (*jpeg_writer)(std::ostream&,const std::string&,
122  unsigned char*,unsigned int,unsigned int,unsigned int,int);
123 
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) {
125  // for example :
126  // #include <tools/jpeg>
127  // ...
128  // viewer.write_inzb_jpeg(tools::jpeg::write,"out.jpeg");
129  //
130  zb_action action(m_zb_mgr,m_out,a_width,a_height);
131  action.zbuffer().clear_color_buffer(0);
133  action.zbuffer().clear_depth_buffer();
134  sg().render(action);
135 
136  unsigned int bpp = 3;
137  uchar* buffer = new unsigned char[a_width*a_height*bpp];
138  if(!buffer) {
139  m_out << "tools::sg::plots_viewer::write_inzb_jpeg : can't alloc buffer." << std::endl;
140  return false;
141  }
142  unsigned char* pos = buffer;
143  zb_action::VCol r,g,b;
144  for(unsigned int row=0;row<a_height;row++) {
145  for(unsigned int col=0;col<a_width;col++) {
146  zb_action::get_rgb(&action,col,a_height-row-1,r,g,b);
147  *pos = (uchar)(255.0F*r);pos++;
148  *pos = (uchar)(255.0F*g);pos++;
149  *pos = (uchar)(255.0F*b);pos++;
150  }
151  }
152 
153  bool status = (*a_writer)(m_out,a_file,buffer,a_width,a_height,bpp,a_quality);
154  if(!status) {
155  m_out << "tools::sg::plots_viewer::write_inzb_jpeg : can't write " << a_file << "." << std::endl;
156  }
157  delete [] buffer;
158  return status;
159  }
160 
161  bool write_inzb_jpeg(jpeg_writer a_writer,const std::string& a_file,int a_quality = 100) {
162  return write_inzb_jpeg(a_writer,a_file,width(),height(),a_quality);
163  }
164 
165  bool write_inzb_ps(const std::string& a_file,unsigned int a_width,unsigned int a_height,bool a_anonymous = false) {
166  zb_action action(m_zb_mgr,m_out,a_width,a_height);
167  action.zbuffer().clear_color_buffer(0);
169  action.zbuffer().clear_depth_buffer();
170  sg().render(action);
171  wps wps(m_out);
172  if(!wps.open_file(a_file,a_anonymous)) {
173  m_out << "tools::viewplot::write_inzb_ps : can't open " << a_file << "." << std::endl;
174  return false;
175  }
176  wps.PS_BEGIN_PAGE();
177  wps.PS_PAGE_SCALE(float(a_width),float(a_height));
179  wps.PS_END_PAGE();
180  wps.close_file();
181  return true;
182  }
183 
184  bool write_inzb_ps(const std::string& a_file,bool a_anonymous = false) {
185  return write_inzb_ps(a_file,width(),height(),a_anonymous);
186  }
187 
188  bool open_inzb_ps_file(const std::string& a_file,bool a_anonymous = false) {
189  if(!m_wps.open_file(a_file,a_anonymous)) {
190  m_out << "tools::plots_viewer::open_inzb_ps_file : can't open " << a_file << "." << std::endl;
191  return false;
192  }
193  return true;
194  }
195  bool write_inzb_ps_page(unsigned int a_width,unsigned int a_height) {
196  sg::zb_action action(m_zb_mgr,m_out,a_width,a_height);
197  action.zbuffer().clear_color_buffer(0);
199  action.zbuffer().clear_depth_buffer();
200  sg().render(action);
202  m_wps.PS_PAGE_SCALE(float(a_width),float(a_height));
204  m_wps.PS_END_PAGE();
205  return true;
206  }
209 
210  bool write_gl2ps(const std::string& a_file,int a_gl2ps_format,unsigned int a_width,unsigned int a_height) {
211  gl2ps_action action(m_gl2ps_mgr,m_out,a_width,a_height);
213  if(!action.open(a_file,a_gl2ps_format)) return false;
214  sg().render(action);
215  action.close();
216  return true;
217  }
218  bool write_gl2ps(const std::string& a_file,int a_gl2ps_format) {return write_gl2ps(a_file,a_gl2ps_format,width(),height());}
219 
220 protected:
221  void create_sg() {
222  m_sg.clear();
223 
224  m_camera.height = 1;
225  float z = 10*1;
226  m_camera.znear = 0.1f*z;
227  m_camera.zfar = 10*z; //100*z induces problems with lego rendering.
228  m_camera.position = vec3f(0,0,z);
229  m_camera.orientation = rotf(vec3f(0,0,1),0);
230  m_camera.focal = z;
231  m_sg.add(new noderef(m_camera));
232 
233  m_sg.add(new noderef(m_plots));
234  }
235 
236 protected:
242 };
243 
244 }}
245 
246 #endif
247 
tools::sg::plots_viewer::write_gl2ps
bool write_gl2ps(const std::string &a_file, int a_gl2ps_format, unsigned int a_width, unsigned int a_height)
Definition: plots_viewer:210
tools::sg::zb_action::get_rgb
static bool get_rgb(void *a_tag, unsigned int a_col, unsigned int a_row, VCol &a_r, VCol &a_g, VCol &a_b)
Definition: zb_action:296
tools::sg::plots::adjust_size
void adjust_size(unsigned int a_ww, unsigned int a_wh)
Definition: plots:271
tools::sg::base_camera::orientation
sf_rotf orientation
Definition: base_camera:33
tools::sg::plots_viewer::write_inzb_jpeg
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)
Definition: plots_viewer:124
tools::sg::plots_viewer::png_writer
bool(* png_writer)(std::ostream &, const std::string &, unsigned char *, unsigned int, unsigned int, unsigned int)
Definition: plots_viewer:78
tools::sg::plots_viewer::open_inzb_ps_file
bool open_inzb_ps_file(const std::string &a_file, bool a_anonymous=false)
Definition: plots_viewer:188
cloud2plot_cp
tools::wps::close_file
bool close_file()
Definition: wps:125
tools::rotf
Definition: rotf:16
tools::sg::action
Definition: action:19
f2plot
viewer
tools::sg::plots_viewer::write_inzb_jpeg
bool write_inzb_jpeg(jpeg_writer a_writer, const std::string &a_file, int a_quality=100)
Definition: plots_viewer:161
zb_action
tools::sg::base_camera::position
sf_vec3f position
Definition: base_camera:29
tools::sg::plots_viewer::write_inzb_png
bool write_inzb_png(png_writer a_writer, const std::string &a_file)
Definition: plots_viewer:117
tools::sg::plots_viewer::m_plots
sg::plots m_plots
Definition: plots_viewer:240
tools::sg::plots_viewer::jpeg_writer
bool(* jpeg_writer)(std::ostream &, const std::string &, unsigned char *, unsigned int, unsigned int, unsigned int, int)
Definition: plots_viewer:121
h2plot_cp
tools::sg::gl2ps_manager
Definition: gl2ps_manager:14
tools::sg::ortho::height
sf< float > height
Definition: ortho:24
plotter_common.icc
tools::sg::plots_viewer::set_size
virtual void set_size(unsigned int a_width, unsigned int a_height)
Definition: plots_viewer:31
tools::sg::plots_viewer::write_inzb_png
bool write_inzb_png(png_writer a_writer, const std::string &a_file, unsigned int a_width, unsigned int a_height)
Definition: plots_viewer:81
tools::sg::base_freetype
Definition: base_freetype:16
tools::wps::PS_PAGE_SCALE
void PS_PAGE_SCALE(float a_width, float a_height, bool a_portrait=true)
Definition: wps:137
tools::sg::plots_viewer::gl2ps_manager
sg::gl2ps_manager & gl2ps_manager()
Definition: plots_viewer:75
tools::sg::plots_viewer::close_inzb_ps_file
bool close_inzb_ps_file()
Definition: plots_viewer:208
tools::wps::PS_END_PAGE
void PS_END_PAGE()
Definition: wps:185
tools::sg::plots_viewer::write_inzb_ps
bool write_inzb_ps(const std::string &a_file, unsigned int a_width, unsigned int a_height, bool a_anonymous=false)
Definition: plots_viewer:165
tools::colorf::g
float g() const
Definition: colorf:33
tools::wps::open_file
bool open_file(const std::string &a_name, bool a_anonymous=false)
Definition: wps:59
tools::sg::plots
Definition: plots:16
tools::sg::base_camera::zfar
sf< float > zfar
Definition: base_camera:28
tools::sg::plots_viewer::m_gl2ps_mgr
sg::gl2ps_manager m_gl2ps_mgr
Definition: plots_viewer:238
tools::sg::plots_viewer
Definition: plots_viewer:28
tools::sg::zb_action::VCol
float VCol
Definition: zb_action:294
tools::sg::group::render
virtual void render(render_action &a_action)
Definition: group:24
tools::sg::ortho
Definition: ortho:12
tools::uchar
unsigned char uchar
Definition: typedefs:99
plots
tools::sg::plots_viewer::m_zb_mgr
sg::zb_manager m_zb_mgr
Definition: plots_viewer:237
tools::sg::plots_viewer::plots_viewer
plots_viewer(const plots_viewer &a_from)
Definition: plots_viewer:54
tools::wps::PS_BEGIN_PAGE
void PS_BEGIN_PAGE()
Definition: wps:179
tools::sg::viewer::m_sg
group m_sg
Definition: viewer:173
ortho
TOOLS_HEADER
#define TOOLS_HEADER(a__class, a__sclass, a__parent)
Definition: HEADER:10
tools::wps
Definition: wps:16
tools::sg::group::add
void add(node *a_node)
Definition: group:96
tools::sg::plots_viewer::create_sg
void create_sg()
Definition: plots_viewer:221
tools::sg::plots_viewer::write_inzb_ps
bool write_inzb_ps(const std::string &a_file, bool a_anonymous=false)
Definition: plots_viewer:184
tools::wps::rgb_4
@ rgb_4
Definition: wps:193
tools::sg::viewer::sg
group & sg()
Definition: viewer:94
tools::sg::gl2ps_action
Definition: gl2ps_action:19
tools::sg::zb_manager
Definition: zb_manager:18
tools::colorf::a
float a() const
Definition: colorf:35
tools::vec3f
Definition: vec3f:13
tools::sg::plots_viewer::operator=
plots_viewer & operator=(const plots_viewer &a_from)
Definition: plots_viewer:63
tools::sg::plots_viewer::plots_viewer
plots_viewer(std::ostream &a_out, const base_freetype &a_ttf, unsigned int a_cols=1, unsigned int a_rows=1, unsigned int a_width=500, unsigned int a_height=500)
Definition: plots_viewer:36
tools::sg::plots_viewer::zb_manager
sg::zb_manager & zb_manager()
Definition: plots_viewer:74
tools::wps::PS_IMAGE
void PS_IMAGE(Uint a_width, Uint a_height, rgb_nbit a_nbit, rgb_func a_proc, void *a_tag)
Definition: wps:197
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::base_camera::focal
sf< float > focal
Definition: base_camera:39
tools::sg::zb_action
Definition: zb_action:26
tools::sg::plots_viewer::m_wps
wps m_wps
Definition: plots_viewer:241
tools::sg::viewer::m_out
std::ostream & m_out
Definition: viewer:169
tools::sg::plots_viewer::write_inzb_ps_page
bool write_inzb_ps_page()
Definition: plots_viewer:207
fit2plot
tools::sg::plots_viewer::m_camera
ortho m_camera
Definition: plots_viewer:239
gl2ps_action
tools::sg::group::clear
void clear()
Definition: group:235
tools::sg::noderef
Definition: noderef:14
tools::colorf::r
float r() const
Definition: colorf:32
tools::sg::plots_viewer::~plots_viewer
virtual ~plots_viewer()
Definition: plots_viewer:48
tools::sg::plots_viewer::write_inzb_ps_page
bool write_inzb_ps_page(unsigned int a_width, unsigned int a_height)
Definition: plots_viewer:195
tools::sg::viewer::height
unsigned int height() const
Definition: viewer:98
tools::sg::viewer::width
unsigned int width() const
Definition: viewer:97
tools::sg::base_camera::znear
sf< float > znear
Definition: base_camera:27
tools::sg::viewer
Definition: viewer:18
tools::sg::plots::rows
sf< unsigned int > rows
Definition: plots:22
tools::sg::plots::clear_sg
void clear_sg()
Definition: plots:919
xy2plot
tools::sg::viewer::m_clear_color
colorf m_clear_color
Definition: viewer:170
tools::sg::plots_viewer::write_gl2ps
bool write_gl2ps(const std::string &a_file, int a_gl2ps_format)
Definition: plots_viewer:218
tools::sg::plots::cols
sf< unsigned int > cols
Definition: plots:21
tools::colorf::b
float b() const
Definition: colorf:34