#include "scatterplot.h" #include #include #include "continuouscolorscale.h" #include "geometry.h" // Glyphs settings static const Color DEFAULT_GLYPH_COLOR(255, 255, 255); static const float DEFAULT_GLYPH_SIZE = 8.0f; static const float GLYPH_OPACITY = 1.0f; static const float GLYPH_OPACITY_SELECTED = 1.0f; static const float GLYPH_OUTLINE_WIDTH = 2.0f; static const Color GLYPH_OUTLINE_COLOR(0, 0, 0); static const Color GLYPH_OUTLINE_COLOR_SELECTED(20, 255, 225); // Brush settings // BRUSHING_MAX_DIST in quadtree.cpp static const float CROSSHAIR_LENGTH = 8.0f; static const float CROSSHAIR_THICKNESS1 = 1.0f; static const float CROSSHAIR_THICKNESS2 = 0.5f; static const Color CROSSHAIR_COLOR1(255, 255, 255); static const Color CROSSHAIR_COLOR2(0, 0, 0); // Selection settings static const Color SELECTION_COLOR(128, 128, 128, 96); // Mouse buttons // static const Qt::MouseButton NORMAL_BUTTON = Qt::LeftButton; // static const Qt::MouseButton SPECIAL_BUTTON = Qt::RightButton; Scatterplot::Scatterplot() : m_glyphSize(DEFAULT_GLYPH_SIZE) , m_colorScale(0) , m_autoScale(true) , m_sx(0, 1, 0, 1) , m_sy(0, 1, 0, 1) , m_anySelected(false) , m_brushedItem(-1) , m_interactionState(StateNone) , m_dragEnabled(false) , m_shouldUpdateGeometry(false) , m_shouldUpdateMaterials(false) { } void Scatterplot::setColorScale(std::shared_ptr colorScale) { m_colorScale = colorScale; if (m_colorData.n_elem > 0) { m_shouldUpdateMaterials = true; update(); } } arma::mat Scatterplot::XY() const { return m_xy; } void Scatterplot::setXY(const arma::mat &xy) { if (xy.n_cols != 2) { return; } m_xy = xy; xyChanged(m_xy); if (m_autoScale) { autoScale(); } updateQuadTree(); if (m_selection.size() != m_xy.n_rows) { m_selection.assign(m_xy.n_rows, false); } if (m_opacityData.n_elem != m_xy.n_rows) { // Reset opacity data m_opacityData.resize(xy.n_rows); m_opacityData.fill(GLYPH_OPACITY); opacityDataChanged(m_opacityData); } m_shouldUpdateGeometry = true; update(); } void Scatterplot::setColorData(const arma::vec &colorData) { if (m_xy.n_rows > 0 && (colorData.n_elem > 0 && colorData.n_elem != m_xy.n_rows)) { return; } m_colorData = colorData; colorDataChanged(m_colorData); m_shouldUpdateMaterials = true; update(); } void Scatterplot::setOpacityData(const arma::vec &opacityData) { if (m_xy.n_rows > 0 && opacityData.n_elem != m_xy.n_rows) { return; } m_opacityData = opacityData; opacityDataChanged(m_opacityData); update(); } void Scatterplot::setScale(const LinearScale &sx, const LinearScale &sy) { m_sx = sx; m_sy = sy; scaleChanged(m_sx, m_sy); updateQuadTree(); m_shouldUpdateGeometry = true; update(); } void Scatterplot::setAutoScale(bool autoScale) { m_autoScale = autoScale; if (autoScale) { this->autoScale(); } } void Scatterplot::autoScale() { m_sx.setDomain(m_xy.col(0).min(), m_xy.col(0).max()); m_sy.setDomain(m_xy.col(1).min(), m_xy.col(1).max()); scaleChanged(m_sx, m_sy); } void Scatterplot::setGlyphSize(float glyphSize) { if (m_glyphSize == glyphSize || glyphSize < 2.0f) { return; } m_glyphSize = glyphSize; glyphSizeChanged(m_glyphSize); m_shouldUpdateGeometry = true; update(); } void Scatterplot::update() { } /* QSGNode *Scatterplot::newSceneGraph() { // NOTE: // The hierarchy in the scene graph is as follows: // root [[splatNode] [glyphsRoot [glyph [...]]] [selectionNode]] QSGNode *root = new QSGNode; QSGNode *glyphTreeRoot = newGlyphTree(); if (glyphTreeRoot) { root->appendChildNode(glyphTreeRoot); } QSGSimpleRectNode *selectionRectNode = new QSGSimpleRectNode; selectionRectNode->setColor(SELECTION_COLOR); root->appendChildNode(selectionRectNode); QSGTransformNode *brushNode = new QSGTransformNode; QSGGeometryNode *whiteCrossHairNode = new QSGGeometryNode; QSGGeometry *whiteCrossHairGeom = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 12); whiteCrossHairGeom->setDrawingMode(GL_POLYGON); whiteCrossHairGeom->setVertexDataPattern(QSGGeometry::DynamicPattern); updateCrossHairGeometry(whiteCrossHairGeom, 0, 0, CROSSHAIR_THICKNESS1, CROSSHAIR_LENGTH); QSGFlatColorMaterial *whiteCrossHairMaterial = new QSGFlatColorMaterial; whiteCrossHairMaterial->setColor(CROSSHAIR_COLOR1); whiteCrossHairNode->setGeometry(whiteCrossHairGeom); whiteCrossHairNode->setMaterial(whiteCrossHairMaterial); whiteCrossHairNode->setFlags(QSGNode::OwnsGeometry | QSGNode::OwnsMaterial); brushNode->appendChildNode(whiteCrossHairNode); QSGGeometryNode *blackCrossHairNode = new QSGGeometryNode; QSGGeometry *blackCrossHairGeom = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 12); blackCrossHairGeom->setDrawingMode(GL_POLYGON); blackCrossHairGeom->setVertexDataPattern(QSGGeometry::DynamicPattern); updateCrossHairGeometry(blackCrossHairGeom, 0, 0, CROSSHAIR_THICKNESS2, CROSSHAIR_LENGTH); QSGFlatColorMaterial *blackCrossHairMaterial = new QSGFlatColorMaterial; blackCrossHairMaterial->setColor(CROSSHAIR_COLOR2); blackCrossHairNode->setGeometry(blackCrossHairGeom); blackCrossHairNode->setMaterial(blackCrossHairMaterial); blackCrossHairNode->setFlags(QSGNode::OwnsGeometry | QSGNode::OwnsMaterial); brushNode->appendChildNode(blackCrossHairNode); root->appendChildNode(brushNode); return root; } */ /* QSGNode *Scatterplot::newGlyphTree() { // NOTE: // The glyph graph is structured as: // root [opacityNode [outlineNode fillNode] ...] if (m_xy.n_rows < 1) { return 0; } QSGNode *node = new QSGNode; int vertexCount = calculateCircleVertexCount(m_glyphSize); for (arma::uword i = 0; i < m_xy.n_rows; i++) { QSGGeometry *glyphOutlineGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount); glyphOutlineGeometry->setDrawingMode(GL_POLYGON); glyphOutlineGeometry->setVertexDataPattern(QSGGeometry::DynamicPattern); QSGGeometryNode *glyphOutlineNode = new QSGGeometryNode; glyphOutlineNode->setGeometry(glyphOutlineGeometry); glyphOutlineNode->setFlag(QSGNode::OwnsGeometry); QSGFlatColorMaterial *material = new QSGFlatColorMaterial; material->setColor(GLYPH_OUTLINE_COLOR); glyphOutlineNode->setMaterial(material); glyphOutlineNode->setFlag(QSGNode::OwnsMaterial); QSGGeometry *glyphGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount); glyphGeometry->setDrawingMode(GL_POLYGON); glyphGeometry->setVertexDataPattern(QSGGeometry::DynamicPattern); QSGGeometryNode *glyphNode = new QSGGeometryNode; glyphNode->setGeometry(glyphGeometry); glyphNode->setFlag(QSGNode::OwnsGeometry); material = new QSGFlatColorMaterial; material->setColor(DEFAULT_GLYPH_COLOR); glyphNode->setMaterial(material); glyphNode->setFlag(QSGNode::OwnsMaterial); // Place the glyph geometry node under an opacity node QSGOpacityNode *glyphOpacityNode = new QSGOpacityNode; glyphOpacityNode->appendChildNode(glyphOutlineNode); glyphOpacityNode->appendChildNode(glyphNode); node->appendChildNode(glyphOpacityNode); } return node; } */ /* QSGNode *Scatterplot::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) { QSGNode *root = oldNode ? oldNode : newSceneGraph(); if (m_xy.n_rows < 1) { return root; } // This keeps track of where we are in the scene when updating QSGNode *node = root->firstChild(); updateGlyphs(node); node = node->nextSibling(); if (m_shouldUpdateGeometry) { m_shouldUpdateGeometry = false; } if (m_shouldUpdateMaterials) { m_shouldUpdateMaterials = false; } // Selection QSGSimpleRectNode *selectionNode = static_cast(node); if (m_interactionState == StateSelecting) { selectionNode->setRect(QRectF(m_dragOriginPos, m_dragCurrentPos)); selectionNode->markDirty(QSGNode::DirtyGeometry); } else { // Hide selection rect selectionNode->setRect(QRectF(-1, -1, 0, 0)); } node = node->nextSibling(); // Brushing updateBrush(node); node = node->nextSibling(); return root; } */ /* void Scatterplot::updateGlyphs(QSGNode *glyphsNode) { qreal x, y, tx, ty, moveTranslationF; if (!m_shouldUpdateGeometry && !m_shouldUpdateMaterials) { return; } if (m_interactionState == StateMoving) { tx = m_dragCurrentPos.x() - m_dragOriginPos.x(); ty = m_dragCurrentPos.y() - m_dragOriginPos.y(); } else { tx = ty = 0; } m_sx.setRange(PADDING, width() - PADDING); m_sy.setRange(height() - PADDING, PADDING); QSGNode *node = glyphsNode->firstChild(); for (arma::uword i = 0; i < m_xy.n_rows; i++) { const arma::rowvec &row = m_xy.row(i); bool isSelected = m_selection[i]; QSGOpacityNode *glyphOpacityNode = static_cast(node); glyphOpacityNode->setOpacity(m_opacityData[i]); QSGGeometryNode *glyphOutlineNode = static_cast(node->firstChild()); QSGGeometryNode *glyphNode = static_cast(node->firstChild()->nextSibling()); if (m_shouldUpdateGeometry) { moveTranslationF = isSelected ? 1.0 : 0.0; x = m_sx(row[0]) + tx * moveTranslationF; y = m_sy(row[1]) + ty * moveTranslationF; QSGGeometry *geometry = glyphOutlineNode->geometry(); updateCircleGeometry(geometry, m_glyphSize, x, y); glyphOutlineNode->markDirty(QSGNode::DirtyGeometry); geometry = glyphNode->geometry(); updateCircleGeometry(geometry, m_glyphSize - 2*GLYPH_OUTLINE_WIDTH, x, y); glyphNode->markDirty(QSGNode::DirtyGeometry); } if (m_shouldUpdateMaterials) { QSGFlatColorMaterial *material = static_cast(glyphOutlineNode->material()); material->setColor(isSelected ? GLYPH_OUTLINE_COLOR_SELECTED : GLYPH_OUTLINE_COLOR); glyphOutlineNode->markDirty(QSGNode::DirtyMaterial); material = static_cast(glyphNode->material()); if (m_colorData.n_elem > 0) { material->setColor(m_colorScale->color(m_colorData[i])); } else { material->setColor(DEFAULT_GLYPH_COLOR); } glyphNode->markDirty(QSGNode::DirtyMaterial); } node = node->nextSibling(); } // XXX: Beware: QSGNode::DirtyForceUpdate is undocumented // // This causes the scene graph to correctly update the materials of glyphs, // even though we individually mark dirty materials. Used to work in Qt 5.5, // though. glyphsNode->markDirty(QSGNode::DirtyForceUpdate); } */ /* void Scatterplot::updateBrush(QSGNode *node) { QMatrix4x4 transform; if (m_brushedItem < 0 || (m_interactionState != StateNone && m_interactionState != StateSelected)) { transform.translate(-width(), -height()); } else { const arma::rowvec &row = m_xy.row(m_brushedItem); transform.translate(m_sx(row[0]), m_sy(row[1])); } QSGTransformNode *brushNode = static_cast(node); brushNode->setMatrix(transform); } */ /* void Scatterplot::mousePressEvent(QMouseEvent *event) { switch (m_interactionState) { case StateNone: case StateSelected: switch (event->button()) { case NORMAL_BUTTON: if (event->modifiers() == Qt::ShiftModifier && m_dragEnabled) { m_interactionState = StateMoving; m_dragOriginPos = event->localPos(); m_dragCurrentPos = m_dragOriginPos; } else { // We say 'brushing', but we mean 'selecting the current brushed // item' m_interactionState = StateBrushing; } break; case SPECIAL_BUTTON: m_interactionState = StateNone; m_selection.assign(m_selection.size(), false); selectionInteractivelyChanged(m_selection); m_shouldUpdateMaterials = true; update(); break; } break; case StateBrushing: case StateSelecting: case StateMoving: // Probably shouldn't reach these break; } } */ /* void Scatterplot::mouseMoveEvent(QMouseEvent *event) { switch (m_interactionState) { case StateBrushing: // Move while brushing becomes selecting, hence the 'fall through' m_interactionState = StateSelecting; m_dragOriginPos = event->localPos(); // fall through case StateSelecting: m_dragCurrentPos = event->localPos(); update(); break; case StateMoving: m_dragCurrentPos = event->localPos(); m_shouldUpdateGeometry = true; update(); break; case StateNone: case StateSelected: break; } } */ /* void Scatterplot::mouseReleaseEvent(QMouseEvent *event) { bool mergeSelection = (event->modifiers() == Qt::ControlModifier); switch (m_interactionState) { case StateBrushing: // Mouse clicked with brush target; set new selection or append to // current if (!mergeSelection) { m_selection.assign(m_selection.size(), false); } if (m_brushedItem == -1) { m_interactionState = StateNone; if (m_anySelected && !mergeSelection) { m_anySelected = false; selectionInteractivelyChanged(m_selection); m_shouldUpdateMaterials = true; update(); } } else { m_interactionState = StateSelected; m_selection[m_brushedItem] = !m_selection[m_brushedItem]; if (m_selection[m_brushedItem]) { m_anySelected = true; } selectionInteractivelyChanged(m_selection); m_shouldUpdateMaterials = true; update(); } break; case StateSelecting: { // Selecting points and mouse is now released; update selection and // brush interactiveSelection(mergeSelection); m_interactionState = m_anySelected ? StateSelected : StateNone; QPoint pos = event->pos(); m_brushedItem = m_quadtree->nearestTo(pos.x(), pos.y()); itemInteractivelyBrushed(m_brushedItem); m_shouldUpdateMaterials = true; update(); } break; case StateMoving: // Moving points and now stopped; apply manipulation m_interactionState = StateSelected; applyManipulation(); m_shouldUpdateGeometry = true; update(); m_dragOriginPos = m_dragCurrentPos; break; case StateNone: case StateSelected: break; } } */ /* void Scatterplot::hoverEnterEvent(QHoverEvent *event) { QPointF pos = event->posF(); m_brushedItem = m_quadtree->nearestTo(pos.x(), pos.y()); itemInteractivelyBrushed(m_brushedItem); update(); } */ /* void Scatterplot::hoverMoveEvent(QHoverEvent *event) { QPointF pos = event->posF(); m_brushedItem = m_quadtree->nearestTo(pos.x(), pos.y()); itemInteractivelyBrushed(m_brushedItem); update(); } */ /* void Scatterplot::hoverLeaveEvent(QHoverEvent *event) { m_brushedItem = -1; itemInteractivelyBrushed(m_brushedItem); update(); } */ void Scatterplot::interactiveSelection(bool mergeSelection) { if (!mergeSelection) { m_selection.assign(m_selection.size(), false); } std::vector selected; m_quadtree->query(RectF(m_dragOriginPos, m_dragCurrentPos), selected); for (auto i: selected) { m_selection[i] = true; } for (auto isSelected: m_selection) { if (isSelected) { m_anySelected = true; break; } } selectionInteractivelyChanged(m_selection); } void Scatterplot::setSelection(const std::vector &selection) { if (m_selection.size() != selection.size()) { return; } m_selection = selection; selectionChanged(m_selection); m_shouldUpdateMaterials = true; update(); } void Scatterplot::brushItem(int item) { m_brushedItem = item; update(); } void Scatterplot::applyManipulation() { m_sx.inverse(); m_sy.inverse(); LinearScale rx = m_sx; LinearScale ry = m_sy; m_sy.inverse(); m_sx.inverse(); float tx = m_dragCurrentPos.x() - m_dragOriginPos.x(); float ty = m_dragCurrentPos.y() - m_dragOriginPos.y(); for (std::vector::size_type i = 0; i < m_selection.size(); i++) { if (m_selection[i]) { arma::rowvec row = m_xy.row(i); row[0] = rx(m_sx(row[0]) + tx); row[1] = ry(m_sy(row[1]) + ty); m_xy.row(i) = row; } } updateQuadTree(); xyInteractivelyChanged(m_xy); } void Scatterplot::updateQuadTree() { m_sx.setRange(PADDING, width() - PADDING); m_sy.setRange(height() - PADDING, PADDING); m_quadtree.reset(new QuadTree(RectF(x(), y(), width(), height()))); for (arma::uword i = 0; i < m_xy.n_rows; i++) { const arma::rowvec &row = m_xy.row(i); m_quadtree->insert(m_sx(row[0]), m_sy(row[1]), (int) i); } }