NPL
Neurological Programs and Libraries
basic_plot.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2014 Micah C Chambers (micahc.vt@gmail.com)
3  *
4  * NPL is free software: you can redistribute it and/or modify it under the
5  * terms of the BSD 2-Clause License available in LICENSE or at
6  * http://opensource.org/licenses/BSD-2-Clause
7  *
8  * @file tga_test1.cpp Tests function plotting,
9  *
10  *****************************************************************************/
11 #ifndef BASIC_PLOT_H
12 #define BASIC_PLOT_H
13 
14 #include <iostream>
15 #include <cstdlib>
16 #include <list>
17 #include <vector>
18 #include <tuple>
19 
20 namespace npl {
21 
22 template <typename T>
23 void writePlot(std::string filename, const std::vector<T>& data);
24 
25 template <typename T>
26 void writePlot(std::string filename, const std::vector<T>& data,
27  size_t xsize, size_t ysize);
28 
29 typedef char rgba[4];
30 
45 class Plotter
46 {
47 public:
48  struct StyleT;
49 
53  Plotter(size_t xres = 1024, size_t yres = 768);
54 
59  void clear();
60 
66  int write(std::string fname);
67 
76  int write(size_t xres, size_t yres, std::string fname);
77 
85  void setXRange(double low, double high);
86 
95  void setYRange(double low, double high);
96 
103  void setRes(size_t xres, size_t yres);
104 
105  // stores style of functions, and functions themselves
106  typedef double (*Function)(double x);
107 
108  void addFunc(Function f);
109  void addFunc(const std::string& style, Function f);
110  void addFunc(const StyleT& style, Function f);
111 
112  void addArray(size_t sz, const double* array);
113  void addArray(size_t sz, const double* xarr, const double* yarr);
114  void addArray(const std::string& style, size_t sz, const double* array);
115  void addArray(const StyleT& style, size_t sz, const double* xarr, const double* yarr);
116 
117  size_t res[2]; //image size
118  double xrange[2];
119  double yrange[2];
120  bool axes;
121 
122  std::list<std::tuple<StyleT, Function>> funcs;
123 
124  // stores style, x values and y values
125  std::list<std::tuple<StyleT, std::vector<double>, std::vector<double>>> arrs;
126 
127  // stores potential colors
128  std::list<StyleT> colors;
129  std::list<StyleT>::iterator curr_color;
130 
131  struct StyleT
132  {
133  StyleT(std::string a) : StyleT("", a) {} ;
134 
135  StyleT(std::string name, std::string a)
136  {
137  this->name = name;
138  full = false;
139  bold = false;
140  dot = false;
141  dash = false;
142  star = false;
143  for(size_t ii=0; ii<a.size(); ii++) {
144  if(a[ii] == '.') {
145  dot = true;
146  } else if(a[ii] == '=') {
147  bold = true;
148  full = true;
149  } else if(a[ii] == '-') {
150  full = true;
151  dash = true;
152  } else if(a[ii] == '*') {
153  star = true;
154  } else if(a[ii] == '!') {
155  full = true;
156  } else if(a[ii] == '#') {
157  // read color into RGB
158  rgba[0]=255;
159  rgba[1]=255;
160  rgba[2]=255;
161  rgba[3]=255;
162  for(int jj=0; ii<a.size() && jj<8; jj++,ii++) {
163 
164  if(a[ii] >= 'A' && a[ii] <= 'F') {
165  if(jj%2 == 0)
166  rgba[jj/2] = a[ii]-'A'+10;
167  else
168  rgba[jj/2] += (a[ii]-'A'+10)*16;
169  } else if(a[ii] >= 'a' && a[ii] <= 'f') {
170  if(jj%2 == 0)
171  rgba[jj/2] = a[ii]-'a'+10;
172  else
173  rgba[jj/2] += (a[ii]-'a'+10)*16;
174  } else if(a[ii] >= '0' && a[ii] <= '9') {
175  if(jj%2 == 0)
176  rgba[jj/2] = a[ii]-'0';
177  else
178  rgba[jj/2] += (a[ii]-'0')*16;
179  } else
180  break;
181  }
182  } else if(a[ii] == 'r') { // red
183  rgba[0] = 255; rgba[1] = 0; rgba[2] = 0; rgba[3] = 255;
184  } else if(a[ii] == 'g') { // green
185  rgba[0] = 0; rgba[1] = 255; rgba[2] = 0; rgba[3] = 255;
186  } else if(a[ii] == 'b') { // blue
187  rgba[0] = 0; rgba[1] = 0; rgba[2] = 255; rgba[3] = 255;
188  } else if(a[ii] == 'k') { // black
189  rgba[0] = 0; rgba[1] = 0; rgba[2] = 0; rgba[3] = 255;
190  } else if(a[ii] == 'w') { // white
191  rgba[0] = 255; rgba[1] = 255; rgba[2] = 255; rgba[3] = 255;
192  } else if(a[ii] == 'y') { // yellow
193  rgba[0] = 255; rgba[1] = 255; rgba[2] = 0; rgba[3] = 255;
194  } else if(a[ii] == 'p') { // pink
195  rgba[0] = 255; rgba[1] = 0; rgba[2] = 255; rgba[3] = 255;
196  } else if(a[ii] == 'G') { // grey
197  rgba[0] = 128; rgba[1] = 128; rgba[2] = 128; rgba[3] = 255;
198  } else if(a[ii] == 'c') { // cyan
199  rgba[0] = 0; rgba[1] = 255; rgba[2] = 255; rgba[3] = 255;
200  }
201 
202  }
203  };
204 
205  std::string name;
206  unsigned char rgba[4];
207  bool dash;
208  bool dot;
209  bool star;
210  bool full;
211  bool bold;
212  };
213 
214 private:
215  void computeRange(size_t xres);
216  int writeTGA(size_t xres, size_t yres, std::string fname);
217  int writeSVG(size_t xres, size_t yres, std::string fname);
218 };
219 
228 template <typename T>
229 void writePlot(std::string filename, const std::vector<T>& data)
230 {
231  Plotter plt;
232  std::vector<double> tmp(data.size());
233  for(size_t ii=0; ii<data.size(); ii++)
234  tmp[ii] = (double)data[ii];
235 
236  plt.addArray(tmp.size(), tmp.data());
237  plt.write(filename);
238 }
239 
240 
251 template <typename T>
252 void writePlot(std::string filename, const std::vector<T>& data, size_t xsize,
253  size_t ysize)
254 {
255  Plotter plt(xsize, ysize);
256 
257  std::vector<double> tmp(data.size());
258  for(size_t ii=0; ii<data.size(); ii++)
259  tmp[ii] = (double)data[ii];
260  plt.addArray(tmp.size(), tmp.data());
261  plt.write(filename);
262 }
263 
264 }; //npl
265 
266 #endif // BASIC_PLOT
267 
StyleT(std::string a)
Definition: basic_plot.h:133
void addFunc(Function f)
Definition: accessors.h:29
size_t res[2]
Definition: basic_plot.h:117
std::list< StyleT > colors
Definition: basic_plot.h:128
void setRes(size_t xres, size_t yres)
Sets the default resolution.
int write(std::string fname)
Writes the output image to the given file.
Class for creating basic plots of arrays or functions. An example might be:
Definition: basic_plot.h:45
double xrange[2]
Definition: basic_plot.h:118
double(* Function)(double x)
Definition: basic_plot.h:106
void addArray(size_t sz, const double *array)
StyleT(std::string name, std::string a)
Definition: basic_plot.h:135
void setXRange(double low, double high)
Sets the x range. To use the extremal values from input arrays just leave these at the default (NAN's...
void clear()
Removes all state variables, including plotted points, and functions.
void setYRange(double low, double high)
Sets the y range. To use the extremal values from input arrays and computed yvalues from functions...
double yrange[2]
Definition: basic_plot.h:119
std::list< std::tuple< StyleT, std::vector< double >, std::vector< double > > > arrs
Definition: basic_plot.h:125
std::list< StyleT >::iterator curr_color
Definition: basic_plot.h:129
Plotter(size_t xres=1024, size_t yres=768)
Constructor.
char rgba[4]
Definition: basic_plot.h:29
std::list< std::tuple< StyleT, Function > > funcs
Definition: basic_plot.h:122
void writePlot(std::string filename, const std::vector< T > &data)
Writes a plot to the given filename. This is a convience wrapper around Plotter, which for quick-and-...
Definition: basic_plot.h:229
std::string name
Definition: basic_plot.h:203