mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 06:35:21 +01:00
fix(editor): the brush after the review of 30.11: a second uniform stroke doubles nothing, the step of a stroke by its serial
- the uniform fill is told of the models which stand near the dab already (scatter::dab(), Standing) and leaves the points of its grid alone where one stands: a second stroke over the same ground adds nothing, as the description says. After a Ctrl+Z of the first the ground is filled again - the step of the history of a stroke is made with its first model and looked up by its serial for each model (editor_mode::brush_step()): a step pushed while the button is held (Del) no longer takes the later models of the stroke, and a press which puts nothing (on the sky, no model chosen) leaves the steps to redo as they were - the settings of the brush are bounded before they are used (scatter: sane()): a number typed into a field which isn't finite gives no place nor transform which isn't, and the fields clamp what is typed (ImGuiSliderFlags_AlwaysClamp). Within the ranges of the panel nothing changes - tests: the second stroke, the settings which aren't numbers, the ranges of the panel - redo_last() ends the stroke after the step of the terrain, and tests/unit/CMakeLists.txt lists editorScatter.cpp apart from the lines #311, #430 and #431 add, so the four merge without a conflict
This commit is contained in:
@@ -1190,11 +1190,12 @@ void editor_mode::undo_one()
|
||||
|
||||
void editor_mode::redo_last()
|
||||
{
|
||||
end_brush_stroke();
|
||||
// a change of the terrain made since the undo goes to the history, and the redo steps are dropped with it
|
||||
m_history_busy = true;
|
||||
terrain_history_step();
|
||||
m_history_busy = false;
|
||||
// Ctrl+Y with the brush still held: the stroke is over and the dabs stop, its step stays as it is
|
||||
end_brush_stroke();
|
||||
// the first of the changes made together is on the top, the others follow it
|
||||
std::vector<scene::basic_node *> touched;
|
||||
do
|
||||
|
||||
@@ -227,6 +227,11 @@ class editor_mode : public application_mode, private editor_track::observer, pri
|
||||
double brush_height(glm::dvec2 const &Point, double Around);
|
||||
// one dab of the stroke of many models, at the cursor on the ground
|
||||
void brush_dab();
|
||||
// the step of the history of the stroke going on, looked up by its serial; Make: made if the stroke has none yet, with its
|
||||
// first model. nullptr: the stroke has no step (yet, or any more)
|
||||
EditorSnapshot *brush_step(bool Make);
|
||||
// the places (x, z) of the models of the scene within Reach of Centre
|
||||
std::vector<glm::dvec2> brush_standing(glm::dvec2 const &Centre, double Reach) const;
|
||||
// the many along a line: a point of it at the cursor, the models put along it, the line and its band drawn on the ground
|
||||
// the brush is the one of many models along a line: the keys of the line (Enter, Backspace, Esc) are its
|
||||
bool brush_on_line();
|
||||
@@ -264,7 +269,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri
|
||||
bool m_dragging = false;
|
||||
glm::dvec3 oldPos; // world position of the last dab of the brush
|
||||
bool m_brush_has_last{false}; // false at the start of a brush stroke, so the first dab ignores the step
|
||||
bool m_brush_stroke{false}; // a stroke of the brush of many models is on: the last step of the history takes its models
|
||||
bool m_brush_stroke{false}; // a stroke of the brush of many models is on
|
||||
std::uint64_t m_brush_step{0}; // the serial of the step of the history the stroke puts its models into, 0 before its first model
|
||||
scatter::stroke m_brush_done; // what the stroke has done so far: the places of its models and the circles of its dabs
|
||||
std::vector<glm::dvec3> m_brush_line; // the points of the line the brush of many puts models along, in the order clicked
|
||||
std::mt19937 m_brush_random{std::random_device{}()}; // NOSONAR(cpp:S2245) where the brush puts models, not security
|
||||
|
||||
@@ -20,9 +20,11 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "simulation/simulation.h"
|
||||
#include "utilities/translation.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -88,13 +90,10 @@ void editor_mode::brush_press()
|
||||
}
|
||||
if (settings.way != brush_object_list::kind::single)
|
||||
{
|
||||
// one step of the history for the whole stroke, the models added to it as they're put
|
||||
trim_history();
|
||||
EditorSnapshot stroke;
|
||||
stroke.action = EditorSnapshot::Action::Array;
|
||||
m_history.push_back(std::move(stroke));
|
||||
g_redo.clear();
|
||||
// one step of the history for the whole stroke, made when its first model is put (brush_step()): a press which puts
|
||||
// nothing, on the sky or with no model chosen, leaves the history and the steps to redo as they were
|
||||
m_brush_stroke = true;
|
||||
m_brush_step = 0;
|
||||
return;
|
||||
}
|
||||
// the point is read once the renderer has the depth under the cursor of the press
|
||||
@@ -126,8 +125,15 @@ void editor_mode::brush_dab()
|
||||
{
|
||||
// picks are resolved a few frames later; drop the ones queued before the button went up, or for a press that turned out
|
||||
// to belong to the UI
|
||||
if (false == mouseHold || false == m_brush_stroke || false == viewport_click() || m_history.empty())
|
||||
if (false == mouseHold || false == m_brush_stroke || false == viewport_click())
|
||||
return;
|
||||
// the step the stroke fills is gone from the history (taken back, or pushed out by the steps made since): the stroke is over
|
||||
if (m_brush_step != 0 && brush_step(false) == nullptr)
|
||||
{
|
||||
m_brush_stroke = false;
|
||||
m_brush_step = 0;
|
||||
return;
|
||||
}
|
||||
glm::dvec3 centre;
|
||||
if (false == brush_ground(centre))
|
||||
return;
|
||||
@@ -137,10 +143,13 @@ void editor_mode::brush_dab()
|
||||
return;
|
||||
oldPos = centre;
|
||||
m_brush_has_last = true;
|
||||
auto &stroke{m_history.back()};
|
||||
// the dab goes to what the stroke has done: its circle isn't filled again, and its places are taken whether a model stands
|
||||
// there or failed, so it isn't tried there on every move
|
||||
auto const points{scatter::dab({centre.x, centre.z}, settings, m_brush_done, m_brush_random)};
|
||||
// there or failed, so it isn't tried there on every move. the uniform fill is also told of the models which stand there
|
||||
// already: its grid is fixed to the world, and a second stroke over the same ground would put every model again
|
||||
std::vector<glm::dvec2> standing;
|
||||
if (settings.mode == scatter::fill::uniform)
|
||||
standing = brush_standing({centre.x, centre.z}, std::max(0.1, settings.radius) + std::max(0.05, settings.spacing));
|
||||
auto const points{scatter::dab({centre.x, centre.z}, settings, m_brush_done, m_brush_random, standing)};
|
||||
if (auto const cut_short{settings.mode != scatter::fill::uniform && scatter::dab_wanted(settings) > scatter::dab_most}; cut_short || static_cast<int>(points.size()) >= scatter::dab_most)
|
||||
ui()->set_status(format(STR_C("Brush: a dab puts %d models at most; widen the spacing or narrow the circle for the rest"), scatter::dab_most));
|
||||
for (auto const &point : points)
|
||||
@@ -148,29 +157,61 @@ void editor_mode::brush_dab()
|
||||
auto *model{place_brush_model({point.x, brush_height(point, centre.y), point.y})};
|
||||
if (model == nullptr)
|
||||
continue;
|
||||
// the step of the stroke is looked up by its serial for each model, never taken for the last one of the history: a
|
||||
// step another key pushed while the button is held (Del) doesn't get the models of the stroke
|
||||
auto *stroke{brush_step(true)};
|
||||
EditorSnapshot::array_copy copy;
|
||||
copy.model = model;
|
||||
copy.uuid = model->uuid;
|
||||
stroke.layer = model->layer();
|
||||
stroke.copies.push_back(std::move(copy));
|
||||
stroke->layer = model->layer();
|
||||
stroke->copies.push_back(std::move(copy));
|
||||
}
|
||||
}
|
||||
|
||||
editor_mode::EditorSnapshot *editor_mode::brush_step(bool const Make)
|
||||
{
|
||||
if (m_brush_step != 0)
|
||||
{
|
||||
// from the end: the step of the stroke is the last one, or among the last
|
||||
auto const found{std::ranges::find_if(m_history.rbegin(), m_history.rend(), [this](EditorSnapshot const &Step) { return Step.serial == m_brush_step; })};
|
||||
return found != m_history.rend() ? &*found : nullptr;
|
||||
}
|
||||
if (false == Make)
|
||||
return nullptr;
|
||||
trim_history();
|
||||
EditorSnapshot stroke;
|
||||
stroke.action = EditorSnapshot::Action::Array;
|
||||
m_brush_step = stroke.serial;
|
||||
m_history.push_back(std::move(stroke));
|
||||
g_redo.clear();
|
||||
return &m_history.back();
|
||||
}
|
||||
|
||||
std::vector<glm::dvec2> editor_mode::brush_standing(glm::dvec2 const &Centre, double const Reach) const
|
||||
{
|
||||
std::vector<glm::dvec2> standing;
|
||||
for (auto const *model : simulation::Instances.sequence())
|
||||
{
|
||||
if (model == nullptr)
|
||||
continue;
|
||||
auto const &at{model->location()};
|
||||
if (glm::dvec2 const place{at.x, at.z}; glm::distance(place, Centre) <= Reach)
|
||||
standing.push_back(place);
|
||||
}
|
||||
return standing;
|
||||
}
|
||||
|
||||
void editor_mode::end_brush_stroke()
|
||||
{
|
||||
if (false == m_brush_stroke)
|
||||
return;
|
||||
m_brush_stroke = false;
|
||||
if (m_history.empty() || m_history.back().action != EditorSnapshot::Action::Array)
|
||||
// nothing put: the stroke made no step; or its step is gone from the history already
|
||||
auto const *step{brush_step(false)};
|
||||
m_brush_step = 0;
|
||||
if (step == nullptr)
|
||||
return;
|
||||
auto const count{m_history.back().copies.size()};
|
||||
if (count == 0)
|
||||
{
|
||||
// nothing put: no empty step in the history
|
||||
m_history.pop_back();
|
||||
return;
|
||||
}
|
||||
ui()->set_status(format(STR_C("Brush: %zu models put; Ctrl+Z takes the whole stroke back"), count));
|
||||
ui()->set_status(format(STR_C("Brush: %zu models put; Ctrl+Z takes the whole stroke back"), step->copies.size()));
|
||||
}
|
||||
|
||||
bool editor_mode::brush_on_line()
|
||||
|
||||
@@ -637,19 +637,19 @@ void brush_object_list::render_dab()
|
||||
return;
|
||||
}
|
||||
ImGui::TextDisabled("%s", STR_C("Hold LMB: models in the circle along the cursor's path; one Ctrl+Z takes the whole stroke back"));
|
||||
if (auto radius{static_cast<float>(dab.radius)}; ImGui::SliderFloat(STR_C("Radius##brush"), &radius, 0.5f, 100.0f, "%.1f m", ImGuiSliderFlags_Logarithmic))
|
||||
if (auto radius{static_cast<float>(dab.radius)}; ImGui::SliderFloat(STR_C("Radius##brush"), &radius, 0.5f, 100.0f, "%.1f m", ImGuiSliderFlags_Logarithmic | ImGuiSliderFlags_AlwaysClamp))
|
||||
dab.radius = radius;
|
||||
std::array<char const *, 3> const fills{STR_C("Random, anywhere in the circle"), STR_C("Uniform (grid)"), STR_C("Feathered to the edge")};
|
||||
ImGui::SetNextItemWidth(220.0f);
|
||||
if (int fillmode{static_cast<int>(dab.mode)}; ImGui::Combo(STR_C("Fill##brush"), &fillmode, fills.data(), static_cast<int>(fills.size())))
|
||||
dab.mode = static_cast<scatter::fill>(fillmode);
|
||||
if (auto spacing{static_cast<float>(dab.spacing)}; ImGui::SliderFloat(STR_C("Spacing##brush"), &spacing, 0.1f, 50.0f, "%.1f m", ImGuiSliderFlags_Logarithmic))
|
||||
if (auto spacing{static_cast<float>(dab.spacing)}; ImGui::SliderFloat(STR_C("Spacing##brush"), &spacing, 0.1f, 50.0f, "%.1f m", ImGuiSliderFlags_Logarithmic | ImGuiSliderFlags_AlwaysClamp))
|
||||
dab.spacing = spacing;
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("%s", dab.mode == scatter::fill::uniform ? STR_C("The step of the grid the models stand on") : STR_C("The least distance between two models"));
|
||||
if (dab.mode == scatter::fill::uniform)
|
||||
return;
|
||||
if (auto density{static_cast<float>(dab.density)}; ImGui::SliderFloat(STR_C("Density##brush"), &density, 0.1f, 200.0f, STR_C("%.1f per 100 m²"), ImGuiSliderFlags_Logarithmic))
|
||||
if (auto density{static_cast<float>(dab.density)}; ImGui::SliderFloat(STR_C("Density##brush"), &density, 0.1f, 200.0f, STR_C("%.1f per 100 m²"), ImGuiSliderFlags_Logarithmic | ImGuiSliderFlags_AlwaysClamp))
|
||||
dab.density = density;
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("%s", dab.mode == scatter::fill::feathered ? STR_C("How many models a 100 m² the middle of the stroke gets, fewer and fewer towards its edge, as many as the spacing lets in.\nGround the stroke has gone over gets no more when the cursor comes back over it") :
|
||||
@@ -659,7 +659,7 @@ void brush_object_list::render_dab()
|
||||
void brush_object_list::render_line()
|
||||
{
|
||||
ImGui::TextDisabled("%s", STR_C("LMB: the points of the line; Enter puts the models, Backspace takes the last point back, Esc drops the line"));
|
||||
if (auto width{static_cast<float>(line.width)}; ImGui::SliderFloat(STR_C("Width each side##brushline"), &width, 0.0f, 100.0f, "%.1f m"))
|
||||
if (auto width{static_cast<float>(line.width)}; ImGui::SliderFloat(STR_C("Width each side##brushline"), &width, 0.0f, 100.0f, "%.1f m", ImGuiSliderFlags_AlwaysClamp))
|
||||
line.width = width;
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("%s", STR_C("How far to each side of the line the models go; 0: one row on the line itself"));
|
||||
@@ -668,24 +668,24 @@ void brush_object_list::render_line()
|
||||
if (int fillmode{static_cast<int>(line.mode)}; ImGui::Combo(STR_C("Fill##brushline"), &fillmode, fills.data(), static_cast<int>(fills.size())))
|
||||
line.mode = static_cast<scatter::line_fill>(fillmode);
|
||||
auto const regular{line.mode == scatter::line_fill::regular};
|
||||
if (auto spacing{static_cast<float>(line.spacing)}; ImGui::SliderFloat(STR_C("Spacing##brushline"), &spacing, 0.1f, 50.0f, "%.1f m", ImGuiSliderFlags_Logarithmic))
|
||||
if (auto spacing{static_cast<float>(line.spacing)}; ImGui::SliderFloat(STR_C("Spacing##brushline"), &spacing, 0.1f, 50.0f, "%.1f m", ImGuiSliderFlags_Logarithmic | ImGuiSliderFlags_AlwaysClamp))
|
||||
line.spacing = spacing;
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("%s", regular ? STR_C("The step along the line and between the rows") : STR_C("The least distance between two models"));
|
||||
if (regular)
|
||||
{
|
||||
if (auto jitter{static_cast<float>(line.jitter * 100.0)}; ImGui::SliderFloat(STR_C("Irregularity##brushline"), &jitter, 0.0f, 50.0f, "%.0f%%"))
|
||||
if (auto jitter{static_cast<float>(line.jitter * 100.0)}; ImGui::SliderFloat(STR_C("Irregularity##brushline"), &jitter, 0.0f, 50.0f, "%.0f%%", ImGuiSliderFlags_AlwaysClamp))
|
||||
line.jitter = jitter / 100.0;
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("%s", STR_C("Each model moved off its place in the rows by up to this share of the spacing each way; 0: exactly in rows"));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (auto density{static_cast<float>(line.density)}; ImGui::SliderFloat(STR_C("Density##brushline"), &density, 0.1f, 200.0f, STR_C("%.1f per 100 m²"), ImGuiSliderFlags_Logarithmic))
|
||||
if (auto density{static_cast<float>(line.density)}; ImGui::SliderFloat(STR_C("Density##brushline"), &density, 0.1f, 200.0f, STR_C("%.1f per 100 m²"), ImGuiSliderFlags_Logarithmic | ImGuiSliderFlags_AlwaysClamp))
|
||||
line.density = density;
|
||||
ImGui::Checkbox(STR_C("Fewer towards the edges##brushline"), &line.thin_out);
|
||||
}
|
||||
if (auto edge{static_cast<float>(line.edge_scale)}; ImGui::SliderFloat(STR_C("Size at the edges##brushline"), &edge, 1.0f, 200.0f, "%.0f%%"))
|
||||
if (auto edge{static_cast<float>(line.edge_scale)}; ImGui::SliderFloat(STR_C("Size at the edges##brushline"), &edge, 1.0f, 200.0f, "%.0f%%", ImGuiSliderFlags_AlwaysClamp))
|
||||
line.edge_scale = edge;
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("%s", STR_C("The size of the models at the edges of the band, as a share of their size on the line; between, it goes evenly from one to the other"));
|
||||
@@ -702,10 +702,10 @@ void brush_object_list::render_look()
|
||||
if (ImGui::RadioButton(STR_C("Random##brushturn"), turn_random))
|
||||
look.turn = random;
|
||||
if (look.turn == fixed)
|
||||
ImGui::DragFloat3(STR_C("Angles x y z##brush"), &look.angles.x, 0.5f, -180.0f, 180.0f, "%.1f°");
|
||||
ImGui::DragFloat3(STR_C("Angles x y z##brush"), &look.angles.x, 0.5f, -180.0f, 180.0f, "%.1f°", ImGuiSliderFlags_AlwaysClamp);
|
||||
else
|
||||
{
|
||||
ImGui::DragFloat3(STR_C("Each way x y z##brushturn"), &look.angle_range.x, 0.5f, 0.0f, 180.0f, "±%.1f°");
|
||||
ImGui::DragFloat3(STR_C("Each way x y z##brushturn"), &look.angle_range.x, 0.5f, 0.0f, 180.0f, "±%.1f°", ImGuiSliderFlags_AlwaysClamp);
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("%s", STR_C("Each model turned by a random angle up to this much each way: x and z tilt it, y turns it round (180: any way)"));
|
||||
}
|
||||
@@ -717,14 +717,14 @@ void brush_object_list::render_look()
|
||||
if (ImGui::RadioButton(STR_C("Random##brushscale"), size_random))
|
||||
look.size = random;
|
||||
if (look.size == fixed)
|
||||
ImGui::DragFloat3(STR_C("Percent x y z##brush"), &look.scale.x, 0.5f, 1.0f, 1000.0f, "%.0f%%");
|
||||
ImGui::DragFloat3(STR_C("Percent x y z##brush"), &look.scale.x, 0.5f, 1.0f, 1000.0f, "%.0f%%", ImGuiSliderFlags_AlwaysClamp);
|
||||
else
|
||||
{
|
||||
ImGui::Checkbox(STR_C("The same on every axis##brush"), &look.same_scale);
|
||||
if (look.same_scale)
|
||||
ImGui::DragFloat(STR_C("Each way##brushscale"), &look.scale_range.x, 0.5f, 0.0f, 99.0f, "±%.0f%%");
|
||||
ImGui::DragFloat(STR_C("Each way##brushscale"), &look.scale_range.x, 0.5f, 0.0f, 99.0f, "±%.0f%%", ImGuiSliderFlags_AlwaysClamp);
|
||||
else
|
||||
ImGui::DragFloat3(STR_C("Each way x y z##brushscale"), &look.scale_range.x, 0.5f, 0.0f, 99.0f, "±%.0f%%");
|
||||
ImGui::DragFloat3(STR_C("Each way x y z##brushscale"), &look.scale_range.x, 0.5f, 0.0f, 99.0f, "±%.0f%%", ImGuiSliderFlags_AlwaysClamp);
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("%s", STR_C("Each model scaled by a random share up to this much each way from its own size"));
|
||||
}
|
||||
|
||||
@@ -14,6 +14,50 @@ namespace scatter
|
||||
namespace
|
||||
{
|
||||
|
||||
// a setting typed into a field of the panel may be anything a float can hold, "inf" and "nan" too, and what the fields
|
||||
// don't stop reaches here. one which isn't a finite number is the Fallback, a finite one is kept between Least and Most: far
|
||||
// more than the panel offers, so within its ranges nothing changes, and no place or transform of a model comes out of a
|
||||
// number which isn't one
|
||||
double bounded(double const Value, double const Least, double const Most, double const Fallback)
|
||||
{
|
||||
return std::isfinite(Value) ? std::clamp(Value, Least, Most) : Fallback;
|
||||
}
|
||||
|
||||
glm::vec3 bounded(glm::vec3 const &Value, float const Least, float const Most, float const Fallback)
|
||||
{
|
||||
return {static_cast<float>(bounded(Value.x, Least, Most, Fallback)), static_cast<float>(bounded(Value.y, Least, Most, Fallback)), static_cast<float>(bounded(Value.z, Least, Most, Fallback))};
|
||||
}
|
||||
|
||||
double constexpr most_metres{1000.0}; // of a radius, a spacing or a width
|
||||
double constexpr most_density{1.0e6};
|
||||
|
||||
dab_settings sane(dab_settings Settings)
|
||||
{
|
||||
Settings.radius = bounded(Settings.radius, 0.0, most_metres, 0.0);
|
||||
Settings.spacing = bounded(Settings.spacing, 0.0, most_metres, 0.0);
|
||||
Settings.density = bounded(Settings.density, 0.0, most_density, 0.0);
|
||||
return Settings;
|
||||
}
|
||||
|
||||
line_settings sane(line_settings Settings)
|
||||
{
|
||||
Settings.width = bounded(Settings.width, 0.0, most_metres, 0.0);
|
||||
Settings.spacing = bounded(Settings.spacing, 0.0, most_metres, 0.0);
|
||||
Settings.jitter = bounded(Settings.jitter, 0.0, 0.5, 0.0);
|
||||
Settings.density = bounded(Settings.density, 0.0, most_density, 0.0);
|
||||
Settings.edge_scale = bounded(Settings.edge_scale, 1.0, 10000.0, 100.0);
|
||||
return Settings;
|
||||
}
|
||||
|
||||
look_settings sane(look_settings Settings)
|
||||
{
|
||||
Settings.angles = bounded(Settings.angles, -36000.0f, 36000.0f, 0.0f);
|
||||
Settings.angle_range = bounded(Settings.angle_range, 0.0f, 36000.0f, 0.0f);
|
||||
Settings.scale = bounded(Settings.scale, 1.0f, 100000.0f, 100.0f);
|
||||
Settings.scale_range = bounded(Settings.scale_range, 0.0f, 10000.0f, 0.0f);
|
||||
return Settings;
|
||||
}
|
||||
|
||||
bool clear_of(glm::dvec2 const &Point, double const Spacing, std::vector<glm::dvec2> const &Points)
|
||||
{
|
||||
auto const least{Spacing * Spacing};
|
||||
@@ -327,13 +371,14 @@ class fresh_ground
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<line_place> along(std::vector<glm::dvec3> const &Points, line_settings const &Settings, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
|
||||
std::vector<line_place> along(std::vector<glm::dvec3> const &Points, line_settings const &Asked, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
|
||||
{
|
||||
if (Points.size() < 2)
|
||||
return {};
|
||||
polyline const line{Points};
|
||||
if (line.length() <= 0.0)
|
||||
return {};
|
||||
auto const Settings{sane(Asked)};
|
||||
auto const spacing{std::max(0.1, Settings.spacing)};
|
||||
if (Settings.mode == line_fill::irregular)
|
||||
return irregular_line(line, Settings, spacing, Random);
|
||||
@@ -342,21 +387,30 @@ std::vector<line_place> along(std::vector<glm::dvec3> const &Points, line_settin
|
||||
|
||||
float side_scale(line_settings const &Settings, double const Side)
|
||||
{
|
||||
auto const edge{std::max(1.0, Settings.edge_scale) / 100.0};
|
||||
return static_cast<float>(std::lerp(1.0, edge, std::clamp(Side, 0.0, 1.0)));
|
||||
auto const edge{std::max(1.0, sane(Settings).edge_scale) / 100.0};
|
||||
return static_cast<float>(std::lerp(1.0, edge, bounded(Side, 0.0, 1.0, 0.0)));
|
||||
}
|
||||
|
||||
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settings, stroke &Stroke, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
|
||||
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Asked, stroke &Stroke, std::mt19937 &Random, std::vector<glm::dvec2> const &Standing) // NOSONAR(cpp:S2245) placement of scenery models, not security
|
||||
{
|
||||
if (false == std::isfinite(Centre.x) || false == std::isfinite(Centre.y))
|
||||
return {};
|
||||
auto const Settings{sane(Asked)};
|
||||
auto const radius{std::max(0.1, Settings.radius)};
|
||||
auto const spacing{std::max(0.05, Settings.spacing)};
|
||||
// only what the stroke put near this circle matters: a long stroke isn't gone through whole for every point
|
||||
std::vector<glm::dvec2> nearby;
|
||||
auto const reach{radius + spacing};
|
||||
std::ranges::copy_if(Stroke.taken, std::back_inserter(nearby), [&](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; });
|
||||
auto const within{[&Centre, reach](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; }};
|
||||
std::ranges::copy_if(Stroke.taken, std::back_inserter(nearby), within);
|
||||
std::vector<glm::dvec2> made;
|
||||
if (Settings.mode == fill::uniform)
|
||||
{
|
||||
// the grid is fixed to the world, so a stroke over the ground of an earlier one comes to the very same points: those
|
||||
// where a model stands already are left alone
|
||||
std::ranges::copy_if(Standing, std::back_inserter(nearby), within);
|
||||
made = grid_points(Centre, radius, spacing, nearby);
|
||||
}
|
||||
else
|
||||
made = fresh_ground{Centre, radius, Settings.mode, Stroke.dabs}.places(tries_for(dab_wanted(Settings), Random), spacing, nearby, Random);
|
||||
Stroke.taken.insert(Stroke.taken.end(), made.begin(), made.end());
|
||||
@@ -364,19 +418,22 @@ std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settin
|
||||
return made;
|
||||
}
|
||||
|
||||
double dab_wanted(dab_settings const &Settings)
|
||||
double dab_wanted(dab_settings const &Asked)
|
||||
{
|
||||
auto const Settings{sane(Asked)};
|
||||
auto const radius{std::max(0.1, Settings.radius)};
|
||||
return std::max(0.0, Settings.density) * std::numbers::pi * radius * radius / 100.0;
|
||||
}
|
||||
|
||||
double dab_step(dab_settings const &Settings)
|
||||
double dab_step(dab_settings const &Asked)
|
||||
{
|
||||
auto const Settings{sane(Asked)};
|
||||
return std::max({0.05, Settings.spacing, 0.5 * Settings.radius});
|
||||
}
|
||||
|
||||
look pick(look_settings const &Settings, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
|
||||
look pick(look_settings const &Asked, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
|
||||
{
|
||||
auto const Settings{sane(Asked)};
|
||||
look out;
|
||||
auto const spread = [&](float const Range) {
|
||||
if (Range <= 0.0f)
|
||||
|
||||
@@ -15,7 +15,9 @@ namespace scatter
|
||||
enum class fill
|
||||
{
|
||||
random, // anywhere in the circle, no closer to each other than the spacing
|
||||
uniform, // on a grid of triangles as wide as the spacing, fixed to the world: strokes over one another don't double it
|
||||
// on a grid of triangles as wide as the spacing, fixed to the world: strokes over one another come to the same points, and a
|
||||
// point where a model stands already (the Standing ones given to dab()) gets no second one
|
||||
uniform,
|
||||
feathered, // anywhere, fewer and fewer towards the edge of the circle
|
||||
};
|
||||
|
||||
@@ -50,8 +52,12 @@ int constexpr dab_most{2000};
|
||||
|
||||
// the places of the models of one dab of the brush round Centre (x, z): inside the circle, Spacing from each other and from those
|
||||
// the Stroke put so far at least, on the ground the stroke hasn't filled yet. the dab is added to the Stroke: its circle, and its
|
||||
// places, whether a model ends up standing there or not
|
||||
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settings, stroke &Stroke, std::mt19937 &Random); // NOSONAR(cpp:S2245) placement of scenery models, not security
|
||||
// places, whether a model ends up standing there or not.
|
||||
// Standing: the places (x, z) of the models which stand near the circle already, whoever put them. the uniform fill leaves the
|
||||
// points of its grid alone where one of them stands within half the spacing, so a second stroke over the same ground adds
|
||||
// nothing to the first; the other fills don't look at them.
|
||||
// settings which aren't finite numbers are taken for the least ones, far too large ones are cut (see sane() in the .cpp)
|
||||
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settings, stroke &Stroke, std::mt19937 &Random, std::vector<glm::dvec2> const &Standing = {}); // NOSONAR(cpp:S2245) placement of scenery models, not security
|
||||
|
||||
// how many models the density asks for in the whole circle of a dab of the random or the feathered fill; more than dab_most
|
||||
// of them and the dab is cut short
|
||||
|
||||
@@ -27,6 +27,8 @@ add_executable(${UNIT_TESTS_NAME}
|
||||
# and the heights of the tracks laid
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorGroundFollow.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorProfile.cpp"
|
||||
# and the places the brush of the editor puts its models at
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorScatter.cpp"
|
||||
# and the switches laid into the curves
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorSwitchCurve.cpp"
|
||||
# and the fouling points of the switches, widened in the curves as the gauge is
|
||||
@@ -38,7 +40,6 @@ add_executable(${UNIT_TESTS_NAME}
|
||||
"${CMAKE_SOURCE_DIR}/rendering/common/freecameramotion.cpp"
|
||||
# and the http client the editor fetches its aids with
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorHttp.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorScatter.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/utilities/asciiname.cpp"
|
||||
# and what the switch tool tells of the drives of the switches
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorSwitchDrives.cpp"
|
||||
|
||||
@@ -6,7 +6,11 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -364,3 +368,171 @@ TEST_CASE("scatter: the size of a model across the band, full on the line, as se
|
||||
std::mt19937 random{1}; // NOSONAR(cpp:S2245) a repeatable test, not security
|
||||
CHECK(scatter::along({{0.0, 0.0, 0.0}}, settings, random).empty());
|
||||
}
|
||||
|
||||
TEST_CASE("scatter: a second uniform stroke over the ground of the first adds nothing where its models stand")
|
||||
{
|
||||
std::mt19937 random{11}; // NOSONAR(cpp:S2245) a repeatable test, not security
|
||||
scatter::dab_settings settings;
|
||||
settings.radius = 6.0;
|
||||
settings.mode = scatter::fill::uniform;
|
||||
settings.spacing = 2.0;
|
||||
// the first stroke: two dabs
|
||||
scatter::stroke first;
|
||||
auto standing{scatter::dab({10.0, 20.0}, settings, first, random)};
|
||||
auto const more{scatter::dab({13.0, 20.0}, settings, first, random)};
|
||||
standing.insert(standing.end(), more.begin(), more.end());
|
||||
REQUIRE(standing.size() > 20);
|
||||
// the button let go and pressed again: a stroke of its own, which knows nothing of the first but the models standing
|
||||
scatter::stroke second;
|
||||
CHECK(scatter::dab({10.0, 20.0}, settings, second, random, standing).empty());
|
||||
CHECK(scatter::dab({13.0, 20.0}, settings, second, random, standing).empty());
|
||||
// a little further on it puts only the points of the grid nothing stands at, each of them once
|
||||
auto const further{scatter::dab({16.0, 20.0}, settings, second, random, standing)};
|
||||
CHECK(false == further.empty());
|
||||
for (auto const &p : further)
|
||||
for (auto const &q : standing)
|
||||
CHECK(glm::distance(p, q) >= 2.0 - 1e-9);
|
||||
// without the models standing (the first stroke taken back with Ctrl+Z) the ground is filled as the first time
|
||||
scatter::stroke again;
|
||||
std::set<std::pair<long long, long long>> first_points;
|
||||
std::set<std::pair<long long, long long>> again_points;
|
||||
auto const key{[](glm::dvec2 const &Point) { return std::pair{std::llround(Point.x * 1000.0), std::llround(Point.y * 1000.0)}; }};
|
||||
scatter::stroke one;
|
||||
for (auto const &p : scatter::dab({10.0, 20.0}, settings, one, random))
|
||||
first_points.insert(key(p));
|
||||
for (auto const &p : scatter::dab({10.0, 20.0}, settings, again, random, {}))
|
||||
again_points.insert(key(p));
|
||||
CHECK(first_points == again_points);
|
||||
// a model of the scene standing between the points of the grid, further than half the spacing from each, takes none of them
|
||||
scatter::stroke beside;
|
||||
std::vector<glm::dvec2> const off{{10.0, 20.0 + 1.0e3}};
|
||||
CHECK(scatter::dab({10.0, 20.0}, settings, beside, random, off).size() == first_points.size());
|
||||
// the other fills don't look at the models standing: a second random stroke adds its models as before
|
||||
settings.mode = scatter::fill::random;
|
||||
settings.density = 20.0;
|
||||
scatter::stroke with;
|
||||
scatter::stroke without;
|
||||
std::mt19937 same_a{5}; // NOSONAR(cpp:S2245) a repeatable test, not security
|
||||
std::mt19937 same_b{5}; // NOSONAR(cpp:S2245) a repeatable test, not security
|
||||
CHECK(scatter::dab({10.0, 20.0}, settings, with, same_a, standing).size() == scatter::dab({10.0, 20.0}, settings, without, same_b).size());
|
||||
}
|
||||
|
||||
TEST_CASE("scatter: settings which aren't finite numbers give places and transforms which are")
|
||||
{
|
||||
auto const nan{std::numeric_limits<double>::quiet_NaN()};
|
||||
auto const inf{std::numeric_limits<double>::infinity()};
|
||||
std::mt19937 random{2}; // NOSONAR(cpp:S2245) a repeatable test, not security
|
||||
auto const finite{[](glm::dvec2 const &Point) { return std::isfinite(Point.x) && std::isfinite(Point.y); }};
|
||||
for (auto const mode : {scatter::fill::random, scatter::fill::uniform, scatter::fill::feathered})
|
||||
{
|
||||
for (auto const bad : {nan, inf, -inf, 1.0e39})
|
||||
{
|
||||
CAPTURE(static_cast<int>(mode));
|
||||
CAPTURE(bad);
|
||||
scatter::dab_settings settings;
|
||||
settings.mode = mode;
|
||||
settings.radius = bad;
|
||||
settings.spacing = bad;
|
||||
settings.density = bad;
|
||||
scatter::stroke stroke;
|
||||
auto const points{scatter::dab({0.0, 0.0}, settings, stroke, random)};
|
||||
CHECK(static_cast<int>(points.size()) <= scatter::dab_most);
|
||||
CHECK(std::ranges::all_of(points, finite));
|
||||
CHECK(std::isfinite(scatter::dab_wanted(settings)));
|
||||
CHECK(std::isfinite(scatter::dab_step(settings)));
|
||||
CHECK(scatter::dab_step(settings) > 0.0);
|
||||
// one of them at a time, the others as the panel gives them
|
||||
scatter::dab_settings wide;
|
||||
wide.mode = mode;
|
||||
wide.radius = bad;
|
||||
scatter::stroke wide_stroke;
|
||||
auto const wide_points{scatter::dab({0.0, 0.0}, wide, wide_stroke, random)};
|
||||
CHECK(static_cast<int>(wide_points.size()) <= scatter::dab_most);
|
||||
CHECK(std::ranges::all_of(wide_points, finite));
|
||||
}
|
||||
}
|
||||
// a place of the cursor which isn't one puts nothing
|
||||
scatter::dab_settings plain;
|
||||
scatter::stroke nowhere;
|
||||
CHECK(scatter::dab({nan, 0.0}, plain, nowhere, random).empty());
|
||||
CHECK(scatter::dab({0.0, inf}, plain, nowhere, random).empty());
|
||||
CHECK(nowhere.dabs.empty());
|
||||
// the turn and the scale
|
||||
for (auto const bad : {std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity()})
|
||||
{
|
||||
CAPTURE(bad);
|
||||
scatter::look_settings look;
|
||||
for (auto const turn : {scatter::vary::fixed, scatter::vary::random})
|
||||
{
|
||||
for (auto const size : {scatter::vary::fixed, scatter::vary::random})
|
||||
{
|
||||
look.turn = turn;
|
||||
look.size = size;
|
||||
look.angles = glm::vec3{bad};
|
||||
look.angle_range = glm::vec3{bad};
|
||||
look.scale = glm::vec3{bad};
|
||||
look.scale_range = glm::vec3{bad};
|
||||
for (auto const same : {true, false})
|
||||
{
|
||||
look.same_scale = same;
|
||||
auto const picked{scatter::pick(look, random)};
|
||||
CHECK(std::isfinite(picked.angles.x + picked.angles.y + picked.angles.z));
|
||||
CHECK(std::isfinite(picked.scale.x + picked.scale.y + picked.scale.z));
|
||||
CHECK(picked.scale.x >= 0.01f);
|
||||
CHECK(picked.scale.y >= 0.01f);
|
||||
CHECK(picked.scale.z >= 0.01f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// along a line
|
||||
std::vector<glm::dvec3> const line{{0.0, 0.0, 0.0}, {50.0, 1.0, 0.0}};
|
||||
for (auto const mode : {scatter::line_fill::regular, scatter::line_fill::irregular})
|
||||
{
|
||||
for (auto const bad : {nan, inf, -inf})
|
||||
{
|
||||
CAPTURE(bad);
|
||||
scatter::line_settings settings;
|
||||
settings.mode = mode;
|
||||
settings.width = bad;
|
||||
settings.spacing = bad;
|
||||
settings.jitter = bad;
|
||||
settings.density = bad;
|
||||
settings.edge_scale = bad;
|
||||
auto const places{scatter::along(line, settings, random)};
|
||||
CHECK(static_cast<int>(places.size()) <= scatter::line_most);
|
||||
for (auto const &place : places)
|
||||
{
|
||||
CHECK(finite(place.point));
|
||||
CHECK(std::isfinite(place.height));
|
||||
CHECK(std::isfinite(place.side));
|
||||
}
|
||||
CHECK(std::isfinite(scatter::side_scale(settings, 0.5)));
|
||||
CHECK(std::isfinite(scatter::side_scale(settings, bad)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("scatter: within the ranges of the panel the bounds of the settings change nothing")
|
||||
{
|
||||
// the same seed, the same places as before the settings were bounded: the grid of a uniform dab is a matter of the settings alone
|
||||
scatter::dab_settings settings;
|
||||
settings.mode = scatter::fill::uniform;
|
||||
settings.radius = 100.0;
|
||||
settings.spacing = 50.0;
|
||||
std::mt19937 random{1}; // NOSONAR(cpp:S2245) a repeatable test, not security
|
||||
scatter::stroke stroke;
|
||||
auto const points{scatter::dab({0.0, 0.0}, settings, stroke, random)};
|
||||
// rows 43.3 m apart, the points 50 m apart in them, every other row shifted by 25 m: 19 within 100 m of the middle
|
||||
CHECK(points.size() == 19);
|
||||
scatter::look_settings look;
|
||||
look.turn = scatter::vary::fixed;
|
||||
look.angles = {-180.0f, 180.0f, 90.0f};
|
||||
look.size = scatter::vary::fixed;
|
||||
look.scale = {1.0f, 1000.0f, 100.0f};
|
||||
auto const picked{scatter::pick(look, random)};
|
||||
CHECK(picked.angles == glm::vec3{-180.0f, 180.0f, 90.0f});
|
||||
CHECK(picked.scale.x == doctest::Approx(0.01f));
|
||||
CHECK(picked.scale.y == doctest::Approx(10.0f));
|
||||
CHECK(picked.scale.z == doctest::Approx(1.0f));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user