LVRPlotter.cpp
Go to the documentation of this file.
1 
28 #include "LVRPlotter.hpp"
29 #include "LVRPointInfo.hpp"
30 #include <QPainter>
31 //#include <../app/LVRMainWindow.hpp"
32 
33 namespace lvr2
34 {
35 
36 LVRPlotter::LVRPlotter(QWidget * parent)
37  : QWidget(parent), m_mode(PlotMode::LINE), m_numPoints(0), m_minX(0), m_maxX(1000)
38 {
39  m_points.reset();
40 }
41 
43 {
44 
45 }
46 
47 void LVRPlotter::mouseReleaseEvent(QMouseEvent* event)
48 {
49  Q_EMIT(mouseRelease());
50 }
51 
53 {
54  m_mode = mode;
55 }
56 
57 void LVRPlotter::setPoints(floatArr points, size_t numPoints)
58 {
59  float max = points[0], min = points[0];
60  for (int i = 0; i < numPoints; i++)
61  {
62  if (points[i] > max)
63  max = points[i];
64  if (points[i] < min)
65  min = points[i];
66  }
67  setPoints(points, numPoints, min, max);
68 }
69 
70 void LVRPlotter::setPoints(floatArr points, size_t numPoints, float min, float max)
71 {
72  m_points = points;
73  m_numPoints = numPoints;
74  m_max = max;
75  m_min = min;
76  update();
77 }
78 
80 {
81  m_points.reset();
82  m_numPoints = 0;
83  update();
84 }
85 
86 void LVRPlotter::setXRange(int min, int max)
87 {
88  m_minX = min;
89  m_maxX = max;
90 }
91 
92 void LVRPlotter::paintEvent(QPaintEvent *)
93 {
94  if (!m_numPoints)
95  {
96  return;
97  }
98  //Create axes and labeling for the plotter
99  QPainter painter(this);
100 
101  painter.setPen(QColor(255, 255, 255));
102  painter.setBrush(QColor(255, 255, 255));
103 
104  painter.drawRect(0, 0, width(), height());
105 
106  painter.setPen(QColor(0, 0, 0));
107  painter.setBrush(QColor(0, 0, 0));
108 
109  QRect rect;
110  painter.drawText(0, 0, width(), height(), Qt::AlignTop, QString("%1").arg(m_max), &rect);
111  int leftMargin = rect.width() + 1;
112 
113  painter.drawText(0, 0, width(), height(), Qt::AlignBottom, QString("%1").arg(m_min), &rect);
114  leftMargin = rect.width() + 1 > leftMargin ? rect.width() + 1 : leftMargin;
115 
116  painter.drawLine(leftMargin, 0, leftMargin, height());
117 
118  float drawWidth = width() - leftMargin;
119 
120  //add wavelength to axes
121  int count = width() / 70;
122  for (int i = 0; i < count; i++)
123  {
124  float new_x = i * drawWidth / count + leftMargin;
125  painter.drawText(new_x, height() - 20, width(), height(), Qt::AlignTop, QString("%1").arg(i * (m_maxX - m_minX) / count + m_minX), &rect);
126  }
127 
128  int botMargin = rect.height() + 1;
129  float drawHeight = height() - botMargin;
130 
131  painter.drawLine(0, drawHeight, width(), drawHeight);
132 
133  painter.setPen(QColor(255, 0, 0));
134  painter.setBrush(QColor(255, 0, 0));
135 
136  float old_x = leftMargin;
137  float old_y = (m_points[0] - m_min) / (m_max - m_min) * drawHeight;
138 
139  //Plot information
140  for (int i = 1; i < m_numPoints; i++)
141  {
142  float new_x = i * drawWidth / m_numPoints + leftMargin;
143  float new_y = (m_points[i] - m_min) / (m_max - m_min) * drawHeight;
144  //Mode to draw Line for Point Preview and Pointview
145  if(m_mode == PlotMode::LINE)
146  {
147  painter.drawLine(old_x, drawHeight - old_y, new_x, drawHeight - new_y);
148  }
149  //Mode to draw bar chart for histogram
150  else if (m_mode == PlotMode::BAR)
151  {
152  painter.setPen(QColor(0, 0, 255));
153  while(old_x <= new_x)
154  {
155  painter.drawLine(old_x, drawHeight - old_y, old_x, drawHeight);
156  old_x++;
157  }
158  }
159  old_x = new_x;
160  old_y = new_y;
161  }
162 
163 }
164 
165 } // namespace lvr2
lvr2::floatArr
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
lvr2::LVRPlotter::m_max
float m_max
Definition: LVRPlotter.hpp:71
lvr2::LVRPlotter::m_maxX
int m_maxX
Definition: LVRPlotter.hpp:74
lvr2::LVRPlotter::mouseRelease
void mouseRelease()
lvr2::LVRPlotter::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: LVRPlotter.cpp:47
lvr2::LVRPlotter::setPoints
void setPoints(floatArr points, size_t numPoints)
Definition: LVRPlotter.cpp:57
lvr2::LVRPlotter::setPlotMode
void setPlotMode(PlotMode mode)
Definition: LVRPlotter.cpp:52
lvr2::LVRPlotter::removePoints
void removePoints()
Definition: LVRPlotter.cpp:79
lvr2::LVRPlotter::LVRPlotter
LVRPlotter(QWidget *parent=(QWidget *) nullptr)
Definition: LVRPlotter.cpp:36
lvr2::PlotMode::BAR
@ BAR
lvr2::LVRPlotter::paintEvent
void paintEvent(QPaintEvent *event)
Create axes, labeling and draw graph or bar chart.
Definition: LVRPlotter.cpp:92
lvr2::LVRPlotter::m_numPoints
size_t m_numPoints
Definition: LVRPlotter.hpp:69
lvr2::LVRPlotter::m_min
float m_min
Definition: LVRPlotter.hpp:70
lvr2::LVRPlotter::setXRange
void setXRange(int min, int max)
Definition: LVRPlotter.cpp:86
lvr2::PlotMode
PlotMode
Definition: LVRPlotter.hpp:38
LVRPlotter.hpp
lvr2::PlotMode::LINE
@ LINE
LVRPointInfo.hpp
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::LVRPlotter::m_mode
PlotMode m_mode
Definition: LVRPlotter.hpp:72
lvr2::LVRPlotter::m_minX
int m_minX
Definition: LVRPlotter.hpp:73
lvr2::LVRPlotter::~LVRPlotter
virtual ~LVRPlotter()
Definition: LVRPlotter.cpp:42
lvr2::LVRPlotter::m_points
floatArr m_points
Definition: LVRPlotter.hpp:68


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:24