diff --git a/CMakeLists.txt b/CMakeLists.txt index 33b764052..151b43f62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -242,6 +242,7 @@ set(SOURCES "editor/editorInfra.cpp" "editor/editorTerrainStreamer.cpp" "editor/editorOrthophoto.cpp" +"editor/editorPhotoRules.cpp" "editor/editorHttp.cpp" "editor/editorNmt.cpp" "editor/editorGeoMap.cpp" diff --git a/application/editorgestures.h b/application/editorgestures.h index 59c73aa63..c2d3710f6 100644 --- a/application/editorgestures.h +++ b/application/editorgestures.h @@ -99,7 +99,7 @@ struct gesture_word // "Ctrl+wheel", "Shift+click x4", "Drag a point", "Q W E": the keys and the parts of the mouse it names, the rest as text inline std::vector gesture_words(std::string const &Keys) { - static char const *const keys[]{"Ctrl", "Shift", "Alt", "Esc", "Del", "Enter", "Backspace", "Tab", "Space", "Home", "End", "PrtScr"}; + static char const *const keys[]{"Ctrl", "Shift", "Alt", "Esc", "Del", "Enter", "Backspace", "Tab", "Space", "Home", "End", "PrtScr", "~"}; auto const mouse = [](std::string const &Word) -> std::string { if (Word == "LMB" || Word == "click" || Word == "Click" || Word == "Press") return "LMB"; diff --git a/application/editormode.cpp b/application/editormode.cpp index f8f368761..516dbb276 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -31,6 +31,7 @@ extern char **environ; #include "application/application.h" #include "editor/editorKilometrage.hpp" #include "editor/editorSettings.hpp" +#include "editor/editorPhotoRules.hpp" #include "editor/editorModelSets.hpp" #include "editor/editorTrackPresets.hpp" #include "editor/editorIncludeInfo.hpp" @@ -67,6 +68,7 @@ extern char **environ; #include "utilities/Logs.h" #include #include +#include #include #include #include @@ -632,6 +634,7 @@ editor_mode::editor_mode() { road_select(nullptr); ground_sources(m_orthophoto); ui()->set_base_layers([this]() { render_base_layers(); }); + ui()->set_status_extra([this]() { render_orthophoto_status(); }); editor_track::observe(this); } @@ -643,10 +646,8 @@ void editor_mode::ground_sources(editor_orthophoto &Layer) gather_ground_triangles(Min, Max, true, 50.0f, Out, true, Heightmap); }); Layer.height_source([](double const X, double const Z, double &Height) { return EditorTerrain.height_at(X, Z, Height); }); - // with the heightmap terrain, a new tile waits while a chunk under it is still to come; where none is nor will be, or the walk of - // the chunks doesn't reach, it lies on the other ground or on the NMT guess as before. A scenery without it as before - Layer.coming_source([](glm::dvec2 const &Min, glm::dvec2 const &Max) { return EditorTerrain.active() && EditorTerrain.ground_coming(Min, Max); }); - // the imagery often comes before the terrain made from the model: it lies where the terrain will be + // a tile doesn't wait for the chunks of the heightmap terrain still to come under it: it lies flat meanwhile, and goes onto them as + // they come. The imagery often comes before the terrain made from the model: it lies where the terrain will be Layer.guess_source([this](double const X, double const Z, double &Height) { if (nmt_ground(X, Z, Height)) return true; @@ -1646,6 +1647,8 @@ bool editor_mode::update() m_ortho_resident = resident; m_ortho_waiting = waiting; steps.step("terrain"); + orthophoto_zoom(); + area_grow(); m_orthophoto.update(Camera.Pos); steps.step("orthophoto"); update_wms_layers(); @@ -1657,6 +1660,7 @@ bool editor_mode::update() auto *background{ImGui::GetBackgroundDrawList(ImGui::GetMainViewport())}; background->PushClipRect(ImVec2(editor_ui::view_min().x, editor_ui::view_min().y), ImVec2(editor_ui::view_max().x, editor_ui::view_max().y), true); draw_orthophoto(); + draw_orthophoto_failures(); draw_nmt_pending(); area_update(); draw_download_area(); @@ -1792,7 +1796,6 @@ bool editor_mode::update() render_open_scenery_popup(); render_shortcuts_window(); render_first_steps(); - render_nmt_prompt(); render_nmt_banner(); render_nmt_buried(); render_empty_scenery_hint(); @@ -1934,6 +1937,15 @@ void editor_mode::render_terrain_loading() ImGui::SameLine(); ImGui::TextDisabled("%s", STR_C("(within the download area, there is none)")); } + // no question asked: the terrain of a new scenery comes as its first track is laid, unless this is off + bool offer{EditorSettings.terrain_offer()}; + if (ImGui::Checkbox(STR_C("The terrain loaded by itself as the first track of a new scenery is laid"), &offer)) + { + EditorSettings.terrain_offer(offer); + EditorSettings.save(); + } + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("In the background, where the scenery lies on the map; the panel of the laying tells it, with the button to go without it")); } namespace @@ -2571,32 +2583,47 @@ void editor_mode::load_orthophoto_settings() config.drape = stored.drape; config.lift = stored.lift; config.in_scene = stored.in_scene; - config.north = 0.0; - config.east = 0.0; m_georeference_read = false; read_georeference(); - if (false == std::as_const(EditorSettings).orthophoto_origin(scenery, config.north, config.east) && m_georeference) - { - config.north = m_georeference_origin.x; - config.east = m_georeference_origin.y; - } - auto enabled{m_orthophoto.enabled()}; + std::optional remembered; + if (double north{0.0}, east{0.0}; std::as_const(EditorSettings).orthophoto_origin(scenery, north, east)) + remembered = glm::dvec2{north, east}; + // the scenery's own line: whether the photo is on, the wizard writes it on, and the origin the editor wrote along + std::optional scenery_on; + std::optional marked_origin; + bool marked_aligned{false}; m_orthophoto_mark.clear(); - if (auto const marked{scene::Layers.empty() ? std::vector{} : scene::Layers.marked(scene::layer_handle{1}, "//$o")}; false == marked.empty() && editor_orthophoto::read_mark(marked.front(), config, enabled)) + if (auto const marked{scene::Layers.empty() ? std::vector{} : scene::Layers.marked(scene::layer_handle{1}, "//$o")}; false == marked.empty()) { - m_orthophoto_mark = marked.front(); - m_orthophoto.enabled(enabled); - m_orthophoto_chosen = true; - WriteLog(format("Editor: the map settings of the scenery, orthophoto %s", enabled ? "on" : "off")); + editor_orthophoto::config mark; + bool on{false}; + if (editor_orthophoto::read_mark(marked.front(), mark, on)) + { + m_orthophoto_mark = marked.front(); + scenery_on = on; + if (mark.north != 0.0 || mark.east != 0.0) + marked_origin = glm::dvec2{mark.north, mark.east}; + marked_aligned = mark.aligned; + } } - // the user's choice stands over the scenery's; turned on only where the scenery lies on the map, a fictional one has nothing to - // show, and there it's left to the terrain model once the scenery is placed - if (auto const on{stored.on}; on.has_value() && (false == *on || config.north != 0.0 || config.east != 0.0)) + // //$g of the scenery file first: an origin the editor remembers for a scenery of the same name made elsewhere doesn't move it. One + // aligned by hand in the editor, //$o says so, goes before it + auto const origin{photo_rules::choose_origin(m_georeference ? std::optional{m_georeference_origin} : std::nullopt, marked_origin, remembered, marked_aligned)}; + config.north = origin.origin.x; + config.east = origin.origin.y; + config.aligned = marked_aligned && origin.from == photo_rules::origin_source::mark; + m_georeference_differs = origin.differs > photo_rules::origin_tolerance ? origin.differs : 0.0; + if (m_georeference_differs > 0.0) { - m_orthophoto.enabled(*on); - m_orthophoto_chosen = true; - WriteLog(format("Editor: the orthophoto %s, as the user left it", *on ? "on" : "off")); + WriteLog(format("Editor: the orthophoto laid out by //$g of the scenery file, the origin kept before was %.1f m off it", m_georeference_differs)); + ui()->set_status(format(STR_C("Photo: laid out by //$g of the scenery file; the origin kept before was %.0f m off it"), m_georeference_differs)); } + // on or off as the user left it in this scenery, else as the scenery keeps it; nothing said, off, and the terrain model turns + // it on where the scenery lies on the map + auto const switched{photo_rules::photo_on(std::as_const(EditorSettings).orthophoto_on(scenery), scenery_on)}; + m_orthophoto_chosen = switched.chosen; + m_orthophoto.enabled(switched.on); + WriteLog(format("Editor: the orthophoto %s%s", switched.on ? "on" : "off", false == switched.chosen ? ", nothing chosen in the scenery" : "")); m_orthophoto.settings(config); m_orthophoto_origin_edit = {config.north, config.east}; // the area the imagery and the terrain model are downloaded in, none in a scenery which has none @@ -2627,8 +2654,12 @@ void editor_mode::choose_orthophoto(bool const On) { m_orthophoto.enabled(On); m_orthophoto_chosen = true; - EditorSettings.orthophoto().on = On; - EditorSettings.save(); + // the scenery's: the photo of another one, the wizard's turned on, isn't turned off by it + if (false == m_orthophoto_scenery.empty()) + { + EditorSettings.orthophoto_on(m_orthophoto_scenery, On); + EditorSettings.save(); + } } void editor_mode::keep_nmt_settings() @@ -2689,6 +2720,8 @@ void editor_mode::render_orthophoto_ui() { config.north = m_orthophoto_origin_edit.x; config.east = m_orthophoto_origin_edit.y; + // aligned by hand: kept so in //$o, which //$g of the scenery file doesn't override when the scenery is opened again + config.aligned = true; changed = true; persist = true; } @@ -2698,13 +2731,16 @@ void editor_mode::render_orthophoto_ui() ImGui::TextDisabled(STR_C("in the scenery file: Y (easting) %.0f m, X (northing) %.0f m"), m_georeference_origin.y, m_georeference_origin.x); if (config.north != m_georeference_origin.x || config.east != m_georeference_origin.y) { - ImGui::SameLine(); + // //$g goes first when the scenery is opened, unless the origin was aligned here by hand, which //$o keeps + if (false == config.aligned) + ImGui::TextColored(ImVec4(1.0f, 0.75f, 0.2f, 1.0f), "%s", STR_C("Opened again, the scenery is laid out by //$g of its file")); if (ImGui::SmallButton(STR_C("Use it"))) { m_orthophoto_origin_edit.x = m_georeference_origin.x; config.north = m_orthophoto_origin_edit.x; m_orthophoto_origin_edit.y = m_georeference_origin.y; config.east = m_orthophoto_origin_edit.y; + config.aligned = false; changed = true; persist = true; } @@ -2860,6 +2896,8 @@ void editor_mode::render_orthophoto_ui() changed = true; persist = true; } + if (false == config.hires) + ImGui::TextDisabled("%s", STR_C("With the view zoomed in to 15 cm a pixel they come by themselves next to the camera")); if (ImGui::IsItemHovered()) ImGui::SetTooltip(STR_C("4096 px tiles (~6 cm/px) from the high-resolution orthophoto, for the tiles around the camera.\n" "Areas it doesn't cover use the standard imagery. Each 4K tile takes ~85 MB of video memory.")); @@ -2903,6 +2941,188 @@ void editor_mode::draw_orthophoto() (*layer)->draw(projection, Camera.Pos, io.DisplaySize.x, io.DisplaySize.y); } +double editor_mode::view_metres_per_pixel() const +{ + auto const &io{ImGui::GetIO()}; + if (io.DisplaySize.y <= 0.0f) + return 0.0; + if (Global.EditorOrtho) + return 2.0 * static_cast(Global.EditorOrthoExtent) / io.DisplaySize.y; + // in the 3d view the ground under the camera, as far below it as the terrain or the photo there is + double ground{static_cast(m_orthophoto.settings().height)}; + if (double height; EditorTerrain.height_at(Camera.Pos.x, Camera.Pos.z, height)) + ground = height; + auto const above{Camera.Pos.y - ground}; + if (above <= 0.0) + return 0.0; + return above * 2.0 * std::tan(glm::radians(Global.FieldOfView / Global.ZoomFactor) * 0.5) / io.DisplaySize.y; +} + +void editor_mode::orthophoto_zoom() +{ + // the 4K photos of the tiles around the camera by themselves once the view shows the ground at 15 cm a pixel or finer; asked of the + // service as the setting for them asks, the tile under the camera alone (the setting for them takes the 3 x 3 around it), so the zoom + // doesn't take some 0.75 GB of the memory of the graphics card by itself + auto const wanted{m_orthophoto.enabled() && photo_rules::hires_wanted(view_metres_per_pixel(), m_orthophoto.zoom_hires())}; + if (wanted != m_orthophoto.zoom_hires()) + { + m_orthophoto.zoom_hires(wanted); + WriteLog(format("Editor: the 4K photos around the camera %s, the view at %.3f m a pixel", wanted ? "on" : "off", view_metres_per_pixel())); + } +} + +void editor_mode::area_grow() +{ + // the area is carried along where the photo is looked at or the tracks are laid, now and then: the tiles added go to the disk + // cache in the background, a few at a time, as the rest of the area + auto const now{ImGui::GetTime()}; + if (now - m_area_grow_time < 5.0 || m_area_drawing.active || m_orthophoto.area().empty() || (false == m_orthophoto.enabled() && false == m_lay.active)) + return; + // every 5 s, whatever comes of it: set only by a growth, an area which didn't grow, or at its limit, was rastered and flood filled + // every frame + m_area_grow_time = now; + auto const &config{m_orthophoto.settings()}; + if (config.north == 0.0 && config.east == 0.0) + return; + std::vector points{{Camera.Pos.x, Camera.Pos.z}}; + if (m_lay.active) + { + points.emplace_back(m_lay.mouse.x, m_lay.mouse.z); + if (false == m_lay.points.empty()) + points.emplace_back(m_lay.points.back().x, m_lay.points.back().z); + } + std::size_t constexpr limit{1600}; // tiles of 256 m: about 105 sq km + for (auto const &point : points) + { + auto const grown{photo_rules::grow_area(m_orthophoto.area(), m_orthophoto.to_puwg(point), editor_orthophoto::tile_size, 1, 2, limit)}; + if (grown.limited) + { + if (false == m_area_grow_limited) + { + m_area_grow_limited = true; + m_area_grow_time = now; + ui()->set_status(format(STR_C("Photo: the download area is at its limit of %zu tiles; draw it again where it's needed, in Map > Orthophoto settings..."), limit)); + } + return; + } + if (grown.polygon.empty()) + continue; + m_area_grow_time = now; + m_orthophoto.area(grown.polygon); + WriteLog(format("Editor: the download area grown by %zu tiles at %.0f %.0f, %zu in all", grown.added, point.x, point.y, grown.total)); + ui()->set_status(format(STR_P("Photo: the download area grown by %zu tile at its edge, %zu in all", "Photo: the download area grown by %zu tiles at its edge, %zu in all", grown.added), grown.added, grown.total)); + return; + } +} + +namespace +{ +// the failed tiles drawn, the first of them; those after aren't outlined +std::size_t constexpr orthophoto_failures_drawn = 400; +} // namespace + +void editor_mode::ground_orthophoto_failures(std::vector> const &Failed) const +{ + // the tiles no longer failing forgotten + std::set> failing; + for (auto const &tile : Failed) + failing.emplace(tile.first.x, tile.first.y); + std::erase_if(m_orthophoto_failure_ground, [&failing](auto const &Entry) { return false == failing.contains(Entry.first); }); + + // two tiles asked for a frame: those with a corner not found yet each second, those found all round each 30 s, as the ground + // under them may have come or changed since + double const now{ImGui::GetTime()}; + int asked{0}; + auto const count{std::min(Failed.size(), orthophoto_failures_drawn)}; + for (std::size_t index = 0; index < count && asked < 2; ++index) + { + auto const &[low, high]{Failed[index]}; + auto &entry{m_orthophoto_failure_ground[{low.x, low.y}]}; + auto const complete{std::ranges::all_of(entry.found, [](bool const Found) { return Found; })}; + if (now - entry.asked < (complete ? 30.0 : 1.0)) + continue; + entry.asked = now; + ++asked; + std::vector const above{{low.x, 10000.0, low.y}, {high.x, 10000.0, low.y}, {high.x, 10000.0, high.y}, {low.x, 10000.0, high.y}}; + std::vector found; + auto const ground{ground_heights(above, false, &found)}; + for (std::size_t corner = 0; corner < entry.heights.size(); ++corner) + { + if (found[corner] == 0) + continue; + entry.heights[corner] = ground[corner]; + entry.found[corner] = true; + } + } +} + +void editor_mode::draw_orthophoto_failures() const +{ + if (false == m_orthophoto.enabled()) + { + m_orthophoto_failure_ground.clear(); + return; + } + auto const failed{m_orthophoto.failed()}; + if (failed.empty()) + { + m_orthophoto_failure_ground.clear(); + return; + } + // the tiles which failed outlined in red, with a cross over them: there's no photo there yet, it's tried again by itself + screen_projection const projection; + auto *drawlist{ImGui::GetBackgroundDrawList(ImGui::GetMainViewport())}; + auto const height{static_cast(m_orthophoto.settings().height)}; + // in the 3D view on the ground, where the photo draped over it lies: at the height of the layer they stood off the tiles they mark + // where the terrain lies well above or below it. Seen from above the height doesn't matter + auto const grounded{m_orthophoto.settings().drape && false == Global.EditorOrtho}; + auto const colour{IM_COL32(240, 70, 60, 210)}; + if (grounded) + ground_orthophoto_failures(failed); + std::size_t drawn{0}; + for (auto const &[low, high] : failed) + { + if (++drawn > orthophoto_failures_drawn) + break; + std::array corners{{{low.x, height, low.y}, {high.x, height, low.y}, {high.x, height, high.y}, {low.x, height, high.y}}}; + if (auto const entry{grounded ? m_orthophoto_failure_ground.find({low.x, low.y}) : m_orthophoto_failure_ground.end()}; entry != m_orthophoto_failure_ground.end()) + { + for (std::size_t i = 0; i < corners.size(); ++i) + corners[i].y = entry->second.found[i] ? entry->second.heights[i] : corners[i].y; + } + for (int i = 0; i < 4; ++i) + projection.line(drawlist, corners[i], corners[(i + 1) % 4], colour, 2.0f); + projection.line(drawlist, corners[0], corners[2], (colour & ~IM_COL32_A_MASK) | IM_COL32(0, 0, 0, 110), 1.5f); + projection.line(drawlist, corners[1], corners[3], (colour & ~IM_COL32_A_MASK) | IM_COL32(0, 0, 0, 110), 1.5f); + } +} + +void editor_mode::render_orthophoto_status() +{ + // in the status bar, after the status: the tiles which failed, why, and the button trying them again at once + if (false == m_orthophoto.enabled()) + return; + auto const stats{m_orthophoto.stats()}; + auto const count{static_cast(stats.failed + stats.area_failed)}; + if (count == 0) + return; + ImGui::SameLine(0.0f, ImGui::GetFontSize()); + auto text{format(STR_P("Photos: %zu tile not downloaded", "Photos: %zu tiles not downloaded", count), count)}; + if (auto const reason{m_orthophoto.last_failure()}; false == reason.empty()) + text += std::string{" ("} + STR(reason) + ")"; + ImGui::TextColored(ImVec4(1.0f, 0.55f, 0.35f, 1.0f), "%s", text.c_str()); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("They're marked red in the view and tried again by themselves, later and later, up to 5 min apart")); + ImGui::SameLine(); + ImGui::TextDisabled("%s", "\xc2\xb7"); + ImGui::SameLine(); + if (ImGui::SmallButton(STR_C("Retry##photos"))) + { + m_orthophoto.retry_failed(); + orthophoto_hurry(); + } +} + void editor_mode::draw_ortho_compass() const { if (false == Global.EditorOrtho) @@ -6345,8 +6565,7 @@ void editor_mode::render_first_steps() } note_first_steps(); follow_tutorial(); - // the question of the terrain of a new scenery isn't drawn over the window: the window waits for the answer - if (false == m_first_steps_open || m_nmt_state.prompt) + if (false == m_first_steps_open) { m_first_steps_shown = 0; return; @@ -6656,7 +6875,9 @@ void editor_mode::render_shortcuts_window() {"RMB held, W S A D", "Fly forwards, back, left, right (arrows in the legacy scheme)"}, {"RMB held, E / Q", "Fly up / down (Page Up / Page Down in the legacy scheme)"}, {"F", "Fly to the selected"}, - {"O", "Top view, orthographic"}}}, + {"O", "Top view, orthographic"}, + {"~", "The aerial photo shown or hidden, kept with the scenery"}, + {"Ctrl+~", "The photos of what the camera sees downloaded before the rest"}}}, {"Ride along the track", {{"Y", "Ride along the track under the cursor, the tools working on; Y again leaves"}, {"RMB held, W / S", "Drive; Shift faster, Ctrl+W / Ctrl+Down much faster"}, @@ -7852,7 +8073,8 @@ void editor_mode::on_key(int const Key, int const Scancode, int const Action, in } if (false == anyModifier) { - m_orthophoto.enabled(false == m_orthophoto.enabled()); + // as the button of the toolbar: the scenery's, and the terrain model doesn't turn it on again behind the user + choose_orthophoto(false == m_orthophoto.enabled()); ui()->set_status(m_orthophoto.enabled() ? STR("Orthophoto on (~)") : STR("Orthophoto off (~)")); } break; @@ -8550,7 +8772,7 @@ void editor_mode::render_camera_settings(bool const Window) void editor_mode::orthophoto_hurry() { if (false == m_orthophoto.enabled()) - m_orthophoto.enabled(true); + choose_orthophoto(true); // what the camera sees of the ground: rays through a grid over the screen to the level of the ground under the camera, or as far // as the reach of the orthophoto along it where they don't come down to it (the sky) ImGuiIO const &io = ImGui::GetIO(); diff --git a/application/editormode.h b/application/editormode.h index 48062325b..129514670 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -557,7 +557,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri std::vector anchors; // x, z of a place of the tracks, the formation there less the model std::map, std::vector> anchor_cells; // the anchors in squares of the plan bool offered{false}; // the terrain was offered to the scenery begun without it - bool prompt{false}; // the offer is shown + bool prompt{false}; // the line of the offer is shown in the panel of the laying + bool offer_loading{false}; // and the terrain of the offer is being loaded bool moving{false}; // the place being picked again char place[96]{}; // typed: degrees or PUWG 1992 int shown{-1}; // the frame the terrain panel showed how far the work got @@ -694,9 +695,10 @@ class editor_mode : public application_mode, private editor_track::observer, pri bool nmt_busy() const; // the terrain made so far stays, what's waiting is dropped, the grade lines not fitted yet are left void nmt_stop(); - // a scenery begun from nothing, with no tracks nor terrain, is asked as the first track is laid whether it wants the terrain + // a scenery begun from nothing, with no tracks nor terrain, has the terrain loaded in the background as the first track is laid, + // where it lies on the map, with no question in the way: a line of the panel of the laying tells it, with the button to go without void nmt_offer(); - void render_nmt_prompt(); + void nmt_offer_start(); // the place picked in the terrain window: the terrain model and the orthophoto take it void nmt_place_scenery(glm::dvec2 const &Point); void nmt_accept_offer(); @@ -928,8 +930,33 @@ class editor_mode : public application_mode, private editor_track::observer, pri // the user turned the orthophoto on or off, or the scenery keeps it so: the terrain model made, or the area drawn, leaves it as it // is then, it turns it on only where nobody chose bool m_orthophoto_chosen{false}; - // the user turns the orthophoto on or off: kept with the editor, for every scenery + // the user turns the orthophoto on or off: kept with the editor, for the scenery void choose_orthophoto(bool const On); + // the 4K photos by themselves while the view is zoomed in to 15 cm a pixel; the m a pixel of the view, the top view's or of the + // ground under the camera + void orthophoto_zoom(); + double view_metres_per_pixel() const; + // the download area carried along where the camera or the laying comes near its edge, told in the status line + void area_grow(); + double m_area_grow_time{-100.0}; + bool m_area_grow_limited{false}; + // the tiles of the photo which failed: marked red in the view, counted in the status bar with the button trying them again + void draw_orthophoto_failures() const; + // the heights of the ground under the corners of the tiles which failed, outlined on it in the 3D view. A few tiles are asked for + // each frame, and a tile whose corners were found only again once in a while, so that the outlines don't keep the tiles of the + // ground in use every frame and their cache growing past its limit + struct orthophoto_failure_ground + { + std::array heights{}; + std::array found{}; + double asked{-1.0e9}; + }; + void ground_orthophoto_failures(std::vector> const &Failed) const; + mutable std::map, orthophoto_failure_ground> m_orthophoto_failure_ground; + void render_orthophoto_status(); + // the terrain loaded in the background as the first track of a new scenery is laid, told in a line of the panel of the laying with + // the button to go without it + void render_nmt_offer_line(); int m_orthophoto_frame{-10}; bool orthophoto_settings_open() const; // view menu: the top view, the lanes of the roads, the structure gauge, the environment @@ -956,6 +983,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri void render_viewport_overlays(); // the tools of the field of work, in the order of the toolbar void work_area_tools(std::vector &Buttons); + // the button of the toolbar of the key or the name given clicked, for the self-test; false: none such + bool press_toolbar(std::string const &Name); // the tools of the field of work in the toolbar, as icons with their names void render_toolbar_tools(); // what the row of the toolbar holds, written at its left end: the fields of work, the tools @@ -969,6 +998,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri bool m_georeference{false}; glm::dvec2 m_georeference_origin{0.0}; // northing, easting std::string m_georeference_line; + // //$g and the origin remembered or the one of //$o were apart, by that many m; //$g taken. 0: they weren't + double m_georeference_differs{0.0}; void read_georeference(); void draw_orthophoto(); void draw_ortho_compass() const; @@ -2535,6 +2566,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri track_hover split_spot() const; void update_track_hover(); void draw_track_lines() const; + mutable std::size_t m_track_lines_drawn{0}; // the paths draw_track_lines drew in the last frame, for the self-test // what the colours of the tracks in the view tell enum class track_colouring { plain, radius, speed, grade }; track_colouring m_track_colouring{track_colouring::plain}; diff --git a/application/editormodelay.cpp b/application/editormodelay.cpp index cf3e5ef47..b8dd757e7 100644 --- a/application/editormodelay.cpp +++ b/application/editormodelay.cpp @@ -2606,6 +2606,7 @@ void editor_mode::render_lay_ui() auto &tool{m_lay}; auto const state{lay_panel_state()}; render_lay_step(state); + render_nmt_offer_line(); if (state == lay_state::clicking || state == lay_state::extending) { render_lay_quality(); diff --git a/application/editormodenmt.cpp b/application/editormodenmt.cpp index 576db215b..9620bade3 100644 --- a/application/editormodenmt.cpp +++ b/application/editormodenmt.cpp @@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/. #include "editor/editorFormat.hpp" #include "editor/editorGeometry.hpp" #include "editor/editorKilometrage.hpp" +#include "editor/editorPhotoRules.hpp" #include "editor/editorSettings.hpp" #include "model/AnimModel.h" #include "utilities/Globals.h" @@ -68,84 +69,17 @@ bool rail(TTrack const &Track) return geometry::plan_distance(path.points[segment_data::point::start], path.points[segment_data::point::end]) <= broken_path; } -// a choice of the new scenery as a card: a sketch of the ground a track lies on, the name and what it means; true when clicked. -// the real terrain: hills under an aerial photo, the track along them; none: the flat plane at 0 drawn as a grid -bool ground_card(char const *Id, char const *Title, char const *Text, bool const Hills, bool const Enabled, float const Width) +// why the model wasn't fetched: a failure of the connection or of the service told in plain words, as the photo tells its own, as +// a sentence of its own (the code of WinHTTP is in the log); the messages of the model itself as they are +std::string nmt_error_text(std::string const &Error) { - auto const font{ImGui::GetFontSize()}; - auto const pad{std::round(font * 0.6f)}; - auto const sketch{std::round(font * 4.0f)}; - auto const wrap{Width - 2.0f * pad}; - auto const title{ImGui::CalcTextSize(Title, nullptr, false, wrap)}; - auto const text{ImGui::CalcTextSize(Text, nullptr, false, wrap)}; - auto const height{pad + sketch + pad * 0.6f + title.y + pad * 0.3f + text.y + pad}; - auto const top{ImGui::GetCursorScreenPos()}; - ImGui::BeginDisabled(false == Enabled); - auto const clicked{ImGui::InvisibleButton(Id, ImVec2(Width, height))}; - ImGui::EndDisabled(); - auto const hovered{Enabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)}; - auto *list{ImGui::GetWindowDrawList()}; - ImVec2 const bottom{top.x + Width, top.y + height}; - auto const rounding{std::round(font * 0.4f)}; - auto const accent{family_colour(0)}; - list->AddRectFilled(top, bottom, ImGui::GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), rounding); - list->AddRect(top, bottom, hovered ? accent : ImGui::GetColorU32(ImGuiCol_Border), rounding, 0, hovered ? 2.0f : 1.0f); - auto const alpha{Enabled ? 1.0f : 0.35f}; - auto const tint = [&](ImU32 const Colour) { return (Colour & ~IM_COL32_A_MASK) | (static_cast(((Colour >> IM_COL32_A_SHIFT) & 0xFF) * alpha) << IM_COL32_A_SHIFT); }; - ImVec2 const low{top.x + pad, top.y + pad}; - ImVec2 const high{top.x + Width - pad, top.y + pad + sketch}; - list->AddRectFilled(low, high, tint(IM_COL32(20, 26, 34, 255)), rounding * 0.6f); - list->PushClipRect(low, high, true); - int constexpr steps{48}; - auto const x_of = [&](float const Share) { return low.x + (high.x - low.x) * Share; }; - if (Hills) - { - auto const ground = [](float const Share) { return 0.45f + 0.18f * std::sin(Share * 7.0f + 0.4f) + 0.08f * std::sin(Share * 17.0f); }; - std::vector line; - for (int i = 0; i <= steps; ++i) - { - auto const share{static_cast(i) / steps}; - line.emplace_back(x_of(share), low.y + (high.y - low.y) * ground(share)); - } - // the strips under the line drawn without the soft edges, else seams show between them - auto const flags{list->Flags}; - list->Flags &= ~ImDrawListFlags_AntiAliasedFill; - for (int i = 0; i < steps; ++i) - list->AddQuadFilled(line[i], line[i + 1], ImVec2(line[i + 1].x, high.y), ImVec2(line[i].x, high.y), tint(IM_COL32(70, 110, 55, 200))); - list->Flags = flags; - list->AddPolyline(line.data(), static_cast(line.size()), tint(IM_COL32(150, 200, 110, 255)), 0, 1.5f); - // the track goes along the ground, a little above it on embankments, below it in cuttings - std::vector track; - for (int i = 0; i <= steps; ++i) - { - auto const share{static_cast(i) / steps}; - auto const smooth{0.45f + 0.09f * std::sin(share * 7.0f + 0.4f)}; - track.emplace_back(x_of(share), low.y + (high.y - low.y) * smooth - 2.0f); - } - list->AddPolyline(track.data(), static_cast(track.size()), tint(IM_COL32(235, 235, 235, 255)), 0, 2.5f); - } - else - { - auto const horizon{low.y + (high.y - low.y) * 0.35f}; - auto const grid{tint(IM_COL32(120, 130, 150, 120))}; - for (int i = 0; i <= 4; ++i) - { - auto const t{static_cast(i) / 4.0f}; - auto const y{horizon + (high.y - horizon) * t * t}; - list->AddLine(ImVec2(low.x, y), ImVec2(high.x, y), grid); - } - auto const middle{(low.x + high.x) * 0.5f}; - for (int i = -6; i <= 6; ++i) - list->AddLine(ImVec2(middle + i * (high.x - low.x) * 0.06f, horizon), ImVec2(middle + i * (high.x - low.x) * 0.22f, high.y), grid); - auto const level{horizon + (high.y - horizon) * 0.45f}; - list->AddLine(ImVec2(low.x, level), ImVec2(high.x, level), tint(IM_COL32(235, 235, 235, 255)), 2.5f); - } - list->PopClipRect(); - auto y{high.y + pad * 0.6f}; - list->AddText(nullptr, 0.0f, ImVec2(low.x, y), tint(ImGui::GetColorU32(ImGuiCol_Text)), Title, nullptr, wrap); - y += title.y + pad * 0.3f; - list->AddText(nullptr, 0.0f, ImVec2(low.x, y), tint(ImGui::GetColorU32(ImGuiCol_TextDisabled)), Text, nullptr, wrap); - return clicked && Enabled; + auto const reason{photo_rules::connection_reason(Error)}; + if (reason.empty()) + return STR(Error); + auto text{STR(reason)}; + if (false == text.empty() && text.front() >= 'a' && text.front() <= 'z') + text.front() = static_cast(text.front() - 'a' + 'A'); + return text; } // what a way of putting the tracks and the terrain together does, drawn along a track: onto, the track goes along the hills of @@ -2180,7 +2114,7 @@ void editor_mode::render_nmt_banner() bool again{false}; if (auto const error{m_nmt.error(&again)}; false == error.empty()) { - ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.3f, 1.0f), "%s", STR(error).c_str()); + ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.3f, 1.0f), "%s", nmt_error_text(error).c_str()); if (again) { ImGui::SameLine(); @@ -2554,7 +2488,7 @@ void editor_mode::render_nmt_ui() if (auto const error{m_nmt.error(&again)}; false == error.empty()) { ImGui::PushTextWrapPos(0.0f); - ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.3f, 1.0f), "%s", STR(error).c_str()); + ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.3f, 1.0f), "%s", nmt_error_text(error).c_str()); ImGui::PopTextWrapPos(); if (again && ImGui::Button(STR_C("Try again"))) m_nmt.retry(); @@ -2580,6 +2514,10 @@ void editor_mode::nmt_place_scenery(glm::dvec2 const &Point) state.under = false; state.anchors.clear(); state.anchor_cells.clear(); + // the line in the panel of the laying waited for the scenery to be placed on the map: its terrain loads now, as the offer has it. + // Left so, the line said the scenery wasn't placed and nothing loaded + if (state.prompt && false == state.offer_loading && m_nmt.placed()) + nmt_accept_offer(); } void editor_mode::nmt_offer() @@ -2590,8 +2528,20 @@ void editor_mode::nmt_offer() state.offered = true; if (m_streamer.resident() > 0 || std::any_of(simulation::Paths.sequence().begin(), simulation::Paths.sequence().end(), [](TTrack const *Track) { return Track != nullptr && rail(*Track); })) return; + nmt_offer_start(); +} + +void editor_mode::nmt_offer_start() +{ + auto &state{m_nmt_state}; nmt_locate(); + // no question in the way of the first track: where the scenery lies on the map the terrain loads in the background, and the photo + // lies flat meanwhile; the line in the panel of the laying tells it, with the button to go without state.prompt = true; + state.offer_loading = false; + if (m_nmt.placed()) + nmt_accept_offer(); + WriteLog(format("Editor: NMT - the terrain of the new scenery %s", state.offer_loading ? "loaded in the background" : "not loaded, the scenery isn't placed on the map"), logtype::generic); } void editor_mode::nmt_accept_offer() @@ -2602,9 +2552,9 @@ void editor_mode::nmt_accept_offer() // no tracks to fit to: the heights of the model as they are state.offset = 0.0; nmt_make(nmt_keys_around()); - state.prompt = false; - // the terrain goes on under the tracks as they're laid and around the camera, as the answer says, also where the settings had it - // off; the model of the download area is put into the cache again if it was stopped + state.offer_loading = true; + // the terrain goes on under the tracks as they're laid and around the camera, also after the scenery before went without it; the + // model of the download area is put into the cache again if that stopped it state.grow = true; state.declined = false; EditorSettings.terrain_while_laying(true); @@ -2618,6 +2568,7 @@ void editor_mode::nmt_decline_offer() { auto &state{m_nmt_state}; state.prompt = false; + state.offer_loading = false; // nothing of the model loaded any more: not under the tracks as they're laid, as the checkbox of the layers turns it off, nor around // the camera within the download area, as the checkbox of the terrain panel does, nor the squares of the download area into the // cache. Kept on, they went on being loaded, and where there's no network their requests failed in the log. For this scenery only: @@ -2625,8 +2576,10 @@ void editor_mode::nmt_decline_offer() state.grow = false; state.declined = true; m_nmt.prefetch({}); - // nor the squares asked for meanwhile, as the aerial photos asked for them to lie on + // nor the squares asked for meanwhile, as the aerial photos asked for them to lie on, nor the chunks waiting for them m_nmt.cancel(); + state.pending.clear(); + state.status = STR("Without terrain: the tracks lie on the flat plane; the terrain can be loaded later in Terrain (F5)"); } bool editor_mode::nmt_area() const @@ -2634,56 +2587,39 @@ bool editor_mode::nmt_area() const return false == m_nmt_state.declined && EditorSettings.terrain_model().area; } -void editor_mode::render_nmt_prompt() +void editor_mode::render_nmt_offer_line() { auto &state{m_nmt_state}; - char const *const title{STR_C("Terrain for the new scenery###nmtoffer")}; if (false == state.prompt) + return; + if (state.offer_loading && false == nmt_busy() && state.pending.empty()) { - // answered by the self-test, not by a card of its own: the question is closed, or it would hold the input as a modal - if (ImGui::IsPopupOpen(title) && ImGui::BeginPopupModal(title, nullptr, ImGuiWindowFlags_NoResize)) - { - ImGui::CloseCurrentPopup(); - ImGui::EndPopup(); - } + // in: the line goes, the terrain is there to be seen + state.prompt = false; + state.offer_loading = false; return; } - if (false == ImGui::IsPopupOpen(title)) - ImGui::OpenPopup(title); - // in the middle of the view, not of the screen with the docks; the window of the lesson waits meanwhile, not to lie under it - ImGui::SetNextWindowPos(ImVec2((editor_ui::view_min().x + editor_ui::view_max().x) * 0.5f, (editor_ui::view_min().y + editor_ui::view_max().y) * 0.5f), ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); - ImGui::SetNextWindowSize(ImVec2(520.0f * Global.ui_scale, 0.0f)); - if (false == ImGui::BeginPopupModal(title, nullptr, ImGuiWindowFlags_NoResize)) - return; - ImGui::TextWrapped("%s", STR_C("The scenery has no terrain yet. Load the terrain model and the aerial photos around here? The tracks laid then go along the real ground.")); - bool const placed{m_nmt.placed()}; - if (false == placed) - render_nmt_place(false, false); - auto const known{placed || m_nmt_map.picked()}; - ImGui::Spacing(); - auto const gap{std::round(ImGui::GetFontSize() * 0.6f)}; - auto const width{std::floor((ImGui::GetContentRegionAvail().x - gap) * 0.5f)}; - if (ground_card("##terrain", STR_C("Load the terrain"), STR_C("The terrain model and the aerial photos of geoportal.gov.pl around here; the tracks go along the real ground"), true, known, width)) + ImGui::AlignTextToFramePadding(); + if (state.offer_loading) { - nmt_accept_offer(); - ImGui::CloseCurrentPopup(); + if (auto const error{m_nmt.error()}; false == error.empty()) + ImGui::TextColored(ImVec4(1.0f, 0.6f, 0.3f, 1.0f), "%s %s", STR_C("Terrain:"), nmt_error_text(error).c_str()); + else + ImGui::TextDisabled("%s", STR_C("Terrain: loading...")); + item_tooltip("The terrain model of geoportal.gov.pl around here, in the background: the tracks laid meanwhile go onto it once it's in, the photo lies flat till then"); } - if (false == known) - item_tooltip("Pick where the scenery lies first"); - ImGui::SameLine(0.0f, gap); - if (ground_card("##flat", STR_C("Without terrain"), STR_C("The tracks lie on the flat plane at the height 0; the terrain can be loaded later in Terrain (F5)"), false, true, width)) + else { + ImGui::TextDisabled("%s", STR_C("Terrain: the scenery isn't placed on the map")); + ImGui::SameLine(); + if (ImGui::SmallButton(STR_C("Place it..."))) + choose_work_area(work_area::terrain); + item_tooltip("In Terrain (F5): the middle of the scenery clicked on the map, then its terrain and photos can be loaded"); + } + ImGui::SameLine(); + if (ImGui::SmallButton(STR_C("Without terrain"))) nmt_decline_offer(); - ImGui::CloseCurrentPopup(); - } - ImGui::Spacing(); - bool ask{EditorSettings.terrain_offer()}; - if (ImGui::Checkbox(STR_C("Ask with every new scenery"), &ask)) - { - EditorSettings.terrain_offer(ask); - EditorSettings.save(); - } - ImGui::EndPopup(); + item_tooltip("The tracks lie on the flat plane at the height 0; the terrain can be loaded later in Terrain (F5)"); } std::size_t editor_mode::nmt_join_ends() diff --git a/application/editormodeselftest.cpp b/application/editormodeselftest.cpp index 88d8121b2..ad7466912 100644 --- a/application/editormodeselftest.cpp +++ b/application/editormodeselftest.cpp @@ -124,7 +124,10 @@ editor_mode::selftest_result editor_mode::selftest_dispatch(std::string const &c // terrainfill ... the area outlined filled, as the fill tool does it // terrainturn the textures of the palette entry turned // terrainwater [size] material and repeat of a body of water; terrainwaterdefault -// orthophoto [in scene 0|1] the imagery layer on, placed and fitted as given +// orthophoto [in scene 0|1] the imagery layer on, placed and fitted as given; orthophoto off: turned +// off as the user does it, for the scenery; orthophoto forget: what the editor keeps of the photo of +// the scenery (whether it's on, its origin) forgotten +// toolbar the button of the toolbar of the field of work with the key or the name clicked (toolbar ~: the photo) // orthoarea ... | clear the download area through the points of the world, as drawn on the map; orthoareapause 0|1 // 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 @@ -181,7 +184,7 @@ editor_mode::selftest_result editor_mode::selftest_dispatch(std::string const &c // mouse , mouseat the cursor moved to the point of the window, or to where the point of the scene is seen, as the mouse moves it // glide [shot] the cursor led to where the point is seen in steps, frames apart, a screenshot after each step with shot // button , wheel the buttons and the wheel of the mouse, with the keys held -// press , release , tap a key held, let go, or both (ctrl, shift, alt, esc, enter, backspace, del, space, tab, a..z, 0..9, f1..f12) +// press , release , tap a key held, let go, or both (ctrl, shift, alt, esc, enter, backspace, del, space, tab, grave (~), a..z, 0..9, f1..f12) // softcursor <0|1> the cursor drawn into the picture, for screenshots // type the characters typed into the text field with the focus // screenshot @@ -196,7 +199,8 @@ editor_mode::selftest_result editor_mode::selftest_dispatch(std::string const &c // turntables the height of each turntable and of the model in its pit, and the heights of the tracks running up // to it out along their lines from the rim, against the model there // nmtstate logs how far the tracks going onto the terrain model are -// nmtquestion the question of the terrain of a new scenery opened, or answered without terrain as its card does; +// nmtquestion the terrain of a new scenery offered as the first L offers it (loaded in the background where the +// scenery is placed, the line in the panel of the laying), or gone without as its button does; // restore: the terrain made by itself as it was before that answer, which leaves the settings be // gradelines the grade line each track shares written to the file, for a map of them // fitreport to the file: the tracks off the terrain model, the switches off their plane, the rails on common timbers apart in height diff --git a/application/editormodeselftestexpect.cpp b/application/editormodeselftestexpect.cpp index 346f52ae7..8f75e9f5e 100644 --- a/application/editormodeselftestexpect.cpp +++ b/application/editormodeselftestexpect.cpp @@ -20,6 +20,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Logs.h" #include "utilities/utilities.h" #include "world/Track.h" +#include "imgui/imgui.h" #include #include @@ -421,7 +422,10 @@ void expect_terrain(std::string const &What, std::istringstream &Words, std::map auto const state{States.find(key)}; if (state == States.end()) { - expectation(false, What, "no state " + key, "grow, area or prompt"); + std::string keys; + for (auto const &[name, value] : States) + keys += (keys.empty() ? "" : ", ") + name; + expectation(false, What, "no state " + key, keys); return; } expectation(state->second == (wanted != 0), What, state->second ? "1" : "0", std::to_string(wanted)); @@ -506,7 +510,8 @@ struct shown_state std::string refusal; // by the cursor, while it's shown std::string status_line; glm::dvec3 lay_cursor{0.0}; - std::map terrain; // grow, area, prompt + std::map terrain; // grow, area, prompt, loading, question + std::map photo; // on, chosen, zoom4k, tracksover std::string tool; std::map windows; // shortcuts, settings, weather, orthophoto std::optional selected; // where the node selected stands @@ -527,6 +532,8 @@ bool expect_shown(std::string const &Kind, std::string const &What, std::istring expect_lay_cursor(What, Words, Shown.lay_cursor); else if (Kind == "terrain") expect_terrain(What, Words, Shown.terrain); + else if (Kind == "photo") + expect_terrain(What, Words, Shown.photo); else if (Kind == "tool") expect_tool(What, Words, Shown.tool); else if (Kind == "window") @@ -605,7 +612,13 @@ editor_mode::selftest_result editor_mode::selftest_expect(std::string const &com else { shown_state shown{m_switch.status, ui()->refusal_shown(), ui()->status_line(), m_lay.mouse, - {{"grow", m_nmt_state.grow}, {"area", nmt_area()}, {"prompt", m_nmt_state.prompt}}, tool_name(ui()->mode()), + {{"grow", m_nmt_state.grow}, + {"area", nmt_area()}, + {"prompt", m_nmt_state.prompt}, + {"loading", m_nmt_state.offer_loading}, + {"question", ImGui::IsPopupOpen(nullptr, ImGuiPopupFlags_AnyPopupId)}}, + {{"on", m_orthophoto.enabled()}, {"chosen", m_orthophoto_chosen}, {"zoom4k", m_orthophoto.zoom_hires()}, {"tracksover", m_track_lines_drawn > 0}}, + tool_name(ui()->mode()), {{"shortcuts", m_shortcuts_open}, {"settings", m_settings_open}, {"weather", m_environment_open}, {"orthophoto", m_orthophoto_window}}, std::nullopt, shown_lay_panel(), shown_switch_panel()}; if (m_node != nullptr) diff --git a/application/editormodeselftestterrain.cpp b/application/editormodeselftestterrain.cpp index 9a288ba96..58846bd71 100644 --- a/application/editormodeselftestterrain.cpp +++ b/application/editormodeselftestterrain.cpp @@ -63,6 +63,14 @@ editor_mode::selftest_result editor_mode::selftest_terrain(std::string const &co WriteLog("SELFTEST orthophoto off"); return selftest_result::handled; } + if (words.str().find(" forget") != std::string::npos) + { + // what the editor keeps of the photo of the scenery forgotten: its choice and its origin, as for a scenery never opened + EditorSettings.orthophoto_forget(m_orthophoto_scenery); + EditorSettings.save(); + WriteLog("SELFTEST orthophoto forget " + m_orthophoto_scenery); + return selftest_result::handled; + } int fit{1}; int inscene{0}; float height{0.0f}; @@ -76,6 +84,15 @@ editor_mode::selftest_result editor_mode::selftest_terrain(std::string const &co WriteLog(format("SELFTEST orthophoto at %.0f %.0f, radius %d, %s%s", config.north, config.east, config.radius, config.drape ? "fitted" : "flat", config.in_scene ? ", in the scene" : "")); return selftest_result::handled; } + if (command == "toolbar") + { + // the button of the toolbar of the key or the name given clicked + std::string name; + words >> name; + auto const pressed{press_toolbar(name)}; + WriteLog(format("SELFTEST toolbar %s: %s, orthophoto %d", name.c_str(), pressed ? "pressed" : "no such button", m_orthophoto.enabled() ? 1 : 0)); + return selftest_result::handled; + } if (command == "orthoarea") { // the download area through the points of the world (x, z), as drawn on the map; clear: none @@ -514,8 +531,10 @@ editor_mode::selftest_result editor_mode::selftest_terrain(std::string const &co std::string answer; words >> answer; auto const &model{EditorSettings.terrain_model()}; + // as the first L in a new scenery: the line of the offer in the panel of the laying, the terrain loaded in the background where + // the scenery is placed, no question asked if (answer == "open") - m_nmt_state.prompt = true; + nmt_offer_start(); else if (answer == "flat") { auto const kept{std::pair{EditorSettings.terrain_while_laying(), model.area}}; @@ -533,7 +552,7 @@ editor_mode::selftest_result editor_mode::selftest_terrain(std::string const &co m_nmt_state.grow = m_selftest.terrain_kept->first; m_nmt_state.declined = m_selftest.terrain_kept->second; } - WriteLog(format("SELFTEST nmtquestion %s: prompt %d, terrain while laying %d, around the camera %d", answer.c_str(), m_nmt_state.prompt ? 1 : 0, m_nmt_state.grow ? 1 : 0, nmt_area() ? 1 : 0)); + WriteLog(format("SELFTEST nmtquestion %s: prompt %d, loading %d, terrain while laying %d, around the camera %d", answer.c_str(), m_nmt_state.prompt ? 1 : 0, m_nmt_state.offer_loading ? 1 : 0, m_nmt_state.grow ? 1 : 0, nmt_area() ? 1 : 0)); return selftest_result::handled; } if (command == "nmtgrow") diff --git a/application/editormodetrackux.cpp b/application/editormodetrackux.cpp index 1bf87e3cc..3da13f52b 100644 --- a/application/editormodetrackux.cpp +++ b/application/editormodetrackux.cpp @@ -204,8 +204,12 @@ void editor_mode::draw_track_lines() const { auto const metres{ortho_metres_per_pixel()}; auto const colouring{ui()->mode() == nodebank_panel::TRACK ? m_track_colouring : track_colouring::plain}; - // the top view of the rails alone draws them in a colour of their own, as on a plan: both rails where they're apart on the screen - auto const rails{metres > 0.0 && Global.EditorOrthoTracks > 1 && colouring == track_colouring::plain}; + // the top view of the rails alone draws them in a colour of their own, as on a plan: both rails where they're apart on the screen. + // Under the photo drawn over the view the tracks laid would be hidden or dimmed: they're drawn over it zoomed in as well, their + // rails where they're apart, so the photo and the tracks are seen together + auto const photo{metres > 0.0 && m_orthophoto.enabled() && false == m_orthophoto.settings().in_scene && m_orthophoto.settings().opacity > 0.0f}; + auto const rails{metres > 0.0 && colouring == track_colouring::plain && (Global.EditorOrthoTracks > 1 || (photo && metres < kLinesFrom))}; + m_track_lines_drawn = 0; if (metres < kLinesFrom && colouring == track_colouring::plain && false == rails) return; auto const &io{ImGui::GetIO()}; @@ -226,6 +230,7 @@ void editor_mode::draw_track_lines() const bezier const curve{path}; if (false == plan_near(curve, centre, reach)) continue; + ++m_track_lines_drawn; auto colour{rails ? railcolour : IM_COL32(235, 235, 235, 150)}; if (colouring == track_colouring::radius) { diff --git a/application/editormodeviewport.cpp b/application/editormodeviewport.cpp index b9d44ad4d..0658b1c6d 100644 --- a/application/editormodeviewport.cpp +++ b/application/editormodeviewport.cpp @@ -39,6 +39,10 @@ struct editor_tool_button bool group{false}; // a gap before it, it starts another group bool enabled{true}; ImU32 accent{0}; // the colour of the family of the tool, 0 if it has none + // a slider of the value 0..1 in its place, its label and the value in per cent in it: the action applies the value as it's + // dragged, Done once it's let go + float *value{nullptr}; + std::function done; }; namespace @@ -174,6 +178,22 @@ void editor_mode::work_area_tools(std::vector &Buttons) add(track_tab::signals, true); add(track_tab::kilometrage, false); add(track_tab::lineside, false); + // the photo the tracks are laid over: shown or hidden for the scenery (~), and how much it covers, at hand + auto const photo{m_orthophoto.enabled()}; + Buttons.push_back({icon::orthophoto, STR_C("Photo"), "~", STR_C("The aerial photo of geoportal.gov.pl under the scenery, shown or hidden in this scenery"), photo, + [this, photo]() { choose_orthophoto(false == photo); }, true}); + if (photo && false == m_orthophoto.settings().in_scene) + { + static float opacity{0.6f}; + opacity = m_orthophoto.settings().opacity; + Buttons.push_back({icon::count, STR_C("Opacity"), nullptr, STR_C("How much the photo covers the scenery under it"), false, [this]() { + auto config{m_orthophoto.settings()}; + config.opacity = opacity; + m_orthophoto.settings(config); + }}); + Buttons.back().value = &opacity; + Buttons.back().done = [this]() { save_orthophoto_settings(); }; + } auto const advanced{m_track_tab == track_tab::turntable || srk_tab() || vehicle || m_track_tab == track_tab::profile || m_track_tab == track_tab::speed || m_track_tab == track_tab::joints || m_track_tab == track_tab::norms || m_gauge.is_open()}; auto const more{EditorSettings.track_tools_more()}; Buttons.push_back({icon::more, more ? STR_C("Less") : STR_C("More"), nullptr, STR_C("Turntable, stations and their signals, line block, trainset, profile, speed, joints, gauge"), more, [more]() { @@ -244,6 +264,21 @@ void editor_mode::work_area_tools(std::vector &Buttons) } } +bool editor_mode::press_toolbar(std::string const &Name) +{ + std::vector buttons; + work_area_tools(buttons); + for (auto const &button : buttons) + if ((button.key != nullptr && Name == button.key) || Name == button.label) + { + if (false == button.enabled || false == static_cast(button.action)) + return false; + button.action(); + return true; + } + return false; +} + editor_mode::section_tool const &editor_mode::surroundings_tool(nodebank_panel::edit_mode const Mode) { // in the order of the edit modes: select, copy to bank, insert, brush, area fill @@ -416,6 +451,9 @@ void editor_mode::render_toolbar_tools() return Button.picture == icon::count || Names == names::all || (Names == names::chosen && Button.chosen); }; auto const width = [&](tool_button const &Button, names const Names) { + // a slider keeps its width, its label in it + if (Button.value != nullptr) + return std::round(std::max(96.0f * s, ImGui::CalcTextSize(Button.label).x + ImGui::CalcTextSize(" 100%").x + 2.0f * pad)); if (false == named(Button, Names)) return std::max(buttonheight, iconside + pad); auto result{2.0f * pad + ImGui::CalcTextSize(Button.label).x}; @@ -451,6 +489,27 @@ void editor_mode::render_toolbar_tools() } auto const size{ImVec2(width(button, shown), buttonheight)}; ImVec2 const position{x, top}; + if (button.value != nullptr) + { + // in the middle of the row, as high as a field + ImGui::SetCursorScreenPos(ImVec2(position.x, std::round(top + (buttonheight - ImGui::GetFrameHeight()) * 0.5f))); + ImGui::PushID(static_cast(index)); + ImGui::SetNextItemWidth(size.x); + int percent{static_cast(std::lround(*button.value * 100.0f))}; + if (ImGui::SliderInt("##value", &percent, 0, 100, (std::string{button.label} + " %d%%").c_str())) + { + *button.value = static_cast(percent) / 100.0f; + if (button.action) + button.action(); + } + if (ImGui::IsItemDeactivatedAfterEdit() && button.done) + button.done(); + if (ImGui::IsItemHovered() && false == ImGui::IsItemActive()) + button_tooltip(button); + ImGui::PopID(); + x = position.x + size.x; + continue; + } ImGui::SetCursorScreenPos(position); ImGui::PushID(static_cast(index)); auto const clicked{ImGui::InvisibleButton("##tool", size)}; diff --git a/application/editoruilayer.cpp b/application/editoruilayer.cpp index 35bc83045..57fec257e 100644 --- a/application/editoruilayer.cpp +++ b/application/editoruilayer.cpp @@ -659,6 +659,8 @@ void editor_ui::render_() else ImGui::TextUnformatted(m_layerspanel.status.c_str()); } + if (m_statusextra) + m_statusextra(); if (m_node != nullptr) { auto const name{m_node->name().empty() ? std::string{STR_C("(no name)")} : m_node->name()}; diff --git a/application/editoruilayer.h b/application/editoruilayer.h index a6bbbf6ec..eaa1b881a 100644 --- a/application/editoruilayer.h +++ b/application/editoruilayer.h @@ -196,6 +196,8 @@ class editor_ui : public ui_layer // what the cursor tells while it's shown, and the status line; for the expectations of the self-test std::string refusal_shown() const; std::string const &status_line() const { return m_layerspanel.status; } + // what the mode draws in the status bar after the status, a lasting state with a button of its own + void set_status_extra(std::function Renderer) { m_statusextra = std::move(Renderer); } private: // methods @@ -212,6 +214,9 @@ class editor_ui : public ui_layer void render_windows_menu() override; void render_tools_menu() override; int palette() const override; + // the panels of the editor are opaque: the 3d scene, drawn under the whole window, doesn't show through the docks around the view; + // a window over the view which wants to be seen through says so itself (SetNextWindowBgAlpha) + float window_alpha() const override { return 1.0f; } void render_dockspace() override; void render_() override; // docks the windows of the editor in their default places @@ -251,6 +256,7 @@ class editor_ui : public ui_layer double m_refusaltime{-100.0}; bool m_refusalnotice{false}; // a warning rather than a refusal double m_statustime{-100.0}; + std::function m_statusextra; int m_savequeued{0}; // the frame of a save asked for from a field being typed in bool m_tooloptionsopen{true}; bool m_inspectoropen{true}; diff --git a/application/scriptedinput.cpp b/application/scriptedinput.cpp index 8f35d8cfe..63cc0ec66 100644 --- a/application/scriptedinput.cpp +++ b/application/scriptedinput.cpp @@ -29,7 +29,7 @@ namespace int key_named(std::string const &Name) { static std::map const names{{"ctrl", GLFW_KEY_LEFT_CONTROL}, {"shift", GLFW_KEY_LEFT_SHIFT}, {"alt", GLFW_KEY_LEFT_ALT}, {"esc", GLFW_KEY_ESCAPE}, {"enter", GLFW_KEY_ENTER}, {"backspace", GLFW_KEY_BACKSPACE}, - {"del", GLFW_KEY_DELETE}, {"space", GLFW_KEY_SPACE}, {"tab", GLFW_KEY_TAB}}; + {"del", GLFW_KEY_DELETE}, {"space", GLFW_KEY_SPACE}, {"tab", GLFW_KEY_TAB}, {"grave", GLFW_KEY_GRAVE_ACCENT}}; if (auto const found{names.find(Name)}; found != names.end()) return found->second; if (Name.size() == 1 && std::isalpha(static_cast(Name[0]))) diff --git a/application/uilayer.cpp b/application/uilayer.cpp index 080623689..e1ab267c6 100644 --- a/application/uilayer.cpp +++ b/application/uilayer.cpp @@ -30,6 +30,7 @@ ImGuiIO *ui_layer::m_imguiio{nullptr}; GLint ui_layer::m_textureunit{GL_TEXTURE0}; bool ui_layer::m_cursorvisible; int ui_layer::m_palette{0}; +float ui_layer::m_window_alpha{-1.0f}; ImFont *ui_layer::font_default{nullptr}; ImFont *ui_layer::font_mono{nullptr}; ImFont *ui_layer::font_loading{nullptr}; @@ -182,9 +183,10 @@ void create_viewport_window(ImGuiViewport *Viewport) } } // namespace -void ui_layer::imgui_colors(int const Palette) +void ui_layer::imgui_colors(int const Palette, float const WindowAlpha) { m_palette = Palette; + m_window_alpha = WindowAlpha; auto const rgb = [](int const Color, float const Alpha = 1.0f) { return ImVec4(((Color >> 16) & 0xff) / 255.0f, ((Color >> 8) & 0xff) / 255.0f, (Color & 0xff) / 255.0f, Alpha); }; @@ -204,7 +206,7 @@ void ui_layer::imgui_colors(int const Palette) colors[ImGuiCol_Text] = rgb(editor ? 0xe6e8eb : 0xffffff); colors[ImGuiCol_TextDisabled] = rgb(editor ? 0x8a929c : 0xa0a0a2); - colors[ImGuiCol_WindowBg] = rgb(panel, Global.UIBgOpacity); // ui.bg.opacity from config file + colors[ImGuiCol_WindowBg] = rgb(panel, WindowAlpha); // ui.bg.opacity from config file, unless the ui has its own colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_PopupBg] = rgb(base, 0.98f); colors[ImGuiCol_Border] = rgb(border); @@ -261,7 +263,7 @@ void ui_layer::imgui_colors(int const Palette) void ui_layer::imgui_style() { - imgui_colors(0); + imgui_colors(0, Global.UIBgOpacity); // flat, square elements with thin borders auto &style = ImGui::GetStyle(); @@ -539,11 +541,16 @@ void ui_layer::render_internal() } } +float ui_layer::window_alpha() const +{ + return Global.UIBgOpacity; +} + void ui_layer::begin_ui_frame() { // the colours follow the ui of the mode drawn - if (palette() != m_palette) - imgui_colors(palette()); + if (palette() != m_palette || window_alpha() != m_window_alpha) + imgui_colors(palette(), window_alpha()); begin_ui_frame_internal(); render_dockspace(); m_dockspaceframe = ImGui::GetFrameCount(); diff --git a/application/uilayer.h b/application/uilayer.h index 69c4e08d3..03eb2667a 100644 --- a/application/uilayer.h +++ b/application/uilayer.h @@ -79,7 +79,7 @@ public: static bool init( GLFWwindow *Window ); static void imgui_style(); // colours of the user interface: 0 the green ones shared with the starter, 1 the amber ones of the editor - static void imgui_colors(int const Palette); + static void imgui_colors(int const Palette, float const WindowAlpha); // assign texturing hardware unit static void set_unit( GLint const Textureunit ) { m_textureunit = GL_TEXTURE0 + Textureunit; } @@ -154,7 +154,10 @@ protected: { return 0; } + // how opaque the windows of this ui are: the setting of the simulator, ui.bg.opacity, unless the ui says otherwise + virtual float window_alpha() const; static int m_palette; // colours set at the moment + static float m_window_alpha; ui_log_panel m_logpanel { "Log", true }; bool m_suppress_menu = false; // if `true`, the menu at the top of the window will not be present bool m_menu_always = false; // if `true`, the menu is shown all the time, not only when the mouse is at the top of the window diff --git a/editor/editorGeoMap.cpp b/editor/editorGeoMap.cpp index 68b153559..f7ce4448e 100644 --- a/editor/editorGeoMap.cpp +++ b/editor/editorGeoMap.cpp @@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/. #include "stb/stb_image.h" #include +#include #include #include #include @@ -37,6 +38,8 @@ double constexpr kCentralMeridian{19.0}; glm::dvec2 const kPolandLow{140000.0, 120000.0}; glm::dvec2 const kPolandHigh{890000.0, 800000.0}; std::string const kMapService{"https://mapy.geoportal.gov.pl/wss/ext/OSM/BaseMap/service"}; +// the orthophoto of GUGiK, the service the photos under the scenery in the editor come from +std::string const kPhotoService{"https://mapy.geoportal.gov.pl/wss/service/PZGIK/ORTO/WMS/StandardResolution"}; double seconds() { @@ -179,6 +182,14 @@ void editor_map::release() m_texture = 0; } +void editor_map::photos(bool const Photos) +{ + if (Photos == m_photos) + return; + m_photos = Photos; + m_dirty = true; +} + void editor_map::pick(glm::dvec2 const &Point) { m_point = Point; @@ -248,7 +259,28 @@ bool editor_map::render(glm::vec2 const &Size) m_changed = seconds(); } } - auto const clicked{ImGui::IsItemDeactivated() && false == m_dragging}; + // the switch of the map and the photos in the top right corner: a click on it isn't a click on the map + auto const switch_labels{std::array{STR_C("Map"), STR_C("Photos")}}; + auto const pad{4.f}; + std::array switch_low{}; + std::array switch_high{}; + { + auto right{origin.x + Size.x - 6.f}; + for (int i = 1; i >= 0; --i) + { + auto const text{ImGui::CalcTextSize(switch_labels[i])}; + switch_high[i] = ImVec2(right, origin.y + 6.f + text.y + 2.f * pad); + switch_low[i] = ImVec2(right - text.x - 4.f * pad, origin.y + 6.f); + right = switch_low[i].x - 2.f; + } + } + auto const on_switch = [&](int const I) { return mouse.x >= switch_low[I].x && mouse.x < switch_high[I].x && mouse.y >= switch_low[I].y && mouse.y < switch_high[I].y; }; + auto clicked{ImGui::IsItemDeactivated() && false == m_dragging}; + if (clicked && (on_switch(0) || on_switch(1))) + { + photos(on_switch(1)); + clicked = false; + } if (clicked) { m_clicked = to_world(mouse); @@ -292,6 +324,33 @@ bool editor_map::render(glm::vec2 const &Size) drawlist->AddLine(ImVec2(at.x - 16.f, at.y), ImVec2(at.x + 16.f, at.y), colour, 2.f); drawlist->AddLine(ImVec2(at.x, at.y - 16.f), ImVec2(at.x, at.y + 16.f), colour, 2.f); } + // the switch drawn, the layer shown lit; the photos asked for but the map zoomed out too far for them told under it + for (int i = 0; i < 2; ++i) + { + auto const chosen{(i == 1) == m_photos}; + auto const over{hovered && on_switch(i)}; + drawlist->AddRectFilled(switch_low[i], switch_high[i], chosen ? IM_COL32(0, 120, 215, 235) : over ? IM_COL32(60, 60, 60, 220) : IM_COL32(0, 0, 0, 170), 4.f); + drawlist->AddText(ImVec2(switch_low[i].x + 2.f * pad, switch_low[i].y + pad), IM_COL32(255, 255, 255, 245), switch_labels[i]); + } + if (hovered && (on_switch(0) || on_switch(1))) + ImGui::SetTooltip("%s", STR_C("The aerial photos of geoportal.gov.pl, the same as under the scenery in the editor, once the map is zoomed in")); + // the photos shown: whose they are, in the corner under them, as the base map has it in its picture + if (m_texture != 0 && m_image_photo) + { + auto const text{STR_C("Imagery: GUGiK, geoportal.gov.pl")}; + auto const size{ImGui::CalcTextSize(text)}; + ImVec2 const at{origin.x + Size.x - size.x - 8.f, origin.y + Size.y - size.y - 6.f}; + drawlist->AddRectFilled(ImVec2(at.x - 4.f, at.y - 2.f), ImVec2(at.x + size.x + 4.f, at.y + size.y + 2.f), IM_COL32(0, 0, 0, 150), 3.f); + drawlist->AddText(at, IM_COL32(255, 255, 255, 220), text); + } + if (m_photos && m_scale > photo_scale) + { + auto const text{STR_C("Zoom in for the photos")}; + auto const size{ImGui::CalcTextSize(text)}; + ImVec2 const at{switch_high[1].x - size.x - 4.f, switch_high[1].y + 4.f}; + drawlist->AddRectFilled(ImVec2(at.x - 4.f, at.y - 2.f), ImVec2(at.x + size.x + 4.f, at.y + size.y + 2.f), IM_COL32(0, 0, 0, 170), 4.f); + drawlist->AddText(at, IM_COL32(255, 255, 255, 230), text); + } std::string info; if (hovered) { @@ -330,11 +389,15 @@ void editor_map::request(glm::vec2 const &Size) auto const high{m_centre + half}; char bbox[160]; std::snprintf(bbox, sizeof(bbox), "%.1f,%.1f,%.1f,%.1f", low.x, low.y, high.x, high.y); - auto const url{kMapService + "?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=osm&STYLES=&SRS=EPSG:2180&BBOX=" + bbox + "&WIDTH=" + std::to_string(width) + "&HEIGHT=" + std::to_string(height) + "&FORMAT=image/png"}; - m_request = std::async(std::launch::async, [url, low, high, closing = &m_closing]() { + // zoomed in the photos, as the orthophoto of the editor gives them; further out the map, the photos of a whole region aren't read + auto const photo{m_photos && m_scale <= photo_scale}; + auto const url{photo ? kPhotoService + "?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=Raster&STYLES=&SRS=EPSG:2180&BBOX=" + bbox + "&WIDTH=" + std::to_string(width) + "&HEIGHT=" + std::to_string(height) + "&FORMAT=image/jpeg" + : kMapService + "?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=osm&STYLES=&SRS=EPSG:2180&BBOX=" + bbox + "&WIDTH=" + std::to_string(width) + "&HEIGHT=" + std::to_string(height) + "&FORMAT=image/png"}; + m_request = std::async(std::launch::async, [url, low, high, photo, closing = &m_closing]() { image result; result.low = low; result.high = high; + result.photo = photo; std::vector data; thread_local editor_http::client client; if (false == client.get(url, data, result.error, [closing]() { return closing->load(); })) @@ -373,4 +436,5 @@ void editor_map::upload() m_texture = renderer != nullptr ? renderer->Create_Image(result.pixels.data(), result.width, result.height) : 0; m_image_low = result.low; m_image_high = result.high; + m_image_photo = result.photo; } diff --git a/editor/editorGeoMap.hpp b/editor/editorGeoMap.hpp index a1dd07124..fb527479f 100644 --- a/editor/editorGeoMap.hpp +++ b/editor/editorGeoMap.hpp @@ -31,8 +31,8 @@ bool in_poland(glm::dvec2 const &Point); } // namespace editor_geo -// the map of Poland to pick a place on: the base map of OpenStreetMap from geoportal.gov.pl, the wheel zooms it, a drag moves -// it, a click picks the place +// the map of Poland to pick a place on: the base map of OpenStreetMap from geoportal.gov.pl, or zoomed in the aerial photos of +// geoportal.gov.pl, the wheel zooms it, a drag moves it, a click picks the place class editor_map { public: @@ -63,6 +63,14 @@ class editor_map } // a line written at the bottom of the map, a word of what a click does there; empty: none void hint(std::string Text) { m_hint = std::move(Text); } + // the aerial photos of geoportal.gov.pl (the orthophoto the editor lays under the scenery, the same service) in place of the map + // where it's zoomed in enough to tell them apart; further out the map. Switched in the corner of the map + bool photos() const { return m_photos; } + // the image shown is of the photos, not of the map: whose it is, is told under it + bool photos_shown() const { return m_texture != 0 && m_image_photo; } + void photos(bool const Photos); + // m a pixel up to which the photos are shown, further out the map + static constexpr double photo_scale{40.0}; private: struct image @@ -73,6 +81,7 @@ class editor_map int height{0}; std::vector pixels; std::string error; + bool photo{false}; // of the aerial photos, not of the map }; void request(glm::vec2 const &Size); @@ -100,4 +109,6 @@ class editor_map std::vector m_outline; bool m_outline_closed{false}; std::string m_hint; + bool m_photos{true}; + bool m_image_photo{false}; // the image shown is of the photos, told under it }; diff --git a/editor/editorOrthophoto.cpp b/editor/editorOrthophoto.cpp index 4f41cf81b..d3ad6e2ab 100644 --- a/editor/editorOrthophoto.cpp +++ b/editor/editorOrthophoto.cpp @@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/. #include "editor/editorOrthophoto.hpp" #include "editor/editorHttp.hpp" #include "editor/editorGeometry.hpp" +#include "editor/editorPhotoRules.hpp" #include "editor/editorRecords.hpp" #include "rendering/renderer.h" @@ -761,12 +762,12 @@ bool editor_orthophoto::owns(gfx::geometry_handle const &Geometry) // the line of the scenery and its format: editor_records::orthophoto_line() and read_orthophoto() std::string editor_orthophoto::mark(config const &Config, bool const Enabled, bool const Origin) { - return editor_records::orthophoto_line(Enabled, Origin, Config.north, Config.east); + return editor_records::orthophoto_line(Enabled, Origin, Config.north, Config.east, Config.aligned); } bool editor_orthophoto::read_mark(std::string const &Text, config &Config, bool &Enabled) { - return editor_records::read_orthophoto(Text, Enabled, Config.north, Config.east); + return editor_records::read_orthophoto(Text, Enabled, Config.north, Config.east, &Config.aligned); } void editor_orthophoto::enabled(bool State) @@ -857,11 +858,27 @@ void editor_orthophoto::restart_prefetch() std::sort(keys.begin(), keys.end(), [&](tile_key const &A, tile_key const &B) { return distance(A) < distance(B); }); { std::lock_guard lock(m_mutex); + source const now{m_config.year, m_config.hires, m_config.service, m_config.layers}; + // the area grown, the photos the same: the tiles which failed keep their tries, and the ones waiting for the next one go on + // waiting, not asked for again all at once. Cleared, every growth started the backoff over, offline over and over + std::map> kept; + if (now == m_prefetch_source) + { + std::set const area(keys.begin(), keys.end()); + for (auto const &[key, retry] : m_prefetch_retry) + if (area.contains(key)) + kept.emplace(key, retry); + } + m_prefetch_failed.clear(); + for (auto const &[key, retry] : kept) + if (retry.second != std::numeric_limits::max()) + m_prefetch_failed.insert(key); + std::erase_if(keys, [this](tile_key const &Key) { return m_prefetch_failed.contains(Key); }); m_prefetch = std::move(keys); m_prefetch_next = 0; - m_prefetch_failed.clear(); + m_prefetch_retry = std::move(kept); m_prefetch_cached.clear(); - m_prefetch_source = source{m_config.year, m_config.hires, m_config.service, m_config.layers}; + m_prefetch_source = now; ++m_prefetch_generation; } m_wakeup.notify_all(); @@ -896,15 +913,6 @@ void editor_orthophoto::ground_arrived() } } -bool editor_orthophoto::waits_for_ground(tile_key const &Key) const -{ - // a tile laid out already stays, sampled again when its ground comes (ground_came) - if (!m_coming || m_tiles.contains(Key)) - return false; - glm::dvec2 const corner = tile_corner(Key); - return m_coming(corner - glm::dvec2{tile_size}, corner); -} - bool editor_orthophoto::ground_came(tile_key const &Key, tile const &Tile) const { glm::dvec2 const corner = tile_corner(Key); @@ -975,13 +983,44 @@ void editor_orthophoto::retry_failed() // the tiles of the area which failed taken again { std::lock_guard lock(m_mutex); + m_prefetch.erase(m_prefetch.begin(), m_prefetch.begin() + static_cast(m_prefetch_next)); + m_prefetch_next = 0; for (auto const &key : m_prefetch_failed) m_prefetch.push_back(key); m_prefetch_failed.clear(); + m_prefetch_retry.clear(); + m_last_error.clear(); } m_wakeup.notify_all(); } +std::vector> editor_orthophoto::failed() const +{ + std::vector> result; + if (false == m_enabled) + return result; + std::set keys; + for (auto const &[key, t] : m_tiles) + if (t.failures > 0 && t.level == 0 && false == t.empty) + keys.insert(key); + { + std::lock_guard lock(m_mutex); + keys.insert(m_prefetch_failed.begin(), m_prefetch_failed.end()); + } + for (auto const &key : keys) + { + glm::dvec2 const corner = tile_corner(key); + result.emplace_back(corner - glm::dvec2{tile_size}, corner); + } + return result; +} + +std::string editor_orthophoto::last_failure() const +{ + std::lock_guard lock(m_mutex); + return m_last_error.empty() ? std::string{} : photo_rules::failure_reason(m_last_error); +} + editor_orthophoto::statistics editor_orthophoto::stats() const { statistics out; @@ -1022,11 +1061,11 @@ bool editor_orthophoto::outside_area(tile_key const &Key) const return false == m_area.empty() && 0 == m_area_tiles.count(Key); } -int editor_orthophoto::level_for(int Distance, bool Hires) +int editor_orthophoto::level_for(int Distance, int HiresReach) { // coarser imagery further away keeps the memory use of large radii in check - if (Distance <= 1) - return Hires ? hires_pixels : standard_pixels; + if (Distance <= HiresReach) + return hires_pixels; if (Distance <= 3) return standard_pixels; if (Distance <= 6) @@ -1034,6 +1073,13 @@ int editor_orthophoto::level_for(int Distance, bool Hires) return standard_pixels / 4; } +int editor_orthophoto::hires_reach() const +{ + if (m_config.hires) + return 1; + return m_zoom_hires ? 0 : -1; +} + editor_orthophoto::tile_key editor_orthophoto::camera_tile(glm::dvec3 const &Camera) const { double const east = m_config.east - Camera.x; @@ -1088,7 +1134,7 @@ void editor_orthophoto::schedule(tile_key const &Key, tile &Tile, int Level, std { std::lock_guard lock(m_mutex); m_queue.erase(std::remove_if(m_queue.begin(), m_queue.end(), [&](job const &Job) { return Job.key == Key; }), m_queue.end()); - m_queue.push_back(job{Key, Level, Alpha, source{m_config.year, m_config.hires, m_config.service, m_config.layers}, m_generation.load()}); + m_queue.push_back(job{Key, Level, Alpha, source{m_config.year, m_config.hires || m_zoom_hires, m_config.service, m_config.layers}, m_generation.load()}); } Tile.requested = Level; Tile.requested_alpha = Alpha; @@ -1117,8 +1163,9 @@ std::size_t editor_orthophoto::apply(result &Result, double Now) { if (current) { + // tried again later and later, never given up: the service comes back, or the connection does ++t.failures; - t.retry_at = (t.failures >= 5) ? std::numeric_limits::max() : Now + std::min(120.0, 5.0 * std::pow(2.0, t.failures - 1)); + t.retry_at = Now + photo_rules::retry_delay(t.failures); } return 0; } @@ -1220,6 +1267,7 @@ void editor_orthophoto::sample_ground(tile_key const &Key, tile &Tile) { Tile.grounded = true; Tile.guessed = false; + Tile.flat.reset(); Tile.scene_dirty = true; Tile.heights.clear(); Tile.surface.clear(); @@ -1333,11 +1381,15 @@ void editor_orthophoto::sample_ground(tile_key const &Key, tile &Tile) heights[static_cast(j) * samples + i] = y; } } - // nothing to go by yet: the tile stays flat at the layer height, and is sampled again when something comes + // nothing to go by yet: the tile lies flat at the height of the ground found last, or of the layer before any was, and is + // sampled again when something comes Tile.guessed = true; missing = std::count(heights.begin(), heights.end(), no_height); if (missing == heights.size()) + { + Tile.flat = m_known_height; return; + } } while (missing > 0) { @@ -1373,6 +1425,7 @@ void editor_orthophoto::sample_ground(tile_key const &Key, tile &Tile) auto const range = std::minmax_element(Tile.heights.begin(), Tile.heights.end()); Tile.height_min = *range.first; Tile.height_max = *range.second; + m_known_height = 0.5f * (Tile.height_min + Tile.height_max); } void editor_orthophoto::remove_shape(tile &Tile) @@ -1438,8 +1491,9 @@ void editor_orthophoto::build_shape(tile_key const &Key, tile &Tile) } else { - // flat quad at the layer height, triangles facing up like the draped surface - double const y = m_config.height; + // flat quad at the layer height, or at the height of the ground found last where there's none yet under it, triangles facing + // up like the draped surface + double const y = (m_config.drape && Tile.flat.has_value()) ? *Tile.flat : m_config.height; glm::dvec3 const nw(corner.x, y, corner.y), ne(corner.x - tile_size, y, corner.y); glm::dvec3 const sw(corner.x, y, corner.y - tile_size), se(corner.x - tile_size, y, corner.y - tile_size); for (auto const &point : {nw, sw, ne, se, ne, sw}) @@ -1592,11 +1646,30 @@ void editor_orthophoto::update(glm::dvec3 const &Camera) return; tile_key const centre = camera_tile(Camera); - if (centre != m_camera_tile) { - m_camera_tile = centre; std::lock_guard lock(m_mutex); - m_focus = centre; + if (centre != m_camera_tile) + { + m_camera_tile = centre; + m_focus = centre; + } + // the tiles of the area which failed, tried again once their time comes; the ones fetched already go from the queue first, so it + // doesn't grow by each retry + bool again{false}; + for (auto it = m_prefetch_retry.begin(); it != m_prefetch_retry.end(); ++it) + if (it->second.second <= now && m_prefetch_failed.erase(it->first) > 0) + { + if (m_prefetch_next > 0) + { + m_prefetch.erase(m_prefetch.begin(), m_prefetch.begin() + static_cast(m_prefetch_next)); + m_prefetch_next = 0; + } + m_prefetch.push_back(it->first); + it->second.second = std::numeric_limits::max(); + again = true; + } + if (again) + m_wakeup.notify_all(); } int const radius = m_config.radius; auto const distance = [&](tile_key const &Key) { return std::max(std::abs(Key.first - centre.first), std::abs(Key.second - centre.second)); }; @@ -1645,12 +1718,11 @@ void editor_orthophoto::update(glm::dvec3 const &Camera) } continue; } - if (waits_for_ground(key)) - continue; + // laid out at once, whether its ground has come or not: it lies flat till it does, then goes onto it tile &t = m_tiles[key]; if (!in_service_area(key.first, key.second)) t.empty = true; - int const level = first_level(t, level_for(std::max(std::abs(dx), std::abs(dy)), m_config.hires)); + int const level = first_level(t, level_for(std::max(std::abs(dx), std::abs(dy)), hires_reach())); if (t.empty || (t.level == level && t.alpha == m_alpha) || (t.requested == level && t.requested_alpha == m_alpha) || now < t.retry_at) continue; schedule(key, t, level, m_alpha); @@ -1755,16 +1827,33 @@ void editor_orthophoto::worker() auto const ok{prefetch_tile(client, request, error, stopping)}; if (m_stop) return; - if (!ok) - ErrorLog("Editor orthophoto: tile " + std::to_string(prefetch->second) + "_" + std::to_string(prefetch->first) + " of the download area failed: " + error); - std::lock_guard lock(m_mutex); - if (prefetchgeneration == m_prefetch_generation) + // a tile failing again and again, offline say, is logged the first time: each retry logged, the log grew by the whole area + // every 5 min + bool again{false}; { - if (ok) - m_prefetch_cached.insert(*prefetch); - else - m_prefetch_failed.insert(*prefetch); + std::lock_guard lock(m_mutex); + if (prefetchgeneration == m_prefetch_generation) + { + if (ok) + { + m_prefetch_cached.insert(*prefetch); + m_prefetch_retry.erase(*prefetch); + } + else + { + // tried again later and later, up to 5 min apart, never given up + m_prefetch_failed.insert(*prefetch); + auto &retry{m_prefetch_retry[*prefetch]}; + again = retry.first > 0; + ++retry.first; + retry.second = steady_seconds() + photo_rules::retry_delay(retry.first); + if (error != "cancelled") + m_last_error = error; + } + } } + if (false == ok && false == again) + ErrorLog("Editor orthophoto: tile " + std::to_string(prefetch->second) + "_" + std::to_string(prefetch->first) + " of the download area failed: " + error); continue; } @@ -1794,7 +1883,14 @@ void editor_orthophoto::worker() if (m_stop) return; if (!out.ok && error != "superseded") + { ErrorLog(std::string("Editor orthophoto: tile ") + std::to_string(current.key.second) + "_" + std::to_string(current.key.first) + " failed: " + error); + if (error != "cancelled") + { + std::lock_guard lock(m_mutex); + m_last_error = error; + } + } if (out.ok && current.alpha < 255) for (std::size_t i = 3; i < picture.rgba.size(); i += 4) picture.rgba[i] = static_cast((picture.rgba[i] * current.alpha + 127u) / 255u); @@ -1852,7 +1948,8 @@ void editor_orthophoto::draw(glm::mat4 const &ViewProjection, glm::dvec3 const & continue; bool const draped = m_config.drape && !t.heights.empty(); - double const flat = m_config.height; + // with no ground under it yet, laid onto the ground, at the height of the ground found last + double const flat = (m_config.drape && t.flat.has_value()) ? *t.flat : m_config.height; double const lift = m_config.lift; double const ylo = draped ? t.height_min + lift : flat; double const yhi = draped ? t.height_max + lift : flat; @@ -1975,7 +2072,7 @@ bool editor_orthophoto::hurry_tile(tile_key const &Key, int const Distance) t.empty = true; if (t.empty) return false; - auto const level{first_level(t, level_for(Distance, m_config.hires))}; + auto const level{first_level(t, level_for(Distance, hires_reach()))}; t.retry_at = 0.0; if (t.level == level && t.alpha == m_alpha) return false; // already shown as it should be diff --git a/editor/editorOrthophoto.hpp b/editor/editorOrthophoto.hpp index b56052e7e..3a046d8ca 100644 --- a/editor/editorOrthophoto.hpp +++ b/editor/editorOrthophoto.hpp @@ -17,6 +17,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include #include #include @@ -55,6 +56,7 @@ class editor_orthophoto { double north{0.0}; // PUWG 1992 X (northing) of the scenery origin double east{0.0}; // PUWG 1992 Y (easting) of the scenery origin + bool aligned{false}; // the origin aligned by hand in the editor: //$o says so, and //$g of the scenery file doesn't override it int radius{2}; // tiles loaded around the camera tile (Chebyshev distance) float height{0.0f}; // world Y of the layer (with drape: only where no ground was found) float opacity{0.6f}; // 0..1 @@ -116,12 +118,18 @@ class editor_orthophoto void ground_source(ground_query Query) { m_ground = std::move(Query); } void height_source(height_query Query) { m_height = std::move(Query); } // the heights the ground will have where there's none yet, as of the terrain model on its way: a tile laid out before its - // ground comes lies there, or level with the tiles beside, rather than at the height of the layer far below or above + // ground comes lies there, or level with the tiles beside, rather than at the height of the layer far below or above. A tile + // doesn't wait for its ground: with nothing to go by it lies flat at the height of the ground last found, and goes onto the + // ground once it comes void guess_source(height_query Query) { m_guess = std::move(Query); } - // true while the ground under an XZ rectangle (Min/Max are x,z) is still to come. A new tile there waits for it, rather than - // being laid out at the height of the layer; elsewhere it lies on whatever ground there is, or on the guess, as without it - using coming_query = std::function; - void coming_source(coming_query Query) { m_coming = std::move(Query); } + // the 4K photos asked for around the camera by themselves, the view zoomed in (see photo_rules::hires_wanted), besides the setting + // for them; the download area is put into the cache in the products of the setting alone + void zoom_hires(bool const Hires) { m_zoom_hires = Hires; } + bool zoom_hires() const { return m_zoom_hires; } + // the tiles around the camera, and the ones of the area, which failed and wait to be tried again: their rectangles (x, z) + std::vector> failed() const; + // why the last download that failed did, in plain words (an English message, see photo_rules::failure_reason); empty: none failed + std::string last_failure() const; // the area the imagery is downloaded in, a polygon of PUWG 1992 (easting, northing): its tiles go to the disk cache in the // background, the products the settings ask for, and no tile outside it is asked for around the camera. Empty: no area, the // tiles around the camera as before @@ -194,6 +202,8 @@ class editor_orthophoto bool hires{false}; std::string service; std::string layers; + + bool operator==(source const &) const = default; }; struct job @@ -242,6 +252,7 @@ class editor_orthophoto float height_max{0.0f}; std::vector surface; // upward facing ground triangles clipped to the tile (3 vertices each) bool guessed{false}; // no ground under it when sampled: the heights guessed, from the terrain model or the tiles beside + std::optional flat; // nothing at all to go by when sampled: flat at the height of the ground found last // the points of the grid of the heights the ground didn't answer when sampled (rows north->south, as heights): filled from // the points beside or guessed, and sampled again once the ground comes under one of them std::vector unanswered; @@ -253,7 +264,10 @@ class editor_orthophoto bool scene_dirty{true}; // the shape needs to be rebuilt }; - static int level_for(int Distance, bool Hires); + static int level_for(int Distance, int HiresReach); + // how many tiles away from the camera the 4K photos reach: 1 with the setting for them, the 3 x 3 tiles around it; 0 by the zoom + // alone, the tile under the camera, about 85 MB of the memory of the graphics card rather than 0.75 GB; -1 none + int hires_reach() const; // the level a tile is asked for at first: a tile showing nothing yet takes the standard image before the 4K one static int first_level(tile const &Tile, int Level); // the tile is outside the download area set, which then has no imagery asked for it @@ -270,8 +284,6 @@ class editor_orthophoto void sample_ground(tile_key const &Key, tile &Tile); // the ground has come under a point of the tile which had none when it was sampled bool ground_came(tile_key const &Key, tile const &Tile) const; - // a new tile isn't asked for yet: the ground under it is still to come - bool waits_for_ground(tile_key const &Key) const; // the height of the ground sampled under a tile beside Own at the point, the tiles with guessed heights left out bool sampled_height(tile_key const &Own, double const X, double const Z, float &Height) const; void build_shape(tile_key const &Key, tile &Tile); @@ -291,7 +303,9 @@ class editor_orthophoto ground_query m_ground; height_query m_height; height_query m_guess; - coming_query m_coming; + bool m_zoom_hires{false}; + // the height of the ground found last under a tile: a tile with no ground under it yet lies there + std::optional m_known_height; scene::basic_region *m_region{nullptr}; // region the scene geometry was put into std::multimap m_free_chunks; // geometry chunks by capacity, reused between tiles // the scene opacity is baked into the textures, which means reading the tiles again; applied once the value settles @@ -316,7 +330,10 @@ class editor_orthophoto std::vector m_prefetch; std::size_t m_prefetch_next{0}; std::set m_prefetch_failed; + // of the ones which failed, how many times they did and when they're tried again + std::map> m_prefetch_retry; std::set m_prefetch_cached; + std::string m_last_error; // the last failure, as the service or the connection told it bool m_prefetch_paused{false}; source m_prefetch_source; std::uint32_t m_prefetch_generation{0}; diff --git a/editor/editorPhotoRules.cpp b/editor/editorPhotoRules.cpp new file mode 100644 index 000000000..67b092b50 --- /dev/null +++ b/editor/editorPhotoRules.cpp @@ -0,0 +1,283 @@ +/* +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/editorPhotoRules.hpp" +#include "editor/editorGeometry.hpp" + +#include +#include +#include +#include +#include +#include + +namespace photo_rules +{ + +namespace +{ + +using square = std::pair; + +bool known(std::optional const &Origin) +{ + return Origin.has_value() && (Origin->x != 0.0 || Origin->y != 0.0); +} + +// the squares 4-connected, the holes in them filled; empty when they aren't one piece +std::set filled(std::set const &Squares) +{ + if (Squares.empty()) + return {}; + int constexpr steps[4][2]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + { + std::set reached{*Squares.begin()}; + std::deque next{*Squares.begin()}; + while (false == next.empty()) + { + auto const at{next.front()}; + next.pop_front(); + for (auto const &step : steps) + if (square const beside{at.first + step[0], at.second + step[1]}; Squares.contains(beside) && reached.insert(beside).second) + next.push_back(beside); + } + if (reached.size() != Squares.size()) + return {}; + } + // the empty squares reached from beyond the edge of the box around them, the others are holes + square low{*Squares.begin()}; + square high{*Squares.begin()}; + for (auto const &at : Squares) + { + low = {std::min(low.first, at.first), std::min(low.second, at.second)}; + high = {std::max(high.first, at.first), std::max(high.second, at.second)}; + } + low = {low.first - 1, low.second - 1}; + high = {high.first + 1, high.second + 1}; + std::set outside{low}; + std::deque next{low}; + while (false == next.empty()) + { + auto const at{next.front()}; + next.pop_front(); + for (auto const &step : steps) + { + square const beside{at.first + step[0], at.second + step[1]}; + if (beside.first < low.first || beside.second < low.second || beside.first > high.first || beside.second > high.second || Squares.contains(beside)) + continue; + if (outside.insert(beside).second) + next.push_back(beside); + } + } + std::set result; + for (int y = low.second; y <= high.second; ++y) + for (int x = low.first; x <= high.first; ++x) + if (false == outside.contains({x, y})) + result.insert({x, y}); + return result; +} + +std::vector outline_of(std::set const &Squares, double const Size, double const Inset) +{ + // the edges along which a square of the set meets one which isn't, the set on their left: counter-clockwise around it + std::map edges; // from a corner to the next one + auto const add = [&](square const From, square const To) { edges[From] = To; }; + for (auto const &[x, y] : Squares) + { + if (false == Squares.contains({x, y - 1})) + add({x, y}, {x + 1, y}); + if (false == Squares.contains({x + 1, y})) + add({x + 1, y}, {x + 1, y + 1}); + if (false == Squares.contains({x, y + 1})) + add({x + 1, y + 1}, {x, y + 1}); + if (false == Squares.contains({x - 1, y})) + add({x, y + 1}, {x, y}); + } + if (edges.empty()) + return {}; + std::vector corners; + auto const first{edges.begin()->first}; + auto at{first}; + do + { + corners.push_back(at); + auto const found{edges.find(at)}; + if (found == edges.end() || corners.size() > edges.size()) + return {}; + at = found->second; + } while (at != first); + // one outline goes around all of them, there are no holes + if (corners.size() != edges.size()) + return {}; + std::vector result; + auto const count{corners.size()}; + for (std::size_t i = 0; i < count; ++i) + { + auto const &previous{corners[(i + count - 1) % count]}; + auto const &here{corners[i]}; + auto const &next{corners[(i + 1) % count]}; + glm::dvec2 const in{static_cast(here.first - previous.first), static_cast(here.second - previous.second)}; + glm::dvec2 const out{static_cast(next.first - here.first), static_cast(next.second - here.second)}; + // the corners along a straight edge left out + if (in == out) + continue; + // the edges in and out moved inwards, onto their left: the corner where they meet then + glm::dvec2 const left_in{-in.y, in.x}; + glm::dvec2 const left_out{-out.y, out.x}; + result.push_back(glm::dvec2{here.first * Size, here.second * Size} + (left_in + left_out) * Inset); + } + return result; +} + +} // namespace + +origin_choice choose_origin(std::optional const &Georeference, std::optional const &Mark, std::optional const &Remembered, bool const MarkAligned) +{ + origin_choice result; + if (MarkAligned && known(Mark)) + { + result.origin = *Mark; + result.from = origin_source::mark; + return result; + } + if (known(Georeference)) + { + result.origin = *Georeference; + result.from = origin_source::georeference; + for (auto const *other : {&Mark, &Remembered}) + if (known(*other)) + result.differs = std::max(result.differs, glm::distance(**other, *Georeference)); + return result; + } + if (known(Mark)) + { + result.origin = *Mark; + result.from = origin_source::mark; + } + else if (known(Remembered)) + { + result.origin = *Remembered; + result.from = origin_source::remembered; + } + return result; +} + +switched photo_on(std::optional const &User, std::optional const &Scenery) +{ + if (User.has_value()) + return {*User, true}; + if (Scenery.has_value()) + return {*Scenery, true}; + return {false, false}; +} + +double retry_delay(int const Failures) +{ + if (Failures <= 0) + return 0.0; + return std::min(retry_most, 5.0 * std::pow(2.0, std::min(Failures, 20) - 1)); +} + +bool hires_wanted(double const MetresPerPixel, bool const Now) +{ + if (MetresPerPixel <= 0.0) + return false; + return MetresPerPixel <= (Now ? hires_until : hires_from); +} + +std::string connection_reason(std::string const &Error) +{ + auto const has = [&](char const *Text) { return Error.find(Text) != std::string::npos; }; + // the errors of WinHTTP (12xxx) and of curl, and the status of HTTP + if (has("12007") || has("12005") || has("resolve")) + return "no connection to the internet: the name of the server isn't found"; + if (has("12002") || has("imed out") || has("Timeout")) + return "the server didn't answer in time"; + if (has("12029") || has("12030") || has("12031") || has("12152") || has("12017") || has("onnect")) + return "the server can't be reached"; + if (has("12175") || has("12157") || has("12045") || has("12037") || has("12038") || has("12044") || has("SSL") || has("certificate")) + return "the secure connection to the server failed"; + if (has("HTTP 5")) + return "the server is busy or out of order"; + if (has("HTTP 4")) + return "the server refused the request"; + if (has("WinHTTP")) + return "the download failed"; + return {}; +} + +std::string failure_reason(std::string const &Error) +{ + auto const has = [&](char const *Text) { return Error.find(Text) != std::string::npos; }; + if (has("HTTP 404")) + return "the server has no photo of the place"; + if (auto const connection{connection_reason(Error)}; false == connection.empty()) + return connection; + if (has("unexpected response")) + return "the server sent an error in place of the photo"; + if (has("decoding")) + return "the photo came damaged"; + if (has("too large") || has("larger")) + return "the photo came too large"; + return "the download failed"; +} + +std::vector squares_outline(std::vector> const &Squares, double const Size, double const Inset) +{ + auto const whole{filled({Squares.begin(), Squares.end()})}; + if (whole.empty()) + return {}; + return outline_of(whole, Size, Inset); +} + +growth grow_area(std::vector const &Polygon, glm::dvec2 const &Point, double const Tile, int const Margin, int const Reach, std::size_t const Limit) +{ + growth result; + if (Polygon.size() < 3 || Tile <= 0.0) + return result; + auto const found{geometry::squares_in(Polygon, Tile)}; + std::set const area{found.begin(), found.end()}; + result.total = area.size(); + if (area.empty()) + return result; + square const centre{static_cast(std::floor(Point.x / Tile)), static_cast(std::floor(Point.y / Tile))}; + // far from the area the point is elsewhere, not at its edge: the area isn't carried after it + auto nearest{std::numeric_limits::max()}; + for (auto const &at : area) + nearest = std::min(nearest, std::max(std::abs(at.first - centre.first), std::abs(at.second - centre.second))); + if (nearest > Margin) + return result; + auto grown{area}; + for (int dy = -Reach; dy <= Reach; ++dy) + for (int dx = -Reach; dx <= Reach; ++dx) + grown.insert({centre.first + dx, centre.second + dy}); + if (grown.size() == area.size()) + return result; + auto const whole{filled(grown)}; + if (whole.empty()) + return result; + if (whole.size() > Limit) + { + result.limited = true; + return result; + } + // the polygon touches only the squares of the grid it's drawn over, a little inside their edges + result.polygon = outline_of(whole, Tile, std::min(1.0, Tile * 0.01)); + if (result.polygon.size() < 3) + { + result.polygon.clear(); + return result; + } + result.total = whole.size(); + result.added = whole.size() - area.size(); + return result; +} + +} // namespace photo_rules diff --git a/editor/editorPhotoRules.hpp b/editor/editorPhotoRules.hpp new file mode 100644 index 000000000..88b742ea8 --- /dev/null +++ b/editor/editorPhotoRules.hpp @@ -0,0 +1,88 @@ +/* +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 +#include + +// the rules the aerial photo of the editor (editor_orthophoto) goes by, standing on their own: which origin of the scenery is +// taken, whether the photo is on, how the download area grows, when a tile is tried again, when the 4K photos come, and what a +// download that failed is told as +namespace photo_rules +{ + +// where the origin of the scenery comes from +enum class origin_source +{ + none, + georeference, // //$g of the scenery file, as Rainsted and the wizard write it + mark, // //$o of the scenery, written by the editor + remembered // the settings of the editor, per scenery +}; + +struct origin_choice +{ + glm::dvec2 origin{0.0}; // northing, easting + origin_source from{origin_source::none}; + // with //$g, how far the other origin known (the one remembered or the one of //$o) lies from it, the farther; 0 when none + double differs{0.0}; +}; + +// //$g goes first, as the file of the scenery says where it lies: an origin remembered by the editor for a scenery of the same name +// elsewhere, or one written into //$o with it, can't move it. Without //$g the editor's own //$o, then the one remembered. A //$o the +// user aligned by hand (MarkAligned, the word aligned in it) goes before //$g: the user moved the photo off it on purpose +origin_choice choose_origin(std::optional const &Georeference, std::optional const &Mark, std::optional const &Remembered, bool const MarkAligned = false); +// the origins further apart than that are told of +double constexpr origin_tolerance{1.0}; + +// the photo on or off: as the user left it in this scenery, else as the scenery keeps it (//$o on, the wizard writes it on), +// else off. Chosen: the terrain model doesn't turn it on by itself +struct switched +{ + bool on{false}; + bool chosen{false}; +}; +switched photo_on(std::optional const &User, std::optional const &Scenery); + +// a tile which failed is tried again after that many seconds, doubling from 5 s up to 5 min, and never given up for good +double retry_delay(int Failures); +double constexpr retry_most{300.0}; + +// the 4K photos (6 cm/px) by themselves when the view is zoomed in that far, m a pixel; off again only once zoomed out past the +// second value, so they don't come and go at the edge +double constexpr hires_from{0.15}; +double constexpr hires_until{0.25}; +bool hires_wanted(double MetresPerPixel, bool Now); + +// what a download that failed is told as, in plain words (an English message to be translated); the raw error is in the log +std::string failure_reason(std::string const &Error); +// of these, the failures of the connection and of the server alone, whatever was downloaded (the terrain model too); empty: the +// error isn't one of them +std::string connection_reason(std::string const &Error); + +// the download area grown to where the camera or the laying goes: the tiles of the polygon (PUWG easting, northing) and the square of +// Reach tiles around the point, as one outline along the grid of the tiles, its holes filled. Only when the point is within Margin +// tiles of the tiles of the area and the square isn't in it already; not past Limit tiles in all +struct growth +{ + std::vector polygon; // empty: the area stays as it is + std::size_t added{0}; // tiles + std::size_t total{0}; // tiles of the area grown, or of the area as it is when it can't grow + bool limited{false}; // it would grow past the limit +}; +growth grow_area(std::vector const &Polygon, glm::dvec2 const &Point, double Tile, int Margin, int Reach, std::size_t Limit); +// the outline of a set of squares of a grid of the size, 4-connected and with no holes, a little inside their edges (Inset m), so +// the squares it touches are the ones of the set and none beside; empty when the set isn't one piece +std::vector squares_outline(std::vector> const &Squares, double Size, double Inset); + +} // namespace photo_rules diff --git a/editor/editorRecords.cpp b/editor/editorRecords.cpp index 2bdf1825d..0795f1900 100644 --- a/editor/editorRecords.cpp +++ b/editor/editorRecords.cpp @@ -871,7 +871,7 @@ std::string camera_line(camera const &Camera) // //$o ------------------------------------------------------------------------------------------------------------------------------- // format of the line: on <0/1> [origin ] -std::string orthophoto_line(bool const Enabled, bool const Origin, double const North, double const East) +std::string orthophoto_line(bool const Enabled, bool const Origin, double const North, double const East, bool const Aligned) { // how the imagery is shown is the user's, kept with the editor (EditorSettings); the scenery keeps whether it's on and where the // scenery lies @@ -882,11 +882,13 @@ std::string orthophoto_line(bool const Enabled, bool const Origin, double const { std::snprintf(text, sizeof(text), " origin %.2f %.2f", North, East); result += text; + if (Aligned) + result += " aligned"; } return result; } -bool read_orthophoto(std::string const &Line, bool &Enabled, double &North, double &East) +bool read_orthophoto(std::string const &Line, bool &Enabled, double &North, double &East, bool *Aligned) { std::istringstream words{Line}; std::string key; @@ -898,6 +900,8 @@ bool read_orthophoto(std::string const &Line, bool &Enabled, double &North, doub words >> Enabled; else if (key == "origin") words >> North >> East; + else if (key == "aligned" && Aligned != nullptr) + *Aligned = true; else continue; if (words.fail()) diff --git a/editor/editorRecords.hpp b/editor/editorRecords.hpp index 3cf25b761..e6e9dce29 100644 --- a/editor/editorRecords.hpp +++ b/editor/editorRecords.hpp @@ -136,8 +136,10 @@ std::string camera_line(camera const &Camera); // //$o, whether the orthophoto is on and, with Origin, where the scenery lies on the map, PUWG 1992 northing and easting; the settings // the older sceneries carry besides are passed over. returns: false if the line says none of it -std::string orthophoto_line(bool const Enabled, bool const Origin, double const North, double const East); -bool read_orthophoto(std::string const &Line, bool &Enabled, double &North, double &East); +// Aligned: the origin was aligned by hand in the editor, which //$g of the scenery file doesn't override; a release before it passes the +// word over +std::string orthophoto_line(bool const Enabled, bool const Origin, double const North, double const East, bool const Aligned = false); +bool read_orthophoto(std::string const &Line, bool &Enabled, double &North, double &East, bool *Aligned = nullptr); // //$z, the area the editor downloads the orthophoto and the terrain model in, PUWG 1992 easting northing: none, below 3 points std::vector area_lines(std::vector const &Polygon); diff --git a/editor/editorSettings.cpp b/editor/editorSettings.cpp index 380a0da36..34f90a5db 100644 --- a/editor/editorSettings.cpp +++ b/editor/editorSettings.cpp @@ -49,6 +49,7 @@ template void parse(std::string const &Text, Type_ &Out) } const std::string origin_prefix{"ortho_origin@"}; +const std::string on_prefix{"ortho_on@"}; const std::string offset_prefix{"nmt_offset@"}; // settings are whitespace separated key/value pairs, so the scenery name used in the key can't contain spaces @@ -107,7 +108,12 @@ bool editorSettings::load() std::ifstream stream(path); if (!stream.is_open()) return false; + read(stream); + return true; +} +void editorSettings::read(std::istream &stream) +{ std::string key, value; while (stream >> key >> value) { @@ -129,8 +135,10 @@ bool editorSettings::load() parse(value, m_orthophoto.lift); else if (key == "ortho_in_scene") m_orthophoto.in_scene = (value == "1"); - else if (key == "ortho_on") - m_orthophoto.on = (value == "1"); + // the photo turned on or off for every scenery, as an older editor kept it, is left behind: it's the scenery's now, and a + // global off hid the photo of the sceneries the wizard made with it on + else if (key.rfind(on_prefix, 0) == 0) + m_orthophoto_on[key] = (value == "1"); else if (key == "platform_edge") parse(value, m_platform_edge); else if (key == "signal_offset") @@ -238,7 +246,6 @@ bool editorSettings::load() m_terrain_model_offsets[key] = {offset, value.substr(separator + 1) == "1"}; } } - return true; } bool editorSettings::save() @@ -253,7 +260,12 @@ bool editorSettings::save() ErrorLog("failed to save editor settings"); return false; } + write(stream); + return true; +} +void editorSettings::write(std::ostream &stream) const +{ stream.imbue(std::locale::classic()); stream << "movement_scheme " << scheme_to_string(m_movement) << "\n"; stream << "ortho_radius " << m_orthophoto.radius << "\n"; @@ -264,8 +276,8 @@ bool editorSettings::save() stream << "ortho_drape " << (m_orthophoto.drape ? 1 : 0) << "\n"; stream << "ortho_lift " << m_orthophoto.lift << "\n"; stream << "ortho_in_scene " << (m_orthophoto.in_scene ? 1 : 0) << "\n"; - if (m_orthophoto.on.has_value()) - stream << "ortho_on " << (*m_orthophoto.on ? 1 : 0) << "\n"; + for (auto const &[key, on] : m_orthophoto_on) + stream << key << " " << (on ? 1 : 0) << "\n"; stream << "platform_edge " << m_platform_edge << "\n"; stream << "signal_offset " << m_signal_offset << "\n"; stream << "signal_plate_look " << m_signal_plate_look << "\n"; @@ -297,7 +309,25 @@ bool editorSettings::save() stream << origin.first << " " << origin.second.first << ";" << origin.second.second << "\n"; for (auto const &offset : m_terrain_model_offsets) stream << offset.first << " " << offset.second.first << ";" << (offset.second.second ? 1 : 0) << "\n"; - return true; +} + +std::optional editorSettings::orthophoto_on(std::string const &Scenery) const +{ + auto const lookup{m_orthophoto_on.find(scenery_key(on_prefix, Scenery))}; + if (lookup == m_orthophoto_on.end()) + return std::nullopt; + return lookup->second; +} + +void editorSettings::orthophoto_on(std::string const &Scenery, bool const On) +{ + m_orthophoto_on[scenery_key(on_prefix, Scenery)] = On; +} + +void editorSettings::orthophoto_forget(std::string const &Scenery) +{ + m_orthophoto_on.erase(scenery_key(on_prefix, Scenery)); + m_orthophoto_origins.erase(scenery_key(origin_prefix, Scenery)); } bool editorSettings::orthophoto_origin(std::string const &Scenery, double &North, double &East) const diff --git a/editor/editorSettings.hpp b/editor/editorSettings.hpp index 62720d534..c66bba7f5 100644 --- a/editor/editorSettings.hpp +++ b/editor/editorSettings.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -21,12 +22,15 @@ public: bool load(); bool save(); + // the settings as the file keeps them, read and written + void read(std::istream &Stream); + void write(std::ostream &Stream) const; movement_scheme movement() const { return m_movement; } void movement(movement_scheme scheme) { m_movement = scheme; } - // geoportal orthophoto layer (editor_orthophoto), how it's shown and whether it's on; the origin is kept per scenery, and the - // scenery keeps it too + // geoportal orthophoto layer (editor_orthophoto), how it's shown; whether it's on and the origin are kept per scenery, and the + // scenery keeps them too struct orthophoto_settings { int radius{2}; @@ -37,13 +41,16 @@ public: bool drape{true}; float lift{0.05f}; bool in_scene{false}; - // turned on or off by the user, for every scenery; none chosen yet: as the scenery keeps it, or the terrain model turns it on - std::optional on; }; orthophoto_settings &orthophoto() { return m_orthophoto; } // PUWG 1992 northing/easting of the scenery's (0,0,0) point; false when none was stored bool orthophoto_origin(std::string const &Scenery, double &North, double &East) const; void orthophoto_origin(std::string const &Scenery, double North, double East); + // the photo turned on or off by the user in the scenery; none: not chosen there yet, as the scenery keeps it + std::optional orthophoto_on(std::string const &Scenery) const; + void orthophoto_on(std::string const &Scenery, bool On); + // what's kept of the scenery's photo, its choice and its origin, forgotten + void orthophoto_forget(std::string const &Scenery); // the WMS layers the user added under the scenery, over the orthophoto, the top one first: the address of the service, the // layers asked of it, its title and terms as its capabilities gave them. Kept with the editor, not with the scenery struct wms_layer @@ -140,6 +147,7 @@ private: orthophoto_settings m_orthophoto; camera_settings m_camera; std::map> m_orthophoto_origins; + std::map m_orthophoto_on; std::vector m_wms_layers; double m_platform_edge{1.725}; double m_signal_offset{2.25}; diff --git a/lang/editor_pl.po b/lang/editor_pl.po index a7e20b842..a0b7b1eae 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -6060,24 +6060,12 @@ msgstr ". To za dużo: położenie scenerii jest pewnie złe, sprawdź ortofotom msgid "There's terrain there already" msgstr "Tam już jest teren" -msgid "Terrain for the new scenery###nmtoffer" -msgstr "Teren dla nowej scenerii###nmtoffer" - -msgid "The scenery has no terrain yet. Load the terrain model and the aerial photos around here? The tracks laid then go along the real ground." -msgstr "Sceneria nie ma jeszcze terenu. Wczytać teren z map (NMT) i ortofotomapę wokół tego miejsca? Tory układane potem pójdą po nim" - msgid "Where is it? PUWG 1992 of this point, X to the north and Y to the east, as geoportal.gov.pl gives them:" msgstr "Gdzie to jest? Współrzędne PUWG 1992 tego miejsca, X na północ i Y na wschód, jak podaje geoportal.gov.pl:" -msgid "Load the terrain" -msgstr "Wczytaj teren z map" - msgid "Without terrain" msgstr "Bez terenu" -msgid "Ask with every new scenery" -msgstr "Pytaj przy każdej nowej scenerii" - msgid "Fetching the terrain model under the tracks: %zu squares left" msgstr "Pobieranie NMT pod torami: zostało %zu kwadratów" @@ -7290,9 +7278,6 @@ msgstr "pochylenie" msgid "cant" msgstr "przechyłka" -msgid "The terrain model and the aerial photos of geoportal.gov.pl around here; the tracks go along the real ground" -msgstr "Teren z map (NMT) i ortofotomapa z geoportal.gov.pl wokół tego miejsca; tory pójdą po prawdziwym terenie" - msgid "Type where the scenery lies first" msgstr "Najpierw wpisz, gdzie leży sceneria" @@ -10077,9 +10062,6 @@ msgstr "Kwadrat 2 km wokół kamery" msgid "The squares of the model the service didn't give are asked for again; it happens by itself every 15 s too" msgstr "Ponownie prosi serwis o brakujące fragmenty NMT; samo dzieje się to też co 15 s" -msgid "Pick where the scenery lies first" -msgstr "Najpierw wskaż, gdzie leży sceneria" - msgid "The scenery has no terrain yet" msgstr "Sceneria nie ma jeszcze terenu" @@ -15379,3 +15361,118 @@ msgstr "Szyny##setuplook" msgid "Trackbed##setuplook" msgstr "Podsypka##setuplook" +# scenery editor: the aerial photo without fuss + +msgid "Photo: laid out by //$g of the scenery file; the origin kept before was %.0f m off it" +msgstr "Zdjęcie: ułożone według //$g pliku scenerii; zapamiętany wcześniej początek układu był o %.0f m dalej" + +msgid "Opened again, the scenery is laid out by //$g of its file" +msgstr "Po ponownym otwarciu sceneria będzie ułożona według //$g swojego pliku" + +msgid "With the view zoomed in to 15 cm a pixel they come by themselves next to the camera" +msgstr "Przy zbliżeniu widoku do 15 cm na piksel włączają się same przy kamerze" + +msgid "Photo: the download area is at its limit of %zu tiles; draw it again where it's needed, in Map > Orthophoto settings..." +msgstr "Zdjęcie: obszar pobierania doszedł do limitu %zu kafli; narysuj go na nowo tam, gdzie trzeba, w Mapa > Ustawienia ortofotomapy..." + +msgid "Photo: the download area grown by %zu tile at its edge, %zu in all" +msgid_plural "Photo: the download area grown by %zu tiles at its edge, %zu in all" +msgstr[0] "Zdjęcie: obszar pobierania powiększony przy krawędzi o %zu kafel, razem %zu" +msgstr[1] "Zdjęcie: obszar pobierania powiększony przy krawędzi o %zu kafle, razem %zu" +msgstr[2] "Zdjęcie: obszar pobierania powiększony przy krawędzi o %zu kafli, razem %zu" + +msgid "Photos: %zu tile not downloaded" +msgid_plural "Photos: %zu tiles not downloaded" +msgstr[0] "Zdjęcia: %zu kafla nie pobrano" +msgstr[1] "Zdjęcia: %zu kafli nie pobrano" +msgstr[2] "Zdjęcia: %zu kafli nie pobrano" + +msgid "They're marked red in the view and tried again by themselves, later and later, up to 5 min apart" +msgstr "Są zaznaczone w widoku na czerwono i ponawiane same, coraz rzadziej, najwyżej co 5 min" + +msgid "no connection to the internet: the name of the server isn't found" +msgstr "brak połączenia z internetem: nie znaleziono nazwy serwera" + +msgid "the server didn't answer in time" +msgstr "serwer nie odpowiedział na czas" + +msgid "the server can't be reached" +msgstr "nie można połączyć się z serwerem" + +msgid "the secure connection to the server failed" +msgstr "nie udało się nawiązać bezpiecznego połączenia z serwerem" + +msgid "the server is busy or out of order" +msgstr "serwer jest przeciążony albo nie działa" + +msgid "the server has no photo of the place" +msgstr "serwer nie ma zdjęcia tego miejsca" + +msgid "the server refused the request" +msgstr "serwer odrzucił żądanie" + +msgid "the server sent an error in place of the photo" +msgstr "serwer przysłał błąd zamiast zdjęcia" + +msgid "the photo came damaged" +msgstr "zdjęcie przyszło uszkodzone" + +msgid "the photo came too large" +msgstr "zdjęcie przyszło za duże" + +msgid "the download failed" +msgstr "pobieranie się nie udało" + +msgid "Retry##photos" +msgstr "Ponów##photos" + +msgid "The aerial photo shown or hidden, kept with the scenery" +msgstr "Zdjęcie lotnicze pokazane albo ukryte, zapamiętane dla scenerii" + +msgid "The photos of what the camera sees downloaded before the rest" +msgstr "Zdjęcia tego, co widzi kamera, pobierane przed resztą" + +msgid "The terrain loaded by itself as the first track of a new scenery is laid" +msgstr "Teren wczytywany sam przy układaniu pierwszego toru nowej scenerii" + +msgid "In the background, where the scenery lies on the map; the panel of the laying tells it, with the button to go without it" +msgstr "W tle, tam gdzie sceneria leży na mapie; mówi o tym panel układania, z przyciskiem, by obejść się bez terenu" + +msgid "Photo" +msgstr "Zdjęcie" + +msgid "The aerial photo of geoportal.gov.pl under the scenery, shown or hidden in this scenery" +msgstr "Zdjęcie lotnicze z geoportal.gov.pl pod scenerią, pokazane albo ukryte w tej scenerii" + +msgid "How much the photo covers the scenery under it" +msgstr "Jak mocno zdjęcie przykrywa scenerię pod nim" + +msgid "Without terrain: the tracks lie on the flat plane; the terrain can be loaded later in Terrain (F5)" +msgstr "Bez terenu: tory leżą na płaszczyźnie; teren możesz wczytać później w dziale Teren (F5)" + +msgid "Terrain:" +msgstr "Teren:" + +msgid "Terrain: loading..." +msgstr "Teren: wczytuję…" + +msgid "The terrain model of geoportal.gov.pl around here, in the background: the tracks laid meanwhile go onto it once it's in, the photo lies flat till then" +msgstr "Teren z map (NMT) z geoportal.gov.pl wokół tego miejsca, w tle: tory ułożone w tym czasie trafią na niego, gdy dojdzie, a zdjęcie do tego czasu leży płasko" + +msgid "Terrain: the scenery isn't placed on the map" +msgstr "Teren: sceneria nie leży na mapie" + +msgid "Place it..." +msgstr "Wskaż miejsce…" + +msgid "In Terrain (F5): the middle of the scenery clicked on the map, then its terrain and photos can be loaded" +msgstr "W dziale Teren (F5): kliknij środek scenerii na mapie, potem możesz wczytać jej teren i zdjęcia" + +msgid "Photos" +msgstr "Zdjęcia" + +msgid "The aerial photos of geoportal.gov.pl, the same as under the scenery in the editor, once the map is zoomed in" +msgstr "Zdjęcia lotnicze z geoportal.gov.pl, te same co pod scenerią w edytorze, po przybliżeniu mapy" + +msgid "Zoom in for the photos" +msgstr "Przybliż, by zobaczyć zdjęcia" diff --git a/launcher/scenery_wizard.cpp b/launcher/scenery_wizard.cpp index f982223ff..22b1e257b 100644 --- a/launcher/scenery_wizard.cpp +++ b/launcher/scenery_wizard.cpp @@ -267,7 +267,8 @@ void ui::scenerywizard_panel::render_form() wrapped(error_colour, m_status); ImGui::Spacing(); - wrapped_disabled(STR("Map: (c) OpenStreetMap contributors, served by geoportal.gov.pl")); + // whose the picture of the map is: the photos of GUGiK zoomed in, the base map of OpenStreetMap further out + wrapped_disabled(m_map.photos_shown() ? STR("Imagery: GUGiK, geoportal.gov.pl") : STR("Map: (c) OpenStreetMap contributors, served by geoportal.gov.pl")); } void ui::scenerywizard_panel::render_place() diff --git a/tests/editor/README.md b/tests/editor/README.md index f9ffa30b3..0c7325b6f 100644 --- a/tests/editor/README.md +++ b/tests/editor/README.md @@ -85,7 +85,8 @@ The expectations (`expect ...`), each logged as `SELFTEST PASS ` or | `expect refusal ` | what the cursor tells while it's shown (a refusal or a warning) holds the text, or doesn't | | `expect statusline ` | the status line of the editor (also shown in the layers window) holds the text, or doesn't | | `expect laycursor ` | the point the line of the lay tool goes to from its last point clicked, in the plan | -| `expect terrain <0\|1>` | the terrain model is made under the tracks as they're laid / around the camera within the download area / the question of the terrain of a new scenery is open | +| `expect terrain <0\|1>` | the terrain model is made under the tracks as they're laid / around the camera within the download area / the line of the terrain of a new scenery is shown in the panel of the laying / its terrain is being loaded in the background / a window asking (a modal popup) is open | +| `expect photo <0\|1>` | the aerial photo is shown / it was turned on or off by the user or the scenery (the terrain model leaves it be) / the 4K photos are asked for by themselves, the view zoomed in / the tracks were drawn over the view in the last frame (the top view) | | `expect laypanel ` | the state of the panel of the lay tool, as the step at its top tells it, and whether its settings are shown or folded to the line of their summary | | `expect switchpanel ` | the state of the panel of the switch tool, as the step at its top tells it | | `expect steps ` | the steps of the history Ctrl+Z takes back, the ones made together counted once: none made by a change refused | @@ -163,7 +164,7 @@ and the first steps in the editor (P1 of the plan of the editor's interface): | --- | --- | | `fs_1_preview_over_panel` | while the pointer is over a panel the line the lay tool shows stays as it was, no leg following the pointer under the panel; back over the view it follows again | | `fs_2_switch_refusal_cleared` | a switch refused is told in the panel of the switch tool and by the cursor, not in the status line as well, and what it told goes once the next switch is laid | -| `fs_3_without_terrain` | the answer Without terrain to the question of the terrain of a new scenery turns off the terrain made of the terrain model by itself: under the tracks as they're laid and around the camera within the download area | +| `fs_3_without_terrain` | Without terrain, the button of the line of the terrain of a new scenery (no question in a window any more), turns off the terrain made of the terrain model by itself: under the tracks as they're laid and around the camera within the download area | and the consistency of the interface of the editor (P2 of the plan of the editor's interface): @@ -204,6 +205,15 @@ and the rest of the editing on the model (E4b), with the setting on: | `m_13_parallel` | the parallel tool beside a line of the model lays a line of the model, its arc concentric at the spacing, one step | | `m_14_layer_origin` | a line of the model saved into a file under an origin and opened again: nothing taken for changed by hand, also after a second save | +and the aerial photo with no fuss (stage 3 of laying the tracks over the aerial photos): + +| case | checks | +| --- | --- | +| `ph_1_photo_per_scenery` | the photo turned on or off is the scenery's, kept with the editor: off over the wizard's `//$o on 1` after a reopen, on again after the next | +| `ph_2_terrain_no_question` | the first L in a new scenery placed on the map asks nothing: the terrain loads in the background, the line in the panel of the laying tells it, Without terrain stops it | +| `ph_3_tracks_over_photo` | in the top view zoomed in under 0.5 m a pixel the tracks are drawn over the photo; without the photo they aren't | +| `ph_4_toolbar_toggle` | the photo button of the toolbar of the tracks and ~ turn the photo on and off as the user's choice, the terrain of the model doesn't turn it on again; F1 lists ~ | + and the assumptions of the project and the norms enforced when laying (`editor-project-setup.md`): | case | checks | diff --git a/tests/editor/cases/fs_3_without_terrain.txt b/tests/editor/cases/fs_3_without_terrain.txt index b2b7c70d7..1a1711857 100644 --- a/tests/editor/cases/fs_3_without_terrain.txt +++ b/tests/editor/cases/fs_3_without_terrain.txt @@ -1,8 +1,9 @@ -# First steps: the answer Without terrain to the question of the terrain of a new empty scenery turns off the terrain made of the +# First steps: Without terrain, the button of the line of the terrain of a new empty scenery, turns off the terrain made of the # terrain model by itself: under the tracks as they're laid (the checkbox of the layers) and around the camera within the download # area (the checkbox of the terrain panel), so nothing of the model is asked for. For this scenery only: the settings, kept for the -# sceneries opened later, stay as they were (nmtquestion flat fails the run if they changed). The question asked as the first L -# in an empty scenery asks it (the fixture has tracks, so it isn't asked by itself) +# sceneries opened later, stay as they were (nmtquestion flat fails the run if they changed). The line is shown as the first L in an +# empty scenery shows it, in place of the question asked in a window before (the fixture has tracks, so it isn't shown by itself); the +# fixture isn't placed on the map, so nothing is loaded meanwhile wait 5 nmtsettings grow 1 wait 2 @@ -10,6 +11,7 @@ expect terrain grow 1 nmtquestion open wait 3 expect terrain prompt 1 +expect terrain question 0 nmtquestion flat wait 3 expect terrain prompt 0 diff --git a/tests/editor/cases/ph_1_photo_per_scenery.txt b/tests/editor/cases/ph_1_photo_per_scenery.txt new file mode 100644 index 000000000..9bb754db0 --- /dev/null +++ b/tests/editor/cases/ph_1_photo_per_scenery.txt @@ -0,0 +1,33 @@ +# The aerial photo is the scenery's. The wizard's //$o on 1 with nothing chosen in the scenery: on. Turned off with the button of the +# toolbar it stays off when the scenery is opened again, though the scenery says on; turned on again, on after a reopen. Kept with +# the editor, not with the save: the scenery isn't saved. The fixture isn't placed on the map, so no photo is downloaded +#> append //$o on 1 +wait 5 +# what an earlier run kept of the photo of this scenery forgotten +orthophoto forget +quit +#> reopen +wait 5 +expect photo on 1 +expect photo chosen 1 +tap f3 +wait 2 +toolbar ~ +wait 2 +expect photo on 0 +quit +#> reopen +wait 5 +# the user's off goes over the scenery's on +expect photo on 0 +tap f3 +wait 2 +toolbar ~ +wait 2 +expect photo on 1 +quit +#> reopen +wait 5 +expect photo on 1 +orthophoto forget +quit diff --git a/tests/editor/cases/ph_2_terrain_no_question.txt b/tests/editor/cases/ph_2_terrain_no_question.txt new file mode 100644 index 000000000..95304e3ef --- /dev/null +++ b/tests/editor/cases/ph_2_terrain_no_question.txt @@ -0,0 +1,31 @@ +# The first L in a new scenery asks nothing: where the scenery lies on the map the terrain loads in the background and a line in the +# panel of the laying tells it, with the button to go without; no window in the way of the laying. The fixture has tracks, so the +# offer is started as the first L starts it (nmtquestion open), after the scenery was placed on the map (in Warsaw, the photo of one tile) +wait 5 +nmtsettings grow 1 +wait 2 +tap f3 +wait 2 +tap l +wait 3 +orthophoto 482500 634000 0 1 0 +wait 3 +nmtquestion open +wait 5 +expect terrain question 0 +expect terrain prompt 1 +expect terrain loading 1 +expect laypanel ready shown +# the photo doesn't wait for the terrain: it's on and its tile asked for while the terrain is still to come +expect photo on 1 +# Without terrain: nothing of the model loaded any more, the line gone +nmtquestion flat +wait 3 +expect terrain prompt 0 +expect terrain loading 0 +expect terrain grow 0 +expect terrain area 0 +nmtquestion restore +wait 2 +orthophoto forget +quit diff --git a/tests/editor/cases/ph_3_tracks_over_photo.txt b/tests/editor/cases/ph_3_tracks_over_photo.txt new file mode 100644 index 000000000..c91ba5266 --- /dev/null +++ b/tests/editor/cases/ph_3_tracks_over_photo.txt @@ -0,0 +1,19 @@ +# In the top view the tracks laid are drawn over the photo, which is drawn over the view, also zoomed in (under 0.5 m a pixel, where they +# were left to the 3d tracks the photo covers): the rails of a1 at about 0.17 m a pixel. Without the photo no lines over them there. The +# origin is outside Poland, so no photo is downloaded +wait 5 +tap f3 +wait 2 +camera 0 200 50 0 0 50.5 +ortho 40 whole +wait 3 +orthophoto 50 50 0 1 0 +wait 3 +expect photo on 1 +expect photo tracksover 1 +orthophoto off +wait 3 +expect photo on 0 +expect photo tracksover 0 +orthophoto forget +quit diff --git a/tests/editor/cases/ph_4_toolbar_toggle.txt b/tests/editor/cases/ph_4_toolbar_toggle.txt new file mode 100644 index 000000000..7f44ff524 --- /dev/null +++ b/tests/editor/cases/ph_4_toolbar_toggle.txt @@ -0,0 +1,29 @@ +# The photo in the toolbar of the tracks: its button and ~ turn it on and off for the scenery, as the user's choice, and the terrain +# made of the model around the camera doesn't turn it on again behind the user (~ turned it off before without it counting as a choice, +# and the terrain turned it on again). ~ is in the shortcuts (F1). In Warsaw: the photo of one tile, the terrain of the model around +wait 5 +tap f3 +wait 2 +orthophoto 482500 634000 0 1 0 +wait 3 +expect photo on 1 +tap grave +wait 2 +expect photo on 0 +expect photo chosen 1 +nmtmake around 100 +wait 10 +expect photo on 0 +toolbar ~ +wait 2 +expect photo on 1 +toolbar ~ +wait 2 +expect photo on 0 +tap f1 +wait 3 +expect window shortcuts 1 +tap f1 +wait 2 +orthophoto forget +quit diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index e97797169..7c0f38716 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -37,6 +37,10 @@ add_executable(${UNIT_TESTS_NAME} "${CMAKE_SOURCE_DIR}/editor/editorHttp.cpp" # and the //$ lines it keeps in the scenery files, and the descriptions of the templates (//$e) "${CMAKE_SOURCE_DIR}/editor/editorRecords.cpp" + # the rules the aerial photo goes by: the origin, on or off, the download area grown, the tiles tried again, the 4K photos (test_photo_rules.cpp) + "${CMAKE_SOURCE_DIR}/editor/editorPhotoRules.cpp" + # and the settings of the editor as their file keeps them, read and written through a stream (the user's file isn't touched) + "${CMAKE_SOURCE_DIR}/editor/editorSettings.cpp" "${CMAKE_SOURCE_DIR}/scene/scenelayercontext.cpp" "${CMAKE_SOURCE_DIR}/editor/editorIncludeInfo.cpp" # the tracks read from the text of the scenery files and written back, with no scene (test_track_text.cpp): the track and diff --git a/tests/unit/test_photo_rules.cpp b/tests/unit/test_photo_rules.cpp new file mode 100644 index 000000000..cc5008a08 --- /dev/null +++ b/tests/unit/test_photo_rules.cpp @@ -0,0 +1,236 @@ +#include "stdafx.h" +#include "doctest.h" +#include "editor/editorGeometry.hpp" +#include "editor/editorPhotoRules.hpp" +#include "editor/editorRecords.hpp" +#include "editor/editorSettings.hpp" + +#include +#include +#include +#include + +namespace { + +using photo_rules::origin_source; + +std::set> squares(std::vector const &Polygon, double const Size) { + auto const found{geometry::squares_in(Polygon, Size)}; + return {found.begin(), found.end()}; +} + +// a square of tiles of 256 m, the tiles from First to Last along both axes +std::vector block(int const First, int const Last) { + double const low{First * 256.0 + 10.0}; + double const high{(Last + 1) * 256.0 - 10.0}; + return {{low, low}, {high, low}, {high, high}, {low, high}}; +} + +} // namespace + +TEST_CASE("photo: the photo turned on or off is the scenery's, kept with the editor") { + editorSettings settings; + CHECK_FALSE(settings.orthophoto_on("a").has_value()); + settings.orthophoto_on("a", false); + settings.orthophoto_on("b c", true); + CHECK(settings.orthophoto_on("a") == std::optional{false}); + CHECK(settings.orthophoto_on("b c") == std::optional{true}); + CHECK_FALSE(settings.orthophoto_on("d").has_value()); + + // written and read back, the scenery with a blank in its name too + std::stringstream file; + settings.write(file); + editorSettings again; + again.read(file); + CHECK(again.orthophoto_on("a") == std::optional{false}); + CHECK(again.orthophoto_on("b c") == std::optional{true}); + CHECK_FALSE(again.orthophoto_on("d").has_value()); + + again.orthophoto_forget("a"); + CHECK_FALSE(again.orthophoto_on("a").has_value()); +} + +TEST_CASE("photo: the global off of an older editor is left behind") { + std::istringstream file{"ortho_on 0\northo_opacity 0.4\n"}; + editorSettings settings; + settings.read(file); + CHECK_FALSE(settings.orthophoto_on("any").has_value()); + CHECK(settings.orthophoto().opacity == doctest::Approx(0.4f)); +} + +TEST_CASE("photo: on as the user left it in the scenery, else as the scenery keeps it, else off") { + // the wizard's //$o on 1 with nothing chosen by the user in the scenery: on + auto const wizard{photo_rules::photo_on(std::nullopt, true)}; + CHECK(wizard.on); + CHECK(wizard.chosen); + // turned off by the user in it: off, whatever the scenery says + CHECK_FALSE(photo_rules::photo_on(false, true).on); + CHECK(photo_rules::photo_on(true, false).on); + // nothing said: off, and the terrain model may turn it on + auto const none{photo_rules::photo_on(std::nullopt, std::nullopt)}; + CHECK_FALSE(none.on); + CHECK_FALSE(none.chosen); +} + +TEST_CASE("photo: //$g wins over the origin remembered and the one of //$o, the difference told") { + glm::dvec2 const g{500000.0, 600000.0}; + auto const choice{photo_rules::choose_origin(g, std::nullopt, glm::dvec2{510000.0, 600000.0})}; + CHECK(choice.from == origin_source::georeference); + CHECK(choice.origin == g); + CHECK(choice.differs == doctest::Approx(10000.0)); + + auto const same{photo_rules::choose_origin(g, g, g)}; + CHECK(same.differs == doctest::Approx(0.0)); + CHECK(same.differs <= photo_rules::origin_tolerance); + + auto const mark{photo_rules::choose_origin(g, glm::dvec2{500000.0, 600030.0}, std::nullopt)}; + CHECK(mark.from == origin_source::georeference); + CHECK(mark.differs == doctest::Approx(30.0)); + + // without //$g: the editor's //$o, then the one remembered + CHECK(photo_rules::choose_origin(std::nullopt, glm::dvec2{1.0, 2.0}, glm::dvec2{3.0, 4.0}).from == origin_source::mark); + auto const remembered{photo_rules::choose_origin(glm::dvec2{0.0}, std::nullopt, glm::dvec2{3.0, 4.0})}; + CHECK(remembered.from == origin_source::remembered); + CHECK(remembered.origin == glm::dvec2{3.0, 4.0}); + CHECK(photo_rules::choose_origin(std::nullopt, std::nullopt, std::nullopt).from == origin_source::none); + + // a //$o aligned by hand goes before //$g: the user moved the photo off it on purpose + auto const aligned{photo_rules::choose_origin(g, glm::dvec2{500000.0, 600030.0}, glm::dvec2{510000.0, 600000.0}, true)}; + CHECK(aligned.from == origin_source::mark); + CHECK(aligned.origin == glm::dvec2{500000.0, 600030.0}); + CHECK(aligned.differs == doctest::Approx(0.0)); +} + +TEST_CASE("photo: //$o says the origin was aligned by hand, which a release before it passes over") { + auto const line{editor_records::orthophoto_line(true, true, 500000.0, 600030.0, true)}; + CHECK(line == "on 1 origin 500000.00 600030.00 aligned"); + bool on{false}; + double north{0.0}; + double east{0.0}; + bool aligned{false}; + REQUIRE(editor_records::read_orthophoto(line, on, north, east, &aligned)); + CHECK(on); + CHECK(north == doctest::Approx(500000.0)); + CHECK(east == doctest::Approx(600030.0)); + CHECK(aligned); + // read as before without the word, and the word passed over where it isn't asked for + CHECK(editor_records::orthophoto_line(true, true, 500000.0, 600030.0) == "on 1 origin 500000.00 600030.00"); + bool again{false}; + REQUIRE(editor_records::read_orthophoto("on 1 origin 1.00 2.00", on, north, east, &again)); + CHECK_FALSE(again); + REQUIRE(editor_records::read_orthophoto(line, on, north, east)); + CHECK(east == doctest::Approx(600030.0)); +} + +TEST_CASE("photo: a tile which failed is tried again later and later, up to 5 min, never given up") { + CHECK(photo_rules::retry_delay(0) == doctest::Approx(0.0)); + CHECK(photo_rules::retry_delay(1) == doctest::Approx(5.0)); + CHECK(photo_rules::retry_delay(2) == doctest::Approx(10.0)); + CHECK(photo_rules::retry_delay(5) == doctest::Approx(80.0)); + CHECK(photo_rules::retry_delay(7) == doctest::Approx(300.0)); + CHECK(photo_rules::retry_delay(1000) == doctest::Approx(300.0)); + for (int failures = 1; failures < 40; ++failures) + CHECK(photo_rules::retry_delay(failures) <= photo_rules::retry_most); +} + +TEST_CASE("photo: the 4K photos come when the view is zoomed in to 15 cm a pixel, and go past 25 cm") { + CHECK(photo_rules::hires_wanted(0.10, false)); + CHECK(photo_rules::hires_wanted(0.15, false)); + CHECK_FALSE(photo_rules::hires_wanted(0.16, false)); + // on already: they stay a little further out + CHECK(photo_rules::hires_wanted(0.20, true)); + CHECK_FALSE(photo_rules::hires_wanted(0.26, true)); + // a view of no known size + CHECK_FALSE(photo_rules::hires_wanted(0.0, true)); +} + +TEST_CASE("photo: what a download that failed is told as") { + CHECK(photo_rules::failure_reason("WinHTTP error 12007") == "no connection to the internet: the name of the server isn't found"); + CHECK(photo_rules::failure_reason("WinHTTP error 12002") == "the server didn't answer in time"); + CHECK(photo_rules::failure_reason("WinHTTP error 12029") == "the server can't be reached"); + CHECK(photo_rules::failure_reason("HTTP 503") == "the server is busy or out of order"); + CHECK(photo_rules::failure_reason("HTTP 404") == "the server has no photo of the place"); + CHECK(photo_rules::failure_reason("unexpected response: = 4); + auto const tiles{squares(grown.polygon, 256.0)}; + // the old tiles all in, the new ones 2 tiles around the camera's, nothing else + for (int x = 0; x < 4; ++x) + for (int y = 0; y < 4; ++y) + CHECK(tiles.count({x, y}) == 1); + for (int x = 1; x <= 5; ++x) + for (int y = -1; y <= 3; ++y) + CHECK(tiles.count({x, y}) == 1); + CHECK(tiles.size() == grown.total); + CHECK(grown.total == 16 + 10 + 3); // the columns 4 and 5 of y -1..3, the row -1 of x 1..3 + CHECK(grown.added == grown.total - 16); + CHECK_FALSE(geometry::polygon_crosses_itself(grown.polygon)); +} + +TEST_CASE("photo: the download area stays where the camera is deep inside it or far from it") { + auto const area{block(0, 9)}; + // in the middle, the square around it inside + auto const inside{photo_rules::grow_area(area, {5.5 * 256.0, 5.5 * 256.0}, 256.0, 1, 2, 1000)}; + CHECK(inside.polygon.empty()); + CHECK(inside.total == 100); + // 3 tiles off its edge: elsewhere, not at its edge + auto const away{photo_rules::grow_area(area, {13.5 * 256.0, 5.5 * 256.0}, 256.0, 1, 2, 1000)}; + CHECK(away.polygon.empty()); + CHECK_FALSE(away.limited); + // no area: nothing to grow + CHECK(photo_rules::grow_area({}, {0.0, 0.0}, 256.0, 1, 2, 1000).polygon.empty()); +} + +TEST_CASE("photo: the download area doesn't grow past its limit") { + auto const area{block(0, 9)}; + auto const grown{photo_rules::grow_area(area, {9.5 * 256.0, 5.5 * 256.0}, 256.0, 1, 2, 105)}; + CHECK(grown.polygon.empty()); + CHECK(grown.limited); + CHECK(grown.total == 100); +} + +TEST_CASE("photo: an area grown across the mouth of a U takes the inside of the U in, no hole left") { + // a U of tiles: the columns 0 and 4 of y 0..4 and the row 0 between them; the camera over the mouth + std::vector> u; + for (int y = 0; y < 5; ++y) + u.insert(u.end(), {{0, y}, {4, y}}); + for (int x = 1; x < 4; ++x) + u.emplace_back(x, 0); + auto const outline{photo_rules::squares_outline(u, 256.0, 1.0)}; + REQUIRE(outline.size() == 8); + CHECK(squares(outline, 256.0).size() == 13); + auto const grown{photo_rules::grow_area(outline, {2.5 * 256.0, 5.5 * 256.0}, 256.0, 2, 2, 1000)}; + REQUIRE(grown.polygon.size() >= 4); + auto const tiles{squares(grown.polygon, 256.0)}; + // the square around the camera, x 0..4 and y 3..7, closes the U: its inside is taken in + for (int x = 0; x < 5; ++x) + for (int y = 0; y < 8; ++y) + CHECK(tiles.count({x, y}) == 1); + CHECK(tiles.size() == 40); + CHECK(grown.total == 40); + CHECK(grown.added == 27); +} + +TEST_CASE("photo: the outline of squares not in one piece is none") { + CHECK(photo_rules::squares_outline({{0, 0}, {2, 0}}, 256.0, 1.0).empty()); + auto const one{photo_rules::squares_outline({{3, 4}}, 256.0, 1.0)}; + REQUIRE(one.size() == 4); + CHECK(squares(one, 256.0) == std::set>{{3, 4}}); +}