/* 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 "world/SleeperLayout.h" #include "world/SleeperFeatures.h" #include "world/SleeperGeometry.h" #include "world/SleeperLines.h" #include "world/SleeperPaths.h" #include "world/SleeperRules.h" #include "world/Track.h" #include namespace track_sleepers { namespace { // whether the plain sleepers of the two paths would meet anywhere, so they lie on timbers together there bool share_anywhere( sampled_path const &Left, sampled_path const &Right ) { if( auto const margin { Left.half + Right.half + half_width + end_gap }; Left.low.x > Right.high.x + margin || Right.low.x > Left.high.x + margin || Left.low.y > Right.high.y + margin || Right.low.y > Left.high.y + margin ) { return false; } for( auto const &[ own, other ] : { std::make_pair( &Left, &Right ), std::make_pair( &Right, &Left ) } ) { auto const &path { *own }; for( std::size_t index = 0; index < path.points.size(); ++index ) { if( shares_with( path, path.points[ index ], heading( path, path.distances[ index ] ), *other ) ) { return true; } } } return false; } // the tracks whose plain sleepers would meet those of the track, put in the cluster where they're not in it yet void reach_from( TTrack const &Track, std::vector &Cluster ) { auto const around { paths_around( Track ) }; for( auto const &path : around ) { if( path.track != &Track ) { continue; } for( auto const &other : around ) { if( other.track == &Track || std::ranges::find( Cluster, other.track ) != Cluster.end() ) { continue; } if( share_anywhere( path, other ) ) { Cluster.push_back( other.track ); } } } } // whether the point of the path is beside the other path so close that their sleepers can't be laid apart bool runs_beside( sampled_path const &Path, glm::dvec3 const &Point, sampled_path const &Other ) { double gap { 0.0 }; glm::dvec2 direction { 0.0 }; return beside( Other, Point, shared_spacing( Path, Other ), gap, direction ); } // whether the two paths cross each other at one of the crossings bool cross_each_other( sampled_path const &Path, sampled_path const &Other, std::vector const &Crossings ) { return std::ranges::any_of( Crossings, [ &Path, &Other ]( feature const &Feature ) { return ( Feature.paths[ 0 ] == &Path && Feature.paths[ 1 ] == &Other ) || ( Feature.paths[ 0 ] == &Other && Feature.paths[ 1 ] == &Path ); } ); } // the stretches of the path where another path of the cluster runs so near that their plain sleepers would meet std::vector zones_of( sampled_path const &Path, std::vector const &Paths, std::vector const &Crossings ) { return stretches_where( Path, [ &Path, &Paths, &Crossings ]( glm::dvec3 const &Point, glm::dvec2 const &Direction ) { return std::ranges::any_of( Paths, [ &Path, &Crossings, &Point, &Direction ]( sampled_path const &Other ) { if( &Other == &Path ) { return false; } // tracks crossing share the timbers where their sleepers would meet, others only where they're too close to part them if( cross_each_other( Path, Other, Crossings ) ) { return shares_with( Path, Point, Direction, Other ); } double gap; glm::dvec2 otherdirection { Direction }; return beside( Other, Point, shared_spacing( Path, Other ), gap, otherdirection ) && gap < shared_spacing( Path, Other ); } ); } ); } // the stretches left of the first ones once the second ones are taken out, those shorter than the least length left out too std::vector without( std::vector const &Stretches, std::vector const &Taken, double const Least ) { std::vector result { Stretches }; for( auto const &taken : Taken ) { std::vector remaining; for( auto const &stretch : result ) { if( stretch.y <= taken.x || stretch.x >= taken.y ) { remaining.push_back( stretch ); continue; } if( stretch.x < taken.x ) { remaining.emplace_back( stretch.x, taken.x ); } if( stretch.y > taken.y ) { remaining.emplace_back( taken.y, stretch.y ); } } result = std::move( remaining ); } std::erase_if( result, [ Least ]( glm::dvec2 const &Stretch ) { return Stretch.y - Stretch.x < Least; } ); return result; } // the stretches of the line the rails take: of the path laying it, of the paths it carries and of those it keeps clear of std::vector bands_on( sampled_path const &Path, glm::dvec2 const &Forward, glm::dvec2 const &Along, std::vector const &Members, std::vector const &Others ) { std::vector bands { band_of( Path, 0.0, std::max( crossing_sine_min, std::abs( glm::dot( Forward, Along ) ) ), Forward ) }; for( auto const *listed : { &Members, &Others } ) { for( auto const &crossing : *listed ) { if( crossing.carried ) { bands.push_back( band_of( *crossing.path, crossing.across, crossing.sine, crossing.direction ) ); } else if( crossing.sine >= crossing_sine_min && angle_between( crossing.direction, Forward ) < grid_angle ) { bands.push_back( band_of( *crossing.path, crossing.across, crossing.sine, crossing.direction, false ) ); } } } return bands; } // which way each beam of the line lies, those under the rails of one track alone turned toward square to it std::vector piece_ways( std::vector const &Bands, std::vector const &Pieces, glm::dvec2 const &Along ) { // the parts of a timber parted between tracks going apart turn square to their own track, the more the further apart the // tracks run, till square where their plain sleepers wouldn't touch any more std::vector turned( Pieces.size(), Along ); if( Pieces.size() < 2 ) { return turned; } for( std::size_t index = 0; index < Pieces.size(); ++index ) { auto const *own { Pieces[ index ].single }; if( own == nullptr ) { continue; } band const *nearest { nullptr }; for( auto const &other : Bands ) { if( &other != own && other.carried && ( nearest == nullptr || std::abs( other.centre - own->centre ) < std::abs( nearest->centre - own->centre ) ) ) { nearest = &other; } } if( nearest == nullptr ) { continue; } auto const spacing { std::abs( nearest->centre - own->centre ) * std::min( own->sine, nearest->sine ) }; auto const parted { shared_spacing( *own->path, *nearest->path ) }; auto const touching { own->path->half + nearest->path->half + end_gap }; auto const share { touching > parted ? std::clamp( ( spacing - parted ) / ( touching - parted ), 0.0, 1.0 ) : 1.0 }; turned[ index ] = rotated( Along, share * turn_off( Along, own->direction ) ); } return turned; } // which way the beam of the line carrying the rails of the path alone lies, the way of the line where none does glm::dvec2 turned_for( std::vector const &Pieces, std::vector const &Turned, sampled_path const *Path, glm::dvec2 const &Along ) { for( std::size_t index = 0; index < Pieces.size(); ++index ) { if( Pieces[ index ].single != nullptr && Pieces[ index ].single->path == Path ) { return Turned[ index ]; } } return Along; } // whether the beam keeps clear of the beams laid before it about it bool clear_of( plan_grid const &Laid, beam_box const &Box ) { return Laid.all_about( Box.centre, Box.half + half_width, [ &Box ]( beam_box const &Other ) { return apart( Box, Other ); } ); } // the beam shortened at either end or both till it's clear of the beams laid before it, none where it can't be std::optional shortened_clear( beam const &Beam, double const Length, plan_grid const &Laid ) { auto const direction { ( Beam.to - Beam.from ) / Length }; auto const most { std::max( 0.0, Beam.corefrom - beam_overhang_min ) }; // off the from end auto const mostto { std::max( 0.0, Length - Beam.coreto - beam_overhang_min ) }; for( int step = 1; step <= 20; ++step ) { auto const share { step / 20.0 }; for( auto const ends : { glm::dvec2 { 0.0, mostto * share }, glm::dvec2 { most * share, 0.0 }, glm::dvec2 { most * share, mostto * share } } ) { auto shortened { Beam }; shortened.from = Beam.from + direction * ends.x; shortened.to = Beam.to - direction * ends.y; shortened.corefrom -= ends.x; shortened.coreto -= ends.x; if( clear_of( Laid, box_of( shortened ) ) ) { shortened.plain = false; return shortened; } } } return std::nullopt; } // the beams of a cluster of tracks, laid together: lines of timbers led along the paths near other paths one after another, each // line across all the rails of the cluster it reaches, and the plain sleepers of every path where no line carries it, in step // with the last timber carrying it class cluster_layout { public: cluster_layout( std::span const Tracks, std::uint64_t const Epoch ) : tracks( Tracks ), epoch( Epoch ) {} std::shared_ptr lay() { auto result { std::make_shared() }; result->epoch = epoch; for( auto const *track : tracks ) { result->beams[ track ]; for( int path = 0; path < path_count( *track ); ++path ) { paths.emplace_back( sample( *track, path ) ); } } std::erase_if( paths, []( sampled_path const &Path ) { return Path.points.size() < 2; } ); gather_context(); features = find_features( paths ); for( auto const &feature : features ) { if( false == feature.frog && false == feature.grid ) { crossings.push_back( feature ); } } zones.resize( paths.size() ); records.resize( paths.size() ); done.resize( paths.size() ); for( std::size_t index = 0; index < paths.size(); ++index ) { zones[ index ] = ( tracks.size() > 1 || paths.size() > 1 ? zones_of( paths[ index ], paths, crossings ) : std::vector {} ); } lead_lines(); // the plain sleepers of each path, between the timbers carrying it and its ends for( std::size_t index = 0; index < paths.size(); ++index ) { lay_plain( index ); } keep_apart(); fill_gaps(); keep_apart(); for( auto const &laid : beams ) { result->beams[ laid.owner ].push_back( laid.value ); } return result; } private: struct owned { TTrack const *owner; beam value; }; std::span const tracks; std::uint64_t const epoch; // of the layout the tracks are laid out for std::vector paths; // of the tracks of the cluster std::vector context; // of the tracks about them, whose rails and sleepers the beams keep clear of std::vector features; std::vector crossings; // the features where tracks cross, without the frogs std::vector> zones; // of each path, where its plain sleepers would meet those of another one // a line of timbers carrying a path: where along the path, and which way the line runs in plan struct record { double at; glm::dvec2 along; }; std::vector> records; // the lines of timbers carrying each path std::vector> done; // the stretches of each path lines were led along std::vector beams; // the paths of the tracks about the cluster, not of it, each taken once void gather_context() { for( auto const *track : tracks ) { for( auto &other : paths_around( *track ) ) { if( std::ranges::find( tracks, other.track ) != tracks.end() || std::ranges::any_of( context, [ &other ]( sampled_path const &Known ) { return Known.track == other.track && Known.path == other.path; } ) ) { continue; } context.emplace_back( std::move( other ) ); } } } // the lines of timbers, led along the path first by priority with a stretch near another path no line carries yet void lead_lines() { for( std::size_t pass = 0; pass < 1000; ++pass ) { auto best { paths.size() }; glm::dvec2 stretch { 0.0 }; for( std::size_t index = 0; index < paths.size(); ++index ) { auto const remaining { without( zones[ index ], covered( index ), 0.3 ) }; if( remaining.empty() ) { continue; } if( best == paths.size() || before( paths[ index ], paths[ best ] ) ) { best = index; stretch = *std::ranges::max_element( remaining, []( glm::dvec2 const &Left, glm::dvec2 const &Right ) { return Left.y - Left.x < Right.y - Right.x; } ); } } if( best == paths.size() ) { break; } lead( best, stretch ); done[ best ].push_back( stretch ); } } // the stretches of the path the lines laid so far carry it on, and those lines were led along std::vector covered( std::size_t const Index ) const { auto result { done[ Index ] }; auto const at { distances( Index ) }; if( at.empty() ) { return result; } auto start { at.front() }; auto last { at.front() }; for( std::size_t index = 1; index <= at.size(); ++index ) { if( index < at.size() && at[ index ] - last <= 1.6 * pitch ) { last = at[ index ]; continue; } result.emplace_back( start - 0.5 * pitch, last + 0.5 * pitch ); if( index < at.size() ) { start = at[ index ]; last = at[ index ]; } } return result; } // the pins of the lines led along the path: the rails of a path crossing this one so wide it's laid on a grid of beams; elsewhere // the lines are spread evenly std::vector pins( sampled_path const &Path ) const { std::vector result; for( auto const &feature : features ) { if( false == feature.grid ) { continue; } for( int side = 0; side < 2; ++side ) { if( feature.paths[ side ] != &Path ) { continue; } // under the rails of the other path, which runs across the beams auto const *other { feature.paths[ 1 - side ] }; auto const sine { std::max( 0.5, std::sin( 2.0 * std::asin( feature.sine ) ) ) }; result.push_back( feature.at[ side ] - 0.5 * other->gauge / sine ); result.push_back( feature.at[ side ] + 0.5 * other->gauge / sine ); } } std::ranges::sort( result ); return result; } // the line of the timbers carrying the path nearest to the distance on the side, between the distance and the reach from it std::optional record_near( std::size_t const Index, double const Distance, double const Side, double const Reach ) const { std::optional result; for( auto const &line : records[ Index ] ) { if( auto const along { ( line.at - Distance ) * Side }; along < -0.25 * pitch || along > Reach ) { continue; } if( false == result.has_value() || std::abs( line.at - Distance ) < std::abs( result->at - Distance ) ) { result = line; } } return result; } // where along the path the lines of timbers carry it, in order std::vector distances( std::size_t const Index ) const { std::vector result; for( auto const &line : records[ Index ] ) { result.push_back( line.at ); } std::ranges::sort( result ); result.erase( std::ranges::unique( result, []( double const Left, double const Right ) { return Right - Left < 0.05; } ).begin(), result.end() ); return result; } // where the path is at the distance and which way it runs in plan; false where it can't tell bool frame_at( sampled_path const &Path, double const Distance, glm::dvec3 &Point, glm::vec3 &Angles, glm::dvec2 &Forward ) const { Path.segment->RaPositionGet( Distance, Point, Angles ); glm::dvec3 behind; glm::dvec3 ahead; glm::vec3 unused; Path.segment->RaPositionGet( std::max( 0.0, Distance - 0.1 ), behind, unused ); Path.segment->RaPositionGet( std::min( Path.length, Distance + 0.1 ), ahead, unused ); Forward = plan( ahead - behind ); if( glm::length( Forward ) < 1e-6 ) { return false; } Forward = glm::normalize( Forward ); return true; } // the turns of the lines from one to the other kept within the rate of the turn before, from the seed where there's one static void limit_turns( std::vector &Turns, std::vector const &Forwards, std::size_t const From, std::size_t const To, std::optional const &Seed, double const Rate, bool const Led ) { auto const step { To > From ? 1 : -1 }; std::optional previous; auto rate { Rate }; if( Seed.has_value() ) { previous = turn_off( Forwards[ From ], Seed->along ); if( false == Led ) { // square after three sleepers rate = std::max( 0.004, std::abs( *previous ) / 3.0 ); } } for( auto index { From }; ; index += step ) { if( previous.has_value() ) { Turns[ index ] = std::clamp( Turns[ index ], *previous - rate, *previous + rate ); } previous = Turns[ index ]; if( index == To ) { break; } } } // the lines at the distances along the path, turned as the frogs and crossings about them ask, but by no more than the rate // from one line to the next, nor from the lines carrying the path at either end, so the ends of long timbers keep their spacing void lay_lines( std::size_t const Index, std::vector const &Stations, std::optional const &First, std::optional const &Last, double const Rate, bool const Led ) { auto const &path { paths[ Index ] }; std::vector distances; std::vector forwards; std::vector turns; // of the lines off square to the path, in plan for( auto const station : Stations ) { glm::dvec3 point; glm::vec3 angles; glm::dvec2 forward; if( false == frame_at( path, station, point, angles, forward ) ) { continue; } distances.push_back( station ); forwards.push_back( forward ); // the plain sleepers lie square to their track, turned only to meet the timbers next to them auto along { Led ? beam_direction( point, forward, features ) : forward }; if( Led ) { // the timbers about a crossing lie square to the mean way of the tracks they carry, the frogs about it don't turn them: // square to the main tracks of a scissors crossing, to the bisector of a diamond if( auto const mean { whole_at( path, point, forward, along ) }; mean.has_value() ) { along = *mean; } } turns.push_back( turn_off( forward, along ) ); } if( distances.empty() ) { return; } limit_turns( turns, forwards, 0, distances.size() - 1, First, Rate, Led ); limit_turns( turns, forwards, distances.size() - 1, 0, Last, Rate, Led ); for( std::size_t index = 0; index < distances.size(); ++index ) { lay_line( Index, distances[ index ], Led, rotated( forwards[ index ], turns[ index ] ) ); } } // the lines of timbers led along the stretch of the path, spread between the lines carrying it at either end, or the ends of the // stretch, and the pins on it void lead( std::size_t const Index, glm::dvec2 const &Stretch ) { auto const &path { paths[ Index ] }; auto const back { record_near( Index, Stretch.x, -1.0, 2.0 * pitch ) }; auto const fore { record_near( Index, Stretch.y, 1.0, 2.0 * pitch ) }; // the free ends of the stretch, taken to the ends of the path close to them auto const freestart { Stretch.x <= 0.01 ? 0.0 : Stretch.x }; auto const freeend { Stretch.y >= path.length - 0.01 ? path.length : Stretch.y }; anchor const start { back.has_value() ? anchor { back->at, 1.0 } : anchor { freestart, 0.5 } }; anchor const end { fore.has_value() ? anchor { fore->at, 1.0 } : anchor { freeend, 0.5 } }; if( end.at - start.at <= 0.0 ) { return; } std::vector anchors { start }; for( auto const pin : pins( path ) ) { if( pin > start.at + 0.5 * pitch_min && pin < end.at - 0.5 * pitch_min && pin - anchors.back().at >= pitch_min ) { anchors.push_back( { pin, 0.0 } ); } } anchors.push_back( end ); std::vector stations; for( std::size_t index = 1; index < anchors.size(); ++index ) { spread( anchors[ index - 1 ], anchors[ index ], stations ); } lay_lines( Index, stations, back, fore, timber_turn, true ); } // where the timber led along the path through the point, lying the given way, carries both tracks of a crossing: the way it lies // then, square to the middle between the ways of the tracks it carries turned furthest either way std::optional whole_at( sampled_path const &Path, glm::dvec3 const &Point, glm::dvec2 const &Forward, glm::dvec2 const &Along ) const { if( crossings.empty() ) { return std::nullopt; } std::vector members; find_crossings( Path, Point, Along, glm::dvec2 { -Along.y, Along.x }, 0.5 * length_cap + Path.reach, paths, members ); carry( Path, Forward, Along, members, false, false ); if( false == whole_with( Path, members ) ) { return std::nullopt; } // the outermost tracks it carries, either way along it: the main tracks of a scissors crossing, the crossing ones of a diamond std::pair low { 0.0, Forward }; std::pair high { 0.0, Forward }; for( auto const &crossing : members ) { if( false == crossing.carried ) { continue; } if( crossing.across < low.first ) { low = { crossing.across, crossing.direction }; } if( crossing.across > high.first ) { high = { crossing.across, crossing.direction }; } } return rotated( Forward, 0.5 * ( turn_off( Forward, low.second ) + turn_off( Forward, high.second ) ) ); } bool whole_with( sampled_path const &Path, std::vector const &Members ) const { auto const carries = [ &Path, &Members ]( sampled_path const *Carried ) { return Carried == &Path || std::ranges::any_of( Members, [ Carried ]( crossing const &Crossing ) { return Crossing.carried && Crossing.path == Carried; } ); }; return std::ranges::any_of( crossings, [ &carries ]( feature const &Feature ) { return carries( Feature.paths[ 0 ] ) && carries( Feature.paths[ 1 ] ); } ); } // the rails of the paths of the cluster crossing it the timber led along the path takes, marked among them void take_rails( sampled_path const &Path, glm::dvec2 const &Forward, glm::dvec2 const &Along, std::vector &Members ) const { // about a crossing the timbers take all the tracks whose sleepers would meet, elsewhere only those too close to part carry( Path, Forward, Along, Members, false ); if( false == whole_with( Path, Members ) ) { for( auto &crossing : Members ) { crossing.carried = false; } carry( Path, Forward, Along, Members, true ); } } // the plain sleepers of the path on the stretches no line of timbers carries it, in step with the timbers at their ends void lay_plain( std::size_t const Index ) { auto const &path { paths[ Index ] }; auto const at { distances( Index ) }; std::vector> stretches; if( at.empty() ) { stretches.push_back( { { 0.0, 0.5 }, { path.length, 0.5 } } ); } else { stretches.push_back( { { 0.0, 0.5 }, { at.front(), 1.0 } } ); for( std::size_t index = 1; index < at.size(); ++index ) { if( at[ index ] - at[ index - 1 ] > 1.6 * pitch ) { stretches.push_back( { { at[ index - 1 ], 1.0 }, { at[ index ], 1.0 } } ); } } stretches.push_back( { { at.back(), 1.0 }, { path.length, 0.5 } } ); } for( auto const &[ from, to ] : stretches ) { if( auto const span { to.at - from.at }; span <= 0.0 || span < ( from.offset + to.offset - 1.0 ) * pitch + 0.5 * pitch_min ) { continue; } std::vector stations; spread( from, to, stations ); // turned back square to the path from the timbers at the ends of the stretch auto const first { from.offset == 1.0 ? record_near( Index, from.at, 1.0, 0.05 ) : std::optional {} }; auto const last { to.offset == 1.0 ? record_near( Index, to.at, -1.0, 0.05 ) : std::optional {} }; lay_lines( Index, stations, first, last, sleeper_turn, false ); } } // the beams on the line through the point of the path at the distance: the timber of the line led along it, taking the rails of // the paths of the cluster it reaches, or the plain sleeper of the path alone void lay_line( std::size_t const Index, double const Distance, bool const Led, glm::dvec2 const &Along ) { auto const &path { paths[ Index ] }; glm::dvec3 point; glm::vec3 angles; glm::dvec2 forward; if( false == frame_at( path, Distance, point, angles, forward ) ) { return; } auto const along { Along }; glm::dvec2 const across { -along.y, along.x }; auto const reach { 0.5 * length_cap + path.reach }; std::vector members; std::vector others; find_crossings( path, point, along, across, reach, paths, members ); find_crossings( path, point, along, across, reach, context, others ); if( Led ) { take_rails( path, forward, along, members ); } auto bands { bands_on( path, forward, along, members, others ) }; // a timber carrying both tracks of a crossing lies whole under all the tracks it carries auto const whole { Led && whole_with( path, members ) }; std::vector pieces; emit_line( bands, pieces, whole ); cut_long( bands, pieces ); auto const turned { piece_ways( bands, pieces, along ) }; if( Led ) { records[ Index ].emplace_back( Distance, turned_for( pieces, turned, &path, along ) ); for( auto const &crossing : members ) { if( crossing.carried ) { records[ crossing.path - paths.data() ].emplace_back( crossing.at, turned_for( pieces, turned, crossing.path, along ) ); } } } // the beam lies tilted with the roll of the path, as the ballast is glm::dvec3 const flatacross { across.x, 0.0, across.y }; glm::dvec3 const up { 0.0, 1.0, 0.0 }; auto const roll { static_cast( angles.x ) }; auto const rolledacross { flatacross * std::cos( roll ) - up * std::sin( roll ) }; auto const rolledup { flatacross * std::sin( roll ) + up * std::cos( roll ) }; auto const top { point - rolledup * static_cast( path.track->rail_height() ) }; // a sleeper of a plain track running alone and square to it, the rest are the timbers of the switches and crossings auto const carried { std::ranges::count_if( bands, []( band const &Band ) { return Band.carried; } ) }; auto const plain { path.track->eType == tt_Normal && carried == 1 && pieces.size() == 1 && false == pieces.front().clipped && std::abs( glm::dot( forward, along ) ) > 0.9998 }; for( std::size_t index = 0; index < pieces.size(); ++index ) { auto const &piece { pieces[ index ] }; auto const &way { turned[ index ] }; if( way == along ) { beams.push_back( { path.track, { top + rolledacross * piece.from, top + rolledacross * piece.to, glm::dvec3 { along.x, 0.0, along.y }, plain, piece.corefrom - piece.from, piece.coreto - piece.from } } ); continue; } // turned about where the middle of its track crosses the line auto const centre { top + rolledacross * piece.single->centre }; glm::dvec3 const turnedacross { glm::dvec3 { -way.y, 0.0, way.x } * std::cos( roll ) - up * std::sin( roll ) }; beams.push_back( { path.track, { centre + turnedacross * ( piece.from - piece.single->centre ), centre + turnedacross * ( piece.to - piece.single->centre ), glm::dvec3 { way.x, 0.0, way.y }, false, piece.corefrom - piece.from, piece.coreto - piece.from } } ); } } // which way along the track the beam laid under the rail at the point runs, of those in the grid; none where there's none std::optional beam_way_at( plan_grid const &Grid, glm::dvec2 const &Rail ) const { auto const *cell { Grid.at( Rail ) }; if( cell == nullptr ) { return std::nullopt; } for( auto const laid : *cell ) { if( auto const box { box_of( beams[ laid ].value ) }; 2.0 * box.half >= 0.1 && holds( box, Rail ) ) { return glm::dvec2 { -box.axis.y, box.axis.x }; } } return std::nullopt; } // the middles of the gaps in the beams under the rail of the path on the side, and the way the beams at their ends run void find_gaps( sampled_path const &Path, double const Side, int const Steps, plan_grid const &Grid, std::vector &Gaps, std::vector &Turns ) const { std::optional> last; for( int step = 0; step <= Steps; ++step ) { auto const distance { Path.length * step / Steps }; glm::dvec3 point; glm::dvec3 across; Path.segment->plan_frame( distance, point, across ); auto const rail { plan( point + across * ( Side * 0.5 * Path.gauge ) ) }; auto const found { beam_way_at( Grid, rail ) }; if( false == found.has_value() ) { continue; } if( last.has_value() && distance - last->first > 0.06 && distance - last->first + 2.0 * half_width > 1.25 * pitch_max ) { // the middle of the stretch without one, and the way the beams at its ends run Gaps.push_back( 0.5 * ( distance + last->first ) ); auto along { last->second + ( glm::dot( last->second, *found ) < 0.0 ? -*found : *found ) }; Turns.push_back( glm::length( along ) > 1e-9 ? glm::normalize( along ) : heading( Path, Gaps.back() ) ); } last = std::make_pair( distance, *found ); } } // a sleeper of the path put in where the rails of it go further without a beam under them than the pitch lets them, turned // as the beams about it are: where beams were left out or lines of timbers led along different paths meet void fill_gaps() { auto const count { beams.size() }; // the beams by the cells of a grid they reach into plan_grid grid { 2.0 }; for( std::size_t laid = 0; laid < count; ++laid ) { auto const &beam { beams[ laid ].value }; grid.insert( plan( 0.5 * ( beam.from + beam.to ) ), 0.5 * glm::length( plan( beam.to - beam.from ) ) + half_width, laid ); } for( std::size_t index = 0; index < paths.size(); ++index ) { auto const &path { paths[ index ] }; // where along the path beams lie under each of its rails, and which way they run auto const steps { std::max( 2, static_cast( std::ceil( path.length / 0.05 ) ) ) }; std::vector gaps; std::vector turns; for( auto const side : { -1.0, 1.0 } ) { find_gaps( path, side, steps, grid, gaps, turns ); } // a sleeper in each, those of both rails about one place put in once for( std::size_t gap = 0; gap < gaps.size(); ++gap ) { if( std::any_of( gaps.begin(), gaps.begin() + gap, [&]( double const Other ) { return std::abs( Other - gaps[ gap ] ) < 0.3; } ) ) { continue; } lay_line( index, gaps[ gap ], false, turns[ gap ] ); } } } // a beam over one laid before it is left out: where tracks cross too wide for one timber to take both, or the lines of two // stretches meet at an angle void keep_apart() { plan_grid grid { 3.0 }; std::vector kept; for( auto laid : beams ) { auto const length { glm::length( laid.value.to - laid.value.from ) }; if( length < 0.1 ) { continue; } if( false == clear_of( grid, box_of( laid.value ) ) ) { // shortened from either end or both, down to the least overhang past the rails it carries, till it's clear auto const shortened { shortened_clear( laid.value, length, grid ) }; if( false == shortened.has_value() ) { ++faults().dropped; continue; } laid.value = *shortened; ++faults().clipped; } auto const box { box_of( laid.value ) }; grid.insert( box.centre, box.half + half_width, box ); kept.push_back( laid ); } beams = std::move( kept ); } }; } // namespace // the tracks whose timbers are laid together with those of the track: all those reached from it through paths whose plain // sleepers would meet, in the order of their names std::vector cluster_of( TTrack const &Track ) { std::vector result { &Track }; std::size_t index { 0 }; while( index < result.size() && result.size() < cluster_cap ) { reach_from( *result[ index ], result ); ++index; } std::ranges::sort( result, []( TTrack const *Left, TTrack const *Right ) { return ( Left->name() != Right->name() ? Left->name() < Right->name() : Left < Right ); } ); return result; } std::shared_ptr lay_out( std::span const Tracks, std::uint64_t const Epoch ) { return cluster_layout( Tracks, Epoch ).lay(); } // how far the paths of the switch stand off one plane where they share the timbers of its panel, and where along the diverging // path; the switch itself is out of shape for its timbers where it's more than the tolerance std::pair panel_off_plane( TTrack const &Track ) { std::pair result { 0.0, 0.0 }; if( Track.eType != tt_Switch ) { return result; } auto const main { sample( Track, 0 ) }; auto const diverging { sample( Track, 1 ) }; if( main.points.size() < 2 || diverging.points.size() < 2 ) { return result; } auto alongside { false }; for( std::size_t index = 0; index < diverging.points.size(); ++index ) { if( false == runs_beside( diverging, diverging.points[ index ], main ) ) { if( alongside ) { // apart far enough to lay their own break; } // at the start, which both paths share continue; } alongside = true; auto const off { off_plane( diverging, diverging.points[ index ], main ) }; if( off > result.first ) { result = { off, diverging.distances[ index ] }; } } return result; } } // namespace track_sleepers