Browse Source

feat: new SvgRect module

master
Dnomd343 2 years ago
parent
commit
7152974e69
  1. 6
      src/graph/CMakeLists.txt
  2. 186
      src/graph/graph.cc
  3. 54
      src/graph/graph.h
  4. 3
      src/graph/svg/CMakeLists.txt
  5. 50
      src/graph/svg/svg.cc
  6. 21
      src/graph/svg/svg.h

6
src/graph/CMakeLists.txt

@ -1,4 +1,8 @@
cmake_minimum_required(VERSION 3.0)
include_directories(svg)
add_subdirectory(svg)
add_library(graph graph.cc)
target_link_libraries(graph analyse)
target_link_libraries(graph analyse svg)

186
src/graph/graph.cc

@ -4,8 +4,32 @@
#include <list>
#include <iostream>
#include "svg.h"
void Graph::svg_demo(Analyse::track_data_t track_data) {
auto s = SvgRect();
s.left = 50;
s.top = 80;
s.width = 100;
s.height = 200;
s.radius = 20;
printf(R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="%d" height="%d">)", 1000, 1000);
printf("\n");
std::cout << " " << s.dump() << std::endl;
printf("</svg>\n");
return;
//
// for (uint32_t i = 0; i < track_data.size(); ++i) {
//
@ -93,13 +117,13 @@ void Graph::svg_demo(Analyse::track_data_t track_data) {
// printf(R"( <rect x="%ld" y="%ld" width="%ld" height="%ld" style="fill:pink;stroke-width:1;stroke:blue"/>)", CASE_X, CASE_Y, CASE_WIDTH, CASE_HEIGHT);
// printf("\n");
SvgCase tmp = SvgCase();
tmp.width = BLOCK_LENGTH;
tmp.gap = BLOCK_GAP;
tmp.left = CASE_X;
tmp.top = CASE_Y;
tmp.code = layer_data[i][j].code;
tmp.render();
// SvgCase tmp = SvgCase();
// tmp.width = BLOCK_LENGTH;
// tmp.gap = BLOCK_GAP;
// tmp.left = CASE_X;
// tmp.top = CASE_Y;
// tmp.code = layer_data[i][j].code;
// tmp.render();
}
@ -110,78 +134,78 @@ void Graph::svg_demo(Analyse::track_data_t track_data) {
}
void SvgRect::to_xml() const {
// TODO: return std::string
printf(R"(<rect x="%ld" y="%ld" width="%ld" height="%ld" style="fill:pink;stroke-width:1;stroke:blue"/>)", left, top, width, height);
printf("\n");
}
void SvgCase::render() const {
std::string result;
uint64_t raw_code = code;
// std::cout << "start rander" << std::endl;
auto tmp = SvgRect();
tmp.left = left;
tmp.top = top;
tmp.width = width * 4 + gap * 5;
tmp.height = width * 5 + gap * 6;
// std::cout << "main" << std::endl;
tmp.to_xml();
for (int addr = 0; raw_code; ++addr, raw_code >>= 3) {
uint32_t block_x = addr % 4;
uint32_t block_y = (addr - block_x) / 4;
switch (raw_code & 0b111) {
case B_1x1:
tmp.top = width * block_y + gap * (block_y + 1) + top;
tmp.left = width * block_x + gap * (block_x + 1) + left;
tmp.height = width;
tmp.width = width;
// std::cout << "1x1" << std::endl;
tmp.to_xml();
break;
case B_1x2:
tmp.top = width * block_y + gap * (block_y + 1) + top;
tmp.left = width * block_x + gap * (block_x + 1) + left;
tmp.height = width;
tmp.width = width * 2 + gap;
// std::cout << "1x2" << std::endl;
tmp.to_xml();
//void SvgRect::to_xml() const {
// // TODO: return std::string
// printf(R"(<rect x="%ld" y="%ld" width="%ld" height="%ld" style="fill:pink;stroke-width:1;stroke:blue"/>)", left, top, width, height);
// printf("\n");
//}
break;
case B_2x1:
tmp.top = width * block_y + gap * (block_y + 1) + top;
tmp.left = width * block_x + gap * (block_x + 1) + left;
tmp.height = width * 2 + gap;
tmp.width = width;
// std::cout << "2x1" << std::endl;
tmp.to_xml();
break;
case B_2x2:
tmp.top = width * block_y + gap * (block_y + 1) + top;
tmp.left = width * block_x + gap * (block_x + 1) + left;
tmp.height = width * 2 + gap;
tmp.width = width * 2 + gap;
// std::cout << "2x2" << std::endl;
tmp.to_xml();
break;
default:
continue;
}
}
}
//void SvgCase::render() const {
//
// std::string result;
//
// uint64_t raw_code = code;
//
//// std::cout << "start rander" << std::endl;
//
// auto tmp = SvgRect();
//
// tmp.left = left;
// tmp.top = top;
// tmp.width = width * 4 + gap * 5;
// tmp.height = width * 5 + gap * 6;
//// std::cout << "main" << std::endl;
// tmp.to_xml();
//
// for (int addr = 0; raw_code; ++addr, raw_code >>= 3) {
//
// uint32_t block_x = addr % 4;
// uint32_t block_y = (addr - block_x) / 4;
//
// switch (raw_code & 0b111) {
// case B_1x1:
//
// tmp.top = width * block_y + gap * (block_y + 1) + top;
// tmp.left = width * block_x + gap * (block_x + 1) + left;
// tmp.height = width;
// tmp.width = width;
//// std::cout << "1x1" << std::endl;
// tmp.to_xml();
//
// break;
// case B_1x2:
//
// tmp.top = width * block_y + gap * (block_y + 1) + top;
// tmp.left = width * block_x + gap * (block_x + 1) + left;
// tmp.height = width;
// tmp.width = width * 2 + gap;
//// std::cout << "1x2" << std::endl;
// tmp.to_xml();
//
// break;
// case B_2x1:
//
// tmp.top = width * block_y + gap * (block_y + 1) + top;
// tmp.left = width * block_x + gap * (block_x + 1) + left;
// tmp.height = width * 2 + gap;
// tmp.width = width;
//// std::cout << "2x1" << std::endl;
// tmp.to_xml();
//
// break;
// case B_2x2:
//
// tmp.top = width * block_y + gap * (block_y + 1) + top;
// tmp.left = width * block_x + gap * (block_x + 1) + left;
// tmp.height = width * 2 + gap;
// tmp.width = width * 2 + gap;
//// std::cout << "2x2" << std::endl;
// tmp.to_xml();
//
// break;
// default:
// continue;
// }
// }
//
//}

54
src/graph/graph.h

@ -2,33 +2,33 @@
#include "analyse.h"
class SvgRect {
public:
uint64_t top;
uint64_t left;
uint64_t width;
uint64_t height;
// TODO: color options
void to_xml() const;
};
class SvgCase {
public:
uint64_t code;
uint64_t top = 0;
uint64_t left = 0;
uint32_t gap = 4;
uint32_t width = 12;
void render() const;
};
//class SvgRect {
//public:
// uint64_t top;
// uint64_t left;
// uint64_t width;
// uint64_t height;
//
// // TODO: color options
//
// void to_xml() const;
//
//};
//class SvgCase {
//public:
//
// uint64_t code;
//
// uint64_t top = 0;
// uint64_t left = 0;
//
// uint32_t gap = 4;
// uint32_t width = 12;
//
// void render() const;
//
//};
class Graph {

3
src/graph/svg/CMakeLists.txt

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.0)
add_library(svg svg.cc)

50
src/graph/svg/svg.cc

@ -0,0 +1,50 @@
#include "svg.h"
#include <cstdio>
#include <iostream>
std::string SvgRect::dump() const {
/// <rect x="120" width="100" height="100" rx="15" />
// char *radius_str = nullptr;
// if (radius == 0) {
// asprintf(&radius_str, " ");
// } else {
// asprintf(&radius_str, R"( rx="%lu" )", radius);
// }
std::string style;
// style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9"
char *xml = nullptr;
asprintf(&xml, R"(<rect x="%ld" y="%ld" width="%ld" height="%ld")", left, top, width, height);
std::string ret(xml);
free(xml);
if (radius != 0) {
char *radius_str = nullptr;
asprintf(&radius_str, R"( rx="%lu")", radius);
ret += radius_str;
free(radius_str);
}
// std::cout << xml << std::endl;
// std::string result = xml;
// free(xml);
// std::string xml("<rect ");
// xml += "x = ";
//
// xml += atoi();
return ret + " />";
}

21
src/graph/svg/svg.h

@ -0,0 +1,21 @@
#pragma once
#include <string>
#include <cstdint>
class SvgRect {
public:
uint64_t top;
uint64_t left;
uint64_t width;
uint64_t height;
uint64_t radius = 0;
uint32_t stroke = 1;
float opacity = 0;
float line_opacity = 0;
std::string color = "pink";
std::string line_color = "blue";
std::string dump() const;
};
Loading…
Cancel
Save