From 873ef1436e233b5b4c542f35838a7cd73432ede6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 22 Oct 2022 19:06:14 +0100 Subject: Tidy network node insertion/searching --- game/network/network.cpp | 17 +++++++++-------- game/network/network.h | 9 +++++++-- game/network/rail.cpp | 10 ++++++---- test/test-network.cpp | 4 ++-- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/game/network/network.cpp b/game/network/network.cpp index d78f672..ad501b5 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -19,14 +19,15 @@ Network::nodeAt(glm::vec3 pos) return newNodeAt(pos).first; } -std::pair +Network::NodeInsertion Network::newNodeAt(glm::vec3 pos) { - const auto [n, i] = candidateNodeAt(pos); - if (!i) { - nodes.insert(n); + if (const auto [n, i] = candidateNodeAt(pos); i == NodeIs::NotInNetwork) { + return {*nodes.insert(std::move(n)).first, i}; + } + else { + return {n, NodeIs::InNetwork}; } - return {n, !i}; } Node::Ptr @@ -38,13 +39,13 @@ Network::findNodeAt(glm::vec3 pos) const return {}; } -std::pair +Network::NodeInsertion Network::candidateNodeAt(glm::vec3 pos) const { if (const auto n = nodes.find(pos); n != nodes.end()) { - return {*n, true}; + return {*n, NodeIs::InNetwork}; } - return {std::make_shared(pos), false}; + return {std::make_shared(pos), NodeIs::NotInNetwork}; } Node::Ptr diff --git a/game/network/network.h b/game/network/network.h index b95dfdc..25f0200 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -23,8 +23,10 @@ public: [[nodiscard]] Node::Ptr findNodeAt(glm::vec3) const; [[nodiscard]] Node::Ptr nodeAt(glm::vec3); - [[nodiscard]] std::pair newNodeAt(glm::vec3); - [[nodiscard]] std::pair candidateNodeAt(glm::vec3) const; + enum class NodeIs { InNetwork, NotInNetwork }; + using NodeInsertion = std::pair; + [[nodiscard]] NodeInsertion newNodeAt(glm::vec3); + [[nodiscard]] NodeInsertion candidateNodeAt(glm::vec3) const; [[nodiscard]] virtual Link::Ptr intersectRayLinks(const Ray &) const = 0; [[nodiscard]] virtual Node::Ptr intersectRayNodes(const Ray &) const; @@ -85,4 +87,7 @@ public: Link::CCollection addExtend(glm::vec3, glm::vec3) override; void render(const Shader &) const override; + +protected: + Link::CCollection addJoins(); }; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 635ee6e..95b6d24 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -30,11 +30,11 @@ std::shared_ptr RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) { auto node1ins = newNodeAt(start), node2ins = newNodeAt(end); - if (node1ins.second && node2ins.second) { + if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::NotInNetwork) { // Both nodes are new, direct link, easy return addLink(start, end); } - if (node1ins.second && !node2ins.second) { + if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::InNetwork) { // node1 is new, node2 exists, but we build from existing outwards std::swap(node1ins, node2ins); std::swap(start, end); @@ -56,7 +56,7 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) return addLink(start, end); } const glm::vec2 flatStart {!start}, flatEnd {!end}; - if (!node2ins.second) { + if (node2ins.second == NodeIs::InNetwork) { auto midheight = [&](auto mid) { const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); return start.z + ((end.z - start.z) * (sm / (sm + em))); @@ -128,7 +128,9 @@ round_sleepers(const float v) return round_frac(v, sleepers); } -RailLinkStraight::RailLinkStraight(const Node::Ptr & a, const Node::Ptr & b) : RailLinkStraight(a, b, b->pos - a->pos) { } +RailLinkStraight::RailLinkStraight(const Node::Ptr & a, const Node::Ptr & b) : RailLinkStraight(a, b, b->pos - a->pos) +{ +} RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const glm::vec3 & diff) : Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)) diff --git a/test/test-network.cpp b/test/test-network.cpp index bfcf64b..fb4c87c 100644 --- a/test/test-network.cpp +++ b/test/test-network.cpp @@ -81,7 +81,7 @@ BOOST_DATA_TEST_CASE(nodeAt, VALID_NODES + INVALID_NODES, p) BOOST_DATA_TEST_CASE(newNodeAt_existing, VALID_NODES, p) { auto n = newNodeAt(p); - BOOST_CHECK(!n.second); + BOOST_CHECK_EQUAL(n.second, Network::NodeIs::InNetwork); BOOST_REQUIRE(n.first); BOOST_CHECK_EQUAL(n.first->pos, p); } @@ -89,7 +89,7 @@ BOOST_DATA_TEST_CASE(newNodeAt_existing, VALID_NODES, p) BOOST_DATA_TEST_CASE(newNodeAt_new, INVALID_NODES, p) { auto n = newNodeAt(p); - BOOST_CHECK(n.second); + BOOST_CHECK_EQUAL(n.second, Network::NodeIs::NotInNetwork); BOOST_REQUIRE(n.first); BOOST_CHECK_EQUAL(n.first->pos, p); } -- cgit v1.2.3