Commit 463a9b18 authored by Kuntal  Majumder's avatar Kuntal Majumder 😟

The algorithm is almost perfect now

- using 8 point neighbors
- fixed the intensity function

Ref T10894
parent ca7340a0
......@@ -33,7 +33,7 @@ struct VertexDescriptor {
enum Direction {
MIN = 0,
N = MIN, S, E, W, NONE
N = MIN, S, E, W, NW, NE, SW, SE, NONE
};
VertexDescriptor(long _x, long _y):
......@@ -69,17 +69,27 @@ struct VertexDescriptor {
int dx = 0, dy = 0;
switch (direction){
case W:
dx = -1;
break;
case E:
dx = 1;
break;
case N:
dy = -1;
break;
case S:
dy = 1;
case W:
case SW:
case NW:
dx = -1;
break;
case E:
case SE:
case NE:
dx = 1;
}
switch(direction){
case N:
case NW:
case NE:
dy = -1;
break;
case S:
case SW:
case SE:
dy = 1;
}
VertexDescriptor const neighbor(x + dx, y + dy);
......@@ -116,7 +126,8 @@ struct KisMagneticGraph{
KisRandomAccessorSP randAccess = m_dev->createRandomAccessorNG(m_dev->exactBounds().x(),m_dev->exactBounds().y());
randAccess->moveTo(pt.x, pt.y);
qint8 val = *(randAccess->rawData());
return std::abs(val);
//offsetting the value, so we don't get negatives
return val + 255;
}
unsigned outDegree(VertexDescriptor pt){
......@@ -125,7 +136,7 @@ struct KisMagneticGraph{
pt == m_rect.bottomLeft() || pt == m_rect.bottomRight()){
if(m_rect.width() == 1 || m_rect.height() == 1)
return 1;
return 2;
return 3;
}
//edges
......@@ -133,10 +144,10 @@ struct KisMagneticGraph{
pt.x == m_rect.bottomRight().x() || pt.y == m_rect.bottomRight().y()){
if(m_rect.width() == 1 || m_rect.height() == 1)
return 2;
return 3;
return 5;
}
return 4;
return 8;
}
......@@ -154,9 +165,7 @@ struct neighbour_iterator : public boost::iterator_facade<neighbour_iterator,
neighbour_iterator(VertexDescriptor v, KisMagneticGraph g, VertexDescriptor::Direction d):
m_point(v), m_direction(d), m_graph(g)
{
//qDebug() << v;
}
{ }
neighbour_iterator()
{ }
......@@ -174,7 +183,6 @@ struct neighbour_iterator : public boost::iterator_facade<neighbour_iterator,
return;
}
if(!m_graph.m_rect.contains(next.x, next.y)){
qDebug() << next;
operator++();
}
}
......
......@@ -82,27 +82,19 @@ double EuclideanDistance(VertexDescriptor p1, VertexDescriptor p2){
class AStarHeuristic : public boost::astar_heuristic<KisMagneticGraph, double> {
private:
PredecessorMap m_pmap;
VertexDescriptor m_goal;
double coeff_a, coeff_b;
public:
AStarHeuristic(VertexDescriptor goal, PredecessorMap const &pmap, double a, double b):
m_pmap(pmap), m_goal(goal), coeff_a(a), coeff_b(b)
AStarHeuristic(VertexDescriptor goal, double a, double b):
m_goal(goal), coeff_a(a), coeff_b(b)
{ }
AStarHeuristic(VertexDescriptor goal, PredecessorMap const &pmap):
m_pmap(pmap), m_goal(goal), coeff_a(0.5), coeff_b(0.5)
AStarHeuristic(VertexDescriptor goal):
m_goal(goal), coeff_a(0.5), coeff_b(0.5)
{ }
double operator()(VertexDescriptor v){
auto prev = m_pmap[v];
double di = (m_goal.y - prev.y) * v.x + (m_goal.x - prev.x) * v.y;
di = std::abs(di + prev.x * m_goal.y + prev.y * m_goal.x);
double dz = EuclideanDistance(prev, m_goal);
di = di/dz;
double dm = EuclideanDistance(v, m_goal);
//return coeff_a * di + coeff_b * (dm - dz);
return EuclideanDistance(v,m_goal);
}
};
......@@ -137,7 +129,7 @@ struct WeightMap{
data_type& operator[](key_type const& k) {
if (m_map.find(k) == m_map.end()) {
double edge_gradient = m_graph.getIntensity((k.first)) + m_graph.getIntensity((k.second))/2;
double edge_gradient = (m_graph.getIntensity(k.first) + m_graph.getIntensity(k.second))/2;
m_map[k] = EuclideanDistance(k.first, k.second) * (edge_gradient + 1);
}
return m_map[k];
......@@ -196,7 +188,7 @@ QVector<QPointF> KisMagneticWorker::computeEdge(KisPaintDeviceSP dev, int radius
std::map<VertexDescriptor, boost::default_color_type> cmap;
std::map<VertexDescriptor, double> imap;
WeightMap wmap(g);
AStarHeuristic heuristic(goal, pmap);
AStarHeuristic heuristic(goal);
QVector<QPointF> result;
try{
......@@ -216,6 +208,7 @@ QVector<QPointF> KisMagneticWorker::computeEdge(KisPaintDeviceSP dev, int radius
}catch(GoalFound const&){
for(VertexDescriptor u=goal; u!=start; u = pmap[u]){
result.push_back(QPointF(u.x,u.y));
//qDebug() << g.getIntensity(u);
}
}
......
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