17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-26 21:44:53 +01:00
Files
maszyna/editor/editorGroundFollow.hpp
maj00r e322947300 fix(editor): the track shown while it's laid over the ground no longer holds up the frames: its grade line is fitted on a thread of its own, the line fitted a moment ago stretched over the track meanwhile, and the click takes the line fitted for the same ground instead of fitting it again; the terrain follows the tracks once the laying ends, not after each click of a chain
Fitting the grade line took 3 ms for 250 m of track, 30 ms for 1 km, 65 ms for 2 km and 200 ms for 4 km, and the preview of the lay tool and of the drag from a free end fitted it every frame, twice for the drag; on the terrain model the preview of some 800 m took 10 to 17 ms a frame, the cursor standing or not. Now the frame waits under 1.5 ms for 4 km. The formations of the tracks led into the terrain went through every chunk under them 0.3 s after each click, some 50 ms, while the cursor went on to the next point; they wait for the laying to end. The tracks laid and the terrain are the same as before.

Selftests: perf logs the longest frame and those over 33 ms, fittime the time of the fit by length, terrainhash a hash of the chunks in memory.
2026-10-24 18:00:21 +02:00

107 lines
3.3 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/.
*/
#pragma once
#include <condition_variable>
#include <functional>
#include <mutex>
#include <optional>
#include <thread>
#include <vector>
#include <glm/glm.hpp>
#include "editor/editorProfile.hpp"
#include "world/Segment.h"
namespace ground_follow
{
using bed_query = std::function<std::vector<double>(std::vector<glm::dvec3> const &, std::vector<glm::dvec2> const &)>;
struct ends
{
double height0{0.0};
double grade0{0.0};
double height1{0.0};
double grade1{0.0};
};
struct knot_line
{
std::vector<double> knots;
std::vector<double> heights;
std::vector<double> slopes;
double grade0{0.0};
double grade1{0.0};
void refresh();
double at(double const Chainage, double &Grade) const;
};
// the grade line the track is laid on where there's ground under it: the solver of the grade lines fits it to the ground
struct grading
{
profile::line settings; // the speed and the norms of the line
profile::ground_fit fit; // how closely it keeps to the ground
double reach{100.0}; // m, how far the ground may part from the line and still be followed
};
// the ground under a track, sampled along it, and what the grade line is fitted to
struct request
{
std::vector<double> chainages;
std::vector<double> targets; // the height of the bed at each chainage, NaN where there's no ground
double total{0.0}; // m, the length in plan
ends heights;
grading grades;
bool operator==(request const &Other) const;
};
// the grade lines of the track shown while it's laid, fitted on a thread of their own: fitting one takes some milliseconds,
// tens of them on a long track over the terrain model, and the track follows the cursor frame after frame. The latest one
// asked for is fitted, the ones asked for meanwhile are left out
class fitter
{
public:
~fitter();
// the line of exactly this request, once fitted; graded false: none was found, the heights lead from end to end
bool done(request const &Request, profile::line &Line, bool &Graded) const;
// the line fitted last, of whatever request
bool latest(request &Request, profile::line &Line, bool &Graded) const;
void ask(request Request);
private:
struct result
{
request asked;
profile::line line;
bool graded{false};
};
void work();
mutable std::mutex m_mutex;
std::condition_variable m_wake;
std::optional<request> m_asked;
std::optional<request> m_fitting;
std::optional<result> m_result;
bool m_stop{false};
std::thread m_thread;
};
// where Bed finds no ground, the heights lead from one end to the other. With a fitter the line it fitted for the same ground
// is taken instead of fitting it again
void follow(std::vector<segment_data> &Pieces, ends const &Ends, bed_query const &Bed, grading const &Grading = {}, fitter const *Fitted = nullptr);
// the same, though the line is fitted by the fitter: until it has it, the track gets the line it fitted last stretched over its
// length, if it went on from the same end, or the heights from end to end
void follow_meanwhile(std::vector<segment_data> &Pieces, ends const &Ends, bed_query const &Bed, grading const &Grading, fitter &Fitter);
} // namespace ground_follow