From 508f600b167475050959d258613fd1fcb88eef7d Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:29:48 +0100 Subject: [PATCH] fix(editor): a point clicked before the terrain under it came takes its height once it's there, Enter waits for it The terrain is made around the cursor from the first point of a track laid, in the background since the photo begins a scenery with no question: the first point clicked in an empty place had no ground under it, its height a guess (the flat ground, the ground nearby), and the points after it came on the terrain made meanwhile. The line was laid from the guessed height to the real one: a grade of thousands per mille at the second point, the track hidden in the ground there (the shots over Warszawa Zachodnia). - click_heights (editorClickHeights): where the height of each point came from, the ground, none yet, level with the point before, or a track's end; the points of none take the height of the ground once it's there under them, the level ones go along, the rest are kept. Enter waits while the terrain is on its way under such a point, 10 s at most. - The lay tool, in any shape: lay_bed tells where the height of the cursor came from, each point clicked keeps it. Every frame the points of a guessed height read the ground again (from above, as the cursor does: the terrain may lie far over the guess), the line shown and its checks follow them. A point laid isn't touched. - Enter, or a free end reached, with the terrain still coming under such a point (its chunk waiting, or not asked for yet within the download area): the panel says "Waiting for the terrain under the points...", the line is laid as it comes; Esc gives the wait up keeping the points, Enter again lays without waiting. Tests: U: the points re-read once the ground is there, the others kept, the level ones along, Enter's wait. E: tr_6_terrain_late (nmtsettings hold holds the terrain of the NMT back, the case makes the terrain itself: the wait, Esc, laid once the terrain came; a first point clicked before the terrain, no jump in the grade at the second); expect laywait, layguessed, laid steepest. --- CMakeLists.txt | 1 + application/editormode.h | 21 +++- application/editormodelay.cpp | 125 +++++++++++++++++++++- application/editormodenmt.cpp | 5 + application/editormodeselftest.cpp | 5 +- application/editormodeselftestexpect.cpp | 37 ++++++- application/editormodeselftestjoin.cpp | 6 +- application/editormodeselftestswitch.cpp | 3 +- application/editormodeselftestterrain.cpp | 6 +- application/editormodetrackbed.cpp | 16 ++- editor/editorClickHeights.cpp | 78 ++++++++++++++ editor/editorClickHeights.hpp | 50 +++++++++ lang/editor_pl.po | 11 ++ tests/editor/README.md | 2 + tests/editor/cases/tr_6_terrain_late.txt | 102 ++++++++++++++++++ tests/unit/CMakeLists.txt | 3 +- tests/unit/test_click_heights.cpp | 110 +++++++++++++++++++ 17 files changed, 565 insertions(+), 16 deletions(-) create mode 100644 editor/editorClickHeights.cpp create mode 100644 editor/editorClickHeights.hpp create mode 100644 tests/editor/cases/tr_6_terrain_late.txt create mode 100644 tests/unit/test_click_heights.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e96993533..1c56f01ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,6 +199,7 @@ set(SOURCES "editor/editorRoad.cpp" "editor/editorAlignment.cpp" "editor/editorTrace.cpp" +"editor/editorClickHeights.cpp" "editor/editorTrackShape.cpp" "editor/editorGroundFollow.cpp" "editor/editorAlignmentStore.cpp" diff --git a/application/editormode.h b/application/editormode.h index b68c0a696..784b0ac13 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -38,6 +38,7 @@ http://mozilla.org/MPL/2.0/. #include "editor/editorConnections.hpp" #include "editor/editorAlignment.hpp" #include "editor/editorTrace.hpp" +#include "editor/editorClickHeights.hpp" #include "editor/editorAlignmentStore.hpp" #include "editor/editorSwitchStore.hpp" #include "editor/editorSwitchCurve.hpp" @@ -433,6 +434,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri double slope{1.5}; double reach{60.0}; std::vector pending; // chunks to make once their model is in + // the self-test holds the terrain back: the chunks waiting aren't made, the model isn't read for the ground, till it lets go + bool held{false}; // of the chunks waiting, those there already with flat ground, made without the model: the model fills their ground std::set refill; std::set given_up; // chunks the growing gave up, next to terrain not from the model: not asked for again @@ -1929,6 +1932,13 @@ class editor_mode : public application_mode, private editor_track::observer, pri std::vector points; std::vector modes; // of the points, as each was clicked std::vector sigmas; // m, the noise of each click: two pixels of the view at it + // where the height of each point came from: one clicked before the terrain under it came takes it again once it's there + std::vector heights; + click_heights::source mouse_height{click_heights::source::ground}; // of the cursor, the next point + // Enter waits for the terrain still coming under the points of a guessed height: since when (below 0: it doesn't), the free + // end the line is to be laid to + double terrain_wait{-1.0}; + editor_track::snap_target wait_end; double sigma_given{0.0}; // m, the noise of the clicks of a self-test, where above 0 lay_mode mode{lay_mode::straights}; // the shape the user chose with Tab or in the panel; until then it follows the photo: traced where it's on, the bends where not @@ -2075,6 +2085,15 @@ class editor_mode : public application_mode, private editor_track::observer, pri void lay_told(double const Length, std::size_t const Tracks); void lay_click(); void lay_finish(editor_track::snap_target const &End); + // the line laid once the terrain under its points of a guessed height is there, or at once if none is coming; Enter again doesn't wait + void lay_finish_on_ground(editor_track::snap_target const &End); + // the points clicked before the terrain under them came take its height, once a frame; the terrain still coming under one of them + void lay_settle_heights(); + bool lay_terrain_coming() const; + bool lay_terrain_coming_at(std::size_t Index) const; + bool lay_waiting() const { return m_lay.terrain_wait >= 0.0; } + // the wait over: the terrain came, none is coming any more, or it took too long; Now: Enter again, laid without waiting + void lay_wait_end(bool Now = false); void lay_cancel(); void lay_enter(); // Tab: the bends of straights or the line traced on the photo, for the points clicked so far and from now on @@ -2680,7 +2699,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri // the grade lines of the tracks shown while they're laid are fitted on a thread of their own, not to hold up the frames mutable ground_follow::fitter m_fitter; mutable bool m_follow_preview{false}; - glm::dvec3 lay_bed(glm::dvec3 const &Point); + glm::dvec3 lay_bed(glm::dvec3 const &Point, click_heights::source *Source = nullptr); std::optional m_cursor_override; bool sweep_grabs(glm::dvec3 const &Point); // menu of the path a click of the right mouse button lands on diff --git a/application/editormodelay.cpp b/application/editormodelay.cpp index b9df74079..37fc5b37d 100644 --- a/application/editormodelay.cpp +++ b/application/editormodelay.cpp @@ -207,11 +207,18 @@ void editor_mode::update_build_tools() } auto const snapping{lap()}; if (pointing && lay.mouse_snap.track == nullptr) - lay.mouse = lay_bed(lay.mouse); + lay.mouse = lay_bed(lay.mouse, &lay.mouse_height); + else if (lay.mouse_snap.track != nullptr) + lay.mouse_height = click_heights::source::fixed; auto const bedding{lap()}; if (false == m_extend.active) joining(lay.points.empty() ? editor_track::snap_target{} : lay.mouse_snap); lay_trim(); + // the terrain came under the points clicked before it: they take its height, and the line shown with them; Enter waiting for it + // lays the line once it's there + lay_settle_heights(); + if (lay_waiting()) + lay_wait_end(); lay.plan = {}; if (lay.points.empty()) { @@ -2225,6 +2232,7 @@ void editor_mode::lay_trim() auto &tool{m_lay}; tool.modes.resize(tool.points.size(), lay_mode::straights); tool.sigmas.resize(tool.points.size(), 0.0); + tool.heights.resize(tool.points.size(), click_heights::source::fixed); } void editor_mode::lay_switch_mode() @@ -2258,6 +2266,9 @@ void editor_mode::lay_shape_default() void editor_mode::lay_click() { auto &tool{m_lay}; + // the line waits for the terrain under its points to be laid: Esc or Enter first + if (lay_waiting()) + return; tool.status.clear(); tool.laid_now = false; lay_trim(); @@ -2267,7 +2278,7 @@ void editor_mode::lay_click() // reaching a free end finishes the track there, in line with the path it joins tool.points.push_back(snap.position); tool.modes.push_back(tool.mode); - lay_finish(snap); + lay_finish_on_ground(snap); return; } if (tool.points.empty()) @@ -2286,6 +2297,7 @@ void editor_mode::lay_click() tool.points.push_back(point); tool.modes.push_back(tool.mode); tool.sigmas.push_back(click_sigma_at(point)); + tool.heights.push_back(snap.track != nullptr ? click_heights::source::fixed : tool.mouse_height); // the photo is traced from above: the view turns to it at the first point if (lay_traced() && false == Global.EditorOrtho) toggle_ortho(); @@ -2299,6 +2311,7 @@ void editor_mode::lay_click() tool.points.push_back(point); tool.modes.push_back(tool.mode); tool.sigmas.push_back(click_sigma_at(point)); + tool.heights.push_back(tool.mouse_height); tool.continuing = false; if (lay_traced() && tool.points.size() >= 3) first_step_done(first_step::trace_points); @@ -2509,6 +2522,7 @@ void editor_mode::lay_finish(editor_track::snap_target const &End) tool.points.clear(); tool.modes.clear(); tool.sigmas.clear(); + tool.heights.clear(); tool.start = {}; tool.laid_step = m_history.size(); straight_refresh(); @@ -2530,6 +2544,7 @@ void editor_mode::lay_finish(editor_track::snap_target const &End) tool.points = {free.position}; tool.modes = {lay_mode::traced}; tool.sigmas = {sigma}; + tool.heights = {click_heights::source::fixed}; tool.continuing = true; } } @@ -2549,6 +2564,12 @@ void editor_mode::lay_enter() { auto &tool{m_lay}; ++tool.enters; + // Enter again while the line waits for the terrain under its points: laid at once, at the heights there are + if (lay_waiting()) + { + lay_wait_end(true); + return; + } if (false == m_typed.empty()) { lay_click(); @@ -2562,7 +2583,7 @@ void editor_mode::lay_enter() { tool.points.push_back(end.position); tool.modes.push_back(tool.mode); - lay_finish(end); + lay_finish_on_ground(end); return; } // a single point is no track: the end of the line laid last it goes on from is left by Enter, one clicked is told @@ -2580,7 +2601,77 @@ void editor_mode::lay_enter() } // the laying ends with the track laid: the next click starts anew where it's made, a click on the free end left goes on from // it. Going on by itself, a click made elsewhere afterwards led a loop out of that end to it - lay_finish({}); + lay_finish_on_ground({}); +} + +void editor_mode::lay_finish_on_ground(editor_track::snap_target const &End) +{ + auto &tool{m_lay}; + lay_settle_heights(); + // a point clicked before the terrain under it came, the terrain still on its way: laid once it's there, not at the height guessed + if (lay_terrain_coming()) + { + tool.terrain_wait = ImGui::GetTime(); + tool.wait_end = End; + tool.status.clear(); + WriteLog(format("Editor: the laying waits for the terrain under %zu point(s) clicked before it came", click_heights::guessed_count(tool.heights)), logtype::generic); + return; + } + lay_finish(End); +} + +void editor_mode::lay_wait_end(bool const Now) +{ + auto &tool{m_lay}; + if (false == lay_waiting()) + return; + auto const waited{ImGui::GetTime() - tool.terrain_wait}; + if (false == Now && click_heights::wait_on(tool.heights, [&](std::size_t const Index) { return lay_terrain_coming_at(Index); }, waited)) + return; + auto const end{tool.wait_end}; + tool.terrain_wait = -1.0; + tool.wait_end = {}; + WriteLog(format("Editor: the laying waited %.1f s for the terrain under the points%s, %zu left at a guessed height", waited, Now ? ", Enter again" : "", click_heights::guessed_count(tool.heights)), logtype::generic); + lay_finish(end); +} + +void editor_mode::lay_settle_heights() +{ + auto &tool{m_lay}; + lay_trim(); + if (false == click_heights::guessed(tool.heights)) + return; + TTrack const *style{tool.start.track != nullptr ? tool.start.track : tool.copy_selected ? selected_track() : nullptr}; + auto const &points{tool.points}; + // read from above, as the cursor reads the ground: the terrain come may lie far over the height guessed + auto const above{static_cast(Global.pCamera.Pos.y)}; + auto const changed{click_heights::settle(tool.points, tool.heights, [&](std::size_t const Index) { + auto const &point{points[Index]}; + auto const direction{Index > 0 ? plan_of(point - points[Index - 1]) : Index + 1 < points.size() ? plan_of(points[Index + 1] - point) : glm::dvec2{0.0}}; + return bed_heights({glm::dvec3{point.x, std::max(point.y, above), point.z}}, {direction}, style).front(); + })}; + if (changed > 0) + WriteLog(format("Editor: the terrain came under %zu point(s) clicked before it, their heights taken from it", changed), logtype::generic); +} + +bool editor_mode::lay_terrain_coming_at(std::size_t const Index) const +{ + auto const &tool{m_lay}; + auto const &state{m_nmt_state}; + if (Index >= tool.points.size() || false == state.grow || false == m_nmt.placed()) + return false; + auto const &point{tool.points[Index]}; + auto const chunk{heightmap::chunk_at(point.x, point.z)}; + if (std::find(state.pending.begin(), state.pending.end(), chunk) != state.pending.end()) + return true; + // not asked for yet: the terrain grows around the cursor every half a second, within the area the terrain is downloaded for + auto const at{plan_of(point)}; + return false == m_streamer.exists(chunk) && false == state.given_up.contains(chunk) && in_download_area(at - glm::dvec2{1.0}, at + glm::dvec2{1.0}); +} + +bool editor_mode::lay_terrain_coming() const +{ + return click_heights::waiting(m_lay.heights, [&](std::size_t const Index) { return lay_terrain_coming_at(Index); }); } void editor_mode::lay_cancel() @@ -2589,6 +2680,9 @@ void editor_mode::lay_cancel() tool.points.clear(); tool.modes.clear(); tool.sigmas.clear(); + tool.heights.clear(); + tool.terrain_wait = -1.0; + tool.wait_end = {}; tool.continuing = false; tool.start = {}; tool.preview.clear(); @@ -2608,6 +2702,20 @@ void editor_mode::lay_escape() { auto &tool{m_lay}; lay_trim(); + // the wait for the terrain under the points given up: nothing laid, the points stay, the free end reached left + if (lay_waiting()) + { + if (tool.wait_end.track != nullptr && false == tool.points.empty()) + { + tool.points.pop_back(); + lay_trim(); + } + tool.terrain_wait = -1.0; + tool.wait_end = {}; + tool.status = STR_C("Not laid: the points stay, Enter lays them"); + tool.status_time = ImGui::GetTime(); + return; + } // the points clicked are no track yet and nothing brings them back: a slip of Esc only warns, the second one drops them auto const pending{lay_pending()}; if (tool.points.size() < 2 || (tool.continuing && tool.points.size() < 2) || ImGui::GetTime() - tool.escape_time < 3.0) @@ -2640,17 +2748,21 @@ void editor_mode::lay_back() { auto &tool{m_lay}; lay_trim(); + tool.terrain_wait = -1.0; + tool.wait_end = {}; // the points not laid yet, the last one first; the tracks laid go back with Ctrl+Z if (tool.points.size() > 1) { tool.points.pop_back(); tool.modes.pop_back(); tool.sigmas.pop_back(); + tool.heights.pop_back(); return; } tool.points.clear(); tool.modes.clear(); tool.sigmas.clear(); + tool.heights.clear(); tool.continuing = false; tool.start = {}; } @@ -3016,6 +3128,11 @@ void editor_mode::render_lay_ui() void editor_mode::render_lay_step(lay_state const State) { auto const traced{m_lay.mode == lay_mode::traced}; + if (lay_waiting()) + { + editor_flow::step_line(STR_C("Waiting for the terrain under the points..."), STR_C("Esc cancels · Enter lays without waiting")); + return; + } switch (State) { case lay_state::extending: editor_flow::step_line(STR_C("Drag to lay from the end; let go to lay it")); break; diff --git a/application/editormodenmt.cpp b/application/editormodenmt.cpp index ac2136a3d..e4ad8face 100644 --- a/application/editormodenmt.cpp +++ b/application/editormodenmt.cpp @@ -805,6 +805,8 @@ bool editor_mode::nmt_ahead(double const X, double const Z, double &Height) cons bool editor_mode::nmt_ground(double const X, double const Z, double &Height) const { + if (m_nmt_state.held) + return false; if (nmt_ahead(X, Z, Height)) return true; if (false == m_nmt.placed() || false == m_nmt.height(X, Z, Height)) @@ -1182,6 +1184,9 @@ void editor_mode::nmt_update() camera_above_ground(); return; } + // the self-test holds the terrain back: the chunks wait as they are + if (state.held) + return; state.camera_check = true; // the terrain of the scenery opened, or begun, for the chunks grown under the track laid as well: they don't need terrain there before if (false == ensure_terrain()) diff --git a/application/editormodeselftest.cpp b/application/editormodeselftest.cpp index 0dfbcdae9..a5a50645a 100644 --- a/application/editormodeselftest.cpp +++ b/application/editormodeselftest.cpp @@ -132,8 +132,9 @@ editor_mode::selftest_result editor_mode::selftest_dispatch(std::string const &c // orthohires 0|1 the 4K tiles on or off; orthostats logs the tiles, the area and the requests made // orthosettings [on|radius|opacity|lift|height|year|hires|drape|scene ]... the settings of the orthophoto changed as its // panel and its checkbox change them, kept with the editor, and logged with the origin of the scenery -// nmtsettings [band|around|shape|common|follow|grow|offset|fitted ]... the settings of the terrain model changed as the terrain -// panel changes them, and logged with the offset of the model to the scenery +// nmtsettings [band|around|shape|common|follow|grow|offset|fitted|hold ]... the settings of the terrain model changed as the +// terrain panel changes them, and logged with the offset of the model to the scenery; hold 1: the +// terrain held back as if still on its way, the chunks waiting not made, the model not read // join [radius] [variant] [transitions 0|1] the free ends at the points joined, as a drag from one to the other // layto from the free end at the first point towards the second, as a drag in the layout // layclicks ... clicks of the lay tool at the points, a free end within 3 m joined diff --git a/application/editormodeselftestexpect.cpp b/application/editormodeselftestexpect.cpp index e92bfc0cf..b96d856d9 100644 --- a/application/editormodeselftestexpect.cpp +++ b/application/editormodeselftestexpect.cpp @@ -564,6 +564,24 @@ void expect_laid(std::string const &What, std::istringstream &Words, std::vector got += (got.empty() ? "" : ", ") + trace::radii_text(curve); expectation(line.curves.size() == count, What, format("%zu (%s)", line.curves.size(), got.c_str()), std::to_string(count)); } + else if (which == "steepest") + { + // the steepest grade of the tracks laid, from end to end of each: a point at a height guessed leaves a jump in it + double most{0.0}; + Words >> most; + if (malformed(What, Words)) + return; + double steepest{0.0}; + for (auto const *track : Tracks) + for (auto const &path : track->m_paths) + { + auto const &from{path.points[segment_data::point::start]}; + auto const &to{path.points[segment_data::point::end]}; + if (auto const run{std::hypot(to.x - from.x, to.z - from.z)}; run > 0.5) + steepest = std::max(steepest, std::abs(to.y - from.y) / run * 1000.0); + } + expectation(steepest <= most, What, format("%.1f per mille", steepest), format("at most %.1f", most)); + } else if (which == "clicks") { std::size_t most{0}; @@ -721,6 +739,23 @@ editor_mode::selftest_result editor_mode::selftest_expect(std::string const &com if (false == malformed(what, words)) expectation(m_lay.continuing == (wanted != 0), what, m_lay.continuing ? "1" : "0", wanted != 0 ? "1" : "0"); } + else if (kind == "laywait") + { + // the line waits for the terrain under its points to be laid + int wanted{0}; + words >> wanted; + if (false == malformed(what, words)) + expectation(lay_waiting() == (wanted != 0), what, lay_waiting() ? "1" : "0", wanted != 0 ? "1" : "0"); + } + else if (kind == "layguessed") + { + // the points clicked whose height is a guess, no ground under them yet + std::size_t wanted{0}; + words >> wanted; + auto const guessed{click_heights::guessed_count(m_lay.heights)}; + if (false == malformed(what, words)) + expectation(guessed == wanted, what, format("%zu of %zu", guessed, m_lay.points.size()), std::to_string(wanted)); + } else { shown_state shown{m_switch.status, ui()->refusal_shown(), ui()->status_line(), m_lay.mouse, @@ -738,7 +773,7 @@ editor_mode::selftest_result editor_mode::selftest_expect(std::string const &com if (false == expect_shown(kind, what, words, shown)) expectation(false, what, "no expectation " + kind, "cant, joint, records, speed, gauge, track, route, norms, findings, height, lift, includes, status, statuslacks, refusal, statusline, laycursor, terrain, tool, window, " - "selected, laypanel, switchpanel, steps, laid, laykeys or laycontinue"); + "selected, laypanel, switchpanel, steps, laid, laykeys, laycontinue, laywait or layguessed"); } return selftest_result::handled; } diff --git a/application/editormodeselftestjoin.cpp b/application/editormodeselftestjoin.cpp index 25fe2160f..6de5b9c1e 100644 --- a/application/editormodeselftestjoin.cpp +++ b/application/editormodeselftestjoin.cpp @@ -436,7 +436,8 @@ editor_mode::selftest_result editor_mode::selftest_join(std::string const &comma auto const ground{ground_heights({Point}, true, &found)}; Point.y = found[0] != 0 ? ground[0] : m_lay.points.empty() ? 0.0 : m_lay.points.back().y; m_lay.mouse_snap = editor_track::find_free_end(nullptr, 1, Point, 3.0, {}); - m_lay.mouse = m_lay.mouse_snap.track != nullptr ? Point : lay_bed(Point); + m_lay.mouse_height = click_heights::source::fixed; + m_lay.mouse = m_lay.mouse_snap.track != nullptr ? Point : lay_bed(Point, &m_lay.mouse_height); lay_click(); }; // the line the points clicked make now, as Enter would lay it: its vertices and curves, the warnings, the fits of the curves traced @@ -1242,7 +1243,8 @@ editor_mode::selftest_result editor_mode::selftest_join(std::string const &comma auto const ground{ground_heights({point}, true, &found)}; point.y = found[0] != 0 ? ground[0] : m_lay.points.empty() ? 0.0 : m_lay.points.back().y; m_lay.mouse_snap = editor_track::find_free_end(nullptr, 1, point, 3.0, {}); - m_lay.mouse = m_lay.mouse_snap.track != nullptr ? point : lay_bed(point); + m_lay.mouse_height = click_heights::source::fixed; + m_lay.mouse = m_lay.mouse_snap.track != nullptr ? point : lay_bed(point, &m_lay.mouse_height); WriteLog(format("SELFTEST layclicks ground %.3f, track %.3f", point.y, m_lay.mouse.y)); if (false == m_lay.points.empty()) { diff --git a/application/editormodeselftestswitch.cpp b/application/editormodeselftestswitch.cpp index 0b1ea7f0c..f3fdb71c2 100644 --- a/application/editormodeselftestswitch.cpp +++ b/application/editormodeselftestswitch.cpp @@ -200,7 +200,8 @@ editor_mode::selftest_result editor_mode::selftest_switch(std::string const &com auto const ground{ground_heights({point}, true, &found)}; point.y = found[0] != 0 ? ground[0] : m_lay.points.empty() ? 0.0 : m_lay.points.back().y; m_lay.mouse_snap = editor_track::find_free_end(nullptr, 1, point, 3.0, {}); - m_lay.mouse = m_lay.mouse_snap.track != nullptr ? point : lay_bed(point); + m_lay.mouse_height = click_heights::source::fixed; + m_lay.mouse = m_lay.mouse_snap.track != nullptr ? point : lay_bed(point, &m_lay.mouse_height); lay_click(); } if (false == points.empty()) diff --git a/application/editormodeselftestterrain.cpp b/application/editormodeselftestterrain.cpp index 0adfa9b5d..34a7d4c3e 100644 --- a/application/editormodeselftestterrain.cpp +++ b/application/editormodeselftestterrain.cpp @@ -194,9 +194,11 @@ editor_mode::selftest_result editor_mode::selftest_terrain(std::string const &co } else if (key == "area") m_area_terrain.run_only = value != 0.0; // for the run only: the setting isn't touched, so no save keeps it + else if (key == "hold") + state.held = value != 0.0; // the terrain held back: the chunks waiting aren't made, the model isn't read for the ground } - WriteLog(format("SELFTEST nmtsettings band %.0f around %d shape %d follow %d grow %d offset %+.2f fitted %d", state.band, m_terrain_add_count, state.shape ? 1 : 0, state.follow ? 1 : 0, state.grow ? 1 : 0, state.offset, - state.fitted ? 1 : 0)); + WriteLog(format("SELFTEST nmtsettings band %.0f around %d shape %d follow %d grow %d offset %+.2f fitted %d hold %d", state.band, m_terrain_add_count, state.shape ? 1 : 0, state.follow ? 1 : 0, state.grow ? 1 : 0, state.offset, + state.fitted ? 1 : 0, state.held ? 1 : 0)); return selftest_result::handled; } if (command == "wmsdescribe") diff --git a/application/editormodetrackbed.cpp b/application/editormodetrackbed.cpp index 374d118c5..383cfe857 100644 --- a/application/editormodetrackbed.cpp +++ b/application/editormodetrackbed.cpp @@ -161,12 +161,24 @@ ground_follow::grading editor_mode::laying_grading(TTrack const *Style) const return grading; } -glm::dvec3 editor_mode::lay_bed(glm::dvec3 const &Point) +glm::dvec3 editor_mode::lay_bed(glm::dvec3 const &Point, click_heights::source *Source) { auto const &tool{m_lay}; // level: on at the height the laying goes on from, the first point on the ground if (EditorSettings.heights() == editorSettings::lay_heights::level && false == tool.points.empty()) + { + if (Source != nullptr) + *Source = click_heights::source::level; return {Point.x, tool.points.back().y, Point.z}; + } TTrack const *style{tool.start.track != nullptr ? tool.start.track : tool.copy_selected ? selected_track() : nullptr}; - return {Point.x, bed_height(Point, tool.points.empty() ? glm::dvec2{0.0} : plan_of(Point - tool.points.back()), style), Point.z}; + auto const direction{tool.points.empty() ? glm::dvec2{0.0} : plan_of(Point - tool.points.back())}; + // no ground there yet, nor the model of it: the height a guess, taken again once the terrain comes + auto const height{bed_heights({Point}, {direction}, style).front()}; + if (Source != nullptr) + *Source = std::isnan(height) ? click_heights::source::none : click_heights::source::ground; + if (false == std::isnan(height)) + return {Point.x, height, Point.z}; + auto const nearby{nearby_ground(Point)}; + return {Point.x, std::isnan(nearby) ? Point.y : nearby + trackbed(style).first, Point.z}; } diff --git a/editor/editorClickHeights.cpp b/editor/editorClickHeights.cpp new file mode 100644 index 000000000..11ee9b035 --- /dev/null +++ b/editor/editorClickHeights.cpp @@ -0,0 +1,78 @@ +/* +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/editorClickHeights.hpp" + +#include +#include + +namespace click_heights +{ + +std::size_t settle(std::vector &Points, std::vector &Sources, std::function const &Ground) +{ + std::size_t changed{0}; + auto const count{std::min(Points.size(), Sources.size())}; + for (std::size_t i = 0; i < count; ++i) + { + auto &point{Points[i]}; + switch (Sources[i]) + { + case source::none: + { + auto const height{Ground(i)}; + if (false == std::isfinite(height)) + break; + if (point.y != height) + { + point.y = height; + ++changed; + } + Sources[i] = source::ground; + break; + } + case source::level: + if (i > 0 && point.y != Points[i - 1].y) + { + point.y = Points[i - 1].y; + ++changed; + } + break; + default: + break; + } + } + return changed; +} + +bool guessed(std::vector const &Sources) +{ + return std::find(Sources.begin(), Sources.end(), source::none) != Sources.end(); +} + +std::size_t guessed_count(std::vector const &Sources) +{ + return static_cast(std::count(Sources.begin(), Sources.end(), source::none)); +} + +bool waiting(std::vector const &Sources, std::function const &Coming) +{ + for (std::size_t i = 0; i < Sources.size(); ++i) + if (Sources[i] == source::none && Coming(i)) + return true; + return false; +} + +bool wait_on(std::vector const &Sources, std::function const &Coming, double const Waited) +{ + return Waited < wait_longest && waiting(Sources, Coming); +} + +} // namespace click_heights diff --git a/editor/editorClickHeights.hpp b/editor/editorClickHeights.hpp new file mode 100644 index 000000000..433b946f1 --- /dev/null +++ b/editor/editorClickHeights.hpp @@ -0,0 +1,50 @@ +/* +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 +#include + +// The heights of the points clicked while a track is laid, and where each came from. The terrain is made around the cursor as the track +// is laid: the first point clicked in an empty place has no ground under it yet, its height a guess, and the terrain comes a moment +// later. Such a point takes its height again once the ground under it is there, before the line is laid, and the line shown with it; +// Enter waits a while for the terrain still on its way. A point on a track, or laid already, isn't touched. +namespace click_heights +{ + +enum class source : char +{ + fixed, // a free end of a track, the end of the line laid last: kept as it is + ground, // the ground under it: the terrain, the model of it, the ground of the scenery + none, // no ground there yet when it was clicked: the height a guess, taken again once the ground is there + level, // the height of the point before it, the heights laid level: it goes along with that one +}; + +// The points of a guessed height over the ground now take its height: Ground of the index of a point, NaN where there's still none +// under it. The level ones after a point changed go along with it; the ground and the fixed ones are kept. Points beyond the sources +// given are kept. The count of the points whose height changed +std::size_t settle(std::vector &Points, std::vector &Sources, std::function const &Ground); + +// any point of a guessed height left +bool guessed(std::vector const &Sources); +std::size_t guessed_count(std::vector const &Sources); + +// a point of a guessed height the terrain under which is on its way (Coming of the index): the line waits for it +bool waiting(std::vector const &Sources, std::function const &Coming); + +// s, how long Enter waits at most for the terrain under the points: then the line is laid at the heights there are +double constexpr wait_longest{10.0}; + +// the line waits on: the terrain is still coming under a point, and the wait began less than wait_longest ago +bool wait_on(std::vector const &Sources, std::function const &Coming, double Waited); + +} // namespace click_heights diff --git a/lang/editor_pl.po b/lang/editor_pl.po index d3fb86f22..d59a1c4c8 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -15514,3 +15514,14 @@ msgstr "Enter, LPM... przy układaniu po zdjęciu" msgid "The line laid goes on from its end with the next clicks; Esc ends it" msgstr "Kolejne kliknięcia ciągną ułożoną linię od jej końca; Esc ją kończy" + +# scenery editor: the points clicked before the terrain came under them + +msgid "Waiting for the terrain under the points..." +msgstr "Czekam na teren pod punktami…" + +msgid "Esc cancels · Enter lays without waiting" +msgstr "Esc anuluje · Enter układa bez czekania" + +msgid "Not laid: the points stay, Enter lays them" +msgstr "Nie ułożono: punkty zostają, Enter je układa" diff --git a/tests/editor/README.md b/tests/editor/README.md index f00a61c89..75d1945bb 100644 --- a/tests/editor/README.md +++ b/tests/editor/README.md @@ -129,6 +129,7 @@ tool's `parallel ` lay turnouts, crossovers and lines of the mod | `expect laid radius [/...] `, `expect laid speed `, `expect laid nearly <0\|1>` | a curve of it: the radii of its arcs, the speed it's laid for, nearly straight (R 5 km and more) | | `expect laid slowtracks `, `expect laid joined <0\|1>` | its tracks given the speed of a curve slowed down; its first track joined at its start to the one it goes on from | | `expect laykeys `, `expect laycontinue <0\|1>` | the shapes switched with Tab and the lines laid with Enter so far; the lay tool goes on from the end of the line laid | +| `expect laid steepest `, `expect laywait <0\|1>`, `expect layguessed ` | the steepest grade of the tracks of the line laid last; the line waits for the terrain under its points to be laid; the points clicked whose height is a guess, no ground under them yet | Files `scenery/autotest_editor..scn` are copied next to the copy of the fixture as `autotest_editor__.scn`, the name of the fixture in them made the name of its copy: scenarios of its map, for the scenario index (`autotest_editor.uses.scn`, a trainset on `s1`). @@ -227,6 +228,7 @@ as on the photo): | `tr_3_compound` | a compound curve of R 600 and 400 m clicked along it: one curve of two radii, each within 3 % | | `tr_4_below_norms` | a curve of R 300 m on a line of 120 km/h laid slowed down to 70 km/h, its tracks given it; one of R 150 m, below the least radius of a main track, not laid | | `tr_5_continue` | after Enter the next clicks carry the line on from its end, joined to it; Esc ends it | +| `tr_6_terrain_late` | the points clicked before the terrain under them came (the NMT held back with `nmtsettings hold 1`, the terrain made by the case with `terrainadd`, `terrainlevel`: no network, nothing waited for by the time): Enter waits for it with a line in the panel, Esc gives the wait up keeping the points, the line laid by itself once the terrain came; a first point clicked before it takes its height, no jump in the grade at the second point | and the assumptions of the project and the norms enforced when laying (`editor-project-setup.md`): diff --git a/tests/editor/cases/tr_6_terrain_late.txt b/tests/editor/cases/tr_6_terrain_late.txt new file mode 100644 index 000000000..aa4fe7ea8 --- /dev/null +++ b/tests/editor/cases/tr_6_terrain_late.txt @@ -0,0 +1,102 @@ +# Laying from the photo where the terrain isn't there yet: it's made around the cursor from the first point clicked, a moment later. The +# terrain of the NMT held back all along (nmtsettings hold 1: the chunks waiting not made, the model not read), the points clicked have +# their heights guessed. Enter waits for the terrain under them with a line in the panel, Esc gives the wait up keeping the points, +# Enter again waits; the terrain made under the points then (terrainadd, terrainlevel), the line is laid by itself. Then the first +# point clicked before the terrain, the next ones on it once it came: the first takes the height of the terrain too, no jump in the +# grade at the second point. The terrain the case makes, not the NMT's: no network, and nothing waited for by the time, the frames of +# the editor as fast as they come. Over Warszawa Zachodnia (the origin of the photo of the shots, the scenery placed so that the +# terrain grows while laying), the station 1 km north of the place of the wait so the terrain of the one doesn't reach the other +wait 5 +nmtsettings grow 1 +wait 2 +tap f3 +wait 5 +orthophoto 485535 634211 4 1 0 +ortho 330 whole +wait 5 +tap l +wait 10 +laymode traced +laysigma 0.3 +# Enter waits for the terrain held back under the two points clicked before it +nmtsettings hold 1 +camera 295 300 -700 295 0 -699.5 +wait 10 +mouseat 290 0 -790 +wait 3 +button left press +wait 1 +button left release +wait 3 +mouseat 292 0 -630 +wait 3 +button left press +wait 1 +button left release +wait 5 +expect layguessed 2 +expect laypanel clicking folded +layenter +wait 3 +expect laywait 1 +expect laykeys 0 1 +# Esc gives the wait up: nothing laid, the points stay +tap esc +wait 3 +expect laywait 0 +expect layguessed 2 +expect laypanel clicking folded +layenter +wait 3 +expect laywait 1 +# the terrain comes under the points, made at 8 m: they take its height, the line is laid by itself +terrainadd 3 2 +terrainlevel 291 -710 600 8 +terrainlevel 291 -710 600 8 +terrainlevel 291 -710 600 8 +wait 10 +expect laywait 0 +expect laypanel laid shown +expect laid clicks 2 +tap esc +wait 5 +# at the station: the first point clicked before the terrain came, guessed; the terrain made under it then, the point takes its height. +# The NMT held back meanwhile: only the terrain made here comes +nmtsettings hold 1 +camera 295 300 300 295 0 300.5 +wait 10 +mouseat 290 0 200 +wait 3 +button left press +wait 1 +button left release +wait 5 +expect layguessed 1 +terrainadd 3 2 +terrainlevel 293 290 600 12 +terrainlevel 293 290 600 12 +terrainlevel 293 290 600 12 +wait 10 +expect layguessed 0 +# the next points on the terrain: laid at once, its grade with no jump at the second point +mouseat 292 0 290 +wait 3 +button left press +wait 1 +button left release +wait 3 +mouseat 296 0 380 +wait 3 +button left press +wait 1 +button left release +wait 5 +expect layguessed 0 +layenter +wait 5 +expect laywait 0 +expect laid clicks 3 +expect laid steepest 60 +nmtsettings hold 0 +wait 2 +quit diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index b27a02738..e7a95dad2 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -26,8 +26,9 @@ add_executable(${UNIT_TESTS_NAME} "${CMAKE_SOURCE_DIR}/editor/editorAlignment.cpp" # and the line traced on the photo, its straights and curves recognised from the points clicked "${CMAKE_SOURCE_DIR}/editor/editorTrace.cpp" - # and the heights of the tracks laid + # and the heights of the tracks laid, and of the points clicked laying them "${CMAKE_SOURCE_DIR}/editor/editorGroundFollow.cpp" + "${CMAKE_SOURCE_DIR}/editor/editorClickHeights.cpp" "${CMAKE_SOURCE_DIR}/editor/editorProfile.cpp" # and the switches laid into the curves, and the catalogue of the switches the turnouts of the domain are laid from "${CMAKE_SOURCE_DIR}/editor/editorSwitchCurve.cpp" diff --git a/tests/unit/test_click_heights.cpp b/tests/unit/test_click_heights.cpp new file mode 100644 index 000000000..955bc7fe4 --- /dev/null +++ b/tests/unit/test_click_heights.cpp @@ -0,0 +1,110 @@ +#include "stdafx.h" +#include "doctest.h" +#include "editor/editorClickHeights.hpp" + +#include +#include + +// The heights of the points clicked while laying: a point clicked where there was no ground yet takes its height again once the terrain +// under it comes, the points level with it go along, the rest stay; Enter waits for the terrain on its way, a while at most +namespace { + +using click_heights::source; + +double constexpr nothing{std::numeric_limits::quiet_NaN()}; + +} // namespace + +TEST_CASE("a point clicked before the terrain came takes the height of the ground once it's there") +{ + // the first point clicked on the empty place: its height a guess (0); the next ones on the terrain made meanwhile, at 94 m + std::vector points{{0.0, 0.0, 0.0}, {0.0, 94.2, 30.0}, {0.0, 94.6, 130.0}}; + std::vector sources{source::none, source::ground, source::ground}; + std::vector ground{nothing, 95.0, 96.0}; + auto const under = [&](std::size_t const Index) { return ground[Index]; }; + // no ground under it yet: it waits as it is + CHECK(click_heights::settle(points, sources, under) == 0); + CHECK(points[0].y == 0.0); + CHECK(sources[0] == source::none); + CHECK(click_heights::guessed(sources)); + CHECK(click_heights::guessed_count(sources) == 1); + // the terrain came: the point takes its height, the others clicked on the ground keep theirs though the ground is read otherwise now + ground[0] = 94.0; + CHECK(click_heights::settle(points, sources, under) == 1); + CHECK(points[0].y == doctest::Approx(94.0)); + CHECK(points[1].y == doctest::Approx(94.2)); + CHECK(points[2].y == doctest::Approx(94.6)); + CHECK(sources[0] == source::ground); + CHECK_FALSE(click_heights::guessed(sources)); + // taken once: the ground changing under it later, the terrain shaped by the track, doesn't move it again + ground[0] = 90.0; + CHECK(click_heights::settle(points, sources, under) == 0); + CHECK(points[0].y == doctest::Approx(94.0)); +} + +TEST_CASE("the points on a track and those of the ground are never read again") +{ + std::vector points{{0.0, 3.5, 0.0}, {0.0, 4.0, 100.0}, {0.0, 0.0, 200.0}}; + std::vector sources{source::fixed, source::ground, source::none}; + std::vector asked; + auto const under = [&](std::size_t const Index) { + asked.push_back(Index); + return 7.0; + }; + CHECK(click_heights::settle(points, sources, under) == 1); + // only the point of a guessed height asked about the ground + REQUIRE(asked.size() == 1); + CHECK(asked[0] == 2); + CHECK(points[0].y == 3.5); + CHECK(points[1].y == 4.0); + CHECK(points[2].y == 7.0); +} + +TEST_CASE("the points laid level go along with the point of a guessed height they're level with") +{ + // the heights laid level: the first point on the empty place, the next ones at its height + std::vector points{{0.0, 0.0, 0.0}, {0.0, 0.0, 100.0}, {0.0, 0.0, 200.0}}; + std::vector sources{source::none, source::level, source::level}; + auto const under = [](std::size_t const Index) { return Index == 0 ? 112.5 : 140.0; }; + CHECK(click_heights::settle(points, sources, under) == 3); + for (auto const &point : points) + CHECK(point.y == doctest::Approx(112.5)); + // level after a fixed one: stays with it + std::vector joined{{0.0, 5.0, 0.0}, {0.0, 5.0, 100.0}}; + std::vector held{source::fixed, source::level}; + CHECK(click_heights::settle(joined, held, under) == 0); + CHECK(joined[1].y == 5.0); +} + +TEST_CASE("points beyond the sources known are kept") +{ + // the points a self-test put in by itself have no sources: nothing is read for them + std::vector points{{0.0, 0.0, 0.0}, {0.0, 0.0, 100.0}}; + std::vector sources{source::none}; + auto const under = [](std::size_t) { return 50.0; }; + CHECK(click_heights::settle(points, sources, under) == 1); + CHECK(points[0].y == 50.0); + CHECK(points[1].y == 0.0); +} + +TEST_CASE("Enter waits for the terrain on its way under a point of a guessed height, a while at most") +{ + std::vector sources{source::none, source::ground, source::ground}; + auto const coming = [](std::size_t) { return true; }; + auto const gone = [](std::size_t) { return false; }; + // the terrain under the guessed point on its way: waited for + CHECK(click_heights::waiting(sources, coming)); + CHECK(click_heights::wait_on(sources, coming, 0.0)); + CHECK(click_heights::wait_on(sources, coming, click_heights::wait_longest - 0.1)); + // for long enough: laid at the heights there are + CHECK_FALSE(click_heights::wait_on(sources, coming, click_heights::wait_longest)); + // none coming there, no data in the model, the terrain off: laid at once + CHECK_FALSE(click_heights::waiting(sources, gone)); + CHECK_FALSE(click_heights::wait_on(sources, gone, 0.0)); + // only the guessed points wait: the terrain still coming under the ones on the ground doesn't hold the line + auto const under_second = [](std::size_t const Index) { return Index == 1; }; + CHECK_FALSE(click_heights::waiting(sources, under_second)); + // every point on the ground: nothing to wait for + std::vector settled{source::ground, source::level, source::fixed}; + CHECK_FALSE(click_heights::waiting(settled, coming)); +}