graphs/include/graph.h

232 lines
6.3 KiB
C
Raw Permalink Normal View History

2024-05-14 02:58:25 -04:00
#pragma once
/*
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef GRAPHS_GRAPH_H
#define GRAPHS_GRAPH_H
#include <config.h>
2024-05-14 02:58:25 -04:00
#include <graph_base.h>
2024-07-29 14:01:07 -04:00
#include <selection.h>
2024-05-14 02:58:25 -04:00
#include <force_algorithms.h>
#include <blt/gfx/window.h>
2024-05-14 21:58:05 -04:00
#include <blt/math/interpolation.h>
#include <blt/std/utility.h>
2024-05-14 02:58:25 -04:00
namespace im = ImGui;
struct bounding_box
{
int min_x = 0;
int min_y = 0;
int max_x = 0;
int max_y = 0;
bounding_box(const int min_x, const int min_y, const int max_x, const int max_y): min_x(min_x), min_y(min_y), max_x(max_x), max_y(max_y)
{
}
bool is_screen = true;
};
class graph_t
{
2024-07-27 20:53:47 -04:00
friend struct loader_t;
2024-07-30 03:16:58 -04:00
2024-07-29 14:01:07 -04:00
friend class selector_t;
2024-07-30 03:16:58 -04:00
2024-05-14 02:58:25 -04:00
private:
2024-07-28 22:07:19 -04:00
std::vector<node_t> nodes;
2024-07-27 20:53:47 -04:00
blt::hashmap_t<std::string, blt::u64> names_to_node;
2024-07-28 22:07:19 -04:00
blt::hashset_t<edge_t, edge_hash, edge_eq> edges;
2024-05-14 02:58:25 -04:00
blt::hashmap_t<blt::u64, blt::hashset_t<blt::u64>> connected_nodes;
bool sim = false;
bool run_infinitely = true;
float sim_speed = 1;
float threshold = 0;
float max_force_last = 1;
int current_iterations = 0;
int max_iterations = 5000;
std::unique_ptr<force_equation> equation;
2024-07-30 03:16:58 -04:00
2024-05-14 02:58:25 -04:00
void create_random_graph(bounding_box bb, blt::size_t min_nodes, blt::size_t max_nodes, blt::f64 connectivity,
blt::f64 scaling_connectivity, blt::f64 distance_factor);
public:
2024-07-27 20:53:47 -04:00
graph_t()
{
use_Eades();
}
2024-05-14 02:58:25 -04:00
void make_new(const bounding_box& bb, const blt::size_t min_nodes, const blt::size_t max_nodes, const blt::f64 connectivity)
{
create_random_graph(bb, min_nodes, max_nodes, connectivity, 0, 25);
}
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)
2024-07-28 14:01:19 -04:00
{
clear();
create_random_graph(bb, min_nodes, max_nodes, connectivity, scaling_connectivity, distance_factor);
}
void clear()
2024-05-14 02:58:25 -04:00
{
sim = false;
current_iterations = 0;
max_force_last = 1.0;
nodes.clear();
edges.clear();
connected_nodes.clear();
2024-07-28 22:07:19 -04:00
names_to_node.clear();
2024-05-14 02:58:25 -04:00
}
void connect(const blt::u64 n1, const blt::u64 n2)
{
connected_nodes[n1].insert(n2);
connected_nodes[n2].insert(n1);
2024-07-28 14:01:19 -04:00
edges.insert({n1, n2});
}
2024-07-30 03:16:58 -04:00
void disconnect(const blt::u64 n1, const blt::u64 n2)
{
connected_nodes[n1].erase(n2);
connected_nodes[n2].erase(n1);
edges.erase({n1, n2});
}
bool is_connected(const blt::u64 n1, const blt::u64 n2)
{
return edges.contains({n1, n2});
}
2024-07-28 22:07:19 -04:00
void connect(const edge_t& edge)
2024-07-28 14:01:19 -04:00
{
connected_nodes[edge.getFirst()].insert(edge.getSecond());
connected_nodes[edge.getSecond()].insert(edge.getFirst());
edges.insert(edge);
2024-05-14 02:58:25 -04:00
}
2024-07-28 22:07:19 -04:00
[[nodiscard]] std::optional<blt::ref<const edge_t>> connected(blt::u64 n1, blt::u64 n2) const
2024-05-14 02:58:25 -04:00
{
2024-07-28 22:07:19 -04:00
auto itr = edges.find(edge_t{n1, n2});
if (itr == edges.end())
return {};
return *itr;
2024-05-14 02:58:25 -04:00
}
2024-05-14 21:58:05 -04:00
void render();
2024-05-14 02:58:25 -04:00
void use_Eades()
{
equation = std::make_unique<Eades_equation>();
}
void use_Fruchterman_Reingold()
{
equation = std::make_unique<Fruchterman_Reingold_equation>();
}
void start_sim()
{
sim = true;
}
void stop_sim()
{
sim = false;
}
[[nodiscard]] std::string getSimulatorName() const
{
return equation->name();
}
[[nodiscard]] auto* getSimulator() const
{
return equation.get();
}
[[nodiscard]] auto getCoolingFactor() const
{
return equation->cooling_factor(current_iterations);
}
void reset_iterations()
{
current_iterations = 0;
}
[[nodiscard]] bool& getIterControl()
{
return run_infinitely;
}
[[nodiscard]] float& getSimSpeed()
{
return sim_speed;
}
[[nodiscard]] float& getThreshold()
{
return threshold;
}
[[nodiscard]] int& getMaxIterations()
{
return max_iterations;
}
[[nodiscard]] int numberOfNodes() const
{
return static_cast<int>(nodes.size());
}
};
class engine_t
{
2024-07-27 20:53:47 -04:00
friend struct loader_t;
2024-05-14 02:58:25 -04:00
private:
graph_t graph;
2024-07-30 03:16:58 -04:00
selector_t selector{graph};
2024-05-14 02:58:25 -04:00
2024-05-14 21:58:05 -04:00
void draw_gui(const blt::gfx::window_data& data);
2024-05-14 02:58:25 -04:00
public:
void init(const blt::gfx::window_data& data)
{
graph.make_new({0, 0, data.width, data.height}, 5, 25, 0.2);
}
2024-05-14 21:58:05 -04:00
void render(const blt::gfx::window_data& data)
2024-05-14 02:58:25 -04:00
{
2024-05-14 21:58:05 -04:00
draw_gui(data);
2024-05-14 02:58:25 -04:00
auto& io = ImGui::GetIO();
2024-05-15 02:26:57 -04:00
if (!io.WantCaptureMouse)
2024-07-29 22:05:18 -04:00
selector.process_mouse(data.width, data.height);
if (!io.WantCaptureKeyboard)
selector.process_keyboard(data.width, data.height);
2024-05-14 02:58:25 -04:00
2024-05-14 21:58:05 -04:00
graph.render();
2024-07-29 22:05:18 -04:00
selector.render(data.width, data.height);
2024-05-14 02:58:25 -04:00
}
};
#endif //GRAPHS_GRAPH_H