mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-24 13:09:16 +01:00
Merge branch 'master' into feat/renderers-cleanup
This commit is contained in:
@@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "stdafx.h"
|
||||
#include "editor/editorGeoMap.hpp"
|
||||
#include "editor/editorFormat.hpp"
|
||||
#include "editor/editorHttp.hpp"
|
||||
#include "editor/editorOrthophoto.hpp"
|
||||
#include "utilities/translation.h"
|
||||
|
||||
@@ -164,6 +165,8 @@ bool parse_place(std::string const &Text, glm::dvec2 &Point)
|
||||
|
||||
editor_map::~editor_map()
|
||||
{
|
||||
// the download running is broken off rather than waited for to its end or its timeout
|
||||
m_closing = true;
|
||||
if (m_request.valid())
|
||||
m_request.wait();
|
||||
release();
|
||||
@@ -328,12 +331,13 @@ void editor_map::request(glm::vec2 const &Size)
|
||||
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]() {
|
||||
m_request = std::async(std::launch::async, [url, low, high, closing = &m_closing]() {
|
||||
image result;
|
||||
result.low = low;
|
||||
result.high = high;
|
||||
std::vector<std::uint8_t> data;
|
||||
if (false == editor_http_get(url, data, result.error))
|
||||
thread_local editor_http::client client;
|
||||
if (false == client.get(url, data, result.error, [closing]() { return closing->load(); }))
|
||||
return result;
|
||||
int w{0}, h{0}, components{0};
|
||||
// the texture loader turns the stb flip on for the whole process only once it loads its first texture: this thread
|
||||
|
||||
@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <future>
|
||||
#include <string>
|
||||
@@ -86,6 +87,7 @@ class editor_map
|
||||
glm::vec2 m_pressed{0.f};
|
||||
bool m_dragging{false};
|
||||
std::future<image> m_request;
|
||||
std::atomic<bool> m_closing{false}; // the download of the map is given up: the map goes
|
||||
std::uint64_t m_texture{0};
|
||||
glm::dvec2 m_image_low{0.0};
|
||||
glm::dvec2 m_image_high{0.0};
|
||||
|
||||
@@ -45,15 +45,209 @@ struct client::platform
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
DWORD constexpr kStopCheck{50}; // ms between the looks at whether the transfer is to be given up
|
||||
|
||||
// what the callback of WinHTTP hands over to the thread waiting for it
|
||||
struct transfer_state
|
||||
{
|
||||
HANDLE done{::CreateEventW(nullptr, FALSE, FALSE, nullptr)}; // the operation begun finished or failed
|
||||
HANDLE closed{::CreateEventW(nullptr, TRUE, FALSE, nullptr)}; // the request is called back no more
|
||||
DWORD error{ERROR_SUCCESS}; // of the operation that failed
|
||||
DWORD size{0}; // bytes available or read
|
||||
|
||||
transfer_state() = default;
|
||||
transfer_state(transfer_state const &) = delete;
|
||||
transfer_state &operator=(transfer_state const &) = delete;
|
||||
~transfer_state()
|
||||
{
|
||||
if (done != nullptr)
|
||||
::CloseHandle(done);
|
||||
if (closed != nullptr)
|
||||
::CloseHandle(closed);
|
||||
}
|
||||
};
|
||||
|
||||
void CALLBACK on_status(HINTERNET, DWORD_PTR const Context, DWORD const Status, LPVOID const Info, DWORD const Length)
|
||||
{
|
||||
auto *state{reinterpret_cast<transfer_state *>(Context)};
|
||||
if (state == nullptr)
|
||||
return;
|
||||
switch (Status)
|
||||
{
|
||||
case WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE:
|
||||
case WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE:
|
||||
break;
|
||||
case WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE:
|
||||
state->size = *static_cast<DWORD const *>(Info);
|
||||
break;
|
||||
case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
|
||||
state->size = Length;
|
||||
break;
|
||||
case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR:
|
||||
state->error = static_cast<WINHTTP_ASYNC_RESULT const *>(Info)->dwError;
|
||||
break;
|
||||
case WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING:
|
||||
::SetEvent(state->closed);
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
::SetEvent(state->done);
|
||||
}
|
||||
|
||||
// one GET on a connection of its own. the steps of WinHTTP are asynchronous and waited for while the stop is looked at, so a
|
||||
// transfer is given up at once also while a service keeps the connection waiting: the closing of the editor doesn't wait for
|
||||
// the timeouts of a service that doesn't answer
|
||||
class transfer
|
||||
{
|
||||
public:
|
||||
transfer(stop_check const &Stop, std::string &Error) :
|
||||
m_stop(Stop),
|
||||
m_error(Error)
|
||||
{
|
||||
}
|
||||
~transfer()
|
||||
{
|
||||
close();
|
||||
}
|
||||
transfer(transfer const &) = delete;
|
||||
transfer &operator=(transfer const &) = delete;
|
||||
|
||||
bool open(HINTERNET const Session, std::wstring const &Host, INTERNET_PORT const Port, std::wstring const &Path, bool const Secure)
|
||||
{
|
||||
if (m_state.done == nullptr || m_state.closed == nullptr)
|
||||
return failed(::GetLastError());
|
||||
m_connection = ::WinHttpConnect(Session, Host.c_str(), Port, 0);
|
||||
if (m_connection == nullptr)
|
||||
return failed(::GetLastError());
|
||||
m_request = ::WinHttpOpenRequest(m_connection, L"GET", Path.c_str(), nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, Secure ? WINHTTP_FLAG_SECURE : 0);
|
||||
if (m_request == nullptr)
|
||||
return failed(::GetLastError());
|
||||
auto context{reinterpret_cast<DWORD_PTR>(&m_state)};
|
||||
if (false == ::WinHttpSetOption(m_request, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(context)))
|
||||
return failed(::GetLastError());
|
||||
if (::WinHttpSetStatusCallback(m_request, on_status, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | WINHTTP_CALLBACK_FLAG_HANDLES, 0) == WINHTTP_INVALID_STATUS_CALLBACK)
|
||||
return failed(::GetLastError());
|
||||
m_called_back = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool send()
|
||||
{
|
||||
return step([this]() { return ::WinHttpSendRequest(m_request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, reinterpret_cast<DWORD_PTR>(&m_state)); });
|
||||
}
|
||||
|
||||
bool receive()
|
||||
{
|
||||
return step([this]() { return ::WinHttpReceiveResponse(m_request, nullptr); });
|
||||
}
|
||||
|
||||
DWORD status() const
|
||||
{
|
||||
DWORD status{0};
|
||||
DWORD size{sizeof(status)};
|
||||
::WinHttpQueryHeaders(m_request, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &status, &size, WINHTTP_NO_HEADER_INDEX);
|
||||
return status;
|
||||
}
|
||||
|
||||
// the whole body of the response
|
||||
bool read_all(std::vector<std::uint8_t> &Out)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (m_stop())
|
||||
{
|
||||
m_error = "cancelled";
|
||||
return false;
|
||||
}
|
||||
if (false == step([this]() { return ::WinHttpQueryDataAvailable(m_request, nullptr); }))
|
||||
return false;
|
||||
auto const available{m_state.size};
|
||||
if (available == 0)
|
||||
return true;
|
||||
if (Out.size() + available > kLargest)
|
||||
{
|
||||
m_error = kTooLarge;
|
||||
Out.clear();
|
||||
return false;
|
||||
}
|
||||
auto const offset{Out.size()};
|
||||
Out.resize(offset + available);
|
||||
auto const read{step([this, &Out, offset, available]() { return ::WinHttpReadData(m_request, Out.data() + offset, available, nullptr); })};
|
||||
Out.resize(offset + (read ? m_state.size : 0));
|
||||
if (false == read)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
if (m_request != nullptr)
|
||||
{
|
||||
::WinHttpCloseHandle(m_request);
|
||||
m_request = nullptr;
|
||||
// the closing is the last callback: after it the state, and the buffer of a read broken off, are no longer written to
|
||||
if (m_called_back)
|
||||
::WaitForSingleObject(m_state.closed, INFINITE);
|
||||
m_called_back = false;
|
||||
}
|
||||
if (m_connection != nullptr)
|
||||
{
|
||||
::WinHttpCloseHandle(m_connection);
|
||||
m_connection = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
bool failed(DWORD const Code)
|
||||
{
|
||||
m_error = "WinHTTP error " + std::to_string(Code);
|
||||
return false;
|
||||
}
|
||||
|
||||
// begins an operation and waits for it: false when it failed, or a stop was asked for meanwhile
|
||||
template <typename Begin> bool step(Begin const &Operation)
|
||||
{
|
||||
m_state.error = ERROR_SUCCESS;
|
||||
if (false == Operation())
|
||||
return failed(::GetLastError());
|
||||
while (::WaitForSingleObject(m_state.done, kStopCheck) != WAIT_OBJECT_0)
|
||||
{
|
||||
if (m_stop())
|
||||
{
|
||||
// closed at once: the operation broken off doesn't write into the buffer any more once this returns
|
||||
close();
|
||||
m_error = "cancelled";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (m_state.error != ERROR_SUCCESS)
|
||||
return failed(m_state.error);
|
||||
return true;
|
||||
}
|
||||
|
||||
stop_check const &m_stop;
|
||||
std::string &m_error;
|
||||
transfer_state m_state;
|
||||
HINTERNET m_connection{nullptr};
|
||||
HINTERNET m_request{nullptr};
|
||||
bool m_called_back{false}; // the closing of the request is called back
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
client::client() :
|
||||
m_platform(std::make_unique<platform>())
|
||||
{
|
||||
wchar_t const *agent = L"MaSzyna-editor";
|
||||
#ifdef WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY
|
||||
m_platform->session = ::WinHttpOpen(agent, WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
||||
m_platform->session = ::WinHttpOpen(agent, WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC);
|
||||
#endif
|
||||
if (m_platform->session == nullptr) // automatic proxy needs windows 8.1+
|
||||
m_platform->session = ::WinHttpOpen(agent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
||||
m_platform->session = ::WinHttpOpen(agent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC);
|
||||
if (m_platform->session != nullptr)
|
||||
::WinHttpSetTimeouts(m_platform->session, 10000, 10000, 15000, 20000);
|
||||
}
|
||||
@@ -94,64 +288,13 @@ bool client::get(std::string const &Url, std::vector<std::uint8_t> &Out, std::st
|
||||
std::wstring const host(parts.lpszHostName, parts.dwHostNameLength);
|
||||
std::wstring const path = std::wstring(parts.lpszUrlPath, parts.dwUrlPathLength) + std::wstring(parts.lpszExtraInfo, parts.dwExtraInfoLength);
|
||||
|
||||
HINTERNET connection = ::WinHttpConnect(m_platform->session, host.c_str(), parts.nPort, 0);
|
||||
HINTERNET request = connection ? ::WinHttpOpenRequest(connection, L"GET", path.c_str(), nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
|
||||
parts.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0)
|
||||
: nullptr;
|
||||
auto const close = [&]() {
|
||||
if (request != nullptr)
|
||||
::WinHttpCloseHandle(request);
|
||||
if (connection != nullptr)
|
||||
::WinHttpCloseHandle(connection);
|
||||
};
|
||||
|
||||
if (request == nullptr || !::WinHttpSendRequest(request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0) || !::WinHttpReceiveResponse(request, nullptr))
|
||||
{
|
||||
Error = "WinHTTP error " + std::to_string(::GetLastError());
|
||||
close();
|
||||
transfer request{Stop, Error};
|
||||
if (false == request.open(m_platform->session, host, parts.nPort, path, parts.nScheme == INTERNET_SCHEME_HTTPS) || false == request.send() || false == request.receive())
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD status{0};
|
||||
DWORD statussize{sizeof(status)};
|
||||
::WinHttpQueryHeaders(request, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &status, &statussize, WINHTTP_NO_HEADER_INDEX);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (Stop())
|
||||
{
|
||||
Error = "cancelled";
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
DWORD available{0};
|
||||
if (!::WinHttpQueryDataAvailable(request, &available))
|
||||
{
|
||||
Error = "WinHTTP error " + std::to_string(::GetLastError());
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
if (available == 0)
|
||||
break;
|
||||
if (Out.size() + available > kLargest)
|
||||
{
|
||||
Error = kTooLarge;
|
||||
close();
|
||||
Out.clear();
|
||||
return false;
|
||||
}
|
||||
std::size_t const offset = Out.size();
|
||||
Out.resize(offset + available);
|
||||
DWORD received{0};
|
||||
if (!::WinHttpReadData(request, Out.data() + offset, available, &received))
|
||||
{
|
||||
Error = "WinHTTP error " + std::to_string(::GetLastError());
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
Out.resize(offset + received);
|
||||
}
|
||||
close();
|
||||
auto const status{request.status()};
|
||||
if (false == request.read_all(Out))
|
||||
return false;
|
||||
request.close();
|
||||
|
||||
if (status != 200)
|
||||
{
|
||||
|
||||
@@ -548,6 +548,71 @@ bool event_names(std::string const &File, std::vector<std::string> &Names)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool switched_tracks(std::string const &File, std::vector<std::string> &Suffixes)
|
||||
{
|
||||
Suffixes.clear();
|
||||
template_text text;
|
||||
std::string error;
|
||||
if (false == read_template(File, text, error))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// event <name> switch <delay> <track> <state>
|
||||
auto const tokens{tokenize(text.content)};
|
||||
std::string const first{"(p1)"};
|
||||
// a drive has the events thrown by the keys and the map, (p1)+ and (p1)-, or moves a model, lights its lamps or throws a double switch at
|
||||
// once; the sleepers or the ballast of a switch may throw it on their own events too, with nothing else, but they don't drive it
|
||||
bool driven{false};
|
||||
for (std::size_t idx = 0; idx + 2 < tokens.size() && false == driven; ++idx)
|
||||
{
|
||||
if (tokens[idx] != "event")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
auto const &name{tokens[idx + 1]};
|
||||
auto const &kind{tokens[idx + 2]};
|
||||
driven = (name.size() > first.size() && name.compare(0, first.size(), first) == 0 && (name.back() == '+' || name.back() == '-')) || (kind != "switch" && kind != "trackvel");
|
||||
}
|
||||
if (false == driven)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
for (std::size_t idx = 0; idx + 4 < tokens.size(); ++idx)
|
||||
{
|
||||
if (tokens[idx] != "event" || tokens[idx + 2] != "switch" || tokens[idx + 4].compare(0, first.size(), first) != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
auto suffix{tokens[idx + 4].substr(first.size())};
|
||||
if (std::find(Suffixes.begin(), Suffixes.end(), suffix) == Suffixes.end())
|
||||
{
|
||||
Suffixes.push_back(std::move(suffix));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool switch_bed(std::string const &File)
|
||||
{
|
||||
template_text text;
|
||||
std::string error;
|
||||
if (false == read_template(File, text, error))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto const tokens{tokenize(text.content)};
|
||||
for (std::size_t idx = 0; idx < tokens.size(); ++idx)
|
||||
{
|
||||
// node <range max> <range min> <name> track ...
|
||||
if (tokens[idx] == "include" || (tokens[idx] == "node" && idx + 4 < tokens.size() && tokens[idx + 4] == "track") ||
|
||||
(tokens[idx] == "event" && idx + 2 < tokens.size() && tokens[idx + 2] != "switch" && tokens[idx + 2] != "trackvel"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool signal_speeds(std::string const &File, std::vector<int> &Speeds)
|
||||
{
|
||||
Speeds.clear();
|
||||
|
||||
@@ -90,6 +90,13 @@ int parameter_count(std::string const &File);
|
||||
bool signal_speeds(std::string const &File, std::vector<int> &Speeds);
|
||||
// the names of the events the template defines, as they're written after "event", lower case. returns: false if the file can't be read
|
||||
bool event_names(std::string const &File, std::vector<std::string> &Names);
|
||||
// the tracks the switch events of the template throw, named by its first parameter: what follows (p1) in their names, lower case, ""
|
||||
// for the track named (p1) itself, "a" for (p1)a of a double switch; none for the sleepers or the ballast of a switch, whose events only
|
||||
// throw it or set its speed, with no (p1)+ or (p1)-. returns: false if the file can't be read
|
||||
bool switched_tracks(std::string const &File, std::vector<std::string> &Suffixes);
|
||||
// true if the template can be the sleepers or the ballast of a switch: it puts no track, includes nothing and has no events but those
|
||||
// throwing a switch or setting its speed. false if the file can't be read
|
||||
bool switch_bed(std::string const &File);
|
||||
// the aspects of a block signal of the template: 4 where its events give S3, the green light flashing with the next signal at S5,
|
||||
// the warning two signals ahead, else 3. returns: 0 if the file can't be read
|
||||
int block_aspects(std::string const &File);
|
||||
|
||||
@@ -63,12 +63,6 @@ double steady_seconds()
|
||||
|
||||
} // namespace
|
||||
|
||||
bool editor_http_get(std::string const &Url, std::vector<std::uint8_t> &Out, std::string &Error)
|
||||
{
|
||||
thread_local http_client client;
|
||||
return client.get(Url, Out, Error, []() { return false; });
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
@@ -688,7 +682,7 @@ std::string xml_element(std::string const &Xml, std::string const &Tag, std::siz
|
||||
|
||||
} // namespace
|
||||
|
||||
editor_orthophoto::service_info editor_orthophoto::describe(std::string const &Service)
|
||||
editor_orthophoto::service_info editor_orthophoto::describe(std::string const &Service, std::function<bool()> const &Stop)
|
||||
{
|
||||
service_info info;
|
||||
auto url{Service};
|
||||
@@ -700,8 +694,10 @@ editor_orthophoto::service_info editor_orthophoto::describe(std::string const &S
|
||||
// a few tries, the services drop connections and time out now and then
|
||||
std::vector<std::uint8_t> data;
|
||||
auto read{false};
|
||||
for (int attempt = 0; attempt < 3 && false == read; ++attempt)
|
||||
read = editor_http_get(url, data, info.error);
|
||||
stop_check const stopping{Stop ? Stop : stop_check{[]() { return false; }}};
|
||||
thread_local http_client client;
|
||||
for (int attempt = 0; attempt < 3 && false == read && false == stopping(); ++attempt)
|
||||
read = client.get(url, data, info.error, stopping);
|
||||
if (false == read)
|
||||
return info;
|
||||
info.error.clear();
|
||||
@@ -937,23 +933,47 @@ void editor_orthophoto::ground_arrived()
|
||||
{
|
||||
if (!m_height)
|
||||
return;
|
||||
for (auto &entry : m_tiles)
|
||||
for (auto &[key, t] : m_tiles)
|
||||
{
|
||||
auto &t{entry.second};
|
||||
if (!t.grounded || !t.guessed)
|
||||
continue;
|
||||
// the middle and the corners: a chunk of the terrain is much larger than a tile, it covers some of these when it comes. A tile
|
||||
// lying flat, with nothing to go by, also goes onto the model once it's in
|
||||
glm::dvec2 const corner = tile_corner(entry.first);
|
||||
for (auto const &[u, v] : {std::pair{0.5, 0.5}, std::pair{0.0, 0.0}, std::pair{1.0, 0.0}, std::pair{0.0, 1.0}, std::pair{1.0, 1.0}})
|
||||
if (double y; m_height(corner.x - u * tile_size, corner.y - v * tile_size, y) || (t.heights.empty() && m_guess && m_guess(corner.x - u * tile_size, corner.y - v * tile_size, y)))
|
||||
{
|
||||
t.grounded = false;
|
||||
break;
|
||||
}
|
||||
if (t.grounded && ground_came(key, t))
|
||||
t.grounded = false;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
// a tile lying flat, with nothing to go by, also goes onto the model once it's in: the middle and the corners
|
||||
if (Tile.guessed && Tile.heights.empty() && m_guess)
|
||||
for (auto const &[u, v] : {std::pair{0.5, 0.5}, std::pair{0.0, 0.0}, std::pair{1.0, 0.0}, std::pair{0.0, 1.0}, std::pair{1.0, 1.0}})
|
||||
if (double y; m_guess(corner.x - u * tile_size, corner.y - v * tile_size, y))
|
||||
return true;
|
||||
// the points the ground didn't answer, every second one of the grid (16 m) and the edges: a chunk of the terrain is about as
|
||||
// large as a tile and comes over a part of it, often a band along a side; partly answered tiles count too, not only the guessed
|
||||
static_assert(ground_grid % 2 == 0, "the edges of the grid are among its even points");
|
||||
int constexpr samples{ground_grid + 1};
|
||||
double constexpr step{tile_size / ground_grid};
|
||||
for (auto const index : Tile.unanswered)
|
||||
{
|
||||
int const i{index % samples};
|
||||
int const j{index / samples};
|
||||
if (i % 2 != 0 || j % 2 != 0)
|
||||
continue;
|
||||
if (double y; m_height(corner.x - i * step, corner.y - j * step, y))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void editor_orthophoto::detach_scene()
|
||||
{
|
||||
for (auto &entry : m_tiles)
|
||||
@@ -1032,6 +1052,20 @@ editor_orthophoto::statistics editor_orthophoto::stats() const
|
||||
return out;
|
||||
}
|
||||
|
||||
int editor_orthophoto::first_level(tile const &Tile, int const Level)
|
||||
{
|
||||
// a tile showing nothing yet takes the standard image first, from the disk cache at once mostly: the 4K one comes from a slow
|
||||
// service a few at a time, the tiles around the camera stayed blank meanwhile though their standard images lay on the disk
|
||||
if (Level > standard_pixels && Tile.level < standard_pixels)
|
||||
return standard_pixels;
|
||||
return Level;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
// coarser imagery further away keeps the memory use of large radii in check
|
||||
@@ -1233,6 +1267,7 @@ void editor_orthophoto::sample_ground(tile_key const &Key, tile &Tile)
|
||||
Tile.scene_dirty = true;
|
||||
Tile.heights.clear();
|
||||
Tile.surface.clear();
|
||||
Tile.unanswered.clear();
|
||||
|
||||
glm::dvec2 const corner = tile_corner(Key);
|
||||
glm::dvec2 const min(corner.x - tile_size, corner.y - tile_size);
|
||||
@@ -1304,6 +1339,21 @@ void editor_orthophoto::sample_ground(tile_key const &Key, tile &Tile)
|
||||
}
|
||||
}
|
||||
|
||||
// the points the ground didn't answer: the ground coming there later (the terrain loaded after the imagery, which comes from the
|
||||
// disk cache at once) has the tile sampled again, rather than left at the heights filled in or guessed now
|
||||
Tile.unanswered.clear();
|
||||
auto const columns{static_cast<std::size_t>(samples)};
|
||||
for (std::size_t index = 0; index < heights.size(); ++index)
|
||||
{
|
||||
if (heights[index] != no_height)
|
||||
continue;
|
||||
// in the scene the heights come from the triangles, and a steep spot (a wall, a quarry face) gives none: where the heightmap
|
||||
// terrain answers the point, its ground is there already, and ground_came() would find it again on every chunk that comes
|
||||
if (double y; surface && m_height && m_height(corner.x - static_cast<double>(index % columns) * step, corner.y - static_cast<double>(index / columns) * step, y))
|
||||
continue;
|
||||
Tile.unanswered.push_back(static_cast<std::uint16_t>(index));
|
||||
}
|
||||
|
||||
// holes (places without ground) take the average of their neighbours, spreading inwards
|
||||
std::size_t missing = std::count(heights.begin(), heights.end(), no_height);
|
||||
if (missing == heights.size())
|
||||
@@ -1630,7 +1680,7 @@ void editor_orthophoto::update(glm::dvec3 const &Camera)
|
||||
{
|
||||
tile_key const key{centre.first + dx, centre.second + dy};
|
||||
// outside the download area: nothing asked for, nothing shown
|
||||
if (false == m_area.empty() && 0 == m_area_tiles.count(key))
|
||||
if (outside_area(key))
|
||||
{
|
||||
if (auto const found{m_tiles.find(key)}; found != m_tiles.end())
|
||||
{
|
||||
@@ -1639,14 +1689,12 @@ void editor_orthophoto::update(glm::dvec3 const &Camera)
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (waits_for_ground(key))
|
||||
continue;
|
||||
tile &t = m_tiles[key];
|
||||
if (!in_service_area(key.first, key.second))
|
||||
t.empty = true;
|
||||
int level = level_for(std::max(std::abs(dx), std::abs(dy)), m_config.hires);
|
||||
// a tile showing nothing yet takes the standard image first, from the disk cache at once mostly: the 4K one comes from a slow
|
||||
// service a few at a time, the tiles around the camera stayed blank meanwhile though their standard images lay on the disk
|
||||
if (level > standard_pixels && t.level < standard_pixels)
|
||||
level = standard_pixels;
|
||||
int const level = first_level(t, level_for(std::max(std::abs(dx), std::abs(dy)), m_config.hires));
|
||||
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);
|
||||
@@ -1713,7 +1761,8 @@ void editor_orthophoto::worker()
|
||||
continue;
|
||||
long long const dx = it->key.first - m_focus.first;
|
||||
long long const dy = it->key.second - m_focus.second;
|
||||
long long const d = dx * dx + dy * dy;
|
||||
// the tiles hurried (Ctrl+~) before all the others, the nearest of them first
|
||||
long long const d = dx * dx + dy * dy - (m_urgent.count(it->key) > 0 ? (1LL << 40) : 0LL);
|
||||
if (d < bestdistance)
|
||||
{
|
||||
bestdistance = d;
|
||||
@@ -1730,6 +1779,7 @@ void editor_orthophoto::worker()
|
||||
{
|
||||
current = *best;
|
||||
m_queue.erase(best);
|
||||
m_urgent.erase(current.key);
|
||||
m_running.insert(current.key);
|
||||
if (current.level >= hires_pixels)
|
||||
++m_running_hires;
|
||||
@@ -1957,3 +2007,58 @@ void editor_orthophoto::draw(glm::mat4 const &ViewProjection, glm::dvec3 const &
|
||||
drawlist->PopTextureID();
|
||||
}
|
||||
}
|
||||
|
||||
bool editor_orthophoto::hurry_tile(tile_key const &Key, int const Distance)
|
||||
{
|
||||
// as update() does: nothing asked for outside the download area, nor outside the area the service covers, where the requests
|
||||
// can't be answered
|
||||
if (outside_area(Key))
|
||||
return false;
|
||||
auto &t{m_tiles[Key]};
|
||||
if (false == in_service_area(Key.first, Key.second))
|
||||
t.empty = true;
|
||||
if (t.empty)
|
||||
return false;
|
||||
auto const level{first_level(t, level_for(Distance, m_config.hires))};
|
||||
t.retry_at = 0.0;
|
||||
if (t.level == level && t.alpha == m_alpha)
|
||||
return false; // already shown as it should be
|
||||
if (false == (t.requested == level && t.requested_alpha == m_alpha))
|
||||
schedule(Key, t, level, m_alpha);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::size_t editor_orthophoto::hurry(glm::dvec2 const &Min, glm::dvec2 const &Max, glm::dvec3 const &Camera)
|
||||
{
|
||||
if (false == m_enabled)
|
||||
return 0;
|
||||
// the tile of the camera as it is now: it isn't followed while the layer is off
|
||||
m_camera_tile = camera_tile(Camera);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_focus = m_camera_tile;
|
||||
}
|
||||
auto const a{camera_tile(glm::dvec3{Min.x, 0.0, Min.y})};
|
||||
auto const b{camera_tile(glm::dvec3{Max.x, 0.0, Max.y})};
|
||||
tile_key const first{std::min(a.first, b.first), std::min(a.second, b.second)};
|
||||
tile_key const last{std::max(a.first, b.first), std::max(a.second, b.second)};
|
||||
auto const centre{m_camera_tile};
|
||||
int const radius = m_config.radius;
|
||||
std::vector<tile_key> keys;
|
||||
for (int y = first.second; y <= last.second; ++y)
|
||||
for (int x = first.first; x <= last.first; ++x)
|
||||
{
|
||||
tile_key const key{x, y};
|
||||
auto const distance{std::max(std::abs(x - centre.first), std::abs(y - centre.second))};
|
||||
if (distance <= radius && hurry_tile(key, distance))
|
||||
keys.push_back(key);
|
||||
}
|
||||
auto const count{keys.size()};
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_urgent.clear();
|
||||
m_urgent.insert(keys.begin(), keys.end());
|
||||
}
|
||||
m_wakeup.notify_all();
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -28,8 +28,6 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "utilities/Classes.h" // material_handle
|
||||
#include "rendering/geometrybank.h" // gfx::geometry_handle
|
||||
|
||||
bool editor_http_get(std::string const &Url, std::vector<std::uint8_t> &Out, std::string &Error);
|
||||
|
||||
namespace scene
|
||||
{
|
||||
class basic_region;
|
||||
@@ -102,6 +100,9 @@ class editor_orthophoto
|
||||
editor_orthophoto(editor_orthophoto const &) = delete;
|
||||
editor_orthophoto &operator=(editor_orthophoto const &) = delete;
|
||||
|
||||
// the tiles over a rectangle of the world (x, z; what the camera sees), within the reach around the camera, asked for before anything
|
||||
// else, the ones which failed tried again at once. returns: how many were put at the front of the queue
|
||||
std::size_t hurry(glm::dvec2 const &Min, glm::dvec2 const &Max, glm::dvec3 const &Camera);
|
||||
bool enabled() const { return m_enabled; }
|
||||
// disabling drops the queue and returns the gpu memory of the resident tiles
|
||||
void enabled(bool State);
|
||||
@@ -117,6 +118,10 @@ class editor_orthophoto
|
||||
// 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
|
||||
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<bool(glm::dvec2 const &Min, glm::dvec2 const &Max)>;
|
||||
void coming_source(coming_query Query) { m_coming = std::move(Query); }
|
||||
// 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
|
||||
@@ -176,7 +181,8 @@ class editor_orthophoto
|
||||
bool puwg{false};
|
||||
std::string error;
|
||||
};
|
||||
static service_info describe(std::string const &Service);
|
||||
// Stop: true to give the reading up
|
||||
static service_info describe(std::string const &Service, std::function<bool()> const &Stop = {});
|
||||
static std::string cache_directory();
|
||||
|
||||
private:
|
||||
@@ -236,6 +242,9 @@ class editor_orthophoto
|
||||
float height_max{0.0f};
|
||||
std::vector<glm::dvec3> 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
|
||||
// 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<std::uint16_t> unanswered;
|
||||
// scene geometry
|
||||
scene::basic_section *section{nullptr}; // section holding the shape (nullptr = not in the scene)
|
||||
scene::basic_cell *cell{nullptr}; // set instead of the section's shape list when the shape is translucent
|
||||
@@ -245,6 +254,12 @@ class editor_orthophoto
|
||||
};
|
||||
|
||||
static int level_for(int Distance, bool Hires);
|
||||
// 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
|
||||
bool outside_area(tile_key const &Key) const;
|
||||
// a tile of the view hurried: put at the front of the queue unless it's shown as it should be. true: counted among the hurried
|
||||
bool hurry_tile(tile_key const &Key, int Distance);
|
||||
tile_key camera_tile(glm::dvec3 const &Camera) const;
|
||||
glm::dvec2 tile_corner(tile_key const &Key) const; // world (x,z) of the north-west corner
|
||||
void schedule(tile_key const &Key, tile &Tile, int Level, std::uint8_t Alpha);
|
||||
@@ -253,6 +268,10 @@ class editor_orthophoto
|
||||
void release_tile(tile &Tile);
|
||||
texture_slot acquire_slot();
|
||||
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);
|
||||
@@ -272,6 +291,7 @@ class editor_orthophoto
|
||||
ground_query m_ground;
|
||||
height_query m_height;
|
||||
height_query m_guess;
|
||||
coming_query m_coming;
|
||||
scene::basic_region *m_region{nullptr}; // region the scene geometry was put into
|
||||
std::multimap<std::size_t, gfx::geometry_handle> 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
|
||||
@@ -300,6 +320,7 @@ class editor_orthophoto
|
||||
bool m_prefetch_paused{false};
|
||||
source m_prefetch_source;
|
||||
std::uint32_t m_prefetch_generation{0};
|
||||
std::set<tile_key> m_urgent; // asked for by hurry(): workers take these first; guarded by m_mutex
|
||||
|
||||
std::atomic<bool> m_stop{false};
|
||||
std::atomic<std::uint32_t> m_generation{1};
|
||||
|
||||
@@ -198,6 +198,18 @@ bool editorSettings::load()
|
||||
layer.terms = fields[5];
|
||||
m_wms_layers.push_back(layer);
|
||||
}
|
||||
else if (key == "camera_speed")
|
||||
{
|
||||
parse(value, m_camera.speed);
|
||||
m_camera.speed = std::clamp(m_camera.speed, 0.05, 10.0);
|
||||
}
|
||||
else if (key == "camera_fast")
|
||||
{
|
||||
parse(value, m_camera.fast);
|
||||
m_camera.fast = std::clamp(m_camera.fast, 1.0, 30.0);
|
||||
}
|
||||
else if (key == "camera_zoomed")
|
||||
m_camera.zoomed = (value == "1");
|
||||
else if (key.rfind(origin_prefix, 0) == 0)
|
||||
{
|
||||
// northing;easting
|
||||
@@ -269,6 +281,9 @@ bool editorSettings::save()
|
||||
stream << "laying_heights " << (m_heights == lay_heights::points ? "points" : m_heights == lay_heights::level ? "level" : "fitted") << "\n";
|
||||
for (auto const &layer : m_wms_layers)
|
||||
stream << "wms_layer " << (layer.visible ? 1 : 0) << ";" << layer.opacity << ";" << encoded(layer.url) << ";" << encoded(layer.layers) << ";" << encoded(layer.title) << ";" << encoded(layer.terms) << "\n";
|
||||
stream << "camera_speed " << m_camera.speed << "\n";
|
||||
stream << "camera_fast " << m_camera.fast << "\n";
|
||||
stream << "camera_zoomed " << (m_camera.zoomed ? 1 : 0) << "\n";
|
||||
stream << std::fixed << std::setprecision(3);
|
||||
for (auto const &origin : m_orthophoto_origins)
|
||||
stream << origin.first << " " << origin.second.first << ";" << origin.second.second << "\n";
|
||||
|
||||
@@ -119,10 +119,19 @@ public:
|
||||
};
|
||||
lay_heights heights() const { return m_heights; }
|
||||
void heights(lay_heights Heights) { m_heights = Heights; }
|
||||
// the free camera: its speed, how many times faster Ctrl makes it, and in the top view as fast as the view is zoomed
|
||||
struct camera_settings
|
||||
{
|
||||
double speed{1.0};
|
||||
double fast{30.0};
|
||||
bool zoomed{false}; // off as the editor was before: "As it was" means it
|
||||
};
|
||||
camera_settings &camera() { return m_camera; }
|
||||
|
||||
private:
|
||||
movement_scheme m_movement{movement_scheme::wsad};
|
||||
orthophoto_settings m_orthophoto;
|
||||
camera_settings m_camera;
|
||||
std::map<std::string, std::pair<double, double>> m_orthophoto_origins;
|
||||
std::vector<wms_layer> m_wms_layers;
|
||||
double m_platform_edge{1.725};
|
||||
|
||||
@@ -20,6 +20,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
|
||||
terrain_streamer EditorTerrain;
|
||||
@@ -1038,6 +1039,44 @@ bool terrain_streamer::owns(gfx::geometry_handle const &Geometry) const
|
||||
return false;
|
||||
}
|
||||
|
||||
bool terrain_streamer::in_walk(chunk_key const &Key) const
|
||||
{
|
||||
// the circle update() walks around the camera and around where it's heading
|
||||
auto const within = [this, &Key](chunk_key const &Centre) {
|
||||
auto const dx{Key.first - Centre.first};
|
||||
auto const dz{Key.second - Centre.second};
|
||||
return std::abs(dx) <= m_walkradius && std::abs(dz) <= m_walkradius && dx * dx + dz * dz <= (m_walkradius + 1) * (m_walkradius + 1);
|
||||
};
|
||||
return within(m_walkhere) || within(m_walkthere);
|
||||
}
|
||||
|
||||
bool terrain_streamer::chunk_coming(chunk_key const &Key) const
|
||||
{
|
||||
if (m_chunks.contains(Key) || m_removed.contains(Key))
|
||||
return false;
|
||||
if (m_loading.contains(Key))
|
||||
return true;
|
||||
// a chunk the walk doesn't reach at this distance isn't loaded, so it isn't waited for: it's taken in when the camera comes near
|
||||
if (false == in_walk(Key))
|
||||
return false;
|
||||
// still to come while its pack isn't read yet, or it's in its pack; not there where the pack is read without it
|
||||
auto const pack{m_packs.find(heightmap::pack_of(Key))};
|
||||
if (pack == m_packs.end() || pack->second.status == pack_state::state::unknown || pack->second.status == pack_state::state::loading)
|
||||
return true;
|
||||
return pack->second.status == pack_state::state::ready && pack->second.file->has(Key);
|
||||
}
|
||||
|
||||
bool terrain_streamer::ground_coming(glm::dvec2 const &Low, glm::dvec2 const &High) const
|
||||
{
|
||||
auto const first{heightmap::chunk_at(Low.x, Low.y)};
|
||||
auto const last{heightmap::chunk_at(High.x, High.y)};
|
||||
for (auto z{std::min(first.second, last.second)}; z <= std::max(first.second, last.second); ++z)
|
||||
for (auto x{std::min(first.first, last.first)}; x <= std::max(first.first, last.first); ++x)
|
||||
if (chunk_coming(chunk_key{x, z}))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool terrain_streamer::exists(chunk_key const &Key) const
|
||||
{
|
||||
if (m_chunks.count(Key) > 0)
|
||||
|
||||
@@ -89,6 +89,10 @@ class terrain_streamer
|
||||
bool removed(chunk_key const &Key) const { return m_removed.count(Key) > 0; }
|
||||
// the chunk in memory, or nullptr; Load: read from its pack file if it isn't in memory yet
|
||||
editor_terrain *chunk(chunk_key const &Key, bool const Load = false);
|
||||
// true while a chunk over an XZ rectangle (Low/High are x,z) is still to come at this distance of the camera: being loaded, or
|
||||
// within the walk around the camera and in its pack or with its pack not read yet. False where all of them are in memory, where
|
||||
// none is nor will be, and where the walk doesn't reach
|
||||
bool ground_coming(glm::dvec2 const &Low, glm::dvec2 const &High) const;
|
||||
|
||||
// editing; the chunks changed are written to the files by save()
|
||||
heightmap::manifest const &manifest() const { return m_manifest; }
|
||||
@@ -269,6 +273,11 @@ class terrain_streamer
|
||||
scene::basic_cell *cell{nullptr};
|
||||
};
|
||||
|
||||
// within the circle of chunks the last walk of update() asked for
|
||||
bool in_walk(chunk_key const &Key) const;
|
||||
// the chunk is still to come at this distance of the camera (see ground_coming)
|
||||
bool chunk_coming(chunk_key const &Key) const;
|
||||
|
||||
void start_workers();
|
||||
void stop_workers();
|
||||
void worker();
|
||||
|
||||
Reference in New Issue
Block a user