18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 22:55:16 +01:00
Files
maszyna/tests/unit/test_ring_room.cpp
jerrrrycho 0c01d660f6 fix(editor): roads joined to a roundabout take only the room they need (#485)
* fix(editor): a road joined to a roundabout takes only the room it needs

A road led into the side of a roundabout gets a junction that cuts the
ring on both sides of it. The cut was measured as on a straight road:
where the edge of the road leaving meets the edge of the road it
leaves, plus the corner, and with the value of the acute corner used
on both sides. The ring bends away from a road leaving it to the
outside, so the edges meet sooner, and a road leaving at a slant meets
the ring much sooner on the side it leans away from. Each junction took
too much of the ring, and the next road was refused with "Too close to
a junction": with roads joined at a slant only two fitted on a
roundabout of 12-15 m.

On a roundabout the room on each side is now measured on the circle of
the ring's outer edge, never more than before. In the editor, five
roads at 45 degrees now join a roundabout of 15 m and four at 30 degrees
one of 12 m (two before); roads joined straight out gain less: two
instead of one on the smallest roundabout (8 m), four of six on one of
16 m instead of three. Saved roundabouts load as before; only junctions
made from now on are laid out differently.

Fixes #349

* fix(editor): junctions on a roundabout leave shorter pieces of the ring

A junction cut into a road leaves at least 2 m of the piece it cuts.
When less would be left, it takes the rest of the piece, so a junction
next to the end of one of the ring's arcs grew by up to 2 m, and the
next junction had to stay 2 m further away. On a roundabout of 8-10 m,
where each road joined straight out takes about 14 m of the ring, that
was the difference between two roads and three or four.

The ring goes on round past such a piece, so on a roundabout the
shortest piece is now 0.5 m (editor_ring::shortest_piece). Roads of
their own keep the 2 m. In the editor, three roads joined straight out
now fit on the smallest roundabout (8 m) and four on one of 9.6 m or
more, wherever they join the ring; six fit on 15 and 16 m. Four roads
on the 8 m one would take smaller corners, and are still refused.
Saved roundabouts load as before.

* fix(editor): the reaches of a junction on a roundabout in a std::array
2026-11-25 05:23:37 +01:00

144 lines
7.4 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "editor/editorRingRoom.hpp"
#include <cmath>
// the room a road leaving the side of a roundabout takes of the ring (eu07/maszyna GH#349): with the ring's bend
// counted, as many roads fit on a small roundabout as there's room for between their edges
namespace {
double constexpr pi{3.14159265358979323846};
// as the road editor has them: a junction takes the road it's made on past where the edges meet by the corner room,
// and between two junctions there's at least the shortest road left
double constexpr kCornerRoom{4.0};
double constexpr kShortestRoad{2.0};
// the roads the editor builds unless told otherwise: a lane each way, 3.5 m each, and a roundabout of a single lane
double constexpr kRoadHalfwidth{3.5};
double constexpr kRingHalfwidth{1.75};
glm::dvec2 at(double const Radius, double const Angle) { return glm::dvec2{std::cos(Angle), std::sin(Angle)} * Radius; }
glm::dvec2 ahead(double const Angle) { return {-std::sin(Angle), std::cos(Angle)}; }
// room of a road leaving the ring of the radius at the angle, turned from the way straight out by the slant (to the way ahead)
double room(double const Radius, double const Slant, bool const Ahead, double const Halfwidth = kRingHalfwidth, double const Branchhalfwidth = kRoadHalfwidth) {
double const angle{0.3};
auto const outwards{at(1.0, angle)};
auto const direction{outwards * std::cos(Slant) + ahead(angle) * std::sin(Slant)};
return editor_ring::edge_room(glm::dvec2{0.0}, Radius, at(Radius, angle), direction, Ahead ? ahead(angle) : -ahead(angle), Halfwidth, Branchhalfwidth);
}
// where the edges of two straight roads meet, measured along the first one, as the editor has it for a road leaving a straight road.
// Lean: the second one leans towards the side measured (the angle between them there is the acute one)
double straight_reach(double const Halfwidth, double const Branchhalfwidth, double const Angle, bool const Lean) {
return (Branchhalfwidth + (Lean ? 1.0 : -1.0) * Halfwidth * std::cos(Angle)) / std::sin(Angle);
}
// whether the roads leaving straight out, evenly all around the ring, each get a junction: the junctions with what's left between them.
// Bend: the room measured on the ring, rather than as on a straight road; Shortest: the shortest piece left between two junctions.
// Arcs: a junction next to the end of an arc of the ring takes the rest of the arc when less than the shortest piece would be left
// of it, so in the worst case it grows by as much
bool fit(double const Radius, int const Roads, bool const Bend, double const Shortest, bool const Arcs) {
auto const side{Bend ? room(Radius, 0.0, true) : straight_reach(kRingHalfwidth, kRoadHalfwidth, pi / 2.0, true)};
auto const junction{2.0 * (side + kCornerRoom)};
return Roads * (junction + Shortest + (Arcs ? Shortest : 0.0)) <= 2.0 * pi * Radius;
}
bool fit(double const Radius, int const Roads, bool const Bend) { return fit(Radius, Roads, Bend, kShortestRoad, false); }
// as the editor has it now, wherever the roads join the ring
bool fits(double const Radius, int const Roads) { return fit(Radius, Roads, true, editor_ring::shortest_piece, true); }
} // namespace
TEST_CASE("ring room: on a very large ring the room is the one of a straight road") {
for (double const slant : {0.0, pi / 12.0, pi / 6.0, pi / 4.0, pi / 3.0}) {
CAPTURE(slant);
double const angle{pi / 2.0 - slant};
CHECK(room(1.0e6, slant, true) == doctest::Approx(straight_reach(kRingHalfwidth, kRoadHalfwidth, angle, true)).epsilon(1e-4));
CHECK(room(1.0e6, slant, false) == doctest::Approx(straight_reach(kRingHalfwidth, kRoadHalfwidth, angle, false)).epsilon(1e-4));
}
}
TEST_CASE("ring room: a road leaving straight out meets the outer edge where the circle reaches its edge") {
for (double const radius : {8.0, 15.0, 40.0}) {
CAPTURE(radius);
double const expected{radius * std::asin(kRoadHalfwidth / (radius + kRingHalfwidth))};
CHECK(room(radius, 0.0, true) == doctest::Approx(expected));
CHECK(room(radius, 0.0, false) == doctest::Approx(expected));
// never more than on a straight road
CHECK(room(radius, 0.0, true) < kRoadHalfwidth);
}
}
TEST_CASE("ring room: a slanted road takes less on the side it leans away from") {
for (double const radius : {8.0, 15.0, 50.0}) {
for (double const slant : {pi / 6.0, pi / 4.0}) {
CAPTURE(radius);
CAPTURE(slant);
auto const towards{room(radius, slant, true)};
auto const away{room(radius, slant, false)};
CHECK(away >= 0.0);
CHECK(away < towards);
// the editor took the room of the acute corner on both sides
CHECK(towards < straight_reach(kRingHalfwidth, kRoadHalfwidth, pi / 2.0 - slant, true));
}
}
// on a straight road the obtuse corner takes less than the acute one too
CHECK(room(1.0e6, pi / 4.0, false) < room(1.0e6, pi / 4.0, true));
// leaving the smallest ring as sharply as the editor allows, 30 degrees off it, the edge on the side the road leans away from
// is outside the ring all along: there's no point to measure to, and the editor keeps the room it took before
CHECK(room(8.0, pi / 3.0, false) < 0.0);
CHECK(room(8.0, pi / 3.0, true) > 0.0);
CHECK(room(15.0, pi / 3.0, false) >= 0.0);
}
TEST_CASE("ring room: none for a road going inwards") {
CHECK(room(15.0, pi * 0.75, true) < 0.0);
CHECK(room(15.0, pi, true) < 0.0);
CHECK(editor_ring::edge_room(glm::dvec2{0.0}, 0.0, glm::dvec2{1.0, 0.0}, glm::dvec2{1.0, 0.0}, glm::dvec2{0.0, 1.0}, 1.0, 1.0) < 0.0);
}
TEST_CASE("ring room: a road wider than the ring is round still meets its edge, sooner than on a straight road") {
// two lanes each way on the smallest roundabout: the edge of the road starts outside the ring
double const halfwidth{7.0};
double const radius{8.0};
double const expected{radius * std::atan2(halfwidth, std::sqrt((radius + kRingHalfwidth) * (radius + kRingHalfwidth) - halfwidth * halfwidth))};
CHECK(room(radius, 0.0, true, kRingHalfwidth, halfwidth) == doctest::Approx(expected));
CHECK(room(radius, 0.0, true, kRingHalfwidth, halfwidth) < halfwidth);
}
TEST_CASE("ring room: roads joined to roundabouts of the radii the editor allows") {
// the smallest roundabout took two roads; three were too many for it
CHECK(fit(8.0, 2, false));
CHECK_FALSE(fit(8.0, 3, false));
CHECK(fit(8.0, 3, true));
// four roads from 11 m
CHECK(fit(11.0, 4, true));
CHECK(fit(15.0, 4, true));
// six from 16 m, where they took 17 m before
CHECK_FALSE(fit(16.0, 6, false));
CHECK(fit(16.0, 6, true));
CHECK(fit(25.0, 6, true));
// and more on the large ones
CHECK(fit(50.0, 8, true));
CHECK(fit(200.0, 30, true));
}
TEST_CASE("ring room: a short piece of the ring is left between junctions, so small roundabouts take three or four roads") {
// the ring goes on past a piece of it, which can be much shorter than a road of its own
CHECK(editor_ring::shortest_piece < kShortestRoad);
CHECK(editor_ring::shortest_piece > 0.25); // further apart than ends taken for joined
// with the shortest road left between them, and a junction growing by as much next to the end of an arc, three roads
// didn't always fit on the smallest roundabout, nor four on one of 10 m
CHECK_FALSE(fit(8.0, 3, true, kShortestRoad, true));
CHECK_FALSE(fit(10.0, 4, true, kShortestRoad, false));
// they do now, wherever they join the ring
CHECK(fits(8.0, 3));
CHECK(fits(10.0, 4));
CHECK(fits(9.6, 4));
// four on the smallest one would take smaller corners
CHECK_FALSE(fits(8.0, 4));
// and the larger ones take more
CHECK(fits(15.0, 6));
CHECK(fits(25.0, 8));
}