Browse Source

feat: add line in output svg

legacy
Dnomd343 2 years ago
parent
commit
9d5e8427ea
  1. 2
      src/graph/case.cc
  2. 37
      src/graph/graph.cc
  3. 6
      src/graph/graph.h
  4. 15
      src/graph/svg/svg.cc
  5. 3
      src/graph/svg/svg.h

2
src/graph/case.cc

@ -3,7 +3,7 @@
void GraphCase::render(SvgGraph &svg, const CaseSkin &skin) const { void GraphCase::render(SvgGraph &svg, const CaseSkin &skin) const {
/// precompute size info /// 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 block_width_2 = block_width * 2 + block_gap;
uint32_t case_width = block_width * 4 + block_gap * 5; uint32_t case_width = block_width * 4 + block_gap * 5;
uint32_t case_height = block_width * 5 + block_gap * 6; uint32_t case_height = block_width * 5 + block_gap * 6;

37
src/graph/graph.cc

@ -79,6 +79,8 @@ void Graph::svg_demo(Analyse::track_data_t track_data) {
if (ret.second) { // insert success if (ret.second) { // insert success
graph_layer[i - 1].emplace_back(&ret.first->second); graph_layer[i - 1].emplace_back(&ret.first->second);
} else {
graph_data[src->code].next.emplace_back(graph_data[c->code].layer_index);
} }
} }
} }
@ -95,23 +97,50 @@ 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_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); 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); 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 < 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) { 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); 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; std::cout << svg.dump() << std::endl;

6
src/graph/graph.h

@ -25,12 +25,12 @@ public:
class GraphCase { class GraphCase {
public: public:
Point start; Point start;
RawCode code; uint64_t code;
uint32_t block_gap; uint32_t block_gap;
uint32_t block_width; uint32_t block_width;
void render(SvgGraph &svg, const CaseSkin &skin) const; 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_gap = gap;
block_width = width; block_width = width;
} }
@ -49,6 +49,8 @@ public:
uint64_t CASE_WIDTH = BLOCK_LENGTH * 4 + BLOCK_GAP * 5; uint64_t CASE_WIDTH = BLOCK_LENGTH * 4 + BLOCK_GAP * 5;
uint64_t CASE_HEIGHT = BLOCK_LENGTH * 5 + BLOCK_GAP * 6; uint64_t CASE_HEIGHT = BLOCK_LENGTH * 5 + BLOCK_GAP * 6;
uint64_t LINE_OFFSET = 3;
void svg_demo(Analyse::track_data_t track_data); void svg_demo(Analyse::track_data_t track_data);
}; };

15
src/graph/svg/svg.cc

@ -14,7 +14,20 @@ std::string SvgLine::dump() const {
/// basic attributes of svg-line /// basic attributes of svg-line
return "<line />"; std::string xml = "<line ";
xml += "x1=\"" + std::to_string(start.x) + "\" ";
xml += "y1=\"" + std::to_string(start.y) + "\" ";
xml += "x2=\"" + std::to_string(end.x) + "\" ";
xml += "y2=\"" + std::to_string(end.y) + "\" ";
std::string style = "stroke:blue;stroke-width:1;";
// <line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2"/>
// TODO: svg-line css style
return xml + "style=\"" + style + "\" />";
} }

3
src/graph/svg/svg.h

@ -28,8 +28,7 @@ public:
~SvgLine() override = default; ~SvgLine() override = default;
std::string dump() const override; std::string dump() const override;
SvgLine(Point s, Point e) : start(s), end(e) {}
// TODO: SvgLine(...)
}; };
/// SVG rectangle element /// SVG rectangle element

Loading…
Cancel
Save