This commit is contained in:
Tobias P.L Wennberg 2025-08-26 13:22:41 +02:00
parent 0d4dd98501
commit 18d2bd6f0c
13 changed files with 224 additions and 76 deletions

View File

@ -14,10 +14,11 @@ INCLUDE_SOURCES = \
include/imgui/backends/imgui_impl_opengl3.cpp \
include/imnodes/imnodes.cpp \
include/implot/implot.cpp \
include/implot/implot_items.cpp
include/implot/implot_items.cpp \
src/world/world.cpp
INCLUDES = -Iinclude -Iinclude/imgui -Iinclude/imnodes -Iinclude/implot -Isrc/world
INCLUDES = -Iinclude -Iinclude/imgui -Iinclude/imnodes -Iinclude/implot
# SDL2 and OpenGL flags
UNAME_S := $(shell uname -s)

BIN
app

Binary file not shown.

56
src/cables/TP4.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "TP4.h"
#include <imnodes.h>
#include <imgui.h>
#include "../world/world.h"
#include <array>
#include <print>
#include "conductor.h"
Cables::TP4::TP4(int t_id) : id(t_id), conductor(std::array<Conductor, 4>({2,2,2,2})){
}
void Cables::TP4::init_ports() {
add_port();
add_port();
}
Cables::TP4::~TP4() {
}
void Cables::TP4::draw_node() {
ImNodes::BeginNode(id);
ImNodes::BeginNodeTitleBar();
ImGui::Text("TP4");
ImNodes::EndNodeTitleBar();
assert(ports.size() == 2);
for(auto& p: ports) {
ImNodes::BeginInputAttribute(p);
ImGui::Text("RJ45");
ImNodes::EndInputAttribute();
}
ImNodes::BeginOutputAttribute(0);
ImGui::Dummy(ImVec2(00.0f, 00.0f));
ImNodes::EndOutputAttribute();
ImNodes::EndNode();
}
void Cables::TP4::add_port() {
auto shared = shared_from_this();
auto shared_cable = static_cast<std::shared_ptr<Cable>>(shared);
int id = World::add_port(shared_from_this()->weak_from_this());
ports.emplace_back(id);
assert(ports.size() <= 2);
}
void Cables::TP4::step() {
for (auto c: conductor)
c.step();
}
void Cables::TP4::inspect() {
std::println("Inspect TP4");
}

59
src/cables/TP4.h Normal file
View File

@ -0,0 +1,59 @@
#pragma once
/*
* Simulates a generic Twisted Pair Cable with four conductors
* Does NOT emulate crosstalk, propagation time or any such fancy
* stuff
*/
#include "conductor.h"
#include <array>
#include "../world/cable.h"
#include <memory>
namespace Cables {
class TP4
: public Cable, public std::enable_shared_from_this<TP4> {
public:
/*
* The four conductors. You may interface with it directly
*/
std::array<Conductor, 4> conductor;
/*
* Always store TP4 as a shared_ptr and run init_ports
* after it has been created.
* This is so that the ports can be stored in world
*/
TP4(int t_id);
void init_ports();
/*
* Step 1ms simulation
*/
void step() override;
/*
* Draw cable node and connection
* to port
*/
void draw_node() override;
/*
* What to draw in the inspect
* window
*/
void inspect() override;
/*
* Get a shared ptr of this object
*/
std::shared_ptr<TP4> shared_ptr();
~TP4() override;
private:
void add_port();
std::vector<int> ports;
int id;
};
}

23
src/cables/cables.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "cables.h"
#include "imgui.h"
#include "../world/world.h"
#include "testcable.h"
#include "TP4.h"
void Cables::draw_cable_buttons() {
if (ImGui::Button("TP4")) {
auto n = std::make_shared<Cables::TP4>(World::generate_id());
n->init_ports();
std::shared_ptr<Node> node = static_cast<std::shared_ptr<Cable>>(n);
World::add_node(node);
}
if (ImGui::Button("Test cable")) {
auto n = std::make_shared<Cables::TestCable>(World::generate_id());
std::shared_ptr<Node> node = static_cast<std::shared_ptr<Cable>>(n);
World::add_node(node);
}
}

7
src/cables/cables.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
namespace Cables {
void draw_cable_buttons();
}

View File

@ -1,19 +1,17 @@
#include "testcable.h"
#include <imgui.h>
#include <imnodes.h>
#include "world.h"
#include "../world/world.h"
TestCable::TestCable(int t_id) : id(t_id) {
Cables::TestCable::TestCable(int t_id) : id(t_id) {
}
TestCable::~TestCable() {
Cables::TestCable::~TestCable() {
}
void TestCable::draw_node() {
void Cables::TestCable::draw_node() {
ImNodes::BeginNode(id);
ImNodes::BeginNodeTitleBar();
@ -39,17 +37,17 @@ void TestCable::draw_node() {
ImNodes::EndNode();
}
void TestCable::add_port() {
void Cables::TestCable::add_port() {
auto shared = shared_from_this();
auto shared_cable = static_cast<std::shared_ptr<Cable>>(shared);
int id = World::add_port(shared_from_this()->weak_from_this());
ports.emplace_back(id);
}
void TestCable::inspect() {
void Cables::TestCable::inspect() {
ImGui::Text("Inspecting a cable");
}
void TestCable::step() {
void Cables::TestCable::step() {
}

View File

@ -2,39 +2,42 @@
#include <memory>
#include <vector>
#include "cable.h"
#include "../world/cable.h"
class TestCable : public Cable, public std::enable_shared_from_this<TestCable> {
public:
namespace Cables {
class TestCable
: public Cable, public std::enable_shared_from_this<TestCable> {
public:
TestCable(int t_id);
TestCable(int t_id);
/*
* Step 1ms simulation
*/
void step() override;
/*
* Step 1ms simulation
*/
void step() override;
/*
* Draw cable node and connection
* to port
*/
void draw_node() override;
/*
* Draw cable node and connection
* to port
*/
void draw_node() override;
/*
* What to draw in the inspect
* window
*/
void inspect() override;
/*
* What to draw in the inspect
* window
*/
void inspect() override;
/*
* Get a shared ptr of this object
*/
std::shared_ptr<TestCable> shared_ptr();
/*
* Get a shared ptr of this object
*/
std::shared_ptr<TestCable> shared_ptr();
~TestCable() override;
~TestCable() override;
private:
void add_port();
std::vector<int> ports;
int id;
};
private:
void add_port();
std::vector<int> ports;
int id;
};
}

View File

@ -106,7 +106,6 @@ int main() {
{
if (update_world(last_step_time)) {
last_step_time = std::clock();
std::println("STEP!");
World::step();
}

View File

@ -7,8 +7,8 @@
#include <limits>
#include "world.h"
#include <print>
#include "../OSI/physical/conductor.h"
#include "../cables/testcable.h"
#include "../cables/cables.h"
#define NO_ID std::numeric_limits<int>::min()
@ -48,13 +48,6 @@ namespace Lookup_table {
static float simulation_speed = 1.;
int get_id() {
static int id = 0;
//static int id = NO_ID;
id++;
std::println("id: {}", id);
return id;
}
void draw_nodes() {
for(auto& index: nodes_table) {
@ -83,35 +76,42 @@ namespace Lookup_table {
}
}
int World::generate_id() {
static int id = 0;
//static int id = NO_ID;
id++;
std::println("id: {}", id);
return id;
}
void add_computer() {
int id = Lookup_table::get_id();
int id = World::generate_id();
auto c = std::make_shared<Computer>(id);
std::shared_ptr<Node> node = std::static_pointer_cast<Node>(c);
Lookup_table::nodes_table.insert({id, node});
}
void add_cable() {
int id = Lookup_table::get_id();
auto c = std::make_shared<TestCable>(id);
std::shared_ptr<Node> node = static_cast<std::shared_ptr<Cable>>(c);
Lookup_table::nodes_table.insert({id, node});
}
int World::add_port(std::weak_ptr<Computer> n){
int id = Lookup_table::get_id();
int World::add_port(std::weak_ptr<Computer> n) {
int id = World::generate_id();
Lookup_table::nodes_table.insert({ id, n });
return id;
}
int World::add_port(std::weak_ptr<Cable> n){
int id = Lookup_table::get_id();
int World::add_port(std::weak_ptr<Cable> n) {
int id = World::generate_id();
Lookup_table::nodes_table.insert({ id, n });
return id;
}
int World::add_node(std::shared_ptr<Node> n) {
int id = World::generate_id();
Lookup_table::nodes_table.insert({id, n});
return id;
}
void World::add_link(int computer_port, int cable_port) {
if (auto val = std::find_if(
Lookup_table::links.begin(), Lookup_table::links.end(), [&](const auto& val){ return val.second == cable_port; }
Lookup_table::links.begin(), Lookup_table::links.end(), [&](const auto& val) { return val.second == cable_port; }
); val != Lookup_table::links.end()) {
Lookup_table::links.erase(val->first);
}
@ -171,27 +171,17 @@ void World::step() {
}
Conductor conduct = Conductor(3);
void World::draw_gui() {
ImGui::Begin("Add Nodes");
ImGui::Text("Add nodes:");
ImGui::Text("Add Computers:");
if(ImGui::Button("Add Computer"))
add_computer();
if(ImGui::Button("Add Cable"))
add_cable();
ImGui::Text("Add Cables:");
Cables::draw_cable_buttons();
ImGui::SliderFloat("Simulation Speed", &Lookup_table::simulation_speed, 0, 5);
ImGui::End();
ImGui::Begin("Test conductor");
conduct.step();
conduct.set_voltage(0, 5);
conduct.set_voltage(1, -2.5);
conduct.set_voltage(2, 1);
conduct.draw_graph_view();
ImGui::End();
draw_nodes();
draw_inspect_node();

View File

@ -30,6 +30,18 @@ namespace World {
int add_port(std::weak_ptr<Computer> n);
int add_port(std::weak_ptr<Cable> n);
/*
* Add a Node, such as computer or cable
* Returns the nodes id
*/
int add_node(std::shared_ptr<Node> n);
/*
* Generates a random id, starting at 1
* increasing.
*/
int generate_id();
/*
* Draw a link between two nodes
*/