main
Brett 2024-07-28 14:01:19 -04:00
parent 8588583acf
commit 0c67b35572
7 changed files with 56 additions and 18 deletions

View File

@ -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)

View File

@ -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);
}

View File

@ -56,7 +56,7 @@ class graph_t
private:
std::vector<node> nodes;
blt::hashmap_t<std::string, blt::u64> names_to_node;
blt::hashset_t<edge, edge_hash> edges;
blt::hashset_t<edge, edge_hash, edge_eq> edges;
blt::hashmap_t<blt::u64, blt::hashset_t<blt::u64>> 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<blt::ref<const edge>> connected(blt::u64 n1, blt::u64 n2) const

View File

@ -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

@ -1 +1 @@
Subproject commit bd9db118274d5c0db22ec8e1237f3f3db5bbab8f
Subproject commit ff2c8e3be8aea2c3324cd9d7c2575085a7f1b56e

View File

@ -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<blt::f32>(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())

View File

@ -36,6 +36,7 @@ std::optional<loader_t> loader_t::load_for(engine_t& engine, const blt::gfx::win
std::optional<std::string_view> save_path)
{
auto& graph = engine.graph;
graph.clear();
static std::random_device dev;
std::uniform_real_distribution pos_x_dist(0.0, static_cast<blt::f64>(window_data.width));
@ -64,25 +65,37 @@ std::optional<loader_t> 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<blt::f32>(load_with_default(node, "x", pos_x_dist(dev)));
auto y = static_cast<blt::f32>(load_with_default(node, "y", pos_y_dist(dev)));
auto size = static_cast<blt::f32>(load_with_default(node, "size", static_cast<f64>(conf::POINT_SIZE)));
auto size = static_cast<blt::f32>(load_with_default(node, "size", static_cast<blt::f64>(conf::POINT_SIZE)));
auto name = load_with_default<std::string>(node, "name", "unnamed");
auto texture = load_with_default<std::string>(node, "texture", conf::DEFAULT_IMAGE);
graph.names_to_node.insert({name, static_cast<blt::u64>(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<std::string>();
auto index2 = nodes[1].get<std::string>();
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;