v2.0.0
Loading...
Searching...
No Matches
imagesc.cpp
Go to the documentation of this file.
1//=============================================================================================================
14
15//=============================================================================================================
16// INCLUDES
17//=============================================================================================================
18
19#include "imagesc.h"
20#include "helpers/colormap.h"
21
22//=============================================================================================================
23// QT INCLUDES
24//=============================================================================================================
25
26#include <QPainter>
27
28//=============================================================================================================
29// USED NAMESPACES
30//=============================================================================================================
31
32using namespace DISPLIB;
33using namespace Eigen;
34
35//=============================================================================================================
36// DEFINE MEMBER METHODS
37//=============================================================================================================
38
39ImageSc::ImageSc(QWidget* parent)
40: Graph(parent)
41{
42 init();
43}
44
45//=============================================================================================================
46
47ImageSc::ImageSc(MatrixXd& p_dMat, QWidget* parent)
48: Graph(parent)
49{
50 init();
51 updateData(p_dMat);
52}
53
54//=============================================================================================================
55
56ImageSc::ImageSc(MatrixXf& p_fMat, QWidget* parent)
57: Graph(parent)
58{
59 init();
60 updateData(p_fMat);
61}
62
63//=============================================================================================================
64
65ImageSc::ImageSc(MatrixXi& p_iMat, QWidget* parent)
66: Graph(parent)
67{
68 init();
69 updateData(p_iMat);
70}
71
72//=============================================================================================================
73
74ImageSc::~ImageSc() = default;
75
76//=============================================================================================================
77
79{
80 //Colormap
82 m_sColorMap = "Hot";
83
84 //Parent init
86
89
90 //Colorbar
91 m_bColorbar = true;
92 m_qFontColorbar.setPixelSize(10);
93 m_qPenColorbar = QPen(Qt::black);
95 m_iColorbarSteps = 7; //>= 2!!
97}
98
99//=============================================================================================================
100
101void ImageSc::updateData(MatrixXd& p_dMat)
102{
103 if (p_dMat.rows() > 0 && p_dMat.cols() > 0) {
104 m_dMinValue = p_dMat.minCoeff();
105 m_dMaxValue = p_dMat.maxCoeff();
106
107 // -- data --
108 m_matCentNormData = p_dMat;
109 double minValue = m_matCentNormData.minCoeff();
110 m_matCentNormData.array() -= minValue;
111 double maxValue = m_matCentNormData.maxCoeff();
112 // A constant matrix has no range: keep it at 0 instead of dividing 0 by 0
113 if (maxValue > 0.0) {
114 m_matCentNormData.array() /= maxValue;
115 }
116
117 updateMaps();
118 }
119}
120
121//=============================================================================================================
122
123void ImageSc::updateData(MatrixXf& p_fMat)
124{
125 MatrixXd t_dMat = p_fMat.cast<double>();
126 updateData(t_dMat);
127}
128
129//=============================================================================================================
130
131void ImageSc::updateData(MatrixXi& p_iMat)
132{
133 MatrixXd t_dMat = p_iMat.cast<double>();
134 updateData(t_dMat);
135}
136
137//=============================================================================================================
138
140{
141 m_pixmapData = QPixmap();
142 m_pixmapColorbar = QPixmap();
143
144 if (m_matCentNormData.rows() > 0 && m_matCentNormData.cols() > 0) {
145 // --Data--
146 qint32 x = m_matCentNormData.cols();
147 qint32 y = m_matCentNormData.rows();
148 QImage t_qImageData(x, y, QImage::Format_RGB32);
149 for (qint32 i = 0; i < x; ++i)
150 for (qint32 j = 0; j < y; ++j)
151 t_qImageData.setPixel(i, j, pColorMapper(m_matCentNormData(j, i), m_sColorMap));
152
153 m_pixmapData = QPixmap::fromImage(t_qImageData);
154
155 // --Colorbar--
156 QImage t_qImageColorbar(1, m_iColorbarGradSteps, QImage::Format_RGB32);
157
158 double t_dQuantile = 1.0 / (static_cast<double>(m_iColorbarGradSteps) - 1);
159 for (qint32 j = 0; j < m_iColorbarGradSteps; ++j) {
160 QRgb t_qRgb = pColorMapper(t_dQuantile * (static_cast<double>(m_iColorbarGradSteps - 1 - j)) * 1.0, m_sColorMap);
161 t_qImageColorbar.setPixel(0, j, t_qRgb);
162 }
163 m_pixmapColorbar = QPixmap::fromImage(t_qImageColorbar);
164
165 // --Scale Values--
166 m_qVecScaleValues.clear();
167 if (m_dMaxValue <= m_dMinValue) {
168 update();
169 return;
170 }
171
172 double scale = pow(10, floor(log(m_dMaxValue - m_dMinValue) / log(10.0)));
173
174 //Zero Based Scale?
175 if (m_dMaxValue > 0 && m_dMinValue < 0) {
176 double quantum = floor((((m_dMaxValue - m_dMinValue) / scale) / (m_iColorbarSteps - 1)) * 10.0) * (scale / 10.0);
177 double start = 0;
178 while (m_dMinValue < (start - quantum))
179 start -= quantum;
180 //Create Steps
181 m_qVecScaleValues.push_back(start);
182 for (qint32 i = 1; i < m_iColorbarSteps - 1; ++i)
183 m_qVecScaleValues.push_back(m_qVecScaleValues[i - 1] + quantum);
184 } else {
185 double quantum = floor((((m_dMaxValue - m_dMinValue) / scale) / (m_iColorbarSteps - 1)) * 10.0) * (scale / 10.0);
186 double start = floor(((m_dMaxValue - m_dMinValue) / 2.0 + m_dMinValue) / scale) * scale;
187 while (m_dMinValue < (start - quantum))
188 start -= quantum;
189 //Create Steps
190 m_qVecScaleValues.push_back(start);
191 for (qint32 i = 1; i < m_iColorbarSteps - 1; ++i)
192 m_qVecScaleValues.push_back(m_qVecScaleValues[i - 1] + quantum);
193 }
194 update();
195 }
196}
197
198//=============================================================================================================
199
200void ImageSc::setColorMap(const QString& p_sColorMap)
201{
202 m_sColorMap = p_sColorMap;
203
204 updateMaps();
205}
206
207//=============================================================================================================
208
209void ImageSc::paintEvent(QPaintEvent* event)
210{
211 Q_UNUSED(event);
212
213 QPainter painter(this);
214 if (!m_pixmapData.isNull()) {
215 QPoint t_qPointTopLeft(0, 0);
216
217 // -- Data --
218 QSize t_qSizePixmapData = m_qSizeWidget;
219
220 t_qSizePixmapData.setHeight(t_qSizePixmapData.height() - m_iBorderTopBottom * 2);
221 t_qSizePixmapData.setWidth(t_qSizePixmapData.width() - m_iBorderLeftRight * 2);
222 // Scale data
223 QPixmap t_qPixmapScaledData = m_pixmapData.scaled(t_qSizePixmapData, Qt::IgnoreAspectRatio); //Qt::KeepAspectRatio);
224 // Calculate data position
225 t_qPointTopLeft.setX((m_qSizeWidget.width() - t_qPixmapScaledData.width()) / 2);
226 t_qPointTopLeft.setY((m_qSizeWidget.height() - t_qPixmapScaledData.height()) / 2);
227 //Draw data
228 painter.drawPixmap(t_qPointTopLeft, t_qPixmapScaledData);
229 //Draw border
230 painter.drawRect(t_qPointTopLeft.x() - 1, t_qPointTopLeft.y() - 1, t_qPixmapScaledData.width() + 1, t_qPixmapScaledData.height() + 1);
231
232 // -- Colorbar --
233 if (m_bColorbar && !m_pixmapColorbar.isNull() && m_qVecScaleValues.size() >= 2) {
234 QSize t_qSizePixmapColorbar = m_qSizeWidget;
235
236 t_qSizePixmapColorbar.setWidth(m_iColorbarWidth);
237 t_qSizePixmapColorbar.setHeight(t_qPixmapScaledData.height());
238
239 // Scale colorbar
240 QPixmap t_qPixmapScaledColorbar = m_pixmapColorbar.scaled(t_qSizePixmapColorbar, Qt::IgnoreAspectRatio);
241 // Calculate colorbar position
242 t_qPointTopLeft.setY(t_qPointTopLeft.y());
243 t_qPointTopLeft.setX(t_qPointTopLeft.x() + t_qPixmapScaledData.width() + m_iBorderLeftRight / 3);
244 //Draw colorbar
245 painter.drawPixmap(t_qPointTopLeft, t_qPixmapScaledColorbar);
246 //Draw border
247 painter.drawRect(t_qPointTopLeft.x() - 1, t_qPointTopLeft.y() - 1, m_iColorbarWidth + 1, t_qPixmapScaledData.height() + 1);
248
249 // -- Scale --
250 painter.setPen(m_qPenColorbar);
251 painter.setFont(m_qFontColorbar);
252
253 qint32 x = t_qPointTopLeft.x() + m_iColorbarWidth + m_qFontColorbar.pixelSize() / 2;
254 qint32 x_markLeft = x - m_iColorbarWidth - m_qFontColorbar.pixelSize() / 2;
255 qint32 x_markRight = x_markLeft + m_iColorbarWidth;
256
257 // max
258 painter.save();
259 qint32 y_max = t_qPointTopLeft.y() - m_qFontColorbar.pixelSize() / 2;
260 painter.translate(x, y_max - 1);
261 painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_dMaxValue));
262 painter.restore();
263 //draw max marks
264 qint32 y_max_mark = y_max + m_qFontColorbar.pixelSize() / 2;
265 painter.drawLine(x_markLeft, y_max_mark, x_markLeft + 2, y_max_mark);
266 painter.drawLine(x_markRight - 3, y_max_mark, x_markRight - 1, y_max_mark);
267
268 // min
269 painter.save();
270 qint32 y_min = t_qPointTopLeft.y() + t_qSizePixmapColorbar.height() - 1 - m_qFontColorbar.pixelSize() / 2;
271 painter.translate(x, y_min - 1);
272 painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_dMinValue));
273 painter.restore();
274 //draw min marks
275 qint32 y_min_mark = y_min + m_qFontColorbar.pixelSize() / 2;
276 painter.drawLine(x_markLeft, y_min_mark, x_markLeft + 2, y_min_mark);
277 painter.drawLine(x_markRight - 3, y_min_mark, x_markRight - 1, y_min_mark);
278
279 //Scale values
280 qint32 y_dist = y_min - y_max;
281 double minPercent = (m_qVecScaleValues[0] - m_dMinValue) / (m_dMaxValue - m_dMinValue);
282 double distPercent = (m_qVecScaleValues[1] - m_qVecScaleValues[0]) / (m_dMaxValue - m_dMinValue);
283 qint32 y_current = y_min - (minPercent * y_dist);
284 qint32 y_current_mark;
285 //draw scale
286 for (qint32 i = 0; i < m_qVecScaleValues.size(); ++i) {
287 painter.save();
288 painter.translate(x, y_current - 1);
289 painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_qVecScaleValues[i]));
290 painter.restore();
291 //draw marks
292 y_current_mark = y_current + m_qFontColorbar.pixelSize() / 2;
293 painter.drawLine(x_markLeft, y_current_mark, x_markLeft + 2, y_current_mark);
294 painter.drawLine(x_markRight - 3, y_current_mark, x_markRight - 1, y_current_mark);
295 //update y_current
296 y_current -= distPercent * y_dist;
297 }
298 }
299
300 //Draw title & axes
301 Graph::drawLabels(t_qPixmapScaledData.width(), t_qPixmapScaledData.height());
302 }
303}
MATLAB-style false-colour heat-map widget for Eigen matrices.
Static scalar-to-colour lookup helpers (Jet, Hot, Bone, Viridis, Cool, RedBlue, MNE) used by every pl...
2-D display widgets and visualisation helpers (charts, topography, colour maps).
Common base widget providing title, axis labels and resize handling for DISPLIB's QPainter plots.
Definition graph.h:73
Graph(QWidget *parent=0)
Definition graph.cpp:43
void init()
Definition graph.cpp:51
QSize m_qSizeWidget
Definition graph.h:138
qint32 m_iBorderLeftRight
Definition graph.h:145
qint32 m_iBorderTopBottom
Definition graph.h:144
void drawLabels(qint32 p_iContentWidth, qint32 p_iContentHeight)
Definition graph.cpp:99
static QRgb valueToColor(double v, const QString &sMap)
Definition colormap.h:683
qint32 m_iColorbarSteps
Definition imagesc.h:190
void paintEvent(QPaintEvent *event)
Definition imagesc.cpp:209
QString m_sColorMap
Definition imagesc.h:180
void setColorMap(const QString &p_sColorMap)
Definition imagesc.cpp:200
Eigen::MatrixXd m_matCentNormData
Definition imagesc.h:182
ImageSc(QWidget *parent=0)
Definition imagesc.cpp:39
QVector< double > m_qVecScaleValues
Definition imagesc.h:188
double m_dMinValue
Definition imagesc.h:184
QPen m_qPenColorbar
Definition imagesc.h:193
QFont m_qFontColorbar
Definition imagesc.h:192
qint32 m_iColorbarGradSteps
Definition imagesc.h:191
double m_dMaxValue
Definition imagesc.h:185
void updateData(Eigen::MatrixXd &p_dMat)
qint32 m_iColorbarWidth
Definition imagesc.h:189
QRgb(* pColorMapper)(double, const QString &)
Definition imagesc.h:195
QPixmap m_pixmapData
Definition imagesc.h:177
QPixmap m_pixmapColorbar
Definition imagesc.h:178