Bsoft  2.1.0
Bernard's software package
Bplot.h
Go to the documentation of this file.
1 
9 #include "Bstring.h"
10 #include "utilities.h"
11 #include <fstream>
12 
13 #ifndef _Bplot_
14 #define _Bplot_
15 class Baxis {
16 private:
17  long id; // Axis: 1=x, 2=x2, 3=y, 4=y2, 5=v
18  Bstring lab; // Label
19  double mn; // Minimum
20  double mx; // Maximum
21  double ic; // Increment
22  double cl[3]; // Color
23  long flg; // Flags: 1=inverse, 2=logarithm
24 public:
25  Baxis() { id=0; lab=0; mn=0; mx=0; ic=0; flg=0; }
26  long which() { return id; }
27  void which(long i) { id = i; }
28  Bstring& label() { return lab; }
29  void label(const Bstring& s) { lab = s; }
30  double min() { return mn; }
31  void min(double v) { mn = v; }
32  double max() { return mx; }
33  void max(double v) { mx = v; }
34  double inc() { return ic; }
35  void inc(double v) { ic = v; }
36  double* color() { return cl; }
37  void color(double r, double g, double b) { cl[0]=r; cl[1]=g; cl[2]=b; }
38  long flags() { return flg; }
39  void flags(long v) { flg = v; }
40 } ;
41 
42 class Bcolumn {
43 private:
44  long num; // Column index
45  Bstring lab; // Label
46  long tp; // Type of plot: 0=points/dots, 1=bars, 2=line, 3=error, 4=variable dots
47  long ax; // Axis: 0=noaxis, 1=x, 2=x2, 3=y, 4=y2, 5=v
48  double mn; // Minimum
49  double mx; // Maximum
50  double es; // Size of dot or line
51  double cl[3]; // Color
52  void initialize() {
53  num=0; lab=0;
54  tp=ax=0; mn=mx=es=0;
55  cl[0]=cl[1]=cl[2]=0;
56  }
57 public:
58  Bcolumn() { initialize(); }
59  Bcolumn(Bstring& s) { initialize(); lab=s; }
60  Bcolumn(long i, Bstring& s) { initialize(); num=i; lab=s; }
61  long number() { return num; }
62  void number(long i) { num = i; }
63  Bstring& label() { return lab; }
64  void label(const Bstring& s) { lab = s; }
65  void label(const char* s) { lab = s; }
66  long type() { return tp; }
67  void type(long v) { tp = v; }
68  long axis() { return ax; }
69  void axis(long v) { ax = v; }
70  double min() { return mn; }
71  void min(double v) { mn = v; }
72  double max() { return mx; }
73  void max(double v) { mx = v; }
74  double element_size() { return es; }
75  void element_size(double v) { es = v; }
76  double* color() { return cl; }
77  void color(double r, double g, double b) { cl[0]=r; cl[1]=g; cl[2]=b; }
78  double red() { return cl[0]; }
79  double green() { return cl[1]; }
80  double blue() { return cl[2]; }
81 } ;
82 
83 class Bpage {
84 private:
85  Bstring tit; // Page title
86  long num; // Page number
87  long nc; // Number of columns
88  Bcolumn* col; // Columns
89  Baxis ax[5]; // Axes: x, x2, y, y2, v
90  Bstring* txt; // Set of strings to be plotted at the bottom
91  void initialize() {
92  tit = 0; txt = NULL; num = 0; nc = 0;
93  col = NULL;
94  axes();
95  }
96 public:
97  Bpage() { initialize(); }
98  Bpage(long n, long ncol) { initialize(); num = n; columns(ncol); }
99 // ~Bpage() { tit = 0; delete[] col; col = NULL; }
100  Bstring& title() { return tit; }
101  void title(const Bstring& t) { tit = t; }
102  void title(const char* t) { tit = t; }
103  void number(long n) { num = n; }
104  long number() { return num; }
105  void columns(long ncol) {
106  nc = ncol;
107  col = new Bcolumn[nc];
108  }
109  long columns() { return nc; }
110  Bcolumn& column(long i) {
111  if ( i<nc ) return col[i];
112  cerr << "Error: The column requested, " << i << " for page " << num <<
113  " is greater than the number of columns, " << nc << endl << endl;
114  exit(-1);
115  }
116  void axes() { for ( long i=0; i<5; i++ ) ax[i].which(i+1); }
117  Baxis& axis(long id) {
118  if ( id > 0 && id < 6 ) return ax[id-1];
119  else return ax[0];
120  }
121  Bstring* text() { return txt; }
122  void add_text(Bstring& s) { string_add(&txt, s); }
123  void limits() {
124  if ( !col ) return;
125  long c, i;
126  for ( c=0; c<nc; c++ ) {
127  i = col[c].axis();
128  if ( i > 0 && i < 6 ) {
129  i--;
130 // cout << "col[" << c << "]: min=" << col[c].min() << " max=" << col[c].max() << endl;
131  if ( ax[i].min() == ax[i].max() ) {
132  ax[i].min(col[c].min());
133  ax[i].max(col[c].max());
134  }
135 // cout << "col[" << c << "] axis[" << i+1 << "]: min=" << ax[i].min() << " max=" << ax[i].max() << endl;
136  }
137  }
138  for ( i=0; i<5; i++ ) {
139  if ( ax[i].min() == ax[i].max() ) ax[i].max(ax[i].min() + 1);
140  if ( ax[i].inc() == 0 ) ax[i].inc((ax[i].max() - ax[i].min())/10);
141 // cout << "axis[ " << i+1 << "]: min=" << ax[i].min() << " max=" << ax[i].max() << endl;
142  }
143  }
144 } ;
145 
146 class Bplot {
147 private:
148  Bstring tit; // Plot title
149  long pw, ph; // Page size
150  long gw, gh; // Plot size
151  long gl, gb; // Plot left and bottom
152  long np; // Number of pages
153  long nr; // Number of points (rows)
154  long nc; // Number of columns (x is first)
155  Bpage* pg; // Array of page information objects
156  double* data; // Plot data
157  void initialize(long npage=0, long nrow=0, long ncol=0) {
158  tit = 0;
159  pw = 600; ph = 800;
160  gw = 450; gh = 450;
161  gl = 70; gb = 200;
162  np = npage;
163  nr = nrow;
164  nc = ncol;
165  pg = NULL;
166  data = NULL;
167  if ( np ) pages(np);
168  if ( nc && nr ) {
169  data = new double[nc*nr];
170  for ( long i=0; i<nr*nc; i++ ) data[i] = 0;
171  }
172  }
173 public:
174  Bplot() { initialize(); }
175  Bplot(long npage, long nrow, long ncol) {
176  initialize(npage, nrow, ncol);
177  }
178  Bplot(Bstring& filename);
179  ~Bplot() {
180  tit = 0;
181  delete[] pg; pg = NULL;
182  delete[] data; data = NULL;
183  }
184  void reset(long npage, long nrow, long ncol) {
185  if ( pg ) delete[] pg;
186  if ( data ) delete[] data;
187  initialize(npage, nrow, ncol);
188  }
189  double& operator[](long i) {
190  while ( i < 0 ) i += nr*nc;
191  while ( i >= nr*nc ) i -= nr*nc;
192  return data[i];
193  }
194  Bstring& title() { return tit; }
195  void title(const Bstring& t) { tit = t; }
196  void title(const char* t) { tit = t; }
197  void page_size(long w, long h) { pw = w; ph = h; }
198  long page_width() { return pw; }
199  long page_height() { return ph; }
200  void size(long w, long h) { gw = w; gh = h; }
201  long width() { return gw; }
202  long height() { return gh; }
203  void origin(long l, long b) { gl = l; gb = b; }
204  long left() { return gl; }
205  long bottom() { return gb; }
206  void pages(long n) {
207  np = n;
208  pg = new Bpage[np];
209  for ( long i=0; i<np; i++ ) pg[i].number(i+1);
210  }
211  long pages() { return np; }
212  void rows(long n) { nr = n; }
213  long rows() { return nr; }
214  void columns(long n) { nc = n; }
215  long columns() { return nc; }
216  Bpage& page(long i) { // cout << "Getting page " << i << endl;
217  if ( pg && i >=0 && i < np ) return pg[i];
218  else {
219  cerr << "Error: The plot page " << i << " cannot be found!" << endl;
220  return pg[0];
221  }
222  }
223  double* data_pointer() { return data; }
224  void limits() {
225  if ( !pg ) return;
226  long i, j, n, c;
227  double min, max;
228  Bcolumn col;
229  for ( n=0; n<np; n++ ) {
230  for ( c=0; c<pg[n].columns(); c++ ) {
231  col = pg[n].column(c);
232  if ( col.min() == col.max() ) {
233  min = 1e100; max = -1e100;
234  for ( i=col.number()*nr, j=0; j<nr; i++, j++ ) {
235  if ( min > data[i] ) min = data[i];
236  if ( max < data[i] ) max = data[i];
237  }
238  pg[n].column(c).min(min);
239  pg[n].column(c).max(max);
240 // cout << "col[" << c << "]: min=" << min << " max=" << max << endl;
241  }
242  }
243  }
244  }
245  void add(Bplot* p, long start=0) {
246  for ( long i=start*nr; i<nr*nc; i++ ) data[i] += p->data[i];
247  }
248  void multiply(double d) {
249  for ( long i=0; i<nr*nc; i++ ) data[i] *= d;
250  }
251  double cut(long icol, double threshold, int dir=1) {
252  long i, i1, j, j1;
253  double val((data[0]+data[1])/2);
254  for ( i=0, i1=1, j=icol*nr, j1=j+1; i1<nr; i++, i1++, j++, j1++ ) {
255  if ( dir < 0 ) { // decreasing threshold
256  if ( data[j] >= threshold && data[j1] < threshold )
257  val = data[i] + (data[i1] - data[i])*(threshold - data[j])/(data[j1] - data[j]);
258  } else { // increasing threshold
259  if ( data[j] <= threshold && data[j1] > threshold )
260  val = data[i] + (data[i1] - data[i])*(threshold - data[j])/(data[j1] - data[j]);
261  }
262  }
263  if ( val < data[1] ) {
264  if ( dir < 0 ) {
265  if ( data[j] > threshold ) val = data[i];
266  } else {
267  if ( data[j] < threshold ) val = data[i];
268  }
269  }
270  return val;
271  }
272  void write_tsv(Bstring& filename) {
273  long i, j, k;
274  ofstream ftsv(filename.c_str());
275  if ( ftsv.fail() ) return;
276 // ftsv << page(0).column(0).label();
277 // for ( j=1; j<nc; j++ ) ftsv << tab << page(0).column(j).label();
278 // ftsv << endl;
279  for ( i=0; i<nr; i++ ) {
280  ftsv << data[i];
281  for ( j=1, k=nr+i; j<nc; j++, k+=nr ) ftsv << tab << data[k];
282  ftsv << endl;
283  }
284  ftsv.close();
285  }
286  void resolution_display(double* fsccut=NULL, double* dprcut=NULL);
287 } ;
288 
289 ostream& operator<<(ostream& output, Bplot* plot);
290 
291 #endif
292 
Bplot::page_width
long page_width()
Definition: Bplot.h:198
Bcolumn::max
void max(double v)
Definition: Bplot.h:73
Baxis::flags
long flags()
Definition: Bplot.h:38
Bstring
Definition: Bstring.h:29
Bplot::rows
void rows(long n)
Definition: Bplot.h:212
Bcolumn
Definition: Bplot.h:42
Bcolumn::number
void number(long i)
Definition: Bplot.h:62
Bcolumn::color
double * color()
Definition: Bplot.h:76
Bcolumn::element_size
double element_size()
Definition: Bplot.h:74
Bplot::height
long height()
Definition: Bplot.h:202
operator<<
ostream & operator<<(ostream &output, Bplot *plot)
Definition: Bplot.cpp:15
Bplot
Definition: Bplot.h:146
Bpage::title
void title(const Bstring &t)
Definition: Bplot.h:101
Bcolumn::type
long type()
Definition: Bplot.h:66
Bplot::rows
long rows()
Definition: Bplot.h:213
Baxis::flags
void flags(long v)
Definition: Bplot.h:39
Bplot::resolution_display
void resolution_display(double *fsccut=NULL, double *dprcut=NULL)
Definition: Bplot.cpp:114
Baxis::color
double * color()
Definition: Bplot.h:36
Bcolumn::axis
long axis()
Definition: Bplot.h:68
Bcolumn::Bcolumn
Bcolumn(long i, Bstring &s)
Definition: Bplot.h:60
Bpage::Bpage
Bpage(long n, long ncol)
Definition: Bplot.h:98
Bpage::axes
void axes()
Definition: Bplot.h:116
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bpage::columns
long columns()
Definition: Bplot.h:109
Bpage::text
Bstring * text()
Definition: Bplot.h:121
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Bpage::title
Bstring & title()
Definition: Bplot.h:100
Bpage::limits
void limits()
Definition: Bplot.h:123
Baxis::min
void min(double v)
Definition: Bplot.h:31
Baxis::label
void label(const Bstring &s)
Definition: Bplot.h:29
Bcolumn::green
double green()
Definition: Bplot.h:79
Baxis::Baxis
Baxis()
Definition: Bplot.h:25
Bplot::Bplot
Bplot()
Definition: Bplot.h:174
Bplot::data_pointer
double * data_pointer()
Definition: Bplot.h:223
Baxis::inc
double inc()
Definition: Bplot.h:34
Bplot::left
long left()
Definition: Bplot.h:204
Bplot::title
void title(const char *t)
Definition: Bplot.h:196
Bcolumn::max
double max()
Definition: Bplot.h:72
Bplot::title
void title(const Bstring &t)
Definition: Bplot.h:195
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
Bpage::title
void title(const char *t)
Definition: Bplot.h:102
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Baxis::inc
void inc(double v)
Definition: Bplot.h:35
Bplot::cut
double cut(long icol, double threshold, int dir=1)
Definition: Bplot.h:251
Bplot::title
Bstring & title()
Definition: Bplot.h:194
Bstring.h
Header file for the string class.
Bplot::bottom
long bottom()
Definition: Bplot.h:205
Baxis::min
double min()
Definition: Bplot.h:30
Bpage::number
void number(long n)
Definition: Bplot.h:103
Bcolumn::min
void min(double v)
Definition: Bplot.h:71
Bpage::number
long number()
Definition: Bplot.h:104
Bplot::limits
void limits()
Definition: Bplot.h:224
Bplot::operator[]
double & operator[](long i)
Definition: Bplot.h:189
Baxis::label
Bstring & label()
Definition: Bplot.h:28
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bcolumn::blue
double blue()
Definition: Bplot.h:80
Baxis::max
void max(double v)
Definition: Bplot.h:33
Bcolumn::Bcolumn
Bcolumn(Bstring &s)
Definition: Bplot.h:59
Bplot::columns
long columns()
Definition: Bplot.h:215
Bplot::page_size
void page_size(long w, long h)
Definition: Bplot.h:197
Bplot::Bplot
Bplot(long npage, long nrow, long ncol)
Definition: Bplot.h:175
Bplot::write_tsv
void write_tsv(Bstring &filename)
Definition: Bplot.h:272
Bpage
Definition: Bplot.h:83
Bcolumn::red
double red()
Definition: Bplot.h:78
Baxis::which
long which()
Definition: Bplot.h:26
Bplot::origin
void origin(long l, long b)
Definition: Bplot.h:203
Bplot::pages
void pages(long n)
Definition: Bplot.h:206
Bplot::page_height
long page_height()
Definition: Bplot.h:199
Bplot::columns
void columns(long n)
Definition: Bplot.h:214
Bcolumn::Bcolumn
Bcolumn()
Definition: Bplot.h:58
Bcolumn::type
void type(long v)
Definition: Bplot.h:67
Bplot::reset
void reset(long npage, long nrow, long ncol)
Definition: Bplot.h:184
Bplot::multiply
void multiply(double d)
Definition: Bplot.h:248
Bplot::pages
long pages()
Definition: Bplot.h:211
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Bpage::Bpage
Bpage()
Definition: Bplot.h:97
Bcolumn::label
void label(const char *s)
Definition: Bplot.h:65
Bcolumn::element_size
void element_size(double v)
Definition: Bplot.h:75
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
Bcolumn::label
void label(const Bstring &s)
Definition: Bplot.h:64
Bcolumn::number
long number()
Definition: Bplot.h:61
Baxis::max
double max()
Definition: Bplot.h:32
Bplot::width
long width()
Definition: Bplot.h:201
Bplot::add
void add(Bplot *p, long start=0)
Definition: Bplot.h:245
Bplot::size
void size(long w, long h)
Definition: Bplot.h:200
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
Bcolumn::min
double min()
Definition: Bplot.h:70
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
Baxis
Definition: Bplot.h:15
Bcolumn::axis
void axis(long v)
Definition: Bplot.h:69
Baxis::color
void color(double r, double g, double b)
Definition: Bplot.h:37
Bplot::~Bplot
~Bplot()
Definition: Bplot.h:179
Baxis::which
void which(long i)
Definition: Bplot.h:27
Bcolumn::color
void color(double r, double g, double b)
Definition: Bplot.h:77