GMMGnuplotVisualization.cpp
Go to the documentation of this file.
1 
19 
20 namespace Visualization {
21 
24 
25 void GMMGnuplotVisualization::plotOrientationHistogram(const std::string& filename, const std::vector<std::pair<double, double>> data, const std::string& rotaxis)
26 {
28  mPointBuffer.clear();
29 
30  double min_x, max_x, min_y, max_y;
31  min_x = max_x = min_y = max_y = 0;
32  for (std::pair<double, double> datum: data)
33  {
34  if (datum.first < min_x) min_x = datum.first;
35  if (datum.first > max_x) max_x = datum.first;
36  if (datum.second < min_y) min_y = datum.second;
37  if (datum.second > max_y) max_y = datum.second;
38  }
39  double xdistance = 0;
40  if (data.size() > 1) xdistance = std::abs(data.at(1).first - data.at(0).first); // Assuming all data points have equal distance on the x axis
41  std::pair<double, double> xRange(min_x, max_x + xdistance); // Add xdistance to be able to stretch the last point out, see below
42  std::pair<double, double> yRange(min_y, max_y + ((double) 1 / data.size())); // y axis goes up to the bucket of the histogram with the highest value and a little above
43 
44  initPlot("Tabular(" + rotaxis + ")", rotaxis, "P(" + rotaxis + ")", xRange, yRange, data.size()); // using data.size() as amount of samples
45  for (unsigned int i = 0; i < data.size() - 1; i++) // add each data point twice for histogram-like look
46  {
47  addPointToBuffer(data.at(i).first, data.at(i).second);
48  addPointToBuffer(data.at(i+1).first, data.at(i).second);
49  }
50  if (!data.empty()) // add last point twice too
51  {
52  addPointToBuffer(data.at(data.size() - 1).first, data.at(data.size() - 1).second);
53  addPointToBuffer(data.at(data.size() - 1).first + xdistance, data.at(data.size() - 1).second);
54  }
55  sendPlot();
56 }
57 
58 void GMMGnuplotVisualization::initPlot(const std::string& pPlotTitle,
59  const std::string& pXLabel,
60  const std::string& pYLabel,
61  const std::pair<double, double>& pXRange,
62  const std::pair<double, double>& pYRange,
63  unsigned int samples)
64 {
65  mPlotFileHandler.reset(); //Create a clean interface to gnuplot.
66 
67  mPlotFileHandler.add("set xlabel \"" + pXLabel + "\"\n"); //Set label for x axis
68  mPlotFileHandler.add("set ylabel \"" + pYLabel + "\"\n"); //Set label for y axis
69  mPlotFileHandler.add("\n");
70  mPlotFileHandler.add("set xrange [" + boost::lexical_cast<std::string>(pXRange.first) + ":" + boost::lexical_cast<std::string>(pXRange.second) + "]\n"); //Set range in x direction
71  mPlotFileHandler.add("set yrange [" + boost::lexical_cast<std::string>(pYRange.first) + ":" + boost::lexical_cast<std::string>(pYRange.second) + "]\n"); //Set range in y direction
72  mPlotFileHandler.add("set nokey\n"); // don't show legend
73  mPlotFileHandler.add("set samples " + boost::lexical_cast<std::string>(samples) + "\n"); // set number of samples
74  mPlotFileHandler.add("set title \"" + pPlotTitle + "\"\n"); //Set title
75  mPlotFileHandler.add("\n");
76 
77 }
78 
80 {
81  mPointBuffer.push_back(std::make_pair(x, y));
82 }
83 
85 {
87  //Prevent system from trying to send data to non-initialized gnuplot handler
89  throw std::runtime_error("Cannot show non-existing gnuplot visualization.");
90 
91  mPlotFileHandler.add("plot \"-\" with lines\n"); // Prepare for plotting.
92 
93  //Push data to gnuplot and file. "end" is added to end of file automatically, see below.
96 }
97 
98 GMMGnuplotVisualization::PlotFileHandler::PlotFileHandler(std::string filename): mFileBuffer(""), mFilename(filename) { }
99 
101 
103 {
104  *(mGnuplotHandler) << in;
105  mFileBuffer += in;
106 }
107 
108 void GMMGnuplotVisualization::PlotFileHandler::send(std::vector<std::pair<double, double> > pointBuffer)
109 {
110  // Write to file if requested and possible
111  if (mFilename != "")
112  {
113  std::ofstream file;
114  file.open(mFilename);
115  if (file.is_open())
116  {
117  file << mFileBuffer; // Write plot configuration to file
118  for (unsigned int i = 0; i < pointBuffer.size(); i++) { // Write data to file
119  file << pointBuffer.at(i).first << " " << pointBuffer.at(i).second << "\n";
120  }
121  file << "end\n"; // Add "end" so gnuplot later recognizes the end of the data block
122  file << "pause -1 \"Hit return to continue\""; // Add pause and waiting for user input to the end of the file
123  file.close();
124  }
125  else { ROS_INFO_STREAM("Could not open file " + mFilename + ". Proceeding without writing to it."); }
126  }
127  // send to gnuplot
128  mGnuplotHandler->send(pointBuffer);
129 }
130 
132 {
133  if(!mGnuplotHandler) return false;
134  else return true;
135 }
136 
138 
139 }
void plotOrientationHistogram(const std::string &filename, const std::vector< std::pair< double, double >> data, const std::string &rotaxis)
void initPlot(const std::string &pPlotTitle, const std::string &pXLabel, const std::string &pYLabel, const std::pair< double, double > &pXRange, const std::pair< double, double > &pYRange, unsigned int samples)
#define ROS_INFO_STREAM(args)
#define ROS_ASSERT(cond)
std::vector< std::pair< double, double > > mPointBuffer
void send(std::vector< std::pair< double, double >> pointBuffer)


asr_psm_visualizations
Author(s): Gehrung Joachim, Meißner Pascal
autogenerated on Sat Nov 9 2019 03:49:12