From df9b9bc264e0bf12e99b447be7903050c822692d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 10 May 2025 13:50:08 +0100 Subject: Add new network building interface Imperfect, matches some legacy interface in places, has some TODO notes. --- test/test-network.cpp | 238 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 216 insertions(+), 22 deletions(-) (limited to 'test/test-network.cpp') diff --git a/test/test-network.cpp b/test/test-network.cpp index 1c91981..2b4b958 100644 --- a/test/test-network.cpp +++ b/test/test-network.cpp @@ -23,10 +23,13 @@ BOOST_GLOBAL_FIXTURE(TestMainWindowAppBase); namespace { struct TestLinkS; + struct TestLinkC; struct TestLink : public virtual Link { using StraightLink = TestLinkS; - using CurveLink = TestLinkS; + using CurveLink = TestLinkC; + + struct Vertex { }; }; struct TestLinkS : public TestLink, public LinkStraight { @@ -35,15 +38,34 @@ namespace { { } - TestLinkS(NetworkLinkHolder &, Node::Ptr nodeA, Node::Ptr nodeB, RelativePosition2D difference) : - Link {{.node = std::move(nodeA), .dir = 0}, {.node = std::move(nodeB), .dir = pi}, glm::length(difference)} + TestLinkS(NetworkLinkHolder & network, Node::Ptr nodeA, Node::Ptr nodeB, + RelativePosition2D difference) : + TestLinkS {network, std::move(nodeA), std::move(nodeB), glm::length(difference), vector_yaw(difference)} { } - struct Vertex { }; + TestLinkS(NetworkLinkHolder &, Node::Ptr nodeA, Node::Ptr nodeB, float length, Angle dirForward) : + Link {{.node = std::move(nodeA), .dir = dirForward}, + {.node = std::move(nodeB), .dir = normalize(pi + dirForward)}, length} + { + } + }; - TestLinkS(NetworkLinkHolder &, Node::Ptr nodeA, Node::Ptr nodeB, float length) : - Link {{.node = std::move(nodeA), .dir = 0}, {.node = std::move(nodeB), .dir = pi}, length} + struct TestLinkC : public TestLink, public LinkCurve { + TestLinkC(NetworkLinkHolder & network, const Node::Ptr & nodeA, const Node::Ptr & nodeB, + GlobalPosition2D centre) : + TestLinkC {network, nodeA, nodeB, centre, + {vector_yaw(difference(nodeA->pos.xy(), centre)), vector_yaw(difference(nodeB->pos.xy(), centre))}} + { + } + + TestLinkC(NetworkLinkHolder &, const Node::Ptr & nodeA, const Node::Ptr & nodeB, + GlobalPosition2D centre, Arc arc) : + Link {{.node = nodeA, .dir = normalize(arc.first + half_pi)}, + {.node = nodeB, .dir = normalize(arc.second - half_pi)}, + // Wrong, but OK for testing, just 10% longer than a straight line + (glm::length(difference(nodeA->pos, nodeB->pos)) * 1.1F)}, + LinkCurve {centre || nodeA->pos.z, glm::length(difference(centre, nodeA->pos.xy())), arc} { } }; @@ -53,24 +75,11 @@ namespace { } template<> NetworkLinkHolder::NetworkLinkHolder() = default; +template<> NetworkLinkHolder::NetworkLinkHolder() = default; namespace { - struct TestNetwork : public NetworkOf { - TestNetwork() : NetworkOf {RESDIR "rails.jpg"} - { - // 0 1 2 - // p000 <-> p100 <-> p200 <-> p300 - // \ | / - // \ 5 / - // 3 | 4 - // \-> p110 <-/ - addLink(P000, P100, 1.F); - addLink(P100, P200, 1.F); - addLink(P200, P300, 1.F); - addLink(P000, P110, 2.F); - addLink(P200, P110, 2.F); - addLink(P100, P110, 1.F); - } + struct EmptyNetwork : public NetworkOf { + EmptyNetwork() : NetworkOf {RESDIR "rails.jpg"} { } void render(const SceneShader &, const Frustum &) const override @@ -90,6 +99,24 @@ namespace { } }; + struct TestNetwork : public EmptyNetwork { + TestNetwork() + { + // 0 1 2 + // p000 <-> p100 <-> p200 <-> p300 + // \ | / + // \ 5 / + // 3 | 4 + // \-> p110 <-/ + addLink(P000, P100, 1.F, 0); + addLink(P100, P200, 1.F, 0); + addLink(P200, P300, 1.F, 0); + addLink(P000, P110, 2.F, 0); + addLink(P200, P110, 2.F, 0); + addLink(P100, P110, 1.F, 0); + } + }; + constexpr auto VALID_NODES = std::array({ P000, P100, @@ -224,6 +251,173 @@ BOOST_AUTO_TEST_CASE(RouteToDownStream3to300) BOOST_AUTO_TEST_SUITE_END() +BOOST_FIXTURE_TEST_SUITE(en, EmptyNetwork) + +using GenCurveDefsData = std::tuple; + +BOOST_DATA_TEST_CASE(GenCurveDefs, + boost::unit_test::data::make({ + {{0, 0, 0}, {1000, 1000, 0}, 0, {{1000, 1000, 0}, {0, 0, 0}, {1000, 0}}}, + {{0, 0, 0}, {-1000, 1000, 0}, 0, {{0, 0, 0}, {-1000, 1000, 0}, {-1000, 0}}}, + {{0, 0, 0}, {0, 1000, 0}, half_pi, {{0, 0, 0}, {0, 1000, 0}, {0, 500}}}, + {{0, 0, 0}, {0, 1000, 0}, -half_pi, {{0, 1000, 0}, {0, 0, 0}, {0, 500}}}, + }), + start, end, startDir, exp) +{ + BOOST_CHECK_EQUAL(genCurveDef(start, end, startDir), exp); +} + +BOOST_AUTO_TEST_CASE(NetworkCreateStraight) +{ + const auto link = create(CreationDefinition { + .fromEnd = {.position = {0, 0, 0}, .direction = {}}, + .toEnd = {.position = {0, 1000, 0}, .direction = {}}, + }); + BOOST_REQUIRE_EQUAL(link.size(), 1); + BOOST_CHECK(links.empty()); + BOOST_CHECK(nodes.empty()); + + BOOST_CHECK_IF(straight, std::dynamic_pointer_cast(link.front())) { + BOOST_CHECK_CLOSE(straight->length, 1000, 1); + BOOST_CHECK_CLOSE_VECI(straight->ends.front().node->pos, GlobalPosition3D(0, 0, 0)); + BOOST_CHECK_CLOSE_VECI(straight->ends.back().node->pos, GlobalPosition3D(0, 1000, 0)); + } + + add(nullptr, link.front()); + BOOST_CHECK_EQUAL(links.size(), 1); + BOOST_CHECK_EQUAL(nodes.size(), 2); +} + +BOOST_AUTO_TEST_CASE(NetworkCreateExtendingCurve) +{ + const auto link = create(CreationDefinition { + .fromEnd = {.position = {0, 0, 0}, .direction = half_pi}, + .toEnd = {.position = {0, 1000, 0}, .direction = {}}, + }); + BOOST_REQUIRE_EQUAL(link.size(), 1); + BOOST_CHECK(links.empty()); + BOOST_CHECK(nodes.empty()); + + BOOST_CHECK_IF(curve, std::dynamic_pointer_cast(link.front())) { + BOOST_CHECK_CLOSE(curve->length, 1100, 1); + BOOST_CHECK_CLOSE(curve->radius, 500, 1); + BOOST_CHECK_CLOSE_VECI(curve->centreBase, GlobalPosition3D(0, 500, 0)); + BOOST_CHECK_CLOSE_VECI(curve->ends.front().node->pos, GlobalPosition3D(0, 0, 0)); + BOOST_CHECK_CLOSE(curve->ends.front().dir, -half_pi, 1); + BOOST_CHECK_CLOSE_VECI(curve->ends.back().node->pos, GlobalPosition3D(0, 1000, 0)); + BOOST_CHECK_CLOSE(curve->ends.back().dir, -half_pi, 1); + } + + add(nullptr, link.front()); + BOOST_CHECK_EQUAL(links.size(), 1); + BOOST_CHECK_EQUAL(nodes.size(), 2); +} + +BOOST_AUTO_TEST_CASE(NetworkCreateExtendeeCurve) +{ + const auto link = create(CreationDefinition { + .fromEnd = {.position = {0, 0, 0}, .direction = {}}, + .toEnd = {.position = {0, 1000, 0}, .direction = half_pi}, + }); + BOOST_REQUIRE_EQUAL(link.size(), 1); + BOOST_CHECK(links.empty()); + BOOST_CHECK(nodes.empty()); + + BOOST_CHECK_IF(curve, std::dynamic_pointer_cast(link.front())) { + BOOST_CHECK_CLOSE(curve->length, 1100, 1); + BOOST_CHECK_CLOSE(curve->radius, 500, 1); + BOOST_CHECK_CLOSE_VECI(curve->centreBase, GlobalPosition3D(0, 500, 0)); + BOOST_CHECK_CLOSE_VECI(curve->ends.front().node->pos, GlobalPosition3D(0, 0, 0)); + BOOST_CHECK_CLOSE(curve->ends.front().dir, -half_pi, 1); + BOOST_CHECK_CLOSE_VECI(curve->ends.back().node->pos, GlobalPosition3D(0, 1000, 0)); + BOOST_CHECK_CLOSE(curve->ends.back().dir, -half_pi, 1); + } + + add(nullptr, link.front()); + BOOST_CHECK_EQUAL(links.size(), 1); + BOOST_CHECK_EQUAL(nodes.size(), 2); +} + +BOOST_AUTO_TEST_CASE(NetworkCreateBiarcPair) +{ + const auto link = create(CreationDefinition { + .fromEnd = {.position = {0, 0, 0}, .direction = pi}, + .toEnd = {.position = {1000, 1000, 0}, .direction = 0}, + }); + BOOST_REQUIRE_EQUAL(link.size(), 2); + BOOST_CHECK(links.empty()); + BOOST_CHECK(nodes.empty()); + + BOOST_CHECK_IF(firstCurve, std::dynamic_pointer_cast(link.front())) { + BOOST_CHECK_CLOSE(firstCurve->length, 777, 1); + BOOST_CHECK_CLOSE(firstCurve->radius, 500, 1); + BOOST_CHECK_CLOSE_VECI(firstCurve->centreBase, GlobalPosition3D(500, 0, 0)); + BOOST_CHECK_CLOSE_VECI(firstCurve->ends.front().node->pos, GlobalPosition3D(0, 0, 0)); + BOOST_CHECK_CLOSE(firstCurve->ends.front().dir, 0, 1); + BOOST_CHECK_CLOSE_VECI(firstCurve->ends.back().node->pos, GlobalPosition3D(500, 500, 0)); + BOOST_CHECK_CLOSE(firstCurve->ends.back().dir, -half_pi, 1); + } + + BOOST_CHECK_IF(secondCurve, std::dynamic_pointer_cast(link.back())) { + BOOST_CHECK_CLOSE(secondCurve->length, 777, 1); + BOOST_CHECK_CLOSE(secondCurve->radius, 500, 1); + BOOST_CHECK_CLOSE_VECI(secondCurve->centreBase, GlobalPosition3D(500, 1000, 0)); + BOOST_CHECK_CLOSE_VECI(secondCurve->ends.front().node->pos, GlobalPosition3D(1000, 1000, 0)); + BOOST_CHECK_CLOSE(secondCurve->ends.front().dir, pi, 1); + BOOST_CHECK_CLOSE_VECI(secondCurve->ends.back().node->pos, GlobalPosition3D(500, 500, 0)); + BOOST_CHECK_CLOSE(secondCurve->ends.back().dir, half_pi, 1); + } + + add(nullptr, link.front()); + add(nullptr, link.back()); + BOOST_CHECK_EQUAL(links.size(), 2); + BOOST_CHECK_EQUAL(nodes.size(), 3); + BOOST_CHECK_EQUAL(link.front()->ends.back().nexts.front().first.lock(), link.back()); + BOOST_CHECK_EQUAL(link.back()->ends.back().nexts.front().first.lock(), link.front()); +} + +BOOST_AUTO_TEST_CASE(NetworkCreateBiarcPairEqTan) +{ + // This could be achieved with a single curve, but not there yet + const auto link = create(CreationDefinition { + .fromEnd = {.position = {0, 0, 0}, .direction = 0}, + .toEnd = {.position = {1000, 0, 0}, .direction = 0}, + }); + BOOST_REQUIRE_EQUAL(link.size(), 2); + BOOST_CHECK(links.empty()); + BOOST_CHECK(nodes.empty()); + + BOOST_CHECK_IF(firstCurve, std::dynamic_pointer_cast(link.front())) { + BOOST_CHECK_CLOSE_VECI(firstCurve->centreBase, GlobalPosition3D(500, 0, 0)); + } + + BOOST_CHECK_IF(secondCurve, std::dynamic_pointer_cast(link.back())) { + BOOST_CHECK_CLOSE_VECI(secondCurve->centreBase, GlobalPosition3D(500, 0, 0)); + } +} + +BOOST_AUTO_TEST_CASE(NetworkCreateBiarcPairEqTanPerp) +{ + // This creates an equal pair of semi-circle arcs + const auto link = create(CreationDefinition { + .fromEnd = {.position = {0, 0, 0}, .direction = 0}, + .toEnd = {.position = {1000, 0, 0}, .direction = pi}, + }); + BOOST_REQUIRE_EQUAL(link.size(), 2); + BOOST_CHECK(links.empty()); + BOOST_CHECK(nodes.empty()); + + BOOST_CHECK_IF(firstCurve, std::dynamic_pointer_cast(link.front())) { + BOOST_CHECK_CLOSE_VECI(firstCurve->centreBase, GlobalPosition3D(250, 0, 0)); + } + + BOOST_CHECK_IF(secondCurve, std::dynamic_pointer_cast(link.back())) { + BOOST_CHECK_CLOSE_VECI(secondCurve->centreBase, GlobalPosition3D(750, 0, 0)); + } +} + +BOOST_AUTO_TEST_SUITE_END() + BOOST_FIXTURE_TEST_CASE(TestRailNetwork, RailLinks) { // 0 1 2 -- cgit v1.2.3