-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot.cpp
More file actions
78 lines (70 loc) · 2.84 KB
/
Copy pathplot.cpp
File metadata and controls
78 lines (70 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/***************************************************************************
* Copyright 2015 Michael Eischer, Philipp Nordhus *
* Robotics Erlangen e.V. *
* http://www.robotics-erlangen.de/ *
* info@robotics-erlangen.de *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include "plot.h"
#include <QtOpenGL/QGLWidget>
Plot::Plot(const QString &name, QObject *parent) :
QObject(parent), // use QObject for garbage collection
m_name(name),
m_pos(0),
m_count(0),
m_time(0)
{
// ringbuffer with 6000 entrys as time, value pair
m_data.resize(6000 * 2);
}
void Plot::addPoint(float time, float value)
{
if (m_pos == m_data.size()) { // wrap around
m_pos = 0;
}
m_time = time; // remember last update
m_data[m_pos++] = time; // save value pair
m_data[m_pos++] = value;
// increase count while the buffer isn't full yet
if (m_count < m_data.size()) {
m_count += 2;
}
}
void Plot::plot(const QColor &color) const
{
glLineWidth(3.0f);
glColor3f(color.redF(), color.greenF(), color.blueF());
glVertexPointer(2, GL_FLOAT, 0, m_data.data());
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_LINE_STRIP, m_pos / 2, (m_count - m_pos) / 2);
glDrawArrays(GL_LINE_STRIP, 0, m_pos / 2);
glLineWidth(1.0f);
}
void Plot::mergeFrom(const Plot *p)
{
// start after the latest value, until the last value
for (int i = p->m_pos; i < p->m_count; i += 2) {
addPoint(p->m_data[i], p->m_data[i+1]);
}
// remaining value until the latest value
for (int i = 0; i < p->m_pos; i += 2) {
addPoint(p->m_data[i], p->m_data[i+1]);
}
}
void Plot::clearData()
{
m_pos = 0;
m_count = 0;
}