MatrixDatafieldDisplayWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
25 
26 #include <iostream>
27 
28 #include <pthread.h>
29 
30 #include <QPainter>
31 
34 
35 namespace armarx
36 {
37  void
38  MatrixDatafieldDisplayWidget::updateRequested()
39  {
40  mtx.lock();
41  this->data = matrixDatafieldOffsetFiltered->getDataField()->get<MatrixFloat>()->toEigen();
42  this->percentiles = percentilesDatafield->getDataField()->get<MatrixFloat>()->toVector();
43  mtx.unlock();
44  update();
45  }
46 
48  ObserverInterfacePrx observer,
49  QWidget* parent) :
50  QWidget(parent)
51  {
52  this->matrixDatafield = DatafieldRefPtr::dynamicCast(matrixDatafield);
53  this->observer = observer;
54  this->min = 0;
55  this->max = 1;
56  this->data = MatrixXf(1, 1);
57  this->data(0, 0) = 0;
58  enableOffsetFilter(false);
59  QColor c[] = {QColor::fromHsv(0, 0, 0),
60  QColor::fromHsv(240, 255, 255),
61  QColor::fromHsv(270, 255, 255),
62  QColor::fromHsv(300, 255, 255),
63  QColor::fromHsv(0, 255, 255),
64  QColor::fromHsv(30, 255, 255),
65  QColor::fromHsv(60, 255, 255)};
66  this->colors = std::valarray<QColor>(c, sizeof c / sizeof c[0]);
67 
68  //connect(this, SIGNAL(updateData(MatrixXf)), SLOT(setData(MatrixXf)), Qt::QueuedConnection);
69  connect(this, SIGNAL(doUpdate()), SLOT(updateRequested()), Qt::QueuedConnection);
70  }
71 
73  {
74  //delete ui;
75  }
76 
77  void
79  {
80  mtx.lock();
81 
82  int paddingBottom = 40;
83  QPainter painter(this);
84  painter.fillRect(rect(), QColor::fromRgb(0, 0, 0));
85  int matrixHeight = (height() - paddingBottom) * 8 / 10;
86  int percentilesHeight = (height() - paddingBottom) - matrixHeight;
87  drawMatrix(QRect(0, 0, width(), matrixHeight), painter);
88  drawPercentiles(QRect(0, matrixHeight, width() - 1, percentilesHeight), painter);
89 
90  painter.setPen(QColor(Qt::GlobalColor::gray));
91  painter.setFont(QFont("Arial", 12));
92  painter.drawText(rect(), Qt::AlignBottom | Qt::AlignRight, infoOverlay);
93 
94  mtx.unlock();
95  }
96 
97  void
99  {
100  if (enabled)
101  {
102  this->matrixDatafieldOffsetFiltered =
103  DatafieldRefPtr::dynamicCast(observer->createFilteredDatafield(
104  DatafieldFilterBasePtr(new filters::OffsetFilter()), matrixDatafield));
105  }
106  else
107  {
108  this->matrixDatafieldOffsetFiltered = this->matrixDatafield;
109  }
110 
111  this->percentilesDatafield = DatafieldRefPtr::dynamicCast(observer->createFilteredDatafield(
112  DatafieldFilterBasePtr(new filters::MatrixPercentilesFilter(50)),
113  matrixDatafieldOffsetFiltered));
114  }
115 
116  QColor
118  {
119  value = (value - min) / (max - min) * (colors.size() - 1);
120 
121  if (value < 0)
122  {
123  return colors[0];
124  }
125 
126  if (value >= colors.size() - 1)
127  {
128  return colors[colors.size() - 1];
129  }
130 
131  int i = (int)value;
132  float f2 = value - i;
133  float f1 = 1 - f2;
134  QColor c1 = colors[i];
135  QColor c2 = colors[i + 1];
136  return QColor((int)(c1.red() * f1 + c2.red() * f2),
137  (int)(c1.green() * f1 + c2.green() * f2),
138  (int)(c1.blue() * f1 + c2.blue() * f2));
139  }
140 
141  void
142  MatrixDatafieldDisplayWidget::drawMatrix(const QRect& target, QPainter& painter)
143  {
144  int pixelSize = std::min(target.width() / data.cols(), target.height() / data.rows());
145  int dx = (target.width() - pixelSize * data.cols()) / 2;
146  int dy = (target.height() - pixelSize * data.rows()) / 2;
147  painter.setFont(QFont("Arial", 8));
148  painter.setPen(QColor(Qt::GlobalColor::gray));
149 
150  for (int x = 0; x < data.cols(); x++)
151  {
152  for (int y = 0; y < data.rows(); y++)
153  {
154  QRect target = QRect(dx + x * pixelSize, dy + y * pixelSize, pixelSize, pixelSize);
155  painter.fillRect(target, getColor(data(y, x), min, max));
156  painter.drawText(target, Qt::AlignCenter, QString::number(data(y, x)));
157  }
158  }
159  }
160 
161  void
162  MatrixDatafieldDisplayWidget::drawPercentiles(const QRect& target, QPainter& painter)
163  {
164  painter.setPen(QColor(Qt::GlobalColor::gray));
165  painter.drawRect(target);
166  painter.setPen(QColor(Qt::GlobalColor::red));
167 
168  for (int i = 0; i < (int)percentiles.size() - 1; i++)
169  {
170  int x1 = i * target.width() / (percentiles.size() - 1);
171  int x2 = (i + 1) * target.width() / (percentiles.size() - 1);
172  float y1 = (percentiles.at(i) - min) / (max - min) * target.height();
173  float y2 = (percentiles.at(i + 1) - min) / (max - min) * target.height();
174  painter.drawLine(target.left() + x1,
175  target.bottom() - (int)y1,
176  target.left() + x2,
177  target.bottom() - (int)y2);
178  }
179  }
180 } // namespace armarx
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:48
armarx::MatrixDatafieldDisplayWidget::enableOffsetFilter
void enableOffsetFilter(bool enabled)
Definition: MatrixDatafieldDisplayWidget.cpp:98
armarx::MatrixDatafieldDisplayWidget::doUpdate
void doUpdate()
armarx::MatrixDatafieldDisplayWidget::paintEvent
void paintEvent(QPaintEvent *) override
Definition: MatrixDatafieldDisplayWidget.cpp:78
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:668
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:297
armarx::filters::OffsetFilter
The OffsetFilter class returns values relative to value from the first call of the filter....
Definition: OffsetFilter.h:40
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::MatrixDatafieldDisplayWidget::MatrixDatafieldDisplayWidget
MatrixDatafieldDisplayWidget(DatafieldRefBasePtr matrixDatafield, ObserverInterfacePrx observer, QWidget *parent=0)
Definition: MatrixDatafieldDisplayWidget.cpp:47
MatrixFilters.h
armarx::MatrixDatafieldDisplayWidget::~MatrixDatafieldDisplayWidget
~MatrixDatafieldDisplayWidget() override
Definition: MatrixDatafieldDisplayWidget.cpp:72
armarx::filters::MatrixPercentilesFilter
Definition: MatrixFilters.h:194
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::MatrixDatafieldDisplayWidget::getColor
QColor getColor(float value, float min, float max)
Definition: MatrixDatafieldDisplayWidget.cpp:117
OffsetFilter.h
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:68
armarx::red
QColor red()
Definition: StyleSheets.h:78
MatrixDatafieldDisplayWidget.h
armarx::VariantType::MatrixFloat
const VariantTypeId MatrixFloat
Definition: MatrixVariant.h:37
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:327
min
T min(T t1, T t2)
Definition: gdiam.h:44
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27