| Bsoft 2.0.5 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 double& operator[](long i) {
185 while ( i < 0 ) i += nr*nc;
186 while ( i >= nr*nc ) i -= nr*nc;
187 return data[i];
188 }
189 Bstring& title() { return tit; }
190 void title(const Bstring& t) { tit = t; }
191 void title(const char* t) { tit = t; }
192 void page_size(long w, long h) { pw = w; ph = h; }
193 long page_width() { return pw; }
194 long page_height() { return ph; }
195 void size(long w, long h) { gw = w; gh = h; }
196 long width() { return gw; }
197 long height() { return gh; }
198 void origin(long l, long b) { gl = l; gb = b; }
199 long left() { return gl; }
200 long bottom() { return gb; }
201 void pages(long n) {
202 np = n;
203 pg = new Bpage[np];
204 for ( long i=0; i<np; i++ ) pg[i].number(i+1);
205 }
206 long pages() { return np; }
207 void rows(long n) { nr = n; }
208 long rows() { return nr; }
209 void columns(long n) { nc = n; }
210 long columns() { return nc; }
211 Bpage& page(long i) { // cout << "Getting page " << i << endl;
212 if ( pg && i >=0 && i < np ) return pg[i];
213 else {
214 cerr << "Error: The plot page " << i << " cannot be found!" << endl;
215 return pg[0];
216 }
217 }
218 double* data_pointer() { return data; }
219 void limits() {
220 if ( !pg ) return;
221 long i, j, n, c;
222 double min, max;
223 Bcolumn col;
224 for ( n=0; n<np; n++ ) {
225 for ( c=0; c<pg[n].columns(); c++ ) {
226 col = pg[n].column(c);
227 if ( col.min() == col.max() ) {
228 min = 1e100; max = -1e100;
229 for ( i=col.number()*nr, j=0; j<nr; i++, j++ ) {
230 if ( min > data[i] ) min = data[i];
231 if ( max < data[i] ) max = data[i];
232 }
233 pg[n].column(c).min(min);
234 pg[n].column(c).max(max);
235 // cout << "col[" << c << "]: min=" << min << " max=" << max << endl;
236 }
237 }
238 }
239 }
240 void add(Bplot* p) {
241 for ( long i=0; i<nr*nc; i++ ) data[i] += p->data[i];
242 }
243 void multiply(double d) {
244 for ( long i=0; i<nr*nc; i++ ) data[i] *= d;
245 }
246 double cut(long icol, double threshold, int dir=1) {
247 long i, i1, j, j1;
248 double val((data[0]+data[1])/2);
249 for ( i=0, i1=1, j=icol*nr, j1=j+1; i1<nr; i++, i1++, j++, j1++ ) {
250 if ( dir < 0 ) { // decreasing threshold
251 if ( data[j] >= threshold && data[j1] < threshold )
252 val = data[i] + (data[i1] - data[i])*(threshold - data[j])/(data[j1] - data[j]);
253 } else { // increasing threshold
254 if ( data[j] <= threshold && data[j1] > threshold )
255 val = data[i] + (data[i1] - data[i])*(threshold - data[j])/(data[j1] - data[j]);
256 }
257 }
258 if ( val < data[1] ) {
259 if ( dir < 0 ) {
260 if ( data[j] > threshold ) val = data[i];
261 } else {
262 if ( data[j] < threshold ) val = data[i];
263 }
264 }
265 return val;
266 }
267 void write_tsv(Bstring& filename) {
268 long i, j, k;
269 ofstream ftsv(filename.c_str());
270 if ( ftsv.fail() ) return;
271 // ftsv << page(0).column(0).label();
272 // for ( j=1; j<nc; j++ ) ftsv << tab << page(0).column(j).label();
273 // ftsv << endl;
274 for ( i=0; i<nr; i++ ) {
275 ftsv << data[i];
276 for ( j=1, k=nr+i; j<nc; j++, k+=nr ) ftsv << tab << data[k];
277 ftsv << endl;
278 }
279 ftsv.close();
280 }
281 void resolution_display(double* fsccut=NULL, double* dprcut=NULL);
282 } ;
283
284 ostream& operator<<(ostream& output, Bplot* plot);
285
286 #endif
287
Baxis::inc
double inc()
Definition: Bplot.h:34
Bplot::title
void title(const Bstring &t)
Definition: Bplot.h:190
Baxis::label
Bstring & label()
Definition: Bplot.h:28
Bplot::cut
double cut(long icol, double threshold, int dir=1)
Definition: Bplot.h:246
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
Bplot::width
long width()
Definition: Bplot.h:196
Baxis::min
double min()
Definition: Bplot.h:30
Bplot::columns
long columns()
Definition: Bplot.h:210
Bpage::Bpage
Bpage(long n, long ncol)
Definition: Bplot.h:98
Bplot::Bplot
Bplot(long npage, long nrow, long ncol)
Definition: Bplot.h:175
Bplot::title
Bstring & title()
Definition: Bplot.h:189
Bcolumn::element_size
double element_size()
Definition: Bplot.h:74
Bpage::columns
long columns()
Definition: Bplot.h:109
utilities.h
Header file for general utilities.
Bplot::size
void size(long w, long h)
Definition: Bplot.h:195
Baxis::which
long which()
Definition: Bplot.h:26
Bplot::origin
void origin(long l, long b)
Definition: Bplot.h:198
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Baxis::color
double * color()
Definition: Bplot.h:36
Bplot::pages
void pages(long n)
Definition: Bplot.h:201
Baxis::max
void max(double v)
Definition: Bplot.h:33
Bpage::limits
void limits()
Definition: Bplot.h:123
Bplot::page_height
long page_height()
Definition: Bplot.h:194
Bcolumn::Bcolumn
Bcolumn(long i, Bstring &s)
Definition: Bplot.h:60
Bcolumn::min
double min()
Definition: Bplot.h:70
Bplot::write_tsv
void write_tsv(Bstring &filename)
Definition: Bplot.h:267
Bcolumn::color
double * color()
Definition: Bplot.h:76
Bplot::page_width
long page_width()
Definition: Bplot.h:193
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Bcolumn::blue
double blue()
Definition: Bplot.h:80
Bcolumn::element_size
void element_size(double v)
Definition: Bplot.h:75
Baxis::max
double max()
Definition: Bplot.h:32
Bcolumn::type
void type(long v)
Definition: Bplot.h:67
operator<<
ostream & operator<<(ostream &output, Bplot *plot)
Definition: Bplot.cpp:15
Bpage::number
long number()
Definition: Bplot.h:104
Bcolumn::max
double max()
Definition: Bplot.h:72
Baxis::which
void which(long i)
Definition: Bplot.h:27
Bplot::limits
void limits()
Definition: Bplot.h:219
Bcolumn::Bcolumn
Bcolumn()
Definition: Bplot.h:58
Bcolumn::Bcolumn
Bcolumn(Bstring &s)
Definition: Bplot.h:59
Bcolumn::number
long number()
Definition: Bplot.h:61
Bpage::title
void title(const Bstring &t)
Definition: Bplot.h:101
Bpage::axes
void axes()
Definition: Bplot.h:116
Bplot::rows
long rows()
Definition: Bplot.h:208
Bcolumn::type
long type()
Definition: Bplot.h:66
Bpage::text
Bstring * text()
Definition: Bplot.h:121
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Bcolumn::red
double red()
Definition: Bplot.h:78
Bpage::title
Bstring & title()
Definition: Bplot.h:100
Baxis::label
void label(const Bstring &s)
Definition: Bplot.h:29
Bcolumn
Definition: Bplot.h:42
Bpage
Definition: Bplot.h:83
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bcolumn::green
double green()
Definition: Bplot.h:79
Bplot::Bplot
Bplot()
Definition: Bplot.h:174
Bplot::pages
long pages()
Definition: Bplot.h:206
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
Bpage::title
void title(const char *t)
Definition: Bplot.h:102
Bplot::add
void add(Bplot *p)
Definition: Bplot.h:240
Baxis::flags
long flags()
Definition: Bplot.h:38
Bstring
Definition: Bstring.h:29
Bpage::Bpage
Bpage()
Definition: Bplot.h:97
Bplot::rows
void rows(long n)
Definition: Bplot.h:207
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:211
Baxis
Definition: Bplot.h:15
Bpage::number
void number(long n)
Definition: Bplot.h:103
Bplot::operator[]
double & operator[](long i)
Definition: Bplot.h:184
Bplot::height
long height()
Definition: Bplot.h:197
Bcolumn::min
void min(double v)
Definition: Bplot.h:71
Bcolumn::max
void max(double v)
Definition: Bplot.h:73
Bstring.h
Header file for the string class.
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
Bplot::~Bplot
~Bplot()
Definition: Bplot.h:179
Baxis::min
void min(double v)
Definition: Bplot.h:31
Baxis::color
void color(double r, double g, double b)
Definition: Bplot.h:37
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
Bplot::title
void title(const char *t)
Definition: Bplot.h:191
Bcolumn::axis
void axis(long v)
Definition: Bplot.h:69
Bplot::page_size
void page_size(long w, long h)
Definition: Bplot.h:192
Baxis::Baxis
Baxis()
Definition: Bplot.h:25
Bplot::data_pointer
double * data_pointer()
Definition: Bplot.h:218
Bcolumn::label
void label(const char *s)
Definition: Bplot.h:65
Bplot::left
long left()
Definition: Bplot.h:199
Bcolumn::color
void color(double r, double g, double b)
Definition: Bplot.h:77
Bcolumn::label
void label(const Bstring &s)
Definition: Bplot.h:64
Bcolumn::number
void number(long i)
Definition: Bplot.h:62
Bplot::columns
void columns(long n)
Definition: Bplot.h:209
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bplot::multiply
void multiply(double d)
Definition: Bplot.h:243
Bcolumn::axis
long axis()
Definition: Bplot.h:68
Bplot
Definition: Bplot.h:146
Bplot::bottom
long bottom()
Definition: Bplot.h:200
Baxis::inc
void inc(double v)
Definition: Bplot.h:35