Commit b64a646d authored by Kuntal  Majumder's avatar Kuntal Majumder 😟

Reduced the number of calls KisPaintDevice::exactBounds() is called

Ref T10894
parent 3f74df11
......@@ -113,7 +113,9 @@ struct KisMagneticGraph{
KisMagneticGraph() { }
KisMagneticGraph(KisPaintDeviceSP dev, QRect graphRect):
m_rect(graphRect), m_dev(dev)
{ }
{
m_randAccess = m_dev->createRandomAccessorNG(m_dev->exactBounds().x(), m_dev->exactBounds().y());
}
typedef VertexDescriptor vertex_descriptor;
typedef std::pair<vertex_descriptor, vertex_descriptor> edge_descriptor;
......@@ -124,10 +126,9 @@ struct KisMagneticGraph{
typedef unsigned degree_size_type;
double getIntensity(VertexDescriptor pt){
KisRandomAccessorSP randAccess = m_dev->createRandomAccessorNG(m_dev->exactBounds().x(),m_dev->exactBounds().y());
randAccess->moveTo(pt.x, pt.y);
qint8 val = *(randAccess->rawData());
double getIntensity(VertexDescriptor pt) {
m_randAccess->moveTo(pt.x, pt.y);
qint8 val = *(m_randAccess->rawData());
//offsetting the value, so we don't get negatives
return val + 255;
}
......@@ -155,6 +156,7 @@ struct KisMagneticGraph{
private:
KisPaintDeviceSP m_dev;
KisRandomAccessorSP m_randAccess;
};
struct neighbour_iterator : public boost::iterator_facade<neighbour_iterator,
......
......@@ -140,6 +140,13 @@ private:
KisMagneticGraph m_graph;
};
KisMagneticWorker::KisMagneticWorker(KisPaintDeviceSP dev) :
m_dev(dev)
{
KisGaussianKernel::applyLoG(m_dev, m_dev->exactBounds(), 2, 1.0, QBitArray(), 0);
KisLazyFillTools::normalizeAndInvertAlpha8Device(m_dev, m_dev->exactBounds());
}
QRect KisMagneticWorker::calculateRect(QPoint p1, QPoint p2, int radius) const {
// I am sure there is a simpler version of it which exists but well
......@@ -178,18 +185,13 @@ QRect KisMagneticWorker::calculateRect(QPoint p1, QPoint p2, int radius) const {
return p.boundingRect();
}
QVector<QPointF> KisMagneticWorker::computeEdge(KisPaintDeviceSP dev, int radius, QPoint begin, QPoint end) {
QVector<QPointF> KisMagneticWorker::computeEdge(int radius, QPoint begin, QPoint end) {
//QRect rect = calculateRect(begin, end, radius);
QRect rect = QPolygon(QVector<QPoint>{begin, end}).boundingRect();
rect.setSize(rect.size()*10);
KisGaussianKernel::applyLoG(dev, rect, 2, 1.0, QBitArray(), 0);
KisLazyFillTools::normalizeAndInvertAlpha8Device(dev, rect);
VertexDescriptor goal(end);
VertexDescriptor start(begin);
KisMagneticGraph g(dev, rect);
KisMagneticGraph g(m_dev, m_dev->exactBounds());
// How many maps does it require?
// Take a look here, if it doesn't make sense, https://www.boost.org/doc/libs/1_70_0/libs/graph/doc/astar_search.html
......
......@@ -23,9 +23,12 @@
class KRITASELECTIONTOOLS_EXPORT KisMagneticWorker{
public:
QVector<QPointF> computeEdge(KisPaintDeviceSP dev, int radius, QPoint start, QPoint end);
KisMagneticWorker(KisPaintDeviceSP dev);
QVector<QPointF> computeEdge(int radius, QPoint start, QPoint end);
private:
QRect calculateRect(QPoint p1, QPoint p2, int radius) const;
QRect calculateRect(QPoint p1, QPoint p2, int radius) const;
KisPaintDeviceSP m_dev;
};
#endif
......@@ -48,9 +48,6 @@
#include "kis_algebra_2d.h"
#include "KisMagneticWorker.h"
#define FEEDBACK_LINE_WIDTH 2
......@@ -58,9 +55,8 @@ KisToolSelectMagnetic::KisToolSelectMagnetic(KoCanvasBase *canvas)
: KisToolSelect(canvas,
KisCursor::load("tool_magnetic_selection_cursor.svg", 16, 16),
i18n("Magnetic Selection")),
m_continuedMode(false), m_complete(true)
{
}
m_continuedMode(false), m_complete(true), m_worker(image()->projection())
{ }
KisToolSelectMagnetic::~KisToolSelectMagnetic()
{
......@@ -89,6 +85,7 @@ void KisToolSelectMagnetic::keyReleaseEvent(QKeyEvent *event)
KisToolSelect::keyReleaseEvent(event);
}
//the cursor is still tracked even when no mousebutton is pressed
void KisToolSelectMagnetic::mouseMoveEvent(KoPointerEvent *event)
{
KisToolSelect::mouseMoveEvent(event);
......@@ -99,8 +96,7 @@ void KisToolSelectMagnetic::mouseMoveEvent(KoPointerEvent *event)
m_lastCursorPos = convertToPixelCoord(event);
auto current = QPoint(m_lastCursorPos.x(), m_lastCursorPos.y());
//qDebug() << current;
KisMagneticWorker worker;
m_points = worker.computeEdge(image()->projection(), 2, m_lastAnchor, current);
m_points = m_worker.computeEdge(2, m_lastAnchor, current);
m_paintPath = QPainterPath();
m_paintPath.moveTo(pixelToView(m_points[m_points.count()-1]));
for(int i=m_points.count()-2; i>0;i--){
......@@ -114,6 +110,7 @@ void KisToolSelectMagnetic::mouseMoveEvent(KoPointerEvent *event)
}
}
//press primary mouse button
void KisToolSelectMagnetic::beginPrimaryAction(KoPointerEvent *event)
{
setMode(KisTool::PAINT_MODE);
......@@ -121,13 +118,16 @@ void KisToolSelectMagnetic::beginPrimaryAction(KoPointerEvent *event)
m_lastAnchor = QPoint(temp.x(), temp.y());
qDebug() << m_lastAnchor;
m_complete = !m_complete; //just for testing
m_worker = KisMagneticWorker(image()->projection());
}
//drag while primary mouse button is pressed
void KisToolSelectMagnetic::continuePrimaryAction(KoPointerEvent *event)
{
KisToolSelectBase::continuePrimaryAction(event);
}
//release primary mouse button
void KisToolSelectMagnetic::endPrimaryAction(KoPointerEvent *event)
{
KisToolSelectBase::endPrimaryAction(event);
......
......@@ -23,6 +23,7 @@
#include "KisSelectionToolFactoryBase.h"
#include <kis_tool_select_base.h>
#include <kis_icon.h>
#include "KisMagneticWorker.h"
class QPainterPath;
......@@ -63,6 +64,7 @@ private:
QPointF m_lastCursorPos;
QPoint m_lastAnchor;
bool m_complete;
KisMagneticWorker m_worker;
};
class KisToolSelectMagneticFactory : public KisSelectionToolFactoryBase
......
......@@ -43,14 +43,14 @@ void KisMagneticWorkerTest::testWorker()
KisPaintDeviceSP dev = loadTestImage("test_main.png", false);
const QRect rect = dev->exactBounds();
KisPaintDeviceSP grayscaleDev = KisPainter::convertToAlphaAsGray(dev);
KisMagneticWorker worker;
KisMagneticWorker worker(grayscaleDev);
KIS_DUMP_DEVICE_2(dev, rect, "main", "dd");
const QPoint startPos(40, 10);
const QPoint endPos(50, 65);
auto points = worker.computeEdge(grayscaleDev, 10, startPos, endPos);
auto points = worker.computeEdge(10, startPos, endPos);
KIS_DUMP_DEVICE_2(grayscaleDev, rect, "draw", "dd");
QVector<QPointF> result = { QPointF(50,65),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment