VideoVis  0.9
Generates a volume visualisation of a video
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
transferbox.cpp
Go to the documentation of this file.
1 #include "transferbox.h"
2 #include <QPainter>
3 #include <QDebug>
4 #include <QMouseEvent>
5 #include <QVector4D>
6 #include <vector>
7 
8 TransferBox::TransferBox(QWidget *parent) :
9  QWidget(parent) {
10  setAttribute(Qt::WA_NoBackground);
11 
12 
13 }
14 
15 void TransferBox::paintEvent(QPaintEvent *) {
16  static bool first = true;
17  if(first) {
18  m_image = QImage(sizeHint(), QImage::Format_RGB32);
19  QPolygonF points;
20  points << QPointF(0, sizeHint().height())
21  << QPointF(sizeHint().width(), sizeHint().height());
22  m_points = points;
23  m_hoverPoints = new HoverPoints(this, HoverPoints::CircleShape);
24  m_hoverPoints->setPoints(points);
25  m_hoverPoints->setPointLock(0, HoverPoints::LockToLeft);
26  m_hoverPoints->setPointLock(1, HoverPoints::LockToRight);
27  m_hoverPoints->setSortType(HoverPoints::XSort);
28  m_hoverPoints->setEditable(true);
30 
31  m_colors.append(QColor(0, 0, 0));
32  m_colors.append(QColor(0, 0, 0));
33  first = false;
34  }
35  QPainter painter(this);
36  painter.drawImage(0, 0, m_image);
37  //draw BoundingBox
38  painter.setPen(QColor(10, 10, 10));
39  painter.drawRect(0, 0, sizeHint().width() - 1, sizeHint().height() - 1);
40  QLinearGradient gradient(0, 0, sizeHint().width(), 0);
41  for(int i = 0; i < m_points.size(); i++) {
42  gradient.setColorAt((double)m_points.at(i).x()/(double)sizeHint().width(), m_colors.at(i));
43  }
44  painter.fillRect(rect(), gradient);
45  emit valueChanged(toFloatArray());
46 }
47 
48 void TransferBox::mousePressEvent(QMouseEvent *event) {
49 
50 }
51 
52 void TransferBox::addColor(QPolygonF points, QColor color) {
53  bool last = true;
54  for(int i = 0; i < m_points.size(); i++) {
55  if(m_points.at(i).x() != points.at(i).x()) {
56  m_colors.insert(i, color);
57  last = false;
58  break;
59  }
60  }
61  if(last) {
62  m_colors.append(color);
63  }
64  m_points = points;
65  repaint();
66 }
67 
68 void TransferBox::deleteColor(QPolygonF points) {
69  bool last = true;
70  for(int i = 0; i < points.size(); i++) {
71  if(m_points.at(i).x() != points.at(i).x()) {
72  m_colors.remove(i);
73  last = false;
74  break;
75  }
76  }
77  if(last) {
78  m_colors.remove(m_points.size()-2);
79  }
80  m_points = points;
81  repaint();
82 }
83 
84 void TransferBox::changeColor(int index, QColor color) {
85  m_colors.remove(index);
86  m_colors.insert(index, color);
87  repaint();
88  qDebug() << m_points.size() << " " << m_colors.size();
89 }
90 
91 QVector<QVector4D> TransferBox::interpolate() {
92  QVector<QVector4D> out;
93  float stepSize = (float)width()/(STEPS-1);
94  for(float step = 0; step < width(); step += stepSize) {
95  int index;
96  for(index = 0; index < m_points.size()-1; index++) {
97  if(step < m_points.at(index+1).x()) {
98  break;
99  }
100  }
101  int nextIndex = index+1;
102  float delta = m_points.at(nextIndex).x() - m_points.at(index).x();
103  float factor = (step - m_points.at(index).x()) / delta;
104  QVector4D vector;
105  float deltaR = m_colors.at(nextIndex).red() - m_colors.at(index).red();
106  float deltaG = m_colors.at(nextIndex).green() - m_colors.at(index).green();
107  float deltaB = m_colors.at(nextIndex).blue() - m_colors.at(index).blue();
108  float deltaA = m_points.at(nextIndex).y() - m_points.at(index).y();
109  float interpolR = (m_colors.at(index).red() + (factor * deltaR))/255.0;
110  float interpolG = (m_colors.at(index).green() + (factor * deltaG))/255.0;
111  float interpolB = (m_colors.at(index).blue() + (factor * deltaB))/255.0;
112  float interpolA = (1.0 - (m_points.at(index).y() + (factor * deltaA))/sizeHint().height());
113  vector.setX(interpolR);
114  vector.setY(interpolG);
115  vector.setZ(interpolB);
116  vector.setW(interpolA);
117  out.append(vector);
118  }
119  //append last
120  float step = width();
121  int index = m_points.size() - 2;
122  int nextIndex = m_points.size() - 1;
123  float delta = m_points.at(nextIndex).x() - m_points.at(index).x();
124  float factor = 1.0;
125  QVector4D vector;
126  float deltaR = m_colors.at(nextIndex).red() - m_colors.at(index).red();
127  float deltaG = m_colors.at(nextIndex).green() - m_colors.at(index).green();
128  float deltaB = m_colors.at(nextIndex).blue() - m_colors.at(index).blue();
129  float deltaA = m_points.at(nextIndex).y() - m_points.at(index).y();
130  float interpolR = (m_colors.at(index).red() + (factor * deltaR))/255.0;
131  float interpolG = (m_colors.at(index).green() + (factor * deltaG))/255.0;
132  float interpolB = (m_colors.at(index).blue() + (factor * deltaB))/255.0;
133  float interpolA = (1.0 - (m_points.at(index).y() + (factor * deltaA))/sizeHint().height());
134  vector.setX(interpolR);
135  vector.setY(interpolG);
136  vector.setZ(interpolB);
137  vector.setW(interpolA);
138  out.append(vector);
139  return out;
140 }
141 
142 float* TransferBox::toFloatArray() {
143  float* value = new float[STEPS * 4];
144  QVector<QVector4D> vector = interpolate();
145  for (int i=0; i < vector.count(); i++) {
146  QVector4D point = vector.at(i);
147  value[i * 4] = point.x();
148  value[i * 4 + 1] = point.y();
149  value[i * 4 + 2] = point.z();
150  value[i * 4 + 3] = pow(point.w(), 8);
151  }
152  return value;
153 }