18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 22:07:55 +01:00
Files
maszyna/editor/editorNmt.cpp
T
maj00r cd20d8ec94 feat(editor): the first steps in the inspector, one way to start, the line laid summed up
The first steps of the editor, after the first-run pass at 1920x1080 and 1366x768 (P1 of the plan of the
editor's interface):

- The tutorial no longer opens by itself as a window over the middle of the view (at 1366x768 over all
  of it, its close button under the tool row). With nothing selected, the inspector shows a compact
  "First steps" card: the lesson of the tool in use, its steps to try ticked as they're reached, the
  links "Show the lesson", "The camera" (the first lesson) and "Hide" (the setting first_steps, which
  the checkbox of the window now names "Show the first steps in the inspector"). Opened, the window of
  the lesson goes into the corner of the view by the inspector, below the rows of the menu and the
  tools; moved, it stays between them and the status line, as high as that room at most, the lesson
  scrolled inside. Its close button is always within reach.
- The lesson follows the tool: the select tool of Tracks opens "Laying track" until its steps are done,
  so a new scenery opened in Tracks starts there, not with the camera, which stays the first page and a
  link of the card.
- An empty scenery in Tracks with nothing selected: "The scenery has no tracks yet" and one highlighted
  button, "Lay the first track (L)"; the tutorial is the card under it, a "Tutorial" link when the card
  is hidden. With tracks, the select tool keeps its hints and a plain "Lay a track (L)".
- "Without terrain", the answer to the question of the terrain at the first L, 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), stops putting the
  squares of the area into the cache, a square on its way giving way at once, and drops the squares
  waiting. Kept on, the model went on being asked for, and offline the log filled with "NMT square ...
  not fetched: WinHTTP error"; now nothing is asked for after the answer.
  "Load the terrain" turns both on again. The squares of the area go to the cache, and the aerial photos
  ask for the model to lie on, only while some terrain is made of the model by itself. The question
  stands in the middle of the view, the window of the lesson waiting meanwhile. The warning of no ground
  under the mouse shows only while the pointer is over the view and the terrain is to be there.
- The tooltip of the line being laid names the panel the rest of its warnings are in ("listed in the
  tool options"): the translation of the text before said "in the panel on the right", the panel is on
  the left.
- While the pointer is over a panel, the line being laid stays as it was: the point of the cursor, the
  free end it snaps to and its height aren't taken from where the pointer is under the panel, which
  added a leg there with curves and warnings of its own. A drag begun in the view goes on over a panel.
- After Enter the inspector sums up the line laid: its tracks, length and curves, those below the norms
  in orange, and what comes next, "A switch (T)" and "Save (Ctrl+S)". It stays while the last track is
  the selection the laying left; a click on a track shows the sheet of that track. The switch tool with
  nothing clicked shows the same, not the sheet of the last track laid.
- A switch the switch tool refuses is told in its panel and by the cursor, no longer in the status line
  and the layers window as well, where it stayed after the next switch was laid. Why a branch couldn't
  lead into a free end goes into the panel with the rest; a switch laid takes the note by the cursor
  away.
- The panel of the lay tool fits one screen at 1366x768: what the tool is for is the tooltip of the
  title of its card, the paragraph of the radius at the bends the tooltip of its "?" (which still opens
  the lesson), "This speed also on from a track with a speed of its own" is under More settings; the
  mode, the speed and the height stay up front.
- The layout made on the first start: the side columns 22% of the width, at least 300 px, at most the
  400 and 460 px they had and 30%; at 1366 px the view has about 766 px instead of 557. The tool options
  take 60% of the left column, and in a window under 800 px high the layers are a tab beside them, the
  tool options shown, so the panel of a tool has the whole column. The line of the keys of the
  surroundings goes on under itself at the edge of the panel. Layouts kept in imgui.ini are left as they
  are.
- The names of the period of the day, the season and the weather in Weather and time, and the type of a
  node in the inspector of the surroundings, go through the translations ("(day)", "(autumn)",
  "(scattered)", "Typ: track" before). The degree sign of the air temperature is written as its UTF-8
  bytes: CMakeLists.txt sets COMPILE_OPTIONS of eu07 and eu07_core after add_compile_options("/utf-8"),
  which drops /utf-8, so MSVC writes the "°" of a literal as the single byte 0xB0 of the ANSI code page,
  not UTF-8, and ImGui drew '?'.
- Self-tests: the expectations statuslacks, refusal, statusline, laycursor and terrain, the command
  nmtquestion, and the cases fs_1_preview_over_panel, fs_2_switch_refusal_cleared and
  fs_3_without_terrain.

lang/editor_pl.po: the Polish of the new texts (the card of the first steps, the summary of the line laid,
"Lay the first track", the names of the periods, seasons and weathers and of the node types), the entries
of the texts replaced given their new English ("Show the first steps in the inspector", "and %zu more,
listed in the tool options", "Lay the first track  (L)" in place of "Or start with:"), and "(do
transmisji)" in place of the English "(stream)" of the drawn cursor.
2026-11-23 12:09:59 +01:00

525 lines
16 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "editor/editorNmt.hpp"
#include "editor/editorHttp.hpp"
#include "editor/editorGeometry.hpp"
#include "editor/editorGeoTiff.hpp"
#include "utilities/Logs.h"
#include "utilities/translation.h"
#include "utilities/utilities.h"
#include <algorithm>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <sstream>
namespace
{
namespace fs = std::filesystem;
// the model at 1 m of the WCS service of geoportal.gov.pl; the name of the model is read from its capabilities
char const *const service{"https://mapy.geoportal.gov.pl/wss/service/PZGIK/NMT/GRID1/WCS/DigitalTerrainModelFormatTIFF"};
char const *const fallback_coverage{"DTM_PL-KRON86-NH_TIFF"};
char const *const game_cache_folder{"editor/cache/nmt/grid1"}; // in the game folder: older builds kept the squares there
int constexpr worker_count{2}; // the service is quick, but isn't to be hammered
int constexpr border{1}; // pixels of the neighbouring squares fetched around a square, so the heights interpolate across its edges
float constexpr no_data{-1000.f}; // the model gives far lower values outside of the country
double constexpr retry_after{15.0}; // s, a square the service didn't give is asked for again
double seconds()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
// among the files kept to be fetched again faster, as the orthophoto: the game folder may not be writable. Without a folder of the
// user's cache, in the game folder
fs::path cache_root()
{
auto const root{user_cache_path("nmt")};
return root.empty() ? fs::path{game_cache_folder} : root / "grid1";
}
bool read_file(fs::path const &Path, std::vector<std::uint8_t> &Out)
{
std::ifstream file{Path, std::ios::binary};
if (false == file.is_open())
return false;
Out.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
return true;
}
// written under a name of its own and renamed, so a write broken off never leaves a part of a square behind
bool write_file(fs::path const &Path, std::vector<std::uint8_t> const &Data)
{
std::error_code fault;
fs::create_directories(Path.parent_path(), fault);
std::ostringstream suffix;
suffix << ".part" << std::this_thread::get_id();
auto temporary{Path};
temporary += suffix.str();
{
std::ofstream file{temporary, std::ios::binary | std::ios::trunc};
if (false == file.is_open())
return false;
file.write(reinterpret_cast<char const *>(Data.data()), static_cast<std::streamsize>(Data.size()));
if (false == static_cast<bool>(file))
{
file.close();
fs::remove(temporary, fault);
return false;
}
}
fs::rename(temporary, Path, fault);
if (fault)
{
fs::remove(temporary, fault);
return false;
}
return true;
}
// the name of the model among the names the capabilities of the service list: the first which says it's a terrain model
std::string coverage_of(std::string const &Capabilities)
{
std::vector<std::string> names;
for (std::size_t at{0}; (at = Capabilities.find("name>", at)) != std::string::npos;)
{
at += 5;
auto const end{Capabilities.find('<', at)};
if (end == std::string::npos)
break;
names.emplace_back(Capabilities.substr(at, end - at));
}
for (auto const &name : names)
{
std::string lower{name};
std::transform(lower.begin(), lower.end(), lower.begin(), [](unsigned char const Letter) { return static_cast<char>(std::tolower(Letter)); });
if (lower.find("dtm") != std::string::npos || lower.find("nmt") != std::string::npos || lower.find("terrain") != std::string::npos)
return name;
}
return fallback_coverage;
}
} // namespace
editor_nmt::editor_nmt() = default;
editor_nmt::~editor_nmt()
{
{
std::lock_guard<std::mutex> lock{m_mutex};
m_stop = true;
m_queue.clear();
}
m_wake.notify_all();
for (auto &worker : m_workers)
if (worker.joinable())
worker.join();
}
bool editor_nmt::can_download()
{
return editor_http::available();
}
void editor_nmt::place(double const North, double const East)
{
std::lock_guard<std::mutex> lock{m_mutex};
m_north = North;
m_east = East;
}
bool editor_nmt::placed() const
{
std::lock_guard<std::mutex> lock{m_mutex};
return m_north != 0.0 || m_east != 0.0;
}
editor_nmt::key editor_nmt::key_of(double const East, double const North) const
{
return {static_cast<int>(std::floor(East / square_size)), static_cast<int>(std::floor(North / square_size))};
}
bool editor_nmt::height(double const X, double const Z, double &Height) const
{
std::shared_ptr<square const> found;
double east, north;
{
std::lock_guard<std::mutex> lock{m_mutex};
east = m_east - X;
north = m_north + Z;
auto const it{m_squares.find(key_of(east, north))};
if (it == m_squares.end())
return false;
found = it->second;
}
auto const &grid{*found};
if (grid.failed)
return false;
// the values lie in the middles of the pixels
auto const column{(east - grid.east) / grid.pixel - 0.5};
auto const row{(grid.north - north) / grid.pixel - 0.5};
auto const c0{std::clamp(static_cast<int>(std::floor(column)), 0, grid.width - 2)};
auto const r0{std::clamp(static_cast<int>(std::floor(row)), 0, grid.height - 2)};
auto const fc{std::clamp(column - c0, 0.0, 1.0)};
auto const fr{std::clamp(row - r0, 0.0, 1.0)};
auto const at = [&](int const C, int const R) { return grid.heights[static_cast<std::size_t>(R) * grid.width + C]; };
float const corners[4]{at(c0, r0), at(c0 + 1, r0), at(c0, r0 + 1), at(c0 + 1, r0 + 1)};
if (std::all_of(std::begin(corners), std::end(corners), [](float const Value) { return std::isfinite(Value) && Value > no_data; }))
{
Height = (corners[0] * (1.0 - fc) + corners[1] * fc) * (1.0 - fr) + (corners[2] * (1.0 - fc) + corners[3] * fc) * fr;
return true;
}
// at the edge of the data: the nearest value there is
auto const nearest{corners[(fr > 0.5 ? 2 : 0) + (fc > 0.5 ? 1 : 0)]};
if (false == std::isfinite(nearest) || nearest <= no_data)
return false;
Height = nearest;
return true;
}
bool editor_nmt::request(glm::dvec2 const &Min, glm::dvec2 const &Max)
{
std::vector<key> wanted;
bool all{true};
{
std::lock_guard<std::mutex> lock{m_mutex};
auto const low{key_of(m_east - Max.x, m_north + Min.y)};
auto const high{key_of(m_east - Min.x, m_north + Max.y)};
auto const middle{key_of(m_east - 0.5 * (Min.x + Max.x), m_north + 0.5 * (Min.y + Max.y))};
auto const now{seconds()};
for (auto e = low.first; e <= high.first; ++e)
for (auto n = low.second; n <= high.second; ++n)
{
key const square{e, n};
if (auto const found{m_squares.find(square)}; found != m_squares.end())
{
if (false == found->second->failed || found->second->outside || now - found->second->failed_at < retry_after)
continue;
m_squares.erase(found);
}
all = false;
if (false == m_queued.contains(square))
wanted.push_back(square);
}
std::sort(wanted.begin(), wanted.end(), [&](key const &Left, key const &Right) {
auto const distance = [&](key const &Square) { return std::abs(Square.first - middle.first) + std::abs(Square.second - middle.second); };
return distance(Left) < distance(Right);
});
for (auto const &square : wanted)
{
m_queued.insert(square);
m_queue.push_back(square);
}
if (false == wanted.empty())
start_workers();
}
if (false == wanted.empty())
m_wake.notify_all();
return all;
}
void editor_nmt::start_workers()
{
if (m_workers.empty())
for (int i = 0; i < worker_count; ++i)
m_workers.emplace_back([this]() { work(); });
}
std::filesystem::path editor_nmt::file_of(key const &Key) const
{
return cache_root() / (std::to_string(Key.first) + "_" + std::to_string(Key.second) + ".tif");
}
void editor_nmt::prefetch(std::vector<glm::dvec2> const &Polygon)
{
std::vector<key> keys;
if (can_download())
for (auto const &square : geometry::squares_in(Polygon, square_size))
keys.emplace_back(square);
{
std::lock_guard<std::mutex> lock{m_mutex};
m_prefetch = std::move(keys);
m_prefetch_next = 0;
m_prefetch_again.clear();
m_prefetch_cached.clear();
m_prefetch_failed.clear();
++m_prefetch_generation;
if (false == m_prefetch.empty())
start_workers();
}
m_wake.notify_all();
}
void editor_nmt::prefetch_paused(bool const Paused)
{
{
std::lock_guard<std::mutex> lock{m_mutex};
m_prefetch_paused = Paused;
}
m_wake.notify_all();
}
editor_nmt::prefetch_state editor_nmt::prefetched() const
{
std::lock_guard<std::mutex> lock{m_mutex};
return {static_cast<int>(m_prefetch_cached.size()), static_cast<int>(m_prefetch.size()), static_cast<int>(m_prefetch_failed.size())};
}
std::size_t editor_nmt::waiting() const
{
std::lock_guard<std::mutex> lock{m_mutex};
return m_queued.size();
}
bool editor_nmt::retrying(glm::dvec2 const &Min, glm::dvec2 const &Max) const
{
std::lock_guard<std::mutex> lock{m_mutex};
auto const low{key_of(m_east - Max.x, m_north + Min.y)};
auto const high{key_of(m_east - Min.x, m_north + Max.y)};
for (auto e = low.first; e <= high.first; ++e)
for (auto n = low.second; n <= high.second; ++n)
if (auto const found{m_squares.find({e, n})}; found != m_squares.end() && found->second->failed && false == found->second->outside)
return true;
return false;
}
void editor_nmt::retry()
{
std::lock_guard<std::mutex> lock{m_mutex};
std::erase_if(m_squares, [](auto const &Square) { return Square.second->failed && false == Square.second->outside; });
}
void editor_nmt::cancel()
{
std::lock_guard<std::mutex> lock{m_mutex};
for (auto const &square : m_queue)
m_queued.erase(square);
m_queue.clear();
}
std::string editor_nmt::error(bool *Again) const
{
std::lock_guard<std::mutex> lock{m_mutex};
std::string result;
bool outside{false};
if (Again != nullptr)
*Again = false;
for (auto const &[key, square] : m_squares)
{
if (false == square->failed)
continue;
if (Again != nullptr && false == square->outside)
*Again = true;
if (result.empty() || (square->outside && false == outside))
{
result = square->error;
outside = square->outside;
}
}
return result;
}
void editor_nmt::clear()
{
std::lock_guard<std::mutex> lock{m_mutex};
m_queue.clear();
m_queued.clear();
m_squares.clear();
}
void editor_nmt::work()
{
while (true)
{
key square;
bool prefetching{false};
std::uint32_t generation{0};
{
std::unique_lock<std::mutex> lock{m_mutex};
auto const prefetchable = [this]() { return false == m_prefetch_paused && (false == m_prefetch_again.empty() || m_prefetch_next < m_prefetch.size()); };
m_wake.wait(lock, [&]() { return m_stop || false == m_queue.empty() || prefetchable(); });
if (m_stop)
return;
if (false == m_queue.empty())
{
square = m_queue.front();
m_queue.pop_front();
}
else
{
// a square of the download area, while none is asked for; one broken off before first
if (false == m_prefetch_again.empty())
{
square = m_prefetch_again.front();
m_prefetch_again.pop_front();
}
else
square = m_prefetch[m_prefetch_next++];
prefetching = true;
generation = m_prefetch_generation;
}
}
if (prefetching)
{
// put into the disk cache only, the file there not read
std::error_code fault;
std::string error;
bool yielded{false};
auto const ok{fs::exists(file_of(square), fault) || false == fetch(square, error, &yielded)->failed};
std::lock_guard<std::mutex> lock{m_mutex};
if (generation != m_prefetch_generation)
continue;
if (yielded)
m_prefetch_again.push_back(square);
else
(ok ? m_prefetch_cached : m_prefetch_failed).insert(square);
continue;
}
std::string error;
auto fetched{fetch(square, error)};
{
std::lock_guard<std::mutex> lock{m_mutex};
if (false == m_queued.contains(square))
continue; // cleared meanwhile
m_queued.erase(square);
m_squares[square] = std::move(fetched);
}
}
}
std::shared_ptr<editor_nmt::square const> editor_nmt::fetch(key const &Key, std::string &Error, bool *Yielded)
{
auto result{std::make_shared<square>()};
std::error_code fault;
auto const file{file_of(Key)};
std::vector<std::uint8_t> data;
if (read_file(file, data))
{
if (editor_geotiff::read(data, *result, no_data - 1.f))
return result;
// damaged: fetched again
fs::remove(file, fault);
}
// a square an older build kept in the game folder is moved to the cache, as it's asked for
if (auto const old{fs::path{game_cache_folder} / file.filename()}; old != file && read_file(old, data))
{
auto const whole{editor_geotiff::read(data, *result, no_data - 1.f)};
if (whole)
{
// moved as it is when the cache is on the same disk, written there and deleted otherwise; one that can't be
// written there stays where it is
fs::create_directories(file.parent_path(), fault);
fs::rename(old, file, fault);
if (fault && write_file(file, data))
fs::remove(old, fault);
return result;
}
// damaged
fs::remove(old, fault);
}
if (false == can_download())
{
Error = STRN("This build can't fetch from the internet");
result->failed = true;
result->outside = true;
result->error = Error;
return result;
}
editor_http::client client;
// the download of the area gives way to the squares asked for: the terrain under the track laid isn't kept waiting for it; and to
// another area, or to none once no terrain is to be made of the model: dropped, it went on to fail in the log long after
std::uint32_t generation{0};
if (Yielded != nullptr)
{
std::lock_guard<std::mutex> lock{m_mutex};
generation = m_prefetch_generation;
}
auto const yield = [this, Yielded, generation]() {
std::lock_guard<std::mutex> lock{m_mutex};
return Yielded != nullptr && (false == m_queue.empty() || m_prefetch_generation != generation);
};
auto const stop = [this, &yield]() {
{
std::lock_guard<std::mutex> lock{m_mutex};
if (m_stop)
return true;
}
return yield();
};
std::string coverage;
{
std::lock_guard<std::mutex> lock{m_mutex};
coverage = m_coverage;
}
if (coverage.empty())
{
std::vector<std::uint8_t> capabilities;
std::string unused;
if (client.get(std::string{service} + "?SERVICE=WCS&VERSION=1.0.0&REQUEST=GetCapabilities", capabilities, unused, stop))
{
coverage = coverage_of(std::string(capabilities.begin(), capabilities.end()));
std::lock_guard<std::mutex> lock{m_mutex};
m_coverage = coverage;
}
else
{
// for this square only: the capabilities are asked for again with the next one
coverage = fallback_coverage;
}
}
// the square with a pixel of each neighbour around it
auto const pixels{static_cast<int>(square_size) + 2 * border};
auto const east{Key.first * square_size - border};
auto const north{Key.second * square_size - border};
char bbox[128];
std::snprintf(bbox, sizeof(bbox), "%.0f,%.0f,%.0f,%.0f", east, north, east + pixels, north + pixels);
auto const url{std::string{service} + "?SERVICE=WCS&VERSION=1.0.0&REQUEST=GetCoverage&COVERAGE=" + coverage + "&CRS=EPSG:2180&RESPONSE_CRS=EPSG:2180&BBOX=" + bbox +
"&WIDTH=" + std::to_string(pixels) + "&HEIGHT=" + std::to_string(pixels) + "&FORMAT=GEOTIFF"};
for (int attempt = 0; attempt < 3; ++attempt)
{
if (client.get(url, data, Error, stop) && editor_geotiff::read(data, *result, no_data - 1.f))
{
write_file(file, data);
Error.clear();
return result;
}
if (stop())
break;
if (std::string const text(data.begin(), data.end()); text.find("outside requested coverage") != std::string::npos)
{
Error = STRN("The place lies outside of the terrain model of Poland: check the location of the scenery");
result->outside = true;
break;
}
if (Error.empty())
Error = STRN("The service gave no terrain model");
}
if (Yielded != nullptr && yield())
{
// broken off, not failed: downloaded again later
*Yielded = true;
result->failed = true;
return result;
}
WriteLog("Editor: NMT square " + std::to_string(Key.first) + "," + std::to_string(Key.second) + " not fetched: " + Error, logtype::generic);
result->failed = true;
result->error = Error;
result->failed_at = seconds();
return result;
}