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
35namespace 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
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
117 MatrixDatafieldDisplayWidget::getColor(float value, float min, float max)
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
constexpr T c
MatrixDatafieldDisplayWidget(DatafieldRefBasePtr matrixDatafield, ObserverInterfacePrx observer, QWidget *parent=0)
QColor getColor(float value, float min, float max)
The OffsetFilter class returns values relative to value from the first call of the filter.
const VariantTypeId MatrixFloat
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition mongodb.cpp:68
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)