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 <QPainter>
27 #include <pthread.h>
28 #include <iostream>
29 
32 
33 namespace armarx
34 {
35  void MatrixDatafieldDisplayWidget::updateRequested()
36  {
37  mtx.lock();
38  this->data = matrixDatafieldOffsetFiltered->getDataField()->get<MatrixFloat>()->toEigen();
39  this->percentiles = percentilesDatafield->getDataField()->get<MatrixFloat>()->toVector();
40  mtx.unlock();
41  update();
42  }
43 
44  MatrixDatafieldDisplayWidget::MatrixDatafieldDisplayWidget(DatafieldRefBasePtr matrixDatafield, ObserverInterfacePrx observer, QWidget* parent) :
45  QWidget(parent)
46  {
47  this->matrixDatafield = DatafieldRefPtr::dynamicCast(matrixDatafield);
48  this->observer = observer;
49  this->min = 0;
50  this->max = 1;
51  this->data = MatrixXf(1, 1);
52  this->data(0, 0) = 0;
53  enableOffsetFilter(false);
54  QColor c[] = {QColor::fromHsv(0, 0, 0), QColor::fromHsv(240, 255, 255), QColor::fromHsv(270, 255, 255), QColor::fromHsv(300, 255, 255),
55  QColor::fromHsv(0, 255, 255), QColor::fromHsv(30, 255, 255), QColor::fromHsv(60, 255, 255)
56  };
57  this->colors = std::valarray<QColor>(c, sizeof c / sizeof c[0]);
58 
59  //connect(this, SIGNAL(updateData(MatrixXf)), SLOT(setData(MatrixXf)), Qt::QueuedConnection);
60  connect(this, SIGNAL(doUpdate()), SLOT(updateRequested()), Qt::QueuedConnection);
61  }
62 
64  {
65  //delete ui;
66  }
67 
69  {
70  mtx.lock();
71 
72  int paddingBottom = 40;
73  QPainter painter(this);
74  painter.fillRect(rect(), QColor::fromRgb(0, 0, 0));
75  int matrixHeight = (height() - paddingBottom) * 8 / 10;
76  int percentilesHeight = (height() - paddingBottom) - matrixHeight;
77  drawMatrix(QRect(0, 0, width(), matrixHeight), painter);
78  drawPercentiles(QRect(0, matrixHeight, width() - 1, percentilesHeight), painter);
79 
80  painter.setPen(QColor(Qt::GlobalColor::gray));
81  painter.setFont(QFont("Arial", 12));
82  painter.drawText(rect(), Qt::AlignBottom | Qt::AlignRight, infoOverlay);
83 
84  mtx.unlock();
85  }
86 
88  {
89  if (enabled)
90  {
91  this->matrixDatafieldOffsetFiltered = DatafieldRefPtr::dynamicCast(observer->createFilteredDatafield(DatafieldFilterBasePtr(new filters::OffsetFilter()), matrixDatafield));
92  }
93  else
94  {
95  this->matrixDatafieldOffsetFiltered = this->matrixDatafield;
96  }
97 
98  this->percentilesDatafield = DatafieldRefPtr::dynamicCast(observer->createFilteredDatafield(DatafieldFilterBasePtr(new filters::MatrixPercentilesFilter(50)), matrixDatafieldOffsetFiltered));
99  }
100 
102  {
103  value = (value - min) / (max - min) * (colors.size() - 1);
104 
105  if (value < 0)
106  {
107  return colors[0];
108  }
109 
110  if (value >= colors.size() - 1)
111  {
112  return colors[colors.size() - 1];
113  }
114 
115  int i = (int)value;
116  float f2 = value - i;
117  float f1 = 1 - f2;
118  QColor c1 = colors[i];
119  QColor c2 = colors[i + 1];
120  return QColor((int)(c1.red() * f1 + c2.red() * f2), (int)(c1.green() * f1 + c2.green() * f2), (int)(c1.blue() * f1 + c2.blue() * f2));
121  }
122 
123  void MatrixDatafieldDisplayWidget::drawMatrix(const QRect& target, QPainter& painter)
124  {
125  int pixelSize = std::min(target.width() / data.cols(), target.height() / data.rows());
126  int dx = (target.width() - pixelSize * data.cols()) / 2;
127  int dy = (target.height() - pixelSize * data.rows()) / 2;
128  painter.setFont(QFont("Arial", 8));
129  painter.setPen(QColor(Qt::GlobalColor::gray));
130 
131  for (int x = 0; x < data.cols(); x++)
132  {
133  for (int y = 0; y < data.rows(); y++)
134  {
135  QRect target = QRect(dx + x * pixelSize, dy + y * pixelSize, pixelSize, pixelSize);
136  painter.fillRect(target, getColor(data(y, x), min, max));
137  painter.drawText(target, Qt::AlignCenter, QString::number(data(y, x)));
138  }
139  }
140  }
141 
142  void MatrixDatafieldDisplayWidget::drawPercentiles(const QRect& target, QPainter& painter)
143  {
144  painter.setPen(QColor(Qt::GlobalColor::gray));
145  painter.drawRect(target);
146  painter.setPen(QColor(Qt::GlobalColor::red));
147 
148  for (int i = 0; i < (int)percentiles.size() - 1; i++)
149  {
150  int x1 = i * target.width() / (percentiles.size() - 1);
151  int x2 = (i + 1) * target.width() / (percentiles.size() - 1);
152  float y1 = (percentiles.at(i) - min) / (max - min) * target.height();
153  float y2 = (percentiles.at(i + 1) - min) / (max - min) * target.height();
154  painter.drawLine(target.left() + x1, target.bottom() - (int)y1, target.left() + x2, target.bottom() - (int)y2);
155  }
156  }
157 }
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
armarx::MatrixDatafieldDisplayWidget::enableOffsetFilter
void enableOffsetFilter(bool enabled)
Definition: MatrixDatafieldDisplayWidget.cpp:87
armarx::MatrixDatafieldDisplayWidget::doUpdate
void doUpdate()
armarx::MatrixDatafieldDisplayWidget::paintEvent
void paintEvent(QPaintEvent *) override
Definition: MatrixDatafieldDisplayWidget.cpp:68
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::filters::OffsetFilter
The OffsetFilter class returns values relative to value from the first call of the filter....
Definition: OffsetFilter.h:41
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::MatrixDatafieldDisplayWidget::MatrixDatafieldDisplayWidget
MatrixDatafieldDisplayWidget(DatafieldRefBasePtr matrixDatafield, ObserverInterfacePrx observer, QWidget *parent=0)
Definition: MatrixDatafieldDisplayWidget.cpp:44
MatrixFilters.h
armarx::MatrixDatafieldDisplayWidget::~MatrixDatafieldDisplayWidget
~MatrixDatafieldDisplayWidget() override
Definition: MatrixDatafieldDisplayWidget.cpp:63
armarx::filters::MatrixPercentilesFilter
Definition: MatrixFilters.h:187
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::MatrixDatafieldDisplayWidget::getColor
QColor getColor(float value, float min, float max)
Definition: MatrixDatafieldDisplayWidget.cpp:101
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:67
armarx::red
QColor red()
Definition: StyleSheets.h:76
MatrixDatafieldDisplayWidget.h
armarx::VariantType::MatrixFloat
const VariantTypeId MatrixFloat
Definition: MatrixVariant.h:36
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28