From 41b298ac6966efaa9f8a2e93e80084cd6a57133f Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:05:22 +0100 Subject: [PATCH] feat(editor): the aerial photo turned on or off kept per scenery, //$g of the scenery first for its origin The photo on or off is the scenery's now, as the user left it in it: a global off kept by the editor no longer hides the photo of a scenery the wizard made with //$o on 1, and the old global choice is left behind. With nothing chosen in the scenery it's as the scenery keeps it, else off, and the terrain model turns it on where the scenery lies on the map. The origin of the scenery is taken from //$g of its file before the one the editor remembered for a scenery of that name, or wrote into //$o with it; where they're apart by more than a metre the status line says so, and the settings of the orthophoto tell that an origin typed there is for the session. The rules the photo goes by stand on their own (editorPhotoRules), with their unit tests; the settings of the editor are read and written through a stream, so they're tested without the user's file. --- CMakeLists.txt | 1 + application/editormode.cpp | 61 +++++--- application/editormode.h | 4 +- editor/editorPhotoRules.cpp | 266 ++++++++++++++++++++++++++++++++ editor/editorPhotoRules.hpp | 84 ++++++++++ editor/editorSettings.cpp | 42 ++++- editor/editorSettings.hpp | 16 +- lang/editor_pl.po | 7 + tests/unit/CMakeLists.txt | 4 + tests/unit/test_photo_rules.cpp | 197 +++++++++++++++++++++++ 10 files changed, 649 insertions(+), 33 deletions(-) create mode 100644 editor/editorPhotoRules.cpp create mode 100644 editor/editorPhotoRules.hpp create mode 100644 tests/unit/test_photo_rules.cpp 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/editormode.cpp b/application/editormode.cpp index f0f4a415b..42c9ecf28 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" @@ -2564,32 +2565,43 @@ 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; 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}; + } } - // 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 + auto const origin{photo_rules::choose_origin(m_georeference ? std::optional{m_georeference_origin} : std::nullopt, marked_origin, remembered)}; + config.north = origin.origin.x; + config.east = origin.origin.y; + 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 @@ -2620,8 +2632,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() @@ -2691,7 +2707,8 @@ 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: an origin typed here is for this session + 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; diff --git a/application/editormode.h b/application/editormode.h index 70c98b156..18d33b33b 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -917,7 +917,7 @@ 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); int m_orthophoto_frame{-10}; bool orthophoto_settings_open() const; @@ -958,6 +958,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; diff --git a/editor/editorPhotoRules.cpp b/editor/editorPhotoRules.cpp new file mode 100644 index 000000000..cff68a271 --- /dev/null +++ b/editor/editorPhotoRules.cpp @@ -0,0 +1,266 @@ +/* +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) +{ + origin_choice 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 failure_reason(std::string const &Error) +{ + auto const has = [&](char const *Text) { return Error.find(Text) != std::string::npos; }; + if (has("12007") || has("resolve") || has("12005")) + 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("onnect")) + return "the server can't be reached"; + if (has("12175") || has("12157") || has("12045") || 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 404")) + return "the server has no photo of the place"; + if (has("HTTP 4")) + return "the server refused the request"; + 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..7a47337bb --- /dev/null +++ b/editor/editorPhotoRules.hpp @@ -0,0 +1,84 @@ +/* +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 +origin_choice choose_origin(std::optional const &Georeference, std::optional const &Mark, std::optional const &Remembered); +// 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); + +// 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/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 6815cdc6b..96cf504cf 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -15373,3 +15373,10 @@ 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" 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..f577a6f7c --- /dev/null +++ b/tests/unit/test_photo_rules.cpp @@ -0,0 +1,197 @@ +#include "stdafx.h" +#include "doctest.h" +#include "editor/editorGeometry.hpp" +#include "editor/editorPhotoRules.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); +} + +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}}); +}