18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/editor/editorRingRoom.hpp
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

64 lines
3.4 KiB
C++

/*
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 <algorithm>
#include <cmath>
#include <glm/glm.hpp>
// 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