18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 07:07:05 +01:00
Files
maszyna/application/editormodetrackbed.cpp
T
maj00r 508f600b16 fix(editor): a point clicked before the terrain under it came takes its height once it's there, Enter waits for it
The terrain is made around the cursor from the first point of a track laid, in the background since the photo begins a scenery with
no question: the first point clicked in an empty place had no ground under it, its height a guess (the flat ground, the ground
nearby), and the points after it came on the terrain made meanwhile. The line was laid from the guessed height to the real one: a
grade of thousands per mille at the second point, the track hidden in the ground there (the shots over Warszawa Zachodnia).

- click_heights (editorClickHeights): where the height of each point came from, the ground, none yet, level with the point before,
  or a track's end; the points of none take the height of the ground once it's there under them, the level ones go along, the rest
  are kept. Enter waits while the terrain is on its way under such a point, 10 s at most.
- The lay tool, in any shape: lay_bed tells where the height of the cursor came from, each point clicked keeps it. Every frame the
  points of a guessed height read the ground again (from above, as the cursor does: the terrain may lie far over the guess), the
  line shown and its checks follow them. A point laid isn't touched.
- Enter, or a free end reached, with the terrain still coming under such a point (its chunk waiting, or not asked for yet within the
  download area): the panel says "Waiting for the terrain under the points...", the line is laid as it comes; Esc gives the wait up
  keeping the points, Enter again lays without waiting.

Tests: U: the points re-read once the ground is there, the others kept, the level ones along, Enter's wait. E: tr_6_terrain_late
(nmtsettings hold holds the terrain of the NMT back, the case makes the terrain itself: the wait, Esc, laid once the terrain came; a
first point clicked before the terrain, no jump in the grade at the second); expect laywait, layguessed, laid steepest.
2026-11-24 19:13:24 +01:00

185 lines
7.1 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 "application/editormode.h"
#include "editor/editorGeometry.hpp"
#include "editor/editorGroundFollow.hpp"
#include "editor/editorSettings.hpp"
#include "utilities/Globals.h"
#include "world/Track.h"
#include <algorithm>
#include <cmath>
#include <limits>
namespace
{
using geometry::plan_distance;
using geometry::plan_of;
double constexpr kSteepest{1000.0}; // per mille: the grades of the user's own track aren't capped
double constexpr kModel{0.15}; // m, how closely the line keeps to the ground, as to the terrain model
} // namespace
glm::dvec3 editor_mode::cursor_terrain(double const Height)
{
auto point{cursor_level(Height)};
for (int i = 0; i < 5 && std::isfinite(point.x) && std::isfinite(point.z); ++i)
{
std::vector<char> found;
auto ground{ground_heights({glm::dvec3{point.x, std::max(point.y, static_cast<double>(Global.pCamera.Pos.y)), point.z}}, true, &found).front()};
if (found[0] == 0 && false == nmt_ground(point.x, point.z, ground))
ground = nearby_ground(point);
if (std::isnan(ground))
return point;
auto const next{cursor_level(ground)};
auto const settled{plan_distance(next, point) < 0.01};
point = {next.x, ground, next.z};
if (settled)
break;
}
return point;
}
double editor_mode::nearby_ground(glm::dvec3 const &Point) const
{
auto const ceiling{std::max(Point.y, static_cast<double>(Global.pCamera.Pos.y))};
for (double const radius : {10.0, 25.0, 50.0, 100.0, 200.0, 400.0})
{
std::vector<glm::dvec3> probes;
for (int i = 0; i < 12; ++i)
{
auto const angle{glm::two_pi<double>() * i / 12.0};
probes.push_back({Point.x + radius * std::cos(angle), ceiling, Point.z + radius * std::sin(angle)});
}
std::vector<char> found;
auto const heights{ground_heights(probes, true, &found)};
double sum{0.0};
int count{0};
for (std::size_t i = 0; i < probes.size(); ++i)
if (found[i] != 0)
{
sum += heights[i];
++count;
}
if (count > 0)
return sum / count;
}
return std::numeric_limits<double>::quiet_NaN();
}
std::pair<double, double> editor_mode::trackbed(TTrack const *Style)
{
return Style != nullptr ? profile_ballast(*Style) : std::pair<double, double>{0.2, 0.5 * 1.435 + 0.5 + 1.1};
}
std::vector<double> editor_mode::bed_heights(std::vector<glm::dvec3> const &Points, std::vector<glm::dvec2> const &Directions, TTrack const *Style) const
{
auto const [depth, half]{trackbed(Style)};
std::vector<glm::dvec3> probes;
probes.reserve(Points.size() * 3);
for (std::size_t i = 0; i < Points.size(); ++i)
{
auto const &direction{Directions[i]};
auto const side{glm::length(direction) > 1e-6 ? glm::normalize(glm::dvec2{-direction.y, direction.x}) * half : glm::dvec2{0.0}};
probes.push_back(Points[i]);
probes.push_back(Points[i] + glm::dvec3{side.x, 0.0, side.y});
probes.push_back(Points[i] - glm::dvec3{side.x, 0.0, side.y});
}
std::vector<char> found;
auto heights{ground_heights(probes, true, &found)};
for (std::size_t i = 0; i < probes.size(); ++i)
if (found[i] == 0 && nmt_ground(probes[i].x, probes[i].z, heights[i]))
found[i] = 1;
std::vector<double> result(Points.size(), std::numeric_limits<double>::quiet_NaN());
for (std::size_t i = 0; i < Points.size(); ++i)
{
auto const axis{found[3 * i] != 0 ? heights[3 * i] : std::numeric_limits<double>::quiet_NaN()};
auto toe{std::numeric_limits<double>::infinity()};
for (std::size_t j = 3 * i + 1; j < 3 * i + 3; ++j)
if (found[j] != 0)
toe = std::min(toe, heights[j]);
if (std::isfinite(toe))
result[i] = std::isnan(axis) ? toe + depth : std::max(toe + depth, axis);
else if (false == std::isnan(axis))
result[i] = axis + depth;
}
return result;
}
double editor_mode::bed_height(glm::dvec3 const &Point, glm::dvec2 const &Direction, TTrack const *Style) const
{
auto const height{bed_heights({Point}, {Direction}, Style).front()};
if (false == std::isnan(height))
return height;
auto const nearby{nearby_ground(Point)};
return std::isnan(nearby) ? Point.y : nearby + trackbed(Style).first;
}
void editor_mode::follow_ground(std::vector<segment_data> &Pieces, double const Height0, double const Grade0, double const Height1, double const Grade1, TTrack const *Style) const
{
// from point to point or level the heights lead from one end to the other, whatever the ground between
if (EditorSettings.heights() != editorSettings::lay_heights::fitted)
{
ground_follow::follow(Pieces, {Height0, Grade0, Height1, Grade1}, [](std::vector<glm::dvec3> const &Points, std::vector<glm::dvec2> const &) { return std::vector<double>(Points.size(), std::numeric_limits<double>::quiet_NaN()); });
return;
}
auto const grading{laying_grading(Style)};
auto const bed = [&](std::vector<glm::dvec3> const &Points, std::vector<glm::dvec2> const &Directions) { return bed_heights(Points, Directions, Style); };
if (m_follow_preview)
ground_follow::follow_meanwhile(Pieces, {Height0, Grade0, Height1, Grade1}, bed, grading, m_fitter);
else
ground_follow::follow(Pieces, {Height0, Grade0, Height1, Grade1}, bed, grading, &m_fitter);
}
ground_follow::grading editor_mode::laying_grading(TTrack const *Style) const
{
// the line goes over the ground as the tracks go onto the terrain: the vertical curves of the norms, the grades as steep as the
// ground has them, a rise or a hollow longer than a structure followed closely, the bumps and what parts from it gone past
ground_follow::grading grading;
grading.settings.norms = m_profile.line.norms;
grading.settings.norms.grade_max = kSteepest;
grading.settings.norms.curve_threshold = 0.0;
grading.settings.speed = design_speed(Style, nullptr);
grading.fit.grade_max = kSteepest;
grading.fit.tolerance = kModel;
grading.fit.follow_length = 60.0;
grading.fit.follow_height = 4.0;
grading.fit.follow_steepest = 100.0;
grading.fit.follow_tolerance = 0.05;
grading.reach = kModel;
return grading;
}
glm::dvec3 editor_mode::lay_bed(glm::dvec3 const &Point, click_heights::source *Source)
{
auto const &tool{m_lay};
// level: on at the height the laying goes on from, the first point on the ground
if (EditorSettings.heights() == editorSettings::lay_heights::level && false == tool.points.empty())
{
if (Source != nullptr)
*Source = click_heights::source::level;
return {Point.x, tool.points.back().y, Point.z};
}
TTrack const *style{tool.start.track != nullptr ? tool.start.track : tool.copy_selected ? selected_track() : nullptr};
auto const direction{tool.points.empty() ? glm::dvec2{0.0} : plan_of(Point - tool.points.back())};
// no ground there yet, nor the model of it: the height a guess, taken again once the terrain comes
auto const height{bed_heights({Point}, {direction}, style).front()};
if (Source != nullptr)
*Source = std::isnan(height) ? click_heights::source::none : click_heights::source::ground;
if (false == std::isnan(height))
return {Point.x, height, Point.z};
auto const nearby{nearby_ground(Point)};
return {Point.x, std::isnan(nearby) ? Point.y : nearby + trackbed(style).first, Point.z};
}