diff --git a/CMakeLists.txt b/CMakeLists.txt index 42e7b48..20fc58f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(graphs VERSION 0.1.2) +project(graphs VERSION 0.1.3) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/config.h b/include/config.h index 96646de..f9b56de 100644 --- a/include/config.h +++ b/include/config.h @@ -35,6 +35,7 @@ namespace conf inline constexpr auto POINT_HIGHLIGHT_COLOR = blt::make_color(0, 1.0, 1.0); inline constexpr auto POINT_SELECT_COLOR = blt::make_color(0, 0.9, 0.7); + inline constexpr float DEFAULT_THICKNESS = 2.0f; inline constexpr auto EDGE_COLOR = blt::make_color(0, 0.8, 0); inline constexpr auto EDGE_OUTLINE_COLOR = blt::make_color(0, 1, 0); } diff --git a/include/graph.h b/include/graph.h index 2ef4ce2..d9ce146 100644 --- a/include/graph.h +++ b/include/graph.h @@ -56,7 +56,7 @@ class graph_t private: std::vector nodes; blt::hashmap_t names_to_node; - blt::hashset_t edges; + blt::hashset_t edges; blt::hashmap_t> connected_nodes; bool sim = false; bool run_infinitely = true; @@ -87,6 +87,12 @@ class graph_t void reset(const bounding_box& bb, const blt::size_t min_nodes, const blt::size_t max_nodes, const blt::f64 connectivity, const blt::f64 scaling_connectivity, const blt::f64 distance_factor) + { + clear(); + create_random_graph(bb, min_nodes, max_nodes, connectivity, scaling_connectivity, distance_factor); + } + + void clear() { sim = false; current_iterations = 0; @@ -94,14 +100,20 @@ class graph_t nodes.clear(); edges.clear(); connected_nodes.clear(); - create_random_graph(bb, min_nodes, max_nodes, connectivity, scaling_connectivity, distance_factor); } void connect(const blt::u64 n1, const blt::u64 n2) { - edges.insert(edge{n1, n2}); connected_nodes[n1].insert(n2); connected_nodes[n2].insert(n1); + edges.insert({n1, n2}); + } + + void connect(const edge& edge) + { + connected_nodes[edge.getFirst()].insert(edge.getSecond()); + connected_nodes[edge.getSecond()].insert(edge.getFirst()); + edges.insert(edge); } [[nodiscard]] std::optional> connected(blt::u64 n1, blt::u64 n2) const diff --git a/include/graph_base.h b/include/graph_base.h index 18077de..aca9362 100644 --- a/include/graph_base.h +++ b/include/graph_base.h @@ -27,11 +27,12 @@ struct node { float repulsiveness = 24.0f; - std::string name; + std::string name = "unnamed"; + std::string texture = conf::DEFAULT_IMAGE; blt::gfx::point2d_t point; - float outline_scale = 1.25f; blt::vec2 velocity; + float outline_scale = 1.25f; blt::color4 outline_color = conf::POINT_OUTLINE_COLOR; explicit node(const blt::gfx::point2d_t& point): point(point) @@ -62,10 +63,8 @@ struct node struct edge { float ideal_spring_length = conf::DEFAULT_SPRING_LENGTH; - float outline_scale = 2.0f; - float thickness = 2.0f; + float thickness = conf::DEFAULT_THICKNESS; blt::color4 color = conf::EDGE_COLOR; - blt::color4 outline_color = conf::EDGE_OUTLINE_COLOR; edge(blt::u64 i1, blt::u64 i2): i1(i1), i2(i2) { @@ -100,4 +99,13 @@ struct edge_hash } }; +struct edge_eq +{ + bool operator()(const edge& e1, const edge& e2) const + { + return e1 == e2; + } +}; + + #endif //GRAPHS_GRAPH_BASE_H diff --git a/lib/BLT-With-Graphics-Template b/lib/BLT-With-Graphics-Template index bd9db11..ff2c8e3 160000 --- a/lib/BLT-With-Graphics-Template +++ b/lib/BLT-With-Graphics-Template @@ -1 +1 @@ -Subproject commit bd9db118274d5c0db22ec8e1237f3f3db5bbab8f +Subproject commit ff2c8e3be8aea2c3324cd9d7c2575085a7f1b56e diff --git a/src/graph.cpp b/src/graph.cpp index c5c5f9a..4c33f2b 100644 --- a/src/graph.cpp +++ b/src/graph.cpp @@ -61,9 +61,13 @@ void graph_t::render() } } - for (const auto& point : nodes) - renderer_2d.drawPointInternal(blt::gfx::render_info_t::make_info(conf::DEFAULT_IMAGE), - point.getRenderObj(), 15.0f); + for (const auto& [index, point] : blt::enumerate(nodes)) + { + auto& obj = point.getRenderObj(); + auto f_index = static_cast(index) + 1; + renderer_2d.drawPointInternal(blt::gfx::render_info_t::make_info(point.texture), obj, 15.0f * f_index); + renderer_2d.drawPointInternal(blt::gfx::render_info_t::make_info(point.outline_color), obj.apply_scale(point.outline_scale), 14.0f * f_index); + } for (const auto& edge : edges) { if (edge.getFirst() >= nodes.size() || edge.getSecond() >= nodes.size()) diff --git a/src/loader.cpp b/src/loader.cpp index 9d66b11..918427b 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -36,6 +36,7 @@ std::optional loader_t::load_for(engine_t& engine, const blt::gfx::win std::optional save_path) { auto& graph = engine.graph; + graph.clear(); static std::random_device dev; std::uniform_real_distribution pos_x_dist(0.0, static_cast(window_data.width)); @@ -64,25 +65,37 @@ std::optional loader_t::load_for(engine_t& engine, const blt::gfx::win if (data.contains("nodes")) { - graph.nodes.clear(); - for (const auto& node : data["nodes"]) { auto x = static_cast(load_with_default(node, "x", pos_x_dist(dev))); auto y = static_cast(load_with_default(node, "y", pos_y_dist(dev))); - auto size = static_cast(load_with_default(node, "size", static_cast(conf::POINT_SIZE))); + auto size = static_cast(load_with_default(node, "size", static_cast(conf::POINT_SIZE))); auto name = load_with_default(node, "name", "unnamed"); + auto texture = load_with_default(node, "texture", conf::DEFAULT_IMAGE); graph.names_to_node.insert({name, static_cast(graph.nodes.size())}); graph.nodes.emplace_back(blt::gfx::point2d_t{x, y, size}); graph.nodes.back().name = std::move(name); + graph.nodes.back().texture = std::move(texture); } } if (data.contains("connections")) { - graph.edges.clear(); - graph.connected_nodes.clear(); + for (const auto& edge : data["edges"]) + { + auto& nodes = edge["nodes"]; + auto index1 = nodes[0].get(); + auto index2 = nodes[1].get(); + auto ideal_length = load_with_default(edge, "length", conf::DEFAULT_SPRING_LENGTH); + auto thickness = load_with_default(edge, "thickness", conf::DEFAULT_THICKNESS); + + ::edge e{graph.names_to_node[index1], graph.names_to_node[index2]}; + e.ideal_spring_length = ideal_length; + e.thickness = thickness; + + graph.connect(e); + } } return loader;