forked from Rsnelllenberg/VolumeProjectorPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferFunctionWidget.cpp
More file actions
638 lines (506 loc) · 21.1 KB
/
Copy pathTransferFunctionWidget.cpp
File metadata and controls
638 lines (506 loc) · 21.1 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
#include "TransferFunctionWidget.h"
#include <CoreInterface.h>
#include <graphics/Bounds.h>
#include <graphics/Vector3f.h>
#include <util/Exception.h>
#include <vector>
#include <QDebug>
#include <QOpenGLFramebufferObject>
#include <QPainter>
#include <QSize>
#include <QWheelEvent>
#include <QWindow>
#include <QRectF>
#include "TransferFunctionPlugin.h"
using namespace mv;
namespace
{
Bounds getDataBounds(const std::vector<Vector2f>& points)
{
Bounds bounds = Bounds::Max;
for (const Vector2f& point : points)
{
bounds.setLeft(std::min(point.x, bounds.getLeft()));
bounds.setRight(std::max(point.x, bounds.getRight()));
bounds.setBottom(std::min(point.y, bounds.getBottom()));
bounds.setTop(std::max(point.y, bounds.getTop()));
}
return bounds;
}
}
TransferFunctionWidget::TransferFunctionWidget() :
QOpenGLWidget(),
_pointRenderer(this),
_isInitialized(false),
_backgroundColor(255, 255, 255, 255),
_widgetSizeInfo(),
_dataRectangleAction(this, "Data rectangle"),
_pixelSelectionTool(this),
_pixelRatio(1.0),
_mousePositions(),
_mouseIsPressed(false),
_areaSelectionBounds(0, 0, 0, 0) // Invalid Rectangle set to signal that no area is selected
{
setContextMenuPolicy(Qt::CustomContextMenu);
setAcceptDrops(true);
setMouseTracking(true);
setFocusPolicy(Qt::ClickFocus);
grabGesture(Qt::PinchGesture);
//setAttribute(Qt::WA_TranslucentBackground);
installEventFilter(this);
_pixelSelectionTool.setEnabled(true);
_pixelSelectionTool.setMainColor(QColor(std::rand() % 256, std::rand() % 256, std::rand() % 256));
_pixelSelectionTool.setFixedBrushRadiusModifier(Qt::AltModifier);
setSelectionOutlineHaloEnabled(false);
connect(&_pixelSelectionTool, &PixelSelectionTool::shapeChanged, [this]() {
if (isInitialized())
update();
});
connect(&_pixelSelectionTool, &PixelSelectionTool::ended, [this]() {
if (isInitialized() && _pixelSelectionTool.isEnabled() && _areaSelectionBounds.isValid() && _createShape) {
const QRectF relativeRect(
static_cast<float>(_areaSelectionBounds.left() - _boundsPointsWindow.left()) / _boundsPointsWindow.width(),
static_cast<float>(_areaSelectionBounds.top() - _boundsPointsWindow.top()) / _boundsPointsWindow.height(),
static_cast<float>(_areaSelectionBounds.width()) / _boundsPointsWindow.width(),
static_cast<float>(_areaSelectionBounds.height()) / _boundsPointsWindow.height()
);
const int borderWidth = 2;
const QRectF adjustedBounds = _areaSelectionBounds.adjusted(borderWidth, borderWidth, -borderWidth, -borderWidth); // The areapixmap doesn't contain the borders
QColor areaColor = _pixelSelectionTool.getMainColor();
areaColor.setAlpha(50); // This is the default modification of the areaColor compared to the mainColor which we can not reach but we simulate here
_interactiveShapes.push_back(InteractiveShape(_pixelSelectionTool.getAreaPixmap().copy(adjustedBounds.toRect()), relativeRect, _boundsPointsWindow, areaColor, _globalAlphaValue));
emit shapeCreated(_interactiveShapes);
_areaSelectionBounds = QRect(0, 0, 0, 0); // Invalid Rectangle set to signal that no area is selected
update();
_pixelSelectionTool.setMainColor(QColor(std::rand() % 256, std::rand() % 256, std::rand() % 256));
}
});
QSurfaceFormat surfaceFormat;
surfaceFormat.setRenderableType(QSurfaceFormat::OpenGL);
surfaceFormat.setVersion(3, 3);
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
surfaceFormat.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
surfaceFormat.setSamples(16);
surfaceFormat.setStencilBufferSize(8);
#ifdef _DEBUG
surfaceFormat.setOption(QSurfaceFormat::DebugContext);
#endif
setFormat(surfaceFormat);
// Call updatePixelRatio when the window is moved between hi and low dpi screens
// e.g., from a laptop display to a projector
// Wait with the connection until we are sure that the window is created
connect(this, &TransferFunctionWidget::created, this, [this](){
[[maybe_unused]] auto windowID = this->window()->winId(); // This is needed to produce a valid windowHandle on some systems
QWindow* winHandle = windowHandle();
// On some systems we might need to use a different windowHandle
if(!winHandle)
{
const QWidget* nativeParent = nativeParentWidget();
winHandle = nativeParent->windowHandle();
}
if(winHandle == nullptr)
{
qDebug() << "TransferFunctionWidget: Not connecting updatePixelRatio - could not get window handle";
return;
}
QObject::connect(winHandle, &QWindow::screenChanged, this, &TransferFunctionWidget::updatePixelRatio, Qt::UniqueConnection);
});
connect(&_pointRenderer.getNavigator(), &Navigator2D::zoomRectangleWorldChanged, this, [this]() -> void { update(); });
_pointRenderer.getNavigator().setEnabled(true);
}
bool TransferFunctionWidget::event(QEvent* event)
{
if (!event)
return QOpenGLWidget::event(event);
// The below three cases are for interactive objects
switch (event->type())
{
case QEvent::MouseButtonPress:
{
if (const auto* mouseEvent = static_cast<QMouseEvent*>(event)) {
_mouseIsPressed = true;
_mousePositions << mouseEvent->pos();
for (auto shape = _interactiveShapes.begin(); shape != _interactiveShapes.end(); ++shape) {
SelectedSide side;
if (shape->isNearTopRightCorner(mouseEvent->pos())) {
_interactiveShapes.erase(shape);
_selectedObject = nullptr;
emit shapeDeleted(_interactiveShapes);
update();
break;
}
if (shape->isNearSide(mouseEvent->pos(), side)) {
qDebug() << "Object side selected for resizing";
_createShape = false;
_pixelSelectionTool.setEnabled(false);
shape->setSelected(true);
_selectedObject = &(*shape);
_selectedSide = side;
break;
}
if (shape->contains(mouseEvent->pos())) {
_createShape = false;
_pixelSelectionTool.setEnabled(false);
shape->setSelected(true);
_selectedObject = &(*shape);
break;
}
}
if (!_selectedObject || !_selectedObject->getSelected())
_createShape = true;
else
emit shapeSelected(_selectedObject);
break;
}
}
case QEvent::MouseMove:
{
if (_mouseIsPressed) {
if (const auto* mouseEvent = static_cast<QMouseEvent*>(event)) {
if (_selectedObject && _selectedObject->getSelected()) {
auto delta = mouseEvent->pos() - _mousePositions[_mousePositions.size() - 1];
if (_selectedSide != SelectedSide::None) {
_selectedObject->resizeBy(delta, _selectedSide);
}
else {
_selectedObject->moveBy(delta);
}
}
else {
if (_pixelSelectionTool.getType() == PixelSelectionType::Rectangle) {
QPoint topLeft(
std::min(_mousePositions[0].x(), mouseEvent->pos().x()),
std::min(_mousePositions[0].y(), mouseEvent->pos().y())
);
QPoint bottomRight(
std::max(_mousePositions[0].x(), mouseEvent->pos().x()),
std::max(_mousePositions[0].y(), mouseEvent->pos().y())
);
_areaSelectionBounds = QRect(topLeft, bottomRight); // Needed to create a valid QRect object
}
else
_areaSelectionBounds = getMousePositionsBounds(mouseEvent->pos());
}
_mousePositions << mouseEvent->pos();
update();
}
}
break;
}
case QEvent::MouseButtonRelease:
{
if (_selectedObject && _selectedObject->getSelected()) {
_selectedObject->setSelected(false);
_selectedSide = SelectedSide::None;
_pixelSelectionTool.setEnabled(true);
}
_mousePositions.clear();
_mouseIsPressed = false;
update();
break;
}
}
return QOpenGLWidget::event(event);
}
QRect TransferFunctionWidget::getMousePositionsBounds(QPoint newMousePosition) {
if (!_areaSelectionBounds.isValid()) {
_areaSelectionBounds = QRect(_mousePositions[0], _mousePositions[0]);
}
const int left = std::min(_areaSelectionBounds.left(), newMousePosition.x());
const int right = std::max(_areaSelectionBounds.right(), newMousePosition.x());
const int top = std::min(_areaSelectionBounds.top(), newMousePosition.y());
const int bottom = std::max(_areaSelectionBounds.bottom(), newMousePosition.y());
return { QPoint(left, top), QPoint(right, bottom) };
}
bool TransferFunctionWidget::isInitialized() const
{
return _isInitialized;
}
PixelSelectionTool& TransferFunctionWidget::getPixelSelectionTool()
{
return _pixelSelectionTool;
}
// Positions need to be passed as a pointer as we need to store them locally in order
// to be able to find the subset of data that's part of a selection. If passed
// by reference then we can upload the data to the GPU, but not store it in the widget.
void TransferFunctionWidget::setData(const std::vector<Vector2f>* points)
{
const auto dataBounds = getDataBounds(*points);
// pass un-adjusted data bounds to renderer for 2D colormapping
const auto dataBoundsRect = QRectF(QPointF(dataBounds.getLeft(), dataBounds.getBottom()), QSizeF(dataBounds.getWidth(), dataBounds.getHeight()));
_pointRenderer.setDataBounds(dataBoundsRect);
_dataRectangleAction.setBounds(dataBounds);
const int w = width();
const int h = height();
const int size = w < h ? w : h;
_boundsPointsWindow = QRect((w - size) / 2.0f, (h - size) / 2.0f, size, size);
_pointRenderer.setData(*points);
_pointRenderer.getNavigator().resetView(true);
update();
}
void TransferFunctionWidget::setHighlights(const std::vector<char>& highlights, const std::int32_t& numSelectedPoints)
{
_pointRenderer.setHighlights(highlights, numSelectedPoints);
update();
}
void TransferFunctionWidget::setPointSize(float pointSize)
{
_pointRenderer.setPointSize(pointSize);
update();
}
void TransferFunctionWidget::setPointOpacity(float pointOpacity)
{
_pointRenderer.setAlpha(pointOpacity);
update();
}
void TransferFunctionWidget::showHighlights(bool show)
{
_pointRenderer.setSelectionOutlineScale(show ? 0.5f : 0);
update();
}
void TransferFunctionWidget::setGlobalAlphaToggle(bool useGlobalAlpha)
{
_useGlobalAlpha = useGlobalAlpha;
update();
}
void TransferFunctionWidget::setGlobalAlphaValue(int globalAlphaValue)
{
_globalAlphaValue = globalAlphaValue;
for (InteractiveShape& shape : _interactiveShapes)
{
shape.setGlobalAlphaValue(globalAlphaValue);
}
update();
}
PointSelectionDisplayMode TransferFunctionWidget::getSelectionDisplayMode() const
{
return _pointRenderer.getSelectionDisplayMode();
}
void TransferFunctionWidget::setSelectionDisplayMode(PointSelectionDisplayMode selectionDisplayMode)
{
_pointRenderer.setSelectionDisplayMode(selectionDisplayMode);
update();
}
void TransferFunctionWidget::setSelectionOutlineHaloEnabled(bool selectionOutlineHaloEnabled)
{
_pointRenderer.setSelectionHaloEnabled(selectionOutlineHaloEnabled);
update();
}
void TransferFunctionWidget::setRandomizedDepthEnabled(bool randomizedDepth)
{
_pointRenderer.setRandomizedDepthEnabled(randomizedDepth);
update();
}
bool TransferFunctionWidget::getRandomizedDepthEnabled() const
{
return _pointRenderer.getRandomizedDepthEnabled();
}
void TransferFunctionWidget::initializeGL()
{
initializeOpenGLFunctions();
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &TransferFunctionWidget::cleanup);
// Initialize renderers
_pointRenderer.init();
// Set a default color map for both renderers
_pointRenderer.setScalarEffect(PointEffect::Color);
_pointRenderer.setPointScaling(Absolute);
_pointRenderer.setSelectionOutlineColor(Vector3f(1, 0, 0));
createDatasets();
_boundsPointsWindow = QRect(0, 0, width(), height());
// OpenGL is initialized
_isInitialized = true;
emit initialized();
}
void TransferFunctionWidget::createDatasets()
{
qDebug() << "Creating datasets";
_tfSourceDataset = mv::data().createDataset<Points>("Points", "Image data");
_tfTextures = mv::data().createDataset<Images>("Images", "TransferFunction texture", _tfSourceDataset);
_tfTextures->setType(ImageData::Type::Stack);
_tfTextures->setNumberOfImages(1);
_tfTextures->setNumberOfComponentsPerPixel(4);
_tfTextures->setImageSize(QSize(_tfTextureSize, _tfTextureSize));
_materialTransitionSourceDataset = mv::data().createDataset<Points>("Points", "Material transition data");
_materialTransitionTexture = mv::data().createDataset<Images>("Images", "Material transition texture", _materialTransitionSourceDataset);
_materialTransitionTexture->setType(ImageData::Type::Stack);
_materialTransitionTexture->setNumberOfImages(1);
_materialTransitionTexture->setNumberOfComponentsPerPixel(4);
_materialTransitionTexture->setImageSize(QSize(_materialTextureSize, _materialTextureSize));
_materialPositionSourceDataset = mv::data().createDataset<Points>("Points", "Material position data");
_materialPositionTexture = mv::data().createDataset<Images>("Images", "Material position texture", _materialPositionSourceDataset);
_materialPositionTexture->setType(ImageData::Type::Stack);
_materialPositionTexture->setNumberOfImages(1);
_materialPositionTexture->setNumberOfComponentsPerPixel(1);
_materialPositionTexture->setImageSize(QSize(_materialPositionTextureSize, _materialPositionTextureSize));
}
void TransferFunctionWidget::resizeGL(int w, int h)
{
_pointRenderer.resize(QSize(w, h));
}
void TransferFunctionWidget::paintGL()
{
if (!_isInitialized)
return;
try {
QPainter painter;
// Begin mixed OpenGL/native painting
if (!painter.begin(this))
throw std::runtime_error("Unable to begin painting");
// Draw layers with OpenGL
painter.beginNativePainting();
{
// Bind the framebuffer belonging to the widget
// glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject());
// Clear the widget to the background color
glClearColor(_backgroundColor.redF(), _backgroundColor.greenF(), _backgroundColor.blueF(), _backgroundColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the blending function
glEnable(GL_BLEND);
if (getRandomizedDepthEnabled())
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
_pointRenderer.render();
}
painter.endNativePainting();
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
// Draw the existing shapes
QImage materialMap = QImage(size(), QImage::Format_ARGB32);
QPainter shapePainter(&materialMap);
shapePainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
for (const auto& obj : _interactiveShapes) {
obj.draw(shapePainter, true, _useGlobalAlpha);
}
shapePainter.end();
paintPixelSelectionToolNative(_pixelSelectionTool, materialMap);
painter.drawImage(0, 0, materialMap);
painter.end();
updateTfTexture();
updateMaterialPositionsTexture();
}
catch (std::exception& e)
{
exceptionMessageBox("Rendering failed", e);
}
catch (...) {
exceptionMessageBox("Rendering failed");
}
}
void TransferFunctionWidget::paintPixelSelectionToolNative(PixelSelectionTool& pixelSelectionTool, QImage& image) const
{
if (!pixelSelectionTool.isEnabled())
return;
QPainter pixelSelectionToolImagePainter(&image);
pixelSelectionToolImagePainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
pixelSelectionToolImagePainter.drawPixmap(rect(), pixelSelectionTool.getShapePixmap());
pixelSelectionToolImagePainter.drawPixmap(rect(), pixelSelectionTool.getAreaPixmap());
pixelSelectionToolImagePainter.end();
}
void TransferFunctionWidget::updateTfTexture()
{
if (!_tfTextures.isValid())
return;
auto materialMap = QImage(_boundsPointsWindow.width(), _boundsPointsWindow.height(), QImage::Format_ARGB32);
QPainter painter(&materialMap);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
for (const auto& obj : _interactiveShapes) {
obj.draw(painter, false, _useGlobalAlpha, false);
}
painter.end();
std::vector<float> data;
data.reserve(_tfTextureSize * _tfTextureSize * 4);
for (int y = _tfTextureSize - 1; y >= 0; y--) {
for (int x = 0; x < _tfTextureSize; x++) {
const int normalizedX = x * materialMap.width() / _tfTextureSize;
const int normalizedY = y * materialMap.height() / _tfTextureSize;
const QColor color = materialMap.pixelColor(normalizedX, normalizedY);
data.push_back(color.redF());
data.push_back(color.greenF());
data.push_back(color.blueF());
data.push_back(color.alphaF());
}
}
_tfSourceDataset->setData(data, 4); // update the data in the dataset
events().notifyDatasetDataChanged(_tfSourceDataset);
events().notifyDatasetDataChanged(_tfTextures);
}
void TransferFunctionWidget::updateMaterialPositionsTexture()
{
if (!_materialPositionTexture.isValid())
return;
auto materialMap = QImage(_boundsPointsWindow.width(), _boundsPointsWindow.height(), QImage::Format_ARGB32);
QPainter painter(&materialMap);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
int id = 1;
for (auto& obj : _interactiveShapes) {
obj.drawID(painter, false, id);
id++;
}
painter.end();
std::vector<float> data;
data.reserve(_materialPositionTextureSize * _materialPositionTextureSize);
for (int y = _materialPositionTextureSize - 1; y >= 0; y--) {
for (int x = 0; x < _materialPositionTextureSize; x++) {
const int normalizedX = x * materialMap.width() / _materialPositionTextureSize;
const int normalizedY = y * materialMap.height() / _materialPositionTextureSize;
const QColor color = materialMap.pixelColor(normalizedX, normalizedY);
data.push_back(color.redF());
}
}
_materialPositionSourceDataset->setData(data, 1); // update the data in the dataset
events().notifyDatasetDataChanged(_materialPositionSourceDataset);
events().notifyDatasetDataChanged(_materialPositionTexture);
}
void TransferFunctionWidget::updateMaterialTransitionTexture(const std::vector<std::vector<QColor>>& transitionsTable)
{
if (!_materialTransitionTexture.isValid())
return;
// A table of all shape trasitions, the absence of a colored area is its own material
std::vector<float> data;
data.reserve(_materialTextureSize * _materialTextureSize * 4);
//for (int y = _materialTextureSize - 1; y >= 0; y--) {
for (size_t y = 0; y < _materialTextureSize; y++) {
for (size_t x = 0; x < _materialTextureSize; x++) {
if (y < transitionsTable.size() && x < transitionsTable[y].size()) // If the transition table is not big enough, we fill the rest with black
{
const QColor color = transitionsTable[y][x];
data.push_back(color.redF());
data.push_back(color.greenF());
data.push_back(color.blueF());
data.push_back(color.alphaF());
}
else
{
data.push_back(0.0f);
data.push_back(0.0f);
data.push_back(0.0f);
data.push_back(0.0f);
}
}
}
_materialTransitionSourceDataset->setData(data, 4); // update the data in the dataset
events().notifyDatasetDataChanged(_materialTransitionSourceDataset);
events().notifyDatasetDataChanged(_materialTransitionTexture);
}
void TransferFunctionWidget::cleanup()
{
qDebug() << "Deleting transferFunction widget, performing clean up...";
_isInitialized = false;
makeCurrent();
_pointRenderer.destroy();
}
void TransferFunctionWidget::updatePixelRatio()
{
const float pixelRatio = devicePixelRatio();
// we only update if the ratio actually changed
if( _pixelRatio != pixelRatio )
{
_pixelRatio = pixelRatio;
resizeGL(width(), height());
update();
}
}
TransferFunctionWidget::~TransferFunctionWidget()
{
disconnect(QOpenGLWidget::context(), &QOpenGLContext::aboutToBeDestroyed, this, &TransferFunctionWidget::cleanup);
cleanup();
}