From 9d5e8427ea6cd2efa20d01f6f770c6d9c6af1a06 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 21 Jan 2023 14:14:17 +0800 Subject: [PATCH] feat: add line in output svg --- src/graph/case.cc | 2 +- src/graph/graph.cc | 39 ++++++++++++++++++++++++++++++++++----- src/graph/graph.h | 6 ++++-- src/graph/svg/svg.cc | 15 ++++++++++++++- src/graph/svg/svg.h | 3 +-- 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/graph/case.cc b/src/graph/case.cc index 05f896a..2ee6bbc 100644 --- a/src/graph/case.cc +++ b/src/graph/case.cc @@ -3,7 +3,7 @@ void GraphCase::render(SvgGraph &svg, const CaseSkin &skin) const { /// precompute size info - auto raw_code = (uint64_t)code; + auto raw_code = code; uint32_t block_width_2 = block_width * 2 + block_gap; uint32_t case_width = block_width * 4 + block_gap * 5; uint32_t case_height = block_width * 5 + block_gap * 6; diff --git a/src/graph/graph.cc b/src/graph/graph.cc index 578c310..52cd3a5 100644 --- a/src/graph/graph.cc +++ b/src/graph/graph.cc @@ -79,6 +79,8 @@ void Graph::svg_demo(Analyse::track_data_t track_data) { if (ret.second) { // insert success graph_layer[i - 1].emplace_back(&ret.first->second); + } else { + graph_data[src->code].next.emplace_back(graph_data[c->code].layer_index); } } } @@ -95,22 +97,49 @@ void Graph::svg_demo(Analyse::track_data_t track_data) { uint64_t graph_width = CASE_WIDTH * max_width + CASE_GAP_X * (max_width + 1); uint64_t graph_height = CASE_HEIGHT * max_height + CASE_GAP_Y * (max_height + 1); + + auto case_address = [this, &graph_width, &graph_layer](uint32_t i, uint32_t j) -> Point { + uint64_t case_top = CASE_GAP_Y * (i + 1) + CASE_HEIGHT * i; + uint64_t offset = (graph_width - (CASE_GAP_X * (graph_layer[i].size() + 1) + CASE_WIDTH * graph_layer[i].size())) / 2; + uint64_t case_left = offset + CASE_GAP_X * (j + 1) + CASE_WIDTH * j; + + return Point {case_left, case_top}; + }; + auto svg = SvgGraph(graph_width, graph_height); +// std::cout << graph_layer[0][0]->next.size() << std::endl; + for (uint32_t i = 0; i < graph_layer.size(); ++i) { +// for (uint32_t i = 0; i < 1; ++i) { - uint64_t case_top = CASE_GAP_Y * (i + 1) + CASE_HEIGHT * i; +// uint64_t case_top = CASE_GAP_Y * (i + 1) + CASE_HEIGHT * i; - uint64_t offset = (graph_width - (CASE_GAP_X * (graph_layer[i].size() + 1) + CASE_WIDTH * graph_layer[i].size())) / 2; +// uint64_t offset = (graph_width - (CASE_GAP_X * (graph_layer[i].size() + 1) + CASE_WIDTH * graph_layer[i].size())) / 2; for (uint32_t j = 0; j < graph_layer[i].size(); ++j) { - uint64_t case_left = offset + CASE_GAP_X * (j + 1) + CASE_WIDTH * j; +// uint64_t case_left = offset + CASE_GAP_X * (j + 1) + CASE_WIDTH * j; - auto g = GraphCase({case_left, case_top}, RawCode::unsafe_create(graph_layer[i][j]->code), BLOCK_GAP, BLOCK_LENGTH); +// auto g = GraphCase({case_left, case_top}, graph_layer[i][j]->code, BLOCK_GAP, BLOCK_LENGTH); + auto g = GraphCase(case_address(i, j), graph_layer[i][j]->code, BLOCK_GAP, BLOCK_LENGTH); g.render(svg, skin); - } + for (const auto next : graph_layer[i][j]->next) { + + auto s = case_address(i, j); + auto e = case_address(i + 1, next); + + s.x += CASE_WIDTH / 2; + s.y += CASE_HEIGHT + LINE_OFFSET; + e.x += CASE_WIDTH / 2; + e.y -= LINE_OFFSET; + + auto line = new SvgLine(s, e); + svg.insert(line); + } + + } } std::cout << svg.dump() << std::endl; diff --git a/src/graph/graph.h b/src/graph/graph.h index 4f462e1..e3d8892 100644 --- a/src/graph/graph.h +++ b/src/graph/graph.h @@ -25,12 +25,12 @@ public: class GraphCase { public: Point start; - RawCode code; + uint64_t code; uint32_t block_gap; uint32_t block_width; void render(SvgGraph &svg, const CaseSkin &skin) const; - GraphCase(Point p, RawCode c, uint32_t gap, uint32_t width) : start(p), code(c) { + GraphCase(Point p, uint64_t c, uint32_t gap, uint32_t width) : start(p), code(c) { block_gap = gap; block_width = width; } @@ -49,6 +49,8 @@ public: uint64_t CASE_WIDTH = BLOCK_LENGTH * 4 + BLOCK_GAP * 5; uint64_t CASE_HEIGHT = BLOCK_LENGTH * 5 + BLOCK_GAP * 6; + uint64_t LINE_OFFSET = 3; + void svg_demo(Analyse::track_data_t track_data); }; diff --git a/src/graph/svg/svg.cc b/src/graph/svg/svg.cc index aa0a323..06d3816 100644 --- a/src/graph/svg/svg.cc +++ b/src/graph/svg/svg.cc @@ -14,7 +14,20 @@ std::string SvgLine::dump() const { /// basic attributes of svg-line - return ""; + std::string xml = " + + + // TODO: svg-line css style + + return xml + "style=\"" + style + "\" />"; } diff --git a/src/graph/svg/svg.h b/src/graph/svg/svg.h index dc7c22e..09ab76c 100644 --- a/src/graph/svg/svg.h +++ b/src/graph/svg/svg.h @@ -28,8 +28,7 @@ public: ~SvgLine() override = default; std::string dump() const override; - - // TODO: SvgLine(...) + SvgLine(Point s, Point e) : start(s), end(e) {} }; /// SVG rectangle element