18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

fix(editor): a stroke of the brush leaves the density set on the ground it goes over

The density of the random and the feathered fills was counted for the whole circle of every dab. The
circles of a stroke overlap (a dab every half of the radius), so each place got its share from every
dab that went over it: 5 models a 100 m² set came to some 15 on the ground, and more wherever the
cursor came back. The count was also rounded for each dab, so a circle too small for half a model
(1 m at 4 a 100 m²) never got any.

A stroke now keeps the circles of its dabs beside the places of its models (scatter::stroke). A dab
draws as many places as the density asks for in its circle and keeps each with the chance of what
the ground there is short of: nothing where an earlier dab of the stroke has filled it, so the
overlaps and the way back add no more. The count is the whole part of what is asked for and one more
with the chance of its fraction, so small circles and low densities get their share over the stroke.
The feathered fill is as dense as set in the middle of the stroke and thins out towards its edge;
a single dab of it used to squeeze the whole count of the circle into its middle.

The uniform fill (the grid) is as it was. The tooltip of the density says what it counts.
This commit is contained in:
miloszunio
2026-11-30 12:20:57 +00:00
parent 71bd8d95f4
commit cb3ac22a8d
7 changed files with 244 additions and 51 deletions
+2 -1
View File
@@ -46,6 +46,7 @@ http://mozilla.org/MPL/2.0/.
#include "editor/editorKilometrage.hpp"
#include "editor/editorSpeed.hpp"
#include "editor/editorRoad.hpp"
#include "editor/editorScatter.hpp"
#include "editor/editorProfile.hpp"
#include "editor/editorGroundFollow.hpp"
#include "editor/editorInfra.hpp"
@@ -264,7 +265,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri
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
std::vector<glm::dvec2> m_brush_taken; // the places the stroke put models on (x, z)
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
bool m_draw_cursor{false}; // the cursor drawn into the picture (View menu), on only while the editor is
+5 -5
View File
@@ -78,7 +78,7 @@ void editor_mode::brush_press()
{
mouseHold = true;
m_brush_has_last = false;
m_brush_taken.clear();
m_brush_done = {};
auto const &settings{ui()->brush()};
if (settings.way != brush_object_list::kind::single && settings.stroke == brush_object_list::shape::line)
{
@@ -138,14 +138,14 @@ void editor_mode::brush_dab()
oldPos = centre;
m_brush_has_last = true;
auto &stroke{m_history.back()};
auto const points{scatter::dab({centre.x, centre.z}, settings, m_brush_taken, m_brush_random)};
if (static_cast<int>(points.size()) >= scatter::dab_most)
// 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)};
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)
{
auto *model{place_brush_model({point.x, brush_height(point, centre.y), point.y})};
// the place is taken either way, so a model which failed isn't tried there on every move
m_brush_taken.push_back(point);
if (model == nullptr)
continue;
EditorSnapshot::array_copy copy;
+2 -1
View File
@@ -652,7 +652,8 @@ void brush_object_list::render_dab()
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))
dab.density = density;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("How many models a 100 m² the circle gets, as many as the spacing lets in"));
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") :
STR_C("How many models a 100 m² the ground under the stroke gets, as many as the spacing lets in.\nGround the stroke has gone over gets no more when the cursor comes back over it"));
}
void brush_object_list::render_line()
+97 -27
View File
@@ -6,6 +6,7 @@
#include <cmath>
#include <iterator>
#include <numbers>
#include <optional>
namespace scatter
{
@@ -242,6 +243,88 @@ std::vector<line_place> irregular_line(polyline const &Line, line_settings const
return made;
}
// the share of the full density a dab leaves at a place, Share of its radius from its middle: all of it anywhere in the circle
// for the random fill, falling from all in the middle to none at the edge for the feathered one
double dab_weight(fill const Mode, double const Share)
{
if (Share >= 1.0)
return 0.0;
if (Mode != fill::feathered)
return 1.0;
auto const inner{1.0 - Share * Share};
return inner * inner;
}
// a number of tries with the Expected count on average: its whole part, and one more with the chance of its fraction. rounded
// instead, a small circle or a low density would get nothing from any of the dabs
int tries_for(double const Expected, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
{
auto const whole{std::floor(Expected)};
std::uniform_real_distribution unit{0.0, 1.0};
auto const more{unit(Random) < Expected - whole ? 1.0 : 0.0};
return static_cast<int>(std::min(static_cast<double>(dab_most), whole + more));
}
// the ground of a dab the stroke hasn't filled yet: the dab adds only what its circle is short of
class fresh_ground
{
public:
fresh_ground(glm::dvec2 const &Centre, double const Radius, fill const Mode, std::vector<dab_circle> const &Dabs) : m_centre{Centre}, m_radius{Radius}, m_mode{Mode}
{
// only the circles which reach into this one
std::ranges::copy_if(Dabs, std::back_inserter(m_before), [&](dab_circle const &Dab) { return glm::distance(Dab.centre, Centre) < Radius + Dab.radius; });
}
// the places of the models, Spacing from each other and from the Nearby ones at least. Count of them are drawn evenly over
// the whole circle (as many as the density asks for in it), each kept with the chance of what the ground there is short of:
// the density comes out the same wherever the circles of the stroke overlap, however many of them do
std::vector<glm::dvec2> places(int const Count, double const Spacing, std::vector<glm::dvec2> const &Nearby, std::mt19937 &Random) const // NOSONAR(cpp:S2245) placement of scenery models, not security
{
std::vector<glm::dvec2> made;
for (int i = 0; i < Count; ++i)
{
if (auto const place{spaced_place(Spacing, Nearby, made, Random)}; place.has_value())
made.push_back(*place);
}
return made;
}
private:
// what the earlier dabs left at the Point already, as a share of the full density
double filled(glm::dvec2 const &Point) const
{
auto most{0.0};
for (auto const &dab : m_before)
most = std::max(most, dab_weight(m_mode, glm::distance(Point, dab.centre) / dab.radius));
return most;
}
// one place drawn in the circle and kept with the chance of what the ground there is short of; one too close to the
// others is drawn again, a number of times before it's given up for the spacing
std::optional<glm::dvec2> spaced_place(double const Spacing, std::vector<glm::dvec2> const &Nearby, std::vector<glm::dvec2> const &Made, std::mt19937 &Random) const // NOSONAR(cpp:S2245) placement of scenery models, not security
{
std::uniform_real_distribution unit{0.0, 1.0};
int constexpr tries_each{30};
for (int attempt = 0; attempt < tries_each; ++attempt)
{
// even over the area: the square root of the share of the radius
auto const share{std::sqrt(unit(Random))};
auto const angle{2.0 * std::numbers::pi * unit(Random)};
glm::dvec2 const point{m_centre.x + m_radius * share * std::cos(angle), m_centre.y + m_radius * share * std::sin(angle)};
if (unit(Random) >= dab_weight(m_mode, share) - filled(point))
return std::nullopt;
if (clear_of(point, Spacing, Nearby, Made))
return point;
}
return std::nullopt;
}
glm::dvec2 m_centre;
double m_radius;
fill m_mode;
std::vector<dab_circle> m_before;
};
} // 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
@@ -263,43 +346,30 @@ float side_scale(line_settings const &Settings, double const Side)
return static_cast<float>(std::lerp(1.0, edge, std::clamp(Side, 0.0, 1.0)));
}
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settings, std::vector<glm::dvec2> const &Taken, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
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
{
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(Taken, std::back_inserter(nearby), [&](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; });
if (Settings.mode == fill::uniform)
return grid_points(Centre, radius, spacing, nearby);
// as many as the density asks for in the circle (no more than a dab can put at once), each tried a number of times before
// it's given up for the spacing
auto const wanted{std::min(dab_most, static_cast<int>(std::lround(std::max(0.0, Settings.density) * std::numbers::pi * radius * radius / 100.0)))};
std::uniform_real_distribution unit{0.0, 1.0};
std::ranges::copy_if(Stroke.taken, std::back_inserter(nearby), [&](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; });
std::vector<glm::dvec2> made;
int constexpr tries_each{30};
auto attempts{wanted * tries_each};
while (attempts > 0 && static_cast<int>(made.size()) < wanted)
{
--attempts;
// even over the area: the square root of the share of the radius
auto const share{std::sqrt(unit(Random))};
auto const angle{2.0 * std::numbers::pi * unit(Random)};
if (Settings.mode == fill::feathered)
{
// fewer towards the edge: kept with the chance falling from 1 in the middle to 0 at the edge
auto const keep{(1.0 - share * share) * (1.0 - share * share)};
if (unit(Random) > keep)
continue;
}
glm::dvec2 const point{Centre.x + radius * share * std::cos(angle), Centre.y + radius * share * std::sin(angle)};
if (clear_of(point, spacing, nearby, made))
made.push_back(point);
}
if (Settings.mode == fill::uniform)
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());
Stroke.dabs.emplace_back(Centre, radius);
return made;
}
double dab_wanted(dab_settings const &Settings)
{
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)
{
return std::max({0.05, Settings.spacing, 0.5 * Settings.radius});
+26 -3
View File
@@ -24,15 +24,38 @@ struct dab_settings
double radius{5.0}; // m
fill mode{fill::random};
double spacing{2.0}; // m: the least distance between two models; the step of the grid of the uniform fill
double density{4.0}; // models a 100 m² for the random and the feathered fills, as much as the spacing lets
// models a 100 m² of the ground the stroke goes over, as much as the spacing lets: for the random fill everywhere under the
// circle, for the feathered one in the middle of the stroke, fewer and fewer towards its edge
double density{4.0};
};
// the circle a dab of the stroke has filled already
struct dab_circle
{
glm::dvec2 centre;
double radius;
};
// what a stroke of the brush has done so far. the circles of its dabs overlap, and the cursor may come back over them: the
// ground a dab has filled gets no more from the next ones, so it holds as many models as the density says, not that many
// from each dab that went over it
struct stroke
{
std::vector<glm::dvec2> taken; // the places of the models put (x, z)
std::vector<dab_circle> dabs; // the circles filled
};
// the most models one dab puts, whatever the fill: a dab wider or denser than that is cut short
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
// already Taken (by the stroke so far) at least
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settings, std::vector<glm::dvec2> const &Taken, std::mt19937 &Random); // NOSONAR(cpp:S2245) placement of scenery models, not security
// 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
// 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
double dab_wanted(dab_settings const &Settings);
// the distance the cursor goes before the next dab: the circles of two dabs overlap by half
double dab_step(dab_settings const &Settings);
+5 -2
View File
@@ -14355,8 +14355,11 @@ msgstr "Gęstość##brush"
msgid "%.1f per 100 m²"
msgstr "%.1f na 100 m²"
msgid "How many models a 100 m² the circle gets, as many as the spacing lets in"
msgstr "Ile modeli na 100 m² dostaje okrąg, o ile pozwala na to odstęp"
msgid "How many models a 100 m² the ground under the stroke gets, as many as the spacing lets in.\nGround the stroke has gone over gets no more when the cursor comes back over it"
msgstr "Ile modeli na 100 m² dostaje teren pod pociągnięciem, o ile pozwala na to odstęp.\nTeren, po którym pociągnięcie już przeszło, nie dostaje więcej, gdy kursor na niego wraca"
msgid "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"
msgstr "Ile modeli na 100 m² dostaje środek pociągnięcia, coraz mniej ku jego brzegowi, o ile pozwala na to odstęp.\nTeren, po którym pociągnięcie już przeszło, nie dostaje więcej, gdy kursor na niego wraca"
msgid "Turn##brush"
msgstr "Obrót##brush"
+107 -12
View File
@@ -29,9 +29,14 @@ TEST_CASE("scatter: the random fill keeps to the circle and the spacing, as dens
settings.radius = 10.0;
settings.spacing = 1.5;
settings.density = 5.0;
auto const points{scatter::dab({100.0, -50.0}, settings, {}, random)};
// 5 a 100 m² over 314 m²: 16, the spacing lets them all in
CHECK(points.size() == 16);
scatter::stroke stroke;
auto const points{scatter::dab({100.0, -50.0}, settings, stroke, random)};
// 5 a 100 m² over 314 m²: 15.7, so 15 or 16, the spacing lets them all in
CHECK(points.size() >= 15);
CHECK(points.size() <= 16);
// the dab is added to the stroke
CHECK(stroke.taken.size() == points.size());
CHECK(stroke.dabs.size() == 1);
for (auto const &p : points)
CHECK(glm::distance(p, glm::dvec2{100.0, -50.0}) <= 10.0 + 1e-9);
CHECK(closest_pair(points) >= 1.5 - 1e-9);
@@ -44,8 +49,9 @@ TEST_CASE("scatter: a dab keeps clear of what the stroke put before")
settings.radius = 4.0;
settings.spacing = 2.0;
settings.density = 30.0;
auto const first{scatter::dab({0.0, 0.0}, settings, {}, random)};
auto const second{scatter::dab({2.0, 0.0}, settings, first, random)};
scatter::stroke stroke;
auto const first{scatter::dab({0.0, 0.0}, settings, stroke, random)};
auto const second{scatter::dab({2.0, 0.0}, settings, stroke, random)};
for (auto const &p : second)
for (auto const &q : first)
CHECK(glm::distance(p, q) >= 2.0 - 1e-9);
@@ -58,17 +64,16 @@ TEST_CASE("scatter: the uniform fill is a grid of the world, a second dab over t
settings.mode = scatter::fill::uniform;
settings.radius = 5.0;
settings.spacing = 2.0;
auto const first{scatter::dab({0.3, 0.2}, settings, {}, random)};
scatter::stroke stroke;
auto const first{scatter::dab({0.3, 0.2}, settings, stroke, random)};
CHECK(first.size() > 15);
CHECK(closest_pair(first) == doctest::Approx(2.0));
auto taken{first};
auto const second{scatter::dab({2.3, 0.2}, settings, taken, random)};
auto const second{scatter::dab({2.3, 0.2}, settings, stroke, random)};
for (auto const &p : second)
for (auto const &q : first)
CHECK(glm::distance(p, q) > 1.9);
// the same place again: nothing new
taken.insert(taken.end(), second.begin(), second.end());
CHECK(scatter::dab({0.3, 0.2}, settings, taken, random).empty());
CHECK(scatter::dab({0.3, 0.2}, settings, stroke, random).empty());
}
TEST_CASE("scatter: the feathered fill thins out towards the edge")
@@ -79,7 +84,8 @@ TEST_CASE("scatter: the feathered fill thins out towards the edge")
settings.radius = 20.0;
settings.spacing = 0.1;
settings.density = 40.0;
auto const points{scatter::dab({0.0, 0.0}, settings, {}, random)};
scatter::stroke stroke;
auto const points{scatter::dab({0.0, 0.0}, settings, stroke, random)};
int inner{0};
int outer{0};
for (auto const &p : points)
@@ -94,6 +100,94 @@ TEST_CASE("scatter: the feathered fill thins out towards the edge")
CHECK(inner > 2 * outer);
}
TEST_CASE("scatter: a stroke leaves the density asked for on the ground it went over, however much its dabs overlap")
{
std::mt19937 random{21}; // NOSONAR(cpp:S2245) a repeatable test, not security
scatter::dab_settings settings;
settings.radius = 10.0;
settings.spacing = 0.2;
settings.density = 5.0;
scatter::stroke stroke;
// 400 m straight on, a dab every step of the brush (half the radius): each place under four circles or so
std::size_t count{0};
for (int i = 0; i * scatter::dab_step(settings) <= 400.0; ++i)
count += scatter::dab({i * scatter::dab_step(settings), 0.0}, settings, stroke, random).size();
// the ground gone over: 400 m by 20 m and the two half circles at the ends, 8314 m², so 416 models
auto const asked{settings.density * (400.0 * 20.0 + 3.14159265 * 100.0) / 100.0};
CHECK(count > 0.85 * asked);
CHECK(count < 1.15 * asked);
// as dense at the end of the stroke as at its start: the 100 m in the middle of each half hold a quarter of the models
auto const within = [&stroke](double const From, double const To) { return std::ranges::count_if(stroke.taken, [&](glm::dvec2 const &Point) { return Point.x >= From && Point.x < To; }); };
CHECK(within(50.0, 150.0) > 0.75 * settings.density * 20.0);
CHECK(within(50.0, 150.0) < 1.25 * settings.density * 20.0);
CHECK(within(250.0, 350.0) > 0.75 * settings.density * 20.0);
CHECK(within(250.0, 350.0) < 1.25 * settings.density * 20.0);
// and across it: the middle half of its width holds half of them, not more
auto const middle{std::ranges::count_if(stroke.taken, [](glm::dvec2 const &Point) { return Point.x >= 50.0 && Point.x < 350.0 && std::abs(Point.y) < 5.0; })};
auto const whole{within(50.0, 350.0)};
CHECK(middle > 0.4 * whole);
CHECK(middle < 0.6 * whole);
}
TEST_CASE("scatter: the cursor back over the ground the stroke has filled adds nothing there")
{
std::mt19937 random{22}; // NOSONAR(cpp:S2245) a repeatable test, not security
scatter::dab_settings settings;
settings.radius = 8.0;
settings.spacing = 0.2;
settings.density = 20.0;
scatter::stroke stroke;
for (int i = 0; i <= 20; ++i)
scatter::dab({4.0 * i, 0.0}, settings, stroke, random);
auto const there{stroke.taken.size()};
CHECK(there > 200);
// all the way back, between the places of the dabs made on the way there
for (int i = 19; i >= 0; --i)
CHECK(scatter::dab({4.0 * i + 2.0, 0.0}, settings, stroke, random).size() <= 2);
// next to nothing: only the slivers at the edges between two circles of the way there
CHECK(stroke.taken.size() < there + there / 20);
}
TEST_CASE("scatter: a circle too small for a whole model a dab still gets its share over the stroke")
{
std::mt19937 random{23}; // NOSONAR(cpp:S2245) a repeatable test, not security
scatter::dab_settings settings;
settings.radius = 1.0;
settings.spacing = 0.2;
settings.density = 4.0;
// 0.13 of a model in the circle: rounded, no dab would ever put one
CHECK(scatter::dab_wanted(settings) < 0.5);
scatter::stroke stroke;
for (int i = 0; i <= 4000; ++i)
scatter::dab({0.5 * i, 0.0}, settings, stroke, random);
// 2 km by 2 m: 160 models
CHECK(stroke.taken.size() > 120);
CHECK(stroke.taken.size() < 200);
}
TEST_CASE("scatter: the feathered fill of a stroke is as dense as asked for in its middle, thinner towards its edges")
{
std::mt19937 random{24}; // NOSONAR(cpp:S2245) a repeatable test, not security
scatter::dab_settings settings;
settings.mode = scatter::fill::feathered;
settings.radius = 10.0;
settings.spacing = 0.1;
settings.density = 20.0;
scatter::stroke stroke;
for (int i = 0; i <= 80; ++i)
scatter::dab({5.0 * i, 0.0}, settings, stroke, random);
auto const band = [&stroke](double const From, double const To) {
return static_cast<double>(std::ranges::count_if(stroke.taken, [&](glm::dvec2 const &Point) { return Point.x >= 50.0 && Point.x < 350.0 && std::abs(Point.y) >= From && std::abs(Point.y) < To; }));
};
// the metre each side of the middle of the stroke, 600 m²: nearly the 20 a 100 m² asked for (the dabs are 5 m apart, the
// feather of each takes a little off between them)
CHECK(band(0.0, 1.0) > 0.75 * 120.0);
CHECK(band(0.0, 1.0) < 1.15 * 120.0);
// fewer and fewer outwards
CHECK(band(4.0, 5.0) < band(0.0, 1.0));
CHECK(band(8.0, 9.0) < 0.5 * band(4.0, 5.0));
}
TEST_CASE("scatter: the turn and the scale of a model")
{
std::mt19937 random{5}; // NOSONAR(cpp:S2245) a repeatable test, not security
@@ -143,7 +237,8 @@ TEST_CASE("scatter: a uniform dab too wide for its spacing stops at the limit at
settings.radius = 100.0;
settings.spacing = 0.1;
// some 3.6 million points of the grid in the circle: cut short at the limit, not gone through
auto const points{scatter::dab({0.0, 0.0}, settings, {}, random)};
scatter::stroke stroke;
auto const points{scatter::dab({0.0, 0.0}, settings, stroke, random)};
CHECK(static_cast<int>(points.size()) == scatter::dab_most);
}