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, m_pPixmapData(nullptr)
42, m_pPixmapColorbar(nullptr)
43{
44 init();
45}
46
47//=============================================================================================================
48
49ImageSc::ImageSc(MatrixXd& p_dMat, QWidget* parent)
50: Graph(parent)
51, m_pPixmapData(nullptr)
52, m_pPixmapColorbar(nullptr)
53{
54 init();
55 updateData(p_dMat);
56}
57
58//=============================================================================================================
59
60ImageSc::ImageSc(MatrixXf& p_fMat, QWidget* parent)
61: Graph(parent)
62, m_pPixmapData(nullptr)
63, m_pPixmapColorbar(nullptr)
64{
65 init();
66 updateData(p_fMat);
67}
68
69//=============================================================================================================
70
71ImageSc::ImageSc(MatrixXi& p_iMat, QWidget* parent)
72: Graph(parent)
73, m_pPixmapData(nullptr)
74, m_pPixmapColorbar(nullptr)
75{
76 init();
77 updateData(p_iMat);
78}
79
80//=============================================================================================================
81
83{
84 delete m_pPixmapData;
85 delete m_pPixmapColorbar;
86}
87
88//=============================================================================================================
89
91{
92 //Colormap
94 m_sColorMap = "Hot";
95
96 //Parent init
98
101
102 //Colorbar
103 m_bColorbar = true;
104 m_qFontColorbar.setPixelSize(10);
105 m_qPenColorbar = QPen(Qt::black);
106 m_iColorbarWidth = 12;
107 m_iColorbarSteps = 7; //>= 2!!
109}
110
111//=============================================================================================================
112
113void ImageSc::updateData(MatrixXd& p_dMat)
114{
115 if (p_dMat.rows() > 0 && p_dMat.cols() > 0) {
116 m_dMinValue = p_dMat.minCoeff();
117 m_dMaxValue = p_dMat.maxCoeff();
118
119 // -- data --
120 m_matCentNormData = p_dMat;
121 double minValue = m_matCentNormData.minCoeff();
122 m_matCentNormData.array() -= minValue;
123 double maxValue = m_matCentNormData.maxCoeff();
124 // A constant matrix has no range: keep it at 0 instead of dividing 0 by 0
125 if (maxValue > 0.0) {
126 m_matCentNormData.array() /= maxValue;
127 }
128
129 updateMaps();
130 }
131}
132
133//=============================================================================================================
134
135void ImageSc::updateData(MatrixXf& p_fMat)
136{
137 MatrixXd t_dMat = p_fMat.cast<double>();
138 updateData(t_dMat);
139}
140
141//=============================================================================================================
142
143void ImageSc::updateData(MatrixXi& p_iMat)
144{
145 MatrixXd t_dMat = p_iMat.cast<double>();
146 updateData(t_dMat);
147}
148
149//=============================================================================================================
150
152{
153 if (m_pPixmapData) {
154 delete m_pPixmapData;
155 m_pPixmapData = nullptr;
156 }
157 if (m_pPixmapColorbar) {
158 delete m_pPixmapColorbar;
159 m_pPixmapColorbar = nullptr;
160 }
161
162 if (m_matCentNormData.rows() > 0 && m_matCentNormData.cols() > 0) {
163 // --Data--
164 qint32 x = m_matCentNormData.cols();
165 qint32 y = m_matCentNormData.rows();
166 QImage t_qImageData(x, y, QImage::Format_RGB32);
167 for (qint32 i = 0; i < x; ++i)
168 for (qint32 j = 0; j < y; ++j)
169 t_qImageData.setPixel(i, j, pColorMapper(m_matCentNormData(j, i), m_sColorMap));
170
171 m_pPixmapData = new QPixmap(QPixmap::fromImage(t_qImageData));
172
173 // --Colorbar--
174 QImage t_qImageColorbar(1, m_iColorbarGradSteps, QImage::Format_RGB32);
175
176 double t_dQuantile = 1.0 / ((double)m_iColorbarGradSteps - 1);
177 for (qint32 j = 0; j < m_iColorbarGradSteps; ++j) {
178 QRgb t_qRgb = pColorMapper(t_dQuantile * ((double)(m_iColorbarGradSteps - 1 - j)) * 1.0, m_sColorMap);
179 t_qImageColorbar.setPixel(0, j, t_qRgb);
180 }
181 m_pPixmapColorbar = new QPixmap(QPixmap::fromImage(t_qImageColorbar));
182
183 // --Scale Values--
184 m_qVecScaleValues.clear();
185 if (m_dMaxValue <= m_dMinValue) {
186 update();
187 return;
188 }
189
190 double scale = pow(10, floor(log(m_dMaxValue - m_dMinValue) / log(10.0)));
191
192 //Zero Based Scale?
193 if (m_dMaxValue > 0 && m_dMinValue < 0) {
194 double quantum = floor((((m_dMaxValue - m_dMinValue) / scale) / (m_iColorbarSteps - 1)) * 10.0) * (scale / 10.0);
195 double start = 0;
196 while (m_dMinValue < (start - quantum))
197 start -= quantum;
198 //Create Steps
199 m_qVecScaleValues.push_back(start);
200 for (qint32 i = 1; i < m_iColorbarSteps - 1; ++i)
201 m_qVecScaleValues.push_back(m_qVecScaleValues[i - 1] + quantum);
202 } else {
203 double quantum = floor((((m_dMaxValue - m_dMinValue) / scale) / (m_iColorbarSteps - 1)) * 10.0) * (scale / 10.0);
204 double start = floor(((m_dMaxValue - m_dMinValue) / 2.0 + m_dMinValue) / scale) * scale;
205 while (m_dMinValue < (start - quantum))
206 start -= quantum;
207 //Create Steps
208 m_qVecScaleValues.push_back(start);
209 for (qint32 i = 1; i < m_iColorbarSteps - 1; ++i)
210 m_qVecScaleValues.push_back(m_qVecScaleValues[i - 1] + quantum);
211 }
212 update();
213 }
214}
215
216//=============================================================================================================
217
218void ImageSc::setColorMap(const QString& p_sColorMap)
219{
220 m_sColorMap = p_sColorMap;
221
222 updateMaps();
223}
224
225//=============================================================================================================
226
227void ImageSc::paintEvent(QPaintEvent* event)
228{
229 Q_UNUSED(event);
230
231 QPainter painter(this);
232 if (m_pPixmapData) {
233 QPoint t_qPointTopLeft(0, 0);
234
235 // -- Data --
236 QSize t_qSizePixmapData = m_qSizeWidget;
237
238 t_qSizePixmapData.setHeight(t_qSizePixmapData.height() - m_iBorderTopBottom * 2);
239 t_qSizePixmapData.setWidth(t_qSizePixmapData.width() - m_iBorderLeftRight * 2);
240 // Scale data
241 QPixmap t_qPixmapScaledData = m_pPixmapData->scaled(t_qSizePixmapData, Qt::IgnoreAspectRatio); //Qt::KeepAspectRatio);
242 // Calculate data position
243 t_qPointTopLeft.setX((m_qSizeWidget.width() - t_qPixmapScaledData.width()) / 2);
244 t_qPointTopLeft.setY((m_qSizeWidget.height() - t_qPixmapScaledData.height()) / 2);
245 //Draw data
246 painter.drawPixmap(t_qPointTopLeft, t_qPixmapScaledData);
247 //Draw border
248 painter.drawRect(t_qPointTopLeft.x() - 1, t_qPointTopLeft.y() - 1, t_qPixmapScaledData.width() + 1, t_qPixmapScaledData.height() + 1);
249
250 // -- Colorbar --
251 if (m_bColorbar && m_pPixmapColorbar && m_qVecScaleValues.size() >= 2) {
252 QSize t_qSizePixmapColorbar = m_qSizeWidget;
253
254 t_qSizePixmapColorbar.setWidth(m_iColorbarWidth);
255 t_qSizePixmapColorbar.setHeight(t_qPixmapScaledData.height());
256
257 // Scale colorbar
258 QPixmap t_qPixmapScaledColorbar = m_pPixmapColorbar->scaled(t_qSizePixmapColorbar, Qt::IgnoreAspectRatio);
259 // Calculate colorbar position
260 t_qPointTopLeft.setY(t_qPointTopLeft.y());
261 t_qPointTopLeft.setX(t_qPointTopLeft.x() + t_qPixmapScaledData.width() + m_iBorderLeftRight / 3);
262 //Draw colorbar
263 painter.drawPixmap(t_qPointTopLeft, t_qPixmapScaledColorbar);
264 //Draw border
265 painter.drawRect(t_qPointTopLeft.x() - 1, t_qPointTopLeft.y() - 1, m_iColorbarWidth + 1, t_qPixmapScaledData.height() + 1);
266
267 // -- Scale --
268 painter.setPen(m_qPenColorbar);
269 painter.setFont(m_qFontColorbar);
270
271 qint32 x = t_qPointTopLeft.x() + m_iColorbarWidth + m_qFontColorbar.pixelSize() / 2;
272 qint32 x_markLeft = x - m_iColorbarWidth - m_qFontColorbar.pixelSize() / 2;
273 qint32 x_markRight = x_markLeft + m_iColorbarWidth;
274
275 // max
276 painter.save();
277 qint32 y_max = t_qPointTopLeft.y() - m_qFontColorbar.pixelSize() / 2;
278 painter.translate(x, y_max - 1);
279 painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_dMaxValue));
280 painter.restore();
281 //draw max marks
282 qint32 y_max_mark = y_max + m_qFontColorbar.pixelSize() / 2;
283 painter.drawLine(x_markLeft, y_max_mark, x_markLeft + 2, y_max_mark);
284 painter.drawLine(x_markRight - 3, y_max_mark, x_markRight - 1, y_max_mark);
285
286 // min
287 painter.save();
288 qint32 y_min = t_qPointTopLeft.y() + t_qSizePixmapColorbar.height() - 1 - m_qFontColorbar.pixelSize() / 2;
289 painter.translate(x, y_min - 1);
290 painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_dMinValue));
291 painter.restore();
292 //draw min marks
293 qint32 y_min_mark = y_min + m_qFontColorbar.pixelSize() / 2;
294 painter.drawLine(x_markLeft, y_min_mark, x_markLeft + 2, y_min_mark);
295 painter.drawLine(x_markRight - 3, y_min_mark, x_markRight - 1, y_min_mark);
296
297 //Scale values
298 qint32 y_dist = y_min - y_max;
299 double minPercent = (m_qVecScaleValues[0] - m_dMinValue) / (m_dMaxValue - m_dMinValue);
300 double distPercent = (m_qVecScaleValues[1] - m_qVecScaleValues[0]) / (m_dMaxValue - m_dMinValue);
301 qint32 y_current = y_min - (minPercent * y_dist);
302 qint32 y_current_mark;
303 //draw scale
304 for (qint32 i = 0; i < m_qVecScaleValues.size(); ++i) {
305 painter.save();
306 painter.translate(x, y_current - 1);
307 painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_qVecScaleValues[i]));
308 painter.restore();
309 //draw marks
310 y_current_mark = y_current + m_qFontColorbar.pixelSize() / 2;
311 painter.drawLine(x_markLeft, y_current_mark, x_markLeft + 2, y_current_mark);
312 painter.drawLine(x_markRight - 3, y_current_mark, x_markRight - 1, y_current_mark);
313 //update y_current
314 y_current -= distPercent * y_dist;
315 }
316 }
317
318 //Draw title & axes
319 Graph::drawLabels(t_qPixmapScaledData.width(), t_qPixmapScaledData.height());
320 }
321}
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:188
void paintEvent(QPaintEvent *event)
Definition imagesc.cpp:227
QString m_sColorMap
Definition imagesc.h:178
void setColorMap(const QString &p_sColorMap)
Definition imagesc.cpp:218
Eigen::MatrixXd m_matCentNormData
Definition imagesc.h:180
ImageSc(QWidget *parent=0)
Definition imagesc.cpp:39
QVector< double > m_qVecScaleValues
Definition imagesc.h:186
double m_dMinValue
Definition imagesc.h:182
QPen m_qPenColorbar
Definition imagesc.h:191
QFont m_qFontColorbar
Definition imagesc.h:190
QPixmap * m_pPixmapData
Definition imagesc.h:175
qint32 m_iColorbarGradSteps
Definition imagesc.h:189
double m_dMaxValue
Definition imagesc.h:183
void updateData(Eigen::MatrixXd &p_dMat)
qint32 m_iColorbarWidth
Definition imagesc.h:187
QRgb(* pColorMapper)(double, const QString &)
Definition imagesc.h:193
QPixmap * m_pPixmapColorbar
Definition imagesc.h:176