diff --git a/editor/editorRingRoom.hpp b/editor/editorRingRoom.hpp new file mode 100644 index 000000000..9051da616 --- /dev/null +++ b/editor/editorRingRoom.hpp @@ -0,0 +1,63 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include +#include +#include + +// the room a road leaving the side of a roundabout takes of the ring, seen from above. on a straight road the edge of the road +// leaving meets the edge of the road it leaves where the two straight lines cross; the ring bends away from the road leaving it +// to the outside, so there the edges meet sooner; and a road leaving at a slant meets the edge of the ring sooner on the side +// it leans away from than on the side it leans towards +namespace editor_ring +{ + +// the shortest piece of the ring a junction leaves before the next junction, or before the end of the arc it's cut from. +// the ring goes on past it, so it can be much shorter than a road of its own; a junction which would leave less takes +// the rest of the arc, and the shorter this is the less a junction grows next to the end of an arc +double constexpr shortest_piece{0.5}; + +// how far along the axis of the ring, from the point a road leaves it, the edge of that road meets the outer edge of the ring. +// Centre: of the ring, Radius: of its axis, Point: where the axis of the road leaving meets the axis of the ring, +// Direction: the way the road leaves, Along: the way along the ring the room is measured, Halfwidth: of the ring, +// Branchhalfwidth: of the road leaving. returns: the distance along the axis of the ring, 0 if the edges cross before they get +// past the point the road leaves, or a negative value if there's no such point: the road doesn't leave towards the outside, +// or it leans so far that the edge on that side stays outside the ring all along +inline double edge_room(glm::dvec2 const &Centre, double const Radius, glm::dvec2 const &Point, glm::dvec2 const &Direction, glm::dvec2 const &Along, double const Halfwidth, + double const Branchhalfwidth) +{ + if (Radius <= 0.0 || glm::length(Direction) < 1e-9 || glm::length(Point - Centre) < 1e-9) + return -1.0; + auto const direction{glm::normalize(Direction)}; + auto const outwards{glm::normalize(Point - Centre)}; + if (glm::dot(direction, outwards) < 1e-6) + return -1.0; + // the edge of the road leaving on the side the room is measured for + glm::dvec2 side{-direction.y, direction.x}; + if (glm::dot(side, Along) < 0.0) + side = -side; + auto const start{Point - Centre + side * Branchhalfwidth}; + auto const edge{Radius + Halfwidth}; + // start + direction * t on the circle of the outer edge, where the edge leaves it going out. a road wide enough, or leaning + // far enough, has the start of that edge outside the circle already: the edges crossed before it + auto const b{glm::dot(start, direction)}; + auto const discriminant{b * b + edge * edge - glm::dot(start, start)}; + if (discriminant < 0.0) + return -1.0; + auto const t{-b + std::sqrt(discriminant)}; + auto const meet{start + direction * t}; + // the angle from the point the road leaves to that point, around the centre and the way the room is measured, carried onto the axis + auto const turn{outwards.x * Along.y - outwards.y * Along.x < 0.0 ? -1.0 : 1.0}; + auto const angle{std::atan2(turn * (outwards.x * meet.y - outwards.y * meet.x), glm::dot(outwards, meet))}; + return std::max(0.0, Radius * angle); +} + +} // namespace editor_ring diff --git a/editor/editorRoad.cpp b/editor/editorRoad.cpp index 51aa495f3..b803416a8 100644 --- a/editor/editorRoad.cpp +++ b/editor/editorRoad.cpp @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "editor/editorRoad.hpp" #include "editor/editorFormat.hpp" +#include "editor/editorRingRoom.hpp" #include "editor/editorTrack.hpp" #include "world/Track.h" @@ -274,7 +275,9 @@ bool follow(road_node &Road, double const T, bool const Forward, double const Di auto const lengths{stations(state)}; auto const here{station_at(lengths, from)}; auto const available{forward ? lengths.back() - here : here}; - if (remaining <= 0.0 || available >= remaining + kShortestRoad) + // what's left of a piece of a roundabout carries on round, so it can be shorter than a road of its own + auto const shortest{state.roundabout ? editor_ring::shortest_piece : kShortestRoad}; + if (remaining <= 0.0 || available >= remaining + shortest) { // the cut is put here; with nothing left to go the piece is kept whole Cut.road = piece; @@ -339,6 +342,27 @@ double edge_reach(double const Halfwidth, double const Otherhalfwidth, double co return (Otherhalfwidth + Halfwidth * std::cos(Angle)) / std::sin(Angle); } +// the circle the axis of a piece bends along at specified value of its curve parameter, seen from above. returns: false if it's straight there +bool ring_circle(road_node::state const &State, double const T, glm::dvec2 &Centre, double &Radius) +{ + auto const points{curve_points(State.axis)}; + auto const u{1.0 - T}; + auto const first{3.0 * u * u * (points[1] - points[0]) + 6.0 * u * T * (points[2] - points[1]) + 3.0 * T * T * (points[3] - points[2])}; + auto const second{6.0 * u * (points[2] - 2.0 * points[1] + points[0]) + 6.0 * T * (points[3] - 2.0 * points[2] + points[1])}; + glm::dvec2 const velocity{first.x, first.z}; + auto const speed{glm::length(velocity)}; + if (speed < 1e-9) + return false; + auto const curvature{(velocity.x * second.z - velocity.y * second.x) / (speed * speed * speed)}; + if (std::abs(curvature) < 1e-5) + return false; + auto const point{curve_point(points, T)}; + auto const tangent{velocity / speed}; + Centre = glm::dvec2{point.x, point.z} + glm::dvec2{-tangent.y, tangent.x} / curvature; + Radius = 1.0 / std::abs(curvature); + return true; +} + // the look of a junction goes after the road it's made on void dress(junction_node::state &Junction, road_node::state const &Road) { @@ -1852,11 +1876,27 @@ bool editor_road::branch_from(road_node &Road, double const T, glm::dvec2 const auto const branchhalfwidth{0.5 * (Incoming + Outgoing) * Width}; auto const reach{edge_reach(halfwidth, branchhalfwidth, angle) + kCornerRoom}; auto const branchreach{edge_reach(branchhalfwidth, halfwidth, angle) + kCornerRoom}; + // how far the junction takes the road back along it and ahead of the centre + std::array reaches{reach, reach}; + glm::dvec2 ringcentre{0.0}; + double ringradius{0.0}; + if (layout.roundabout && ring_circle(layout, T, ringcentre, ringradius)) + { + // the ring bends away from a road leaving it to the outside, so their edges meet sooner than on a straight road; and a road + // leaving at a slant meets the edge of the ring sooner on the side it leans away from. the room of a straight road, measured + // on the side the road leans towards, is the most a side takes + for (int side = 0; side < 2; ++side) + { + auto const room{editor_ring::edge_room(ringcentre, ringradius, glm::dvec2{centre.x, centre.z}, direction, side == 0 ? -along : along, halfwidth, branchhalfwidth)}; + if (room >= 0.0) + reaches[side] = std::min(reach, room + kCornerRoom); + } + } // where the road is cut on both sides of the centre road_cut cuts[2]; std::vector consumed; - if (false == follow(Road, T, false, reach, cuts[0], consumed, Error) || false == follow(Road, T, true, reach, cuts[1], consumed, Error)) + if (false == follow(Road, T, false, reaches[0], cuts[0], consumed, Error) || false == follow(Road, T, true, reaches[1], cuts[1], consumed, Error)) return false; if (cuts[0].road == cuts[1].road && cuts[0].road != &Road) { diff --git a/tests/unit/test_ring_room.cpp b/tests/unit/test_ring_room.cpp new file mode 100644 index 000000000..835072f75 --- /dev/null +++ b/tests/unit/test_ring_room.cpp @@ -0,0 +1,143 @@ +#include "stdafx.h" +#include "doctest.h" +#include "editor/editorRingRoom.hpp" + +#include + +// 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)); +}