18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 17:37:09 +01:00
Files
maszyna/editor/layout/layoutNetwork.cpp
T
maj00r 6f0b8be091 feat(editor): the edits E2a left: vertices, the grade line, the class and the profile of a line, a join from a curve, no new BLOCK
The rest of the list E2a left for E2b (editor-tracks-domain.md §8), commands of the domain model of the track geometry like the others:
kept whole or refused with the reasons, one step to undo.

- insert_vertex (after a vertex, or after the start, with its radius, transitions and cant, its id given) and remove_vertex.
- set_grades: the breaks of the grade line with their heights and vertical radii; checked by the solver (overlapping vertical curves)
  and the norms.
- set_line_class and set_line_profile: the class of a line (its speed, traffic, main or side track) and a profile of the norms of its
  own (the line's own wins over the areas, as norms::area_map has it); a chain of the norms doesn't run on into a line of another
  profile of its own. The tracks of a turnout take a class too, their speeds derived again.
- join_ends from a free end in a curve (a line cut inside one): one curve where the tangents meet, going on from the curve at the end,
  its arc of the radius there with no transition (or, where that doesn't fit, a radius the norms prefer and a transition from the
  curvature of the end), the transition at the straight end as long as makes the curve begin right at the curved end (bisected to the
  bit), refused where both ends lie in curves or the curve would turn the other way. The original join and this one lay the new line
  the same way (add_joined).
- edit_policy::no_new_block for old tracks (stage E6): an edit is refused by a BLOCK finding only where it adds one; one there before,
  of the same parameter on the same stretch (the elements within 1.5 m, the findings carry their element as points of the plan) and no
  worse, doesn't refuse; the same rule of the same turnout neither. The invariants refuse in any case. model(network) takes a network
  over as it is, the norms not asked.

Tests: U: a vertex put in and another after it, both taken out, the line as it was; a grade break and its vertical curve, two
overlapping and one of no radius refused; a class of 200 km/h refused on curves fine at 80, one of 60 kept; R 280 m at 80 km/h warned
by PLK, not by the TSI as the line's own profile; a line joined to a free end in an arc going on from it with the transition the part
taken away had, the track the same within a micrometre; both ends in curves refused; R 170 m laid by the TSI and judged by PLK: an
edit refused when any BLOCK refuses, kept with no_new_block, a sharper curve and a cant past (R - 50) / 1.5 refused, a wider curve
kept. P: 250 random edits of an old track breaking the norms (curves laid for 80 km/h judged at 120), seed 4242, with no_new_block: no
BLOCK finding over all the network which wasn't there before, a refusal changing nothing, an undo exact; the random edits with
turnouts put vertices into straight lines and take them out.
2026-11-23 12:41:49 +01:00

804 lines
29 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/.
*/
#include "stdafx.h"
#include "editor/layout/layoutNetwork.hpp"
#include "editor/layout/layoutTurnout.hpp"
#include "editor/editorGeometry.hpp"
#include <algorithm>
#include <cmath>
#include <format>
namespace layout
{
namespace
{
double constexpr kHeading{1e-9}; // rad: the ends of a node head the same way within this
double constexpr kCurvature{1e-9}; // 1/m: a smaller difference of the curvature isn't a step
double constexpr kCantStep{0.5}; // mm, as the norms engine takes a step of the cant
double constexpr kGradeBreak{0.002}; // a change of the grade up to 2 per mille needn't be rounded (ST-T1-A6 10.2 pkt 2)
std::string line_name(line_id const Id)
{
return std::format("line {}", number(Id));
}
std::string node_name(node_id const Id)
{
return std::format("node {}", number(Id));
}
// the values of the end of the line as the node sees them, along its heading
struct seen_end
{
glm::dvec2 heading;
double curvature;
double cant;
double grade;
};
seen_end seen(line const &Line, end_ref const &End)
{
auto const &values{End.end == line_end::start ? Line.solved.start : Line.solved.end};
auto const sense{static_cast<double>(End.sense)};
return {values.heading * sense, values.curvature * sense, values.cant * sense, values.grade * sense};
}
// the node takes what the ends it holds tell: the heading of the only one, the curvature and the cant of the first one which doesn't go
// on from it (one going on reads them), the grade of the first; the end of the diverging track of a turnout first, which the turnout lays,
// the start of one not at all, which leaves the node at the curvature of the main track. True where the curvature or the cant changed
bool derive(network const &Network, node &Node)
{
auto const before{std::make_pair(Node.curvature, Node.cant)};
bool graded{false};
bool curved{false};
std::vector<end_ref> ends;
for (auto const &end : Node.ends)
if (auto const *owner{owner_of(Network, end.line)}; owner != nullptr && owner->diverging == end.line && end.end == line_end::end)
ends.insert(ends.begin(), end);
else if (false == diverging_start(Network, end))
ends.push_back(end);
for (auto const &end : ends)
{
auto const *owner{Network.find(end.line)};
if (owner == nullptr || false == owner->solved.valid())
continue;
auto const values{seen(*owner, end)};
if (Node.ends.size() == 1)
Node.heading = values.heading;
if (false == graded)
{
Node.grade = values.grade;
graded = true;
}
if (false == curved && false == owner->continues(end.end))
{
Node.curvature = values.curvature;
Node.cant = values.cant;
curved = true;
}
}
return before != std::make_pair(Node.curvature, Node.cant);
}
norms::element reversed(norms::element Element, double const Length)
{
auto const start{Element.start};
Element.start = Length - (start + Element.length);
auto const curvature{Element.curvature_start};
Element.curvature_start = -Element.curvature_end;
Element.curvature_end = -curvature;
std::swap(Element.from, Element.to);
std::swap(Element.open_start, Element.open_end);
return Element;
}
// the elements run on alike: the same kind, the curvature going on as it did
bool runs_on(norms::element const &A, norms::element const &B)
{
if (A.kind != B.kind || std::abs(A.curvature_end - B.curvature_start) > kCurvature || A.speed != B.speed || A.features != B.features)
return false;
if (A.kind != norms::element_kind::transition)
return std::abs(A.curvature_start - B.curvature_end) <= kCurvature;
auto const rate_a{(A.curvature_end - A.curvature_start) / std::max(A.length, 1e-9)};
auto const rate_b{(B.curvature_end - B.curvature_start) / std::max(B.length, 1e-9)};
return std::abs(rate_a - rate_b) <= 1e-6 * std::max(std::abs(rate_a), std::abs(rate_b)) + 1e-12;
}
char const *parameter_name(norms::parameter const What)
{
switch (What)
{
case norms::parameter::cant_deficiency: return "cant deficiency";
case norms::parameter::cant_excess: return "cant excess";
case norms::parameter::deficiency_jump: return "step of the cant deficiency";
case norms::parameter::cant_gradient: return "cant ramp";
case norms::parameter::cant_rate: return "rate of the cant change";
case norms::parameter::deficiency_rate: return "rate of the cant deficiency change";
case norms::parameter::element_length: return "element length";
case norms::parameter::transition_length: return "transition length";
case norms::parameter::transition_shift: return "transition shift";
case norms::parameter::radius: return "radius";
case norms::parameter::cant: return "cant";
case norms::parameter::cant_min: return "least cant";
case norms::parameter::vertical_radius: return "vertical radius";
case norms::parameter::grade: return "grade";
case norms::parameter::intermediate_length: return "element between steps";
case norms::parameter::profile_boundary: return "arc across a profile boundary";
default: return "value";
}
}
} // namespace
node const *network::find(node_id const Id) const
{
auto const found{m_nodes.find(Id)};
return found != m_nodes.end() ? &found->second : nullptr;
}
line const *network::find(line_id const Id) const
{
auto const found{m_lines.find(Id)};
return found != m_lines.end() ? &found->second : nullptr;
}
turnout const *network::find(turnout_id const Id) const
{
auto const found{m_turnouts.find(Id)};
return found != m_turnouts.end() ? &found->second : nullptr;
}
end_ref const *network::end_of(line_id const Line, line_end const End) const
{
auto const *owner{find(Line)};
if (owner == nullptr)
return nullptr;
auto const *at{find(owner->node_at(End))};
if (at == nullptr)
return nullptr;
auto const found{std::ranges::find_if(at->ends, [&](end_ref const &Ref) { return Ref.line == Line && Ref.end == End; })};
return found != at->ends.end() ? &*found : nullptr;
}
end_condition network::condition(line const &Line, line_end const End) const
{
end_condition result;
auto const *at{find(Line.node_at(End))};
if (at == nullptr)
return result;
result.position = at->position;
result.continues = Line.continues(End);
if (auto const *ref{end_of(Line.id, End)}; ref != nullptr && result.continues)
{
result.curvature = at->curvature * ref->sense;
result.cant = at->cant * ref->sense;
}
return result;
}
line &transaction::change(line_id const Id)
{
auto &kept{m_before.lines};
if (false == kept.contains(Id))
{
auto const found{m_network.m_lines.find(Id)};
kept[Id] = found != m_network.m_lines.end() ? std::optional<line>{found->second} : std::nullopt;
}
return m_network.m_lines.at(Id);
}
node &transaction::change(node_id const Id)
{
auto &kept{m_before.nodes};
if (false == kept.contains(Id))
{
auto const found{m_network.m_nodes.find(Id)};
kept[Id] = found != m_network.m_nodes.end() ? std::optional<node>{found->second} : std::nullopt;
}
return m_network.m_nodes.at(Id);
}
turnout &transaction::change(turnout_id const Id)
{
auto &kept{m_before.turnouts};
if (false == kept.contains(Id))
{
auto const found{m_network.m_turnouts.find(Id)};
kept[Id] = found != m_network.m_turnouts.end() ? std::optional<turnout>{found->second} : std::nullopt;
}
return m_network.m_turnouts.at(Id);
}
line &transaction::add(line Line)
{
auto const id{Line.id};
if (false == m_before.lines.contains(id))
m_before.lines[id] = m_network.find(id) != nullptr ? std::optional<line>{*m_network.find(id)} : std::nullopt;
return m_network.m_lines[id] = std::move(Line);
}
node &transaction::add(node Node)
{
auto const id{Node.id};
if (false == m_before.nodes.contains(id))
m_before.nodes[id] = m_network.find(id) != nullptr ? std::optional<node>{*m_network.find(id)} : std::nullopt;
return m_network.m_nodes[id] = std::move(Node);
}
turnout &transaction::add(turnout Turnout)
{
auto const id{Turnout.id};
if (false == m_before.turnouts.contains(id))
m_before.turnouts[id] = m_network.find(id) != nullptr ? std::optional<turnout>{*m_network.find(id)} : std::nullopt;
return m_network.m_turnouts[id] = std::move(Turnout);
}
void transaction::erase(line_id const Id)
{
if (m_network.find(Id) == nullptr)
return;
change(Id);
m_network.m_lines.erase(Id);
}
void transaction::erase(node_id const Id)
{
if (m_network.find(Id) == nullptr)
return;
change(Id);
m_network.m_nodes.erase(Id);
}
void transaction::erase(turnout_id const Id)
{
if (m_network.find(Id) == nullptr)
return;
change(Id);
m_network.m_turnouts.erase(Id);
}
std::set<line_id> transaction::touched_lines() const
{
std::set<line_id> result;
for (auto const &[id, before] : m_before.lines)
result.insert(id);
return result;
}
std::set<node_id> transaction::touched_nodes() const
{
std::set<node_id> result;
for (auto const &[id, before] : m_before.nodes)
result.insert(id);
return result;
}
std::set<turnout_id> transaction::touched_turnouts() const
{
std::set<turnout_id> result;
for (auto const &[id, before] : m_before.turnouts)
result.insert(id);
return result;
}
snapshot transaction::after() const
{
snapshot result;
for (auto const &[id, before] : m_before.lines)
result.lines[id] = m_network.find(id) != nullptr ? std::optional<line>{*m_network.find(id)} : std::nullopt;
for (auto const &[id, before] : m_before.nodes)
result.nodes[id] = m_network.find(id) != nullptr ? std::optional<node>{*m_network.find(id)} : std::nullopt;
for (auto const &[id, before] : m_before.turnouts)
result.turnouts[id] = m_network.find(id) != nullptr ? std::optional<turnout>{*m_network.find(id)} : std::nullopt;
return result;
}
void transaction::roll_back()
{
restore(m_network, m_before);
m_before = {};
}
void restore(network &Network, snapshot const &State)
{
// through a transaction of its own, which is the friend of the network
transaction change{Network};
for (auto const &[id, kept] : State.lines)
{
if (kept.has_value())
change.add(*kept);
else
change.erase(id);
}
for (auto const &[id, kept] : State.nodes)
{
if (kept.has_value())
change.add(*kept);
else
change.erase(id);
}
for (auto const &[id, kept] : State.turnouts)
{
if (kept.has_value())
change.add(*kept);
else
change.erase(id);
}
}
turnout const *owner_of(network const &Network, line_id const Line)
{
auto const *owner{Network.find(Line)};
return owner != nullptr && owner->turnout != turnout_id::none ? Network.find(owner->turnout) : nullptr;
}
bool diverging_start(network const &Network, end_ref const &End)
{
auto const *owner{owner_of(Network, End.line)};
return owner != nullptr && owner->diverging == End.line && End.end == line_end::start;
}
void settle(transaction &Change)
{
std::set<line_id> pending;
for (auto const id : Change.touched_lines())
if (Change.view().find(id) != nullptr)
pending.insert(id);
std::set<turnout_id> turnouts;
for (auto const id : Change.touched_turnouts())
if (Change.view().find(id) != nullptr)
turnouts.insert(id);
// the nodes touched take the values of their ends too, the lines going on from them laid again where those changed; a few rounds
// settle it, each line going on from a curve depending on one node at each end, a diverging track on the start and the main track of
// its turnout
std::set<node_id> nodes{Change.touched_nodes()};
for (int round = 0; round < 8 && (false == pending.empty() || false == nodes.empty() || false == turnouts.empty()); ++round)
{
std::set<line_id> next;
for (auto const id : pending)
{
if (auto const *owner{owner_of(Change.view(), id)}; owner != nullptr && owner->diverging == id)
{
turnouts.insert(owner->id);
continue;
}
auto &laid{Change.change(id)};
auto const &view{Change.view()};
laid.solved = solve(laid.shape, view.condition(laid, line_end::start), view.condition(laid, line_end::end));
nodes.insert(laid.start);
nodes.insert(laid.end);
}
// the turnouts whose main track or start changed lay their diverging tracks, the node at the end of one where it ends now, and the
// lines going on from there laid again
for (auto const &[id, each] : Change.view().turnouts())
if (pending.contains(each.main) || nodes.contains(each.start))
turnouts.insert(id);
for (auto const id : turnouts)
{
auto const *existing{Change.view().find(id)};
if (existing == nullptr)
continue;
auto updated{*existing};
derive_turnout(Change.view(), updated);
if (updated != *existing)
Change.change(id) = updated;
auto const type{turnout_type_of(updated.type)};
auto const *track{Change.view().find(updated.diverging)};
if (false == type.has_value() || track == nullptr)
continue;
auto const laid{lay_diverging(frame_of(Change.view(), updated), *type, updated.hand)};
if (laid != track->solved)
Change.change(updated.diverging).solved = laid;
auto const *at{Change.view().find(updated.diverging_end)};
if (at == nullptr || false == laid.valid())
continue;
auto moved{*at};
moved.position = laid.end.position;
moved.heading = laid.end.heading;
moved.curvature = laid.end.curvature;
moved.cant = laid.end.cant;
moved.grade = laid.end.grade;
if (moved == *at)
continue;
Change.change(updated.diverging_end) = moved;
nodes.insert(updated.diverging_end);
for (auto const &end : moved.ends)
if (end.line != updated.diverging)
next.insert(end.line);
}
turnouts.clear();
for (auto const id : nodes)
{
auto const *existing{Change.view().find(id)};
if (existing == nullptr)
continue;
auto updated{*existing};
auto const curved{derive(Change.view(), updated)};
if (updated != *existing)
Change.change(id) = updated;
if (false == curved)
continue;
for (auto const &end : updated.ends)
if (auto const *owner{Change.view().find(end.line)}; owner != nullptr && owner->continues(end.end))
next.insert(end.line);
}
nodes.clear();
pending = std::move(next);
}
}
std::vector<chain_part> chain_of(network const &Network, line_id const Line)
{
std::vector<chain_part> result;
auto const *first{Network.find(Line)};
if (first == nullptr)
return result;
result.push_back({Line, true});
auto const contains = [&](line_id const Id) { return std::ranges::any_of(result, [&](chain_part const &Part) { return Part.line == Id; }); };
// the other end at the node, where it holds two; at the start of a turnout the line it starts from runs on into its main track, its
// diverging track into none. Not into a line of another profile of its own
auto const across = [&](node_id const At, line_id const From, line_end const End) -> end_ref const * {
auto const *at{Network.find(At)};
if (at == nullptr || diverging_start(Network, {From, End, 1}))
return nullptr;
end_ref const *from{nullptr};
std::vector<end_ref const *> others;
for (auto const &end : at->ends)
if (end.line == From && end.end == End)
from = &end;
else if (false == diverging_start(Network, end))
others.push_back(&end);
if (from == nullptr || others.size() != 1 || others.front()->side() == from->side())
return nullptr;
auto const *next{Network.find(others.front()->line)};
if (next == nullptr || next->profile != Network.find(From)->profile)
return nullptr;
return others.front();
};
// on beyond the end of the last part
while (true)
{
auto const &last{result.back()};
auto const *owner{Network.find(last.line)};
auto const beyond{last.forward ? line_end::end : line_end::start};
auto const *other{across(owner->node_at(beyond), last.line, beyond)};
if (other == nullptr || contains(other->line) || Network.find(other->line) == nullptr)
break;
result.push_back({other->line, other->end == line_end::start});
}
// and back beyond the start of the first one
while (true)
{
auto const &head{result.front()};
auto const *owner{Network.find(head.line)};
auto const behind{head.forward ? line_end::start : line_end::end};
auto const *other{across(owner->node_at(behind), head.line, behind)};
if (other == nullptr || contains(other->line) || Network.find(other->line) == nullptr)
break;
result.insert(result.begin(), {other->line, other->end == line_end::end});
}
return result;
}
described_chain describe_chain(network const &Network, std::vector<chain_part> const &Chain)
{
described_chain result;
double offset{0.0};
for (auto const &part : Chain)
{
auto const &owner{*Network.find(part.line)};
auto own{described(owner.solved, owner.category)};
auto const length{owner.solved.length};
// a track of a turnout: a connection of tracks, its frog, bent or not, its own speed
if (auto const *switched{owner_of(Network, owner.id)})
{
auto const diverging{switched->diverging == owner.id};
auto const features{norms::feature::switch_track | (switched->movable ? norms::feature::movable_frog : 0u) | (switched->bent() ? norms::feature::curved_switch : 0u) |
(diverging ? norms::feature::diverging : 0u)};
auto const speed{diverging ? switched->speed_diverging : switched->speed_main};
for (auto &element : own.elements)
{
element.features |= features;
if (speed > 0.0)
element.speed = std::min(element.speed > 0.0 ? element.speed : speed, speed);
}
}
result.starts.push_back(offset);
if (false == part.forward)
{
for (auto &element : own.elements)
element = reversed(element, length);
std::ranges::reverse(own.elements);
for (auto &point : own.cant)
point = {length - point.chainage, -point.cant};
std::ranges::reverse(own.cant);
for (auto &grade : own.grades)
grade = {length - (grade.start + grade.length), grade.length, -grade.grade_end, -grade.grade_start};
std::ranges::reverse(own.grades);
}
for (auto element : own.elements)
{
element.start += offset;
auto &elements{result.line.elements};
if (false == elements.empty() && runs_on(elements.back(), element))
{
elements.back().length = element.end() - elements.back().start;
elements.back().curvature_end = element.curvature_end;
elements.back().to = element.to;
continue;
}
elements.push_back(element);
}
for (auto point : own.cant)
{
point.chainage += offset;
auto &cant{result.line.cant};
if (false == cant.empty() && std::abs(cant.back().chainage - point.chainage) < 1e-9 && std::abs(cant.back().cant - point.cant) < 1e-9)
continue;
cant.push_back(point);
}
for (auto grade : own.grades)
{
grade.start += offset;
result.line.grades.push_back(grade);
}
offset += length;
}
if (false == result.line.elements.empty())
{
result.line.elements.front().open_start = true;
result.line.elements.back().open_end = true;
}
// an element cut by a turnout: its length isn't its own
auto &elements{result.line.elements};
for (std::size_t i = 1; i < elements.size(); ++i)
if ((elements[i - 1].features & norms::feature::switch_track) != (elements[i].features & norms::feature::switch_track))
{
elements[i - 1].open_end = true;
elements[i].open_start = true;
}
if (false == Chain.empty())
result.line.name = std::format("l{}", number(Chain.front().line));
return result;
}
bool same_finding(notice const &Old, notice const &Found)
{
if (Found.turnout != turnout_id::none || Old.turnout != turnout_id::none)
return Found.turnout == Old.turnout && Found.text == Old.text;
if (Old.finding.what != Found.finding.what)
return false;
auto const margin{1e-6 * std::max(1.0, std::abs(Old.finding.value))};
if (norms::minimum(Found.finding.what) ? Found.finding.value < Old.finding.value - margin : Found.finding.value > Old.finding.value + margin)
return false;
for (auto const &a : Old.along)
for (auto const &b : Found.along)
if (glm::distance(a, b) <= 1.5)
return true;
return false;
}
std::vector<std::string> verdict::refusals() const
{
auto result{reasons};
for (auto const &block : blocks)
result.push_back(block.turnout != turnout_id::none ? block.text : std::format("{} at {:.1f} m: {}", line_name(block.line), block.chainage, block.text));
return result;
}
std::string finding_text(norms::finding const &Finding)
{
auto text{std::format("{} {:.4g}, the limit {:.4g}", parameter_name(Finding.what), Finding.value, Finding.limit)};
if (false == Finding.from.document.empty())
text += std::format(" ({} {})", Finding.from.document, Finding.from.place);
return text;
}
namespace
{
// the invariants of the line: laid between its nodes, which know its ends
void check_line(network const &Network, line const &Line, verdict &Result)
{
for (auto const &error : Line.solved.errors)
Result.reasons.push_back(std::format("{}: {}", line_name(Line.id), error));
if (Line.start == Line.end)
Result.reasons.push_back(std::format("{} starts and ends at {}", line_name(Line.id), node_name(Line.start)));
if (Line.turnout != turnout_id::none)
{
auto const *owner{Network.find(Line.turnout)};
if (owner == nullptr || (owner->main != Line.id && owner->diverging != Line.id))
Result.reasons.push_back(std::format("{} is a track of turnout {}, which hasn't it", line_name(Line.id), number(Line.turnout)));
}
for (auto const end : {line_end::start, line_end::end})
{
if (Network.find(Line.node_at(end)) == nullptr)
Result.reasons.push_back(std::format("{}: no {} at its {}", line_name(Line.id), node_name(Line.node_at(end)), end == line_end::start ? "start" : "end"));
else if (Network.end_of(Line.id, end) == nullptr)
Result.reasons.push_back(std::format("{} doesn't hold the {} of {}", node_name(Line.node_at(end)), end == line_end::start ? "start" : "end", line_name(Line.id)));
}
}
// the invariants of the node: its ends exist, two at most, on its two sides, and run on as one track through it
void check_node(network const &Network, node const &Node, verdict &Result)
{
if (Node.ends.empty())
Result.reasons.push_back(std::format("{} holds no end of a line", node_name(Node.id)));
// the start of a turnout: its diverging track beside its main track, on its side of the node; one line at most on the other
std::vector<end_ref> ends;
for (auto const &end : Node.ends)
{
if (false == diverging_start(Network, end))
{
ends.push_back(end);
continue;
}
auto const *switched{owner_of(Network, end.line)};
auto const main{std::ranges::find_if(Node.ends, [&](end_ref const &Each) { return Each.line == switched->main; })};
if (switched->start != Node.id || main == Node.ends.end())
Result.reasons.push_back(std::format("{}: the diverging track of turnout {} starts there, its main track doesn't", node_name(Node.id), number(switched->id)));
else if (main->side() != end.side())
Result.reasons.push_back(std::format("{}: the tracks of turnout {} leave it to both sides", node_name(Node.id), number(switched->id)));
}
if (ends.size() > 2)
Result.reasons.push_back(std::format("{} holds {} ends; more than two meet only at the start of a turnout", node_name(Node.id), Node.ends.size()));
if (ends.size() == 2 && ends[0].side() == ends[1].side())
Result.reasons.push_back(std::format("{}: both lines lie on the same side of it", node_name(Node.id)));
for (auto const &end : Node.ends)
{
auto const *owner{Network.find(end.line)};
if (owner == nullptr || owner->node_at(end.end) != Node.id || std::abs(end.sense) != 1)
{
Result.reasons.push_back(std::format("{} holds an end of {}, which doesn't end there", node_name(Node.id), line_name(end.line)));
continue;
}
if (false == owner->solved.valid())
continue;
auto const &values{end.end == line_end::start ? owner->solved.start : owner->solved.end};
if (values.position != Node.position)
Result.reasons.push_back(std::format("{} doesn't end at {}", line_name(end.line), node_name(Node.id)));
auto const ends{seen(*owner, end)};
auto const kink{std::abs(geometry::signed_angle(Node.heading, ends.heading))};
if (kink > kHeading)
Result.reasons.push_back(std::format("{} meets {} at an angle of {:.6f} deg", line_name(end.line), node_name(Node.id), glm::degrees(kink)));
if (std::abs(ends.cant - Node.cant) > kCantStep)
Result.reasons.push_back(std::format("the cant steps by {:.1f} mm at {} where {} meets it", std::abs(ends.cant - Node.cant), node_name(Node.id), line_name(end.line)));
if (std::abs(ends.grade - Node.grade) > kGradeBreak)
Result.reasons.push_back(std::format("the grade breaks by {:.1f} per mille at {} with no vertical curve", 1000.0 * std::abs(ends.grade - Node.grade), node_name(Node.id)));
// a step of the curvature, which the norms judge along the chain (the step of the cant deficiency); one going on from the node
// has its curvature by construction
if (owner->continues(end.end) && std::abs(ends.curvature - Node.curvature) > kCurvature)
Result.reasons.push_back(std::format("{} goes on from the curve of {} at another curvature", line_name(end.line), node_name(Node.id)));
}
}
void check_norms(network const &Network, std::set<line_id> const &Lines, verdict &Result)
{
std::set<line_id> done;
for (auto const id : Lines)
{
if (done.contains(id) || Network.find(id) == nullptr)
continue;
auto const chain{chain_of(Network, id)};
bool laid{true};
for (auto const &part : chain)
{
done.insert(part.line);
laid = laid && Network.find(part.line)->solved.valid();
}
if (false == laid)
continue;
auto const described{describe_chain(Network, chain)};
auto const &first{*Network.find(chain.front().line)};
// the profile of the lines' own wins over the areas: the lines of a chain have the same
auto areas{Network.areas};
if (first.profile.has_value())
areas.lines.insert(areas.lines.begin(), {described.line.name, *first.profile});
for (auto const &finding : norms::evaluate(described.line, first.category, areas))
{
if (finding.level == norms::tier::none)
continue;
// the line it's on, by the chainage along the chain
auto part{static_cast<std::size_t>(std::upper_bound(described.starts.begin(), described.starts.end(), finding.chainage + 1e-9) - described.starts.begin())};
part = part > 0 ? part - 1 : 0;
auto const &owner{*Network.find(chain[part].line)};
auto const along{finding.chainage - described.starts[part]};
auto const chainage{chain[part].forward ? along : owner.solved.length - along};
auto const text{finding_text(finding)};
if (finding.level == norms::tier::block)
{
// where along the chain: the element it's on, as points of the plan
auto const position_at = [&](double const At) {
auto index{static_cast<std::size_t>(std::upper_bound(described.starts.begin(), described.starts.end(), At + 1e-9) - described.starts.begin())};
index = index > 0 ? index - 1 : 0;
auto const &line{Network.find(chain[index].line)->solved};
auto const local{At - described.starts[index]};
return line.position(std::clamp(chain[index].forward ? local : line.length - local, 0.0, line.length));
};
notice found{owner.id, chainage, finding, text};
auto const &element{described.line.elements[std::min(finding.element, described.line.elements.size() - 1)]};
auto const count{std::clamp(static_cast<int>(std::ceil(element.length / 2.0)), 1, 256)};
for (int i = 0; i <= count; ++i)
found.along.push_back(position_at(element.start + element.length * i / count));
found.along.push_back(position_at(finding.chainage));
Result.blocks.push_back(std::move(found));
}
else if (Lines.contains(owner.id))
Result.notices.push_back({owner.id, chainage, finding, text});
}
}
}
} // namespace
verdict check(network const &Network, std::set<line_id> const &Lines, std::set<node_id> const &Nodes, std::set<turnout_id> const &Turnouts)
{
verdict result;
std::set<node_id> nodes{Nodes};
std::set<turnout_id> turnouts;
for (auto const id : Turnouts)
if (Network.find(id) != nullptr)
turnouts.insert(id);
for (auto const id : Lines)
{
auto const *owner{Network.find(id)};
if (owner == nullptr)
continue;
check_line(Network, *owner, result);
nodes.insert(owner->start);
nodes.insert(owner->end);
if (owner->turnout != turnout_id::none && Network.find(owner->turnout) != nullptr)
turnouts.insert(owner->turnout);
}
for (auto const id : nodes)
if (auto const *at{Network.find(id)}; at != nullptr)
{
check_node(Network, *at, result);
// a turnout whose leg it is, and whose ramps it may come near
for (auto const &end : at->ends)
if (auto const *switched{owner_of(Network, end.line)})
turnouts.insert(switched->id);
}
// the lines of the turnouts touched are judged with them
std::set<line_id> lines{Lines};
for (auto const id : turnouts)
{
auto const &switched{*Network.find(id)};
lines.insert(switched.main);
lines.insert(switched.diverging);
}
if (false == result.reasons.empty())
return result;
for (auto const id : turnouts)
check_turnout(Network, *Network.find(id), result);
if (result.reasons.empty())
check_norms(Network, lines, result);
return result;
}
verdict check_all(network const &Network)
{
std::set<line_id> lines;
std::set<node_id> nodes;
std::set<turnout_id> turnouts;
for (auto const &[id, owner] : Network.lines())
lines.insert(id);
for (auto const &[id, at] : Network.nodes())
nodes.insert(id);
for (auto const &[id, each] : Network.turnouts())
turnouts.insert(id);
return check(Network, lines, nodes, turnouts);
}
} // namespace layout