diff --git a/lib/qtgraph/edge.cpp b/lib/qtgraph/edge.cpp index 399b769..0843730 100644 --- a/lib/qtgraph/edge.cpp +++ b/lib/qtgraph/edge.cpp @@ -37,18 +37,22 @@ void Edge::adjust() if (!source || !dest) return; - QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0)); - qreal length = line.length(); +// QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0)); +// qreal length = line.length(); + + QLineF line(source->pos(), dest->pos()); prepareGeometryChange(); - if (length > qreal(20.)) { - QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length); - sourcePoint = line.p1() + edgeOffset; - destPoint = line.p2() - edgeOffset; - } else { - sourcePoint = destPoint = line.p1(); - } +// if (length > qreal(20.)) { +// QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length); +// sourcePoint = line.p1() + edgeOffset; +// destPoint = line.p2() - edgeOffset; +// } else { +// sourcePoint = destPoint = line.p1(); +// } + sourcePoint = line.p1(); + destPoint = line.p2(); } QRectF Edge::boundingRect() const @@ -74,12 +78,14 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) if (qFuzzyCompare(line.length(), qreal(0.))) return; + const qreal line_thickness = 0.1; + // Draw the line itself 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); } 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); } @@ -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); painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); } -} \ No newline at end of file +} diff --git a/lib/qtgraph/floats.hpp b/lib/qtgraph/floats.hpp index b4b6257..adcc912 100644 --- a/lib/qtgraph/floats.hpp +++ b/lib/qtgraph/floats.hpp @@ -22,13 +22,14 @@ struct float2 { inline float2(float f) : x(f), y(f) {} constexpr inline float2(float x_, float y_) : x(x_), y(y_) {} 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 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 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); } diff --git a/lib/qtgraph/main.cpp b/lib/qtgraph/main.cpp index 68d021b..881523a 100644 --- a/lib/qtgraph/main.cpp +++ b/lib/qtgraph/main.cpp @@ -76,10 +76,9 @@ int main(int argc, char **argv) { SafeQApplication app(argc, argv); - const std::string xml_file = "graph_example.xml"; - Graph g = readGraphFromXML(xml_file, float2creator); + Graph g; - const std::string png_file = "first_map.png"; + const std::string png_file = "test.png"; GraphWidget *widget = new GraphWidget(&g, QString::fromStdString(png_file)); widget->updateFromGraph(); @@ -95,6 +94,5 @@ int main(int argc, char **argv) mainWindow.show(); const int app_retval = app.exec(); - writeGraphToXML(g, xml_file, float2serializer); return app_retval; } diff --git a/lib/qtgraph/marching_squares.cpp b/lib/qtgraph/marching_squares.cpp index a74132e..1430d31 100644 --- a/lib/qtgraph/marching_squares.cpp +++ b/lib/qtgraph/marching_squares.cpp @@ -40,6 +40,9 @@ namespace { int2 (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() @@ -67,8 +70,8 @@ void MarchingSquares::ReadImage(const std::string& filename) /// @todo extend image? // 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) - c = SOLID; +// if (x == 0 || x == width_ - 1 || y == 0 || y == height_ - 1) +// c = SOLID; // mark white "hole" cells sorrounded by 4 black cells as black 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 def(Color::FG_DEFAULT); if (mask != 0) - std::cout << std::left << std::setw(3) << mask; + std::cout << std::left << std::setw(3) << mask; else std::cout << blue << mask << def << " "; @@ -126,17 +129,6 @@ MarchingSquares::RunMarchingSquares() { if (mask == 0x0 || mask == 0xf) // empty or full 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); } std::cout << std::endl; @@ -172,11 +164,39 @@ void MarchingSquares::visitPoint(int x, int y, int mask, std::vector< bool >& vi if (!horizontal && !vertical) 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(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(x, y), float2( i.x, i.y))); + } + } if (horizontal) { + + // go left till possible int2 i(x, y); while (true) { i += int2(1, 0); @@ -191,7 +211,10 @@ void MarchingSquares::visitPoint(int x, int y, int mask, std::vector< bool >& vi else break; } - lines.push_back(std::pair(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(x, y), float2( i.x, i.y))); + } } if (vertical) { @@ -209,6 +232,9 @@ void MarchingSquares::visitPoint(int x, int y, int mask, std::vector< bool >& vi else break; } - lines.push_back(std::pair(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(x, y), float2( i.x, i.y))); + } } } diff --git a/lib/qtgraph/test.png b/lib/qtgraph/test.png new file mode 100644 index 0000000..3005a91 Binary files /dev/null and b/lib/qtgraph/test.png differ