Skip to content

Commit 5d908a2

Browse files
authored
Fix transfer function textures with new renderer use (#8)
* Constexpr members * Fix small warnings * Reorder init list * Let's be explicit in this function call * Simplify interactive shape methods * Pass by const ref * more const * Preallocate memory for texture generation * fix typo * Fix transfer function * Speed up transfer function generation * Format
1 parent 02650b5 commit 5d908a2

5 files changed

Lines changed: 148 additions & 84 deletions

File tree

DVRTransferFunction/src/InteractiveShape.cpp

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#include "InteractiveShape.h"
2+
3+
#include <renderers/PointRenderer.h>
4+
25
#include <QDebug>
36

4-
InteractiveShape::InteractiveShape(const QPixmap& pixmap, const QRectF& rect, const QRect& bounds, QColor pixmapColor, float globalAlphaValue, qreal threshold)
5-
: _pixmap(pixmap), _rect(rect), _bounds(bounds), _isSelected(false), _pixmapColor(pixmapColor), _globalAlphaValue(globalAlphaValue), _threshold(threshold) {
7+
InteractiveShape::InteractiveShape(const QPixmap& pixmap, const QRectF& rect, const QRect& bounds, const QColor& pixmapColor,
8+
int globalAlphaValue, mv::gui::PointRenderer* pointRenderer, qreal threshold):
9+
_pixmap(pixmap), _rect(rect), _bounds(bounds), _isSelected(false),
10+
_threshold(threshold), _pixmapColor(pixmapColor), _globalAlphaValue(globalAlphaValue), _pointRenderer(pointRenderer)
11+
{
12+
assert(pointRenderer);
613
_mask = _pixmap.createMaskFromColor(Qt::transparent);
714

815
_gradient1D = QImage(":textures/gaussian1D_texture", ".png");
@@ -15,8 +22,10 @@ InteractiveShape::InteractiveShape(const QPixmap& pixmap, const QRectF& rect, co
1522
setGlobalAlphaValue(globalAlphaValue); // This will create the globalAlphaPixmap
1623
}
1724

18-
void InteractiveShape::draw(QPainter& painter, bool drawBorder, bool useGlobalAlpha, bool normalizeWindow /*true*/, QColor borderColor /* Black */) const {
19-
const QRectF adjustedRect = normalizeWindow ? getRelativeRect() : getAbsoluteRect();
25+
void InteractiveShape::draw(QPainter& painter, bool drawBorder, bool useGlobalAlpha, bool normalizeWindow , QColor borderColor /* Black */, int scaleTo /* 0 */) const {
26+
const QRectF adjustedRect = normalizeWindow ? getRelativeRect() : getAdjustedWorldRect(scaleTo);
27+
const QPixmap& pixmap = useGlobalAlpha ? _globalAlphaColormap : _pixmap;
28+
2029
if (drawBorder) {
2130
QPen pen(borderColor);
2231
pen.setWidth(2);
@@ -29,18 +38,13 @@ void InteractiveShape::draw(QPainter& painter, bool drawBorder, bool useGlobalAl
2938
}
3039

3140
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
32-
if (useGlobalAlpha) {
33-
painter.drawPixmap(adjustedRect.toRect(), _globalAlphaColormap);
34-
}
35-
else {
36-
painter.drawPixmap(adjustedRect.toRect(), _pixmap);
37-
}
41+
painter.drawPixmap(adjustedRect.toRect(), pixmap);
3842
}
3943

40-
void InteractiveShape::drawID(QPainter& painter, bool normalizeWindow, int id) const {
41-
const QRectF adjustedRect = normalizeWindow ? getRelativeRect() : getAbsoluteRect();
44+
void InteractiveShape::drawID(QPainter& painter, bool normalizeWindow, int id, int scaleTo) const {
45+
const QRectF adjustedRect = normalizeWindow ? getRelativeRect() : getAdjustedWorldRect(scaleTo);
4246

43-
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
47+
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
4448

4549
QPixmap newPixmap(_pixmap.size());
4650
newPixmap.fill(Qt::transparent);
@@ -75,6 +79,7 @@ void InteractiveShape::resizeBy(const QPointF& delta, SelectedSide& side) {
7579
case SelectedSide::Bottom:
7680
_rect.setBottom(_rect.bottom() + delta.y() / _bounds.height());
7781
break;
82+
case SelectedSide::None: [[fallthrough]];
7883
default:
7984
break;
8085
}
@@ -154,7 +159,7 @@ QColor InteractiveShape::getColor() const {
154159
}
155160

156161
bool InteractiveShape::isNearTopRightCorner(const QPointF& point) const {
157-
QPointF topRight = getRelativeRect().topRight();
162+
const QPointF topRight = getRelativeRect().topRight();
158163
return (std::abs(point.x() - topRight.x()) <= _threshold && std::abs(point.y() - topRight.y()) <= _threshold);
159164
}
160165

@@ -166,7 +171,7 @@ void InteractiveShape::setBounds(const QRect& bounds) {
166171
_bounds = bounds;
167172
}
168173

169-
void InteractiveShape::updateGradient(gradientData data)
174+
void InteractiveShape::updateGradient(const gradientData& data)
170175
{
171176
_gradientData = data;
172177
if (_gradientData.gradient) {
@@ -179,11 +184,11 @@ void InteractiveShape::updateGradient(gradientData data)
179184
qCritical() << "Unknown texture ID: currently only 0 and 1 exist";
180185
return;
181186
}
182-
QSize pixmapSize = _pixmap.size();
183-
float biggestFit = std::max(gradient.width() / pixmapSize.width(), gradient.height() / pixmapSize.height());
187+
const QSize pixmapSize = _pixmap.size();
188+
const float biggestFit = std::max(gradient.width() / pixmapSize.width(), gradient.height() / pixmapSize.height());
184189

185190
gradient = gradient.copy(QRect((_gradientData.xOffset + ((1 - _gradientData.width) / 2)) * gradient.width(), (_gradientData.yOffset + ((1 - _gradientData.height) / 2)) * gradient.height(), _gradientData.width * gradient.width(), _gradientData.height * gradient.height()));
186-
gradient.scaled(_pixmap.size() * biggestFit); //scale to the ratio of the pixmap to simulate gradient width and height
191+
gradient.scaled(pixmapSize * biggestFit); //scale to the ratio of the pixmap to simulate gradient width and height
187192

188193
gradient = gradient.transformed(QTransform().rotate(_gradientData.rotation));
189194
//gradient = gradient.copy(QRect((gradient.width() - _pixmap.width()) / 2, (gradient.height() - _pixmap.height()) / 2, _pixmap.width(), _pixmap.height()));
@@ -244,20 +249,56 @@ void InteractiveShape::updatePixmap()
244249
}
245250
}
246251

252+
QRectF InteractiveShape::getAdjustedWorldRect(int scaleTo) const {
253+
254+
const auto left = -1.f * _pointRenderer->getDataBounds().left();
255+
const auto top = -1.f * _pointRenderer->getDataBounds().top();
256+
auto adjustedWorldRec = getWorldRect().adjusted(left, top, left, top);
257+
258+
const auto dataExtendX = _pointRenderer->getDataBounds().width();
259+
const auto dataExtendY = _pointRenderer->getDataBounds().height();
260+
261+
// Ensure rect is in data bounds
262+
adjustedWorldRec.setLeft(std::clamp(adjustedWorldRec.left(), 0., dataExtendX));
263+
adjustedWorldRec.setTop(std::clamp(adjustedWorldRec.top(), 0., dataExtendY));
264+
adjustedWorldRec.setRight(std::clamp(adjustedWorldRec.right(), 0., dataExtendX));
265+
adjustedWorldRec.setBottom(std::clamp(adjustedWorldRec.bottom(), 0., dataExtendY));
266+
267+
// Scale to [0, scaleTo]
268+
const auto scaleX = static_cast<qreal>(scaleTo) / dataExtendX;
269+
const auto scaleY = static_cast<qreal>(scaleTo) / dataExtendY;
270+
271+
adjustedWorldRec.setLeft(adjustedWorldRec.left() * scaleX);
272+
adjustedWorldRec.setTop(adjustedWorldRec.top() * scaleY);
273+
adjustedWorldRec.setRight(adjustedWorldRec.right() * scaleX);
274+
adjustedWorldRec.setBottom(adjustedWorldRec.bottom() * scaleY);
275+
276+
adjustedWorldRec = adjustedWorldRec.normalized();
277+
278+
return adjustedWorldRec;
279+
}
280+
281+
QRectF InteractiveShape::getWorldRect() const {
282+
const auto relRect = getRelativeRect();
283+
284+
const auto topLeft = _pointRenderer->getScreenPointToWorldPosition(_pointRenderer->getNavigator().getViewMatrix(), relRect.topLeft().toPoint());
285+
const auto bottomRight = _pointRenderer->getScreenPointToWorldPosition(_pointRenderer->getNavigator().getViewMatrix(), relRect.bottomRight().toPoint());
286+
287+
return QRectF{
288+
QPointF{ topLeft.x(), topLeft.y() },
289+
QPointF{ bottomRight.x(), bottomRight.y() }
290+
};
291+
}
292+
247293
QRectF InteractiveShape::getRelativeRect() const {
248-
return QRectF(
249-
_bounds.left() + _rect.left() * _bounds.width(),
250-
_bounds.top() + _rect.top() * _bounds.height(),
251-
_rect.width() * _bounds.width(),
252-
_rect.height() * _bounds.height()
253-
);
294+
return getAbsoluteRect().adjusted(_bounds.left(), _bounds.top(), _bounds.left(), _bounds.top());
254295
}
255296

256297
QRectF InteractiveShape::getAbsoluteRect() const {
257-
return QRectF(
298+
return {
258299
_rect.left() * _bounds.width(),
259300
_rect.top() * _bounds.height(),
260301
_rect.width() * _bounds.width(),
261302
_rect.height() * _bounds.height()
262-
);
303+
};
263304
}

DVRTransferFunction/src/InteractiveShape.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ struct gradientData {
2323
int rotation;
2424
};
2525

26+
namespace mv::gui
27+
{
28+
class PointRenderer;
29+
}
30+
2631
class InteractiveShape {
2732
public:
28-
InteractiveShape(const QPixmap& pixmap, const QRectF& rect, const QRect& bounds, QColor pixmapColor, float globalAlphaValue, qreal threshold = 10.0);
33+
InteractiveShape(const QPixmap& pixmap, const QRectF& rect, const QRect& bounds, const QColor& pixmapColor, int globalAlphaValue, mv::gui::PointRenderer* pointRenderer, qreal threshold = 10.0);
2934

30-
void draw(QPainter& painter, bool drawBorder, bool useGlobalAlpha, bool normalizeWindow = true, QColor borderColor = Qt::black) const;
31-
void drawID(QPainter& painter, bool normalizeWindow, int id) const;
35+
void draw(QPainter& painter, bool drawBorder, bool useGlobalAlpha, bool normalizeWindow, QColor borderColor = Qt::black, int scaleTo = 0) const;
36+
void drawID(QPainter& painter, bool normalizeWindow, int id, int scaleTo = 0) const;
3237
bool contains(const QPointF& point) const;
3338
void moveBy(const QPointF& delta);
3439
void resizeBy(const QPointF& delta, SelectedSide& side);
@@ -45,17 +50,18 @@ class InteractiveShape {
4550
void setThreshold(qreal threshold);
4651
void setBounds(const QRect& bounds);
4752

48-
void updateGradient(gradientData data);
53+
void updateGradient(const gradientData& data);
4954
QImage getGradientImage() const;
5055
gradientData getGradientData() const;
5156

5257
void setGlobalAlphaValue(int globalAlphaValue);
5358
QRectF getRelativeRect() const;
59+
QRectF getWorldRect() const;
60+
QRectF getAdjustedWorldRect(int scaleTo = 0) const;
5461

5562
private:
5663

5764
QRectF getAbsoluteRect() const;
58-
5965
void updatePixmap();
6066

6167
private:
@@ -78,4 +84,6 @@ class InteractiveShape {
7884
QImage _usedGradient;
7985

8086
gradientData _gradientData;
87+
88+
mv::gui::PointRenderer* _pointRenderer = nullptr;
8189
};

0 commit comments

Comments
 (0)