marching squares 1,1 stairstep draft. buggy

master
dmatetelki 9 years ago
parent fe0d2faafd
commit 3348cebc9e

@ -37,18 +37,22 @@ void Edge::adjust()
if (!source || !dest) if (!source || !dest)
return; return;
QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0)); // QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
qreal length = line.length(); // qreal length = line.length();
QLineF line(source->pos(), dest->pos());
prepareGeometryChange(); prepareGeometryChange();
if (length > qreal(20.)) { // if (length > qreal(20.)) {
QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length); // QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
sourcePoint = line.p1() + edgeOffset; // sourcePoint = line.p1() + edgeOffset;
destPoint = line.p2() - edgeOffset; // destPoint = line.p2() - edgeOffset;
} else { // } else {
sourcePoint = destPoint = line.p1(); // sourcePoint = destPoint = line.p1();
} // }
sourcePoint = line.p1();
destPoint = line.p2();
} }
QRectF Edge::boundingRect() const QRectF Edge::boundingRect() const
@ -74,12 +78,14 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
if (qFuzzyCompare(line.length(), qreal(0.))) if (qFuzzyCompare(line.length(), qreal(0.)))
return; return;
const qreal line_thickness = 0.1;
// Draw the line itself // Draw the line itself
if (m_isRoute) { if (m_isRoute) {
painter->setPen(QPen(Qt::red, 10, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->setPen(QPen(Qt::red, line_thickness*2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
setZValue(1); setZValue(1);
} else { } else {
painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->setPen(QPen(Qt::blue, line_thickness, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
setZValue(0); setZValue(0);
} }
@ -106,4 +112,4 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
const QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, cos(angle - Pi + Pi / 3) * arrowSize); const QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, cos(angle - Pi + Pi / 3) * arrowSize);
painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
} }
} }

@ -22,13 +22,14 @@ struct float2 {
inline float2(float f) : x(f), y(f) {} inline float2(float f) : x(f), y(f) {}
constexpr inline float2(float x_, float y_) : x(x_), y(y_) {} constexpr inline float2(float x_, float y_) : x(x_), y(y_) {}
float2(const struct float3& v); float2(const struct float3& v);
float2(const struct float4& v); float2(const struct float4& v);
inline float2 operator +() const { return *this; } inline float2 operator +() const { return *this; }
inline float2 operator -() const { return float2(-x, -y); } inline float2 operator -() const { return float2(-x, -y); }
}; };
inline bool operator ==(const float2& v1, const float2& v2) { return v1.x == v2.x && v1.y == v2.y; } inline bool operator ==(const float2& v1, const float2& v2) { return v1.x == v2.x && v1.y == v2.y; }
inline bool operator !=(const float2& v1, const float2& v2) { return v1.x != v2.x || v1.y != v2.y; }
inline float2 operator +(const float2& v1, const float2& v2) { return float2(v1.x + v2.x, v1.y + v2.y); } inline float2 operator +(const float2& v1, const float2& v2) { return float2(v1.x + v2.x, v1.y + v2.y); }
inline float2 operator -(const float2& v1, const float2& v2) { return float2(v1.x - v2.x, v1.y - v2.y); } inline float2 operator -(const float2& v1, const float2& v2) { return float2(v1.x - v2.x, v1.y - v2.y); }

@ -76,10 +76,9 @@ int main(int argc, char **argv)
{ {
SafeQApplication app(argc, argv); SafeQApplication app(argc, argv);
const std::string xml_file = "graph_example.xml"; Graph<float2> g;
Graph<float2> g = readGraphFromXML<float2>(xml_file, float2creator);
const std::string png_file = "first_map.png"; const std::string png_file = "test.png";
GraphWidget *widget = new GraphWidget(&g, QString::fromStdString(png_file)); GraphWidget *widget = new GraphWidget(&g, QString::fromStdString(png_file));
widget->updateFromGraph(); widget->updateFromGraph();
@ -95,6 +94,5 @@ int main(int argc, char **argv)
mainWindow.show(); mainWindow.show();
const int app_retval = app.exec(); const int app_retval = app.exec();
writeGraphToXML<float2>(g, xml_file, float2serializer);
return app_retval; return app_retval;
} }

@ -40,6 +40,9 @@ namespace {
int2 (const int2& o) : x(o.x), y(o.y) {} int2 (const int2& o) : x(o.x), y(o.y) {}
void operator+=(const int2 o) { x += o.x; y += o.y; } void operator+=(const int2 o) { x += o.x; y += o.y; }
}; };
#define ASSERT(x) \
if (!(x)) std::cerr << __FILE__ << ":" << __LINE__ << ":" << __PRETTY_FUNCTION__ << (x) << std::endl;
} }
MarchingSquares::MarchingSquares() MarchingSquares::MarchingSquares()
@ -67,8 +70,8 @@ void MarchingSquares::ReadImage(const std::string& filename)
/// @todo extend image? /// @todo extend image?
// mark borders as SOLID, such that the entities in the world cannot fall off // mark borders as SOLID, such that the entities in the world cannot fall off
if (x == 0 || x == width_ - 1 || y == 0 || y == height_ - 1) // if (x == 0 || x == width_ - 1 || y == 0 || y == height_ - 1)
c = SOLID; // c = SOLID;
// mark white "hole" cells sorrounded by 4 black cells as black // mark white "hole" cells sorrounded by 4 black cells as black
if (y > 1 && y < height_-1 && x > 1 && x < width_-1 && if (y > 1 && y < height_-1 && x > 1 && x < width_-1 &&
@ -116,7 +119,7 @@ MarchingSquares::RunMarchingSquares() {
Color::Modifier blue(Color::FG_BLUE); Color::Modifier blue(Color::FG_BLUE);
Color::Modifier def(Color::FG_DEFAULT); Color::Modifier def(Color::FG_DEFAULT);
if (mask != 0) if (mask != 0)
std::cout << std::left << std::setw(3) << mask; std::cout << std::left << std::setw(3) << mask;
else else
std::cout << blue << mask << def << " "; std::cout << blue << mask << def << " ";
@ -126,17 +129,6 @@ MarchingSquares::RunMarchingSquares() {
if (mask == 0x0 || mask == 0xf) // empty or full if (mask == 0x0 || mask == 0xf) // empty or full
continue; continue;
// vertical/horizontal lines start with an edge
// assert(mask != 3);
// assert(mask != 12);
// assert(mask != 5);
// assert(mask != 10);
// cannot reach nonvisited bottomright corner
/// @note you can. the last is not visited
// assert(mask != 1);
// assert(mask != 14);
visitPoint(point.x, point.y, mask, visited, lines); visitPoint(point.x, point.y, mask, visited, lines);
} }
std::cout << std::endl; std::cout << std::endl;
@ -172,11 +164,39 @@ void MarchingSquares::visitPoint(int x, int y, int mask, std::vector< bool >& vi
if (!horizontal && !vertical) if (!horizontal && !vertical)
return; return;
// assert(horizontal || vertical);
// const bool start_2_lines = (mask == 0x7 || mask == 0x6 || mask == 0x8 || mask == 0x9); if (mask == 0x8 || mask == 0xb) {
int2 i(x, y);
while ((getMaskAt(i.x, i.y+1) == 0xe || getMaskAt(i.x, i.y+1) == 0x6) &&
getMaskAt(i.x-1, i.y+1) == 0x8 /*&&
visited[(i.y)*width_ + i.x] == false*/) {
visited[(i.y+1)*width_ + i.x] = true;
visited[(i.y+1)*width_ + i.x-1] = true;
i += int2(-1, 1);
}
if (i.x != x || i.y != y) {
visited[(i.y-1)*width_ + i.x+1] = false;
lines.push_back(std::pair<float2, float2>(float2(x, y), float2( i.x, i.y)));
}
}
if (mask == 1) {
int2 i(x, y);
while ((getMaskAt(i.x-1, i.y) == 0x6 || getMaskAt(i.x-1, i.y) == 0x7) &&
getMaskAt(i.x-1, i.y+1) == 0x1 /*&&
visited[(i.y)*width_ + i.x] == false*/) {
visited[i.y*width_ + i.x-1] = true;
visited[(i.y+1)*width_ + i.x-1] = true;
i += int2(-1, 1);
}
if (i.x != x || i.y != y) {
visited[(i.y-1)*width_ + i.x+1] = false;
lines.push_back(std::pair<float2, float2>(float2(x, y), float2( i.x, i.y)));
}
}
if (horizontal) { if (horizontal) {
// go left till possible
int2 i(x, y); int2 i(x, y);
while (true) { while (true) {
i += int2(1, 0); i += int2(1, 0);
@ -191,7 +211,10 @@ void MarchingSquares::visitPoint(int x, int y, int mask, std::vector< bool >& vi
else else
break; break;
} }
lines.push_back(std::pair<float2, float2>(float2(x, y), float2( i.x, i.y))); if (i.x != x) {
visited[i.y*width_ + i.x] = false;
lines.push_back(std::pair<float2, float2>(float2(x, y), float2( i.x, i.y)));
}
} }
if (vertical) { if (vertical) {
@ -209,6 +232,9 @@ void MarchingSquares::visitPoint(int x, int y, int mask, std::vector< bool >& vi
else else
break; break;
} }
lines.push_back(std::pair<float2, float2>(float2(x, y), float2( i.x, i.y))); if (i.y != y) {
visited[i.y*width_ + i.x] = false;
lines.push_back(std::pair<float2, float2>(float2(x, y), float2( i.x, i.y)));
}
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 478 B

Loading…
Cancel
Save