-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLayer.cpp
More file actions
1113 lines (861 loc) · 43.2 KB
/
Copy pathLayer.cpp
File metadata and controls
1113 lines (861 loc) · 43.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Layer.h"
#include "ImageViewerPlugin.h"
#include "SettingsAction.h"
#include "DataHierarchyItem.h"
#include "ImageProp.h"
#include "SelectionProp.h"
#include "SelectionToolProp.h"
#include "LayersRenderer.h"
#include <util/Exception.h>
#include <PointData/PointData.h>
#include <ClusterData/ClusterData.h>
#include <QPainter>
#include <QFontMetrics>
#include <QDebug>
#include <QMenu>
#include <set>
using namespace mv;
using namespace mv::gui;
Layer::Layer(QObject* parent, const QString& title) :
GroupsAction(parent, title),
Renderable(),
_imageViewerPlugin(nullptr),
_active(false),
_imagesDataset(),
_sourceDataset(),
_selectedIndices(),
_generalAction(this, "General"),
_imageSettingsAction(this, "Image"),
_selectionAction(this, "Selection"),
_miscellaneousAction(this, "Miscellaneous"),
_subsetAction(this, "Subset"),
_selectionData(),
_imageSelectionRectangle(),
_maskData()
{
}
void Layer::initialize(ImageViewerPlugin* imageViewerPlugin, const mv::Dataset<Images>& imagesDataset)
{
Q_ASSERT(imageViewerPlugin != nullptr);
if (imageViewerPlugin == nullptr)
return;
_imageViewerPlugin = imageViewerPlugin;
setRenderer(&_imageViewerPlugin->getImageViewerWidget().getRenderer());
_imagesDataset = imagesDataset;
_sourceDataset = _imagesDataset->getDataHierarchyItem().getParent()->getDataset();
if (!_sourceDataset.isValid())
throw std::runtime_error("The layer source dataset is not valid after initialization");
if (!_imagesDataset.isValid())
throw std::runtime_error("The layer images dataset is not valid after initialization");
// Resize selection data with number of pixels
_selectionData.resize(_imagesDataset->getNumberOfPixels());
_props << new ImageProp(*this, "ImageProp");
_props << new SelectionProp(*this, "SelectionProp");
_props << new SelectionToolProp(*this, "SelectionToolProp");
this->getPropByName<ImageProp>("ImageProp")->setGeometry(_imagesDataset->getRectangle());
this->getPropByName<SelectionProp>("SelectionProp")->setGeometry(_imagesDataset->getRectangle());
this->getPropByName<SelectionToolProp>("SelectionToolProp")->setGeometry(_imagesDataset->getRectangle());
_generalAction.initialize(this);
_imageSettingsAction.initialize(this);
_selectionAction.initialize(this, &_imageViewerPlugin->getImageViewerWidget(), &_imageViewerPlugin->getImageViewerWidget().getPixelSelectionTool());
_subsetAction.initialize(_imageViewerPlugin);
computeSelectionIndices();
_maskData.resize(_imagesDataset->getNumberOfPixels());
// Update the color map scalar data in the image prop
const auto updateChannelScalarData = [this](ScalarChannelAction& channelAction) {
switch (channelAction.getIdentifier()) {
case ScalarChannelAction::Channel1:
case ScalarChannelAction::Channel2:
case ScalarChannelAction::Channel3:
{
this->getPropByName<ImageProp>("ImageProp")->setChannelScalarData(channelAction.getIdentifier(), channelAction.getScalarData(), channelAction.getDisplayRange());
break;
}
case ScalarChannelAction::Count:
break;
}
// Render
invalidate();
};
const auto updateInterpolationType = [this]() {
this->getPropByName<ImageProp>("ImageProp")->setInterpolationType(static_cast<InterpolationType>(_imageSettingsAction.getInterpolationTypeAction().getCurrentIndex()));
invalidate();
};
connect(&_generalAction.getVisibleAction(), &ToggleAction::toggled, this, &Layer::invalidate);
connect(&_imageSettingsAction, &ImageSettingsAction::channelChanged, this, updateChannelScalarData);
connect(&_imageSettingsAction.getInterpolationTypeAction(), &OptionAction::currentIndexChanged, this, updateInterpolationType);
// Update prop when selection overlay color and opacity change
connect(&_selectionAction.getPixelSelectionAction().getOverlayColorAction(), &ColorAction::colorChanged, this, &Layer::invalidate);
connect(&_selectionAction.getPixelSelectionAction().getOverlayOpacityAction(), &DecimalAction::valueChanged, this, &Layer::invalidate);
// Update the model matrix and re-render
const auto updateModelMatrixAndReRender = [this]() {
updateModelMatrix();
invalidate();
};
connect(&_generalAction.getScaleAction(), &DecimalAction::valueChanged, this, updateModelMatrixAndReRender);
connect(&_generalAction.getPositionAction(), &PositionAction::changed, this, updateModelMatrixAndReRender);
updateChannelScalarData(_imageSettingsAction.getScalarChannel1Action());
updateChannelScalarData(_imageSettingsAction.getScalarChannel2Action());
updateChannelScalarData(_imageSettingsAction.getScalarChannel3Action());
updateInterpolationType();
updateModelMatrixAndReRender();
const auto nameChanged = [this]() -> void {
setText(_generalAction.getNameAction().getString());
updateWindowTitle();
};
nameChanged();
connect(&_generalAction.getNameAction(), &StringAction::stringChanged, this, nameChanged);
connect(&_generalAction.getDatasetNameAction(), &StringAction::stringChanged, this, &Layer::invalidate);
const auto updateSelectionRoi = [this]() {
computeSelection();
publishSelection();
};
connect(&_imageViewerPlugin->getImageViewerWidget().getRenderer(), &LayersRenderer::zoomRectangleChanged, this, [this, updateSelectionRoi]() {
updateRoi();
if (!_active)
return;
if (_selectionAction.getPixelSelectionAction().getPixelSelectionTool()->getType() != PixelSelectionType::ROI)
return;
if (!_selectionAction.getPixelSelectionAction().getNotifyDuringSelectionAction().isChecked())
return;
updateSelectionRoi();
});
connect(&_imageViewerPlugin->getImageViewerWidget(), &ImageViewerWidget::navigationEnded, this, [this, updateSelectionRoi]() {
if (!_active)
return;
if (_selectionAction.getPixelSelectionAction().getPixelSelectionTool()->getType() != PixelSelectionType::ROI)
return;
if (_selectionAction.getPixelSelectionAction().getNotifyDuringSelectionAction().isChecked())
return;
updateSelectionRoi();
});
// Update ROI selection when the pixel selection type changes to ROI
connect(&_selectionAction.getPixelSelectionAction().getTypeAction(), &OptionAction::currentIndexChanged, this, [this, updateSelectionRoi](const std::int32_t& currentIndex) {
if (currentIndex == static_cast<std::int32_t>(PixelSelectionType::ROI))
updateSelectionRoi();
else
_imagesDataset->selectNone();
});
connect(&_miscellaneousAction.getRoiViewAction(), &DecimalRectangleAction::rectangleChanged, getRenderer(), [this](float left, float right, float bottom, float top) -> void {
auto rectangle = QRectF{ left, bottom, right - left, top - bottom};
if (rectangle == getRenderer()->getZoomRectangle())
return;
const auto animationEnabled = getRenderer()->getAnimationEnabled();
getRenderer()->setAnimationEnabled(false);
{
getRenderer()->setZoomRectangle(rectangle);
}
getRenderer()->setAnimationEnabled(animationEnabled);
});
auto updateMaskData = [this]() {
_imagesDataset->getMaskData(_maskData);
// Apply masking to props
this->getPropByName<ImageProp>("ImageProp")->setMaskData(_maskData);
this->getPropByName<SelectionProp>("SelectionProp")->setMaskData(_maskData);
};
connect(&_imagesDataset, &Dataset<Images>::dataChanged, this, updateMaskData);
updateMaskData();
}
Layer::~Layer()
{
#if _DEBUG
qDebug() << "Delete layer" << _generalAction.getNameAction().getString();
#endif
}
void Layer::render(const QMatrix4x4& modelViewProjectionMatrix)
{
try {
// Don't render if invisible
if (!_generalAction.getVisibleAction().isChecked())
return;
// Render props
for (auto prop : _props) {
// Do not render the selection prop and selection tool prop in ROI selection mode
if (prop->getName() == "SelectionProp" || prop->getName() == "SelectionToolProp")
if (_selectionAction.getPixelSelectionAction().getTypeAction().getCurrentIndex() == static_cast<std::int16_t>(PixelSelectionType::ROI))
continue;
// Render the prop
prop->render(modelViewProjectionMatrix);
}
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to render layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to render layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
void Layer::updateModelMatrix()
{
#if _DEBUG
qDebug() << "Update model matrix for layer:" << _generalAction.getNameAction().getString();
#endif
try {
// Get reference to general action for getting the layer position and scale
auto& generalAction = _generalAction;
QMatrix4x4 translateMatrix, scaleMatrix;
// Compute the translation matrix
translateMatrix.translate(generalAction.getPositionAction().getXAction().getValue(), generalAction.getPositionAction().getYAction().getValue(), 0.0f);
// Get the scale factor
const auto scaleFactor = 0.01f * generalAction.getScaleAction().getValue();
// And compute the scale factor
scaleMatrix.scale(scaleFactor, scaleFactor, scaleFactor);
// Assign model matrix
setModelMatrix(translateMatrix * scaleMatrix);
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to update the model matrix for layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to update the model matrix for layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
void Layer::setColorMapImage(const QImage& colorMapImage, const InterpolationType& interpolationType)
{
// Get pointer to image prop
auto imageProp = this->getPropByName<ImageProp>("ImageProp");
// Set the color map image and interpolation type in the image prop
imageProp->setColorMapImage(colorMapImage);
imageProp->setColorMapInterpolationType(interpolationType);
// Render
invalidate();
}
void Layer::updateWindowTitle()
{
try {
#if _DEBUG
qDebug() << "Update the window title for layer:" << _generalAction.getNameAction().getString();
#endif
// Get layer name
const auto name = _generalAction.getNameAction().getString();
// Update the window title
_imageViewerPlugin->getWidget().setWindowTitle(QString("%1%2").arg(_imageViewerPlugin->getGuiName(), _active ? QString(": %1").arg(name) : ""));
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to update the window title for layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to update the window title for layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
void Layer::updateRoi()
{
const auto modelViewMatrix = getRenderer()->getViewMatrix() * getModelMatrix() *getPropByName<SelectionToolProp>("SelectionToolProp")->getModelMatrix();
const auto roiTopLeft = getRenderer()->getScreenPointToWorldPosition(modelViewMatrix, QPoint(0, getRenderer()->getParentWidgetSize().height()));
const auto roiBottomRight = getRenderer()->getScreenPointToWorldPosition(modelViewMatrix, QPoint(getRenderer()->getParentWidgetSize().width(), 0));
const auto inputImageSize = _imagesDataset->getImageSize();
QRect imageRoi;
imageRoi.setBottomLeft(QPoint(std::clamp(static_cast<int>(std::round(roiTopLeft.x())), 0, inputImageSize.width()), std::clamp(static_cast<int>(std::round(roiTopLeft.y())), 0, inputImageSize.height())));
imageRoi.setTopRight(QPoint(std::clamp(static_cast<int>(std::round(roiBottomRight.x())), 0, inputImageSize.width()), std::clamp(static_cast<int>(std::round(roiBottomRight.y())), 0, inputImageSize.height())));
_miscellaneousAction.getRoiLayerAction().setRectangle(imageRoi);
const auto zoomRectangle = getRenderer()->getZoomRectangle();
_miscellaneousAction.getRoiViewAction().setRectangle(zoomRectangle.left(), zoomRectangle.right(), zoomRectangle.top(), zoomRectangle.bottom());
}
QRectF Layer::getWorldBoundingRectangle() const
{
// Compute composite matrix and rectangle extents in world coordinates
const auto matrix = getModelMatrix() * getPropByName<ImageProp>("ImageProp")->getModelMatrix();
const auto visibleRectangle = QRectF(_imagesDataset->getVisibleRectangle());
const auto worldTopLeft = matrix * visibleRectangle.topLeft();
const auto worldBottomRight = matrix * visibleRectangle.bottomRight();
//qDebug() << _imagesDataset->getGuiName() << visibleRectangle;
const auto rectangleFromPoints = [](const QPointF& first, const QPointF& second) -> QRectF {
QRectF rectangle;
rectangle.setLeft(std::min(first.x(), second.x()));
rectangle.setRight(std::max(first.x(), second.x()));
rectangle.setTop(std::min(first.y(), second.y()));
rectangle.setBottom(std::max(first.y(), second.y()));
return rectangle;
};
return rectangleFromPoints(worldTopLeft, worldBottomRight);
}
QRectF Layer::getScreenBoundingRectangle() const
{
return getRenderer()->getScreenRectangleFromWorldRectangle(getWorldBoundingRectangle());
}
ImageViewerPlugin& Layer::getImageViewerPlugin()
{
return *_imageViewerPlugin;
}
QMenu* Layer::getContextMenu(QWidget* parent /*= nullptr*/)
{
auto menu = new QMenu(_generalAction.getNameAction().getString(), parent);
menu->addAction(&_generalAction.getVisibleAction());
menu->addAction(&_imageSettingsAction.getOpacityAction());
return menu;
}
void Layer::activate()
{
try {
#if _DEBUG
qDebug() << "Activate layer:" << _generalAction.getNameAction().getString();
#endif
// Set active
_active = true;
// Enable shortcuts for the layer
_selectionAction.getPixelSelectionAction().setShortcutsEnabled(true);
// Enable the pixel selection tool
_selectionAction.getPixelSelectionAction().getPixelSelectionTool()->setEnabled(true);
// Update the view plugin window tile
updateWindowTitle();
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to activate layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to activate layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
void Layer::deactivate()
{
try {
#if _DEBUG
qDebug() << "Deactivate layer:" << _generalAction.getNameAction().getString();
#endif
// Set active
_active = false;
// Disable shortcuts for the layer
_selectionAction.getPixelSelectionAction().setShortcutsEnabled(false);
// Disable the pixel selection tool
_selectionAction.getPixelSelectionAction().getPixelSelectionTool()->setEnabled(false);
// Update the view plugin window tile
updateWindowTitle();
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to deactivate layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to deactivate layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
bool Layer::isActive() const
{
return _active;
}
void Layer::invalidate()
{
_imageViewerPlugin->getImageViewerWidget().update();
}
void Layer::scaleToFit(const QRectF& layersRectangle)
{
// Only fit into valid rectangle
if (!layersRectangle.isValid())
return;
// Get target rectangle center and size
const auto rectangleCenter = layersRectangle.center();
// Position layer at center
_generalAction.getPositionAction().getXAction().setValue(rectangleCenter.x());
_generalAction.getPositionAction().getYAction().setValue(rectangleCenter.y());
// Compute composite matrix and fetch image size
const auto matrix = getModelMatrix() * getPropByName<ImageProp>("ImageProp")->getModelMatrix();
const auto imageRectangleSize = QSizeF(_imagesDataset->getRectangle().size());
// Compute x- and y scale
const auto scale = QVector2D(layersRectangle.width() / imageRectangleSize.width(), layersRectangle.height() / imageRectangleSize.height());
// Assign scale
_generalAction.getScaleAction().setValue(100.0f * std::min(scale.x(), scale.y()));
}
void Layer::paint(QPainter& painter, const PaintFlag& paintFlags)
{
try {
// Don't draw if the layer is invisible
if (!_generalAction.getVisibleAction().isChecked())
return;
// Get image prop visible screen bounding rectangle
const auto propRectangle = getScreenBoundingRectangle();
// Get pixel selection color
const auto pixelSelectionColor = _imageViewerPlugin->getImageViewerWidget().getPixelSelectionTool().getMainColor();
// Draw layer bounds
if (paintFlags & Layer::Bounds) {
// Configure pen and brush
painter.setPen(QPen(QBrush(_generalAction.getColorAction().getColor()), _active ? 2.0f : 1.0f, _active ? Qt::SolidLine : Qt::DashLine));
painter.setBrush(Qt::transparent);
// Draw the bounding rectangle
painter.drawRect(propRectangle);
}
// Draw layer selection rectangle (if not in ROI selection mode)
if ((paintFlags & Layer::SelectionRectangle) && _imageSelectionRectangle.isValid() && _selectionAction.getPixelSelectionAction().getTypeAction().getCurrentIndex() != static_cast<std::int16_t>(PixelSelectionType::ROI)) {
// Create perimeter pen
auto perimeterPen = QPen(QBrush(_imageViewerPlugin->getImageViewerWidget().getPixelSelectionTool().getMainColor()), 0.7f);
// Custom dash pattern
perimeterPen.setDashPattern(QVector<qreal>{ 7, 7 });
// Configure pen and brush
painter.setPen(perimeterPen);
painter.setBrush(Qt::transparent);
// Draw the bounding rectangle
painter.drawRect(getRenderer()->getScreenRectangleFromWorldRectangle(getWorldSelectionRectangle()));
}
// Draw layer label
if ((paintFlags & Layer::Label) && _active) {
// Establish label text
const auto labelText = QString("%1 (%2)").arg(_generalAction.getNameAction().getString(), _imagesDataset->getDataHierarchyItem().getLocation());
// Configure pen and brush
painter.setPen(QPen(QBrush(_generalAction.getColorAction().getColor()), _active ? 2.0f : 1.0f));
painter.setBrush(Qt::transparent);
// Upper margin in pixels
const auto margin = 5;
// Draw the text
painter.setFont(QApplication::font());
painter.drawText(QRectF(propRectangle.bottomLeft() + QPoint(0.0f, margin), QSize(500, 100)), labelText, QTextOption(Qt::AlignTop | Qt::AlignLeft));
}
// Draw sample information
if (paintFlags & Layer::Sample) {
// Get mouse position in widget coordinates
const auto mousePositionWidget = _imageViewerPlugin->getImageViewerWidget().mapFromGlobal(QCursor::pos());
// Get mouse position in image coordinates
const auto mousePositionImage = _renderer->getScreenPointToWorldPosition(getModelViewMatrix() * getPropByName<ImageProp>("ImageProp")->getModelMatrix(), mousePositionWidget).toPoint();
// Establish label prefix text
QString labelText = QString("Pixel ID\t: [%1, %2]\n").arg(QString::number(mousePositionImage.x()), QString::number(mousePositionImage.y()));
// Compute pixel index
const auto pixelIndex = static_cast<std::uint32_t>(mousePositionImage.y() * _imagesDataset->getImageSize().width() + mousePositionImage.x());
if (pixelIndex >= 0 && pixelIndex < _imagesDataset->getNumberOfPixels()) {
// Show scalar(s) data if hovering over an image that originates from points data
if (getSourceDataset()->getDataType() == PointType) {
if (_imageSettingsAction.getScalarChannel1Action().getEnabledAction().isChecked())
labelText += "Scalar 1\t: " + QString::number(_imageSettingsAction.getScalarChannel1Action().getScalarData()[pixelIndex], 'f', 2);
if (_imageSettingsAction.getScalarChannel2Action().getEnabledAction().isChecked())
labelText += "\nScalar 2\t: " + QString::number(_imageSettingsAction.getScalarChannel2Action().getScalarData()[pixelIndex], 'f', 2);
if (_imageSettingsAction.getScalarChannel3Action().getEnabledAction().isChecked())
labelText += "\nScalar 3\t: " + QString::number(_imageSettingsAction.getScalarChannel3Action().getScalarData()[pixelIndex], 'f', 2);
}
// Show cluster name if hovering over an image that originates from clusters data
if (getSourceDataset()->getDataType() == ClusterType) {
// Get cluster index from channel scalar data
const auto clusterIndex = static_cast<std::int32_t>(_imageSettingsAction.getScalarChannel1Action().getScalarData()[pixelIndex]);
// Get cluster name from cluster index
const auto clusterName = _sourceDataset.get<Clusters>()->getClusters()[clusterIndex].getName();
// Add cluster name to the label text
labelText += "Cluster\t: " + clusterName;
}
// Configure pen and brush
painter.setPen(QPen(QBrush(_imageViewerPlugin->getImageViewerWidget().getPixelSelectionTool().getMainColor()), _active ? 2.0f : 1.0f));
painter.setBrush(Qt::transparent);
// Upper margin in pixels
const auto margin = 15;
// Text font from application
auto font = QApplication::font();
// Adjust font size
font.setPointSizeF(7.0f);
// Create font metrics for establishing the text rectangle size
QFontMetrics fontMetrics(font);
// Text rectangle size
const auto textRectangle = fontMetrics.boundingRect(QRect(QPoint(), QSize(512, 512)), Qt::TextExpandTabs, labelText);
// Create text options for more control over text layout
QTextOption textOption(Qt::AlignLeft);
// Adjust tab stop distance so that the values align
textOption.setTabStopDistance(50);
// Draw text rectangle
painter.setPen(QPen(QBrush(pixelSelectionColor), 0.7f));
painter.setBrush(QBrush(QColor::fromHsl(pixelSelectionColor.hslHue(), pixelSelectionColor.hslSaturation(), 80, 200)));
painter.drawRoundedRect(textRectangle.translated(mousePositionWidget - QPoint(0, textRectangle.height()) + QPoint(margin, -margin)).marginsAdded(QMargins(5, 5, 25, 5)), 2.5f, 2.5f);
// Draw text
painter.setFont(font);
painter.setPen(QPen(pixelSelectionColor, 1.0f));
painter.drawText(QRectF(mousePositionWidget - QPoint(0, textRectangle.height()) + QPoint(margin, -margin), QSize(512, textRectangle.height())), labelText, textOption);
}
}
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to native paint layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to native paint layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
const QString Layer::getImagesDatasetId() const
{
return _imagesDataset->getId();
}
const std::uint32_t Layer::getNumberOfImages() const
{
if (!_imagesDataset.isValid())
return 0;
return _imagesDataset->getNumberOfImages();
}
const QSize Layer::getImageSize() const
{
if (!_imagesDataset.isValid())
return QSize();
return _imagesDataset->getImageSize();
}
const QStringList Layer::getDimensionNames() const
{
if (!_imagesDataset.isValid() || !_sourceDataset.isValid())
return QStringList();
QStringList dimensionNames;
if (_sourceDataset->getDataType() == PointType) {
// Get reference to points dataset
auto points = Dataset<Points>(const_cast<Layer*>(this)->getSourceDataset());
// Populate dimension names
if (points->getDimensionNames().size() == points->getNumDimensions()) {
for (const auto& dimensionName : points->getDimensionNames())
dimensionNames << dimensionName;
}
else {
for (std::uint32_t dimensionIndex = 0; dimensionIndex < points->getNumDimensions(); dimensionIndex++)
dimensionNames << QString("Dim %1").arg(QString::number(dimensionIndex));
}
}
if (_sourceDataset->getDataType() == ClusterType) {
dimensionNames << "Cluster index";
}
return dimensionNames;
}
void Layer::selectAll()
{
if (_sourceDataset->getDataType() == PointType)
Dataset<Points>(getSourceDataset())->selectAll();
}
void Layer::selectNone()
{
if (_sourceDataset->getDataType() == PointType)
Dataset<Points>(getSourceDataset())->selectNone();
}
void Layer::selectInvert()
{
if (_sourceDataset->getDataType() == PointType)
Dataset<Points>(getSourceDataset())->selectInvert();
}
void Layer::startSelection()
{
try {
#if _DEBUG
qDebug() << "Start the pixel selection for layer:" << _generalAction.getNameAction().getString();;
#endif
// Compute the selection in the selection tool prop
this->getPropByName<SelectionToolProp>("SelectionToolProp")->resetOffScreenSelectionBuffer();
// Render
invalidate();
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to start the layer pixel selection for layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to start the layer pixel selection for layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
void Layer::computeSelection(const QVector<QPoint>& mousePositions /*= QVector<QPoint>()*/)
{
try {
#if _DEBUG
qDebug() << "Compute the pixel selection for layer:" << _generalAction.getNameAction().getString();;
#endif
// Compute the selection in the selection tool prop
this->getPropByName<SelectionToolProp>("SelectionToolProp")->compute(mousePositions);
// Render
invalidate();
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to compute layer selection for layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to compute layer selection for layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
void Layer::resetSelectionBuffer()
{
try {
#if _DEBUG
qDebug() << "Reset the selection buffer for layer:" << _generalAction.getNameAction().getString();
#endif
// Reset the off-screen selection buffer
getPropByName<SelectionToolProp>("SelectionToolProp")->resetOffScreenSelectionBuffer();
// Trigger render
invalidate();
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to reset the off-screen selection buffer for layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to reset the off-screen selection buffer for layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
void Layer::publishSelection()
{
try {
#if _DEBUG
qDebug() << "Publish pixel selection for layer:" << _generalAction.getNameAction().getString();
#endif
if (!_generalAction.getVisibleAction().isChecked())
return;
// Make sure we have a valid points dataset
if (!_sourceDataset.isValid())
throw std::runtime_error("The layer points dataset is not valid after initialization");
// Make sure we have a valid images dataset
if (!_imagesDataset.isValid())
throw std::runtime_error("The layer images dataset is not valid after initialization");
// Get reference to the pixel selection tool
auto& pixelSelectionTool = getImageViewerPlugin().getImageViewerWidget().getPixelSelectionTool();
// Get current selection image
auto selectionImage = getPropByName<SelectionToolProp>("SelectionToolProp")->getSelectionImage().mirrored(true, true);
const auto noComponents = 4;
const auto width = static_cast<float>(getImageSize().width());
const auto noPixels = _imagesDataset->getNumberOfPixels();
const auto imageRectangle = _imagesDataset->getRectangle();
// Get pixel index from two-dimensional pixel coordinates
const auto getPixelIndex = [&imageRectangle](const std::int32_t& pixelX, const std::int32_t& pixelY) -> std::int32_t {
return pixelY * imageRectangle.width() + pixelX;
};
if (_sourceDataset->getDataType() == PointType) {
// Get reference to points selection indices
std::vector<std::uint32_t> selectionIndices = _sourceDataset->getSelection<Points>()->indices;
// Establish new selection indices depending on the type of modifier
switch (pixelSelectionTool.getModifier())
{
// Replace pixels with new selection
case PixelSelectionModifierType::Replace:
{
selectionIndices.clear();
selectionIndices.reserve(noPixels);
// Loop over all the pixels in the selection image in row-column order and add to the selection indices if the pixel is non-zero
for (std::int32_t pixelY = 0; pixelY < imageRectangle.height(); pixelY++) {
for (std::int32_t pixelX = 0; pixelX < imageRectangle.width(); pixelX++) {
if (_maskData[getPixelIndex(pixelX, pixelY)] > 0u && selectionImage.bits()[getPixelIndex(pixelX, pixelY) * noComponents] > 0)
selectionIndices.push_back(getPixelIndex(pixelX, pixelY));
}
}
break;
}
// Add pixels to current selection
case PixelSelectionModifierType::Add:
{
// Create selection set of current selection indices
auto selectionSet = std::set<std::uint32_t>(selectionIndices.begin(), selectionIndices.end());
// Loop over all the pixels in the selection image in row-column order and insert the selection index into the set if the pixel is non-zero
for (std::int32_t pixelY = 0; pixelY < imageRectangle.height(); pixelY++) {
for (std::int32_t pixelX = 0; pixelX < imageRectangle.width(); pixelX++) {
if (_maskData[getPixelIndex(pixelX, pixelY)] > 0u && selectionImage.bits()[getPixelIndex(pixelX, pixelY) * noComponents] > 0)
selectionSet.insert(getPixelIndex(pixelX, pixelY));
}
}
// Convert the set back to a vector
selectionIndices = std::vector<std::uint32_t>(selectionSet.begin(), selectionSet.end());
break;
}
// Remove pixels from current selection
case PixelSelectionModifierType::Subtract:
{
// Create selection set of current selection indices
auto selectionSet = std::set<std::uint32_t>(selectionIndices.begin(), selectionIndices.end());
// Loop over all the pixels in the selection image in row-column order and remove the selection index from the set if the pixel is non-zero
for (std::int32_t pixelY = 0; pixelY < imageRectangle.height(); pixelY++) {
for (std::int32_t pixelX = 0; pixelX < imageRectangle.width(); pixelX++) {
if (_maskData[getPixelIndex(pixelX, pixelY)] > 0u && selectionImage.bits()[getPixelIndex(pixelX, pixelY) * noComponents] > 0)
selectionSet.erase(getPixelIndex(pixelX, pixelY));
}
}
// Convert the set back to a vector
selectionIndices = std::vector<std::uint32_t>(selectionSet.begin(), selectionSet.end());
break;
}
default:
break;
}
_sourceDataset->setSelectionIndices(selectionIndices);
}
if (_sourceDataset->getDataType() == ClusterType) {
// Get reference to clusters selection indices
auto& selectionIndices = _sourceDataset->getSelection<Clusters>()->indices;
// Convert floating point scalars to unsigned integer scalars
std::vector<std::uint32_t> integerScalarData(_imageSettingsAction.getScalarChannel1Action().getScalarData().begin(), _imageSettingsAction.getScalarChannel1Action().getScalarData().end());
// Establish new selection indices depending on the type of modifier
switch (pixelSelectionTool.getModifier())
{
// Replace pixels with new selection
case PixelSelectionModifierType::Replace:
{
selectionIndices.clear();
selectionIndices.reserve(noPixels);
// Loop over all the pixels in the selection image in row-column order and add to the selection indices if the pixel is non-zero
for (std::int32_t pixelY = 0; pixelY < imageRectangle.height(); pixelY++) {
for (std::int32_t pixelX = 0; pixelX < imageRectangle.width(); pixelX++) {
if (_maskData[getPixelIndex(pixelX, pixelY)] > 0u && selectionImage.bits()[getPixelIndex(pixelX, pixelY) * noComponents] > 0)
selectionIndices.push_back(integerScalarData[getPixelIndex(pixelX, pixelY)]);
}
}
// Convert selection indices vector to set to remove duplicates
std::set<std::uint32_t> selectionSet(selectionIndices.begin(), selectionIndices.end());
// Convert the set back to a vector
selectionIndices = std::vector<std::uint32_t>(selectionSet.begin(), selectionSet.end());
break;
}
// Add pixels to current selection
case PixelSelectionModifierType::Add:
{
// Create selection set of current selection indices
auto selectionSet = std::set<std::uint32_t>(selectionIndices.begin(), selectionIndices.end());
// Loop over all the pixels in the selection image in row-column order and insert the selection index into the set if the pixel is non-zero
for (std::int32_t pixelY = 0; pixelY < imageRectangle.height(); pixelY++) {
for (std::int32_t pixelX = 0; pixelX < imageRectangle.width(); pixelX++) {
if (_maskData[getPixelIndex(pixelX, pixelY)] > 0u && selectionImage.bits()[getPixelIndex(pixelX, pixelY) * noComponents] > 0)
selectionSet.insert(integerScalarData[getPixelIndex(pixelX, pixelY)]);
}
}
// Convert the set back to a vector
selectionIndices = std::vector<std::uint32_t>(selectionSet.begin(), selectionSet.end());
break;
}
// Remove pixels from current selection
case PixelSelectionModifierType::Subtract:
{
// Create selection set of current selection indices
auto selectionSet = std::set<std::uint32_t>(selectionIndices.begin(), selectionIndices.end());
// Loop over all the pixels in the selection image in row-column order and remove the selection index from the set if the pixel is non-zero
for (std::int32_t pixelY = 0; pixelY < imageRectangle.height(); pixelY++) {
for (std::int32_t pixelX = 0; pixelX < imageRectangle.width(); pixelX++) {
if (_maskData[getPixelIndex(pixelX, pixelY)] > 0u && selectionImage.bits()[getPixelIndex(pixelX, pixelY) * noComponents] > 0)
selectionSet.erase(integerScalarData[getPixelIndex(pixelX, pixelY)]);
}
}
// Convert the set back to a vector
selectionIndices = std::vector<std::uint32_t>(selectionSet.begin(), selectionSet.end());
break;
}
default:
break;
}
// Get reference to the clusters dataset
auto clusters = Dataset<Clusters>(_sourceDataset);
// Get selected indices
const auto selectedIndices = clusters->getSelectedIndices();
// Get reference to clusters input points and its selection
auto points = _sourceDataset->getDataHierarchyItem().getParent()->getDataset<Points>();
auto selection = points->getSelection<Points>();
// Reserve enough space for selection
selection->indices.clear();
selection->indices.reserve(selectedIndices.size());
std::vector<std::uint32_t> globalIndices;
// Get global indices for mapping selection
points->getGlobalIndices(globalIndices);
// Translate selection indices and add them
for (auto selectedIndex : selectedIndices)
selection->indices.push_back(globalIndices[selectedIndex]);
// Notify others that the point selection changed
events().notifyDatasetDataSelectionChanged(points);
}
// Notify listeners of the selection change
events().notifyDatasetDataSelectionChanged(_sourceDataset->getSourceDataset<DatasetImpl>());
// Render
invalidate();
}
catch (std::exception& e)
{
exceptionMessageBox(QString("Unable to publish selection change for layer: %1").arg(_generalAction.getNameAction().getString()), e);
}
catch (...) {
exceptionMessageBox(QString("Unable to publish selection change for layer: %1").arg(_generalAction.getNameAction().getString()));
}
}
void Layer::computeSelectionIndices()
{
try {
// Get selection image, selected indices and selection boundaries from the image dataset
_imagesDataset->getSelectionData(_selectionData, _selectedIndices, _imageSelectionRectangle);
// Assign the scalar data to the prop
this->getPropByName<SelectionProp>("SelectionProp")->setSelectionData(_selectionData);
// Notify others that the selection changed
emit selectionChanged(_selectedIndices);
// Render layer
invalidate();
}
catch (std::exception& e)
{
qDebug() << QString("Unable to compute selected indices for layer %1: %2").arg(_generalAction.getNameAction().getString(), e.what());
}