another version of tracing across buses
This commit is contained in:
@@ -1264,14 +1264,17 @@ void BlockView::trace(QPointF scene_pos_from, QPointF scene_pos_to, BlockBusItem
|
|||||||
double signx = (p1.x() >= p0.x() ? 1. : -1.), signy = (p1.y() >= p0.y() ? 1. : -1.);
|
double signx = (p1.x() >= p0.x() ? 1. : -1.), signy = (p1.y() >= p0.y() ? 1. : -1.);
|
||||||
int steps(0);
|
int steps(0);
|
||||||
if ((dx + dy) < grid_step) continue;
|
if ((dx + dy) < grid_step) continue;
|
||||||
|
BlockViewWavetrace::CellState cs = BlockViewWavetrace::Blocked;
|
||||||
if (dx >= dy) { // by x
|
if (dx >= dy) { // by x
|
||||||
sx = grid_step;
|
sx = grid_step;
|
||||||
sy = sx * dy / dx;
|
sy = sx * dy / dx;
|
||||||
steps = qRound(dx / grid_step);
|
steps = qRound(dx / grid_step);
|
||||||
|
cs = BlockViewWavetrace::HorizontalBus;
|
||||||
} else {
|
} else {
|
||||||
sy = grid_step;
|
sy = grid_step;
|
||||||
sx = sy * dx / dy;
|
sx = sy * dx / dy;
|
||||||
steps = qRound(dy / grid_step);
|
steps = qRound(dy / grid_step);
|
||||||
|
cs = BlockViewWavetrace::VerticalBus;
|
||||||
}
|
}
|
||||||
sx *= signx;
|
sx *= signx;
|
||||||
sy *= signy;
|
sy *= signy;
|
||||||
@@ -1279,7 +1282,7 @@ void BlockView::trace(QPointF scene_pos_from, QPointF scene_pos_to, BlockBusItem
|
|||||||
for (int j = 0; j < steps; ++j) {
|
for (int j = 0; j < steps; ++j) {
|
||||||
QPoint tp = quantize(cp, grid_step).toPoint() / grid_step + dp;
|
QPoint tp = quantize(cp, grid_step).toPoint() / grid_step + dp;
|
||||||
if (tp != qpt)
|
if (tp != qpt)
|
||||||
wavetrace.fill(tp, BlockViewWavetrace::Jump);
|
wavetrace.fill(tp, cs);
|
||||||
//qDebug() << " set" << cp;
|
//qDebug() << " set" << cp;
|
||||||
cp += QPointF(sx, sy);
|
cp += QPointF(sx, sy);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "blockviewwavetrace.h"
|
#include "blockviewwavetrace.h"
|
||||||
#include <QTime>
|
|
||||||
|
|
||||||
|
|
||||||
BlockViewWavetrace::BlockViewWavetrace(int width, int height) {
|
BlockViewWavetrace::BlockViewWavetrace(int width, int height) {
|
||||||
@@ -17,14 +16,13 @@ void BlockViewWavetrace::resize(int width, int height) {
|
|||||||
for (int i = 0; i < wid; ++i) {
|
for (int i = 0; i < wid; ++i) {
|
||||||
if (field[i].size() != hei) {
|
if (field[i].size() != hei) {
|
||||||
field[i].resize(hei);
|
field[i].resize(hei);
|
||||||
field[i].fill(-1);
|
field[i].fill(Cell());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BlockViewWavetrace::fill(short val) {
|
void BlockViewWavetrace::fill(short val) {
|
||||||
QTime tm; tm.restart();
|
|
||||||
for (int i = 0; i < wid; ++i) {
|
for (int i = 0; i < wid; ++i) {
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
field[i].fill(val);
|
field[i].fill(val);
|
||||||
@@ -37,11 +35,18 @@ void BlockViewWavetrace::fill(short val) {
|
|||||||
void BlockViewWavetrace::fill(const QRect & rect, short val) {
|
void BlockViewWavetrace::fill(const QRect & rect, short val) {
|
||||||
for (int i = rect.left(); i <= rect.right(); ++i)
|
for (int i = rect.left(); i <= rect.right(); ++i)
|
||||||
for (int j = rect.top(); j <= rect.bottom(); ++j)
|
for (int j = rect.top(); j <= rect.bottom(); ++j)
|
||||||
field[i][j] = val;
|
field[i][j].value = field[i][j].direction = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BlockViewWavetrace::fill(int px, int py, short val) {
|
void BlockViewWavetrace::fill(int px, int py, short val) {
|
||||||
field[px][py] = val;
|
short p = field[px][py].value;
|
||||||
|
if ((val == HorizontalBus && p == VerticalBus ) ||
|
||||||
|
(val == VerticalBus && p == HorizontalBus))
|
||||||
|
field[px][py].value = Blocked;
|
||||||
|
else
|
||||||
|
field[px][py].value = val;
|
||||||
|
field[px][py].direction = field[px][py].value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -51,44 +56,33 @@ bool BlockViewWavetrace::trace(const QPoint & start, const QPoint & finish) {
|
|||||||
if (dir_ == NoTrace) return true;
|
if (dir_ == NoTrace) return true;
|
||||||
//qDebug() << "trace" << start << finish;
|
//qDebug() << "trace" << start << finish;
|
||||||
//return true;
|
//return true;
|
||||||
int cx, cy;
|
|
||||||
short cl = 0;
|
short cl = 0;
|
||||||
QRect frect(0, 0, wid - 1, hei - 1);
|
QRect frect(0, 0, wid - 1, hei - 1);
|
||||||
QVector<QPoint> cpnts, npnts;
|
QVector<QPoint> cpnts, npnts;
|
||||||
fill(st, cl);
|
fill(st, cl);
|
||||||
jumps.clear();
|
cpnts.push_back(st);
|
||||||
cpnts.append(st);
|
if (field[fn.x()][fn.y()].value == (short)Blocked)
|
||||||
auto checkAndFill = [this, &npnts, &frect](int x, int y, short c) {
|
return false;
|
||||||
|
auto checkAndFill = [this, &npnts, &frect](int x, int y, short acc_dir, short c) {
|
||||||
if (!frect.contains(x, y)) return;
|
if (!frect.contains(x, y)) return;
|
||||||
short p = field[x][y];
|
short p = field[x][y].value;
|
||||||
if (p == (short)Empty || p == (short)Jump) {
|
if (p == (short)Empty || p == acc_dir) {
|
||||||
npnts.append(QPoint(x, y));
|
npnts.push_back(QPoint(x, y));
|
||||||
fill(x, y, c);
|
field[x][y].value = c;
|
||||||
}
|
|
||||||
if (p == (short)Jump) {
|
|
||||||
jumps.append(QPoint(x, y));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (field[fn.x()][fn.y()] < (short)Empty)
|
|
||||||
return false;
|
|
||||||
while (cpnts.size() > 0) {
|
while (cpnts.size() > 0) {
|
||||||
npnts.clear();
|
npnts.clear();
|
||||||
cl++;
|
cl++;
|
||||||
if (cl >= max_steps) return false;
|
if (cl >= max_steps)
|
||||||
|
return false;
|
||||||
for (int i = 0; i < cpnts.size(); ++i) {
|
for (int i = 0; i < cpnts.size(); ++i) {
|
||||||
if (cpnts[i] == fn) {
|
if (cpnts[i] == fn)
|
||||||
return true;
|
return true;
|
||||||
}
|
checkAndFill(cpnts[i].x() - 1, cpnts[i].y() , (short)VerticalBus , cl);
|
||||||
cx = cpnts[i].x() - 1;
|
checkAndFill(cpnts[i].x() + 1, cpnts[i].y() , (short)VerticalBus , cl);
|
||||||
cy = cpnts[i].y();
|
checkAndFill(cpnts[i].x() , cpnts[i].y() - 1, (short)HorizontalBus, cl);
|
||||||
checkAndFill(cx, cy, cl);
|
checkAndFill(cpnts[i].x() , cpnts[i].y() + 1, (short)HorizontalBus, cl);
|
||||||
cx = cpnts[i].x() + 1;
|
|
||||||
checkAndFill(cx, cy, cl);
|
|
||||||
cx = cpnts[i].x();
|
|
||||||
cy = cpnts[i].y() - 1;
|
|
||||||
checkAndFill(cx, cy, cl);
|
|
||||||
cy = cpnts[i].y() + 1;
|
|
||||||
checkAndFill(cx, cy, cl);
|
|
||||||
}
|
}
|
||||||
cpnts = npnts;
|
cpnts = npnts;
|
||||||
//qDebug() << cl << ": " << cpnts.size();
|
//qDebug() << cl << ": " << cpnts.size();
|
||||||
@@ -106,27 +100,21 @@ void BlockViewWavetrace::gatherPath() {
|
|||||||
}
|
}
|
||||||
int pa = -1, ca = -1;
|
int pa = -1, ca = -1;
|
||||||
bool first = true;
|
bool first = true;
|
||||||
short cl = field[fn.x()][fn.y()];
|
short cl = field[fn.x()][fn.y()].value;
|
||||||
QRect frect(0, 0, wid, hei);
|
QRect frect(0, 0, wid, hei);
|
||||||
QPoint cpnt = fn;
|
QPoint cpnt = fn;
|
||||||
//cout << "start from " << cl << endl;
|
//cout << "start from " << cl << endl;
|
||||||
auto checkAndStep = [this, &cpnt, &first, &frect] (int & dir, short c, int & ca_, int pa_)->bool {
|
auto checkAndStep = [this, &cpnt, &first, &frect] (int dir, short c, int & ca_, int pa_)->bool {
|
||||||
int cx = cpnt.x() + dps[dir].x();
|
int cx = cpnt.x() + dps[dir].x();
|
||||||
int cy = cpnt.y() + dps[dir].y();
|
int cy = cpnt.y() + dps[dir].y();
|
||||||
dir++;
|
|
||||||
if (frect.contains(cx, cy)) {
|
if (frect.contains(cx, cy)) {
|
||||||
if (field[cx][cy] == c) {
|
const Cell & cell(field[cx][cy]);
|
||||||
if (jumps.contains(QPoint(cx, cy))) {
|
if (cell.value == c) {
|
||||||
int jx = cx - 1;
|
if (cell.direction == HorizontalBus || cell.direction == VerticalBus) {
|
||||||
int jy = cy;
|
if (dps[dir].x() == 0 && cell.direction == VerticalBus)
|
||||||
if (jumps.contains(QPoint(jx, jy))) fill(jx, jy, (short)Jump);
|
return false;
|
||||||
jx = cx + 1;
|
if (dps[dir].y() == 0 && cell.direction == HorizontalBus)
|
||||||
if (jumps.contains(QPoint(jx, jy))) fill(jx, jy, (short)Jump);
|
return false;
|
||||||
jx = cx;
|
|
||||||
jy = cy - 1;
|
|
||||||
if (jumps.contains(QPoint(jx, jy))) fill(jx, jy, (short)Jump);
|
|
||||||
jy = cy + 1;
|
|
||||||
if (jumps.contains(QPoint(jx, jy))) fill(jx, jy, (short)Jump);
|
|
||||||
}
|
}
|
||||||
ca_ = QLineF(QPointF(cx, cy), cpnt).angle();
|
ca_ = QLineF(QPointF(cx, cy), cpnt).angle();
|
||||||
if (ca_ != pa_ && !first)
|
if (ca_ != pa_ && !first)
|
||||||
@@ -141,11 +129,12 @@ void BlockViewWavetrace::gatherPath() {
|
|||||||
while (cl > 0) {
|
while (cl > 0) {
|
||||||
cl--;
|
cl--;
|
||||||
pa = ca;
|
pa = ca;
|
||||||
int dir = 0;
|
|
||||||
if (checkAndStep(dir, cl, ca, pa)) continue;
|
if (checkAndStep(0, cl, ca, pa)) continue;
|
||||||
if (checkAndStep(dir, cl, ca, pa)) continue;
|
if (checkAndStep(1, cl, ca, pa)) continue;
|
||||||
if (checkAndStep(dir, cl, ca, pa)) continue;
|
if (checkAndStep(2, cl, ca, pa)) continue;
|
||||||
if (checkAndStep(dir, cl, ca, pa)) continue;
|
if (checkAndStep(3, cl, ca, pa)) continue;
|
||||||
|
|
||||||
}
|
}
|
||||||
path_.push_front(st);
|
path_.push_front(st);
|
||||||
//cout << path_.size() << endl;
|
//cout << path_.size() << endl;
|
||||||
@@ -154,13 +143,13 @@ void BlockViewWavetrace::gatherPath() {
|
|||||||
|
|
||||||
void BlockViewWavetrace::setPreferredDirection(BlockViewWavetrace::Direction dir) {
|
void BlockViewWavetrace::setPreferredDirection(BlockViewWavetrace::Direction dir) {
|
||||||
dir_ = dir;
|
dir_ = dir;
|
||||||
if (dir == Horizontal) {
|
if (dir == BlockViewWavetrace::Horizontal) {
|
||||||
dps[0] = QPoint(0, -1);
|
dps[0] = QPoint(0, -1);
|
||||||
dps[1] = QPoint(0, 1);
|
dps[1] = QPoint(0, 1);
|
||||||
dps[2] = QPoint(-1, 0);
|
dps[2] = QPoint(-1, 0);
|
||||||
dps[3] = QPoint(1, 0);
|
dps[3] = QPoint(1, 0);
|
||||||
}
|
}
|
||||||
if (dir == Vertical) {
|
if (dir == BlockViewWavetrace::Vertical) {
|
||||||
dps[2] = QPoint(0, -1);
|
dps[2] = QPoint(0, -1);
|
||||||
dps[3] = QPoint(0, 1);
|
dps[3] = QPoint(0, 1);
|
||||||
dps[0] = QPoint(-1, 0);
|
dps[0] = QPoint(-1, 0);
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class QAD_EXPORT BlockViewWavetrace {
|
|||||||
public:
|
public:
|
||||||
BlockViewWavetrace(int width = 1, int height = 1);
|
BlockViewWavetrace(int width = 1, int height = 1);
|
||||||
|
|
||||||
enum CellState {Empty = -1, Blocked = -2, Jump = -3};
|
enum CellState {Empty = -1, Blocked = -2, HorizontalBus = -3, VerticalBus = -4};
|
||||||
enum Direction {NoTrace, Horizontal, Vertical};
|
enum Direction {NoTrace, Horizontal, Vertical};
|
||||||
|
|
||||||
int width() const {return wid;}
|
int width() const {return wid;}
|
||||||
@@ -36,7 +36,7 @@ public:
|
|||||||
void resize(const QSize & sz) {resize(sz.width(), sz.height());}
|
void resize(const QSize & sz) {resize(sz.width(), sz.height());}
|
||||||
void fill(short val = -1);
|
void fill(short val = -1);
|
||||||
void fill(const QRect & rect, short val = -1);
|
void fill(const QRect & rect, short val = -1);
|
||||||
void fill(const QPoint & point, short val = -1) {field[point.x()][point.y()] = val;}
|
void fill(const QPoint & point, short val = -1) {fill(point.x(), point.y(), val);}
|
||||||
void fill(int px, int py, short val);
|
void fill(int px, int py, short val);
|
||||||
void fill(BlockViewWavetrace::CellState val = Empty) {fill((short)val);}
|
void fill(BlockViewWavetrace::CellState val = Empty) {fill((short)val);}
|
||||||
void fill(const QRect & rect, BlockViewWavetrace::CellState val = Empty) {fill(rect, (short)val);}
|
void fill(const QRect & rect, BlockViewWavetrace::CellState val = Empty) {fill(rect, (short)val);}
|
||||||
@@ -52,9 +52,15 @@ public:
|
|||||||
const QVector<QPoint> & path() const;
|
const QVector<QPoint> & path() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct Cell {
|
||||||
|
Cell(short v = Empty): value(v), direction(0) {}
|
||||||
|
short value;
|
||||||
|
short direction;
|
||||||
|
};
|
||||||
|
|
||||||
int wid, hei, max_steps;
|
int wid, hei, max_steps;
|
||||||
Direction dir_;
|
Direction dir_;
|
||||||
QVector<QVector<short> > field;
|
QVector<QVector<Cell> > field;
|
||||||
QVector<QPoint> path_;
|
QVector<QPoint> path_;
|
||||||
QVector<QPoint> jumps;
|
QVector<QPoint> jumps;
|
||||||
QPoint dps[4], st, fn;
|
QPoint dps[4], st, fn;
|
||||||
|
|||||||
Reference in New Issue
Block a user