提交 75523a97 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

加入迭代直线归并过程

上级 8f97922f
...@@ -90,6 +90,9 @@ void demoWidget::paintEvent(QPaintEvent * evt) ...@@ -90,6 +90,9 @@ void demoWidget::paintEvent(QPaintEvent * evt)
} }
} }
QString it = QString("Iter = %1 times.").arg(iter);
painter.drawText(1,16,it);
evt->accept(); evt->accept();
} }
...@@ -120,7 +123,7 @@ void demoWidget::mousePressEvent(QMouseEvent * evt) ...@@ -120,7 +123,7 @@ void demoWidget::mousePressEvent(QMouseEvent * evt)
//找路径,不归并 //找路径,不归并
min_distance_find(v_mat, startx,starty, endx,endy, &rx,&ry,&ridx,false); min_distance_find(v_mat, startx,starty, endx,endy, &rx,&ry,&ridx,false);
//找路径,做归并 //找路径,做归并
min_distance_find(v_mat, startx,starty, endx,endy, &cx,&cy,&idx,true); iter = min_distance_find(v_mat, startx,starty, endx,endy, &cx,&cy,&idx,true);
update(); update();
} }
...@@ -12,6 +12,7 @@ public: ...@@ -12,6 +12,7 @@ public:
protected: protected:
//0,1联通矩阵,0是障碍,1是可通区域 //0,1联通矩阵,0是障碍,1是可通区域
std::vector<std::vector<char> > v_mat; std::vector<std::vector<char> > v_mat;
int iter = 0;
protected: protected:
void paintEvent(QPaintEvent * evt) override; void paintEvent(QPaintEvent * evt) override;
void mousePressEvent(QMouseEvent * evt) override; void mousePressEvent(QMouseEvent * evt) override;
......
...@@ -240,7 +240,8 @@ int min_dis_opt( ...@@ -240,7 +240,8 @@ int min_dis_opt(
const std::vector<int> & ry, const std::vector<int> & ry,
std::vector<int> * cx, std::vector<int> * cx,
std::vector<int> * cy, std::vector<int> * cy,
std::vector<int> * pidx std::vector<int> * pidx,
bool safe_join
) )
{ {
const int rows = v_mat.size(); const int rows = v_mat.size();
...@@ -288,6 +289,8 @@ int min_dis_opt( ...@@ -288,6 +289,8 @@ int min_dis_opt(
break; break;
if (tx ==x2 && ty==y2) if (tx ==x2 && ty==y2)
break; break;
if (!safe_join)
break;
} }
} }
...@@ -377,19 +380,47 @@ int min_distance_find( ...@@ -377,19 +380,47 @@ int min_distance_find(
std::vector<int> *x, std::vector<int> *x,
std::vector<int> *y, std::vector<int> *y,
std::vector<int> * pidx, std::vector<int> * pidx,
bool join bool join,
bool safe_join,
int max_itertimers
) )
{ {
std::vector<std::vector<unsigned int> > v_rev ; std::vector<std::vector<unsigned int> > v_rev ;
if (!mdf_rev_fill(v_mat,startx,starty,endx,endy, &v_rev)) if (!mdf_rev_fill(v_mat,startx,starty,endx,endy, &v_rev))
return false; return 0;
std::vector<int> cx,cy; std::vector<int> cx,cy;
if (!mdf_path_find(v_rev,startx,starty,&cx,&cy)) if (!mdf_path_find(v_rev,startx,starty,&cx,&cy))
return false; return 0;
if (join) if (join)
return min_dis_opt(v_mat,cx,cy,x,y,pidx); {
bool res = true;
int iter = 0;
while (res && iter<max_itertimers)
{
++iter;
res = res && min_dis_opt(v_mat,cx,cy,x,y,pidx,safe_join);
if (!res)
break;
if (cx.size()==x->size())
{
bool same = true;
const size_t szxc = cx.size();
for (size_t i=0;i<szxc && same;++i)
{
if ((*x)[i]!=cx[i] || (*y)[i]!=cy[i])
same = false;
}
if (same)
break;
}
pidx->clear();
cx = std::move(*x);
cy = std::move(*y);
}
return res?iter:0;
}
size_t sz = cx.size(); size_t sz = cx.size();
pidx->clear(); pidx->clear();
......
...@@ -10,6 +10,8 @@ int min_distance_find( ...@@ -10,6 +10,8 @@ int min_distance_find(
std::vector<int> *x,//着色点 std::vector<int> *x,//着色点
std::vector<int> *y, std::vector<int> *y,
std::vector<int> *pidx,//关键点 std::vector<int> *pidx,//关键点
bool join = true bool join = true,
bool safe_join = true,
int max_itertimers = 16
); );
#endif // FLOODFILL_MDF_H #endif // FLOODFILL_MDF_H
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册